回顾下之前的章节:
本文我们将回顾下FMC的知识,并给出Flash读写的接口设计和示例,这在设计升级程序时十分重要。
闪存控制器(FMC),提供了片上闪存需要的所有功能。一般而言,MCU的Flash包括4个部分:
Flash读写的流程和注意事项在芯片手册中写的清清白白,就不赘述了;如果大家感兴趣,可以对着厂家给的API接口比对着看,理解会更好一点。
FMC的操作实际上比较简单,厂家给的API都很成熟,需要注意的几个点:
配置Flash程序区:
flash配置
接口封装统一(io_fmc.h
):
#ifdef STM32
#define fmc_sector_erase FLASH_ErasePage
#define fmc_unlock FLASH_Unlock
#define fmc_lock FLASH_Lock
#define fmc_word_program FLASH_ProgramWord
#endif
测试函数:
多嘴一句,STM32给的擦除扇区接口的入口参数是扇区首地址,GD32给的接口是扇区编号,从软件使用的角度来看,GD32的更优。
static uint8_t fmc_test_flag = 0;
static void fmc_test(void)
{
#ifdef FMC_TEST
#define FMC_TEST_DATA_COUNT 5
uint32_t index = 0;
#ifdef GD32
uint32_t addr = 0x08100000;
uint32_t sector = CTL_SECTOR_NUMBER_12;
#endif
#ifdef STM32
uint32_t addr = 0x08040000;
uint32_t sector = addr;
#endif
uint32_t *pdata = (uint32_t *)addr;
int32_t fmc_state;
if (fmc_test_flag)
{
return;
}
fmc_test_flag = 1;
/* 1. before erase */
printf("before erase,\tdata = ");
for (index = 0; index < FMC_TEST_DATA_COUNT; index++)
{
printf("0x%X ", pdata[index]);
}
printf("\r\n");
/* 2. after erase */
fmc_unlock();
fmc_state = fmc_sector_erase(sector);
fmc_lock();
printf("after erase[%d]\tdata = ", fmc_state);
for (index = 0; index < FMC_TEST_DATA_COUNT; index++)
{
printf("0x%X ", pdata[index]);
}
printf("\r\n");
/* 2. after program */
fmc_unlock();
for (index = 0; index < FMC_TEST_DATA_COUNT; index++)
{
fmc_state = fmc_word_program(addr, index+9);
addr += 4;
printf("programing [0x%0X] state = [%d]\r\n", addr, fmc_state);
}
fmc_lock();
printf("after program,\tdata = ");
for (index = 0; index < FMC_TEST_DATA_COUNT; index++)
{
printf("0x%X ", pdata[index]);
}
printf("\r\n");
#endif
}
测试结果(GD32,正常状态返回值是0):
before erase, data = 0x9 0xA 0xB 0xC 0xD
after erase[0] data = 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF
programing [0x8100004] state = [0]
programing [0x8100008] state = [0]
programing [0x810000C] state = [0]
programing [0x8100010] state = [0]
programing [0x8100014] state = [0]
after program, data = 0x9 0xA 0xB 0xC 0xD
测试结果(STM32,正常状态返回值是4):
before erase, data = 0x9 0xA 0xB 0xC 0xD
after erase[4] data = 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF
programing [0x8040004] state = [4]
programing [0x8040008] state = [4]
programing [0x804000C] state = [4]
programing [0x8040010] state = [4]
programing [0x8040014] state = [4]
after program, data = 0x9 0xA 0xB 0xC 0xD
注:STM32F103不包含OTP区。
通过Keil的调试功能可以查看Flash地址的数据,当然也可以直接在程序中读取。
STM OB的描述:
STM OB数据:
GD32 OB数据:
GD32 OPT数据:
--EOF--