copy files over from slave
This commit is contained in:
246
Core/Src/ADBMS_Abstraction.c
Normal file
246
Core/Src/ADBMS_Abstraction.c
Normal file
@ -0,0 +1,246 @@
|
||||
/*
|
||||
* ADBMS_Abstraction.c
|
||||
*
|
||||
* Created on: 14.07.2022
|
||||
* Author: max
|
||||
*/
|
||||
|
||||
#include "ADBMS_Abstraction.h"
|
||||
#include "ADBMS_CMD_MAKROS.h"
|
||||
#include "ADBMS_LL_Driver.h"
|
||||
#include <stddef.h>
|
||||
|
||||
uint8 numberofcells;
|
||||
uint8 numberofauxchannels;
|
||||
|
||||
#define CHECK_RETURN(x) \
|
||||
{ \
|
||||
uint8 status = x; \
|
||||
if (status != 0) \
|
||||
return status; \
|
||||
}
|
||||
|
||||
uint8 amsReset() {
|
||||
amsWakeUp();
|
||||
readCMD(SRST, NULL, 0);
|
||||
mcuDelay(10);
|
||||
amsWakeUp();
|
||||
amsStopBalancing();
|
||||
amsConfigOverUnderVoltage(DEFAULT_OV, DEFAULT_UV);
|
||||
|
||||
uint8 buffer[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
CHECK_RETURN(writeCMD(CLRFLAG, buffer, 6)); //clear flags,
|
||||
CHECK_RETURN(writeCMD(CLOVUV, buffer, 6)); //OVUV flags
|
||||
CHECK_RETURN(writeCMD(ADCV | ADCV_CONT | ADCV_RD, NULL, 0)); //start continuous cell voltage measurement with redundancy
|
||||
CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, NULL, 0)); //start aux measurement
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8 initAMS(SPI_HandleTypeDef* hspi, uint8 numofcells, uint8 numofaux) {
|
||||
adbmsDriverInit(hspi);
|
||||
numberofcells = numofcells;
|
||||
numberofauxchannels = numofaux;
|
||||
|
||||
return amsReset();
|
||||
}
|
||||
|
||||
uint8 amsWakeUp() {
|
||||
uint8 buf[6];
|
||||
return readCMD(RDCFGA, buf, 6);
|
||||
}
|
||||
|
||||
uint8 amsCellMeasurement(Cell_Module* module) {
|
||||
#warning check conversion counter to ensure that continous conversion has not been stopped
|
||||
#warning check for OW conditions: ADSV | ADSV_OW_0 / ADSV_OW_1
|
||||
return amsReadCellVoltages(module);
|
||||
}
|
||||
|
||||
uint8 amsConfigCellMeasurement(uint8 numberofChannels) {
|
||||
numberofcells = numberofChannels;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8 amsAuxAndStatusMeasurement(Cell_Module* module) {
|
||||
uint8 rxbuf[AUX_GROUP_A_SIZE] = {};
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATC, rxbuf, STATUS_GROUP_C_SIZE));
|
||||
|
||||
module->status.CS_FLT = rxbuf[0] | (rxbuf[1] << 8);
|
||||
module->status.CCTS = rxbuf[2] | (rxbuf[3] << 8);
|
||||
module->status.VA_OV = (rxbuf[4] >> 7) & 0x01;
|
||||
module->status.VA_UV = (rxbuf[4] >> 6) & 0x01;
|
||||
module->status.VD_OV = (rxbuf[4] >> 5) & 0x01;
|
||||
module->status.VD_UV = (rxbuf[4] >> 4) & 0x01;
|
||||
module->status.CED = (rxbuf[4] >> 3) & 0x01;
|
||||
module->status.CMED = (rxbuf[4] >> 2) & 0x01;
|
||||
module->status.SED = (rxbuf[4] >> 1) & 0x01;
|
||||
module->status.SMED = (rxbuf[4] >> 0) & 0x01;
|
||||
module->status.VDEL = (rxbuf[5] >> 7) & 0x01;
|
||||
module->status.VDE = (rxbuf[5] >> 6) & 0x01;
|
||||
module->status.COMPARE= (rxbuf[5] >> 5) & 0x01;
|
||||
module->status.SPIFLT = (rxbuf[5] >> 4) & 0x01;
|
||||
module->status.SLEEP = (rxbuf[5] >> 3) & 0x01;
|
||||
module->status.THSD = (rxbuf[5] >> 2) & 0x01;
|
||||
module->status.TMODCHK= (rxbuf[5] >> 1) & 0x01;
|
||||
module->status.OSCCHK = (rxbuf[5] >> 0) & 0x01;
|
||||
|
||||
if (pollCMD(PLAUX) == 0x0) { //TODO: check for SPI fault
|
||||
return 0; // aux ADC data not ready
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXA, rxbuf, AUX_GROUP_A_SIZE));
|
||||
|
||||
module->auxVoltages[0] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
|
||||
module->auxVoltages[1] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
|
||||
module->auxVoltages[2] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXB, rxbuf, AUX_GROUP_A_SIZE));
|
||||
|
||||
module->auxVoltages[3] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
|
||||
module->auxVoltages[4] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
|
||||
module->auxVoltages[5] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXC, rxbuf, AUX_GROUP_A_SIZE));
|
||||
|
||||
module->auxVoltages[6] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
|
||||
module->auxVoltages[7] = mV_from_ADBMS6830(rxbuf[2] | (rxbuf[3] << 8));
|
||||
module->auxVoltages[8] = mV_from_ADBMS6830(rxbuf[4] | (rxbuf[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXD, rxbuf, AUX_GROUP_A_SIZE));
|
||||
|
||||
module->auxVoltages[9] = mV_from_ADBMS6830(rxbuf[0] | (rxbuf[1] << 8));
|
||||
|
||||
uint8 rxbuffer[STATUS_GROUP_A_SIZE];
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATA, rxbuffer, STATUS_GROUP_A_SIZE));
|
||||
|
||||
module->internalDieTemp = rxbuffer[2] | (rxbuffer[3] << 8);
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATB, rxbuffer, STATUS_GROUP_B_SIZE));
|
||||
module->digitalSupplyVoltage = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->analogSupplyVoltage = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->refVoltage = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, NULL, 0)); //start aux measurement for next cycle
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8 amsConfigBalancing(uint32 channels, uint8 dutyCycle) {
|
||||
uint8 buffer_a[PWM_GROUP_A_SIZE] = {};
|
||||
uint8 buffer_b[PWM_GROUP_B_SIZE] = {};
|
||||
CHECK_RETURN(readCMD(RDPWMA, buffer_a, CFG_GROUP_A_SIZE));
|
||||
CHECK_RETURN(readCMD(RDPWMB, buffer_b, CFG_GROUP_B_SIZE));
|
||||
|
||||
if (dutyCycle > 0x0F) { // there are only 4 bits for duty cycle
|
||||
return 1;
|
||||
}
|
||||
|
||||
#warning fixme
|
||||
|
||||
for (size_t i = 0; i < 16; i += 2) {
|
||||
if (i < 12) { // cells 0, 1 are in regbuffer[0], cells 2, 3 in regbuffer[1], ...
|
||||
buffer_a[i / 2] = ((channels & (1 << (i + 1))) ? (dutyCycle << 4) : 0) |
|
||||
((channels & (1 << i)) ? dutyCycle : 0);
|
||||
} else {
|
||||
buffer_b[(i - 12) / 2] = ((channels & (1 << (i + 1))) ? (dutyCycle << 4) : 0) |
|
||||
((channels & (1 << i)) ? dutyCycle : 0);
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_RETURN(writeCMD(WRPWMA, buffer_a, CFG_GROUP_A_SIZE));
|
||||
CHECK_RETURN(writeCMD(WRPWMB, buffer_b, CFG_GROUP_B_SIZE));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8 amsStartBalancing(uint8 dutyCycle) { return writeCMD(UNMUTE, NULL, 0); }
|
||||
|
||||
uint8 amsStopBalancing() { return writeCMD(MUTE, NULL, 0); }
|
||||
|
||||
uint8 amsSelfTest() { return 0; }
|
||||
|
||||
uint8 amsConfigOverUnderVoltage(uint16 overVoltage, uint16 underVoltage) {
|
||||
uint8 buffer[CFG_GROUP_A_SIZE];
|
||||
|
||||
if (underVoltage & 0xF000 || overVoltage & 0xF000) { // only 12 bits allowed
|
||||
return 1;
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDCFGB, buffer, CFG_GROUP_A_SIZE));
|
||||
|
||||
//UV
|
||||
buffer[0] = (uint8) (underVoltage & 0xFF);
|
||||
buffer[1] &= 0xF0;
|
||||
buffer[1] |= (uint8) ((underVoltage >> 8) & 0x0F);
|
||||
|
||||
//OV
|
||||
buffer[1] &= 0x0F;
|
||||
buffer[1] |= (uint8) (overVoltage << 4);
|
||||
buffer[2] = (uint8) (overVoltage >> 4);
|
||||
|
||||
return writeCMD(WRCFGB, buffer, CFG_GROUP_A_SIZE);
|
||||
}
|
||||
|
||||
uint8 amsCheckUnderOverVoltage(Cell_Module* module) {
|
||||
uint8 regbuffer[STATUS_GROUP_D_SIZE];
|
||||
uint32 ov_uv_data = 0;
|
||||
CHECK_RETURN(readCMD(RDSTATD, regbuffer, STATUS_GROUP_D_SIZE));
|
||||
ov_uv_data = (regbuffer[0] << 0) | (regbuffer[1] << 8) |
|
||||
(regbuffer[2] << 16) | (regbuffer[3] << 24);
|
||||
|
||||
module->overVoltage = 0;
|
||||
module->underVoltage = 0;
|
||||
|
||||
for (size_t i = 0; i < numberofcells; i++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ...
|
||||
module->underVoltage |= (ov_uv_data >> (i * 2)) & 0x01;
|
||||
module->overVoltage |= (ov_uv_data >> (i * 2 + 1)) & 0x01;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8 amsClearAux() {
|
||||
uint8 buffer[6];
|
||||
return writeCMD(CLRAUX, buffer, 0);
|
||||
}
|
||||
|
||||
uint8 amsClearCells() {
|
||||
uint8 buffer[6];
|
||||
return writeCMD(CLRCELL, buffer, 0);
|
||||
}
|
||||
|
||||
uint8 amsReadCellVoltages(Cell_Module* module) {
|
||||
uint8 rxbuffer[CV_GROUP_A_SIZE];
|
||||
CHECK_RETURN(readCMD(RDCVA, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[0] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->cellVoltages[1] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->cellVoltages[2] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDCVB, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[3] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->cellVoltages[4] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->cellVoltages[5] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDCVC, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[6] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->cellVoltages[7] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->cellVoltages[8] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDCVD, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[9] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->cellVoltages[10] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->cellVoltages[11] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDCVE, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[12] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
module->cellVoltages[13] = mV_from_ADBMS6830(rxbuffer[2] | (rxbuffer[3] << 8));
|
||||
module->cellVoltages[14] = mV_from_ADBMS6830(rxbuffer[4] | (rxbuffer[5] << 8));
|
||||
|
||||
CHECK_RETURN(readCMD(RDCVF, rxbuffer, CV_GROUP_A_SIZE));
|
||||
module->cellVoltages[15] = mV_from_ADBMS6830(rxbuffer[0] | (rxbuffer[1] << 8));
|
||||
|
||||
return 0;
|
||||
}
|
||||
347
Core/Src/ADBMS_LL_Driver.c
Normal file
347
Core/Src/ADBMS_LL_Driver.c
Normal file
@ -0,0 +1,347 @@
|
||||
/*
|
||||
* ADBMS_LL_Driver.c
|
||||
*
|
||||
* Created on: 05.06.2022
|
||||
* Author: max
|
||||
*/
|
||||
|
||||
#include "ADBMS_LL_Driver.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define INITIAL_COMMAND_PEC 0x0010
|
||||
#define INITIAL_DATA_PEC 0x0010
|
||||
#define ADBMS_SPI_TIMEOUT 100 // Timeout in ms
|
||||
#warning ask about the timeout value
|
||||
|
||||
SPI_HandleTypeDef* adbmsspi;
|
||||
|
||||
uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi) {
|
||||
mcuAdbmsCSLow();
|
||||
HAL_Delay(1);
|
||||
mcuAdbmsCSHigh();
|
||||
adbmsspi = hspi;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//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) {
|
||||
din = (din >> 7) & 0x01;
|
||||
uint8 in0 = din ^ ((currentPEC >> 14) & 0x01);
|
||||
uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
|
||||
uint8 in4 = in0 ^ ((currentPEC >> 3) & 0x01);
|
||||
uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
|
||||
uint8 in8 = in0 ^ ((currentPEC >> 7) & 0x01);
|
||||
uint8 in10 = in0 ^ ((currentPEC >> 9) & 0x01);
|
||||
uint8 in14 = in0 ^ ((currentPEC >> 13) & 0x01);
|
||||
|
||||
uint16 newPEC = 0;
|
||||
|
||||
newPEC |= in14 << 14;
|
||||
newPEC |= (currentPEC & (0x01 << 12)) << 1;
|
||||
newPEC |= (currentPEC & (0x01 << 11)) << 1;
|
||||
newPEC |= (currentPEC & (0x01 << 10)) << 1;
|
||||
newPEC |= in10 << 10;
|
||||
newPEC |= (currentPEC & (0x01 << 8)) << 1;
|
||||
newPEC |= in8 << 8;
|
||||
newPEC |= in7 << 7;
|
||||
newPEC |= (currentPEC & (0x01 << 5)) << 1;
|
||||
newPEC |= (currentPEC & (0x01 << 4)) << 1;
|
||||
newPEC |= in4 << 4;
|
||||
newPEC |= in3 << 3;
|
||||
newPEC |= (currentPEC & (0x01 << 1)) << 1;
|
||||
newPEC |= (currentPEC & (0x01)) << 1;
|
||||
newPEC |= in0;
|
||||
|
||||
return newPEC;
|
||||
}
|
||||
|
||||
//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) {
|
||||
uint16_t remainder = 16; /* PEC_SEED; 0000010000 */
|
||||
uint16_t polynom = 0x8F; /* x10 + x7 + x3 + x2 + x + 1 <- the CRC15 polynomial
|
||||
100 1000 1111 48F */
|
||||
|
||||
/* Perform modulo-2 division, a byte at a time. */
|
||||
for (uint8_t pbyte = 0; pbyte < len; ++pbyte) {
|
||||
/* Bring the next byte into the remainder. */
|
||||
remainder ^= (uint16_t)(data[pbyte] << 2);
|
||||
/* Perform modulo-2 division, a bit at a time.*/
|
||||
for (uint8_t bit_ = 8; bit_ > 0; --bit_) {
|
||||
/* Try to divide the current data bit. */
|
||||
if ((remainder & 0x200) >
|
||||
0) // equivalent to remainder & 2^14 simply check for MSB
|
||||
{
|
||||
remainder = (uint16_t)((remainder << 1));
|
||||
remainder = (uint16_t)(remainder ^ polynom);
|
||||
} else {
|
||||
remainder = (uint16_t)(remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rx_cmd == true) {
|
||||
remainder ^= (uint16_t)((data[len] & 0xFC) << 2);
|
||||
/* Perform modulo-2 division, a bit at a time */
|
||||
for (uint8_t bit_ = 6; bit_ > 0; --bit_) {
|
||||
/* Try to divide the current data bit */
|
||||
if ((remainder & 0x200) >
|
||||
0) // equivalent to remainder & 2^14 simply check for MSB
|
||||
{
|
||||
remainder = (uint16_t)((remainder << 1));
|
||||
remainder = (uint16_t)(remainder ^ polynom);
|
||||
} else {
|
||||
remainder = (uint16_t)((remainder << 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return ((uint16_t)(remainder & 0x3FF));
|
||||
}
|
||||
|
||||
typedef uint16_t crc;
|
||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes);
|
||||
|
||||
uint8 calculateDataPEC(uint8_t* data, uint8_t datalen) {
|
||||
|
||||
if (datalen >= 3) {
|
||||
|
||||
|
||||
crc currentpec = pec10_calc(true, datalen - 2, data) & 0x3FF; // mask to 10 bits
|
||||
|
||||
// memory layout is [[zeroes], PEC[9:8]], [PEC[7:0]]
|
||||
data[datalen - 2] = (currentpec >> 8) & 0xFF;
|
||||
data[datalen - 1] = currentpec & 0xFF;
|
||||
|
||||
volatile uint8 result = pec10_calc(true, datalen, data);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8 checkDataPEC(uint8* data, uint8 len) {
|
||||
if (len <= 2) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
crc currentpec = F_CRC_CalculaCheckSum(data, len);
|
||||
|
||||
return (currentpec == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
static crc F_CRC_ObtenValorDeTabla(uint8_t VP_Pos_Tabla) {
|
||||
crc VP_CRCTableValue = 0;
|
||||
uint8_t VP_Pos_bit = 0;
|
||||
|
||||
VP_CRCTableValue = ((crc)(VP_Pos_Tabla)) << (10 - 8);
|
||||
|
||||
for (VP_Pos_bit = 0; VP_Pos_bit < 8; VP_Pos_bit++) {
|
||||
if (VP_CRCTableValue & (((crc)1) << (10 - 1))) {
|
||||
VP_CRCTableValue = (VP_CRCTableValue << 1) ^ 0x8F;
|
||||
} else {
|
||||
VP_CRCTableValue = (VP_CRCTableValue << 1);
|
||||
}
|
||||
}
|
||||
return ((VP_CRCTableValue));
|
||||
}
|
||||
crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
|
||||
crc VP_CRCTableValue = 16;
|
||||
int16_t VP_bytes = 0;
|
||||
|
||||
for (VP_bytes = 0; VP_bytes < VF_nBytes; VP_bytes++) {
|
||||
|
||||
VP_CRCTableValue = (VP_CRCTableValue << 8) ^
|
||||
F_CRC_ObtenValorDeTabla(
|
||||
((uint8_t)((VP_CRCTableValue >> (10 - 8)) & 0xFF)) ^
|
||||
AF_Datos[VP_bytes]);
|
||||
}
|
||||
|
||||
if ((8 * sizeof(crc)) > 10) {
|
||||
VP_CRCTableValue = VP_CRCTableValue & ((((crc)(1)) << 10) - 1);
|
||||
}
|
||||
|
||||
return (VP_CRCTableValue ^ 0x0000);
|
||||
}
|
||||
|
||||
uint16 updateDataPEC(uint16 currentPEC, uint8 din) {
|
||||
din = (din >> 7) & 0x01;
|
||||
uint8 in0 = din ^ ((currentPEC >> 9) & 0x01);
|
||||
uint8 in2 = in0 ^ ((currentPEC >> 1) & 0x01);
|
||||
uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
|
||||
uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
|
||||
|
||||
uint16 newPEC = 0;
|
||||
|
||||
newPEC |= (currentPEC & (0x01 << 8)) << 1;
|
||||
newPEC |= (currentPEC & (0x01 << 7)) << 1;
|
||||
newPEC |= in7 << 7;
|
||||
newPEC |= (currentPEC & (0x01 << 5)) << 1;
|
||||
newPEC |= (currentPEC & (0x01 << 4)) << 1;
|
||||
newPEC |= in3 << 3;
|
||||
newPEC |= in2 << 2;
|
||||
newPEC |= (currentPEC & (0x01)) << 1;
|
||||
newPEC |= in0;
|
||||
|
||||
return newPEC;
|
||||
}
|
||||
|
||||
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen) {
|
||||
uint8 ret;
|
||||
if (arglen > 0) {
|
||||
uint8 buffer[6 + arglen]; //command + PEC (2 bytes) + data + DPEC (2 bytes)
|
||||
buffer[0] = (command >> 8) & 0xFF;
|
||||
buffer[1] = (command) & 0xFF;
|
||||
|
||||
calculateCommandPEC(buffer, 4);
|
||||
|
||||
for (uint8 i = 0; i < arglen; i++) {
|
||||
buffer[4 + i] = args[i];
|
||||
}
|
||||
|
||||
calculateDataPEC(&buffer[4], arglen + 2); //DPEC is calculated over the data, not the command, and placed at the end of the data
|
||||
|
||||
mcuAdbmsCSLow();
|
||||
ret = mcuSPITransmit(buffer, 6 + arglen);
|
||||
mcuAdbmsCSHigh();
|
||||
} else {
|
||||
uint8 buffer[4];
|
||||
buffer[0] = (command >> 8) & 0xFF;
|
||||
buffer[1] = (command) & 0xFF;
|
||||
calculateCommandPEC(buffer, 4);
|
||||
|
||||
mcuAdbmsCSLow();
|
||||
|
||||
ret = mcuSPITransmit(buffer, 4);
|
||||
|
||||
mcuAdbmsCSHigh();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen) {
|
||||
uint8 txbuffer[6 + buflen] = {};
|
||||
uint8 rxbuffer[6 + buflen] = {};
|
||||
|
||||
txbuffer[0] = (command >> 8) & 0xFF;
|
||||
txbuffer[1] = (command)&0xFF;
|
||||
calculateCommandPEC(txbuffer, 4);
|
||||
|
||||
mcuAdbmsCSLow();
|
||||
uint8 status = mcuSPITransmitReceive(rxbuffer, txbuffer, 6 + buflen);
|
||||
mcuAdbmsCSHigh();
|
||||
|
||||
if (status != 0) {
|
||||
return status;
|
||||
}
|
||||
|
||||
for (uint8 i = 0; i < buflen; i++) {
|
||||
buffer[i] = rxbuffer[i + 4];
|
||||
}
|
||||
|
||||
[[maybe_unused]] uint8 commandCounter = rxbuffer[sizeof(rxbuffer) - 2] & 0xFC; //command counter is bits 7-2
|
||||
//TODO: check command counter?
|
||||
|
||||
return checkDataPEC(&rxbuffer[4], buflen + 2);
|
||||
}
|
||||
|
||||
//check poll command - no data PEC sent back
|
||||
uint8 pollCMD(uint16 command) {
|
||||
uint8 txbuffer[5] = {};
|
||||
uint8 rxbuffer[5] = {};
|
||||
|
||||
txbuffer[0] = (command >> 8) & 0xFF;
|
||||
txbuffer[1] = (command)&0xFF;
|
||||
calculateCommandPEC(txbuffer, 4);
|
||||
|
||||
mcuAdbmsCSLow();
|
||||
uint8 status = mcuSPITransmitReceive(rxbuffer, txbuffer, 5);
|
||||
mcuAdbmsCSHigh();
|
||||
|
||||
if (status != 0) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return rxbuffer[4]; //last byte will be poll response
|
||||
}
|
||||
|
||||
void mcuAdbmsCSLow() {
|
||||
//HAL_GPIO_WritePin(CSB_GPIO_Port, CSB_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
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); }
|
||||
113
Core/Src/AMS_CAN.c
Normal file
113
Core/Src/AMS_CAN.c
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* AMS_CAN.c
|
||||
*
|
||||
* Created on: Mar 19, 2022
|
||||
* Author: jasper
|
||||
*/
|
||||
|
||||
#include "AMS_CAN.h"
|
||||
|
||||
#include "ADBMS_Abstraction.h"
|
||||
|
||||
#include "AMS_HighLevel.h"
|
||||
#include "TMP1075.h"
|
||||
#include "common_defs.h"
|
||||
#include "eeprom.h"
|
||||
#include "errors.h"
|
||||
#include "main.h"
|
||||
#include "stm32f3xx.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
#include "stm32f3xx_hal_can.h"
|
||||
|
||||
#include "can-halal.h"
|
||||
#include "stm32f3xx_hal_gpio.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CAN_ID_SLAVE_PANIC 0x009
|
||||
#define CAN_ID_SLAVE_STATUS_BASE 0x080
|
||||
#define CAN_ID_SLAVE_LOG_BASE 0x600
|
||||
|
||||
void ams_can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
|
||||
|
||||
#define ITER_COUNT 10
|
||||
static uint8_t count = 0;
|
||||
static bool isOn = false;
|
||||
|
||||
void ams_can_send_status() {
|
||||
if (count == ITER_COUNT) {
|
||||
HAL_GPIO_WritePin(STATUS_LED_G_GPIO_Port, STATUS_LED_G_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(STATUS_LED_B_GPIO_Port, STATUS_LED_B_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
|
||||
count = 0;
|
||||
isOn = !isOn;
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
|
||||
static uint8_t data[8];
|
||||
int error = error_data.error_sources != 0;
|
||||
data[0] = eeprom_config.id | (error << 7);
|
||||
data[1] = stateofcharge;
|
||||
uint8_t* ptr = &data[2];
|
||||
uint16_t min_volt = 0xFFFF;
|
||||
uint16_t max_volt = 0;
|
||||
for (size_t i = 0; i < numberofCells; i++) {
|
||||
if (module.cellVoltages[i] < min_volt) {
|
||||
min_volt = module.cellVoltages[i];
|
||||
}
|
||||
if (module.cellVoltages[i] > max_volt) {
|
||||
max_volt = module.cellVoltages[i];
|
||||
}
|
||||
}
|
||||
ptr = ftcan_marshal_unsigned(ptr, min_volt, 2);
|
||||
ptr = ftcan_marshal_unsigned(ptr, max_volt, 2);
|
||||
int16_t max_temp = -0x8000;
|
||||
for (size_t i = 0; i < N_TEMP_SENSORS; i++) {
|
||||
if (tmp1075_temps[i] > max_temp &&
|
||||
(tmp1075_failed_sensors & (1 << i)) == 0) {
|
||||
max_temp = tmp1075_temps[i];
|
||||
}
|
||||
}
|
||||
ftcan_marshal_unsigned(ptr, max_temp, 2);
|
||||
uint16_t id = CAN_ID_SLAVE_STATUS_BASE | eeprom_config.id;
|
||||
ftcan_transmit(id, data, sizeof(data));
|
||||
}
|
||||
|
||||
void ams_can_send_error() {
|
||||
static uint8_t data[6];
|
||||
data[0] = eeprom_config.id;
|
||||
data[1] = error_data.data_kind;
|
||||
memcpy(&data[2], error_data.data, 4);
|
||||
ftcan_transmit(CAN_ID_SLAVE_PANIC, data, sizeof(data));
|
||||
}
|
||||
|
||||
void ams_can_send_log() {
|
||||
static uint8_t call_count = 0;
|
||||
static uint8_t data[8];
|
||||
|
||||
uint16_t can_addr =
|
||||
CAN_ID_SLAVE_LOG_BASE | (eeprom_config.id << 4) | call_count;
|
||||
uint8_t* ptr = &data[0];
|
||||
|
||||
if (call_count < N_CELLS / 4) {
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
size_t offset = call_count * 4;
|
||||
ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[offset + i], 2);
|
||||
}
|
||||
ftcan_transmit(can_addr, data, sizeof(data));
|
||||
} else if (call_count == N_CELLS / 4) {
|
||||
// Send last cell & failed temperature sensors
|
||||
ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[N_CELLS - 1], 2);
|
||||
ptr = ftcan_marshal_unsigned(ptr, tmp1075_failed_sensors, 4);
|
||||
ftcan_transmit(can_addr, data, 6);
|
||||
} else {
|
||||
size_t offset = (call_count - N_CELLS / 4 - 1) * 8;
|
||||
for (size_t i = 0; i < 8; i++) {
|
||||
data[i] = tmp1075_temps[offset + i] >> 4;
|
||||
}
|
||||
ftcan_transmit(can_addr, data, sizeof(data));
|
||||
}
|
||||
call_count = (call_count + 1) % (N_CELLS / 4 + 1 + N_TEMP_SENSORS / 8);
|
||||
}
|
||||
348
Core/Src/AMS_HighLevel.c
Normal file
348
Core/Src/AMS_HighLevel.c
Normal file
@ -0,0 +1,348 @@
|
||||
/*
|
||||
* AMS_HighLevel.c
|
||||
*
|
||||
* Created on: 20.07.2022
|
||||
* Author: max
|
||||
*/
|
||||
|
||||
#include "AMS_HighLevel.h"
|
||||
#include "ADBMS_Abstraction.h"
|
||||
#include "ADBMS_LL_Driver.h"
|
||||
#include "AMS_CAN.h"
|
||||
#include "TMP1075.h"
|
||||
#include "can-halal.h"
|
||||
#include "errors.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
#include <stdint.h>
|
||||
|
||||
Cell_Module module = {};
|
||||
uint32_t balancedCells = 0;
|
||||
uint8_t BalancingActive = 0;
|
||||
uint8_t stateofcharge = 100;
|
||||
int64_t currentintegrator = 0;
|
||||
uint32_t lastticks = 0;
|
||||
uint32_t currenttick = 0;
|
||||
uint8_t eepromconfigured = 0;
|
||||
|
||||
uint8_t internalbalancingalgo = 1;
|
||||
uint16_t startbalancingthreshold = 41000;
|
||||
uint16_t stopbalancingthreshold = 30000;
|
||||
uint16_t balancingvoltagedelta = 10;
|
||||
|
||||
uint16_t amsuv = 0;
|
||||
uint16_t amsov = 0;
|
||||
|
||||
uint8_t amserrorcode = 0;
|
||||
uint8_t amswarningcode = 0;
|
||||
|
||||
uint8_t numberofCells = 16;
|
||||
uint8_t numberofAux = 0;
|
||||
|
||||
uint8_t packetChecksumFails = 0;
|
||||
#define MAX_PACKET_CHECKSUM_FAILS 5
|
||||
|
||||
uint8_t deviceSleeps = 0;
|
||||
#define MAX_DEVICE_SLEEP 3 //TODO: change to correct value
|
||||
|
||||
amsState currentAMSState = AMSDEACTIVE;
|
||||
amsState lastAMSState = AMSDEACTIVE;
|
||||
|
||||
struct pollingTimes {
|
||||
uint32_t S_ADC_OW_CHECK;
|
||||
uint32_t TMP1075;
|
||||
};
|
||||
|
||||
struct pollingTimes pollingTimes = {0, 0, 0, 0};
|
||||
|
||||
void AMS_Init(SPI_HandleTypeDef* hspi) {
|
||||
if (eepromconfigured == 1) {
|
||||
/*amsov = eepromcellovervoltage>>4;
|
||||
amsuv = (eepromcellundervoltage-1)>>4;
|
||||
numberofCells = eepromnumofcells;
|
||||
numberofAux = eepromnumofaux;
|
||||
initAMS(hspi, eepromnumofcells, eepromnumofaux);*/
|
||||
amsConfigOverUnderVoltage(amsov, amsuv);
|
||||
} else {
|
||||
initAMS(hspi, numberofCells, numberofAux);
|
||||
amsov = DEFAULT_OV;
|
||||
amsuv = DEFAULT_UV;
|
||||
}
|
||||
|
||||
pollingTimes = (struct pollingTimes) {HAL_GetTick(), HAL_GetTick()};
|
||||
|
||||
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:
|
||||
writeWarningLog(0x01);
|
||||
break;
|
||||
case AMSERROR:
|
||||
writeErrorLog(amserrorcode);
|
||||
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;
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
}
|
||||
|
||||
packetChecksumFails += amsAuxAndStatusMeasurement(&module);
|
||||
|
||||
if (module.status.SLEEP) {
|
||||
deviceSleeps++;
|
||||
if (deviceSleeps > MAX_DEVICE_SLEEP) {
|
||||
error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT;
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
} else {
|
||||
amsReset();
|
||||
}
|
||||
}
|
||||
|
||||
if (module.status.CS_FLT || module.status.SPIFLT || module.status.CMED ||
|
||||
module.status.SMED || module.status.VDE || module.status.VDEL ||
|
||||
module.status.OSCCHK || module.status.TMODCHK) {
|
||||
error_data.data_kind = SEK_INTERNAL_BMS_FAULT;
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
}
|
||||
|
||||
if (module.status.THSD) {
|
||||
error_data.data_kind = SEK_INTERNAL_BMS_OVERTEMP;
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
}
|
||||
|
||||
packetChecksumFails += amsCellMeasurement(&module);
|
||||
packetChecksumFails += amsCheckUnderOverVoltage(&module);
|
||||
packetChecksumFails += integrateCurrent();
|
||||
|
||||
if (packetChecksumFails > MAX_PACKET_CHECKSUM_FAILS) {
|
||||
error_data.data_kind = SEK_INTERNAL_BMS_CHECKSUM_FAIL;
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
}
|
||||
|
||||
tmp1075_measure();
|
||||
|
||||
int any_voltage_error = 0;
|
||||
for (size_t i = 0; i < numberofCells; i++) {
|
||||
if (module.cellVoltages[i] < 2500) {
|
||||
any_voltage_error = 1;
|
||||
error_data.data_kind = SEK_UNDERVOLT;
|
||||
error_data.data[0] = i;
|
||||
uint8_t* ptr = &error_data.data[1];
|
||||
ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[i], 2);
|
||||
} else if (module.cellVoltages[i] > 4200) {
|
||||
any_voltage_error = 1;
|
||||
error_data.data_kind = SEK_OVERVOLT;
|
||||
error_data.data[0] = i;
|
||||
uint8_t* ptr = &error_data.data[1];
|
||||
ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[i], 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (module.internalDieTemp > 28000) { //TODO: change to correct value
|
||||
error_data.data_kind = SEK_INTERNAL_BMS_OVERTEMP;
|
||||
uint8_t* ptr = &error_data.data[0];
|
||||
ptr = ftcan_marshal_unsigned(ptr, module.internalDieTemp, 2);
|
||||
|
||||
set_error_source(ERROR_SOURCE_INTERNAL);
|
||||
} else {
|
||||
clear_error_source(ERROR_SOURCE_INTERNAL);
|
||||
}
|
||||
|
||||
if (any_voltage_error) {
|
||||
set_error_source(ERROR_SOURCE_VOLTAGES);
|
||||
} else {
|
||||
clear_error_source(ERROR_SOURCE_VOLTAGES);
|
||||
}
|
||||
|
||||
mcuDelay(10);
|
||||
|
||||
// static uint32_t channelstobalance = 1;
|
||||
|
||||
// channelstobalance = 0x1FFFF;
|
||||
/* if(channelstobalance & 0x20000){
|
||||
channelstobalance = 1;
|
||||
}*/
|
||||
|
||||
// amsConfigBalancing(channelstobalance);
|
||||
// amsStartBalancing(100);
|
||||
|
||||
if ((module.overVoltage | module.underVoltage)) {
|
||||
// amsSendWarning();
|
||||
// currentAMSState = AMSWARNING;
|
||||
}
|
||||
|
||||
// AMS_Balancing_Loop();
|
||||
|
||||
/* if(BalancingActive)
|
||||
{
|
||||
amsStartBalancing(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
amsStopBalancing();
|
||||
}*/
|
||||
// amsConfigBalancing(balancedCells);
|
||||
// volatile amscheck = amscheckOpenCellWire(&module);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t AMS_Warning_Loop() {
|
||||
|
||||
amsWakeUp();
|
||||
amsConfigOverUnderVoltage(amsov, amsuv);
|
||||
amsClearAux();
|
||||
amsCellMeasurement(&module);
|
||||
amsAuxAndStatusMeasurement(&module);
|
||||
amsCheckUnderOverVoltage(&module);
|
||||
|
||||
if (!(module.overVoltage | module.underVoltage)) {
|
||||
currentAMSState = AMSIDLE;
|
||||
// amsClearWarning();
|
||||
}
|
||||
amsStopBalancing();
|
||||
|
||||
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() {
|
||||
uint8_t balancingdone = 1;
|
||||
if ((eepromconfigured == 1) && (internalbalancingalgo == 1) &&
|
||||
(module.internalDieTemp <
|
||||
28000 /*Thermal Protection 93°C*/)) // If the EEPROM is configured and
|
||||
// the internal Balancing Algorithm
|
||||
// should be used
|
||||
{
|
||||
uint16_t highestcellvoltage = module.cellVoltages[0];
|
||||
uint16_t lowestcellvoltage = module.cellVoltages[0];
|
||||
uint8_t highestcell = 0;
|
||||
uint8_t lowestcell = 0;
|
||||
|
||||
for (uint8_t n = 0; n < numberofCells; n++) {
|
||||
if (module.cellVoltages[n] > highestcellvoltage) {
|
||||
highestcellvoltage = module.cellVoltages[n];
|
||||
highestcell = n;
|
||||
}
|
||||
if (module.cellVoltages[n] < lowestcellvoltage) {
|
||||
lowestcellvoltage = module.cellVoltages[n];
|
||||
lowestcell = n;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentAMSState ==
|
||||
AMSCHARGING) // Balancing is only Active if the BMS is in Charging Mode
|
||||
{
|
||||
|
||||
uint32_t channelstobalance = 0;
|
||||
|
||||
if (highestcellvoltage > startbalancingthreshold) {
|
||||
for (uint8_t n = 0; n < numberofCells; n++) {
|
||||
if (module.cellVoltages[n] > stopbalancingthreshold) {
|
||||
uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
|
||||
if (dv > (balancingvoltagedelta * 1000)) {
|
||||
balancingdone = 0;
|
||||
channelstobalance |= 1 << n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
amsConfigBalancing(channelstobalance, 0x0F);
|
||||
amsStartBalancing(100);
|
||||
}
|
||||
|
||||
else if (currentAMSState == AMSIDLEBALANCING) {
|
||||
|
||||
uint32_t channelstobalance = 0;
|
||||
|
||||
if (lowestcellvoltage <
|
||||
stopbalancingthreshold) // If under Voltage of one Cell is reached
|
||||
{
|
||||
amsStopBalancing();
|
||||
balancingdone = 1;
|
||||
} else // otherwise continue with regular Balancing Algorithm
|
||||
{
|
||||
for (uint8_t n = 0; n < numberofCells; n++) {
|
||||
uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
|
||||
if (dv > balancingvoltagedelta) {
|
||||
balancingdone = 0;
|
||||
channelstobalance |= 1 << n;
|
||||
}
|
||||
}
|
||||
|
||||
amsConfigBalancing(channelstobalance, 0x0F);
|
||||
amsStartBalancing(100);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
amsStopBalancing();
|
||||
balancingdone = 1;
|
||||
}
|
||||
return balancingdone;
|
||||
}
|
||||
|
||||
uint8_t writeWarningLog(uint8_t warningCode) {
|
||||
// eepromWriteWarningLog(warningCode);
|
||||
return 0;
|
||||
}
|
||||
uint8_t writeErrorLog(uint8_t errorCode) {
|
||||
// eepromWriteErrorLog(errorCode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t integrateCurrent() {
|
||||
lastticks = currenttick;
|
||||
currenttick = HAL_GetTick();
|
||||
if (currenttick < lastticks) {
|
||||
currentintegrator += (module.auxVoltages[0] - module.auxVoltages[2]) *
|
||||
(currenttick - lastticks);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
83
Core/Src/TMP1075.c
Normal file
83
Core/Src/TMP1075.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include "TMP1075.h"
|
||||
|
||||
#include "can-halal.h"
|
||||
|
||||
#include "errors.h"
|
||||
#include "stm32f3xx_hal_def.h"
|
||||
#include "stm32f3xx_hal_i2c.h"
|
||||
|
||||
#define MAX_TEMP ((int16_t)(59 / 0.0625f))
|
||||
#define MAX_FAILED_TEMP 12 //TODO: change value for compliance with the actual number of sensors
|
||||
#warning "change value for compliance with the actual number of sensors"
|
||||
|
||||
int16_t tmp1075_temps[N_TEMP_SENSORS] = {0};
|
||||
uint32_t tmp1075_failed_sensors = 0;
|
||||
uint8_t nfailed_temp_sensors = 0;
|
||||
|
||||
I2C_HandleTypeDef* hi2c;
|
||||
|
||||
HAL_StatusTypeDef tmp1075_init(I2C_HandleTypeDef* handle) {
|
||||
hi2c = handle;
|
||||
for (int i = 0; i < N_TEMP_SENSORS; i++) {
|
||||
HAL_StatusTypeDef status = tmp1075_sensor_init(i);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
void handle_over_maxtemp(uint8_t index, uint16_t value) {
|
||||
set_error_source(ERROR_SOURCE_TEMPERATURES);
|
||||
error_data.data_kind = SEK_OVERTEMP;
|
||||
error_data.data[0] = index;
|
||||
uint8_t* ptr = &error_data.data[1];
|
||||
ptr = ftcan_marshal_unsigned(ptr, value, 2);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef tmp1075_measure() {
|
||||
int err = 0;
|
||||
int temp_error = 0;
|
||||
for (int i = 0; i < N_TEMP_SENSORS; i++) {
|
||||
if (tmp1075_sensor_read(i, &tmp1075_temps[i]) != HAL_OK ||
|
||||
(tmp1075_temps[i] & 0x000F) != 0) {
|
||||
tmp1075_failed_sensors |= 1 << i;
|
||||
nfailed_temp_sensors++;
|
||||
err = 1;
|
||||
} else {
|
||||
tmp1075_temps[i] >>= 4;
|
||||
tmp1075_failed_sensors &= ~(1 << i);
|
||||
if (tmp1075_temps[i] >= MAX_TEMP) {
|
||||
temp_error = 1;
|
||||
handle_over_maxtemp(i, tmp1075_temps[i]);
|
||||
}
|
||||
#warning "check for under temp"
|
||||
}
|
||||
}
|
||||
if (nfailed_temp_sensors > MAX_FAILED_TEMP) {
|
||||
error_data.data_kind = SEK_TOO_FEW_TEMPS;
|
||||
set_error_source(ERROR_SOURCE_TEMPERATURES);
|
||||
} else if (!temp_error) {
|
||||
clear_error_source(ERROR_SOURCE_TEMPERATURES);
|
||||
}
|
||||
nfailed_temp_sensors = 0;
|
||||
return err ? HAL_ERROR : HAL_OK;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef tmp1075_sensor_init(int n) {
|
||||
uint16_t addr = (0b1000000 | n) << 1;
|
||||
uint8_t data[] = {0};
|
||||
return HAL_I2C_Master_Transmit(hi2c, addr, data, sizeof(data), 100);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef tmp1075_sensor_read(int n, int16_t* res) {
|
||||
uint16_t addr = (0b1000000 | n) << 1;
|
||||
addr |= 1; // Read
|
||||
uint8_t result[2];
|
||||
HAL_StatusTypeDef status =
|
||||
HAL_I2C_Master_Receive(hi2c, addr, result, sizeof(result), 5); //5ms timeout for failure (cascading faliure max = 30 * 5 = 150ms)
|
||||
if (status == HAL_OK) {
|
||||
*res = (result[0] << 8) | result[1];
|
||||
}
|
||||
return status;
|
||||
}
|
||||
55
Core/Src/eeprom.c
Normal file
55
Core/Src/eeprom.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include "eeprom.h"
|
||||
#include "errors.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
#include "stm32f3xx_hal_exti.h"
|
||||
#include "stm32f3xx_hal_i2c.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define EEPROM_I2C_ADDR 0xA0
|
||||
// Don't use the beginning of the EEPROM, since the testbench writes there
|
||||
#define EEPROM_CONFIG_BASE 0x0100
|
||||
#define EEPROM_CONFIG_ECC_OFFSET 0x20
|
||||
|
||||
EEPROMConfig eeprom_config;
|
||||
|
||||
static I2C_HandleTypeDef* hi2c;
|
||||
|
||||
void eeprom_init(I2C_HandleTypeDef* handle) {
|
||||
hi2c = handle;
|
||||
uint8_t buf[sizeof(EEPROMConfig) * 3];
|
||||
// Read 3 EEPROM config buffers at 32 byte offsets
|
||||
for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
|
||||
HAL_I2C_Mem_Read(hi2c, EEPROM_I2C_ADDR,
|
||||
EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
|
||||
buf + sizeof(EEPROMConfig) * ecc_i, sizeof(EEPROMConfig),
|
||||
100);
|
||||
}
|
||||
// ECC
|
||||
for (size_t i = 0; i < sizeof(EEPROMConfig); i++) {
|
||||
uint8_t a = buf[i + sizeof(EEPROMConfig) * 0];
|
||||
uint8_t b = buf[i + sizeof(EEPROMConfig) * 1];
|
||||
uint8_t c = buf[i + sizeof(EEPROMConfig) * 2];
|
||||
if (a == b || a == c) {
|
||||
buf[i] = a;
|
||||
} else if (b == c) {
|
||||
buf[i] = b;
|
||||
} else {
|
||||
set_error_source(ERROR_SOURCE_EEPROM);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(&eeprom_config, buf, sizeof(EEPROMConfig));
|
||||
// Write back config
|
||||
eeprom_config.id = 10;
|
||||
eeprom_config_save();
|
||||
}
|
||||
|
||||
void eeprom_config_save() {
|
||||
for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
|
||||
HAL_I2C_Mem_Write(hi2c, EEPROM_I2C_ADDR,
|
||||
EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
|
||||
(uint8_t*)&eeprom_config, sizeof(eeprom_config), 100);
|
||||
HAL_Delay(100);
|
||||
}
|
||||
}
|
||||
13
Core/Src/errors.c
Normal file
13
Core/Src/errors.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include "errors.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
|
||||
SlaveErrorData error_data;
|
||||
|
||||
void set_error_source(int source) {
|
||||
if (!error_data.error_sources) {
|
||||
error_data.errors_since = HAL_GetTick();
|
||||
}
|
||||
error_data.error_sources |= source;
|
||||
}
|
||||
|
||||
void clear_error_source(int source) { error_data.error_sources &= ~source; }
|
||||
@ -21,7 +21,14 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "ADBMS_Abstraction.h"
|
||||
#include "ADBMS_CMD_MAKROS.h"
|
||||
#include "AMS_CAN.h"
|
||||
#include "AMS_HighLevel.h"
|
||||
#include "TMP1075.h"
|
||||
#include "eeprom.h"
|
||||
#include "errors.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@ -109,7 +116,8 @@ int main(void)
|
||||
MX_USART1_UART_Init();
|
||||
MX_TIM1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
tmp1075_init(&hi2c1);
|
||||
AMS_Init(&hspi1);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
@ -119,6 +127,14 @@ int main(void)
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
AMS_Loop();
|
||||
|
||||
ams_can_send_status();
|
||||
if (error_data.error_sources &&
|
||||
HAL_GetTick() - error_data.errors_since >= ERROR_TIME_THRESH) {
|
||||
ams_can_send_error();
|
||||
}
|
||||
ams_can_send_log();
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
@ -139,7 +155,9 @@ void SystemClock_Config(void)
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
@ -149,7 +167,7 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
@ -273,11 +291,11 @@ static void MX_SPI1_Init(void)
|
||||
hspi1.Instance = SPI1;
|
||||
hspi1.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi1.Init.DataSize = SPI_DATASIZE_4BIT;
|
||||
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
|
||||
Reference in New Issue
Block a user