当前位置:网站首页>[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 .
边栏推荐
- 18、 MySQL -- index
- Offset related concepts + drag modal box case
- "Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
- 如临现场的视觉感染力,NBA决赛直播还能这样看?
- jvm jni 及 pvm pybind11 大批量数据传输及优化
- Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
- Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?
- How to handle wechat circle of friends marketing activities and share production and release skills
- QT tutorial: signal and slot mechanism
- How to choose cache read / write strategies in different business scenarios?
猜你喜欢

2.1 use of variables
![AI enhanced safety monitoring project [with detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
AI enhanced safety monitoring project [with detailed code]

JS three families

18、 MySQL -- index

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

The 29th day of force deduction (DP topic)

Wargames study notes -- Leviathan

@Transactional注解失效的场景

如临现场的视觉感染力,NBA决赛直播还能这样看?

你真的知道自己多大了吗?
随机推荐
"Designer universe" argument: Data Optimization in the design field ultimately falls on cost, safety and health | chinabrand.com org
Change deepin to Alibaba image source
Scientific research document management Zotero
Rhcsa third day notes
强化學習-學習筆記1 | 基礎概念
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Machine learning support vector machine SVM
6006. Take out the minimum number of magic beans
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
11-grom-v2-04-advanced query
MySQL master-slave synchronization principle
Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?
【c】 Digital bomb
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
全网都在疯传的《老板管理手册》(转)
18、 MySQL -- index
How to set the system volume programmatically- How to programmatically set the system volume?
JVM JNI and PVM pybind11 mass data transmission and optimization
2.7 format output of values
2.6 formula calculation