写出GD32L233/235在读写flash的区别(用于OTA功能)

1.说到OTA功能,实际上 往flash 指定区域写 数据, 拷贝数据,然后 跳转, 必要时 注意单片机的偏移量。

2.不多说废话,进入正题,区别这两款 单片机的在读写flash的 不同点. go

在写本文时,L233支持单字写入,而L235只有双字写入. 一个写入地址+4 一个写入地址+8

在单片机里面表示 大概是 address += sizeof(uint32_t) address += sizeof(uint64_t)

下面是写入flash 代码:

记得在写入前 擦除

cpp 复制代码
void fmc_flags_clear(void)
{
    fmc_flag_clear(FMC_FLAG_END);
    fmc_flag_clear(FMC_FLAG_WPERR);
    fmc_flag_clear(FMC_FLAG_PGAERR);
    fmc_flag_clear(FMC_FLAG_PGERR);
}
void WriteFlash(uint32_t address,const uint8_t *pBuffer,uint32_t Numlengh)
{
    #ifdef GD32E230  //GD32L233
	uint32_t i,temp;
  	fmc_unlock();
	for(i = 0; i < Numlengh;i+= 4)
	{
		temp =  (uint32_t)pBuffer[i+3]<<24;
		temp |=  (uint32_t)pBuffer[i+2]<<16;
		temp |=  (uint32_t)pBuffer[i+1]<<8;
		temp |=  (uint32_t)pBuffer[i];
		fmc_word_program(address+i,temp);
		fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
	}
  	fmc_lock();
    #endif

    #ifdef GD32L235
	uint32_t i;
    uint32_t temp1,temp2;
    uint64_t temp_v;
  	fmc_unlock();
	for(i = 0; i < Numlengh;i+=8)
	{
        temp1 =  (uint32_t)pBuffer[i+3]<<24;
		temp1 |=  (uint32_t)pBuffer[i+2]<<16;
		temp1 |=  (uint32_t)pBuffer[i+1]<<8;
		temp1 |=  (uint32_t)pBuffer[i];

        temp2 =   (uint32_t)pBuffer[i+7]<<24;
		temp2 |=  (uint32_t)pBuffer[i+6]<<16;
		temp2 |=  (uint32_t)pBuffer[i+5]<<8;
		temp2 |=  (uint32_t)pBuffer[i+4];

        temp_v = ((uint64_t)temp2) << 32 | temp1;

        fmc_doubleword_program(address+i,temp_v);
		fmc_flags_clear();
	}
  	fmc_lock();
    #endif
}

这个是 自己写的擦除一页 数据,当然可以参考 提供的demo去实现,看自己需求

cpp 复制代码
uint8_t EarseFlash_1K(uint32_t address){
    //PR_INFO("begin_earse,0x%X\r\n",address);
    fmc_state_enum fmc_state;
	if(address % 1024 == 0)
	{
        fmc_unlock();
        fmc_flags_clear();
		fmc_state = fmc_page_erase(address);
        if(fmc_state == FMC_READY){
            PR_INFO("erase_ok\r\n");
        }else{
            PR_ERR("erase_fail\r\n");
        }
        fmc_flags_clear();
        fmc_lock();
	}
	else
	{
        PR_NOTICE("without_EarseFlash_1K\r\n");
		return 0;
	}
	return 1;
}

接下来是 读取 双字,也就是在L235上的读取

cpp 复制代码
void ReadFlashTwoWorld(uint32_t address,uint8_t *Nbuffer,uint32_t  length){
    uint64_t temp = 0;
    uint32_t count = 0;
	while(count < length){
        temp = *((volatile uint64_t *)address);
        *Nbuffer++ = (temp & 0xff);
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 8) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 16) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 24) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 32) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 40) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 48) & 0xff;
		count++;
        if(count >= length)break;
        *Nbuffer++ = (temp >> 56) & 0xff;
		count++;
        if(count >= length)break;
        address += sizeof(uint64_t);
    }
}

以及 下面的 读取单字的 ( GD32L233 以及 GD32E230 )

cpp 复制代码
void ReadFlash(uint32_t address,uint8_t *Nbuffer,uint32_t  length)
{
	uint32_t temp = 0;
	uint32_t count = 0;
	while(count < length)
	{
		temp = *((volatile uint32_t *)address);
		*Nbuffer++ = (temp & 0xff);
		count++;
    if(count >= length)
      break;
	  *Nbuffer++ = (temp >> 8) & 0xff;
		count++;
		if(count >= length)
      break;
		*Nbuffer++ = (temp >> 16) & 0xff;
		count++;
		if(count >= length)
      break;
		*Nbuffer++ = (temp >> 24) & 0xff;
		count++;
		if(count >= length)
      break;

        #ifdef GD32E230       //GD32L233
		    address += 4;
        #endif
	}
}
相关推荐
fengfuyao9855 小时前
STM32如何定位HardFault错误,一种实用方法
stm32·单片机·嵌入式硬件
爱学习的颖颖6 小时前
EXTI外部中断的执行逻辑|以对射式红外传感器计次为例
单片机·嵌入式硬件·exti中断
艾莉丝努力练剑6 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
AI精钢7 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网
Cx330❀8 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
..过云雨9 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
etcix10 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
谱写秋天10 小时前
在STM32F103上进行FreeRTOS移植和配置(STM32CubeIDE)
c语言·stm32·单片机·freertos
我不是板神10 小时前
程序设计|C语言教学——C语言基础2:计算与控制语句
c语言
基于python的毕设10 小时前
C语言栈的实现
linux·c语言·ubuntu