当前位置:网站首页>[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 .
边栏推荐
- Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
- Exercises of function recursion
- Rhcsa third day operation
- 2.6 formula calculation
- Producer consumer mode (multithreading, use of shared resources)
- 设计电商秒杀系统
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- Cannot load driver class: com. mysql. cj. jdbc. Driver
- 浅议.NET遗留应用改造
- 2.5 conversion of different data types (2)
猜你喜欢

Q&A:Transformer, Bert, ELMO, GPT, VIT

Node MySQL serialize cannot rollback transactions

Go learning notes (4) basic types and statements (3)

Transformer structure analysis and the principle of blocks in it

It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET

Machine learning support vector machine SVM

JMeter plug-in installation

Example of peanut shell inner net penetration

Etcd 基于Raft的一致性保证

Qt6 QML Book/Qt Quick 3D/基础知识
随机推荐
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
电子科技大学|强化学习中有效利用的聚类经验回放
Wireless network (preprocessing + concurrent search)
Example of peanut shell inner net penetration
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
强化學習-學習筆記1 | 基礎概念
Set, weakset, map, weakmap in ES6
Apprentissage intensif - notes d'apprentissage 1 | concepts de base
Gee calculated area
11-grom-v2-05-initialization
设计电商秒杀系统
Camera calibration (I): robot hand eye calibration
[postgresql]postgresql custom function returns an instance of table type
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Commands related to files and directories
Q&A:Transformer, Bert, ELMO, GPT, VIT
Reinforcement learning - learning notes 1 | basic concepts
For in, foreach, for of
强化学习-学习笔记1 | 基础概念
Test access criteria