• STM32F4X 内部FLASH使用



    在嵌入式开发中,经常需要实时保存一些数据。如果工程的代码量比较大的话可以使用一些外部的存储器件进行数据的保存。如果工程的代码量比较小则可以利用MCU的内部FLASH进行数据的存储。

    STM32F4X 内部FLASH

    当我们把程序编译好之后下载到MCU之后,下载器会把程序文件下载到MCU的内部FLASH中,内部FLASH除非人为擦除,否则会永久保存。内部FLASH除了可以保存代码之外,还可以保存数据。STM32F4X的内部FLASH有以下特点。

    • STM32F40X和STM32F41X的内部FLASH容量是1MB,而STM32F42X和STM32F43X的内部FLASH容量是2MB
    • STM32F4X的内部FLASH支持字节、半字、字和双字写入
    • STM32F4X的内部FLASH支持扇区擦除和全擦除

    STM32F4X内部FLASH结构

    STM32F40X和STM32F41X内部FLASH结构

    在这里插入图片描述

    可以看到STM32F40X和STM32F41X将1MB的内部FLASH分成了12个扇区,每个扇区的大小都不一样,分别是4个16KB的扇区,1个64KB的扇区和7个128KB的扇区。

    STM32F42X和STM32F43X内部FLASH结构

    在这里插入图片描述
    STM32F42X和STM32F43X的内部FLASH结构跟STM32F40X和STM32F41X的差不多,只是有点细微的差别。STM32F42X和STM32F43X将内部2MB的FLASH分成了2个块,每个块是1MB,每个块分成了12个扇区,每个扇区的大小都不一样,分别是4个16KB的扇区,1个64KB的扇区和7个128KB的扇区。

    STM32F4X内部FLASH操作

    • 先将内部FLASH解锁
    • 擦除扇区
    • 往FLASH中写入数据
    • 读出写入的数据进行校验
    • FLASH上锁

    例程

    internal_flash.h

    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    internal_flash.c

    #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;
    }	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181

    main.c

    #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){
    	
    	  
    	}
      
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

  • 相关阅读:
    Linux进程
    在vue3+vite3中使用socket.io-client踩坑记录
    Linux 安装字体
    3、Pinpoint-Agent端注册服务到Server端
    SpringBoot容器化部署_Dockerfile制作镜像
    【苹果家庭群发iMessage】 软件安装应用层的数据包的丢失不涉及IP层
    [Java反序列化]—CommonsCollections6
    2023年全球及中国层析系统市场发展趋势分析:未来层析设备市场将持续增长[图]
    【成功实现】CentOS磁盘扩容
    4. Java 的线程安全机制之`volatile`
  • 原文地址:https://blog.csdn.net/hwx1546/article/details/133706640