Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
flash
GD32F450
GD32F450 片内 flash驱动适配
发布于 2024-12-13 09:26:11 浏览:26
订阅该版
drv_flash.h 的内容如下: ``` #ifndef __DRV_FLASH_H__ #define __DRV_FLASH_H__ #include
#include "gd32f4xx_fmc.h" #ifdef __cplusplus extern "C" { #endif int gd32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size); int gd32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size); int gd32_flash_erase(rt_uint32_t addr, size_t size); #ifdef __cplusplus } #endif #endif /* __DRV_FLASH_H__ */ ``` drv_flash.c 的内容如下: ``` /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-12-5 SummerGift first version */ #include "board.h" #ifdef BSP_USING_ON_CHIP_FLASH #include "drv_flash.h" #if defined(RT_USING_FAL) #include "fal.h" #endif #define DRV_DEBUG //#define LOG_TAG "drv.flash" //#include
//对于主存储闪存容量不多于3072KB的GD32F4xx,包含8个16KB的扇区、2个64KB的扇区、14 //个128KB的扇区、4个256KB的扇区。主存储闪存的每个扇区都可以单独擦除。 512 code + 512 data //其中code区的地址范围是0x08000000~0x0807FFFF,和data区地址范围是0x08080000~0x080FFFFF,code和data区的读写速率差距很大 /* Base address of the Flash sectors Bank 0 */ //code #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */ #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */ #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */ #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */ #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */ #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */ #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */ #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */ //data #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */ #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */ #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */ #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */ /* //FLASH起始地址 #define GD32_FLASH_BASE 0x08000000 //GD32 FLASH的起始地址 //FLASH 扇区的起始地址 #define ADDR_FLASH_SECTOR_0 ((unsigned int)0x08000000) //扇区0起始地址, 16 Kbytes #define ADDR_FLASH_SECTOR_1 ((unsigned int)0x08004000) //扇区1起始地址, 16 Kbytes #define ADDR_FLASH_SECTOR_2 ((unsigned int)0x08008000) //扇区2起始地址, 16 Kbytes #define ADDR_FLASH_SECTOR_3 ((unsigned int)0x0800C000) //扇区3起始地址, 16 Kbytes #define ADDR_FLASH_SECTOR_4 ((unsigned int)0x08010000) //扇区4起始地址, 64 Kbytes #define ADDR_FLASH_SECTOR_5 ((unsigned int)0x08020000) //扇区5起始地址, 128 Kbytes #define ADDR_FLASH_SECTOR_6 ((unsigned int)0x08040000) //扇区6起始地址, 128 Kbytes #define ADDR_FLASH_SECTOR_7 ((unsigned int)0x08060000) //扇区7起始地址, 128 Kbytes #define ADDR_FLASH_SECTOR_8 ((unsigned int)0x08080000) //扇区8起始地址, 128 Kbytes #define ADDR_FLASH_SECTOR_9 ((unsigned int)0x080A0000) //扇区9起始地址, 128 Kbytes #define ADDR_FLASH_SECTOR_10 ((unsigned int)0x080C0000) //扇区10起始地址,128 Kbytes #define ADDR_FLASH_SECTOR_11 ((unsigned int)0x080E0000) //扇区11起始地址,128 Kbytes */ // @defgroup FLASH_Sectors #define FLASH_Sector_0 ((uint16_t)0x0000) /*!< Sector Number 0 */ #define FLASH_Sector_1 ((uint16_t)0x0008) /*!< Sector Number 1 */ #define FLASH_Sector_2 ((uint16_t)0x0010) /*!< Sector Number 2 */ #define FLASH_Sector_3 ((uint16_t)0x0018) /*!< Sector Number 3 */ #define FLASH_Sector_4 ((uint16_t)0x0020) /*!< Sector Number 4 */ #define FLASH_Sector_5 ((uint16_t)0x0028) /*!< Sector Number 5 */ #define FLASH_Sector_6 ((uint16_t)0x0030) /*!< Sector Number 6 */ #define FLASH_Sector_7 ((uint16_t)0x0038) /*!< Sector Number 7 */ #define FLASH_Sector_8 ((uint16_t)0x0040) /*!< Sector Number 8 */ #define FLASH_Sector_9 ((uint16_t)0x0048) /*!< Sector Number 9 */ #define FLASH_Sector_10 ((uint16_t)0x0050) /*!< Sector Number 10 */ #define FLASH_Sector_11 ((uint16_t)0x0058) /*!< Sector Number 11 */ #define FLASH_Sector_12 ((uint16_t)0x0080) /*!< Sector Number 12 */ #define FLASH_Sector_13 ((uint16_t)0x0088) /*!< Sector Number 13 */ #define FLASH_Sector_14 ((uint16_t)0x0090) /*!< Sector Number 14 */ #define FLASH_Sector_15 ((uint16_t)0x0098) /*!< Sector Number 15 */ #define FLASH_Sector_16 ((uint16_t)0x00A0) /*!< Sector Number 16 */ #define FLASH_Sector_17 ((uint16_t)0x00A8) /*!< Sector Number 17 */ #define FLASH_Sector_18 ((uint16_t)0x00B0) /*!< Sector Number 18 */ #define FLASH_Sector_19 ((uint16_t)0x00B8) /*!< Sector Number 19 */ #define FLASH_Sector_20 ((uint16_t)0x00C0) /*!< Sector Number 20 */ #define FLASH_Sector_21 ((uint16_t)0x00C8) /*!< Sector Number 21 */ #define FLASH_Sector_22 ((uint16_t)0x00D0) /*!< Sector Number 22 */ #define FLASH_Sector_23 ((uint16_t)0x00D8) /*!< Sector Number 23 */ // https://blog.csdn.net/qq_51930953/article/details/127511598 #define GD32_FLASH_START_ADRESS ((uint32_t)0x08000000) #define GD32_FLASH_SIZE (1024 * 1024) //512 code + 512 data #define GD32_FLASH_END_ADDRESS ((uint32_t)(GD32_FLASH_START_ADRESS + GD32_FLASH_SIZE)) #define GD32_FLASH_START_ADRESS_16K ((uint32_t)0x08000000) #define FLASH_SIZE_GRANULARITY_16K (4*16 * 1024) #define GD32_FLASH_START_ADRESS_64K ((uint32_t)0x08010000) #define FLASH_SIZE_GRANULARITY_64K (1*64 * 1024) #define GD32_FLASH_START_ADRESS_128K ((uint32_t)0x08020000) #define FLASH_SIZE_GRANULARITY_128K (7*128 * 1024) /** * @brief Gets the sector of a given address * @param None * @retval The sector of a given address */ static rt_uint32_t GetSector(rt_uint32_t Address) { // rt_uint32_t sector = FLASH_Sector_0; // if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) // { // sector = FLASH_Sector_0; // } // else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) // { // sector = FLASH_Sector_1; // } // else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) // { // sector = FLASH_Sector_2; // } // else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) // { // sector = FLASH_Sector_3; // } // else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) // { // sector = FLASH_Sector_4; // } // else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) // { // sector = FLASH_Sector_5; // } // else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) // { // sector = FLASH_Sector_6; // } // else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) // { // sector = FLASH_Sector_7; // } // else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) // { // sector = FLASH_Sector_8; // } // else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) // { // sector = FLASH_Sector_9; // } // else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) // { // sector = FLASH_Sector_10; // } // else if((Address < GD32_FLASH_END_ADDRESS) && (Address >= ADDR_FLASH_SECTOR_11)) // { // sector = FLASH_Sector_11; // } // else // { // } // return sector; rt_uint32_t sector = 0; if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) { sector = 0; } else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) { sector = 1; } else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) { sector = 2; } else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) { sector = 3; } else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) { sector = 4; } else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) { sector = 5; } else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) { sector = 6; } else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) { sector = 7; } else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) { sector = 8; } else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) { sector = 9; } else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) { sector = 10; } else if((Address < GD32_FLASH_END_ADDRESS) && (Address >= ADDR_FLASH_SECTOR_11)) { sector = 11; } else { } return sector; } /** * Read data from flash. * @note This operation's units is word. * * @param addr flash address * @param buf buffer to store read data * @param size read bytes size * * @return result */ static int gd32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size) { size_t i; if ((addr + size) > GD32_FLASH_END_ADDRESS) { rt_kprintf("read outrange flash size! addr is (0x%p)", (void*)(addr + size)); return -1; } for (i = 0; i < size; i++, buf++, addr++) { *buf = *(rt_uint8_t *) addr; } return size; } /** * Write data to flash. * @note This operation's units is word. * @note This operation must after erase. @see flash_erase. * * @param addr flash address * @param buf the write data buffer * @param size write bytes size * * @return result */ static int gd32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size) { rt_err_t result = RT_EOK; rt_uint32_t end_addr = addr + size; if ((end_addr) > GD32_FLASH_END_ADDRESS) { rt_kprintf("write outrange flash size! addr is (0x%p)", (void*)(addr + size)); return -RT_EINVAL; } if (size < 1) { return -RT_EINVAL; } fmc_unlock(); //解锁flash保护 fmc_flag_clear(FMC_FLAG_END); fmc_flag_clear( FMC_FLAG_OPERR ); fmc_flag_clear(FMC_FLAG_WPERR ); fmc_flag_clear( FMC_FLAG_PGMERR ); fmc_flag_clear(FMC_FLAG_PGSERR ); fmc_flag_clear(FMC_FLAG_RDDERR); for (size_t i = 0; i < size; i++, addr++, buf++) { /* write data to flash */ //if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK) if( fmc_byte_program(addr, (rt_uint8_t)(*buf)) == FMC_READY ) { if (*(rt_uint8_t *)addr != *buf) { result = -RT_ERROR; break; } } else { result = -RT_ERROR; break; } } fmc_lock(); //flash上锁 if (result != RT_EOK) { return result; } return size; } /** * Erase data on flash. * @note This operation is irreversible. * @note This operation's units is different which on many chips. * * @param addr flash address * @param size erase bytes size * * @return result */ static int gd32_flash_erase(rt_uint32_t addr, size_t size) { rt_err_t result = RT_EOK; rt_uint32_t FirstSector = 0, NbOfSectors = 0; if ((addr + size) > GD32_FLASH_END_ADDRESS) { rt_kprintf("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size)); return -RT_EINVAL; } // /* Unlock the Flash to enable the flash control register access */ // HAL_FLASH_Unlock(); fmc_unlock(); //解锁flash保护 /* Get the 1st sector to erase */ FirstSector = GetSector(addr); /* Get the number of sector to erase from 1st sector*/ NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1; fmc_flag_clear(FMC_FLAG_END); fmc_flag_clear( FMC_FLAG_OPERR ); fmc_flag_clear(FMC_FLAG_WPERR ); fmc_flag_clear( FMC_FLAG_PGMERR ); fmc_flag_clear(FMC_FLAG_PGSERR ); fmc_flag_clear(FMC_FLAG_RDDERR); for(int i=0;i
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
YZRD
这家伙很懒,什么也没写!
文章
23
回答
220
被采纳
19
关注TA
发私信
相关文章
1
OTA 片上FLASH擦除失败
2
请问 怎么实现flash的FIFO存储呢
3
FAL初始化输出调试信息bug
4
spi1挂载w25q32失败
5
SD卡与外置flash(dg25q32)一起应用,挂载spi2总线上
6
w25q128 flash 怎么在电脑和开发板上切换挂载
7
RTT没有对FLASH进行操作的指令吗
8
求一份基于RTT系统封装好的STM32F1系列的FLASH操作程序
9
使用FAL接口写的片内flash的擦除函数会死掉
10
关于潘多拉flash操作例程的
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
freemodbus
PWM
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
10
个答案
1
次被采纳
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
YZRD
2
篇文章
3
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部