Skip to content

Commit

Permalink
fix(errors): error expire at random
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonidotpy committed Jul 15, 2024
1 parent 2938bd8 commit e4455c4
Show file tree
Hide file tree
Showing 15 changed files with 2,863 additions and 2,747 deletions.
96 changes: 71 additions & 25 deletions mainboard/Inc/error/error-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* Generated by error_gen ruby gem, for more information see:
* https://github.com/eagletrt/micro-utils/tree/master/error-handler-generator
*
* Error_gen version 1.3.4
* Generation date: 2024-06-04 16:25:13 +0200
* Error_gen version 1.6.1
* Generation date: 2024-07-13 18:26:53 +0200
* Generated from: errors.json
* With prefix: none
* The error handler contains:
* - 15 error groups
* - 66 total error instances
Expand All @@ -23,6 +24,7 @@

/**
* @brief Set or reset an instance of an error based on a condition
*
* @details If the condition is true the error is set, otherwise it is reset
*
* @param condition A boolean expression
Expand All @@ -33,27 +35,46 @@
#define ERROR_TOGGLE_IF(condition, group, instance, timestamp) \
((condition) ? error_set(group, instance, timestamp) : error_reset(group, instance))

/** @brief Type of the error that categorize a group of instances */
/**
* @brief Type of the error that categorize a group of instances
*
* @details
* - ERROR_GROUP_ERROR_CELL_UNDER_VOLTAGE no description
* - ERROR_GROUP_ERROR_CELL_OVER_VOLTAGE no description
* - ERROR_GROUP_ERROR_CELL_UNDER_TEMPERATURE no description
* - ERROR_GROUP_ERROR_CELL_OVER_TEMPERATURE no description
* - ERROR_GROUP_ERROR_OVER_CURRENT no description
* - ERROR_GROUP_ERROR_CAN no description
* - ERROR_GROUP_ERROR_INT_VOLTAGE_MISMATCH no description
* - ERROR_GROUP_ERROR_CELLBOARD_COMM no description
* - ERROR_GROUP_ERROR_CELLBOARD_INTERNAL no description
* - ERROR_GROUP_ERROR_CONNECTOR_DISCONNECTED no description
* - ERROR_GROUP_ERROR_FANS_DISCONNECTED no description
* - ERROR_GROUP_ERROR_FEEDBACK no description
* - ERROR_GROUP_ERROR_FEEDBACK_CIRCUITRY no description
* - ERROR_GROUP_ERROR_EEPROM_COMM no description
* - ERROR_GROUP_ERROR_EEPROM_WRITE no description
*/
typedef enum {
ERROR_CELL_UNDER_VOLTAGE,
ERROR_CELL_OVER_VOLTAGE,
ERROR_CELL_UNDER_TEMPERATURE,
ERROR_CELL_OVER_TEMPERATURE,
ERROR_OVER_CURRENT,
ERROR_CAN,
ERROR_INT_VOLTAGE_MISMATCH,
ERROR_CELLBOARD_COMM,
ERROR_CELLBOARD_INTERNAL,
ERROR_CONNECTOR_DISCONNECTED,
ERROR_FANS_DISCONNECTED,
ERROR_FEEDBACK,
ERROR_FEEDBACK_CIRCUITRY,
ERROR_EEPROM_COMM,
ERROR_EEPROM_WRITE,
ERROR_COUNT
ERROR_GROUP_ERROR_CELL_UNDER_VOLTAGE,
ERROR_GROUP_ERROR_CELL_OVER_VOLTAGE,
ERROR_GROUP_ERROR_CELL_UNDER_TEMPERATURE,
ERROR_GROUP_ERROR_CELL_OVER_TEMPERATURE,
ERROR_GROUP_ERROR_OVER_CURRENT,
ERROR_GROUP_ERROR_CAN,
ERROR_GROUP_ERROR_INT_VOLTAGE_MISMATCH,
ERROR_GROUP_ERROR_CELLBOARD_COMM,
ERROR_GROUP_ERROR_CELLBOARD_INTERNAL,
ERROR_GROUP_ERROR_CONNECTOR_DISCONNECTED,
ERROR_GROUP_ERROR_FANS_DISCONNECTED,
ERROR_GROUP_ERROR_FEEDBACK,
ERROR_GROUP_ERROR_FEEDBACK_CIRCUITRY,
ERROR_GROUP_ERROR_EEPROM_COMM,
ERROR_GROUP_ERROR_EEPROM_WRITE,
ERROR_GROUP_COUNT
} ErrorGroup;

// Single error instance type definition
/** @brief Single error instance type definition */
typedef uint16_t ErrorInstance;

