创建基于RTThread的项目工程,根据自己的情况创建,理论上stm32F(x)系列的单片机都是通用的代码; 然后打开spi驱动并通过cubemx配置初始化spi的代码 这里的代码是我用STM32CubeMx生成的,你们可以直接使用下面这段代码修改成自己的SPI端口和SPIx就省去配置了。
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(spiHandle->Instance==SPI2)
{
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
创建app文件
#include <rtthread.h>
#include <board.h>
#include "rtdevice.h"
#include "./sfud/inc/sfud.h"
#include <dfs_posix.h>
#define W25Q_SPI_DEVICE_NAME "spi20"
static int bsp_spi_attach_init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
rt_err_t ret = rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_12);
if(ret < 0)
{
rt_kprintf("flash attach spi2 failed");
return -RT_ERROR;
}
return RT_EOK;
}
static void bsp_spi_w25q_sample(int argc, char *argv[])
{
struct rt_spi_device *spi_dev_w25q;
char name[RT_NAME_MAX];
rt_uint8_t w25x_read_id = 0x90;
rt_uint8_t id[5] = {0};
if (argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
}
spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
if (!spi_dev_w25q)
{
rt_kprintf("spi sample run failed! can't find %s device!\n", name);
}
else
{
rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
rt_kprintf("use rt_qspi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
}
}
static int bsp_spi_block_device_init(void)
{
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi20"))
{
LOG_E("flash sfud failed\n");
return -RT_ERROR;
};
return RT_EOK;
}
static int bsp_spi_flash_mnt_init(void)
{
if(dfs_mount("W25Q128", "/", "elm", 0, 0)==0)
{
LOG_I("dfs mount success\r\n");
return RT_EOK;
}else {
LOG_E("dfs mount failed\r\n");
return -RT_ERROR;
}
}
static void readwrite_sample(void)
{
int fd, size;
char s[] = "write file text!", buffer[80];
fd = open("/updata.bin", O_WRONLY | O_CREAT);
if (fd >= 0)
{
write(fd, s, sizeof(s));
close(fd);
rt_kprintf("Write done.\n");
}
fd = open("/updata.bin", O_RDONLY);
if (fd >= 0)
{
size = read(fd, buffer, sizeof(buffer));
close(fd);
rt_kprintf("read: %s \n", buffer);
if (size < 0)
return ;
}
}
MSH_CMD_EXPORT(readwrite_sample, readwrite sample);
INIT_COMPONENT_EXPORT(bsp_spi_flash_mnt_init);
MSH_CMD_EXPORT(bsp_spi_w25q_sample, spi w25q sample);
INIT_DEVICE_EXPORT(bsp_spi_attach_init);
INIT_DEVICE_EXPORT(bsp_spi_block_device_init);
然后开始编译 打开控制台调试测试 挂载文件系统失败了,检查DFS配置
最后的测试
|