//
// Created by ilya on 20.06.24.
//

#ifndef AMPLIFIER_AW87579_LIST_H
#define AMPLIFIER_AW87579_LIST_H
#include "Amplifier_AW87579_enum.h"
/*
 * Default data
 */

#define AW87579_ID_DEFAULT 0x5A
#define AW87579_SYSCTRL_DEFAULT 0x38
#define AW87579_BATSAFE_DEFAULT 0x09
#define AW87579_BSTOVR_DEFAULT 0x0C
#define AW87579_BSTCPR2_DEFAULT 0x08
#define AW87579_PAGR_DEFAULT 0x10
#define AW87579_PAGC3OPR_DEFAULT 0x43
#define AW87579_PAGC3PR_DEFAULT 0x4E
#define AW87579_PAGC2OPR_DEFAULT 0x03
#define AW87579_PAGC2PR_DEFAULT 0x08
#define AW87579_PAGC1PR_DEFAULT 0x4A
#define AW87579_ADP_MODE_DEFAULT  0x03
#define AW87579_ADPBST_TIME1_DEFAULT 0xDD
#define AW87579_ADPBST_VTH_DEFAULT 0x23

/*
 * List
 */
    #define def_id 0
    #define def_sysctr 1
    #define def_batsafe 2
    #define def_bstvout 3
    #define def_bstcpr2 5
    #define def_pagr 6
    #define def_pagc3Opr 7
    #define def_pagc3Pr 8
    #define def_pagc2Opr 9
    #define def_pagc2Pr 10
    #define def_pagc1Pr 11
    #define def_adpMode 12
    #define def_adpbstTime1 13
    #define def_adpbstVth 14


// CHIP ID REGISTER
typedef struct {
    uint8_t IDCODE:7; // Chip ID will be returned after reading.
}tAW87579_ChipId;


// SYSTEM CONTROL (SYSCTR) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        bool EN_HVBAT: 1; // Enable high voltage battery
        uint8_t res: 1;
        bool RCV_MODE: 1; // Receiver mode
        bool EN_PA: 1; // PA Enable.
        bool EN_BOOST: 1; //Boost Enable.
        uint8_t : 1;
        bool EN_SW: 1; //Chip software Enable
        uint8_t : 1;
    };
}tAW87579_SYSCTR;
#define SYSCTR_SET_RESERV(RES) RES = 0b1


// BATTERY SAFEGUARD (BATSAFE) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eBAT_SFGD_LEVEL BAT_SFGD_LEVEL: 2; // Maxim output level when enter into battery safe_guard mode
        bool EN_BAT_SFGD: 1; // Software control battery safeguard
        eBAT_SFGD_VTH BAT_SFGD_VTH: 2; // Battery voltage when enter into battery safe_guard mode.
        eBAT_SFGD_DEGLITCH BAT_SFGD_DEGLITCH: 2; // Battery safe guard deglitch time.
        uint8_t : 1;
    };
}tAW87579_BATSAFE;


// BOOST OUTPUT VOLTAGE (BSTVOUT) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eBST_VOUT BST_VOUT: 5; // BOOST output voltage set
        uint8_t : 3;
    };
}tAW87579_BSTVOUT;


// BOOSTBOOST CONTROL (BSTCPR2)REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eBST_IPEAK BST_IPEAK: 4; // BOOST peak current limit
        uint8_t : 4;
    };
}tAW87579_BSTCPR2;


// CLASS D GAIN CONTROL (PAGR) REGISTER
/*
 * PA Input SignalGain
 *  when RCV_MODE=1 PA_GAIN must be set to 00000~00111
 *  (0dB~9dB)
 *  when RCV_MODE=0 PA_GAIN must be set to 01000~10010
 *  (12dB~27dB)
 */
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        uint8_t PA_GAIN: 5;
        uint8_t : 3;
    };
}tAW87579_PAGR;