/**
Expand All @@ -73,6 +94,7 @@ typedef struct {

/**
* @brief Initialize the internal error handler structures
*
* @details A critical section is defined as a block of code where, if an interrupt
* happens, undefined behaviour with the modified data within the block can happen
*
Expand All @@ -83,14 +105,14 @@ void error_init(void (* cs_enter)(void), void (* cs_exit)(void));

/**
* @brief Get the number of errors that has been set but they still have to expire
*
*
* @param size_t The number of running errors
*/
size_t error_get_running(void);

/**
* @brief Get the number of expired errors
*
*
* @param size_t The number of expired errors
*/
size_t error_get_expired(void);
Expand All @@ -99,6 +121,7 @@ size_t error_get_expired(void);
* @brief Get the number of running error of a specific group
*
* @param group The error group
*
* @return uint16_t The number of running errors
*/
uint16_t error_get_group_running(ErrorGroup group);
Expand All @@ -107,53 +130,62 @@ uint16_t error_get_group_running(ErrorGroup group);
* @brief Get the number of expired error of a specific group
*
* @param group The error group
*
* @return uint16_t The number of running errors
*/
uint16_t error_get_group_expired(ErrorGroup group);

/**
* @brief Get a copy of all the errors that are currently running
*
* @attention This function can be quite expensive in terms of time
* and should be used wisely, do not call to often
* @attention This function calls the critical section handler functions
*
* @details The out array should be able to contain all the instances
*
* @param out A pointer to an array of errors where the data is copied into
*
* @return size_t The number of copied errors
*/
size_t error_dump_running(Error * out);

/**
* @brief Get a copy of all the errors that are expired
*
* @attention This function can be quite expensive in terms of time
* and should be used wisely, do not call to often
* @attention This function calls the critical section handler functions
*
* @details The out array should be able to contain all the instances
*
* @param out A pointer to an array of errors where the data is copied into
*
* @return size_t The number of copied errors
*/
size_t error_dump_expired(Error * out);

/**
* @brief Get all the groups in which at least one error is running
*
*
* @param out A pointer to an array of groups where the data is copied into
*
* @return size_t The number of copied groups
*/
size_t error_dump_running_groups(ErrorGroup * out);

/**
* @brief Get all the groups in which at least one error is expired
*
*
* @param out A pointer to an array of groups where the data is copied into
*
* @return size_t The number of copied groups
*/
size_t error_dump_expired_groups(ErrorGroup * out);

/**
* @brief Set an error which will expire after a certain amount of time (the timeout)
*
*
* @param group The group to which the error belongs
* @param instance The instance of the error
* @param The current time (in ms)
Expand All @@ -171,17 +203,29 @@ void error_reset(ErrorGroup group, ErrorInstance instance);
/** @brief Set the error as expired */
void error_expire(void);

/**
* @brief Set the error as expired immediately even if it is not running
*
* @param group The group to which the error belongs
* @param instance The instance of the error
*/
void error_expire_immediate(ErrorGroup group, ErrorInstance instance);

/**
* @brief Routine that updates the internal error states
*
* @attention This function should not be called inside interrupts callback
* or other threads
*
* @details This function should be called periodically
*/
void error_routine(void);

/**
* @brief Update the timer that should expire the error after a certain amount of time
*
* @attention This function have to be defined by the user
*
* @details This function is called internally when an error is set, reset or expired
*
* @param timestamp The time in which the error was set (in ms)
Expand All @@ -191,7 +235,9 @@ void error_update_timer_callback(uint32_t timestamp, uint16_t timeout);

/**
* @brief Stop the timer that should expire the errors
*
* @attention This function have to be defined by the user
*
* @details This function is called internally when an error is reset or expired
*/
void error_stop_timer_callback(void);
Expand Down
2 changes: 2 additions & 0 deletions mainboard/Inc/error/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef ERROR_H
#define ERROR_H

#include <stdint.h>

