简介
STM32F103C8T6 内部Flash 为 64KB,本次将对他多余空间进行读写。
介绍
数据手册下载
STM32F103x8/STM32F103xB 数据手册
包含Flash Memory Page分布
STM32F设备命名
设备容量类型
中容量类型
内部空间介绍
64 KBytes大小
Flash Memory 从 0x0800 0000 ~ 0x0801 FFFF
页分布1K每页, STM32F103C8T6 只有64KByte, 那就是64页
代码
使用
controllerstech Flash Program Code
复制 FLASH_PAGE_F1.h 和 FLASH_PAGE_F1.c到项目中
FLASH_PAGE_F1.h 加入宏定义
#define FLASH_START_ADDRESS (0x08000000 + FLASH_PAGE_SIZE * 20) //0~20页这20K用于存储你的程序
#define FLASH_MAX_PAGE_NUM (64-1-20) // 0 ~ (63-20) 是最大的页码
FLASH_PAGE_F1.c 修改
static uint32_t GetPage(uint32_t Address)
{
for (int indx=0; indx<(FLASH_MAX_PAGE_NUM+1); indx++)
{
if((Address < (FLASH_START_ADDRESS + (FLASH_PAGE_SIZE *(indx+1))) ) && (Address >= (FLASH_START_ADDRESS + FLASH_PAGE_SIZE*indx)))
{
return (FLASH_START_ADDRESS + FLASH_PAGE_SIZE*indx);
}
}
return 0;
}
测试代码
main.c
char *data = "hello FLASH from ControllerTech\
This is a test to see how many words can we work with";
uint32_t data2[] = {0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9};
uint32_t Rx_Data[30];
char string[100];
int number = 123;
float val = 123.456;
float RxVal;
int main()
{
...
printf("Hello World!\r\n");
Flash_Write_Data(GetPageFromPageNum(0) , (uint32_t *)data2, 9);
Flash_Read_Data(GetPageFromPageNum(0) , Rx_Data, 10);
printf ("RxData: { ");
for ( int i = 0; i < 9; ++i )
{
printf ("0x%X ", Rx_Data[i]);
}
printf ("};\r\n");
int numofwords = (strlen(data)/4)+((strlen(data)%4)!=0);
Flash_Write_Data(GetPageFromPageNum(1) , (uint32_t *)data, numofwords);
Flash_Read_Data(GetPageFromPageNum(1) , Rx_Data, numofwords);
Convert_To_Str(Rx_Data, string);
printf ("String: %s\r\n", string);
Flash_Write_NUM(GetPageFromPageNum(2), number);
RxVal = Flash_Read_NUM(GetPageFromPageNum(2));
printf ("Read Num_1: %f\r\n", RxVal);
Flash_Write_NUM(GetPageFromPageNum(3), val);
RxVal = Flash_Read_NUM(GetPageFromPageNum(3));
printf ("Read Num_2: %f\r\n", RxVal);
...
}
设置烧录地址区域, 避免其他区域被覆盖
20K = 0x5000 即0 ~ 20页 这个区域用来存放程序
共赏
代码
不需要下载, 上面都提供了
参考
普中STM32-PZ6806L开发板(HAL库函数实现-USART1 printf+scanf/gets)
Note
- FLASH_BASE 就是 0x08000000这个地址, 所以可以用 FLASH_BASE取代0x08000000