当前位置:网站首页>[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
2022-07-03 20:45:00 【freemote】
1、 About GD32 Flash
GD32 flash Officially called FMC.
2、 About GD32L233CCT6 flash

Used this time GD32L233CCT6 flash The size is 256k;
It can be seen that , It is divided into 64 page , from 0-63 page , The size of each page is 4kb;
The manual says , Support 32 Bitwise integer sum 16 Bit half word programming , But library functions only have whole word programming .
3、 Code implementation
#define FLASH_PAGE_SIZE 0x1000 //4k
void FlashWrite(uint16_t len,uint8_t *data,uint32_t addr_start)
{
uint16_t i=0;
uint32_t temp=0;
uint32_t addr=addr_start;
fmc_state_enum fmc_state=FMC_READY;
fmc_unlock();
for(i=0;i<len/4;i++)
{
temp = (data[0]<<0)+(data[1]<<8)+(data[2]<<16)+(data[3]<<24);
fmc_state=fmc_word_program(addr, temp);
if(fmc_state!=FMC_READY)
{
return;
}
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
data += 4;
addr += 4;
}
if((len % 4)==3)
{
temp = (data[0]<<0)+(data[1]<<8)+(data[2]<<16);
temp = temp | 0xff000000;
fmc_state=fmc_word_program(addr,temp);
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
}
else
{
if((len % 4)==2)
{
temp = (data[0]<<0)+(data[1]<<8);
temp = temp | 0xffff0000;
fmc_state=fmc_word_program(addr,temp);
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
}
else
{
if((len % 4)==1)
{
temp = (data[0]<<0);
temp = temp | 0xffffff00 ;
fmc_state=fmc_word_program(addr,temp);
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
}
}
}
fmc_lock();
}
void FlashRead(uint16_t len,uint8_t *outdata,uint32_t addr_start)
{
uint32_t addr;
uint16_t i;
addr = addr_start;
for(i=0;i<len;i++)
{
*outdata = *(uint8_t*) addr;
addr = addr + 1;
outdata++;
}
}
void FlashErase(uint32_t start, uint32_t end)
{
uint32_t EraseCounter;
fmc_state_enum fmc_state=FMC_READY;
/* unlock the flash program/erase controller */
fmc_unlock();
/* clear all pending flags */
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
/* erase the flash pages */
while(start < end)
{
EraseCounter = start/FLASH_PAGE_SIZE;
fmc_state=fmc_page_erase(EraseCounter*FLASH_PAGE_SIZE);
if(fmc_state!=FMC_READY)
{
return;
}
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
start += FLASH_PAGE_SIZE;
}
/* lock the main FMC after the erase operation */
fmc_lock();
}
4、 Read and write test
Use the last page (0x0803E000-0x0803 FFFF), Test reading and writing .
#define APP_DATA_SATRT_ADDR 0x0803E000 //4k
#define TEST_DATA_LEN 11
uint8_t WriteData[TEST_DATA_LEN]={
1,2,3,4,5,6,7,8,9,10,11};
uint8_t ReadData[TEST_DATA_LEN]={
0,0,0,0,0,0,0,0,0,0 ,0};
void FlashTest(void)
{
FlashErase(APP_DATA_SATRT_ADDR, APP_DATA_SATRT_ADDR+FLASH_PAGE_SIZE);
FlashWrite(TEST_DATA_LEN,WriteData,APP_DATA_SATRT_ADDR);
FlashRead(TEST_DATA_LEN,ReadData, APP_DATA_SATRT_ADDR);
printf("Write Data[");
for(uint8_t i=0;i<TEST_DATA_LEN;i++)
{
printf(" %d",WriteData[i]);
}
printf("]\r\n");
printf("Read Data[");
for(uint8_t i=0;i<TEST_DATA_LEN;i++)
{
printf(" %d",ReadData[i]);
}
printf("]\r\n");
}
5、 Experimental phenomena

You can find , The written and read data are the same .
边栏推荐
- MySQL learning notes - single table query
- 全网都在疯传的《老板管理手册》(转)
- 19、 MySQL -- SQL statements and queries
- In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
- 设计电商秒杀系统
- Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
- Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
- Test access criteria
- Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
- Oak-d raspberry pie cloud project [with detailed code]
猜你喜欢

Commands related to files and directories
![[Tang Laoshi] C -- encapsulation: member variables and access modifiers](/img/be/0b38c0f1a27f819f7c79bcf634fbd4.jpg)
[Tang Laoshi] C -- encapsulation: member variables and access modifiers

Scientific research document management Zotero

Rhcsa third day notes

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...

2.6 formula calculation

How to choose cache read / write strategies in different business scenarios?

How to modify the network IP addresses of mobile phones and computers?

一台服务器最大并发 tcp 连接数多少?65535?

LabVIEW training
随机推荐
Preliminary practice of niuke.com (11)
Measurement fitting based on Halcon learning -- Practice [1]
Etcd 基于Raft的一致性保证
强基计划 数学相关书籍 推荐
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
Set, weakset, map, weakmap in ES6
String and+
In 2021, the global revenue of syphilis rapid detection kits was about US $608.1 million, and it is expected to reach US $712.9 million in 2028
Cap and base theory
Recommendation of books related to strong foundation program mathematics
2.4 conversion of different data types
不同业务场景该如何选择缓存的读写策略?
How to do Taobao full screen rotation code? Taobao rotation tmall full screen rotation code
18、 MySQL -- index
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
上周内容回顾
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
For in, foreach, for of
Virtual machine installation deepin system