58 lines
1.5 KiB
C
58 lines
1.5 KiB
C
#ifndef FLASH_H_
|
|
#define FLASH_H_
|
|
#include "stm32f446xx.h"
|
|
|
|
|
|
|
|
|
|
/* for addr in FLASH */
|
|
typedef struct{
|
|
uint8_t data_id;
|
|
uint8_t value;
|
|
uint16_t crc;
|
|
}FLASH_RECORD;
|
|
|
|
static uint32_t write_ptr;
|
|
#define FLASH_RECORD_SIZE sizeof(FLASH_RECORD) //size flash struct
|
|
#define PARAM_COUNT 4 // count data in flash
|
|
|
|
// 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_7 0x08060000 // 128KB
|
|
|
|
|
|
#define FLAG_BOOT (0x08040000 + 4)
|
|
// 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_PSIZE | FLASH_CR_PSIZE_0
|
|
|
|
// Flash command bits
|
|
#define FLASH_LOCK FLASH->CR |= FLASH_CR_LOCK
|
|
#define FLASH_UNLOCK FLASH->KEYR = FLASH_KEY1; FLASH->KEYR = FLASH_KEY2
|
|
|
|
|
|
// 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 program_word(uint32_t address, uint32_t data,uint32_t byte_len);
|
|
uint8_t flash_read_word(uint32_t address);
|
|
|
|
|
|
|
|
#endif /* FLASH_H_ */
|