Compare commits
No commits in common. "983c8b1e2f1f545356c5002d369d28dfe829232f" and "219a5c3a87ec6be2a88fbf1b326a0ae381613057" have entirely different histories.
983c8b1e2f
...
219a5c3a87
3
AMS_Master_Code/.gitignore
vendored
3
AMS_Master_Code/.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
/.vscode/*
|
||||
/.vscode/
|
||||
/build/
|
||||
/.cache/
|
||||
/.idea/
|
||||
@ -8,4 +8,3 @@ compile_commands.json
|
||||
STM32Make.make
|
||||
openocd.cfg
|
||||
.stm32env
|
||||
!/.vscode/launch.json
|
60
AMS_Master_Code/.vscode/launch.json
vendored
60
AMS_Master_Code/.vscode/launch.json
vendored
@ -1,60 +0,0 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"showDevDebugOutput": "none",
|
||||
"cwd": "${workspaceRoot}",
|
||||
"executable": "./build/debug/AMS_Master_Nucleo.elf",
|
||||
"name": "Debug STM32",
|
||||
"request": "launch",
|
||||
"type": "cortex-debug",
|
||||
"servertype": "openocd",
|
||||
"preLaunchTask": "Build STM",
|
||||
"device": "stm32h7a3xxq.s",
|
||||
"configFiles": [
|
||||
"openocd.cfg"
|
||||
],
|
||||
"swoConfig": {
|
||||
"enabled": true,
|
||||
"source": "probe",
|
||||
"swoFrequency": 10666666,
|
||||
"cpuFrequency": 64000000,
|
||||
"decoders": [
|
||||
{
|
||||
"port": 0,
|
||||
"type": "console",
|
||||
"label": "SWO LOG",
|
||||
"encoding": "ascii"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"showDevDebugOutput": "none",
|
||||
"cwd": "${workspaceRoot}",
|
||||
"executable": "./build/debug/AMS_Master_Nucleo.elf",
|
||||
"name": "Attach STM32",
|
||||
"request": "attach",
|
||||
"type": "cortex-debug",
|
||||
"servertype": "openocd",
|
||||
"preLaunchTask": "Build STM",
|
||||
"device": "stm32h7a3xxq.s",
|
||||
"configFiles": [
|
||||
"openocd.cfg"
|
||||
],
|
||||
"swoConfig": {
|
||||
"enabled": true,
|
||||
"source": "probe",
|
||||
"swoFrequency": 10666666,
|
||||
"cpuFrequency": 64000000,
|
||||
"decoders": [
|
||||
{
|
||||
"port": 0,
|
||||
"type": "console",
|
||||
"label": "SWO LOG",
|
||||
"encoding": "ascii"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -14,57 +14,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_MESSAGE_LENGTH 256
|
||||
#define USE_MULTIPLE_CHANNELS false // if true, each log level has its own channel (FATAL : 0, ERROR : 1, etc.)
|
||||
#define USE_ANSI_ESCAPE_CODES true // if true, log messages will be colored according to their log level, and the console can be cleared
|
||||
|
||||
#if !USE_MULTIPLE_CHANNELS
|
||||
#define DEBUG_CHANNEL 0 // channel to output messages on
|
||||
#define USE_CHANNEL_MASK_VARIABLE true
|
||||
#endif
|
||||
|
||||
#if USE_CHANNEL_MASK_VARIABLE
|
||||
#define MASK_VARIABLE logging_mask // variable to store the channel mask, must be globally defined somewhere
|
||||
extern volatile uint32_t MASK_VARIABLE;
|
||||
#endif
|
||||
|
||||
enum log_level_t {
|
||||
LOG_LEVEL_FATAL,
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_NOISY,
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_NOISY
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_FATAL
|
||||
};
|
||||
|
||||
#if USE_ANSI_ESCAPE_CODES
|
||||
[[maybe_unused]] static const char *const log_level_names[] = {
|
||||
"\033[31m[FATAL]\033[0m ", "\033[31m[ERROR]\033[0m ",
|
||||
"\033[93m[WARN] \033[0m ", "\033[97m[INFO] \033[0m ",
|
||||
"\033[37m[DEBUG]\033[0m ", "\033[37m[NOISY]\033[0m "};
|
||||
#else
|
||||
[[maybe_unused]] static const char *const log_level_names[] = {
|
||||
"[FATAL] ", "[ERROR] ", "[WARN] ", "[INFO] ", "[DEBUG] ", "[NOISY] "};
|
||||
#endif
|
||||
[[maybe_unused]] static const char * const log_level_names[] = {
|
||||
"\033[37m[NOISY]\033[0m ",
|
||||
"\033[37m[DEBUG]\033[0m ",
|
||||
"\033[97m[INFO] \033[0m ",
|
||||
"\033[93m[WARN] \033[0m ",
|
||||
"\033[31m[ERROR]\033[0m ",
|
||||
"\033[31m[FATAL]\033[0m "
|
||||
};
|
||||
|
||||
static inline bool __ITM_channel_enabled(uint32_t channel) {
|
||||
#if !USE_MULTIPLE_CHANNELS
|
||||
#if USE_CHANNEL_MASK_VARIABLE
|
||||
return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) &&
|
||||
((MASK_VARIABLE & (1UL << channel)) != 0UL);
|
||||
#else
|
||||
channel = DEBUG_CHANNEL;
|
||||
#endif
|
||||
#endif
|
||||
return ((ITM->TER & (1UL << channel)) != 0UL);
|
||||
return (ITM->TER & (1UL << channel)) != 0UL;
|
||||
}
|
||||
|
||||
// adapted from ITM_SendChar() in the CMSIS-Core
|
||||
//
|
||||
// channel should be between 0 and 31
|
||||
static inline uint32_t __swo_putc(uint32_t c, unsigned int channel) {
|
||||
#if !USE_MULTIPLE_CHANNELS
|
||||
channel = DEBUG_CHANNEL;
|
||||
#endif
|
||||
if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */
|
||||
((ITM->TER & (1UL << channel)) != 0UL)) /* ITM Port enabled */
|
||||
{
|
||||
@ -92,27 +68,15 @@ static inline void __swo_print(unsigned int channel, const char *str) {
|
||||
}
|
||||
}
|
||||
|
||||
[[maybe_unused]]
|
||||
static void debug_clear_console() {
|
||||
for (int i = 0; i < LOG_LEVEL_NOISY; i++) {
|
||||
#if USE_ANSI_ESCAPE_CODES
|
||||
__swo_print(i, "\033[2J\033[;H"); // clear screen
|
||||
#else
|
||||
__swo_print(i, "\n\n\n\n\n-------------------\n\n\n\n\n"); // clear screen
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#define debug_log(level, msg, ...) \
|
||||
do { \
|
||||
if (DEBUG_CHANNEL_ENABLED(level)) { \
|
||||
char __swo_buffer[MAX_MESSAGE_LENGTH]; \
|
||||
size_t len = \
|
||||
snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \
|
||||
char buffer[MAX_MESSAGE_LENGTH]; \
|
||||
size_t len = snprintf(buffer, sizeof(buffer), msg, ##__VA_ARGS__); \
|
||||
__swo_putc('\n', level); \
|
||||
__swo_print(level, log_level_names[level]); \
|
||||
__swo_print(level, __swo_buffer); \
|
||||
if (len >= sizeof(__swo_buffer)) { \
|
||||
__swo_print(level, buffer); \
|
||||
if (len >= sizeof(buffer)) { \
|
||||
__swo_print(level, " [message length exceeded] "); \
|
||||
} \
|
||||
} \
|
||||
@ -121,13 +85,13 @@ static void debug_clear_console() {
|
||||
#define debug_log_cont(level, msg, ...) \
|
||||
do { \
|
||||
if (DEBUG_CHANNEL_ENABLED(level)) { \
|
||||
char __swo_buffer[MAX_MESSAGE_LENGTH]; \
|
||||
size_t len = \
|
||||
snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \
|
||||
__swo_print(level, __swo_buffer); \
|
||||
if (len >= sizeof(__swo_buffer)) { \
|
||||
char buffer[MAX_MESSAGE_LENGTH]; \
|
||||
size_t len = snprintf(buffer, sizeof(buffer), msg, ##__VA_ARGS__); \
|
||||
__swo_print(level, buffer); \
|
||||
if (len >= sizeof(buffer)) { \
|
||||
__swo_print(level, " [message length exceeded] "); \
|
||||
} \
|
||||
__swo_putc('\n', level); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
@ -20,7 +20,7 @@ static const char* const HAL_Statuses[] = {"HAL_OK", "HAL_ERROR", "HAL_BUSY", "H
|
||||
do { \
|
||||
HAL_StatusTypeDef status = x; \
|
||||
if (status != 0) { \
|
||||
debug_log(LOG_LEVEL_ERROR, "in %s:%d@%s: %s failed with status %d (%s)", __FILE_NAME__, __LINE__, __func__,\
|
||||
debug_log(LOG_LEVEL_ERROR, "@%s:%s:%d: %s failed with status %d (%s)", __FILE_NAME__, __func__, __LINE__, \
|
||||
#x, status, \
|
||||
(status < (sizeof(HAL_Statuses) / sizeof(HAL_Statuses[0]))) ? HAL_Statuses[status] : "Unknown"); \
|
||||
return status; \
|
||||
@ -57,8 +57,9 @@ HAL_StatusTypeDef amsReset() {
|
||||
for (size_t j = 0; j < SID_GROUP_SIZE; j++) {
|
||||
id |= sidbuffer[BUFFER_BMS_OFFSET(i, SID_GROUP_SIZE) + j] << (j * 8);
|
||||
}
|
||||
debug_log_cont(LOG_LEVEL_INFO, "0x%lx%lx ", (uint32_t)(id >> 32), (uint32_t)(id & 0xFFFFFFFF)); //newlib does not support %llu
|
||||
debug_log_cont(LOG_LEVEL_INFO, "0x%llx ", id);
|
||||
}
|
||||
debug_log_cont(LOG_LEVEL_INFO, "\n");
|
||||
}
|
||||
|
||||
mcuDelay(10);
|
||||
|
@ -46,8 +46,6 @@ ADBMS_DetailedStatus AMS_Init(SPI_HandleTypeDef* hspi) {
|
||||
}
|
||||
|
||||
pollingTimes = (struct pollingTimes){HAL_GetTick(), HAL_GetTick()};
|
||||
packetChecksumFails = 0;
|
||||
deviceSleeps = 0;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
@ -61,7 +59,7 @@ ADBMS_DetailedStatus AMS_Init(SPI_HandleTypeDef* hspi) {
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
first_match + 1; \
|
||||
first_match; \
|
||||
})
|
||||
|
||||
ADBMS_DetailedStatus AMS_Idle_Loop() {
|
||||
@ -72,11 +70,11 @@ ADBMS_DetailedStatus AMS_Idle_Loop() {
|
||||
|
||||
packetChecksumFails += (amsAuxAndStatusMeasurement(&modules) == HAL_ERROR);
|
||||
|
||||
int match = 0;
|
||||
static int match = 0;
|
||||
if ((match = any(module.status.SLEEP))) {
|
||||
deviceSleeps++;
|
||||
if (deviceSleeps > MAX_DEVICE_SLEEP) {
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_TIMEOUT, match - 1};
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_TIMEOUT, match};
|
||||
} else {
|
||||
amsReset();
|
||||
}
|
||||
@ -88,7 +86,7 @@ ADBMS_DetailedStatus AMS_Idle_Loop() {
|
||||
// iteration.
|
||||
amsClearFlag();
|
||||
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_FAULT, match - 1};
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_FAULT, match};
|
||||
}
|
||||
|
||||
packetChecksumFails += (amsCellMeasurement(&modules) == HAL_ERROR);
|
||||
@ -99,16 +97,16 @@ ADBMS_DetailedStatus AMS_Idle_Loop() {
|
||||
}
|
||||
|
||||
if ((match = any(module.overVoltage))) {
|
||||
return (ADBMS_DetailedStatus){ADBMS_OVERVOLT, match - 1};
|
||||
return (ADBMS_DetailedStatus){ADBMS_OVERVOLT, match};
|
||||
}
|
||||
|
||||
if ((match = any(module.underVoltage))) {
|
||||
return (ADBMS_DetailedStatus){ADBMS_UNDERVOLT, match - 1};
|
||||
return (ADBMS_DetailedStatus){ADBMS_UNDERVOLT, match};
|
||||
}
|
||||
|
||||
// TODO: replace with the correct threshold for the internal die temperature
|
||||
if ((match = any(module.internalDieTemp > 28000 || module.status.THSD))) {
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_OVERTEMP, match - 1};
|
||||
return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_OVERTEMP, match};
|
||||
}
|
||||
|
||||
// mcuDelay(10);
|
||||
|
@ -173,17 +173,17 @@ HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) {
|
||||
args[1] = (command) & 0xFF;
|
||||
|
||||
if (DEBUG_CHANNEL_ENABLED(LOG_LEVEL_NOISY)) {
|
||||
debug_log(LOG_LEVEL_NOISY, "%lu W | %02X %02X ", HAL_GetTick(), args[0], args[1]);
|
||||
debug_log(LOG_LEVEL_NOISY, "%lu W | %x %x ", HAL_GetTick(), args[0], args[1]);
|
||||
|
||||
//print out data bytes
|
||||
if (arglen > 0) {
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i);
|
||||
for (size_t j = 0; j < arglen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%02X ", args[BUFFER_BMS_OFFSET(i, arglen) + j]);
|
||||
}
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i);
|
||||
for (size_t j = 0; j < arglen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%x ", args[4 + (i * (arglen + 2)) + j]);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "\n");
|
||||
}
|
||||
|
||||
calculateCommandPEC(args, 4);
|
||||
@ -232,17 +232,17 @@ HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen)
|
||||
//TODO: check command counter?
|
||||
|
||||
if (DEBUG_CHANNEL_ENABLED(LOG_LEVEL_NOISY)) {
|
||||
debug_log(LOG_LEVEL_NOISY, "%lu R | %02X %02X ", HAL_GetTick(), command >> 8, command & 0xFF);
|
||||
debug_log(LOG_LEVEL_NOISY, "%lu R | %x %x ", HAL_GetTick(), command >> 8, command & 0xFF);
|
||||
|
||||
//print out data bytes
|
||||
if (arglen > 0) {
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i);
|
||||
for (size_t j = 0; j < arglen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%02X ", buffer[BUFFER_BMS_OFFSET(i, arglen) + j]);
|
||||
}
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i);
|
||||
for (size_t j = 0; j < arglen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%x ", buffer[4 + (i * (arglen + 2)) + j]);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "\n");
|
||||
}
|
||||
|
||||
//check data PEC
|
||||
|
@ -22,7 +22,6 @@
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "ADBMS_Driver.h"
|
||||
#include "swo_log.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@ -59,7 +58,7 @@ static void MX_SPI1_Init(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
uint32_t volatile logging_mask = 0b11111; // no LOG_LEVEL_NOISY
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@ -96,30 +95,15 @@ int main(void)
|
||||
MX_GPIO_Init();
|
||||
MX_SPI1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
debug_clear_console();
|
||||
debug_log(LOG_LEVEL_INFO, "AMS_Master on %s (%s), compiled at %s", COMMIT_BRANCH, COMMIT_HASH, COMPILE_DATE);
|
||||
debug_log(LOG_LEVEL_INFO, "Starting BMS...");
|
||||
int status = -1;
|
||||
while (status != ADBMS_NO_ERROR) {
|
||||
status = AMS_Init(&hspi1).status;
|
||||
if (status != ADBMS_NO_ERROR) {
|
||||
debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS, AMS_Init returned %d", status);
|
||||
HAL_Delay(2000);
|
||||
}
|
||||
}
|
||||
AMS_Init(&hspi1);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
status = AMS_Idle_Loop().status;
|
||||
if (status != ADBMS_NO_ERROR) {
|
||||
debug_log(LOG_LEVEL_ERROR, "AMS_Idle_Loop returned %d", status);
|
||||
HAL_Delay(2000);
|
||||
AMS_Init(&hspi1);
|
||||
}
|
||||
HAL_Delay(100);
|
||||
AMS_Idle_Loop();
|
||||
HAL_Delay(20);
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
@ -24,9 +24,6 @@ cDefinitions:
|
||||
- STM32H7
|
||||
- USE_HAL_DRIVER
|
||||
- FTCAN_NUM_FILTERS=20
|
||||
- COMMIT_HASH='"$(shell git describe --always --dirty --abbrev=8)"'
|
||||
- COMMIT_BRANCH='"$(shell git rev-parse --abbrev-ref HEAD)"'
|
||||
- COMPILE_DATE='"$(shell date +'%Y-%m-%d %H:%M:%S')"'
|
||||
|
||||
cxxDefinitions: []
|
||||
asDefinitions: []
|
||||
@ -43,7 +40,6 @@ asDefinitionsFile:
|
||||
cFlags:
|
||||
- -Wall
|
||||
- -Wextra
|
||||
- -Wshadow
|
||||
- -fdata-sections
|
||||
- -ffunction-sections
|
||||
- -std=gnu23
|
||||
|
Loading…
x
Reference in New Issue
Block a user