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

#ifndef INC_ADBMS_ABSTRACTION_H_
#define INC_ADBMS_ABSTRACTION_H_

#include "ADBMS_Driver.h"

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

// Macro to convert voltage in mV to the internal format for under/over voltage thresholds.
// Calculation: internal = (mV - 1500) / (16 * 0.15) = (mV - 1500) / 2.4.
// To perform integer arithmetic with rounding, we compute:
//   internal = ((mV - 1500) * 5 + 6) / 12
#define mV_to_ADBMS6830(mV) ((((mV) - 1500) * 5 + 6) / 12)

HAL_StatusTypeDef amsReset();

HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi);
HAL_StatusTypeDef amsWakeUp();

HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]);

HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]);

HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint8_t dutyCycle);
HAL_StatusTypeDef amsStartBalancing();
HAL_StatusTypeDef amsStopBalancing();

HAL_StatusTypeDef amsSelfTest();

HAL_StatusTypeDef amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t underVoltage); //arguments in mV

HAL_StatusTypeDef amsCheckUnderOverVoltage(Cell_Module (*module)[N_BMS]);

HAL_StatusTypeDef amsClearFlag();
HAL_StatusTypeDef amsClearAux();
HAL_StatusTypeDef amsClearOVUV();

HAL_StatusTypeDef amsReadCellVoltages(Cell_Module (*module)[N_BMS]);

#endif /* INC_ADBMS_ABSTRACTION_H_ */
