/*
 * ADBMS_CMD_MAKROS.h
 *
 *  Created on: 14.07.2022
 *      Author: max
 */

#ifndef INC_ADBMS_CMD_MAKROS_H_
#define INC_ADBMS_CMD_MAKROS_H_

#include <stdint.h>
#define WRCFGA 0x0001 // Write Configuration Register Group A
#define RDCFGA 0x0002 // Read Configuration Register Group A
#define WRCFGB 0x0024 // Write Configuration Register Group B
#define RDCFGB 0x0026 // Read Configuration Register Group B

#define WRPWMA 0x0020 // Write PWM Register Group A
#define RDPWMA 0x0022 // Read PWM Register Group A
#define WRPWMB 0x0021 // Write PWM Register Group B
#define RDPWMB 0x0023 // Read PWM Register Group B

#define RDCVA 0x0004 // Read Cell Voltage Register Group A
#define RDCVB 0x0006 // Read Cell Voltage Register Group B
#define RDCVC 0x0008 // Read Cell Voltage Register Group C
#define RDCVD 0x000A // Read Cell Voltage Register Group D
#define RDCVE 0x0009 // Read Cell Voltage Register Group E
#define RDCVF 0x000B // Read Cell Voltage Register Group F
#define RDCVALL 0x000C // Read All Cell Voltage Register Groups

#define RDACA 0x0044 // Read averaged Cell Voltage Register Group A
#define RDACB 0x0046 // Read averaged Cell Voltage Register Group B
#define RDACC 0x0048 // Read averaged Cell Voltage Register Group C
#define RDACD 0x004A // Read averaged Cell Voltage Register Group D
#define RDACE 0x0049 // Read averaged Cell Voltage Register Group E
#define RDACF 0x004B // Read averaged Cell Voltage Register Group F
#define RDACALL 0x004C // Read averaged All Cell Voltage Register Groups

#define RDAUXA 0x0019 // Read Auxilliary Register Group A
#define RDAUXB 0x001A // Read Auxilliary Register Group B
#define RDAUXC 0x001B // Read Auxilliary Register Group C
#define RDAUXD 0x001F // Read Auxilliary Register Group D

#define RDAUXALL 0x0035 // Read All Auxilliary and Status Register Groups

#define RDSTATA 0x0030 // Read Status Register Group A
#define RDSTATB 0x0031 // Read Status Register Group B
#define RDSTATC 0x0032 // Read Status Register Group C
#define RDSTATD 0x0033 // Read Status Register Group D
#define RDSTATE 0x0034 // Read Status Register Group E

#define ADCV 0x0260 // Start Cell Voltage Conversion with C-ADC
#define ADCV_OW_0 (1u << 0)
#define ADCV_OW_1 (1u << 1)
#define ADCV_RSTF (1u << 2)
#define ADCV_DCP  (1u << 4)
#define ADCV_CONT (1u << 7) // Continuous Mode
#define ADCV_RD   (1u << 8) // Redundancy Mode

#define ADSV 0x0168 // Start Cell Voltage Conversion with S-ADC
#define ADSV_OW_0 (1u << 0)
#define ADSV_OW_1 (1u << 1)
#define ADSV_DCP (1u << 4)
#define ADSV_CONT (1u << 7) // Continuous Mode

#define ADAX 0x0410 // Start GPIOs and Vref2 Conversion
#define ADAX_CONV_ALL 0x0000 // Convert all GPIOs, VREF2, VD, VA, ITEMP
#define ADAX_OW (1u << 8)

#define CLRCELL 0x0711 // Clear Cell Voltage Register Groups
#define CLRAUX  0x0712 // Clear Auxiliary Register Groups
#define CLOVUV 0x0715 // Clear Overvoltage and Undervoltage Flags
#define CLRFLAG 0x0717 // Clear all Flags

#define PLADC   0x0718 // Poll ADC Conversion Status
#define PLAUX   0x071E // Poll AUX Conversion Status

#define SRST   0x0027 //Soft reset

#define DIAGN   0x0715 // Diagnos MUX and Poll Status
#define WRCOMM  0x0721 // Write COMM Register Group
#define RDCOMM  0x0722 // Read COMM Register Group
#define STCOMM  0x0723 // Start I2C/SPI Communication
#define MUTE    0x0028 // Mute Discharge
#define UNMUTE  0x0029 // Unmute Discharge



/* GPIO Selection for ADC Converion
 * 000: GPIO1 to 5, 2nd Reference, GPIO 6 to 9
 * 001: GPIO1 and GPIO6
 * 010 GPIO2 and GPIO7
 * 011 GPIO3 and GPIO8
 * 100 GPIO4 and GPIO9
 * 101 GPIO5
 * 110 2nd Reference
 */

#define CHG000 (0x00)
#define CHG001 (0x01)
#define CHG010 (0x02)
#define CHG011 (0x03)
#define CHG100 (0x04)
#define CHG101 (0x05)
#define CHG110 (0x06)

/* Status Group Selection
 * 000: SC,ITMP,VA,VD
 * 001: SC
 * 010: ITMP
 * 011: VA
 * 100: VD
 */

#define CHST000 (0x00)
#define CHST001 (0x01)
#define CHST010 (0x02)
#define CHST011 (0x03)
#define CHST100 (0x04)

#define PEC_FIELD_SIZE 2

#define CFG_GROUP_A_SIZE 6
#define CFG_GROUP_B_SIZE 6

#define PWM_GROUP_A_SIZE 6
#define PWM_GROUP_B_SIZE 2

#define CV_GROUP_A_SIZE  6
#define CV_GROUP_B_SIZE  6
#define CV_GROUP_C_SIZE  6
#define CV_GROUP_D_SIZE  6
#define CV_GROUP_E_SIZE  6
#define CV_GROUP_F_SIZE  6

#define AUX_GROUP_A_SIZE 6
#define AUX_GROUP_B_SIZE 6
#define AUX_GROUP_C_SIZE 6
#define AUX_GROUP_D_SIZE 6

#define STATUS_GROUP_A_SIZE        6
#define STATUS_GROUP_B_SIZE        6
#define STATUS_GROUP_C_SIZE        6
#define STATUS_GROUP_D_SIZE        6
#define STATUS_GROUP_E_SIZE        6
#define COMM_GROUP_SIZE            6
#define S_CONTROL_GROUP_SIZE       6
#define PWM_GROUP_SIZE             6
#define PWM_S_CONTROL_GROUP_B_SIZE 6

#define CFG_GROUP_A_ID 1
#define CFG_GROUP_B_ID 2
#define CV_GROUP_A_ID  3
#define CV_GROUP_B_ID  4
#define CV_GROUP_C_ID  5
#define CV_GROUP_D_ID  6
#define CV_GROUP_E_ID  7
#define CV_GROUP_F_ID  8

#define AUX_GROUP_A_ID 9
#define AUX_GROUP_B_ID 10
#define AUX_GROUP_C_ID 11
#define AUX_GROUP_D_ID 12

#define STATUS_GROUP_A_ID        13
#define STATUS_GROUP_B_ID        14
#define COMM_GROUP_ID            15
#define S_CONTROL_GROUP_ID       16
#define PWM_GROUP_ID             17
#define PWM_S_CONTROL_GROUP_B_ID 18

#endif /* INC_ADBMS_CMD_MAKROS_H_ */