/**
* @brief Enter a critical section where interrupts can cause problems
* @details For more info refer to the docs
Expand Down
2 changes: 1 addition & 1 deletion mainboard/Src/bms_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ void fsm_run() {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, led_status);

// Set or reset connection error
ERROR_TOGGLE_IF(HAL_GPIO_ReadPin(CONNS_DETECTION_GPIO_Port, CONNS_DETECTION_Pin) == GPIO_PIN_RESET, ERROR_CONNECTOR_DISCONNECTED, 0, HAL_GetTick());
ERROR_TOGGLE_IF(HAL_GPIO_ReadPin(CONNS_DETECTION_GPIO_Port, CONNS_DETECTION_Pin) == GPIO_PIN_RESET, ERROR_GROUP_ERROR_CONNECTOR_DISCONNECTED, 0, HAL_GetTick());

// Run the FSM and updates
fsm_state = run_state(fsm_state, NULL);
Expand Down
28 changes: 14 additions & 14 deletions mainboard/Src/cli_bms.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ const char * bms_state_names[NUM_STATES] = {
const char *bal_state_names[2] = { "off", "discharging" };

const char * error_names[] = {
[ERROR_CELL_UNDER_VOLTAGE] = "under-voltage",
[ERROR_CELL_OVER_VOLTAGE] = "over-voltage",
[ERROR_CELL_UNDER_TEMPERATURE] = "under-temperature",
[ERROR_CELL_OVER_TEMPERATURE] = "over-temperature",
[ERROR_OVER_CURRENT] = "over-current",
[ERROR_CAN] = "CAN",
[ERROR_INT_VOLTAGE_MISMATCH] = "internal voltage mismatch",
[ERROR_CELLBOARD_COMM] = "cellboard communication",
[ERROR_CELLBOARD_INTERNAL] = "cellboard internal",
[ERROR_CONNECTOR_DISCONNECTED] = "connection error",
[ERROR_FEEDBACK] = "feedback",
[ERROR_FEEDBACK_CIRCUITRY] = "feedback_circuitry",
[ERROR_EEPROM_COMM] = "EEPROM communication",
[ERROR_EEPROM_WRITE] = "EEPROM write"};
[ERROR_GROUP_ERROR_CELL_UNDER_VOLTAGE] = "under-voltage",
[ERROR_GROUP_ERROR_CELL_OVER_VOLTAGE] = "over-voltage",
[ERROR_GROUP_ERROR_CELL_UNDER_TEMPERATURE] = "under-temperature",
[ERROR_GROUP_ERROR_CELL_OVER_TEMPERATURE] = "over-temperature",
[ERROR_GROUP_ERROR_OVER_CURRENT] = "over-current",
[ERROR_GROUP_ERROR_CAN] = "CAN",
[ERROR_GROUP_ERROR_INT_VOLTAGE_MISMATCH] = "internal voltage mismatch",
[ERROR_GROUP_ERROR_CELLBOARD_COMM] = "cellboard communication",
[ERROR_GROUP_ERROR_CELLBOARD_INTERNAL] = "cellboard internal",
[ERROR_GROUP_ERROR_CONNECTOR_DISCONNECTED] = "connection error",
[ERROR_GROUP_ERROR_FEEDBACK] = "feedback",
[ERROR_GROUP_ERROR_FEEDBACK_CIRCUITRY] = "feedback_circuitry",
[ERROR_GROUP_ERROR_EEPROM_COMM] = "EEPROM communication",
[ERROR_GROUP_ERROR_EEPROM_WRITE] = "EEPROM write"};

char const *const feedback_names[FEEDBACK_N] = {
[FEEDBACK_IMPLAUSIBILITY_DETECTED_POS] = "FEEDBACK_IMPLAUSBILITY_DETECTED",
Expand Down
12 changes: 6 additions & 6 deletions mainboard/Src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool config_read(config_t *config) {

if (m95256_ReadBuffer(eeprom, buffer, config->address, config->size + CONFIG_VERSION_SIZE) ==
EEPROM_STATUS_COMPLETE) {
error_reset(ERROR_EEPROM_COMM, 0);
error_reset(ERROR_GROUP_ERROR_EEPROM_COMM, 0);

// Check if EEPROM's version matches config's
if (*((CONFIG_VERSION_TYPE *)buffer) == config->version) {
Expand All @@ -55,7 +55,7 @@ bool config_read(config_t *config) {
return true;
}
} else {
error_set(ERROR_EEPROM_COMM, 0, HAL_GetTick());
error_set(ERROR_GROUP_ERROR_EEPROM_COMM, 0, HAL_GetTick());
}
return false;
}
Expand All @@ -68,24 +68,24 @@ bool config_write(config_t *config) {

if (m95256_WriteBuffer(eeprom, buffer, config->address, config->size + CONFIG_VERSION_SIZE) ==
EEPROM_STATUS_COMPLETE) {
error_reset(ERROR_EEPROM_COMM, 0);
error_reset(ERROR_GROUP_ERROR_EEPROM_COMM, 0);

// Read just-written data and compare for errors
uint8_t testbuf[EEPROM_BUFFER_SIZE] = {0};
if (m95256_ReadBuffer(eeprom, testbuf, config->address, config->size + CONFIG_VERSION_SIZE) ==
EEPROM_STATUS_COMPLETE) {
if (memcmp(buffer, testbuf, CONFIG_VERSION_SIZE) == 0) {
error_reset(ERROR_EEPROM_WRITE, 0);
error_reset(ERROR_GROUP_ERROR_EEPROM_WRITE, 0);
config->dirty = false;
return true;
}

error_set(ERROR_EEPROM_WRITE, 0, HAL_GetTick());
error_set(ERROR_GROUP_ERROR_EEPROM_WRITE, 0, HAL_GetTick());
return false;
}

} else {
error_set(ERROR_EEPROM_COMM, 0, HAL_GetTick());
error_set(ERROR_GROUP_ERROR_EEPROM_COMM, 0, HAL_GetTick());
return false;
}
}
Expand Down
Loading

0 comments on commit e4455c4

Please sign in to comment.