From f60b9166ca8504aef3b09bab991d4873f8d69f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 28 Aug 2023 12:57:16 +0200 Subject: [PATCH] implement memory init for EEPROM --- 306-controller_interface.X/app/can_message.c | 130 +++++++++++++----- 306-controller_interface.X/app/car.h | 19 ++- .../app/factory/factory.c | 12 +- .../middleware/eeprom_interface.c | 119 ++++++++++++++-- .../middleware/eeprom_interface.h | 9 +- threewheeler.DBF | 9 +- 6 files changed, 232 insertions(+), 66 deletions(-) diff --git a/306-controller_interface.X/app/can_message.c b/306-controller_interface.X/app/can_message.c index d8f3fe3..05f929a 100644 --- a/306-controller_interface.X/app/can_message.c +++ b/306-controller_interface.X/app/can_message.c @@ -6,9 +6,9 @@ */ #include "can_message.h" -#include "../middleware/can_interface.h" #include "car.h" #include "../app/factory/factory.h" +#include "../middleware/can_interface.h" typedef union { struct { @@ -24,40 +24,44 @@ typedef union { void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data){ switch(idSender){ - case 0: // Broadcast / Debug - /* - S R M - 0 1 0 CONTROL_SETUP steeringMode - - controlAliveTime - 0 1 1 CONTROL_SPEED_FACTOR valHH valH valL valLL - 0 1 2 CONTROL_POWER_FACTOR valHH valH valL valLL - 0 1 3 CONTROL_STEERING_FACTOR valHH valH valL valLL - 0 1 4 CONTROL_SECURITY_PARAM maxSpeedFw maxSpeedBw - - - 0 1 5 CONTROL_SETUP_PARAM displayAliveTime steeringAliveTime - 0 1 6 CONTROL_SETUP_PARAM_JOY joystickMode joystickParam1 joystickParam2 joystickAliveTime - 0 1 7 CONTROL_SETUP_PARAM_DRIVE driveAliveTime driveSpeedTime driveStopTime - 0 1 8 CONTROL_SETUP_PARAM_BATTERY batteryVoltTime batteryCurrentTime batteryEnergyTime batteryAliveTime - */ + + /********************* + * BROADCAST / DEBUG * + *********************/ + case 0: if(idMsg == 0x0) { // CONTROL_SETUP - + // steeringMode - - controlAliveTime + BYTES_4 tmpData; + tmpData.full.bytes = data; + CAR_CST.CONTROL_STEERING_MODE = tmpData.separate.byte0; + CAR_CST.CONTROL_ALIVE_TIME = tmpData.separate.byte3; + 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); + WATCHDOG_setTime(WDcontroller(), CAR_CST.CONTROL_ALIVE_TIME); } if(idMsg == 0x1) { // CONTROL_SPEED_FACTOR + // valHH valH valL valLL } if(idMsg == 0x2) { // CONTROL_POWER_FACTOR + // valHH valH valL valLL } if(idMsg == 0x3) { // CONTROL_STEERING_FACTOR + // valHH valH valL valLL } if(idMsg == 0x4) { // CONTROL_SECURITY_PARAM + // maxSpeedFw maxSpeedBw - - } if(idMsg == 0x5) { // CONTROL_SETUP_PARAM + // displayAliveTime steeringAliveTime - - } @@ -65,41 +69,50 @@ void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data){ // joystickMode joystickParam1 joystickParam2 joystickAliveTime BYTES_4 tmpData; tmpData.full.bytes = data; - MEM_write_one_byte(MEMADD_JOYSTICK_MODE, tmpData.separate.byte0); - MEM_write_one_byte(MEMADD_JOYSTICK_PARAM1, tmpData.separate.byte1); - MEM_write_one_byte(MEMADD_JOYSTICK_PARAM2, tmpData.separate.byte2); - MEM_write_one_byte(MEMADD_JOYSTICK_ALIVE_TIME, tmpData.separate.byte3); - CAR_CST.JOYSTICK_MODE = MEM_read_one_byte(MEMADD_JOYSTICK_MODE); - CAR_CST.JOYSTICK_PARAM1 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM1); - CAR_CST.JOYSTICK_PARAM2 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM2); - CAR_CST.JOYSTICK_ALIVE_TIME = MEM_read_one_byte(MEMADD_JOYSTICK_ALIVE_TIME); + CAR_CST.JOYSTICK_MODE = tmpData.separate.byte0; + CAR_CST.JOYSTICK_PARAM1 = tmpData.separate.byte1; + CAR_CST.JOYSTICK_PARAM2 = tmpData.separate.byte2; + CAR_CST.JOYSTICK_ALIVE_TIME = tmpData.separate.byte3; + 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); + ALIVE_CHECKER_setAliveTime(ACjoy(), CAR_CST.JOYSTICK_ALIVE_TIME); CM_JOY_SETUP(NULL); } if(idMsg == 0x7) { // CONTROL_SETUP_PARAM_DRIVE + // driveAliveTime driveSpeedTime driveStopTime - } if(idMsg == 0x8) { // CONTROL_SETUP_PARAM_BATTERY + // batteryVoltTime batteryCurrentTime batteryEnergyTime batteryAliveTime } break; - case 2: // Joystick + + /************ + * JOYSTICK * + ************/ + case 2: if(idMsg == 0x1) { // JOY_MESURE + // posX posY button - } if(idMsg == 0xF) { // JOY_ALIVE + // - - - - ALIVE_CHECKER_ISALIVE(ACjoy()); if(ACjoy()->state == STAC_DEAD){ - CAR_CST.JOYSTICK_MODE = MEM_read_one_byte(MEMADD_JOYSTICK_MODE); - CAR_CST.JOYSTICK_PARAM1 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM1); - CAR_CST.JOYSTICK_PARAM2 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM2); - CAR_CST.JOYSTICK_ALIVE_TIME = MEM_read_one_byte(MEMADD_JOYSTICK_ALIVE_TIME); + 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); ALIVE_CHECKER_setAliveTime(ACjoy(), CAR_CST.JOYSTICK_ALIVE_TIME); ALIVE_CHECKER_emitBorn(ACjoy(), 0, 0); ALIVE_CHECKER_emitReady(ACjoy(), 100, 0); @@ -107,19 +120,70 @@ void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data){ } break; - case 3: // Display + + /*********** + * DISPLAY * + ***********/ + case 3: + if(idMsg == 0xF) { // DISPLAY_ALIVE + // powerMode - - - + + } break; - case 4: // Drive + + /********* + * DRIVE * + *********/ + case 4: + if(idMsg == 0x0) { // DRIVE_SPEED + // speedHH speedH speedL speedLL + + } + + if(idMsg == 0xF) { // DRIVE_ALIVE + // statusH statusL - - + + } break; - case 5: // Steering + + /************ + * STEERING * + ************/ + case 5: + if(idMsg == 0x1) { // STEERING_GET_CENTER + // valHH valH valL valLL + + } + + if(idMsg == 0x2) { // STEERING_GET_POSITION + // valHH valH valL valLL + + } + + if(idMsg == 0xF) { // STEERING_ALIVE + // statusH statusL - - + + } break; - case 6: // Supply + + /********** + * SUPPLY * + **********/ + case 6: + if(idMsg == 0xF) { // BATTERY_ALIVE + // - - - - + + } break; - case 7: // Undefined + + /************* + * UNDEFINED * + *************/ + case 7: break; } } diff --git a/306-controller_interface.X/app/car.h b/306-controller_interface.X/app/car.h index 5d3a9a8..d1ba7a7 100644 --- a/306-controller_interface.X/app/car.h +++ b/306-controller_interface.X/app/car.h @@ -34,12 +34,15 @@ #define MEMADD_JOYSTICK_PARAM1 0x12 #define MEMADD_JOYSTICK_PARAM2 0x13 #define MEMADD_JOYSTICK_ALIVE_TIME 0x14 -#define MEMADD_DISPLAY_ALIVE_TIME 0x -#define MEMADD_DRIVE_SPEED_TIME 0x -#define MEMADD_DRIVE_STOP_TIME 0x -#define MEMADD_DRIVE_ALIVE_TIME 0x -#define MEMADD_STEERING_ALIVE_TIME 0x -#define MEMADD_BATTERY_ALIVE_TIME 0x +#define MEMADD_DISPLAY_ALIVE_TIME 0x15 +#define MEMADD_DRIVE_SPEED_TIME 0x16 +#define MEMADD_DRIVE_STOP_TIME 0x17 +#define MEMADD_DRIVE_ALIVE_TIME 0x18 +#define MEMADD_STEERING_ALIVE_TIME 0x19 +#define MEMADD_BATTERY_VOLT_TIME 0x1A +#define MEMADD_BATTERY_CURRENT_TIME 0x1B +#define MEMADD_BATTERY_ENERGY_TIME 0x1C +#define MEMADD_BATTERY_ALIVE_TIME 0x1D typedef struct { uint8_t CONTROL_STEERING_MODE; @@ -49,7 +52,6 @@ typedef struct { uint32_t CONTROL_STEERING_FACTOR; uint8_t CONTROL_MAX_SPEED_FW; uint8_t CONTROL_MAX_SPEED_BW; - uint8_t JOYSTICK_MODE; uint8_t JOYSTICK_PARAM1; uint8_t JOYSTICK_PARAM2; @@ -59,6 +61,9 @@ typedef struct { uint8_t DRIVE_STOP_TIME; uint8_t DRIVE_ALIVE_TIME; uint8_t STEERING_ALIVE_TIME; + uint8_t BATTERY_VOLT_TIME; + uint8_t BATTERY_CURRENT_TIME; + uint8_t BATTERY_ENERGY_TIME; uint8_t BATTERY_ALIVE_TIME; } CAR_CST_TYPE; CAR_CST_TYPE CAR_CST; diff --git a/306-controller_interface.X/app/factory/factory.c b/306-controller_interface.X/app/factory/factory.c index b381255..11b5ec4 100644 --- a/306-controller_interface.X/app/factory/factory.c +++ b/306-controller_interface.X/app/factory/factory.c @@ -64,22 +64,12 @@ void Factory_init() { LED_off(l1()); // TODO init EPROM interface + MEM_init(); // TODO init watchdog with EPROM CST WATCHDOG_init(WDcontroller()); - MEM_write_one_byte(MEMADD_CONTROL_ALIVE_TIME, 10); - - CAR_CST.CONTROL_ALIVE_TIME = MEM_read_one_byte(MEMADD_CONTROL_ALIVE_TIME); WATCHDOG_setTime(WDcontroller(), CAR_CST.CONTROL_ALIVE_TIME); - MEM_write_one_byte(MEMADD_JOYSTICK_MODE, 0); - MEM_write_one_byte(MEMADD_JOYSTICK_PARAM1, 100); - MEM_write_one_byte(MEMADD_JOYSTICK_PARAM2, 1); - MEM_write_one_byte(MEMADD_JOYSTICK_ALIVE_TIME, CAR_CST.CONTROL_ALIVE_TIME); - CAR_CST.JOYSTICK_MODE = MEM_read_one_byte(MEMADD_JOYSTICK_MODE); - CAR_CST.JOYSTICK_PARAM1 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM1); - CAR_CST.JOYSTICK_PARAM2 = MEM_read_one_byte(MEMADD_JOYSTICK_PARAM2); - CAR_CST.JOYSTICK_ALIVE_TIME = MEM_read_one_byte(MEMADD_JOYSTICK_ALIVE_TIME); ALIVE_CHECKER_init(ACjoy()); } diff --git a/306-controller_interface.X/middleware/eeprom_interface.c b/306-controller_interface.X/middleware/eeprom_interface.c index 7feb700..657329c 100644 --- a/306-controller_interface.X/middleware/eeprom_interface.c +++ b/306-controller_interface.X/middleware/eeprom_interface.c @@ -6,6 +6,7 @@ */ #include "eeprom_interface.h" +#include "../app/car.h" typedef union { struct { @@ -14,23 +15,121 @@ typedef union { uint8_t byte2; uint8_t byte3; } separate; - struct { - uint32_t bytes; - } full; + uint32_t full; + uint8_t array[4]; } BYTES_4; -void MEM_write_one_byte(uint8_t addresse, uint8_t data) { - DATAEE_WriteByte(addresse, data); +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_four_byte(uint8_t addresse, uint32_t data) { +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_one_byte(uint8_t adresse) { - return DATAEE_ReadByte(adresse); +uint8_t MEM_read_1_byte(uint8_t address) { + return DATAEE_ReadByte(address); } -uint32_t MEM_read_four_byte(uint8_t adresse) { - +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; } diff --git a/306-controller_interface.X/middleware/eeprom_interface.h b/306-controller_interface.X/middleware/eeprom_interface.h index d0f30a2..d4fff8e 100644 --- a/306-controller_interface.X/middleware/eeprom_interface.h +++ b/306-controller_interface.X/middleware/eeprom_interface.h @@ -11,10 +11,11 @@ #include // usage of boolean types #include "../mcc_generated_files/mcc.h" -void MEM_write_one_byte(uint8_t addresse, uint8_t data); -void MEM_write_four_byte(uint8_t adresse, uint32_t data); -uint8_t MEM_read_one_byte(uint8_t adresse); -uint32_t MEM_read_four_byte(uint8_t adresse); +void MEM_init(); +void MEM_write_1_byte(uint8_t address, uint8_t data); +void MEM_write_4_byte(uint8_t address, uint32_t data); +uint8_t MEM_read_1_byte(uint8_t address); +uint32_t MEM_read_4_byte(uint8_t address); // void MEM_write_CONTROL_STEERING_MODE(uint8_t data); #endif /* EEPROM_INTERFACE_H */ diff --git a/threewheeler.DBF b/threewheeler.DBF index 6f5fd33..727ded4 100644 --- a/threewheeler.DBF +++ b/threewheeler.DBF @@ -6,7 +6,7 @@ [BUSMASTER_VERSION] [3.2.2] -[NUMBER_OF_MESSAGES] 25 +[NUMBER_OF_MESSAGES] 26 [START_MSG] JOY_MEASURE,529,3,3,1,S [START_SIGNALS] posX,8,1,0,I,127,-128,1,0.000000,1.000000,%, @@ -150,4 +150,11 @@ [START_MSG] STEERING_GET_POS,1298,4,1,1,S [START_SIGNALS] POSITION,32,4,0,I,2147483647,-2147483648,0,0.000000,1.000000,qc, +[END_MSG] + +[START_MSG] CONTROL_SETUP_PARAM_JOY,22,4,4,1,S +[START_SIGNALS] mode,1,1,0,B,1,0,1,0.000000,1.000000,, +[START_SIGNALS] timeMeasureOrDeltaX,8,2,0,U,255,0,1,0.000000,10.000000,msOrVal, +[START_SIGNALS] DeltaY,8,3,0,U,255,0,1,0.000000,1.000000,val, +[START_SIGNALS] aliveTime,8,4,0,U,255,0,1,0.000000,10.000000,ms, [END_MSG] \ No newline at end of file