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

#ifndef INC_ADBMS_ABSTRACTION_H_
#define INC_ADBMS_ABSTRACTION_H_

#include "ADBMS_CMD_MAKROS.h"
#include "ADBMS_LL_Driver.h"
#include "main.h"


#define MAXIMUM_CELL_VOLTAGES 16
#define MAXIMUM_AUX_VOLTAGES  10
#define MAXIMUM_GPIO          10

//see table 103 in datasheet (page 71)
#define DEFAULT_UV 417	//VUV * 16 * 150 uV + 1.5 V     Default Setting 2.5V
#define DEFAULT_OV 1125 //VOV * 16 * 150 uV + 1.5 V		  Default Setting 4.2V

#define mV_from_ADBMS6830(x) (((((int16_t) (x))) * 0.150) + 1500)

struct ADBMS6830_Internal_Status {
  uint16 CS_FLT : 16; //ADC fault - mismatch between S- and C-ADC
  uint16 : 3;
  uint16 CCTS : 13; //Conversion counter
  uint16 VA_OV : 1; //5V analog supply overvoltage
  uint16 VA_UV : 1; //5V analog supply undervoltage
  uint16 VD_OV : 1; //3V digital supply overvoltage
  uint16 VD_UV : 1; //3V digital supply undervoltage
  uint16 CED : 1; //C-ADC single trim error (correctable)
  uint16 CMED : 1; //C-ADC multiple trim error (uncorrectable)
  uint16 SED : 1; //S-ADC single trim error (correctable)
  uint16 SMED : 1; //S-ADC multiple trim error (uncorrectable)
  uint16 VDEL : 1; //Latent supply voltage error
  uint16 VDE : 1; //Supply voltage error
  uint16 COMPARE : 1; //Comparasion between S- and C-ADC active
  uint16 SPIFLT : 1; //SPI fault
  uint16 SLEEP : 1; //Sleep mode previously entered
  uint16 THSD : 1; //Thermal shutdown
  uint16 TMODCHK : 1; //Test mode check
  uint16 OSCCHK : 1; //Oscillator check
};

typedef struct {
  int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES];
  int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES];

  struct ADBMS6830_Internal_Status status;
  uint16 internalDieTemp;
  uint16 analogSupplyVoltage;
  uint16 digitalSupplyVoltage;
  uint16 sumOfCellMeasurements;
  uint16 refVoltage;

  uint16 GPIO_Values[MAXIMUM_GPIO];

  uint32 overVoltage;
  uint32 underVoltage;

} Cell_Module;

uint8 amsReset();

uint8 initAMS(SPI_HandleTypeDef* hspi, uint8 numofcells, uint8 numofaux);
uint8 amsWakeUp();

uint8 amsCellMeasurement(Cell_Module* module);
uint8 amsConfigCellMeasurement(uint8 numberofChannels);

uint8 amsAuxAndStatusMeasurement(Cell_Module* module);
uint8 amsConfigAuxMeasurement(uint16 Channels);

uint8 amsConfigGPIO(uint16 gpios);
uint8 amsSetGPIO(uint16 gpios);
uint8 readGPIO(Cell_Module* module);

uint8 amsConfigBalancing(uint32 Channels, uint8 dutyCycle);
uint8 amsStartBalancing(uint8 dutyCycle);
uint8 amsStopBalancing();

uint8 amsSelfTest();

uint8 amsConfigOverUnderVoltage(uint16 overVoltage, uint16 underVoltage);

uint8 amsCheckUnderOverVoltage(Cell_Module* module);
uint8 amsConfigOverVoltage(uint16 overVoltage);

uint8 amscheckOpenCellWire(Cell_Module* module);

uint8 amsClearStatus();
uint8 amsClearAux();
uint8 amsClearCells();

uint8 amsSendWarning();
uint8 amsSendError();

uint8 amsClearWarning();
uint8 amsClearError();

uint8 amsReadCellVoltages(Cell_Module* module);

#endif /* INC_ADBMS_ABSTRACTION_H_ */
