102 lines
2.5 KiB
Markdown
102 lines
2.5 KiB
Markdown
|
||
## 底层配置
|
||
|
||
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
|
||
回读文件
|