【GD32L233C-START】6、flash读写——使用内部flash存储数据

2022-04-04 15:53:16 浏览数 (1)

1、关于GD32 Flash

GD32 flash官方称为FMC。

2、关于GD32L233CCT6 flash

本次使用的GD32L233CCT6 flash大小为256k;

可以看出,分为64页,从0-63页,每页大小为4kb;

手册上说,支持32位整字和16位半字编程,但是库函数只有整字编程。

3、代码实现

代码语言:javascript复制
#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、读写测试

使用最后一页(0x0803E000-0x0803 FFFF),测试读写。

代码语言:javascript复制
#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("]rn");

	printf("Read Data[");
	for(uint8_t i=0;i<TEST_DATA_LEN;i  )
	{
		printf(" %d",ReadData[i]);
	}
	printf("]rn");
}

5、实验现象

可以发现,写入的和读出的数据是一样的。

0 人点赞