74 lines
2.4 KiB
C
74 lines
2.4 KiB
C
#ifndef FLASH_H_
|
|
#define FLASH_H_
|
|
#include "stm32f446xx.h"
|
|
#include "hal_conf_extra.h"
|
|
/* for addr in FLASH */
|
|
typedef struct{
|
|
uint32_t write_ptr_now;
|
|
uint16_t crc;
|
|
uint8_t value;
|
|
uint8_t data_id; // data_id = id register of can
|
|
}FLASH_RECORD;
|
|
enum {
|
|
addr_id = 0
|
|
};
|
|
|
|
|
|
|
|
#define FLASH_RECORD_SIZE sizeof(FLASH_RECORD) //size flash struct
|
|
#define PARAM_COUNT 1 // count data in flash
|
|
|
|
|
|
|
|
#define FLAG_BOOT 0x08060000 // Адрес хранения флага для обновления прошивки
|
|
#define UPDATE_FLAG 0xDEADBEEF // Уникальное 32-битное значение
|
|
#define APP_ADDRESS 0x08008000 // Адрес основной прошивки
|
|
#define BOOT_CAN_ID 0x01 // CAN ID бутлоадера
|
|
#define BOOT_CAN_END 0x02 // CAN ID завершения передачи
|
|
#define DATA_CAN_ID 0x03 // CAN ID данных
|
|
#define ACK_CAN_ID 0x05 // CAN ID подтверждения
|
|
#define MAX_FW_SIZE 0x3FFF // Макс. размер прошивки (256KB)
|
|
|
|
|
|
|
|
// Flash sectors for STM32F407
|
|
|
|
#define SECTOR_2 0x08008000 // 16KB
|
|
#define SECTOR_3 0x0800C000 // 16KB
|
|
#define SECTOR_4 0x08010000 // 64KB
|
|
#define SECTOR_5 0x08020000 // 128KB
|
|
#define SECTOR_6 0x08040000 // 128KB
|
|
#define SECTOR_6_END (SECTOR_6 + 128 * 1024) // sector 6 end
|
|
|
|
|
|
|
|
|
|
// Flash keys for unlocking flash memory
|
|
|
|
|
|
//FLASH SET ONE PROGRAMM WORD
|
|
#define FLASH_8BYTE FLASH->CR &= ~FLASH_CR_PSIZE & ~FLASH_CR_PSIZE_1
|
|
#define FLASH_32BYTE \
|
|
FLASH->CR = (FLASH->CR & ~FLASH_CR_PSIZE) | (0x2 << FLASH_CR_PSIZE_Pos)
|
|
|
|
// Flash status flags
|
|
#define FLASH_BUSY (FLASH->SR & FLASH_SR_BSY)
|
|
#define FLASH_ERROR (FLASH->SR & (FLASH_SR_WRPERR | FLASH_SR_PGAERR | FLASH_SR_PGPERR | FLASH_SR_PGSERR))
|
|
|
|
//for bootloader
|
|
typedef void(*pFunction)(void);
|
|
|
|
// Function prototypes
|
|
void flash_unlock(void);
|
|
void flash_lock(void);
|
|
void erase_sector(uint8_t sector);
|
|
void flash_program_word(uint32_t address,uint32_t data,uint32_t byte_len);
|
|
void write_flash_page(const uint8_t *data, uint16_t len);
|
|
void flash_read(uint32_t addr,FLASH_RECORD* ptr);
|
|
bool validate_crc(FLASH_RECORD* crc);
|
|
uint8_t flash_read_word(uint32_t address);
|
|
void flash_write(uint32_t addr, FLASH_RECORD* record);
|
|
FLASH_RECORD load_params();
|
|
//bool write_flash_page(const uint8_t* data, uint16_t len);
|
|
|
|
#endif /* FLASH_H_ */
|