当我们把程序编译好之后下载到MCU之后,下载器会把程序文件下载到MCU的内部FLASH中,内部FLASH除非人为擦除,否则会永久保存。内部FLASH除了可以保存代码之外,还可以保存数据。STM32F4X的内部FLASH有以下特点。
可以看到STM32F40X和STM32F41X将1MB的内部FLASH分成了12个扇区,每个扇区的大小都不一样,分别是4个16KB的扇区,1个64KB的扇区和7个128KB的扇区。
STM32F42X和STM32F43X的内部FLASH结构跟STM32F40X和STM32F41X的差不多,只是有点细微的差别。STM32F42X和STM32F43X将内部2MB的FLASH分成了2个块,每个块是1MB,每个块分成了12个扇区,每个扇区的大小都不一样,分别是4个16KB的扇区,1个64KB的扇区和7个128KB的扇区。
#ifndef __INTERNAL_FLASH__H
#define __INTERNAL_FLASH__H
#include "stm32f4xx.h"
#include "stdio.h"
#define ADDR_FLASH_SERTOR_0 (0x8000000)
#define ADDR_FLASH_SERTOR_1 (0x8004000)
#define ADDR_FLASH_SERTOR_2 (0x8008000)
#define ADDR_FLASH_SERTOR_3 (0x800C000)
#define ADDR_FLASH_SERTOR_4 (0x8010000)
#define ADDR_FLASH_SERTOR_5 (0x8020000)
#define ADDR_FLASH_SERTOR_6 (0x8040000)
#define ADDR_FLASH_SERTOR_7 (0x8060000)
#define ADDR_FLASH_SERTOR_8 (0x8080000)
#define ADDR_FLASH_SERTOR_9 (0x80A0000)
#define ADDR_FLASH_SERTOR_10 (0x80C0000)
#define ADDR_FLASH_SERTOR_11 (0x80E0000)
int flash_test(void);
unsigned int get_sector_index(unsigned int start_address);
int compare_u8_data(const unsigned char *src,unsigned int start_address,int len);
int compare_u16_data(const unsigned short *src,unsigned int start_address,int len);
int compare_u32_data(const unsigned int *src,unsigned int start_address,int len);
#endif
#include "internal_flash.h"
static unsigned char test_data_u8[] =
{
//随机数个数:100;每行显示个数:10;取值范围:0到255
157, 115, 145, 130, 66, 251, 215, 97, 214, 233,
69, 161, 177, 204, 140, 72, 143, 247, 248, 120,
219, 190, 224, 68, 45, 233, 65, 194, 17, 98,
166, 97, 10, 49, 75, 125, 214, 170, 129, 49,
244, 229, 84, 109, 100, 111, 108, 72, 174, 63,
49, 151, 37, 236, 51, 190, 109, 29, 200, 198,
59, 64, 247, 176, 92, 127, 148, 9, 249, 239,
173, 191, 25, 11, 37, 51, 103, 98, 159, 143,
54, 140, 157, 90, 194, 98, 233, 74, 184, 111,
53, 89, 249, 161, 254, 26, 155, 68, 182, 78,
};
static unsigned short test_data_u16[] =
{
//随机数个数:100;每行显示个数:10;取值范围:256到65535
10301, 24195, 26063, 23491, 30667, 1467, 25287, 28733, 28285, 649,
20974, 22870, 22983, 28195, 8943, 22043, 4696, 15632, 15502, 9559,
17054, 31636, 5956, 17834, 30344, 11887, 27433, 31707, 22671, 11207,
29976, 4975, 16375, 32866, 5901, 3451, 29778, 13592, 22481, 12726,
19015, 2184, 1498, 13778, 32145, 28939, 364, 28429, 27722, 31462,
29189, 16899, 5994, 22212, 27384, 17606, 3211, 11363, 17240, 18224,
7407, 1421, 9495, 13688, 1475, 25930, 5999, 2308, 28069, 24404,
2177, 15230, 23397, 17355, 6717, 28714, 2568, 13098, 13953, 18295,
15891, 11898, 11909, 4308, 32774, 30661, 26998, 20164, 24614, 22222,
26209, 16693, 10111, 5631, 5459, 27150, 6698, 19363, 19391, 16391,
};
static unsigned int test_data_u32[] =
{
//随机数个数:100;每行显示个数:10;取值范围:65536到100000
77260, 82517, 87839, 66237, 89695, 72376, 96222, 92678, 95797, 93033,
80246, 71041, 65849, 67378, 98262, 66674, 82041, 77441, 81670, 96973,
83937, 83480, 75533, 69009, 76105, 91653, 83731, 84311, 86195, 87253,
93123, 75563, 95693, 82693, 81139, 82842, 83646, 88761, 89735, 68620,
77854, 96176, 71826, 87637, 91718, 72568, 84671, 71302, 70603, 94686,
77552, 75529, 86341, 96681, 77604, 87789, 84565, 75144, 86686, 78462,
78822, 67912, 67140, 80409, 96178, 91978, 83013, 97086, 80136, 78389,
89854, 81895, 71373, 87784, 95007, 83992, 85698, 90803, 83209, 82360,
75268, 88518, 68987, 94879, 80601, 86505, 96622, 84208, 74665, 75118,
90429, 83369, 98123, 86628, 97033, 95608, 88707, 88590, 68700, 93137,
};
unsigned int get_sector_index(unsigned int start_address)
{
if(start_address >= ADDR_FLASH_SERTOR_0 && start_address < ADDR_FLASH_SERTOR_1)
return FLASH_Sector_0;
else if(start_address >= ADDR_FLASH_SERTOR_1 && start_address < ADDR_FLASH_SERTOR_2)
return FLASH_Sector_1;
else if(start_address >= ADDR_FLASH_SERTOR_2 && start_address < ADDR_FLASH_SERTOR_3)
return FLASH_Sector_2;
else if(start_address >= ADDR_FLASH_SERTOR_3 && start_address < ADDR_FLASH_SERTOR_4)
return FLASH_Sector_3;
else if(start_address >= ADDR_FLASH_SERTOR_4 && start_address < ADDR_FLASH_SERTOR_5)
return FLASH_Sector_4;
else if(start_address >= ADDR_FLASH_SERTOR_5 && start_address < ADDR_FLASH_SERTOR_6)
return FLASH_Sector_5;
else if(start_address >= ADDR_FLASH_SERTOR_6 && start_address < ADDR_FLASH_SERTOR_7)
return FLASH_Sector_6;
else if(start_address >= ADDR_FLASH_SERTOR_7 && start_address < ADDR_FLASH_SERTOR_8)
return FLASH_Sector_7;
else if(start_address >= ADDR_FLASH_SERTOR_8 && start_address < ADDR_FLASH_SERTOR_9)
return FLASH_Sector_8;
else if(start_address >= ADDR_FLASH_SERTOR_9 && start_address < ADDR_FLASH_SERTOR_10)
return FLASH_Sector_9;
else if(start_address >= ADDR_FLASH_SERTOR_10 && start_address < ADDR_FLASH_SERTOR_11)
return FLASH_Sector_10;
else
return FLASH_Sector_11;
}
int flash_test(void)
{
unsigned int i,write_setor,ret = 0;
FLASH_Unlock();
write_setor = get_sector_index(ADDR_FLASH_SERTOR_6); // 判断FLASH地址的扇区索引
/* 字节读写测试 */
if(FLASH_EraseSector(write_setor,VoltageRange_3) != FLASH_COMPLETE) // 扇区擦除
printf("flash erase error\r\n");
else
printf("flash erase success\r\n");
for(i = 0;i < sizeof(test_data_u8);i++)
FLASH_ProgramByte(ADDR_FLASH_SERTOR_6 + i,test_data_u8[i]); // 写数据
if(compare_u8_data(test_data_u8,ADDR_FLASH_SERTOR_6,sizeof(test_data_u8)) != 0) // 比较
{
printf("flash write u8 error\r\n");
ret = 1;
goto end;
}
else
printf("flash write u8 success\r\n");
/* 半字读写测试 */
if(FLASH_EraseSector(write_setor,VoltageRange_3) != FLASH_COMPLETE) // 扇区擦除
printf("flash erase error\r\n");
else
printf("flash erase success\r\n");
for(i = 0;i < sizeof(test_data_u16);i++)
FLASH_ProgramHalfWord(ADDR_FLASH_SERTOR_6 + i * 2,test_data_u16[i]); // 写数据
if(compare_u16_data(test_data_u16,ADDR_FLASH_SERTOR_6,sizeof(test_data_u16)) != 0) // 比较
{
printf("flash write u16 error\r\n");
ret = 1;
goto end;
}
else
printf("flash write u16 success\r\n");
/* 字读写测试 */
if(FLASH_EraseSector(write_setor,VoltageRange_3) != FLASH_COMPLETE) // 扇区擦除
printf("flash erase error\r\n");
else
printf("flash erase success\r\n");
for(i = 0;i < sizeof(test_data_u32);i++)
FLASH_ProgramWord(ADDR_FLASH_SERTOR_6 + i * 4,test_data_u32[i]); // 写数据
if(compare_u32_data(test_data_u32,ADDR_FLASH_SERTOR_6,sizeof(test_data_u32)) != 0) // 比较
{
printf("flash write u32 error\r\n");
ret = 1;
goto end;
}
else
printf("flash write u32 success\r\n");
end:
FLASH_Lock();
return ret;
}
int compare_u8_data(const unsigned char *src,unsigned int start_address,int len)
{
int i;
for(i = 0;i < len;i++)
{
if(*((unsigned char *)(start_address + i)) != src[i])
return 1;
}
return 0;
}
int compare_u16_data(const unsigned short *src,unsigned int start_address,int len)
{
int i;
for(i = 0;i < len;i++)
{
if(*((unsigned short *)(start_address + i * 2)) != src[i])
return 1;
}
return 0;
}
int compare_u32_data(const unsigned int *src,unsigned int start_address,int len)
{
int i;
for(i = 0;i < len;i++)
{
if(*((unsigned int *)(start_address + i * 4)) != src[i])
return 1;
}
return 0;
}
#include "stm32f4xx.h"
#include "delay.h"
#include "usart.h"
#include "internal_flash.h"
int main(void)
{
int i;
NVIC_PriorityGroupConfig(2);
system_tick_init();
bsp_usart_init(115200);
if(flash_test() == 0)
printf("STM32F4X Internal Test Scuess\r\n");
else
printf("STM32F4X Internal Test Error\r\n");
while(1){
}
}