This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/app/eeprom.c

140 lines
5.5 KiB
C
Raw Normal View History

2023-08-27 15:39:45 +00:00
/**
2023-08-27 19:35:19 +00:00
* @author R<EFBFBD>mi Heredero
2023-08-27 15:39:45 +00:00
* @version 1.0.0
* @date August 2023
* @file eeprom_interface.h
*/
2023-08-28 16:23:11 +00:00
#include "eeprom.h"
2023-08-28 10:57:16 +00:00
#include "../app/car.h"
2023-08-27 19:35:19 +00:00
typedef union {
struct {
uint8_t byte0;
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
} separate;
2023-08-28 10:57:16 +00:00
uint32_t full;
uint8_t array[4];
2023-08-27 19:35:19 +00:00
} BYTES_4;
2023-08-28 10:57:16 +00:00
void MEM_init(){
uint8_t check = MEM_read_1_byte(0x0);
if(check != 0x42){
2023-08-30 13:37:53 +00:00
KART_CST.CONTROL_STEERING_MODE = 0;
KART_CST.CONTROL_ALIVE_TIME = 50;
KART_CST.CONTROL_SPEED_FACTOR = 0;
2023-08-31 11:42:31 +00:00
KART_CST.CONTROL_POWER_FACTOR = 10000;
KART_CST.CONTROL_STEERING_FACTOR = 400000000;
KART_CST.CONTROL_MAX_SPEED_FW = 500;
KART_CST.CONTROL_MAX_SPEED_BW = 250;
2023-08-30 13:37:53 +00:00
KART_CST.JOYSTICK_MODE = 0;
KART_CST.JOYSTICK_PARAM1 = 5;
KART_CST.JOYSTICK_PARAM2 = 1;
KART_CST.JOYSTICK_ALIVE_TIME = 25;
2023-08-31 15:02:23 +00:00
KART_CST.DISPLAY_ALIVE_TIME = 100;
2023-08-30 13:37:53 +00:00
2023-08-31 15:02:23 +00:00
KART_CST.DRIVE_SPEED_TIME = 20;
KART_CST.DRIVE_STOP_TIME = 10;
KART_CST.DRIVE_ALIVE_TIME = 25;
2023-08-30 13:37:53 +00:00
KART_CST.STEERING_ALIVE_TIME = 100;
KART_CST.BATTERY_VOLT_TIME = 50;
KART_CST.BATTERY_CURRENT_TIME = 50;
KART_CST.BATTERY_ENERGY_TIME = 50;
KART_CST.BATTERY_ALIVE_TIME = 50;
MEM_write_1_byte(MEMADD_CONTROL_STEERING_MODE, KART_CST.CONTROL_STEERING_MODE);
MEM_write_1_byte(MEMADD_CONTROL_ALIVE_TIME, KART_CST.CONTROL_ALIVE_TIME);
MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR);
MEM_write_4_byte(MEMADD_CONTROL_POWER_FACTOR, KART_CST.CONTROL_POWER_FACTOR);
MEM_write_4_byte(MEMADD_CONTROL_STEERING_FACTOR, KART_CST.CONTROL_STEERING_FACTOR);
MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_FW, KART_CST.CONTROL_MAX_SPEED_FW);
MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_BW, KART_CST.CONTROL_MAX_SPEED_BW);
MEM_write_1_byte(MEMADD_JOYSTICK_MODE, KART_CST.JOYSTICK_MODE);
MEM_write_1_byte(MEMADD_JOYSTICK_PARAM1, KART_CST.JOYSTICK_PARAM1);
MEM_write_1_byte(MEMADD_JOYSTICK_PARAM2, KART_CST.JOYSTICK_PARAM2);
MEM_write_1_byte(MEMADD_JOYSTICK_ALIVE_TIME, KART_CST.JOYSTICK_ALIVE_TIME);
MEM_write_1_byte(MEMADD_DISPLAY_ALIVE_TIME, KART_CST.DISPLAY_ALIVE_TIME);
MEM_write_1_byte(MEMADD_DRIVE_SPEED_TIME, KART_CST.DRIVE_SPEED_TIME);
MEM_write_1_byte(MEMADD_DRIVE_STOP_TIME, KART_CST.DRIVE_STOP_TIME);
MEM_write_1_byte(MEMADD_DRIVE_ALIVE_TIME, KART_CST.DRIVE_ALIVE_TIME);
MEM_write_1_byte(MEMADD_STEERING_ALIVE_TIME, KART_CST.STEERING_ALIVE_TIME);
MEM_write_1_byte(MEMADD_BATTERY_VOLT_TIME, KART_CST.BATTERY_VOLT_TIME);
MEM_write_1_byte(MEMADD_BATTERY_CURRENT_TIME, KART_CST.BATTERY_CURRENT_TIME);
MEM_write_1_byte(MEMADD_BATTERY_ENERGY_TIME, KART_CST.BATTERY_ENERGY_TIME);
MEM_write_1_byte(MEMADD_BATTERY_ALIVE_TIME, KART_CST.BATTERY_ALIVE_TIME);
2023-08-28 10:57:16 +00:00
MEM_write_1_byte(0x0, 0x42);
} else {
2023-08-30 13:37:53 +00:00
KART_CST.CONTROL_STEERING_MODE = MEM_read_1_byte(MEMADD_CONTROL_STEERING_MODE);
KART_CST.CONTROL_ALIVE_TIME = MEM_read_1_byte(MEMADD_CONTROL_ALIVE_TIME);
KART_CST.CONTROL_SPEED_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_SPEED_FACTOR);
KART_CST.CONTROL_POWER_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_POWER_FACTOR);
KART_CST.CONTROL_STEERING_FACTOR = MEM_read_1_byte(MEMADD_CONTROL_STEERING_FACTOR);
KART_CST.CONTROL_MAX_SPEED_FW = MEM_read_1_byte(MEMADD_CONTROL_MAX_SPEED_FW);
KART_CST.CONTROL_MAX_SPEED_BW = MEM_read_1_byte(MEMADD_CONTROL_MAX_SPEED_BW);
KART_CST.JOYSTICK_MODE = MEM_read_1_byte(MEMADD_JOYSTICK_MODE);
KART_CST.JOYSTICK_PARAM1 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM1);
KART_CST.JOYSTICK_PARAM2 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM2);
KART_CST.JOYSTICK_ALIVE_TIME = MEM_read_1_byte(MEMADD_JOYSTICK_ALIVE_TIME);
KART_CST.DISPLAY_ALIVE_TIME = MEM_read_1_byte(MEMADD_DISPLAY_ALIVE_TIME);
KART_CST.DRIVE_SPEED_TIME = MEM_read_1_byte(MEMADD_DRIVE_SPEED_TIME);
KART_CST.DRIVE_STOP_TIME = MEM_read_1_byte(MEMADD_DRIVE_STOP_TIME);
KART_CST.DRIVE_ALIVE_TIME = MEM_read_1_byte(MEMADD_DRIVE_ALIVE_TIME);
KART_CST.STEERING_ALIVE_TIME = MEM_read_1_byte(MEMADD_STEERING_ALIVE_TIME);
KART_CST.BATTERY_VOLT_TIME = MEM_read_1_byte(MEMADD_BATTERY_VOLT_TIME);
KART_CST.BATTERY_CURRENT_TIME = MEM_read_1_byte(MEMADD_BATTERY_CURRENT_TIME);
KART_CST.BATTERY_ENERGY_TIME = MEM_read_1_byte(MEMADD_BATTERY_ENERGY_TIME);
KART_CST.BATTERY_ALIVE_TIME = MEM_read_1_byte(MEMADD_BATTERY_ALIVE_TIME);
2023-08-28 10:57:16 +00:00
}
}
2023-08-29 13:50:04 +00:00
void MEM_reset() {
MEM_write_1_byte(0x0, 0x0);
}
2023-08-28 10:57:16 +00:00
void MEM_write_1_byte(uint8_t address, uint8_t data) {
DATAEE_WriteByte(address, data);
2023-08-27 19:35:19 +00:00
}
2023-08-28 10:57:16 +00:00
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]);
}
2023-08-27 19:35:19 +00:00
}
2023-08-28 10:57:16 +00:00
uint8_t MEM_read_1_byte(uint8_t address) {
return DATAEE_ReadByte(address);
2023-08-27 19:35:19 +00:00
}
2023-08-28 10:57:16 +00:00
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;
2023-08-27 19:35:19 +00:00
}