stm32f_open失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stm32f_open失败相关的知识,希望对你有一定的参考价值。
STM32用CubeMX创建SDIO+FatFs,f_Open失败如题,具体环境是CubeMX最新版,HAL库最新版,MDK5.24a,STLINKv2-1,板子是STM32F407Vet6核心板(某宝四五十块钱)。
SDIO单独测试TF卡(4G卡肯定不是正版)成功,可以读出CSD,CID,卡的状态,卡的容量等,SDIO四线无DMA读写正常。
本人前前后后试过无数次,好几个月,现在不得不弄好!感谢大佬的帮助!
CubeMX:SDIO四线,无DMA,无SDIO全局中断,勾选FatFS文件系统,文件系统加入长名STACK,单片机HEAP-0x800,STACK-0x1000
具体代码:
主程序:
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
FRESULT res; /* FatFs function common retSDult code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* USER CODE END PV */
int main(void)
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_USART2_UART_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
****************************************************
从HAL库中F4Disco里抄来的代码
****************************************************
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
/* FatFs Initialization Error */
Error_Handler();
else
/* Create and Open a new text file object with write access */
if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
else
/* Write data to the text file */
res = f_write(&SDFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (retSD != FR_OK))
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
else
/* Close the open text file */
f_close(&SDFile);
/* Open the text file object with read access */
if(f_open(&SDFile, "STM32.TXT", FA_READ) != FR_OK)
/* 'STM32.TXT' file Open for read Error */
Error_Handler();
else
/* Read data from the text file */
res = f_read(&SDFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (retSD != FR_OK))
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
else
/* Close the open text file */
f_close(&SDFile);
/* 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 */
HAL_GPIO_WritePin(GPIOA, D2_Pin|D3_Pin, GPIO_PIN_SET);
/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(SDPath);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
****************************************************
单步调试结果 Sd_diskio.c中死循环
****************************************************
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
DRESULT res = RES_ERROR;
ReadStatus = 0;
uint32_t timeout;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
uint32_t alignedAddr;
#endif
if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,
(uint32_t) (sector),
count) == MSD_OK)
/* Wait that the reading process is completed or a timeout occurs */
timeout = HAL_GetTick();
while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))//在此处无限循环
/* incase of a timeout return error */
if (ReadStatus == 0)
res = RES_ERROR;
else
ReadStatus = 0;
timeout = HAL_GetTick();
while((HAL_GetTick() - timeout) < SD_TIMEOUT)
if (BSP_SD_GetCardState() == SD_TRANSFER_OK)
res = RES_OK;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
/*
the SCB_InvalidateDCache_by_Addr() requires a 32-Byte aligned address,
adjust the address and the D-Cache size to invalidate accordingly.
*/
alignedAddr = (uint32_t)buff & ~0x1F;
SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint3 参考技术A 下面两个原因。
stm32f_open失败,有两个可能的原因:
1,相对路径错了;
2,文件名命名的有问题,比如打开fopen("1.txt","r");在相对路径下创建文本文件的的时候,将该文件命名为1 就可以了,不要加后缀.txt 。
如果是字符转义,肯定就是无法打开一个不存在的文件,只要通过获取错误码就可以解决这个问题。
以上是关于stm32f_open失败的主要内容,如果未能解决你的问题,请参考以下文章
STM32F0,ST-link v2,OpenOCD 0.9.0:打开失败
stm32f427 swd烧写程序的时候出现擦出flash失败是啥原因