本帖最后由 lebment 于 2018-7-12 20:59 编辑 如题,具体环境是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 + ((uint32_t)buff - alignedAddr)); #endif break; } } } } return res; } 由于MX更新,最新代码没有搞,怎么改,有空发。 |
评分
查看全部评分
况且所谓的DMA方式也是要等待DMA读写完毕以后函数才返回的,和直接读取没太大的区别。
楼主还是老老实实用普通模式的文件读写吧。
评分
查看全部评分
然后才能使用FatFS函数
评分
查看全部评分
有,我也知道Cubemx生成肯定是有问题的
SDIO早就初始化了,自动生成的
有没有DMA,SDIO读卡信息都是好的
点评
点评
评分
查看全部评分
遇到相同的问题,请你您是怎么解决的?