From eb2dba0106c2f241102f1a87a7ed439fd7645ed2 Mon Sep 17 00:00:00 2001 From: Kilian Bracher Date: Mon, 3 Feb 2025 21:03:43 +0100 Subject: [PATCH] refactor: code cleanup, update .gitignore --- AMS_Master_Code/.gitignore | 1 + .../Core/Inc/ADBMS_Abstraction.h | 2 +- .../Core/Inc/ADBMS_LL_Driver.h | 13 ------------- .../Core/Src/ADBMS_Abstraction.c | 18 +++++++++--------- .../Core/Src/ADBMS_LL_Driver.c | 4 +--- .../ADBMS6830B_Driver/Core/Src/AMS_HighLevel.c | 4 ++-- .../Lib/ADBMS6830B_Driver/Core/Src/errors.c | 2 -- 7 files changed, 14 insertions(+), 30 deletions(-) diff --git a/AMS_Master_Code/.gitignore b/AMS_Master_Code/.gitignore index 4507d33..3ca1f42 100644 --- a/AMS_Master_Code/.gitignore +++ b/AMS_Master_Code/.gitignore @@ -1,6 +1,7 @@ /.vscode/ /build/ /.cache/ +/.idea/ .clangd TouchGFX/build compile_commands.json diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h index 698557f..380038d 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h @@ -72,7 +72,7 @@ HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]); HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]); -HAL_StatusTypeDef amsConfigBalancing(uint32_t channels[N_BMS], uint8_t dutyCycle); +HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint8_t dutyCycle); HAL_StatusTypeDef amsStartBalancing(uint8_t dutyCycle); HAL_StatusTypeDef amsStopBalancing(); diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h index 0beea4d..1c4b924 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h @@ -24,19 +24,6 @@ uint8_t adbmsDriverInit(SPI_HandleTypeDef* hspi); #define CMD_EMPTY_BUFFER ((uint8_t[CMD_BUFFER_SIZE(0)]){0}) #define CMD_EMPTY_BUFFER_SIZE CMD_BUFFER_SIZE(0) -//macro to function for better type checking (access attribute) -[[gnu::access(read_only, 4, 1)]] -static inline void __modify_writeCMD_args(size_t arglen, uint8_t args[static restrict N_BMS * (arglen + 2)], uint8_t bms, uint8_t * restrict data) { - for (uint8_t i = 0; i < arglen; i++) { - args[(bms * (arglen + 2)) + i] = data[i]; - } -} - -#define modify_writeCMD_args(args, bms, data) \ - static_assert(bms < N_BMS, "bms out of range"); \ - __modify_writeCMD_args(args.arglen, args.args + 4, bms, data) - - HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen); [[gnu::access(read_write, 2, 4), gnu::nonnull(2), gnu::always_inline]] //add dummy size variable for bounds checking, should be optimized out diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c index 2c851bc..fd844c8 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c @@ -15,11 +15,11 @@ extern uint8_t numberofCells; #define CHECK_RETURN(x) \ - { \ + do { \ HAL_StatusTypeDef status = x; \ if (status != 0) \ return status; \ - } + } while (0) HAL_StatusTypeDef amsReset() { amsWakeUp(); @@ -71,7 +71,7 @@ HAL_StatusTypeDef amsWakeUp() { HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]) { #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 HAL_ERROR; //amsReadCellVoltages(module); + return amsReadCellVoltages(module); } HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) { @@ -153,7 +153,7 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) { return HAL_OK; } -HAL_StatusTypeDef amsConfigBalancing(uint32_t channels[N_BMS], uint8_t dutyCycle) { +HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint8_t dutyCycle) { if (dutyCycle > 0x0F) { return HAL_ERROR; // only 4 bits are allowed for dutyCycle } @@ -318,7 +318,7 @@ HAL_StatusTypeDef amsReadCellVoltages(Cell_Module (*module)[N_BMS]) { } //Each selected BMS must have a corresponding address, and the data array for that BMS must be at least datalens[i] bytes long -HAL_StatusTypeDef amsSendI2C(uint8_t addresses[static N_BMS], uint8_t * data[static N_BMS], uint8_t datalens[static N_BMS], uint32_t bms_mask) { +HAL_StatusTypeDef amsSendI2C(const uint8_t addresses[static N_BMS], uint8_t * data[static N_BMS], const uint8_t datalens[static N_BMS], uint32_t bms_mask) { uint8_t buffer[CMD_BUFFER_SIZE(COMM_GROUP_SIZE)] = {}; //COMM register works in 3 bytes max per go, interleaved with control information @@ -351,7 +351,7 @@ HAL_StatusTypeDef amsSendI2C(uint8_t addresses[static N_BMS], uint8_t * data[sta if (i == 0) { buffer[offset + 0] = I2C_SEND_START; - buffer[offset + 1] = addresses[j]; + buffer[offset + 1] = addresses[j] << 1; //shift the address left by 1 to make space for the R/W bit continue; } @@ -390,7 +390,7 @@ HAL_StatusTypeDef amsSendI2C(uint8_t addresses[static N_BMS], uint8_t * data[sta return HAL_OK; } -HAL_StatusTypeDef amsReadI2C(uint8_t addresses[static N_BMS], uint8_t * data[static N_BMS], uint8_t datalens[static N_BMS], uint32_t bms_mask) { +HAL_StatusTypeDef amsReadI2C(const uint8_t addresses[static N_BMS], uint8_t * data[static N_BMS], const uint8_t datalens[static N_BMS], uint32_t bms_mask) { uint8_t buffer[CMD_BUFFER_SIZE(COMM_GROUP_SIZE)] = {}; uint8_t max_datalen = 0; @@ -422,7 +422,7 @@ HAL_StatusTypeDef amsReadI2C(uint8_t addresses[static N_BMS], uint8_t * data[sta if (i == 0) { buffer[offset + 0] = I2C_SEND_START; - buffer[offset + 1] = addresses[j]; + buffer[offset + 1] = addresses[j] << 1 | 0x01; // shift the address left by 1 to make space for the R/W bit continue; } @@ -441,7 +441,7 @@ HAL_StatusTypeDef amsReadI2C(uint8_t addresses[static N_BMS], uint8_t * data[sta } // case 3: we are still receiving data - buffer[offset + ((i % 3) * 2)] = I2C_RECV_ACK; + buffer[offset + ((i % 3) * 2)] = I2C_SEND_ACK; } // send the command data diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c index 33e5e46..9802b1f 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c @@ -6,11 +6,9 @@ */ #include "ADBMS_LL_Driver.h" -#include "ADBMS_CMD_MAKROS.h" #include "config_ADBMS6830.h" #include "stm32h7xx_hal.h" #include "swo_log.h" -#include #include #include @@ -132,7 +130,7 @@ static uint8_t checkDataPEC(uint8_t* data, uint8_t len) { } HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { - HAL_StatusTypeDef ret = HAL_OK; + HAL_StatusTypeDef ret; if (arglen > 0) { args[0] = (command >> 8) & 0xFF; args[1] = (command) & 0xFF; diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/AMS_HighLevel.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/AMS_HighLevel.c index 1cf03ef..3099684 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/AMS_HighLevel.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/AMS_HighLevel.c @@ -43,8 +43,8 @@ void AMS_Init(SPI_HandleTypeDef* hspi) { #define any(x) ({ \ bool any = false; \ - for (size_t i = 0; i < N_BMS; i++) { \ - Cell_Module module = modules[i]; \ + for (size_t __any_intern_i = 0; __any_intern_i < N_BMS; __any_intern_i++) { \ + Cell_Module module = modules[__any_intern_i]; \ any |= (x); \ } \ any; \ diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/errors.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/errors.c index 0579ade..1767191 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/errors.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/errors.c @@ -1,7 +1,5 @@ #include "errors.h" #include "stm32h7xx_hal.h" -#include -#include SlaveErrorData error_data[NUM_ERROR_KINDS] = {}; uint32_t error_sources = 0;