/** * @author R�mi Heredero * @version 1.0.0 * @date August 2023 * @file eeprom_interface.h */ #include "eeprom_interface.h" #include "../app/car.h" typedef union { struct { uint8_t byte0; uint8_t byte1; uint8_t byte2; uint8_t byte3; } separate; uint32_t full; uint8_t array[4]; } BYTES_4; void MEM_init(){ uint8_t check = MEM_read_1_byte(0x0); if(check != 0x42){ CAR_CST.CONTROL_STEERING_MODE = 0; CAR_CST.CONTROL_ALIVE_TIME = 50; CAR_CST.CONTROL_SPEED_FACTOR = 0; CAR_CST.CONTROL_POWER_FACTOR = 0; CAR_CST.CONTROL_STEERING_FACTOR = 0; CAR_CST.CONTROL_MAX_SPEED_FW = 0; CAR_CST.CONTROL_MAX_SPEED_BW = 0; CAR_CST.JOYSTICK_MODE = 0; CAR_CST.JOYSTICK_PARAM1 = 100; CAR_CST.JOYSTICK_PARAM2 = 1; CAR_CST.JOYSTICK_ALIVE_TIME = 50; CAR_CST.DISPLAY_ALIVE_TIME = 0; CAR_CST.DRIVE_SPEED_TIME = 0; CAR_CST.DRIVE_STOP_TIME = 0; CAR_CST.DRIVE_ALIVE_TIME = 0; CAR_CST.STEERING_ALIVE_TIME = 0; CAR_CST.BATTERY_VOLT_TIME = 0; CAR_CST.BATTERY_CURRENT_TIME = 0; CAR_CST.BATTERY_ENERGY_TIME = 0; CAR_CST.BATTERY_ALIVE_TIME = 0; MEM_write_1_byte(MEMADD_CONTROL_STEERING_MODE, CAR_CST.CONTROL_STEERING_MODE); MEM_write_1_byte(MEMADD_CONTROL_ALIVE_TIME, CAR_CST.CONTROL_ALIVE_TIME); MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, CAR_CST.CONTROL_SPEED_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_POWER_FACTOR, CAR_CST.CONTROL_POWER_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_STEERING_FACTOR, CAR_CST.CONTROL_STEERING_FACTOR); MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_FW, CAR_CST.CONTROL_MAX_SPEED_FW); MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_BW, CAR_CST.CONTROL_MAX_SPEED_BW); MEM_write_1_byte(MEMADD_JOYSTICK_MODE, CAR_CST.JOYSTICK_MODE); MEM_write_1_byte(MEMADD_JOYSTICK_PARAM1, CAR_CST.JOYSTICK_PARAM1); MEM_write_1_byte(MEMADD_JOYSTICK_PARAM2, CAR_CST.JOYSTICK_PARAM2); MEM_write_1_byte(MEMADD_JOYSTICK_ALIVE_TIME, CAR_CST.JOYSTICK_ALIVE_TIME); MEM_write_1_byte(MEMADD_DISPLAY_ALIVE_TIME, CAR_CST.DISPLAY_ALIVE_TIME); MEM_write_1_byte(MEMADD_DRIVE_SPEED_TIME, CAR_CST.DRIVE_SPEED_TIME); MEM_write_1_byte(MEMADD_DRIVE_STOP_TIME, CAR_CST.DRIVE_STOP_TIME); MEM_write_1_byte(MEMADD_DRIVE_ALIVE_TIME, CAR_CST.DRIVE_ALIVE_TIME); MEM_write_1_byte(MEMADD_STEERING_ALIVE_TIME, CAR_CST.STEERING_ALIVE_TIME); MEM_write_1_byte(MEMADD_BATTERY_VOLT_TIME, CAR_CST.BATTERY_VOLT_TIME); MEM_write_1_byte(MEMADD_BATTERY_CURRENT_TIME, CAR_CST.BATTERY_CURRENT_TIME); MEM_write_1_byte(MEMADD_BATTERY_ENERGY_TIME, CAR_CST.BATTERY_ENERGY_TIME); MEM_write_1_byte(MEMADD_BATTERY_ALIVE_TIME, CAR_CST.BATTERY_ALIVE_TIME); MEM_write_1_byte(0x0, 0x42); } else { CAR_CST.CONTROL_STEERING_MODE = MEM_read_1_byte(MEMADD_CONTROL_STEERING_MODE); CAR_CST.CONTROL_ALIVE_TIME = MEM_read_1_byte(MEMADD_CONTROL_ALIVE_TIME); CAR_CST.CONTROL_SPEED_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_SPEED_FACTOR); CAR_CST.CONTROL_POWER_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_POWER_FACTOR); CAR_CST.CONTROL_STEERING_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_STEERING_FACTOR); CAR_CST.CONTROL_MAX_SPEED_FW = MEM_read_1_byte(MEMADD_CONTROL_MAX_SPEED_FW); CAR_CST.CONTROL_MAX_SPEED_BW = MEM_read_1_byte(MEMADD_CONTROL_MAX_SPEED_BW); CAR_CST.JOYSTICK_MODE = MEM_read_1_byte(MEMADD_JOYSTICK_MODE); CAR_CST.JOYSTICK_PARAM1 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM1); CAR_CST.JOYSTICK_PARAM2 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM2); CAR_CST.JOYSTICK_ALIVE_TIME = MEM_read_1_byte(MEMADD_JOYSTICK_ALIVE_TIME); CAR_CST.DISPLAY_ALIVE_TIME = MEM_read_1_byte(MEMADD_DISPLAY_ALIVE_TIME); CAR_CST.DRIVE_SPEED_TIME = MEM_read_1_byte(MEMADD_DRIVE_SPEED_TIME); CAR_CST.DRIVE_STOP_TIME = MEM_read_1_byte(MEMADD_DRIVE_STOP_TIME); CAR_CST.DRIVE_ALIVE_TIME = MEM_read_1_byte(MEMADD_DRIVE_ALIVE_TIME); CAR_CST.STEERING_ALIVE_TIME = MEM_read_1_byte(MEMADD_STEERING_ALIVE_TIME); CAR_CST.BATTERY_VOLT_TIME = MEM_read_1_byte(MEMADD_BATTERY_VOLT_TIME); CAR_CST.BATTERY_CURRENT_TIME = MEM_read_1_byte(MEMADD_BATTERY_CURRENT_TIME); CAR_CST.BATTERY_ENERGY_TIME = MEM_read_1_byte(MEMADD_BATTERY_ENERGY_TIME); CAR_CST.BATTERY_ALIVE_TIME = MEM_read_1_byte(MEMADD_BATTERY_ALIVE_TIME); } } void MEM_write_1_byte(uint8_t address, uint8_t data) { DATAEE_WriteByte(address, data); } void MEM_write_4_byte(uint8_t address, uint32_t data) { BYTES_4 tmpData; tmpData.full = data; for(uint8_t i = 0; i<4;i++) { uint8_t add = address; add += i; MEM_write_1_byte(add, tmpData.array[i]); } } uint8_t MEM_read_1_byte(uint8_t address) { return DATAEE_ReadByte(address); } uint32_t MEM_read_4_byte(uint8_t address) { BYTES_4 tmpData; for(uint8_t i = 0; i<4;i++) { uint8_t add = address; add += i; tmpData.array[i] = MEM_read_1_byte(add); } return tmpData.full; }