With bootloader
This commit is contained in:
parent
b5ff05bed6
commit
023026987c
8 changed files with 312 additions and 141 deletions
|
@ -1,4 +1,9 @@
|
|||
#include "flash.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
static uint32_t write_ptr = SECTOR_6;
|
||||
|
||||
|
||||
void flash_unlock(){
|
||||
|
||||
|
@ -73,16 +78,78 @@ void flash_program_word(uint32_t address,uint32_t data,uint32_t byte_len){
|
|||
FLASH->CR &= ~FLASH_CR_PG;
|
||||
|
||||
}
|
||||
void flash_write(uint32_t addr, FLASH_RECORD* record){
|
||||
|
||||
uint8_t flash_read_word(uint32_t address){
|
||||
|
||||
// Check if address is valid
|
||||
if(address < FLASH_BASE || address > FLASH_END) {
|
||||
return 0;
|
||||
uint32_t* data = (uint32_t*)record;
|
||||
uint32_t size = FLASH_RECORD_SIZE / 4; //count words in struct
|
||||
// Wait if flash is busy
|
||||
while(FLASH_BUSY);
|
||||
|
||||
// Check if flash is locked and unlock if needed
|
||||
if(FLASH->CR & FLASH_CR_LOCK) {
|
||||
flash_unlock();
|
||||
}
|
||||
|
||||
// Read byte from flash memory
|
||||
return *((volatile uint8_t*)address);
|
||||
|
||||
// Set program bit and write data to flash
|
||||
FLASH_32BYTE;
|
||||
FLASH->CR |= FLASH_CR_PG;
|
||||
|
||||
for(int i = 0;i < size;i++){
|
||||
*(volatile uint32_t*)(addr + i * 4) = data[i];
|
||||
}
|
||||
|
||||
// Clear program bit
|
||||
FLASH->CR &= ~FLASH_CR_PG;
|
||||
flash_lock();
|
||||
}
|
||||
// Wait if flash
|
||||
|
||||
// Wait if flash
|
||||
|
||||
|
||||
bool validate_crc(FLASH_RECORD* crc){
|
||||
if(crc->crc == 0x6933)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
/* read struct from FLASH */
|
||||
void flash_read(uint32_t addr,FLASH_RECORD* ptr){
|
||||
uint32_t* flash_ptr = (uint32_t*)addr;
|
||||
uint32_t* dest = (uint32_t*)ptr;
|
||||
for (int i = 0; i < FLASH_RECORD_SIZE / 4; i++) {
|
||||
dest[i] = flash_ptr[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Поиск актуального адреса во флэш памяти */
|
||||
FLASH_RECORD load_params(){
|
||||
__disable_irq();
|
||||
FLASH_RECORD latest = {0};
|
||||
FLASH_RECORD res;
|
||||
for(uint32_t addr = SECTOR_6;addr < SECTOR_6_END;addr +=FLASH_RECORD_SIZE) {
|
||||
flash_read(addr,&res);
|
||||
if (!validate_crc(&res))
|
||||
break;
|
||||
else if(res.data_id == addr_id) {
|
||||
latest = res;
|
||||
}
|
||||
}
|
||||
|
||||
__enable_irq();
|
||||
return latest;
|
||||
}
|
||||
/**
|
||||
* @brief Записывает страницу данных во флеш-память
|
||||
* @param data: Указатель на буфер с данными
|
||||
* @param len: Длина данных в байтах (должна быть кратна 4)
|
||||
* @retval bool: true - успех, false - ошибка
|
||||
*/
|
||||
void write_flash_page(const uint8_t* data, uint16_t len) { // Добавлен const
|
||||
flash_unlock();
|
||||
for (uint16_t i = 0; i < len; i += 4) {
|
||||
uint32_t word;
|
||||
memcpy(&word, &data[i], 4); // Безопасное копирование
|
||||
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, write_ptr + i, word);
|
||||
}
|
||||
flash_lock();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue