在上次实验SDIO+FATFS的基础上,再加外扩的SRAM一个,同时挂载到FATFS。
SRAM型号是IS61LV25616,地址线19位,数据线16位,连接在FSMC总线上。
Pinout配置如下
FSMC总线时序,使能写操作,extended mode(读写使用不同的时序)关闭,配置如下:
代码生成后需做一些调整:
1. 变量定义。
Fatfs.c
FATFS MYSD_FATFS;
FATFS MYSRAM_FATFS;
Mian.c
uint8_t fserrors;
UINT bw;
uint8_t rbuffer[30] = {0};
FIL fileb;
2. 初始化函数顺序调整,将FSMC初始化函数调至FATFS初始化之前(否则调用函数f_mount时会返回错误FR_NOT_ENABLED)。
MX_GPIO_Init();
MX_CRC_Init();
MX_FSMC_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
//MX_FSMC_Init();//原位置
3. FATFS初始化函数中添加挂载函数调用。
void MX_FATFS_Init(void)
{
/*## FatFS: Link the SRAMDISK driver ###########################*/
retSRAMDISK = FATFS_LinkDriver(&SRAMDISK_Driver, SRAMDISK_Path);
/*## FatFS: Link the SD driver ###########################*/
retSD = FATFS_LinkDriver(&SD_Driver, SD_Path);
/* USER CODE BEGIN Init */
/* additional user code for init */
if(!retSRAMDISK)
{
retSRAMDISK = f_mount(&MYSRAM_FATFS, SRAMDISK_Path, 0);
printf("f_mount retSRAMDISK = %d\n", retSRAMDISK);
}
if(!retSD)
{
retSD = f_mount(&MYSD_FATFS, SD_Path, 0);
printf("f_mount retSD = %d\n", retSD);
}
/* USER CODE END Init */
}
4. 主循环while如下
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(200);
fserrors = f_mkfs(SRAMDISK_Path,0,0);
printf("mkfs error = %d\n", fserrors);
HAL_Delay(200);
fserrors = f_open(&fileb, "test1.txt", FA_WRITE | FA_CREATE_ALWAYS);
printf("open error = %d\n", fserrors);
HAL_Delay(200);
fserrors = f_write(&fileb, "123456789", 10, &bw);
printf("write error = %d\n", fserrors);
HAL_Delay(200);
fserrors = f_close ( &fileb );
HAL_Delay(200);
fserrors = f_open(&fileb, "test1.txt", FA_READ);
HAL_Delay(200);
fserrors = f_read(&fileb, rbuffer, 10, &bw);
printf("read error = %d\n", fserrors);
HAL_Delay(200);
fserrors = f_close ( &fileb );
HAL_Delay(200);
while(1);
}
5. 编译并下载仿真调试。
|