servo/controller/fw/embed/bootloader/flash.h

47 lines
1.4 KiB
C
Raw Normal View History

#ifndef FLASH_H_
#define FLASH_H_
#include "stm32f446xx.h"
// Flash sectors for STM32F407
#define BOOT 0x08000000 // 16KB - Bootloader
#define APP_ADDR 0x08004000 // 16KB - Application
#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
#define FLASH_KEY1 0x45670123
#define FLASH_KEY2 0xCDEF89AB
//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 flash_erase_sector(uint8_t sector);
void flash_program_word(uint32_t address, uint32_t data,uint32_t byte_len);
uint8_t flash_read_word(uint32_t address);
#endif /* FLASH_H_ */