diff --git a/stm-firmware/calibration.c b/stm-firmware/calibration.c index 432b580..3919a29 100644 --- a/stm-firmware/calibration.c +++ b/stm-firmware/calibration.c @@ -193,6 +193,7 @@ shellmatta_retCode_t calibration_sequence_shell_cmd(shellmatta_handle_t shell, c case CAL_START: /* Clear errors of PT1000 reading */ safety_controller_ack_flag(ERR_FLAG_MEAS_ADC_WATCHDOG); + safety_controller_ack_flag(ERR_FLAG_OVERTEMP); adc_pt1000_get_resistance_calibration(&offset, &sens_dev, &cal_active); if (cal_active) { shellmatta_printf(shell, "Already calibrated.\r\n\tOffset: %f\r\n\tSens: %f\r\n", offset, sens_dev); @@ -213,6 +214,7 @@ shellmatta_retCode_t calibration_sequence_shell_cmd(shellmatta_handle_t shell, c ret_val = SHELLMATTA_BUSY; shellmatta_printf(shell, "Measurement...\r\n"); safety_controller_ack_flag(ERR_FLAG_MEAS_ADC_WATCHDOG); + safety_controller_ack_flag(ERR_FLAG_OVERTEMP); data_buffer = calibration_acquire_data_start(512UL, &flag); break; } else if (stdin_data[i] == '\x03') { @@ -264,6 +266,7 @@ shellmatta_retCode_t calibration_sequence_shell_cmd(shellmatta_handle_t shell, c ret_val = SHELLMATTA_BUSY; shellmatta_printf(shell, "Measurement...\r\n"); safety_controller_ack_flag(ERR_FLAG_MEAS_ADC_WATCHDOG); + safety_controller_ack_flag(ERR_FLAG_OVERTEMP); data_buffer = calibration_acquire_data_start(512UL, &flag); break; } else if (stdin_data[i] == '\x03') { diff --git a/stm-firmware/include/reflow-controller/safety/safety-config.h b/stm-firmware/include/reflow-controller/safety/safety-config.h index f1e3d93..381723c 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-config.h +++ b/stm-firmware/include/reflow-controller/safety/safety-config.h @@ -55,6 +55,7 @@ enum safety_flag { ERR_FLAG_SAFETY_MEM_CORRUPT = (1<<15), ERR_FLAG_SAFETY_TAB_CORRUPT = (1<<16), ERR_FLAG_AMON_SUPPLY_VOLT = (1<<17), + ERR_FLAG_OVERTEMP = (1<<18), }; /** @@ -128,6 +129,11 @@ enum analog_value_monitor { #define SAFETY_EXT_WATCHDOG_RCC_MASK RCC_AHB1ENR_GPIODEN #define SAFETY_EXT_WATCHDOG_PIN (12) +/** + * @brief Default Limit of the overtemperature detection + */ +#define SAFETY_DEFAULT_OVERTEMP_LIMIT_DEGC (260.0f) + /** * @brief Key used to lock the safety flags coming from the measurment ADC from external ack'ing */ @@ -156,6 +162,7 @@ enum analog_value_monitor { ERR_FLAG_PERSIST_ENTRY(ERR_FLAG_SAFETY_MEM_CORRUPT, true), \ ERR_FLAG_PERSIST_ENTRY(ERR_FLAG_SAFETY_TAB_CORRUPT, true), \ ERR_FLAG_PERSIST_ENTRY(ERR_FLAG_AMON_SUPPLY_VOLT, false), \ + ERR_FLAG_PERSIST_ENTRY(ERR_FLAG_OVERTEMP, false), \ #define SAFETY_CONFIG_DEFAULT_WEIGHTS ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_MEAS_ADC_OFF, SAFETY_FLAG_CONFIG_WEIGHT_PID), \ ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_MEAS_ADC_WATCHDOG, SAFETY_FLAG_CONFIG_WEIGHT_PID), \ @@ -177,5 +184,6 @@ enum analog_value_monitor { ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_SAFETY_MEM_CORRUPT, SAFETY_FLAG_CONFIG_WEIGHT_PID), \ ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_SAFETY_TAB_CORRUPT, SAFETY_FLAG_CONFIG_WEIGHT_PANIC), \ ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_AMON_SUPPLY_VOLT, SAFETY_FLAG_CONFIG_WEIGHT_PID), \ + ERR_FLAG_WEIGHT_ENTRY(ERR_FLAG_OVERTEMP, SAFETY_FLAG_CONFIG_WEIGHT_PID), \ #endif /* __SAFETY_CONFIG_H__ */ diff --git a/stm-firmware/include/reflow-controller/safety/safety-controller.h b/stm-firmware/include/reflow-controller/safety/safety-controller.h index 790d58d..730628b 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-controller.h +++ b/stm-firmware/include/reflow-controller/safety/safety-controller.h @@ -250,7 +250,22 @@ int safety_controller_get_timing_mon_name_by_index(uint32_t index, char *buffer, * @brief Get the count of timing monitors * @return Timing monitor count */ -uint32_t safety_controller_get_timing_monitor_count(); +uint32_t safety_controller_get_timing_monitor_count(void); + +/** + * @brief Set the overtemperature limit and store it permanently in the EEPROM + * + * If no EEPROM is present, this will fail. The default value @ref SAFETY_DEFAULT_OVERTEMP_LIMIT_DEGC will be used. + * @param over_temperature Over temperature to set + * @return 0 if successfully saved and applied, negative if error + */ +int safety_controller_set_overtemp_limit(float over_temperature); + +/** + * @brief Read the current overtemperature limit. + * @return Over temperature limit + */ +float safety_controller_get_overtemp_limit(void); #endif /* __SAFETY_CONTROLLER_H__ */ diff --git a/stm-firmware/include/reflow-controller/settings/settings-eeprom.h b/stm-firmware/include/reflow-controller/settings/settings-eeprom.h index 7fc0153..f7b0ee7 100644 --- a/stm-firmware/include/reflow-controller/settings/settings-eeprom.h +++ b/stm-firmware/include/reflow-controller/settings/settings-eeprom.h @@ -29,4 +29,8 @@ int settings_eeprom_save_calibration(float sens, float offset, bool active); int settings_eeprom_load_calibration(float *sens, float *offset, bool *active); +int settings_eeprom_save_overtemp_limit(float overtemp_limit, bool active); + +int settings_eeprom_load_overtemp_limit(float *overtemp_limit); + #endif /* __SETTINGS_SETTINGS_EEPROM_H__ */ diff --git a/stm-firmware/include/reflow-controller/settings/settings.h b/stm-firmware/include/reflow-controller/settings/settings.h index 9168991..abe0347 100644 --- a/stm-firmware/include/reflow-controller/settings/settings.h +++ b/stm-firmware/include/reflow-controller/settings/settings.h @@ -54,6 +54,10 @@ int settings_load_calibration(float *sens_dev, float *offset); enum settings_load_result settings_load_pid_oven_parameters(struct oven_pid_settings *settings); +enum settings_load_result settings_load_overtemp_limit(float *over_temp_limit); + +int settings_save_overtemp_limit(float over_temp_limit, bool active); + void settings_setup(void); #endif /* __SETTINGS_SETTINGS_H__ */ diff --git a/stm-firmware/include/reflow-controller/temp-converter.h b/stm-firmware/include/reflow-controller/temp-converter.h index febd392..a126b13 100644 --- a/stm-firmware/include/reflow-controller/temp-converter.h +++ b/stm-firmware/include/reflow-controller/temp-converter.h @@ -29,4 +29,13 @@ */ int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out); +/** + * @brief Convert temperature to PT1000 resistance + * @param temp Temperature in degrees celsius + * @param[out] resistance_out Resistance value + * @return 0 if ok, -1 if tmeperature is below the lookup table, 1 if value is above the lookup table, -1000 in case of a pointer error, + * -100 if an internal error occured. This should never happen, if the lookup table is correct + */ +int temp_convertet_convert_temp_to_resistance(float temp, float *resistance_out); + #endif /* __TEMP_CONVERTER_H__ */ diff --git a/stm-firmware/main.c b/stm-firmware/main.c index 7c90768..dc1f805 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -168,6 +168,8 @@ static inline void handle_boot_status(void) static inline void setup_system(void) { + float tmp; + setup_nvic_priorities(); /* Init safety controller and safety memory */ @@ -182,6 +184,11 @@ static inline void setup_system(void) uart_gpio_config(); settings_setup(); + /* Load the overtemperature limit from eeprom if available. Otherwise the default value will be used */ + if (settings_load_overtemp_limit(&tmp) == SETT_LOAD_SUCCESS) { + safety_controller_set_overtemp_limit(tmp); + } + handle_boot_status(); setup_shell_uart(&shell_uart); diff --git a/stm-firmware/safety/safety-controller.c b/stm-firmware/safety/safety-controller.c index 17affc0..02f533f 100644 --- a/stm-firmware/safety/safety-controller.c +++ b/stm-firmware/safety/safety-controller.c @@ -41,6 +41,8 @@ #include #include #include +#include +#include /** * @brief Macro that checks if a given @ref error_flag is persistent @@ -144,6 +146,13 @@ struct analog_mon { uint64_t timestamp; }; +struct overtemp_config { + uint32_t crc_dummy_seed; + float overtemp_deg_celsius; + float overtemp_equiv_resistance; + uint32_t crc; +}; + /** * @brief All safety error flags. */ @@ -166,6 +175,7 @@ static volatile struct error_flag IN_SECTION(.ccm.data) flags[] = { ERR_FLAG_ENTRY(ERR_FLAG_SAFETY_MEM_CORRUPT), ERR_FLAG_ENTRY(ERR_FLAG_SAFETY_TAB_CORRUPT), ERR_FLAG_ENTRY(ERR_FLAG_AMON_SUPPLY_VOLT), + ERR_FLAG_ENTRY(ERR_FLAG_OVERTEMP), }; /** @@ -233,6 +243,45 @@ static volatile struct safety_weight IN_SECTION(.ccm.bss) flag_weights[COUNT_OF( */ static uint32_t IN_SECTION(.ccm.bss) flag_weight_crc; +/** + * @brief Configuration struct containing the overtemperature flag configuration + */ +static struct overtemp_config IN_SECTION(.ccm.bss) safety_controller_overtemp_config; + +/** + * @brief Configure the overtemperature flag's settings + * @param over_temperature Temperature to set the limit to. + */ +static void set_overtemp_config(float over_temperature) +{ + int result; + float resistance; + + result = temp_convertet_convert_temp_to_resistance(over_temperature, &resistance); + /* An error in this function is really bad... */ + if (result < -1) + panic_mode(); + + crc_unit_reset(); + safety_controller_overtemp_config.crc_dummy_seed = 0xA4F5C7E6UL; + safety_controller_overtemp_config.overtemp_deg_celsius = over_temperature; + safety_controller_overtemp_config.overtemp_equiv_resistance = resistance; + crc_unit_input_array((const uint32_t *)&safety_controller_overtemp_config, wordsize_of(struct overtemp_config) - 1); + safety_controller_overtemp_config.crc = crc_unit_get_crc(); +} + +static bool over_temperature_config_check(void) +{ + if (safety_controller_overtemp_config.crc_dummy_seed != 0xA4F5C7E6UL) + return true; + crc_unit_reset(); + crc_unit_input_array((const uint32_t *)&safety_controller_overtemp_config, wordsize_of(struct overtemp_config) - 1); + if (crc_unit_get_crc() != safety_controller_overtemp_config.crc) + return true; + + return false; +} + /** * @brief Convert a flag enum to the flag number. * @@ -515,6 +564,7 @@ static void safety_controller_process_active_timing_mons() /** * @brief safety_controller_process_monitor_checks * Process the analog and timing monitors and set the relevant flags in case of a monitor outside its limits. + * Furthermore, the PT1000 resistance is checked for overtemperature * * The checking of the analog monitors will only be armed after a startup delay of 1000 ms to allow the values to stabilize. */ @@ -524,6 +574,7 @@ static void safety_controller_process_monitor_checks(void) struct analog_monitor_info amon_info; uint32_t idx; uint32_t analog_mon_count; + float pt1000_val = 1000000.0f; if (!startup_completed && systick_get_global_tick() >= 1000) startup_completed = true; @@ -541,6 +592,11 @@ static void safety_controller_process_monitor_checks(void) } } + adc_pt1000_get_current_resistance(&pt1000_val); + if (pt1000_val > safety_controller_overtemp_config.overtemp_equiv_resistance) { + safety_controller_report_error(ERR_FLAG_OVERTEMP); + } + safety_controller_process_active_timing_mons(); } @@ -720,6 +776,9 @@ void safety_controller_init() init_safety_flag_persistencies_from_default(); apply_config_overrides(); + /* Set the default limit of the overtemperature check */ + set_overtemp_config(SAFETY_DEFAULT_OVERTEMP_LIMIT_DEGC); + if (found_memory_state == SAFETY_MEMORY_INIT_CORRUPTED) safety_controller_report_error(ERR_FLAG_SAFETY_MEM_CORRUPT); else if (found_memory_state == SAFETY_MEMORY_INIT_VALID_MEMORY) { @@ -841,6 +900,7 @@ static void safety_controller_handle_safety_adc() * Aditionally, the default flag weights are restored from Flash. * 3) The flag persistency table is CRC checked. In case of an error, the @ref ERR_FLAG_SAFETY_TAB_CORRUPT flag is set. * Aditionally, the default values of the flag persistence is restored from Flash. + * 4) Check the Overtemperature flag configuration structure */ static void safety_controller_handle_memory_checks(void) { @@ -869,6 +929,12 @@ static void safety_controller_handle_memory_checks(void) safety_controller_report_error(ERR_FLAG_SAFETY_TAB_CORRUPT); init_safety_flag_persistencies_from_default(); } + + /* check overtemp struct */ + if (over_temperature_config_check()) { + safety_controller_report_error(ERR_FLAG_SAFETY_TAB_CORRUPT); + set_overtemp_config(SAFETY_DEFAULT_OVERTEMP_LIMIT_DEGC); + } } } @@ -1219,4 +1285,15 @@ int safety_controller_get_timing_mon_by_index(uint32_t index, struct timing_moni return 0; } +int safety_controller_set_overtemp_limit(float over_temperature) +{ + set_overtemp_config(over_temperature); + return 0; +} + +float safety_controller_get_overtemp_limit(void) +{ + return safety_controller_overtemp_config.overtemp_deg_celsius; +} + /** @} */ diff --git a/stm-firmware/settings/settings-eeprom.c b/stm-firmware/settings/settings-eeprom.c index 99171bb..b9d7958 100644 --- a/stm-firmware/settings/settings-eeprom.c +++ b/stm-firmware/settings/settings-eeprom.c @@ -23,8 +23,8 @@ #include #include -#define EEPROM_HEADER_MAGIC 0xA5 -#define EEPROM_HEADER_COMP_VER 0xFF +#define EEPROM_HEADER_MAGIC 0xC5 +#define EEPROM_HEADER_COMP_VER 0x01 static const uint8_t expected_header[2] = {EEPROM_HEADER_MAGIC, EEPROM_HEADER_COMP_VER}; @@ -36,6 +36,12 @@ struct eeprom_calibration_settings { uint32_t crc; }; +#define EEPROM_OVER_TEMP_CONFIG_BASE_ADDR (EEPROM_CALIBRATION_BASE_ADDR + sizeof(struct eeprom_calibration_settings)) +struct eeprom_over_temp_config { + float over_temperature; + uint32_t over_temp_crc; +}; + static bool check_eeprom_header(void) { uint8_t header[2] = {0}; @@ -51,6 +57,7 @@ static bool check_eeprom_header(void) static void settings_eeprom_zero() { settings_eeprom_save_calibration(0.0f, 0.0f, false); + settings_eeprom_save_overtemp_limit(0.0, false); } bool settings_eeprom_detect_and_prepare(void) @@ -138,3 +145,46 @@ int settings_eeprom_load_calibration(float *sens, float *offset, bool *active) return -1; } } + +int settings_eeprom_save_overtemp_limit(float overtemp_limit, bool active) +{ + int res; + struct eeprom_over_temp_config over_temp_conf_entry; + const uint8_t zero_vals[sizeof(over_temp_conf_entry)] = {0}; + + if (!active) { + res = spi_eeprom_write(EEPROM_OVER_TEMP_CONFIG_BASE_ADDR, (const uint8_t *)zero_vals, + sizeof(zero_vals)); + return res ? -1 : 0; + } + + over_temp_conf_entry.over_temperature = overtemp_limit; + crc_unit_reset(); + crc_unit_input_array((uint32_t *)&over_temp_conf_entry, 1); + over_temp_conf_entry.over_temp_crc = crc_unit_get_crc(); + res = spi_eeprom_write(EEPROM_OVER_TEMP_CONFIG_BASE_ADDR, (const uint8_t *)&over_temp_conf_entry, + sizeof(over_temp_conf_entry)); + + return res ? -1 : 0; +} + +int settings_eeprom_load_overtemp_limit(float *overtemp_limit) +{ + int res; + struct eeprom_over_temp_config over_temp_conf_entry; + + if (!overtemp_limit) + return -1001; + res = spi_eeprom_read(EEPROM_OVER_TEMP_CONFIG_BASE_ADDR, (uint8_t *)&over_temp_conf_entry, + sizeof(struct eeprom_over_temp_config)); + if (res) + return -2; + + crc_unit_reset(); + crc_unit_input_array((uint32_t *)&over_temp_conf_entry, 1); + if (crc_unit_get_crc() != over_temp_conf_entry.over_temp_crc) + return -1; + + *overtemp_limit = over_temp_conf_entry.over_temperature; + return 0; +} diff --git a/stm-firmware/settings/settings.c b/stm-firmware/settings/settings.c index 7583e45..09d2ae8 100644 --- a/stm-firmware/settings/settings.c +++ b/stm-firmware/settings/settings.c @@ -64,3 +64,30 @@ void settings_setup(void) else settings_use_eeprom = false; } + +enum settings_load_result settings_load_overtemp_limit(float *over_temp_limit) +{ + int res; + float tmp; + + if (!over_temp_limit) + return SETT_LOAD_ERR; + + if (!settings_use_eeprom) + return SETT_LOAD_FILE_NOT_FOUND; + + res = settings_eeprom_load_overtemp_limit(&tmp); + if (res) + return SETT_LOAD_DISK_ERR; + *over_temp_limit = tmp; + + return SETT_LOAD_SUCCESS; +} + +int settings_save_overtemp_limit(float over_temp_limit, bool active) +{ + if (settings_use_eeprom) + return settings_eeprom_save_overtemp_limit(over_temp_limit, active); + else + return -100; +} diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 8e31918..9405c97 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -641,6 +641,48 @@ shellmatta_retCode_t shell_cmd_update(const shellmatta_handle_t handle, const ch return SHELLMATTA_OK; } +shellmatta_retCode_t shell_cmd_overtemp_cfg(const shellmatta_handle_t handle, const char *args, uint32_t len) +{ + float overtemp_lim; + shellmatta_retCode_t ret; + char *argument; + uint32_t arg_length; + char option; + bool temp_passed = false; + bool persistent = false; + + static const shellmatta_opt_long_t options[] = { + {"persistent", 'p', SHELLMATTA_OPT_ARG_NONE}, + {NULL, '\0', SHELLMATTA_OPT_ARG_NONE}, + }; + + do { + ret = shellmatta_opt_long(handle, options, &option, &argument, &arg_length); + if (ret != SHELLMATTA_OK) + break; + switch (option) { + case 'p': + persistent = true; + break; + case '\0': + temp_passed = true; + overtemp_lim = strtof(argument, NULL); + break; + } + } while (1); + + if (temp_passed) { + safety_controller_set_overtemp_limit(overtemp_lim); + if (persistent) + settings_save_overtemp_limit(overtemp_lim, true); + } + + overtemp_lim = safety_controller_get_overtemp_limit(); + shellmatta_printf(handle, "Overtemperature configured for: %.1f °C\r\n", overtemp_lim); + + return ret; +} + //typedef struct shellmatta_cmd //{ // char *cmd; /**< command name */ @@ -650,7 +692,7 @@ shellmatta_retCode_t shell_cmd_update(const shellmatta_handle_t handle, const ch // shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */ // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[19] = { +static shellmatta_cmd_t cmd[20] = { { .cmd = "version", .cmdAlias = "ver", @@ -801,8 +843,16 @@ static shellmatta_cmd_t cmd[19] = { .helpText = "Update Firmware", .usageText = "", .cmdFct = shell_cmd_update, - .next = NULL, + .next = &cmd[19], + }, + { + .cmd = "overtemp", + .cmdAlias = NULL, + .helpText = "Overtemperature Config", + .usageText = "", + .cmdFct = shell_cmd_overtemp_cfg, + .next = NULL, } }; diff --git a/stm-firmware/stm32f407vet6_flash.ld b/stm-firmware/stm32f407vet6_flash.ld index 11edd5d..cce71be 100644 --- a/stm-firmware/stm32f407vet6_flash.ld +++ b/stm-firmware/stm32f407vet6_flash.ld @@ -29,7 +29,7 @@ __stack_corruption_area_size = 128; /* END OF USER PARAMETERS */ ENTRY(Reset_Handler) -__ld_top_of_stack = 0x20020000; /* One byte above the end of the SRAM. Stack is pre-decrewmenting, so this is okay */ +__ld_top_of_stack = 0x20020000; /* One byte above the end of the SRAM. Stack is pre-decrementing, so this is okay */ /* Available memory areas */ diff --git a/stm-firmware/temp-converter.c b/stm-firmware/temp-converter.c index b0649ac..0c87c41 100644 --- a/stm-firmware/temp-converter.c +++ b/stm-firmware/temp-converter.c @@ -21,6 +21,7 @@ #include #include #include +#include static const float temp_lookup[(TEMP_CONVERSION_MAX_RES-TEMP_CONVERSION_MIN_RES) / TEMP_CONVERSION_RES_STEP+1] = {TEMP_CONVERSION_ARRAY_DATA}; @@ -73,3 +74,49 @@ int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out) return_ret_val: return ret_val; } + +int temp_convertet_convert_temp_to_resistance(float temp, float *resistance_out) +{ + int retcode = 0; + unsigned int i; + float lower_temp; + float upper_temp; + float lower_resistance; + float upper_resistance; + float temp_ratio; + bool found = false; + + if (!resistance_out) + return -1000; + + if (temp < temp_lookup[0]) { + /* Requested temperature is smaller than minimum value in lookup table */ + *resistance_out = temp_lookup[0]; + retcode = -1; + goto exit; + } else if (temp > temp_lookup[COUNT_OF(temp_lookup) - 1]) { + /* Requested temperature is higher than maximum value in lookup table */ + *resistance_out = temp_lookup[COUNT_OF(temp_lookup) -1]; + retcode = 1; + goto exit; + } + + for (i = 0U; i < (COUNT_OF(temp_lookup) - 1); i++) { + if (temp >= temp_lookup[i] && temp <= temp_lookup[i+1]) { + upper_temp = temp_lookup[i+1]; + lower_temp = temp_lookup[i]; + lower_resistance = TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * (i); + upper_resistance = lower_resistance + TEMP_CONVERSION_RES_STEP; + found = true; + } + } + + if (!found) + return -100; + + temp_ratio = (temp - lower_temp) / (upper_temp - lower_temp); + *resistance_out = lower_resistance + (upper_resistance - lower_resistance) * temp_ratio; + +exit: + return retcode; +}