当前位置:网站首页>[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 .
边栏推荐
- Kubernetes 通信异常网络故障 解决思路
- Print linked list from end to end
- Pytorch sets the weight and bias of the model to zero
- 上周内容回顾
- Sort out several network request methods of JS -- get rid of callback hell
- Virtual machine installation deepin system
- Test changes in Devops mode -- learning and thinking
- Battle drag method 1: moderately optimistic, build self-confidence (1)
- 如临现场的视觉感染力,NBA决赛直播还能这样看?
- 2.6 formula calculation
猜你喜欢
An old programmer gave it to college students
[Yugong series] go teaching course 002 go language environment installation in July 2022
How can the outside world get values when using nodejs to link MySQL
Virtual machine installation deepin system
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
Etcd 基于Raft的一致性保证
Transformer structure analysis and the principle of blocks in it
强基计划 数学相关书籍 推荐
如临现场的视觉感染力,NBA决赛直播还能这样看?
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
随机推荐
String and+
Gee calculated area
Qt6 QML Book/Qt Quick 3D/基础知识
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
2.4 conversion of different data types
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
Strange way of expressing integers (expanding Chinese remainder theorem)
Plan for the first half of 2022 -- pass the PMP Exam
Basic number theory -- Chinese remainder theorem
Measurement fitting based on Halcon learning -- Practice [1]
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Virtual machine installation deepin system
Discussion Net legacy application transformation
"Designer universe" argument: Data Optimization in the design field ultimately falls on cost, safety and health | chinabrand.com org
QT6 QML book/qt quick 3d/ Basics
Global and Chinese market of high temperature Silver sintering paste 2022-2028: Research Report on technology, participants, trends, market size and share
Get log4net log file in C - get log4net log file in C
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
1.4 learn more about functions
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events