diff --git a/controller/fw/embed/include/flash.h b/controller/fw/embed/include/flash.h index 6551ee7..e6ae270 100644 --- a/controller/fw/embed/include/flash.h +++ b/controller/fw/embed/include/flash.h @@ -7,14 +7,20 @@ /* for addr in FLASH */ typedef struct{ - uint8_t data_id; + uint8_t data_id; // data_id = id register of can uint8_t value; uint16_t crc; }FLASH_RECORD; +enum { + addr_id = 0, + foc_id = 1, +kp_id = 2, + ki_id = 3, + kd_id = 4 +}; -static uint32_t write_ptr; #define FLASH_RECORD_SIZE sizeof(FLASH_RECORD) //size flash struct -#define PARAM_COUNT 4 // count data in flash +#define PARAM_COUNT 3 // count data in flash // Flash sectors for STM32F407 @@ -32,7 +38,8 @@ static uint32_t write_ptr; //FLASH SET ONE PROGRAMM WORD #define FLASH_8BYTE FLASH->CR &= ~FLASH_CR_PSIZE & ~FLASH_CR_PSIZE_1 -#define FLASH_32BYTE FLASH->CR |= FLASH_CR_PSIZE | FLASH_CR_PSIZE_0 +#define FLASH_32BYTE \ + FLASH->CR = (FLASH->CR & ~FLASH_CR_PSIZE) | (0x2 << FLASH_CR_PSIZE_Pos) // Flash command bits #define FLASH_LOCK FLASH->CR |= FLASH_CR_LOCK @@ -52,7 +59,11 @@ void flash_lock(void); void erase_sector(uint8_t sector); void program_word(uint32_t address, uint32_t data,uint32_t byte_len); uint8_t flash_read_word(uint32_t address); - - +void write_param(uint8_t param_id, uint8_t val); +FLASH_RECORD* load_params(); +void compact_page(); +void flash_read(uint32_t addr,FLASH_RECORD* ptr); +bool validate_crc(FLASH_RECORD* crc); +void flash_write(uint32_t addr, FLASH_RECORD* record); #endif /* FLASH_H_ */ diff --git a/controller/fw/embed/include/reg_cah.h b/controller/fw/embed/include/reg_cah.h index ce4057a..2a0c51d 100644 --- a/controller/fw/embed/include/reg_cah.h +++ b/controller/fw/embed/include/reg_cah.h @@ -2,7 +2,7 @@ #define REG_CAH_H_ #define APP_ADDR 0x0800400 // 16KB - Application -#define ADDR_VAR 0x8050000 +#define ADDR_VAR 0x8040000 #define ADDR_VAR_ID ADDR_VAR #define ADDR_VAR_P (ADDR_VAR + 1) #define ADDR_VAR_I (ADDR_VAR + 2) diff --git a/controller/fw/embed/src/flash.cpp b/controller/fw/embed/src/flash.cpp index 6c94616..ac2824d 100644 --- a/controller/fw/embed/src/flash.cpp +++ b/controller/fw/embed/src/flash.cpp @@ -1,7 +1,8 @@ #include "flash.h" #include +#include "hal_conf_extra.h" - +static uint32_t write_ptr = SECTOR_6; void flash_unlock(){ @@ -93,7 +94,7 @@ void flash_write(uint32_t addr, FLASH_RECORD* record){ FLASH->CR |= FLASH_CR_PG; for(int i = 0;i < size;i++){ - *(volatile uint32_t*)(addr + i * 4) = data[i]; + *(volatile uint32_t*)addr = data[i]; } // Clear program bit @@ -130,7 +131,7 @@ void flash_read(uint32_t addr,FLASH_RECORD* ptr){ void compact_page(){ FLASH_RECORD latest[PARAM_COUNT] = {0}; - for(int i = (uint32_t)SECTOR_6;i < (uint32_t)SECTOR_7,i += FLASH_RECORD_SIZE;) { + for(int i = (uint32_t)SECTOR_6;i < (uint32_t)SECTOR_7;i += FLASH_RECORD_SIZE) { FLASH_RECORD rec; flash_read(i,&rec); @@ -152,15 +153,31 @@ void compact_page(){ void write_param(uint8_t param_id, uint8_t val){ - FLASH_RECORD param_flash = { - .data_id = param_id, - .value = val, - .crc = 6933 - }; + FLASH_RECORD param_flash = {param_id,val,6933}; + /* Exit data from FLASH */ if (write_ptr + FLASH_RECORD_SIZE >= SECTOR_7) compact_page(); - flash_write((uint32_t)write_ptr,¶m_flash); + // Проверка выравнивания адреса + if (write_ptr % 4 != 0) + write_ptr += (4 - (write_ptr % 4)); // Выравнивание до 4 байт + __disable_irq(); + flash_write(write_ptr,¶m_flash); write_ptr += FLASH_RECORD_SIZE; + flash_program_word(SECTOR_7 - sizeof(uint32_t),write_ptr,0); +} + +FLASH_RECORD* load_params(){ + __disable_irq(); + static FLASH_RECORD latest[PARAM_COUNT] = {0}; + FLASH_RECORD res; + for(uint32_t addr = SECTOR_6;addr < SECTOR_7;addr +=FLASH_RECORD_SIZE) { + flash_read(addr,&res); + if (validate_crc(&res)) + latest[res.data_id] = res; + + } + __enable_irq(); + return latest; } \ No newline at end of file diff --git a/controller/fw/embed/src/main.cpp b/controller/fw/embed/src/main.cpp index 00e0706..24017fe 100644 --- a/controller/fw/embed/src/main.cpp +++ b/controller/fw/embed/src/main.cpp @@ -29,7 +29,8 @@ uint32_t SectorError; -static FLASH_RECORD flash_rec; +static FLASH_RECORD* flash_rec; +static FLASH_RECORD flash_buf[PARAM_COUNT]; static CAN_message_t CAN_TX_msg; static CAN_message_t CAN_inMsg; @@ -149,18 +150,8 @@ void send_id() { } void setup_id(uint8_t my_id) { - /* Чтобы разблокировать флэш память для записи*/ - flash_unlock(); - while (FLASH_BUSY) { } - - erase_sector(6); //очистка 6 сектора - while ((FLASH->SR & FLASH_SR_BSY) != 0) - __NOP(); - FLASH->SR |= (FLASH_SR_EOP | FLASH_SR_WRPERR | FLASH_SR_PGAERR); - FLASH->CR |= FLASH_CR_PG; - *(volatile uint8_t*)ADDR_VAR_ID = my_id; - - FLASH->CR |= FLASH_CR_LOCK; + write_param(addr_id,my_id); + send_id(); } @@ -204,8 +195,11 @@ void send_data() { void listen_can() { + flash_rec = load_params(); + for(int i = 0;i < PARAM_COUNT;i++) + flash_buf[i] = flash_rec[i]; uint8_t reg_id = CAN_inMsg.buf[0]; //reg id - if (CAN_inMsg.id == *(volatile uint8_t*)ADDR_VAR_ID) { + if (CAN_inMsg.id == flash_buf[addr_id].value) { if (CAN_inMsg.buf[1] == REG_WRITE) { switch (reg_id) { case REG_ID: