Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
FAL
SFUD
stdio
RTT stdio FAL SFUD STM32F1、H7详细流程
发布于 2023-04-08 10:02:29 浏览:995
订阅该版
##引言:是否存在drv_flash_xx.c,文章基于STM32F1、STM32H7 大部分开发板在drivers文件夹中没有drv_flash_xx.c,RTT的fal支持drv_flash_xx.c文件的有f1、f4、l4等系列,h7不支持。 ##一、STM32H7-内部flash ![image-20230406103913359.png](https://oss-club.rt-thread.org/uploads/20230408/1d1ef0d1bfe246c5080137a25945e9fc.png.webp) ![screenshot_image-20230406104200200.png](https://oss-club.rt-thread.org/uploads/20230408/603562c34618d76ef9eecf1b93535bb1.png.webp) `fal_flash_stm32f2_port.c` 这个示例是基于STM32F2生成的,可以改文件名 ```c #include
#include
#include "board.h" static int read(long offset, uint8_t *buf, size_t size); static int write(long offset, const uint8_t *buf, size_t size); static int erase(long offset, size_t size);//不用这个 const struct fal_flash_dev stm32h7_onchip_flash = { .name = "stm32_onchip", .addr = ROM_START, .len = ROM_SIZE, .blk_size = 2*1024, .ops = {NULL, read, write, erase}, .write_gran = 1 }; static int erase(long offset, size_t size) { } static int read(long offset, uint8_t *buf, size_t size) { size_t i; uint32_t addr = stm32h7_onchip_flash.addr + offset; if ((addr + size) > ROM_END) { rt_kprintf("ERROR: read outrange flash size! addr is (0x%p)\n", (void*)(addr + size)); return -1; } for (i = 0; i < size; i++, buf++, addr++) { *buf = *(uint8_t *) addr; } return size; } static int write(long offset, const uint8_t *buf, size_t size) { size_t i, j; int8_t status = 0; uint64_t write_data = 0, temp_data = 0; uint32_t addr = stm32h7_onchip_flash.addr + offset; if ((addr + size) > ROM_END) { rt_kprintf("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size)); return -1; } HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGSERR); if (size < 1) { return -1; } for (i = 0; i < size;) { if ((size - i) < 8) { for (j = 0; (size - i) > 0; i++, j++) { temp_data = *buf; write_data = (write_data) | (temp_data << 8*j); buf ++; } } else { for (j = 0; j < 8; j++, i++) { temp_data = *buf; write_data = (write_data) | (temp_data << 8*j); buf ++; } } /* write data FLASH_TYPEPROGRAM_FLASHWORD=0x01U*/ if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, addr, write_data) == HAL_OK) { /* Check the written value */ if (*(uint64_t*)addr != write_data) { rt_kprintf("ERROR: write data != read data\n"); status = -1; goto exit; } } else { status = -1; goto exit; } temp_data = 0; write_data = 0; addr +=8; } exit: HAL_FLASH_Lock(); if (status != 0) { return status; } return size; } ``` `board.h` ![screenshot_image-20230407200003799.png](https://oss-club.rt-thread.org/uploads/20230408/f98294659fb62dd78cfcc183fe3d68df.png) 保留如下设备驱动,参数按实际更改 ```C const struct fal_flash_dev stm32h7_onchip_flash = { .name = "stm32_onchip", .addr = 0x08000000,//起始位置 .len = 128*1024,//总大小 .blk_size = 32*1024,//最小的分块大小 .ops = {init, read, write, erase},//操作 .write_gran = 8//写入的字节长度 }; ``` `fal_cfg.h` ```C #ifndef _FAL_CFG_H_ #define _FAL_CFG_H_ #include
#include
/* ===================== Flash device Configuration ========================= */ extern const struct fal_flash_dev stm32h7_onchip_flash; /* flash device table */ #define FAL_FLASH_DEV_TABLE \ { \ &stm32h7_onchip_flash, \ } /* ====================== Partition Configuration ========================== */ #ifdef FAL_PART_HAS_TABLE_CFG /* partition table */ #define FAL_PART_TABLE \ { \ {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 32*1024, 0}, \ {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 32*1024, 32*1024, 0}, \ {FAL_PART_MAGIC_WORD, "download", "stm32_onchip", 64*1024, 64*1024, 0}, \ } #endif /* FAL_PART_HAS_TABLE_CFG */ #endif /* _FAL_CFG_H_ */ ``` >`分区说明`:如内部128k size,"stm32_onchip"名字,无所谓任意名字 - b1为开始位置“0k”、大小为32k - app为开始位置“32k”、大小为32k、此时两个扇区分区占用了64k - download为开始位置“64k”、大小为64k,到此128k分完 没有用到nor_flash0,也就是没有使能SFUD,这个是外部flash,下节介绍 ![screenshot_image-20230406104646482.png](https://oss-club.rt-thread.org/uploads/20230408/9e0ffafb102974510f13e5d0deeeb400.png) `线程中-如main` ![screenshot_image-20230406104758807.png](https://oss-club.rt-thread.org/uploads/20230408/852b7427e17891594800590012db3b7d.png) ##结果与命令界面 ![screenshot_image-20230407200341209.png](https://oss-club.rt-thread.org/uploads/20230408/2b961eae85ccd57b13402f7f225723a1.png) ![screenshot_image-20230407200503545.png](https://oss-club.rt-thread.org/uploads/20230408/9cf647d126b129c40035a5db8930fdec.png) ![screenshot_image-20230407200530615.png](https://oss-club.rt-thread.org/uploads/20230408/76684ac2e5642e8c1684764dcaff4c14.png) ![screenshot_image-20230407200616308.png](https://oss-club.rt-thread.org/uploads/20230408/66ec999fa43c3c67b28759f9e9f6c4da.png) ##外部flash-内部flash要大于128k-程序文件较大 https://blog.csdn.net/qq_44419932/article/details/119913104 https://bbs.huaweicloud.com/blogs/378738 参考 ##cubemx-SPI1、SPI2 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230408/9f873092373c61d41e67d3a73fccad3c.png.webp) **`HAL_SPI_MspInit` 函数复制到 `board.c` 文件最后面** ```c void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(hspi->Instance==SPI1) { __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**SPI1 GPIO Configuration PA5 ------> SPI1_SCK PA7 ------> SPI1_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(hspi->Instance==SPI2) { __HAL_RCC_SPI2_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**SPI2 GPIO Configuration PB13 ------> SPI2_SCK PB14 ------> SPI2_MISO PB15 ------> SPI2_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_14; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } } void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) { if(hspi->Instance==SPI1) { __HAL_RCC_SPI1_CLK_DISABLE(); /**SPI1 GPIO Configuration PA5 ------> SPI1_SCK PA7 ------> SPI1_MOSI */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7); } else if(hspi->Instance==SPI2) { __HAL_RCC_SPI2_CLK_DISABLE(); /**SPI2 GPIO Configuration PB13 ------> SPI2_SCK PB14 ------> SPI2_MISO PB15 ------> SPI2_MOSI */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15); } } ``` ![screenshot_image-20230407220008364.png](https://oss-club.rt-thread.org/uploads/20230408/c4b54dd181625ecd9fff211249dfaa94.png) ![screenshot_image-20230407202742537.png](https://oss-club.rt-thread.org/uploads/20230408/27c23ac6d872ed186816536b6c337ff5.png.webp) ![screenshot_image-20230407202715093.png](https://oss-club.rt-thread.org/uploads/20230408/6babfe036e4b280add1c6f8c4b0c8a63.png) `board.h` ![screenshot_image-20230407202941568.png](https://oss-club.rt-thread.org/uploads/20230408/d0b5022bd962d2b0d02787641748c25f.png) ![screenshot_image-20230407202950000.png](https://oss-club.rt-thread.org/uploads/20230408/7df08316c0e02b6f7f36826d1696b835.png) `fal_cfg.h` ```C #ifndef _FAL_CFG_H_ #define _FAL_CFG_H_ #include
#include
/* ===================== Flash device Configuration ========================= */ extern const struct fal_flash_dev stm32h7_onchip_flash; extern const struct fal_flash_dev W25Q128BV; /* flash device table */ #define FAL_FLASH_DEV_TABLE \ { \ &stm32h7_onchip_flash, \ &W25Q128BV, \ } /* ====================== Partition Configuration ========================== */ #ifdef FAL_PART_HAS_TABLE_CFG /* partition table */ #define FAL_PART_TABLE \ { \ {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 32*1024, 0}, \ {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 32*1024, 32*1024, 0}, \ {FAL_PART_MAGIC_WORD, "download", "stm32_onchip", 64*1024, 64*1024, 0}, \ {FAL_PART_MAGIC_WORD, "w1", FAL_USING_NOR_FLASH_DEV_NAME, 0*1024, 1024*1024, 0}, \ {FAL_PART_MAGIC_WORD, "w2", FAL_USING_NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \ {FAL_PART_MAGIC_WORD, "w3", FAL_USING_NOR_FLASH_DEV_NAME, 2048*1024, 4096*1024, 0}, \ } #endif /* FAL_PART_HAS_TABLE_CFG */ #endif /* _FAL_CFG_H_ */ ``` `fal_flash_sfud_port.c` ```C #include
#include
#ifdef FAL_USING_SFUD_PORT #ifdef RT_USING_SFUD #include
#endif static int init(void); static int read(long offset, uint8_t *buf, size_t size); static int write(long offset, const uint8_t *buf, size_t size); static int erase(long offset, size_t size); static sfud_flash_t sfud_dev = NULL; const struct fal_flash_dev W25Q128BV = { .name = FAL_USING_NOR_FLASH_DEV_NAME, .addr = 0, .len = 8 * 1024 * 1024, .blk_size = 1024, .ops = {init, read, write, erase}, .write_gran = 1 }; static int init(void) { #ifdef RT_USING_SFUD /* RT-Thread RTOS platform */ sfud_dev = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME); #else /* bare metal platform */ extern sfud_flash sfud_norflash0; sfud_dev = &sfud_norflash0; #endif if (NULL == sfud_dev) { return -1; } /* update the flash chip information */ //nor_flash0.blk_size = sfud_dev->chip.erase_gran; //nor_flash0.len = sfud_dev->chip.capacity; return 0; } static int read(long offset, uint8_t *buf, size_t size) { assert(sfud_dev); assert(sfud_dev->init_ok); sfud_read(sfud_dev, W25Q128BV.addr + offset, size, buf); return size; } static int write(long offset, const uint8_t *buf, size_t size) { assert(sfud_dev); assert(sfud_dev->init_ok); if (sfud_write(sfud_dev, W25Q128BV.addr + offset, size, buf) != SFUD_SUCCESS) { return -1; } return size; } static int erase(long offset, size_t size) { assert(sfud_dev); assert(sfud_dev->init_ok); if (sfud_erase(sfud_dev, W25Q128BV.addr + offset, size) != SFUD_SUCCESS) { return -1; } return size; } #endif /* FAL_USING_SFUD_PORT */ ``` ![screenshot_image-20230407220311171.png](https://oss-club.rt-thread.org/uploads/20230408/cc05c5d54a3f0da10bd3c858d1cba2fd.png.webp) `main.c` ![screenshot_image-20230407220548329.png](https://oss-club.rt-thread.org/uploads/20230408/899c2b535c012f79a19dacd1ee8ac6b2.png.webp) ##错误的结果 ![screenshot_image-20230407214100227.png](https://oss-club.rt-thread.org/uploads/20230408/fb2590c402a7cb4e8a5af26b28148647.png) `这里是名字不对,而且还漏了一个地方` `sfud_cfg.h-可支持多个外部flash` ![screenshot_image-20230407220805161.png](https://oss-club.rt-thread.org/uploads/20230408/eaa353930648ae9182ab1221c87b78f0.png) ![screenshot_image-20230407220920623.png](https://oss-club.rt-thread.org/uploads/20230408/bcf72785d96f90e4b7344346fe2fd6d8.png.webp) ##结果与命令界面 ![screenshot_image-20230407221238287.png](https://oss-club.rt-thread.org/uploads/20230408/bb3ccf33872bfb85381758655efe01d2.png) ##STM32F1-内部flash ![screenshot_image-20230406113448958.png](https://oss-club.rt-thread.org/uploads/20230408/244ea8e00949431dcad27c85022db958.png) ![screenshot_image-20230406113549761.png](https://oss-club.rt-thread.org/uploads/20230408/1782f7176c9b070c976150d33de6b047.png.webp) `board.h` ![screenshot_image-20230406113635803.png](https://oss-club.rt-thread.org/uploads/20230408/b5697d0ecdda47523d4a7dcd5f9569e6.png) ![screenshot_image-20230406113900798.png](https://oss-club.rt-thread.org/uploads/20230408/f5d198414335310639bd377a192c6d0b.png) `fal_cfg.h` ```C #ifndef _FAL_CFG_H_ #define _FAL_CFG_H_ #include
#include
/* ===================== Flash device Configuration ========================= */ extern const struct fal_flash_dev stm32_onchip_flash; /* flash device table */ #define FAL_FLASH_DEV_TABLE \ { \ &stm32_onchip_flash, \ } /* ====================== Partition Configuration ========================== */ #ifdef FAL_PART_HAS_TABLE_CFG /* partition table */ #define FAL_PART_TABLE \ { \ {FAL_PART_MAGIC_WORD, "bl", "onchip_flash", 0, 128*1024, 0}, \ {FAL_PART_MAGIC_WORD, "app", "onchip_flash", 128*1024, 192*1024, 0}, \ {FAL_PART_MAGIC_WORD, "download", "onchip_flash", 320*1024, 180*1024, 0}, \ } #endif /* FAL_PART_HAS_TABLE_CFG */ #endif /* _FAL_CFG_H_ */ ``` `drv_flash_f1.c` ![screenshot_image-20230406113745848.png](https://oss-club.rt-thread.org/uploads/20230408/06d723ddd00e2fa4e4da82aead07a885.png) ![screenshot_image-20230406113814377.png](https://oss-club.rt-thread.org/uploads/20230408/bc9fd0e64327996f3d19d1b3b72336b9.png) `线程中` ![screenshot_image-20230406113955183.png](https://oss-club.rt-thread.org/uploads/20230408/d35dbf9f24341bdf4a3da6d86fd401fa.png) ##外部flash-同理上述-此flash_f1文件仅对内部操作
9
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
xuyuhong
分享
文章
2
回答
1
被采纳
0
关注TA
发私信
相关文章
1
【分享】如何使用 SFUD 库来操作 SPI Flash 设备
2
SFUD怎么使用
3
请教大家一下关于 SFUD 的的问题
4
移植sfud
5
使用SFUD操作FLASH
6
env使用easyflash不是3.2.4版本,而且没有type插件?
7
新增了W25X32设备成功,无法格式化
8
easyflash随着数据存储量增加,耗费时间加长
9
关于SFDP的问题
10
spi nand flash 能使用SFUD驱动吗
推荐文章
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
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
SFUD
msh
rt_mq_消息队列_msg_queue
keil_MDK
ulog
MicroPython
C++_cpp
本月问答贡献
出出啊
1517
个答案
342
次被采纳
小小李sunny
1443
个答案
289
次被采纳
张世争
805
个答案
174
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
4
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
1
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部