当前位置:网站首页>[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 .
边栏推荐
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
- 2.2 integer
- 18、 MySQL -- index
- Task of gradle learning
- Reinforcement learning - learning notes 1 | basic concepts
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- Analysis of gas fee setting under eip1559
- Gauss elimination solves linear equations (floating-point Gauss elimination template)
- QT6 QML book/qt quick 3d/ Basics
- 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
猜你喜欢

Go learning notes (4) basic types and statements (3)
![AI enhanced safety monitoring project [with detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
AI enhanced safety monitoring project [with detailed code]

Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths

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

19、 MySQL -- SQL statements and queries
![Oak-d raspberry pie cloud project [with detailed code]](/img/34/76b461bf03fba373da5b5898c5204c.jpg)
Oak-d raspberry pie cloud project [with detailed code]

Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022

全网都在疯传的《老板管理手册》(转)

Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?

Discussion Net legacy application transformation
随机推荐
Recommendation of books related to strong foundation program mathematics
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
Rhcsa third day operation
For in, foreach, for of
Use of CMD command
Sort out several network request methods of JS -- get rid of callback hell
Derivation of decision tree theory
MySQL learning notes - single table query
Gauss elimination solves linear equations (floating-point Gauss elimination template)
APEC industry +: father of the king of the ox mill, industrial Internet "king of the ox mill anti-wear faction" Valentine's Day greetings | Asia Pacific Economic media | ChinaBrand
阻塞非阻塞和同步异步的区分 参考一些书籍
Cap and base theory
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?
Scientific research document management Zotero
Q&A:Transformer, Bert, ELMO, GPT, VIT
【c】 Digital bomb
Pytorch sets the weight and bias of the model to zero
Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
Discussion Net legacy application transformation