完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
如题,具体环境是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更新,最新代码没有搞,怎么改,有空发。 |
|
相关推荐
10个回答
|
|
|
|
|
|
如果用DMA方式,现行的驱动是有问题的。
况且所谓的DMA方式也是要等待DMA读写完毕以后函数才返回的,和直接读取没太大的区别。 楼主还是老老实实用普通模式的文件读写吧。 |
|
|
|
|
|
|
|
***7 发表于 2019-1-4 20:22 有,我也知道Cubemx生成肯定是有问题的 |
|
|
|
|
|
|
|
60user61 发表于 2019-1-4 20:31 有没有DMA,SDIO读卡信息都是好的 |
|
|
|
已解决,可以完成读写TXT!CubeMX生成的有问题,SDIO初始化里需要加入HAL SDIO初始化和四线初始化。
|
|
|
|
遇到相同的问题,请你您是怎么解决的? |
|
|
|
库太乱,看的累
|
|
|
|
不帮你了
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
991 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
970 浏览 2 评论
2080 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1177 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 17:29 , Processed in 0.763525 second(s), Total 62, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号