#define PA_GAIN_SET__dB_RCV_MODE_EN(DB) (DB<1.5?0b00000:(DB>10.5?0b00111:(uint8_t)(DB/1.5)))
#define PA_GAIN_SET__dB_RCV_MODE_DIS(DB) (DB<12?0b01000:(DB>27?0b10010:(uint8_t)(DB/1.5)))
#define PA_GAIN_SET__dB(RCV_MODE,DB) RCV_MODE == false?PA_GAIN_SET__dB_RCV_MODE_DIS(DB):PA_GAIN_SET__dB_RCV_MODE_EN(DB)


// CLASS D AGC3 OUTPUT POWER (AGC3_Po)(PAGC3OPR) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        AGC3_OUTPUT_POWER AGC3_OUTPUT_POWER: 4; // Speaker Protection output Power Level
        bool PD_AGC3: 1; // Disable AGC3
        uint8_t res: 3;
    };
}tAW87579_PAGC3OPR;
#define PAGC3OPR_SET_RESERV(RES) RES = 0b010



// CLASS D AGC3 PARAMETER REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        uint8_t res: 2;
        eAGC3_ATT_TIME AGC3_ATT_TIME: 3; // Total 13.5dB attack time
        eAGC3_REL_TIME AGC3_REL_TIME: 3; // Total 13.5dB release time
    };
}tAW87579_PAGC3PR;
#define PAGC3PR_SET_RESERV(RES) RES = 0b10



// CLASS D AGC2 OUTPUT POWER (AGC2_Po) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        AGC2_OUTPUT_POWER AGC2_OUTPUT_POWER: 4; // Speaker Protection output Power Level
        uint8_t res: 4;
    };
}tAW87579_PAGC2OPR;



// CLASS D AGC2 PARAMETER REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        uint8_t : 2;
        eAGC2_ATT_TIME AGC2_ATT_TIME: 3; // AGC2 total attack time
        uint8_t : 3;
    };
}tAW87579_PAGC2PR;


// CLASS D AGC1 PARAMETER (AGC1) REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        bool PD_AGC1: 1; // Disable fastest Level AGC
        eAGC1_ATT_TIME AGC1_ATT_TIME: 2; //Fastest Level AGC attack time control
        uint8_t res: 5;
    };
}tAW87579_PAGC1PR;
#define PAGC1PR_SET_RESERV(RES) RES = 0b01001


// ADP MODE PARAMETER REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eADPBOOST_MODE ADPBOOST_MODE: 3; // BOOST MODE
        bool AGC1_ATT_TIMEA: 1; // Fastest Levl AGC attack time control, Refer to 0x0B register
        uint8_t : 4;
    };
}tAW87579_ADP_MODE;


// ADP BOOST PARAMETER REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eADP_BST_TIME ADP_BST_TIME1: 4; // Duration time Po less than Po1
        eADP_BST_TIME ADP_BST_TIME2: 4; // Duration time Po less than Po2
    };
}tAW87579_ADPBST_TIME1;


// ADP BOOST PARAMETER REGISTER
typedef union {
    uint8_t data:8;
    struct __attribute__((packed)){
        eSET_BOOST_VTH1 SET_BOOST_VTH1: 3; // Boost threshold Po1
        eSET_BOOST_VTH2 SET_BOOST_VTH2: 3; // Boost threshold Po2
        eADP_LOW_STEP ADP_LOW_STEP: 2; // ADP BOOST mode2 first step set BOOST_OUT<4:0>
    };

}tAW87579_ADPBST_VTH;

// AW87579 ALL LIST
typedef struct __attribute__((packed)) {
    tAW87579_SYSCTR sysctr;
    tAW87579_BATSAFE batsafe;
    tAW87579_BSTVOUT bstvout;
    tAW87579_BSTCPR2 bstcpr2;
    tAW87579_PAGR pagr;
    tAW87579_PAGC3OPR pagc3Opr;
    tAW87579_PAGC3PR pagc3Pr;
    tAW87579_PAGC2OPR pagc2Opr;
    tAW87579_PAGC2PR pagc2Pr;
    tAW87579_PAGC1PR pagc1Pr;
    tAW87579_ADP_MODE adpMode;
    tAW87579_ADPBST_TIME1 adpbstTime1;
    tAW87579_ADPBST_VTH adpbstVth;
}tAW87579_RegsList;

#endif //AMPLIFIER_AW87579_LIST_H
