只说解决方法不介绍软件环境的就是耍流氓!
软件环境:STM32CubeMX V4.25.0+STM32Cube FW_F4 V1.21.0
软件环境非常重要,检查下自己不是这个版本就不要照着操作了,解决方法仅供参考,如果版本一致,100%问题会解决
简单介绍下遇到的问题
按照网上能搜到的说法,使用CubeMX配置SDIO+FatFS,DMA方式读写SD卡,执行
f_mount(&SDFatFS, (TCHAR const*)SDPath, 1);
f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
这两步都正常,并且可以创建文件,但是到这一步
retSD = f_write(&SDFile, wtext, sizeof(wtext), (void *) & byteswritten);//写入内容
retSD返回值为1,查错误代码为
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
然后就在各种博客各种论坛找各种解决方法,都得不到解决,
插入下之前尝试过哪些操作吧:
怀疑时钟频率太高,分频系数设置为200,失败
怀疑SDIO_BUS_WIDE_4B有问题改为SDIO_BUS_WIDE_1B,失败
怀疑SDIO_IRQn中断与DMA中断冲突,改中断优先级,失败
怀疑DMA_PRIORITY_LOW中断优先级太低,改为DMA_PRIORITY_HIGH,失败
怀疑SD卡格式有问题不支持,各种格式化,FAT,FAT32,各种单元大小组合,失败
怀疑SD卡兼容性不好,更换SD卡,失败
怀疑FatFS设置有问题,设置各种选项测试,失败
怀疑SDIO管脚配置没有开上拉,设置上拉,失败
怀疑SDIO没初始化,再次初始化,失败
踩坑完毕
最后在ST官方的例子中找到解决方法,耗时两天时间终于解决了,如果你看到这篇博客,那么恭喜你,各种坑我都帮你踩过了,你节省了两天时间,不煽情,直接看代码
其它保持默认
代码变更部分
fatfs.c
void MX_FATFS_Init(void)
{
/*## FatFS: Link the SD driver ###########################*/
retSD = FATFS_LinkDriver(&SD_Driver, SDPath);
/* USER CODE BEGIN Init */
/* additional user code for init */
f_mount(&SDFatFS, (TCHAR const*)SDPath, 1);//挂载SD卡
/* USER CODE END Init */
}
重点来了
在bsp_driver_sd.c里增加以下代码
/* USER CODE BEGIN BeforeCallBacksSection */
/* can be used to modify previous code / undefine following code / add code */
/* USER CODE END BeforeCallBacksSection */
/**
* @brief SD Abort callbacks
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_AbortCallback();
}
/**
* @brief Tx Transfer completed callback
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_WriteCpltCallback();
}
/**
* @brief Rx Transfer completed callback
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_ReadCpltCallback();
}
/* USER CODE BEGIN CallBacksSection_C */
/**
* @brief BSP SD Abort callback
* @retval None
*/
__weak void BSP_SD_AbortCallback(void)
{
}
/**
* @brief BSP Tx Transfer completed callback
* @retval None
*/
__weak void BSP_SD_WriteCpltCallback(void)
{
}
/**
* @brief BSP Rx Transfer completed callback
* @retval None
*/
__weak void BSP_SD_ReadCpltCallback(void)
{
}
/* USER CODE END CallBacksSection_C */
在bsp_driver_sd.h里增加以下代码
/* These functions can be modified in case the current settings (e.g. DMA stream)
need to be changed for specific application needs */
void BSP_SD_AbortCallback(void);
void BSP_SD_WriteCpltCallback(void);
void BSP_SD_ReadCpltCallback(void);
/* USER CODE END BSP_H_CODE */
FreeRTOS.c
增加全局变量
uint32_t byteswritten; /* File write counts */
uint32_t bytesread; /* File read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffers */
测试代码
if (f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)//创建一个文件
{
retSD = f_write(&SDFile, wtext, sizeof(wtext), (void *) & byteswritten);//写入内容
if ((byteswritten == 0) || (retSD != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
else
{
f_close(&SDFile); //关闭文件
/*##-7- Open the text file object with read access ###############*/
if (f_open(&SDFile, "STM32.TXT", FA_READ) == FR_OK) //再次打开文件
{
retSD = f_read(&SDFile, rtext, sizeof(rtext), (UINT*) & bytesread);//读出内容
if ((bytesread == 0) || (retSD != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
}
else
{
f_close(&SDFile); //关闭文件
/*##-10- Compare read data with the expected data ############*/
if ((bytesread != byteswritten))//比较
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
LED3_WRITE(LED_ON); //灯亮
}
}
}
}
}
完成
版权声明:注明作者,随意转载。 by [email protected]