2020-09-04 22:55:34 +02:00
/* Reflow Oven Controller
*
* Copyright ( C ) 2020 Mario Hüttel < mario . huettel @ gmx . net >
*
* This file is part of the Reflow Oven Controller Project .
*
* The reflow oven controller is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project .
* If not , see < http : //www.gnu.org/licenses/>.
*/
2020-09-04 23:02:23 +02:00
# ifndef __SAFETY_MEMORY_H__
# define __SAFETY_MEMORY_H__
2020-09-04 22:55:34 +02:00
# include <stdint.h>
/**
2020-09-04 23:02:23 +02:00
* @ brief Magic number to signal a valid safety memory header .
2020-09-04 22:55:34 +02:00
*/
2020-09-04 23:02:23 +02:00
# define SAFETY_MEMORY_MAGIC 0x12AA5CB7
2020-09-04 22:55:34 +02:00
2020-09-05 17:37:56 +02:00
/**
* @ brief Error memory NOP entry
*/
# define SAFETY_MEMORY_NOP_ENTRY 0xC1AA1222
2020-09-04 22:55:34 +02:00
/**
2020-09-04 23:02:23 +02:00
* @ brief Offset address for the safety_memory_header .
* @ note Any other value than 0UL doesn ' t really make sense . Therfore , this should not be changed .
2020-09-04 22:55:34 +02:00
*/
2020-09-04 23:02:23 +02:00
# define SAFETY_MEMORY_HEADER_ADDRESS 0UL
2020-09-04 22:55:34 +02:00
2020-09-05 15:56:52 +02:00
# define SAFETY_MEMORY_CONFIG_OVERRIDE_COUNT 32UL
2020-09-05 15:15:31 +02:00
2020-09-04 22:55:34 +02:00
/**
2020-09-04 23:02:23 +02:00
* @ brief Safety memory header
2020-09-04 22:55:34 +02:00
*/
2020-09-04 23:02:23 +02:00
struct safety_memory_header {
uint32_t magic ; /**< @brief Magic. Set to @ref SAFETY_MEMORY_MAGIC */
uint32_t boot_status_offset ; /**< @brief Offset of the safety_memory_boot_status struct (in 32 bit words)*/
2020-09-04 23:51:51 +02:00
uint32_t config_overrides_offset ; /**< @brief Offset address of override entries */
uint32_t config_overrides_len ; /**< @brief Length of override entries in words */
2020-09-04 23:02:23 +02:00
uint32_t err_memory_offset ; /**< @brief Offset of the error memory */
uint32_t err_memory_end ; /**< @brief End of the error memory. This points to the word after the error memory, containing the CRC of the whole backup RAM. */
uint32_t magic_i ; /**< @brief Invers Magic. Set to the bitwise inverse of @ref SAFETY_MEMORY_MAGIC */
} ;
2020-09-04 22:55:34 +02:00
2020-09-04 23:02:23 +02:00
struct safety_memory_boot_status {
2020-09-05 19:00:01 +02:00
/**
* @ brief Reboot into the bootloader
*
* When this flag is set , the controller will load the bootloader to
* memory and execute it .
*/
2020-09-04 23:02:23 +02:00
uint32_t reboot_to_bootloader ;
2020-09-05 19:00:01 +02:00
/**
* @ brief Bootloader has updated the code
*
* This flag is set , if the firmware ahs been updated successfully
*/
2020-09-05 16:32:31 +02:00
uint32_t code_updated ;
2020-09-05 19:00:01 +02:00
/**
* @ brief reset_from_panic
*
* This flag is set , when entering the panic mode .
* Because the panic mode is reset by a watchdog reset ,
* this flag is needed , in order to ensure , that the panic is handled correcly after
* the watchdog reset .
*/
2020-09-05 16:32:31 +02:00
uint32_t reset_from_panic ;
2020-09-04 23:02:23 +02:00
} ;
2020-09-05 15:15:31 +02:00
enum safety_memory_state {
SAFETY_MEMORY_INIT_FRESH = 0 ,
SAFETY_MEMORY_INIT_CORRUPTED = 1 ,
SAFETY_MEMORY_INIT_VALID_MEMORY = 2 ,
} ;
enum safety_memory_error_entry_type {
SAFETY_MEMORY_ERR_ENTRY_FLAG = 1 ,
SAFETY_MEMORY_ERR_ENTRY_NOP = 2 ,
} ;
struct error_memory_entry {
enum safety_memory_error_entry_type type ;
2020-09-05 16:32:31 +02:00
uint8_t flag_num ;
2020-09-05 20:06:13 +02:00
uint16_t counter ;
2020-09-05 15:15:31 +02:00
} ;
enum config_override_entry_type {
SAFETY_MEMORY_CONFIG_OVERRIDE_WEIGHT = 1 ,
SAFETY_MEMORY_CONFIG_OVERRIDE_PERSISTANCE = 2 ,
} ;
struct config_override {
enum config_override_entry_type type ;
union {
struct {
2020-09-05 16:32:31 +02:00
uint8_t flag ;
2020-09-05 15:15:31 +02:00
uint8_t weight ;
} weight_override ;
struct {
2020-09-05 16:32:31 +02:00
uint8_t flag ;
2020-09-05 15:15:31 +02:00
uint8_t persistance ;
} persistance_override ;
} entry ;
} ;
int safety_memory_init ( enum safety_memory_state * found_state ) ;
2020-09-05 15:56:52 +02:00
int safety_memory_reinit ( enum safety_memory_state * found_state ) ;
2020-09-05 15:15:31 +02:00
int safety_memory_get_boot_status ( struct safety_memory_boot_status * status ) ;
2020-09-05 16:32:31 +02:00
int safety_memory_set_boot_status ( const struct safety_memory_boot_status * status ) ;
2020-09-05 15:15:31 +02:00
int safety_memory_get_error_entry_count ( uint32_t * count ) ;
int safety_memory_check ( void ) ;
int safety_memory_get_error_entry ( uint32_t idx , struct error_memory_entry * entry ) ;
int safety_memory_insert_error_entry ( struct error_memory_entry * entry ) ;
int safety_memory_insert_config_override ( struct config_override * config_override ) ;
int safety_memory_get_config_override_count ( uint32_t * count ) ;
int safety_memory_get_config_override ( uint32_t idx , struct config_override * config_override ) ;
2020-09-04 23:02:23 +02:00
# endif /* __SAFETY_MEMORY_H__ */