more cleanup
This commit is contained in:
parent
d06336d5fe
commit
fab8722157
|
@ -8,7 +8,6 @@
|
|||
#ifndef INC_COMMON_DEFS_H_
|
||||
#define INC_COMMON_DEFS_H_
|
||||
|
||||
#define N_CELLS 15
|
||||
#define N_TEMP_SENSORS 31
|
||||
#define N_BMS 2
|
||||
|
||||
#endif /* INC_COMMON_DEFS_H_ */
|
|
@ -8,10 +8,10 @@
|
|||
#ifndef ADBMS_LL_DRIVER_H_
|
||||
#define ADBMS_LL_DRIVER_H_
|
||||
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include <stdint.h>
|
||||
#define TARGET_STM32
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#ifdef TARGET_STM32
|
||||
typedef uint8_t uint8;
|
||||
typedef uint16_t uint16;
|
||||
|
@ -19,13 +19,6 @@ typedef uint32_t uint32;
|
|||
#endif
|
||||
|
||||
uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi);
|
||||
uint8 calculateCommandPEC(uint8* data, uint8 datalen);
|
||||
uint16 updateCommandPEC(uint16 currentPEC, uint8 din);
|
||||
uint8 checkCommandPEC(uint8* data, uint8 datalen);
|
||||
|
||||
uint8 calculateDataPEC(uint8* data, uint8 datalen);
|
||||
uint16 updateDataPEC(uint16 currentPEC, uint8 din);
|
||||
uint8 checkDataPEC(uint8* data, uint8 datalen);
|
||||
|
||||
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen);
|
||||
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen);
|
||||
|
@ -34,11 +27,7 @@ uint8 pollCMD(uint16 command);
|
|||
void mcuAdbmsCSLow();
|
||||
void mcuAdbmsCSHigh();
|
||||
|
||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize);
|
||||
uint8 mcuSPIReceive(uint8* buffer, uint8 buffersize);
|
||||
uint8 mcuSPITransmitReceive(uint8* rxbuffer, uint8* txbuffer, uint8 buffersize);
|
||||
|
||||
uint8 wakeUpCmd();
|
||||
void mcuDelay(uint16 delay);
|
||||
static inline void mcuDelay(uint16 delay) { HAL_Delay(delay); };
|
||||
|
||||
#endif /* ADBMS_LL_DRIVER_H_ */
|
||||
|
|
|
@ -33,13 +33,7 @@ extern uint8_t numberofCells;
|
|||
extern uint8_t numberofAux;
|
||||
|
||||
void AMS_Init(SPI_HandleTypeDef* hspi);
|
||||
void AMS_Loop();
|
||||
|
||||
uint8_t AMS_Balancing_Loop();
|
||||
uint8_t AMS_Idle_Loop();
|
||||
uint8_t AMS_Warning_Loop();
|
||||
uint8_t AMS_Error_Loop();
|
||||
uint8_t AMS_Charging_Loop();
|
||||
uint8_t AMS_Discharging_Loop();
|
||||
|
||||
#endif /* INC_AMS_HIGHLEVEL_H_ */
|
||||
|
|
|
@ -6,7 +6,10 @@
|
|||
*/
|
||||
|
||||
#include "ADBMS_LL_Driver.h"
|
||||
#include "ADBMS_CMD_MAKROS.h"
|
||||
#include "config_ADBMS6830.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define INITIAL_COMMAND_PEC 0x0010
|
||||
#define INITIAL_DATA_PEC 0x0010
|
||||
|
@ -14,6 +17,7 @@
|
|||
#warning ask about the timeout value
|
||||
|
||||
SPI_HandleTypeDef* adbmsspi;
|
||||
uint8_t command_queue[N_BMS][12] = {0};
|
||||
|
||||
uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi) {
|
||||
mcuAdbmsCSLow();
|
||||
|
@ -23,53 +27,34 @@ uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
uint8 rxbuf[buffersize];
|
||||
status = HAL_SPI_TransmitReceive(adbmsspi, buffer, rxbuf, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
__HAL_SPI_CLEAR_OVRFLAG(adbmsspi);
|
||||
return status;
|
||||
}
|
||||
|
||||
uint8 mcuSPIReceive(uint8* buffer, uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
status = HAL_SPI_Receive(adbmsspi, buffer, buffersize, ADBMS_SPI_TIMEOUT);
|
||||
return status;
|
||||
}
|
||||
|
||||
uint8 mcuSPITransmitReceive(uint8* rxbuffer, uint8* txbuffer,
|
||||
uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
status = HAL_SPI_TransmitReceive(adbmsspi, txbuffer, rxbuffer, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
return status;
|
||||
}
|
||||
|
||||
//command PEC calculation
|
||||
//CRC-15
|
||||
//x^15 + x^14 + x^10 + x^8 + x^7 + x^4 + x^3 + 1
|
||||
|
||||
uint8 calculateCommandPEC(uint8_t* data, uint8_t datalen) {
|
||||
uint16 currentpec = INITIAL_COMMAND_PEC;
|
||||
if (datalen >= 3) {
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
|
||||
data[datalen - 2] = (currentpec >> 7) & 0xFF;
|
||||
data[datalen - 1] = (currentpec << 1) & 0xFF;
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8 checkCommandPEC(uint8* data, uint8 datalen) {
|
||||
if (datalen <= 3) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
uint16 currentpec = INITIAL_COMMAND_PEC;
|
||||
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
|
||||
uint8 pechigh = (currentpec >> 7) & 0xFF;
|
||||
uint8 peclow = (currentpec << 1) & 0xFF;
|
||||
|
||||
if ((pechigh == data[datalen - 2]) && (peclow == data[datalen - 1])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16 updateCommandPEC(uint16 currentPEC, uint8 din) {
|
||||
static uint16 updateCommandPEC(uint16 currentPEC, uint8 din) {
|
||||
din = (din >> 7) & 0x01;
|
||||
uint8 in0 = din ^ ((currentPEC >> 14) & 0x01);
|
||||
uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
|
||||
|
@ -100,11 +85,53 @@ uint16 updateCommandPEC(uint16 currentPEC, uint8 din) {
|
|||
return newPEC;
|
||||
}
|
||||
|
||||
static uint8 calculateCommandPEC(uint8_t* data, uint8_t datalen) {
|
||||
uint16 currentpec = INITIAL_COMMAND_PEC;
|
||||
if (datalen >= 3) {
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
|
||||
data[datalen - 2] = (currentpec >> 7) & 0xFF;
|
||||
data[datalen - 1] = (currentpec << 1) & 0xFF;
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8 checkCommandPEC(uint8* data, uint8 datalen) {
|
||||
if (datalen <= 3) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
uint16 currentpec = INITIAL_COMMAND_PEC;
|
||||
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
|
||||
uint8 pechigh = (currentpec >> 7) & 0xFF;
|
||||
uint8 peclow = (currentpec << 1) & 0xFF;
|
||||
|
||||
if ((pechigh == data[datalen - 2]) && (peclow == data[datalen - 1])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//data PEC calculation
|
||||
//CRC-10
|
||||
//x^10 + x^7 + x^3 + x^2 + x + 1
|
||||
|
||||
uint16_t pec10_calc(bool rx_cmd, int len, uint8_t* data) {
|
||||
static uint16_t pec10_calc(bool rx_cmd, int len, uint8_t* data) {
|
||||
uint16_t remainder = 16; /* PEC_SEED; 0000010000 */
|
||||
uint16_t polynom = 0x8F; /* x10 + x7 + x3 + x2 + x + 1 <- the CRC15 polynomial
|
||||
100 1000 1111 48F */
|
||||
|
@ -145,9 +172,9 @@ uint16_t pec10_calc(bool rx_cmd, int len, uint8_t* data) {
|
|||
}
|
||||
|
||||
typedef uint16_t crc;
|
||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes);
|
||||
static crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes);
|
||||
|
||||
uint8 calculateDataPEC(uint8_t* data, uint8_t datalen) {
|
||||
static uint8 calculateDataPEC(uint8_t* data, uint8_t datalen) {
|
||||
|
||||
if (datalen >= 3) {
|
||||
|
||||
|
@ -166,7 +193,7 @@ uint8 calculateDataPEC(uint8_t* data, uint8_t datalen) {
|
|||
}
|
||||
}
|
||||
|
||||
uint8 checkDataPEC(uint8* data, uint8 len) {
|
||||
static uint8 checkDataPEC(uint8* data, uint8 len) {
|
||||
if (len <= 2) {
|
||||
return 255;
|
||||
}
|
||||
|
@ -192,7 +219,7 @@ static crc F_CRC_ObtenValorDeTabla(uint8_t VP_Pos_Tabla) {
|
|||
}
|
||||
return ((VP_CRCTableValue));
|
||||
}
|
||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
|
||||
static crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
|
||||
crc VP_CRCTableValue = 16;
|
||||
int16_t VP_bytes = 0;
|
||||
|
||||
|
@ -211,7 +238,7 @@ crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
|
|||
return (VP_CRCTableValue ^ 0x0000);
|
||||
}
|
||||
|
||||
uint16 updateDataPEC(uint16 currentPEC, uint8 din) {
|
||||
static uint16 updateDataPEC(uint16 currentPEC, uint8 din) {
|
||||
din = (din >> 7) & 0x01;
|
||||
uint8 in0 = din ^ ((currentPEC >> 9) & 0x01);
|
||||
uint8 in2 = in0 ^ ((currentPEC >> 1) & 0x01);
|
||||
|
@ -321,28 +348,3 @@ void mcuAdbmsCSLow() {
|
|||
void mcuAdbmsCSHigh() {
|
||||
//HAL_GPIO_WritePin(CSB_GPIO_Port, CSB_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
uint8 rxbuf[buffersize];
|
||||
status = HAL_SPI_TransmitReceive(adbmsspi, buffer, rxbuf, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
__HAL_SPI_CLEAR_OVRFLAG(adbmsspi);
|
||||
return status;
|
||||
}
|
||||
|
||||
uint8 mcuSPIReceive(uint8* buffer, uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
status = HAL_SPI_Receive(adbmsspi, buffer, buffersize, ADBMS_SPI_TIMEOUT);
|
||||
return status;
|
||||
}
|
||||
|
||||
uint8 mcuSPITransmitReceive(uint8* rxbuffer, uint8* txbuffer,
|
||||
uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
status = HAL_SPI_TransmitReceive(adbmsspi, txbuffer, rxbuffer, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
return status;
|
||||
}
|
||||
|
||||
inline void mcuDelay(uint16 delay) { HAL_Delay(delay); }
|
||||
|
|
|
@ -49,53 +49,6 @@ void AMS_Init(SPI_HandleTypeDef* hspi) {
|
|||
currentAMSState = AMSIDLE;
|
||||
}
|
||||
|
||||
void AMS_Loop() {
|
||||
|
||||
// On Transition Functions called ones if the State Changed
|
||||
|
||||
if (currentAMSState != lastAMSState) {
|
||||
switch (currentAMSState) {
|
||||
case AMSIDLE:
|
||||
break;
|
||||
case AMSDEACTIVE:
|
||||
break;
|
||||
case AMSCHARGING:
|
||||
break;
|
||||
case AMSIDLEBALANCING:
|
||||
break;
|
||||
case AMSDISCHARGING:
|
||||
break;
|
||||
case AMSWARNING:
|
||||
break;
|
||||
case AMSERROR:
|
||||
break;
|
||||
}
|
||||
lastAMSState = currentAMSState;
|
||||
}
|
||||
|
||||
// Main Loops for different AMS States
|
||||
|
||||
switch (currentAMSState) {
|
||||
case AMSIDLE:
|
||||
AMS_Idle_Loop();
|
||||
break;
|
||||
case AMSDEACTIVE:
|
||||
break;
|
||||
case AMSCHARGING:
|
||||
break;
|
||||
case AMSIDLEBALANCING:
|
||||
AMS_Idle_Loop();
|
||||
break;
|
||||
case AMSDISCHARGING:
|
||||
break;
|
||||
case AMSWARNING:
|
||||
AMS_Warning_Loop();
|
||||
break;
|
||||
case AMSERROR:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t AMS_Idle_Loop() {
|
||||
if (!amsWakeUp()) {
|
||||
//error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT; //we don't receive data for the wakeup command
|
||||
|
@ -181,16 +134,3 @@ uint8_t AMS_Idle_Loop() {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t AMS_Warning_Loop() { return 0; }
|
||||
|
||||
uint8_t AMS_Error_Loop() { return 0; }
|
||||
|
||||
uint8_t AMS_Charging_Loop() { return 0; }
|
||||
|
||||
uint8_t AMS_Discharging_Loop() { return 0; }
|
||||
|
||||
uint8_t AMS_Balancing_Loop() {
|
||||
//TODO: implement
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue