From 1abe7bfed6599ee377f327785d56c686deba84b7 Mon Sep 17 00:00:00 2001
From: kbracher <k.bracher@fasttube.de>
Date: Thu, 27 Mar 2025 19:01:45 +0100
Subject: [PATCH] refactor: move more options to config file

---
 AMS_Master_Code/Core/Inc/config_ADBMS6830.h               | 8 ++++++--
 .../Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h | 2 --
 .../Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c    | 5 +----
 .../Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c | 4 +---
 4 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h
index dedb364..2fdaa6a 100644
--- a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h
+++ b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h
@@ -4,13 +4,17 @@
 #include "main.h"
 
 #define N_BMS 1
+#define N_CELLS 16
 #define ADBMS_MAX_CHIP_TEMP 110 // max temperature of ADBMS6830B (not battery) in C
+#define ADBMS_SPI_TIMEOUT    50 // Timeout in ms
 
-[[maybe_unused]] static inline void mcuAdbmsCSLow() {
+[[maybe_unused, gnu::always_inline]]
+static inline void mcuAdbmsCSLow() {
     HAL_GPIO_WritePin(AMS_CS_GPIO_Port, AMS_CS_Pin, GPIO_PIN_RESET);
 }
 
-[[maybe_unused]] static inline void mcuAdbmsCSHigh() {
+[[maybe_unused, gnu::always_inline]]
+static inline void mcuAdbmsCSHigh() {
     HAL_GPIO_WritePin(AMS_CS_GPIO_Port, AMS_CS_Pin, GPIO_PIN_SET);
 }
 
diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h
index 2222503..ced8255 100644
--- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h
+++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h
@@ -30,6 +30,4 @@ extern Cell_Module modules[N_BMS];
 extern uint32_t balancedCells;
 extern bool BalancingActive;
 
-extern uint8_t numberofCells;
-
 #endif /* INC_AMS_HIGHLEVEL_H_ */
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 2204cc6..a8924cc 100644
--- 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
@@ -13,8 +13,6 @@
 #include <stddef.h>
 #include "NTC.h"
 
-extern uint8_t numberofCells;
-
 static const char* const HAL_Statuses[] = {"HAL_OK", "HAL_ERROR", "HAL_BUSY", "HAL_TIMEOUT"};
 
 #define CHECK_RETURN(x)                                                                                                \
@@ -265,8 +263,7 @@ HAL_StatusTypeDef amsCheckUnderOverVoltage(Cell_Module (*module)[N_BMS]) {
         (*module)[i].overVoltage = 0;
         (*module)[i].underVoltage = 0;
 
-        for (size_t j = 0; j < numberofCells;
-             j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ...
+        for (size_t j = 0; j < N_CELLS; j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ...
             (*module)[i].underVoltage |= (ov_uv_data >> (j * 2)) & 0x01;
             (*module)[i].overVoltage |= (ov_uv_data >> (j * 2 + 1)) & 0x01;
         }
diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c
index 6fb50f7..afd150f 100644
--- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c
+++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c
@@ -18,8 +18,6 @@ Cell_Module modules[N_BMS] = {};
 uint32_t balancedCells = 0;
 bool balancingActive = false;
 
-uint8_t numberofCells = 16;
-
 uint8_t packetChecksumFails = 0;
 #define MAX_PACKET_CHECKSUM_FAILS 5
 
@@ -37,7 +35,7 @@ static constexpr ADBMS_DetailedStatus NO_ERROR = {ADBMS_NO_ERROR};
 
 ADBMS_DetailedStatus AMS_Init(SPI_HandleTypeDef* hspi) {
     debug_log(LOG_LEVEL_INFO, "ADBMS6830B HAL - configured for %d controllers and %d cells per controller...", N_BMS,
-              numberofCells);
+              N_CELLS);
     if (initAMS(hspi) != HAL_OK) {
         debug_log(LOG_LEVEL_ERROR, "ADBMS6830B HAL - initialization failed");
         return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_FAULT, -1};