First draft of safety controller
This commit is contained in:
		@@ -34,6 +34,7 @@ enum safety_flag {
 | 
			
		||||
	ERR_FLAG_STACK = (1<<8),
 | 
			
		||||
	ERR_FLAG_SAFETY_ADC = (1<<9),
 | 
			
		||||
	ERR_FLAG_SYSTICK = (1<<10),
 | 
			
		||||
	ERR_FLAG_WTCHDG_FIRED = (1<<11),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum timing_monitor {
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,7 @@
 | 
			
		||||
 | 
			
		||||
#include <reflow-controller/safety/safety-config.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
enum analog_monitor_status {ANALOG_MONITOR_OK = 0,
 | 
			
		||||
			    ANALOG_MONITOR_ERROR,
 | 
			
		||||
@@ -66,6 +67,8 @@ int safety_controller_get_flag(enum safety_flag flag, bool *status, bool try_ack
 | 
			
		||||
 | 
			
		||||
int safety_controller_ack_flag(enum safety_flag flag);
 | 
			
		||||
 | 
			
		||||
int safety_controller_ack_flag_with_key(enum safety_flag flag, uint32_t key);
 | 
			
		||||
 | 
			
		||||
#endif /* __SAFETY_CONTROLLER_H__ */
 | 
			
		||||
 | 
			
		||||
/** @} */
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,7 @@
 | 
			
		||||
 | 
			
		||||
#include <reflow-controller/safety/safety-config.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Setup the watchdog for the safety controller
 | 
			
		||||
@@ -39,5 +40,11 @@ int watchdog_setup(uint8_t prescaler);
 | 
			
		||||
 */
 | 
			
		||||
int watchdog_ack(uint32_t magic);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Check if reset was generated by the watchdog.
 | 
			
		||||
 * @note This also clears the relevant flag, so the function will reutrn false when called a second time
 | 
			
		||||
 * @return
 | 
			
		||||
 */
 | 
			
		||||
bool watchdog_check_reset_source(void);
 | 
			
		||||
 | 
			
		||||
#endif /* __WATCHDOG_H__ */
 | 
			
		||||
 
 | 
			
		||||
@@ -64,5 +64,4 @@ int rcc_manager_enable_clock(volatile uint32_t *rcc_enable_register, uint8_t bit
 | 
			
		||||
 */
 | 
			
		||||
int rcc_manager_disable_clock(volatile uint32_t *rcc_enable_register, uint8_t bit_no);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif /* __CLOCK_ENABLE_MANAGER_H__ */
 | 
			
		||||
 
 | 
			
		||||
@@ -30,9 +30,6 @@
 | 
			
		||||
#include <reflow-controller/stack-check.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
#include <reflow-controller/systick.h>
 | 
			
		||||
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
 | 
			
		||||
struct error_flag {
 | 
			
		||||
@@ -40,6 +37,7 @@ struct error_flag {
 | 
			
		||||
	enum safety_flag flag;
 | 
			
		||||
	bool error_state;
 | 
			
		||||
	bool persistent;
 | 
			
		||||
	uint32_t key;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct timing_mon {
 | 
			
		||||
@@ -68,7 +66,7 @@ struct analog_mon {
 | 
			
		||||
 | 
			
		||||
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
 | 
			
		||||
 | 
			
		||||
#define ERR_FLAG_ENTRY(errflag, persistency) {.name=#errflag, .flag = (errflag), .error_state = false, .persistent = (persistency)}
 | 
			
		||||
#define ERR_FLAG_ENTRY(errflag, persistency) {.name=#errflag, .flag = (errflag), .error_state = false, .persistent = (persistency), .key = 0UL}
 | 
			
		||||
#define TIM_MON_ENTRY(mon, min, max, flag) {.name=#mon, .monitor = (mon), .associated_flag=(flag), .min_delta = (min), .max_delta = (max), .last = 0ULL, .enabled= false}
 | 
			
		||||
#define ANA_MON_ENTRY(mon, min_value, max_value, flag) {.name=#mon, .monitor = (mon), .associated_flag=(flag), .min = (min_value), .max = (max_value), .value = 0.0f, .valid = false}
 | 
			
		||||
 | 
			
		||||
@@ -84,6 +82,7 @@ static struct error_flag flags[] = {
 | 
			
		||||
	ERR_FLAG_ENTRY(ERR_FLAG_STACK, true),
 | 
			
		||||
	ERR_FLAG_ENTRY(ERR_FLAG_SAFETY_ADC, true),
 | 
			
		||||
	ERR_FLAG_ENTRY(ERR_FLAG_SYSTICK, true),
 | 
			
		||||
	ERR_FLAG_ENTRY(ERR_FLAG_WTCHDG_FIRED, true),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct timing_mon timings[] = {
 | 
			
		||||
@@ -301,6 +300,8 @@ int safety_controller_handle()
 | 
			
		||||
 | 
			
		||||
	safety_controller_check_stack();
 | 
			
		||||
	safety_controller_handle_safety_adc();
 | 
			
		||||
	if (watchdog_check_reset_source())
 | 
			
		||||
		safety_controller_report_error(ERR_FLAG_WTCHDG_FIRED);
 | 
			
		||||
 | 
			
		||||
	safety_controller_process_checks();
 | 
			
		||||
 | 
			
		||||
@@ -371,14 +372,24 @@ int safety_controller_get_flag(enum safety_flag flag, bool *status, bool try_ack
 | 
			
		||||
	found_flag = find_error_flag(flag);
 | 
			
		||||
	if (found_flag) {
 | 
			
		||||
		*status = found_flag->error_state;
 | 
			
		||||
		if (try_ack && !found_flag->persistent)
 | 
			
		||||
			found_flag->error_state = false;
 | 
			
		||||
		if (try_ack && !found_flag->persistent) {
 | 
			
		||||
			/* Flag is generally non persistent
 | 
			
		||||
			 * If key is set, this function cannot remove the flag
 | 
			
		||||
			 */
 | 
			
		||||
			if (found_flag->key == 0UL)
 | 
			
		||||
				found_flag->error_state = false;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int safety_controller_ack_flag(enum safety_flag flag)
 | 
			
		||||
{
 | 
			
		||||
	return safety_controller_ack_flag_with_key(flag, 0UL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int safety_controller_ack_flag_with_key(enum safety_flag flag, uint32_t key)
 | 
			
		||||
{
 | 
			
		||||
	int ret = -1;
 | 
			
		||||
	struct error_flag *found_flag;
 | 
			
		||||
@@ -389,7 +400,7 @@ int safety_controller_ack_flag(enum safety_flag flag)
 | 
			
		||||
 | 
			
		||||
	found_flag = find_error_flag(flag);
 | 
			
		||||
	if (found_flag) {
 | 
			
		||||
		if (!found_flag->persistent) {
 | 
			
		||||
		if (!found_flag->persistent && found_flag->key == key) {
 | 
			
		||||
			found_flag->error_state = false;
 | 
			
		||||
			ret = 0;
 | 
			
		||||
		} else {
 | 
			
		||||
 
 | 
			
		||||
@@ -105,4 +105,16 @@ int watchdog_ack(uint32_t magic)
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool watchdog_check_reset_source(void)
 | 
			
		||||
{
 | 
			
		||||
	bool ret;
 | 
			
		||||
 | 
			
		||||
	ret = !!(RCC->CSR & RCC_CSR_WDGRSTF);
 | 
			
		||||
 | 
			
		||||
	if (ret)
 | 
			
		||||
		RCC->CSR |= RCC_CSR_RMVF;
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** @} */
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user