4012 views|0 replies

565

Posts

0

Resources
The OP
 

[GD32L233C-START Review] 5. Flash Read and Write - Using Internal Flash to Store Data [Copy link]

related articles:

[GD32L233C-START Review] 1. GD32L233C-START with obvious advantages and disadvantages (unboxing)

[GD32L233C-START Review] 2. Non-blocking way to light up, blink, blink, blink...

[GD32L233C-START Review] 3. PWM to achieve breathing light

[GD32L233C-START Review] 4. Serial port variable length data reception

1. About GD32 Flash

GD32 flash is officially called FMC.

2. About GD32L233CCT6 flash

The GD32L233CCT6 flash used this time has a size of 256k;
As can be seen, it is divided into 64 pages, from page 0 to page 63, and the size of each page is 4kb;

The manual says that 32-bit full word and 16-bit half word programming is supported, but the library function only supports full 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. Reading and Writing Test

Use the last page (0x0803E000-0x0803 FFFF) to 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 phenomenon

It can be found that the written and read data are the same.

This post is from GD32 MCU
Personal signaturestm32/LoRa物联网:304350312
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Homemade STEVAL-IPM05F 3Sh board: FOC motor control 400V/8A non-sensing/sensing Hall/sensing encoder and other reference programs...

This post was last edited by music_586 on 2019-4-4 19:06 This content was originally created by EEWORLD forum user musi ...

Raspberry Pi high-resolution square display PIMORONI HYPERPIXEL 4.0 SQUARES

Available in capacitive touch and non-touch versions This square version of HyperPixel 4.0 is perfect for custom inte ...

Soliciting opinions: October community theme event, Hongmeng?

In August, I did what I had always wanted to do. >>Follow the expert Cruelfox and check in to learn FreeRTOS In Sept ...

A very interesting experiment about KVL and Faraday's law of electromagnetic induction

This post was last edited by Buyixin on 2020-11-8 17:49 Question : https://www.bilibili.com/video/BV1ht411U7q7 Resp ...

35 "Ten Thousand Miles" Raspberry Pi Car——ROS Learning (Realizing Hello World)

The best way to learn ROS is to use it. The ROS official website has a Chinese version of the tutorial . After install ...

[Flower carving DIY] Interesting and fun music visualization project (11) --- WS2812 fantasy light strip

I had the urge to do a series of topics on sound visualization. This topic is a bit difficult and covers a wide range of ...

Learn to make and use Tina's Docker image compilation from scratch

This post was last edited by walker2048 on 2022-8-28 10:43 ### Foreword This is the first time that a novice player has ...

Buy a solid state drive and find a surprise when you open it

I saw in the news that someone bought a 512GB solid-state drive on Xianyu, but the speed was very slow, not even as fast ...

【DigiKey Creative Contest】Multi-channel Micro Gas Chromatography Acquisition Unit-1. Introduction and System Solution

This post was last edited by sunduoze on 2024-1-5 00:02 1. Introduction This work mainly develops a multi-channel acqu ...

Evaluation summary: Mil based on Allwinner T527 development board

Activity details: Updated to 2024-06-05Evaluation report summary:@JerryZhen Postscript of the Fifth Project Chapter 4 Bu ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list