Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
FAL
flash
Hardfault
FAL使用片内FLASH,使用出现Hard Fault
发布于 2022-01-11 10:46:47 浏览:1531
订阅该版
![image.png](https://oss-club.rt-thread.org/uploads/20220111/7b5262a9a84a34f42ff7f6802c7d73c7.png.webp) ![image.png](https://oss-club.rt-thread.org/uploads/20220111/2b50bfbd7f0077ecde13fd202b1e08ec.png) ![image.png](https://oss-club.rt-thread.org/uploads/20220111/b83e779900c1786bf84e94c49273b48b.png) 移植完fal,使用的片内的2M flash,在命令行里测试 读写擦都是可以了,用代码就出错了。 复位后,用命令行测试是写进去了。 ![image.png](https://oss-club.rt-thread.org/uploads/20220111/47d2e01f110789edc34a7ad823b22891.png) 下面是我移植的fal代码: port.c: ``` #include
#include
#define DBG_TAG "fal_flash_port" #define DBG_LVL DBG_LOG #include
/* base address of the flash sectors */ #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 K bytes */ #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 K bytes */ #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 K bytes */ #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 K bytes */ #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 K bytes */ #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 K bytes */ #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 K bytes */ #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 K bytes */ #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 K bytes */ #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 K bytes */ #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 K bytes */ #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 K bytes */ #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base address of Sector 0, 16 K bytes */ #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base address of Sector 1, 16 K bytes */ #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base address of Sector 2, 16 K bytes */ #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base address of Sector 3, 16 K bytes */ #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base address of Sector 4, 64 K bytes */ #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base address of Sector 5, 128 K bytes */ #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base address of Sector 6, 128 K bytes */ #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base address of Sector 7, 128 K bytes */ #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base address of Sector 8, 128 K bytes */ #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base address of Sector 9, 128 K bytes */ #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base address of Sector 10, 128 K bytes */ #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base address of Sector 11, 128 K bytes */ /** * Get the sector of a given address * * @param address flash address * * @return The sector of a given address */ static uint32_t stm32_get_sector(uint32_t address) { uint32_t 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 < ADDR_FLASH_SECTOR_12) && (address >= ADDR_FLASH_SECTOR_11)){ sector = FLASH_SECTOR_11; } else if ((address < ADDR_FLASH_SECTOR_13) && (address >= ADDR_FLASH_SECTOR_12)) { sector = FLASH_SECTOR_12; } else if ((address < ADDR_FLASH_SECTOR_14) && (address >= ADDR_FLASH_SECTOR_13)) { sector = FLASH_SECTOR_13; } else if ((address < ADDR_FLASH_SECTOR_15) && (address >= ADDR_FLASH_SECTOR_14)) { sector = FLASH_SECTOR_14; } else if ((address < ADDR_FLASH_SECTOR_16) && (address >= ADDR_FLASH_SECTOR_15)) { sector = FLASH_SECTOR_15; } else if ((address < ADDR_FLASH_SECTOR_17) && (address >= ADDR_FLASH_SECTOR_16)) { sector = FLASH_SECTOR_16; } else if ((address < ADDR_FLASH_SECTOR_18) && (address >= ADDR_FLASH_SECTOR_17)) { sector = FLASH_SECTOR_17; } else if ((address < ADDR_FLASH_SECTOR_19) && (address >= ADDR_FLASH_SECTOR_18)) { sector = FLASH_SECTOR_18; } else if ((address < ADDR_FLASH_SECTOR_20) && (address >= ADDR_FLASH_SECTOR_19)) { sector = FLASH_SECTOR_19; } else if ((address < ADDR_FLASH_SECTOR_21) && (address >= ADDR_FLASH_SECTOR_20)) { sector = FLASH_SECTOR_20; }else if ((address < ADDR_FLASH_SECTOR_22) && (address >= ADDR_FLASH_SECTOR_21)) { sector = FLASH_SECTOR_21; } else if ((address < ADDR_FLASH_SECTOR_23) && (address >= ADDR_FLASH_SECTOR_22)) { sector = FLASH_SECTOR_22; }else { sector = FLASH_SECTOR_23; } return sector; } /** * Get the sector size * * @param sector sector * * @return sector size */ static uint32_t stm32_get_sector_size(uint32_t sector) { assert(IS_FLASH_SECTOR(sector)); switch (sector) { case FLASH_SECTOR_0: return 16 * 1024; case FLASH_SECTOR_1: return 16 * 1024; case FLASH_SECTOR_2: return 16 * 1024; case FLASH_SECTOR_3: return 16 * 1024; case FLASH_SECTOR_4: return 64 * 1024; case FLASH_SECTOR_5: return 128 * 1024; case FLASH_SECTOR_6: return 128 * 1024; case FLASH_SECTOR_7: return 128 * 1024; case FLASH_SECTOR_8: return 128 * 1024; case FLASH_SECTOR_9: return 128 * 1024; case FLASH_SECTOR_10: return 128 * 1024; case FLASH_SECTOR_11: return 128 * 1024; case FLASH_SECTOR_12: return 16 * 1024; case FLASH_SECTOR_13: return 16 * 1024; case FLASH_SECTOR_14: return 16 * 1024; case FLASH_SECTOR_15: return 16 * 1024; case FLASH_SECTOR_16: return 64 * 1024; case FLASH_SECTOR_17: return 128 * 1024; case FLASH_SECTOR_18: return 128 * 1024; case FLASH_SECTOR_19: return 128 * 1024; case FLASH_SECTOR_20: return 128 * 1024; case FLASH_SECTOR_21: return 128 * 1024; case FLASH_SECTOR_22: return 128 * 1024; case FLASH_SECTOR_23: return 128 * 1024; default : return 128 * 1024; } } static int init(void) { /* do nothing now */ } static int read(long offset, uint8_t *buf, size_t size) { size_t i; uint32_t addr = stm32f4_onchip_flash.addr + offset; if ((addr + size) > STM32_FLASH_END_ADDRESS) { LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size)); return -1; } for (i = 0; i < size; i++, addr++, buf++) { *buf = *(uint8_t *) addr; } return size; } static int write(long offset, const uint8_t *buf, size_t size) { size_t i; rt_err_t result = RT_EOK; uint32_t read_data; uint32_t addr = stm32f4_onchip_flash.addr + offset; uint32_t end_addr = addr + size; if ((end_addr) > STM32_FLASH_END_ADDRESS) { LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size)); return -RT_EINVAL; } if (size < 1) { return -RT_EINVAL; } HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); 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 (*(rt_uint8_t *)addr != *buf) { result = -RT_ERROR; break; } } else { result = -RT_ERROR; break; } } HAL_FLASH_Lock(); if (result != RT_EOK) { return result; } return size; } static int erase(long offset, size_t size) { uint32_t addr = stm32f4_onchip_flash.addr + offset; rt_err_t result = RT_EOK; rt_uint32_t FirstSector = 0, NbOfSectors = 0; rt_uint32_t SECTORError = 0; if ((addr + size) > STM32_FLASH_END_ADDRESS) { LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size)); return -RT_EINVAL; } /*Variable used for Erase procedure*/ FLASH_EraseInitTypeDef EraseInitStruct; /* Unlock the Flash to enable the flash control register access */ HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); /* Get the 1st sector to erase */ FirstSector = stm32_get_sector(addr); /* Get the number of sector to erase from 1st sector*/ NbOfSectors = stm32_get_sector(addr + size - 1) - FirstSector + 1; /* Fill EraseInit structure*/ EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; EraseInitStruct.Sector = FirstSector; EraseInitStruct.NbSectors = NbOfSectors; if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&SECTORError) != HAL_OK) { result = -RT_ERROR; goto __exit; } __exit: HAL_FLASH_Lock(); if (result != RT_EOK) { return result; } LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size); return size; } static int stm32_flash_init(void) { /* doing nothing now */ } const struct fal_flash_dev stm32f4_onchip_flash = { .name = "stm32f4_onchip", .addr = 0x08000000, // Flash start address .len = 2*1024*1024, //stm32f429bit6 2M Flash .blk_size = 128*1024, //largest sector size //.ops = {stm32_flash_init, stm32_flash_read, stm32_flash_write, stm32_flash_erase}, .ops = {stm32_flash_init, read, write, erase}, .write_gran = 8 }; ``` fal_cfg.h: ``` #ifndef _FAL_CFG_H_ #define _FAL_CFG_H_ #include
#include
/* ===================== Flash device Configuration ========================= */ extern const struct fal_flash_dev stm32f4_onchip_flash; /* flash device table */ #define FAL_FLASH_DEV_TABLE \ { \ &stm32f4_onchip_flash, \ } /* ====================== Partition Configuration ========================== */ #ifdef FAL_PART_HAS_TABLE_CFG /* partition table */ #define FAL_PART_TABLE \ { \ {FAL_PART_MAGIC_WORD, "bl", "stm32f4_onchip", 0, 128*1024, 0}, \ {FAL_PART_MAGIC_WORD, "app1", "stm32f4_onchip", 0x20000, 7*128*1024, 0}, \ {FAL_PART_MAGIC_WORD, "arg", "stm32f4_onchip", 0x100000, 128*1024, 0}, \ {FAL_PART_MAGIC_WORD, "app2", "stm32f4_onchip", 0x120000, 7*128*1024, 0}, \ } #endif /* FAL_PART_HAS_TABLE_CFG */ #endif /* _FAL_CFG_H_ */ ```
查看更多
whj467467222
认证专家
2022-01-11
开源,分享,交流,共同进步
`uint8_t buf[4096]` 导致爆栈了吧。两个解决思路: - 把你执行这个函数的线程栈改大,例如 8192 ; - 把这个数组定义在函数外面。
3
个回答
默认排序
按发布时间排序
flashman2002
2022-01-11
这家伙很懒,什么也没写!
估计是芯片内的flash在清除pages时比较耗时,需要有一点时间延时等待,等清除flash结束后才能写入新数据。
TXH97
2022-04-04
这家伙很懒,什么也没写!
不会把代码段给擦除了吧
撰写答案
登录
注册新账号
关注者
0
被浏览
1.5k
关于作者
ZsjLoveStudy
这家伙很懒,什么也没写!
提问
8
回答
4
被采纳
0
关注TA
发私信
相关问题
1
RTT1.0,STM32调试时会跑到HardFault【已解决】,出现新问题
2
新手请教关于hardfault怎么查
3
github最新版本库中stm32f0X分支,运行切换任务时候出现hardfault[已解决]
4
实现iap功能,bootloader使用了rtt操作系统,在跳转到app代码的时候提示出现hardfault的问题
5
ymodem在on_begin内发送can无法断开连接,而且RTT会报hardfault
6
挂载UFFS文件系统执行到退出_BuildTreeStepOne函数时hardfault异常
7
STM32F1+RTT串口接收中断进入hardfault
8
STM32F746NG随机进入hardfault(已解决)
9
[已解决]自已建了个STM32F103的MDK工程,初始化时总是进入HardFault,求教!
10
将 Cortex M3 的 hardfault 处理与 M4、M7、M0 保持一致?
推荐文章
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组件
最新文章
1
2024 RT-Thread睿赛德开发者大会共探商业共赢的开源生态
2
rt-thread 小内存算法源码分析
3
env中添加lvgl软件包后,keil编译包--c99错误
4
【NXP-MCXA153】 定时器驱动移植
5
GD32F450 看门狗驱动适配
热门标签
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在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
Debug
中断
编译报错
rt_mq_消息队列_msg_queue
SFUD
keil_MDK
msh
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
20
个答案
3
次被采纳
红枫
8
个答案
2
次被采纳
踩姑娘的小蘑菇
7
个答案
2
次被采纳
三世执戟
7
个答案
1
次被采纳
Ryan_CW
5
个答案
1
次被采纳
本月文章贡献
YZRD
3
篇文章
6
次点赞
catcatbing
3
篇文章
6
次点赞
lizimu
2
篇文章
12
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部