V1.4
This commit is contained in:
82
Core/Src/ADBMS_LL_Driver.c
Normal file → Executable file
82
Core/Src/ADBMS_LL_Driver.c
Normal file → Executable file
@ -6,11 +6,11 @@
|
||||
*/
|
||||
|
||||
#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;
|
||||
|
||||
@ -31,7 +31,7 @@ uint8 calculateCommandPEC(uint8_t* data, uint8_t datalen) {
|
||||
if (datalen >= 3) {
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
const uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
@ -53,13 +53,13 @@ uint8 checkCommandPEC(uint8* data, uint8 datalen) {
|
||||
|
||||
for (int i = 0; i < (datalen - 2); i++) {
|
||||
for (int n = 0; n < 8; n++) {
|
||||
uint8 din = data[i] << (n);
|
||||
const uint8 din = data[i] << (n);
|
||||
currentpec = updateCommandPEC(currentpec, din);
|
||||
}
|
||||
}
|
||||
|
||||
uint8 pechigh = (currentpec >> 7) & 0xFF;
|
||||
uint8 peclow = (currentpec << 1) & 0xFF;
|
||||
const uint8 pechigh = (currentpec >> 7) & 0xFF;
|
||||
const uint8 peclow = (currentpec << 1) & 0xFF;
|
||||
|
||||
if ((pechigh == data[datalen - 2]) && (peclow == data[datalen - 1])) {
|
||||
return 0;
|
||||
@ -70,13 +70,13 @@ uint8 checkCommandPEC(uint8* data, uint8 datalen) {
|
||||
|
||||
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);
|
||||
const uint8 in0 = din ^ ((currentPEC >> 14) & 0x01);
|
||||
const uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
|
||||
const uint8 in4 = in0 ^ ((currentPEC >> 3) & 0x01);
|
||||
const uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
|
||||
const uint8 in8 = in0 ^ ((currentPEC >> 7) & 0x01);
|
||||
const uint8 in10 = in0 ^ ((currentPEC >> 9) & 0x01);
|
||||
const uint8 in14 = in0 ^ ((currentPEC >> 13) & 0x01);
|
||||
|
||||
uint16 newPEC = 0;
|
||||
|
||||
@ -103,9 +103,9 @@ uint16 updateCommandPEC(uint16 currentPEC, uint8 din) {
|
||||
//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
|
||||
uint16_t pec10_calc(bool rx_cmd, int len, const uint8_t* data) {
|
||||
uint16_t remainder = 16; /* PEC_SEED; 0000010000 */
|
||||
const 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. */
|
||||
@ -149,16 +149,13 @@ 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
|
||||
const 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;
|
||||
@ -170,7 +167,7 @@ uint8 checkDataPEC(uint8* data, uint8 len) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
crc currentpec = F_CRC_CalculaCheckSum(data, len);
|
||||
const crc currentpec = F_CRC_CalculaCheckSum(data, len);
|
||||
|
||||
return (currentpec == 0) ? 0 : 1;
|
||||
}
|
||||
@ -212,10 +209,10 @@ crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) {
|
||||
|
||||
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);
|
||||
const uint8 in0 = din ^ ((currentPEC >> 9) & 0x01);
|
||||
const uint8 in2 = in0 ^ ((currentPEC >> 1) & 0x01);
|
||||
const uint8 in3 = in0 ^ ((currentPEC >> 2) & 0x01);
|
||||
const uint8 in7 = in0 ^ ((currentPEC >> 6) & 0x01);
|
||||
|
||||
uint16 newPEC = 0;
|
||||
|
||||
@ -232,7 +229,7 @@ uint16 updateDataPEC(uint16 currentPEC, uint8 din) {
|
||||
return newPEC;
|
||||
}
|
||||
|
||||
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen) {
|
||||
uint8 writeCMD(uint16 command, const uint8 * args, uint8 arglen) {
|
||||
uint8 ret;
|
||||
if (arglen > 0) {
|
||||
uint8 buffer[6 + arglen]; //command + PEC (2 bytes) + data + DPEC (2 bytes)
|
||||
@ -266,23 +263,9 @@ uint8 writeCMD(uint16 command, uint8* args, uint8 arglen) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define ITER_COUNT 50
|
||||
static uint8_t count = 0;
|
||||
static bool isOn = false;
|
||||
|
||||
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen) {
|
||||
if (count == ITER_COUNT) {
|
||||
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++;
|
||||
}
|
||||
|
||||
|
||||
uint8 txbuffer[6 + buflen] = {};
|
||||
uint8 rxbuffer[6 + buflen] = {};
|
||||
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;
|
||||
@ -335,25 +318,22 @@ void mcuAdbmsCSHigh() {
|
||||
}
|
||||
|
||||
uint8 mcuSPITransmit(uint8* buffer, uint8 buffersize) {
|
||||
HAL_StatusTypeDef status;
|
||||
uint8 rxbuf[buffersize];
|
||||
status = HAL_SPI_TransmitReceive(adbmsspi, buffer, rxbuf, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
uint8 rxbuf[buffersize];
|
||||
const HAL_StatusTypeDef 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);
|
||||
const HAL_StatusTypeDef 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);
|
||||
const HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(adbmsspi, txbuffer, rxbuffer, buffersize,
|
||||
ADBMS_SPI_TIMEOUT);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user