2019-08-22 06:46 AM
I'm trying to transfer my Arduino code to the ST HAL library. I've got everything implemented except for the SD card. After some research I set up FatFS using the SD 4-bit bus. Using PA15 as the detect SDIO pin, which I've both tried to set as a pull_up, pull_down and neither.
Now I'm trying to use the example code to get a sense of how to implement my own code, but I keep getting a FR_DISK_ERR when trying to open anything.
When debugging the f_mount returns that mounting of the card was done successfully (it returns FR_OK), here is the code for that:
#include "fatfs.h"
uint8_t retSD; /* Return value for SD */
char SDPath[4]; /* SD logical drive path */
FATFS SDFatFS; /* File system object for SD logical drive */
FIL SDFile; /* File object for SD */
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
void MX_FATFS_Init(void)
{
/*## FatFS: Link the SD driver ###########################*/
retSD = FATFS_LinkDriver(&SD_Driver, SDPath);
/* USER CODE BEGIN Init */
FRESULT res = f_mount(&SDFatFS, (TCHAR const *) SDPath, 0 );
if(res != FR_OK){
Error_Handler();
}
/* additional user code for init */
/* USER CODE END Init */
}
Then when trying to open directories, it returns a disk error:
MX_GPIO_Init();
MX_I2C2_Init();
MX_SDIO_SD_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_USART1_UART_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
LED DASH_LED_1(DASH_LED_1_GPIO_Port, DASH_LED_1_Pin);
LED DASH_LED_2(DASH_LED_2_GPIO_Port, DASH_LED_2_Pin);
LED DASH_LED_3(DASH_LED_3_GPIO_Port, DASH_LED_3_Pin);
LED DASH_LED_4(DASH_LED_4_GPIO_Port, DASH_LED_4_Pin);
//FATFS FatFs;
//File object
FIL fil;
DIR directory;
FRESULT res =f_opendir(&directory, (const TCHAR *) "0:SD-CARD");
if(res == FR_OK){
DASH_LED_1.turnOn();
if(f_open(&fil, (const TCHAR *)"Test.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK){
DASH_LED_2.turnOn();
if(f_putc('S', &fil) == FR_OK){
DASH_LED_3.turnOn();
}
}
}else if(res == FR_DISK_ERR){
DASH_LED_3.turnOn();
}
I've read some threads on that CubeMX (which I used to initialize a lot of stuff) does not do a good job with SDIO, but I couldn't find out if there were certain functions that I needed to change or overwrite to get it to work.
The cubeMX also created seven other files in my include/source folder that might be useful, I've added them as attachments.
I hope that I gave enough information on my problem, feel free to ask for more files if needed.
2019-08-22 10:19 AM
Some custom hardware arrangement?
Either the socket or STM32 would want PULL-UP resistors on the DATA and CMD lines. Not on the socket wiring, enable on the STM32 side, or vice versa.
FR_DISK_ERR indicates a failure at the DISKIO layer, and the routines talking to SDIO and Card. Suggests the hardware's not wired/functioning correctly.
You could instrument the SDIO code, you could try porting the SDIO code from one of the EVAL board HAL examples. Personally don't trust CubeMX to generate viable code.
Try using 1-bit mode, rather than 4-bit, see if that works.
2019-08-23 04:12 AM
Thanks for the quick answer, I've changed the mode from 4 bit to 1 bit and most of the things seems to work. Thanks so much.
2020-07-14 03:18 PM
Hello! I'm somewhat new to all this and I am having a similar issue. Could one of you be a bit more clear on what mode is being discussed, and maybe where to find it?
2020-07-14 05:25 PM
Review the Reference Manual Chapter on the SDIO peripheral, RM0008 for the STM32F1 series.
Instrument code so you can understand sequence and failure mode.
Normally it transitions though various states
1-bit 400 KHz (initialization)
1-bit 12 MHz (perhaps 18 or 24 MHz, depends on divider settings)
4-bit 12 MHz
So in cases where the high speed 4-bit mode isn't working you stop at the high speed 1-bit mode.
Usually circuitry and wiring issues
2020-07-14 06:13 PM
2020-07-14 06:48 PM
Just asking you to review the SDIO chapter and walk your code so you can see where it switches speeds/modes.
Using a really old part, so probably not of many to work on your issue. Not looking to spend hours on it either.
SDIO definitely works on the STM3210C-EVAL board, check the example apps for that, and diff/merge.
As likely to be a wiring/characteristics issue as a software one.
2020-07-14 06:55 PM
uint8_t BSP_SD_Init(void)
{
uint8_t sd_state = MSD_OK;
/* Check if the SD card is plugged in the slot */
if (BSP_SD_IsDetected() != SD_PRESENT)
{
return MSD_ERROR;
}
/* HAL SD initialization */
sd_state = HAL_SD_Init(&hsd);
/* Configure SD Bus width (4 bits mode selected) SO DON'T DO THIS LAST SECTION IF 4-BIT IS NOT VIABLE */
if (sd_state == MSD_OK)
{
/* Enable wide operation */
if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
{
sd_state = MSD_ERROR;
}
}
return sd_state;
}
2020-07-14 09:27 PM