Make safety controller use config overrides from backup ram
This commit is contained in:
		@@ -23,6 +23,7 @@
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
/** @addtogroup safety-memory
 | 
			
		||||
 * @{
 | 
			
		||||
@@ -143,7 +144,7 @@ struct config_override {
 | 
			
		||||
		} weight_override;
 | 
			
		||||
		struct {
 | 
			
		||||
			uint8_t flag;
 | 
			
		||||
			uint8_t persistance;
 | 
			
		||||
			bool persistance;
 | 
			
		||||
		} persistance_override;
 | 
			
		||||
	} entry;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -134,6 +134,33 @@ static uint32_t IN_SECTION(.ccm.bss) flag_persistencies_crc;
 | 
			
		||||
static volatile struct safety_weight IN_SECTION(.ccm.bss) flag_weights[COUNT_OF(default_flag_weights)];
 | 
			
		||||
static uint32_t IN_SECTION(.ccm.bss) flag_weight_crc;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static uint8_t flag_enum_to_flag_no(enum safety_flag flag)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t flag_mask;
 | 
			
		||||
	uint8_t i;
 | 
			
		||||
 | 
			
		||||
	if (!is_power_of_two(flag))
 | 
			
		||||
		return 0xFF;
 | 
			
		||||
 | 
			
		||||
	flag_mask = (uint32_t)flag;
 | 
			
		||||
	for (i = 0; i < 32; i++) {
 | 
			
		||||
		if ((flag_mask >> i) & 0x1U)
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static enum safety_flag flag_no_to_flag_enum(uint8_t no)
 | 
			
		||||
{
 | 
			
		||||
	if (no >= COUNT_OF(flags))
 | 
			
		||||
		return ERR_FLAG_NO_FLAG;
 | 
			
		||||
 | 
			
		||||
	return (1U << no);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int flag_weight_table_crc_check(void)
 | 
			
		||||
{
 | 
			
		||||
	/* Check the flag weight table */
 | 
			
		||||
@@ -215,6 +242,51 @@ static void init_safety_flag_persistencies_from_default(void)
 | 
			
		||||
	flag_persistencies_crc = crc_unit_get_crc();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void apply_config_overrides(void)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t count;
 | 
			
		||||
	uint32_t idx;
 | 
			
		||||
	struct config_override override;
 | 
			
		||||
	int res;
 | 
			
		||||
	enum safety_flag flag_enum;
 | 
			
		||||
	volatile struct error_flag *flag;
 | 
			
		||||
 | 
			
		||||
	res = safety_memory_get_config_override_count(&count);
 | 
			
		||||
	if (res)
 | 
			
		||||
		return;
 | 
			
		||||
	for (idx = 0; idx < count; idx++) {
 | 
			
		||||
		res = safety_memory_get_config_override(idx, &override);
 | 
			
		||||
		if (res)
 | 
			
		||||
			continue;
 | 
			
		||||
		switch (override.type) {
 | 
			
		||||
		case SAFETY_MEMORY_CONFIG_OVERRIDE_WEIGHT:
 | 
			
		||||
			flag_enum = flag_no_to_flag_enum(override.entry.weight_override.flag);
 | 
			
		||||
			flag = find_error_flag(flag_enum);
 | 
			
		||||
			if (flag && flag->weight) {
 | 
			
		||||
				flag->weight->weight = override.entry.weight_override.weight;
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		case SAFETY_MEMORY_CONFIG_OVERRIDE_PERSISTANCE:
 | 
			
		||||
			flag_enum = flag_no_to_flag_enum(override.entry.persistance_override.flag);
 | 
			
		||||
			flag = find_error_flag(flag_enum);
 | 
			
		||||
			if (flag && flag->persistency) {
 | 
			
		||||
				flag->persistency->persistency = override.entry.persistance_override.persistance;
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Patch new CRCs */
 | 
			
		||||
	crc_unit_reset();
 | 
			
		||||
	crc_unit_input_array((uint32_t *)flag_persistencies, wordsize_of(flag_persistencies));
 | 
			
		||||
	flag_persistencies_crc = crc_unit_get_crc();
 | 
			
		||||
	crc_unit_reset();
 | 
			
		||||
	crc_unit_input_array((uint32_t*)flag_weights, wordsize_of(flag_weights));
 | 
			
		||||
	flag_weight_crc = crc_unit_get_crc();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool error_flag_get_status(const volatile struct error_flag *flag)
 | 
			
		||||
{
 | 
			
		||||
	if (!flag)
 | 
			
		||||
@@ -292,31 +364,6 @@ static void safety_controller_process_monitor_checks()
 | 
			
		||||
	safety_controller_process_active_timing_mons();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint8_t flag_enum_to_flag_no(enum safety_flag flag)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t flag_mask;
 | 
			
		||||
	uint8_t i;
 | 
			
		||||
 | 
			
		||||
	if (!is_power_of_two(flag))
 | 
			
		||||
		return 0xFF;
 | 
			
		||||
 | 
			
		||||
	flag_mask = (uint32_t)flag;
 | 
			
		||||
	for (i = 0; i < 32; i++) {
 | 
			
		||||
		if ((flag_mask >> i) & 0x1U)
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static enum safety_flag flag_no_to_flag_enum(uint8_t no)
 | 
			
		||||
{
 | 
			
		||||
	if (no >= COUNT_OF(flags))
 | 
			
		||||
		return ERR_FLAG_NO_FLAG;
 | 
			
		||||
 | 
			
		||||
	return (1U << no);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int report_error(enum safety_flag flag, uint32_t key, bool prevent_error_mem_enty)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t i;
 | 
			
		||||
@@ -446,6 +493,7 @@ void safety_controller_init()
 | 
			
		||||
 | 
			
		||||
	init_safety_flag_weight_table_from_default();
 | 
			
		||||
	init_safety_flag_persistencies_from_default();
 | 
			
		||||
	apply_config_overrides();
 | 
			
		||||
 | 
			
		||||
	if (found_memory_state == SAFETY_MEMORY_INIT_CORRUPTED)
 | 
			
		||||
		safety_controller_report_error(ERR_FLAG_SAFETY_MEM_CORRUPT);
 | 
			
		||||
 
 | 
			
		||||
@@ -489,7 +489,7 @@ static uint32_t convert_config_override_to_word(const struct config_override *co
 | 
			
		||||
	} else if (conf_override->type == SAFETY_MEMORY_CONFIG_OVERRIDE_PERSISTANCE) {
 | 
			
		||||
		data |= 0xBB00008EUL;
 | 
			
		||||
		data |= ((uint32_t)conf_override->entry.persistance_override.flag) << 16;
 | 
			
		||||
		data |= ((uint32_t)conf_override->entry.persistance_override.persistance) << 8;
 | 
			
		||||
		data |= ((uint32_t)(conf_override->entry.persistance_override.persistance ? 1UL : 0UL)) << 8;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return data;
 | 
			
		||||
@@ -599,7 +599,7 @@ int safety_memory_get_config_override(uint32_t idx, struct config_override *conf
 | 
			
		||||
		/* persistance override */
 | 
			
		||||
		config_override->type = SAFETY_MEMORY_CONFIG_OVERRIDE_PERSISTANCE;
 | 
			
		||||
		config_override->entry.persistance_override.flag = (data & 0xFF0000UL) >> 16;
 | 
			
		||||
		config_override->entry.persistance_override.persistance = (data & 0xFF00UL) >> 8;
 | 
			
		||||
		config_override->entry.persistance_override.persistance = ((data & 0xFF00UL) >> 8) ? true : false;
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		return -2;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user