74 lines
2.1 KiB
C
74 lines
2.1 KiB
C
#ifndef FLASH_H_
|
|
#define FLASH_H_
|
|
#include "stm32f446xx.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
/* for addr in FLASH */
|
|
typedef struct{
|
|
uint8_t data_id; // data_id = id register of can
|
|
uint8_t value;
|
|
uint16_t crc;
|
|
// uint32_t write_ptr_now;
|
|
}FLASH_RECORD;
|
|
enum {
|
|
addr_id = 0,
|
|
foc_id = 1,
|
|
angl = 2,
|
|
vel = 3,
|
|
pid_p = 4,
|
|
pid_i,
|
|
pid_d
|
|
};
|
|
|
|
#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_6_END (SECTOR_6 + 128 * 1024) // sector 6 end
|
|
#define SECTOR_7 0x08060000 // 128KB
|
|
|
|
|
|
#define FLAG_BOOT (0x08040000 + 4)
|
|
// Flash keys for unlocking flash memory
|
|
#define BYTE32 0
|
|
#define BYTE8 1
|
|
#define UPDATE_FLAG 0xDEADBEEF // Unique 32bit value
|
|
//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 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 flash_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);
|
|
uint16_t validate_crc16(uint8_t *data,uint32_t length);
|
|
void flash_write(uint32_t addr, FLASH_RECORD* record);
|
|
bool validaate_crc(FLASH_RECORD* crc);
|
|
#endif /* FLASH_H_ */
|