(本人使用的是stm32f427开发板)
1、使能spi引脚
本人的板子通过spi3与w25q128通信,故使能spi3的引脚(注意spi引脚是否与自己的板子是对应的)。
2、配置spi驱动与文件管理系统
在Kconfig文件中添加如下代码:
config BSP_USING_SPI3
bool "Enable spi3"
default n
然后打开env进行相关配置:
配置好之后,我们可以看到工程中多了不少文件:
修改fal_con.h文件代码如下:
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtconfig.h>
#include <board.h>
#define NOR_FLASH_DEV_NAME "norflash0"
/* ===================== Flash device Configuration ========================= */
//extern const struct fal_flash_dev stm32f2_onchip_flash;
extern struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \
{FAL_PART_MAGIC_WORD, "flash0", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
main.c文件代码实现如下:
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "fal.h"
#include "dfs_fs.h"
#include "drv_spi.h"
#include "spi_flash_sfud.h"
/* defined the LED0 pin: PB1 */
#define LED_Y GET_PIN(A, 1)
#define FS_PARTITION_NAME "flash0"
int main(void)
{
rt_err_t ret = rt_hw_spi_device_attach("spi3", "spi10", GPIOA, GPIO_PIN_15); //spi的cs引脚为PA15
if(ret != RT_EOK)
{
rt_kprintf("rt_hw_spi_device_attach fail");
return -1;
}
if(rt_sfud_flash_probe("norflash0", "spi10") == NULL)
{
rt_kprintf("rt_sfud_flash_probe fail\r\n");
return -1;
}
fal_init();
struct rt_device *flash_dev = (struct rt_device *)fal_blk_device_create(FS_PARTITION_NAME);
if (flash_dev == NULL)
{
rt_kprintf("Can't create a block device on '%s' partition.\n", FS_PARTITION_NAME);
return -1;
}
#if 1
if (dfs_mount(FS_PARTITION_NAME, "/", "elm", 0, 0) == 0)
{
rt_kprintf("Filesystem initialized!\n");
}
else
{
dfs_mkfs("elm", FS_PARTITION_NAME);
if (dfs_mount(FS_PARTITION_NAME, "/", "elm", 0, 0) == 0)
{
rt_kprintf("Filesystem initialized!");
}
else
{
rt_kprintf("Failed to initialize filesystem!");
return -1;
}
}
#endif
/* set LED0 pin mode to output */
rt_pin_mode(LED_Y, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED_Y, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED_Y, PIN_LOW);
rt_thread_mdelay(500);
}
return RT_EOK;
}
编译运行,效果如下:
友情提醒:编译时可能会有bug,大家自行修改,这些bug好像挺简单的!!!
3、配置usb引脚
注意:还要配置usb的时钟为48MHZ!!!
4、配置usb驱动
在Kconfig添加如下代码:
menuconfig BSP_USING_USBD
bool "Eable USB"
select RT_USING_USB_DEVICE
default n
然后打开env进行如下配置:
注意:这里还要去增加usb引脚的初始化,最好再增加usb的复位,避免复位后需要插拔数据线(具体操作可去查看我上篇博文——记录在“rtt实现usb转虚拟串口”中踩的坑)
之后,还要注释掉main.c 文件代码中的一部分代码,将**#if 1改为#if 0**便可(之前添加这里的代码是为了验证能够成功挂载文件系统。因为有时候不一定能够挂载成功的,u盘会出现问题)。
最后,编译运行,用usb将板子和电脑连接起来,在电脑上可以看到多了一个盘——
(若觉得哪里描述不好,或者有误的地方,请大佬们指出!!!)