obsidian-notes/代码/RT-thread/SPI,挂载文件系统.md
CSSC-WORK\murmur 3e6078442b init version
2024-04-15 11:19:57 +08:00

102 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 底层配置
1. 点击工程setting或menuconfig
2. 图形界面中开启**DFS Fatfs SPI SFUD**env中对应
3. 更新最小扇区
W25Q128是16MB spi flash一共有256个block 每个Block 64KB。
每个Block可以分割为16个扇区small sector每个扇区4096字节4KB所以我们需要修改fatfs的扇区大小为4096
4. 按board.h中说明开启宏定义
5. 添加SPI初始化函数到board.c可从cubwMX考取
```c
/**
* @brief SPI MSP Initialization
* This function configures the hardware resources used in this example
* @param hspi: SPI handle pointer
* @retval None
*/
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hspi->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**SPI1 GPIO Configuration
PB3 ------> SPI1_SCK
PB4 ------> SPI1_MISO
PB5 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
}
}
```
5. 其它默认即可
## 程序设置
1. 初始化。在spi1总线上注册spi10设备的函数并将W25Q128抽象为块设备供rtt使用后续挂载文件系统
```c
int rt_hw_spi_flash_init(void)
{
rt_hw_spi_device_attach("spi1", "spi10", GPIOG, GPIO_PIN_8);//片选信号
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
{
rt_kprintf("sfud error.\n");
return -RT_ERROR;
};
else
{
rt_kprintf("sfud success.\n");
return RT_EOK;
}
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
```
2. 挂载
```c
void w25q128_mount(void)
{
//dfs_mkfs("elm", "W25Q128");//格式化
rt_device_t dev;
dev = rt_device_find("W25Q128");
if(dev != RT_NULL) {
if(dfs_mount("W25Q128", "/", "elm", 0, 0) == 0){
rt_kprintf("spi_flash mount to spi!\n");
} else {
rt_kprintf("spi_flash mount to spi failed!\n");
}
}
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(w25q128_mount);
```
3. 格式化
在msh界面手动格式化一次
`mkfs W25Q128`
## msh常用命令
### echo
新建文件
### cat
回读文件