cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up SDIO for the STM32F103ZC, fatfs FR_DISK_ERR error.

JGale
Associate II

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.

8 REPLIES 8

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
JGale
Associate II

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.

GBarb.2
Associate II

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?

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
I was sort of hoping to piggyback off of this issue, since it is similar but maybe I should start a new thread, since I do have some differences. I am running an stm32f107 and the chapter on USB says that it doesn't apply to the 107s. That RM0008 document is 1134 pages, and as much as I appreciate putting some footwork in to get my answers, that's a bit much and sort of leaves me at square one. Truthfully, I am still a student and this is just beyond my comfort zone. And I would not be in a hurry to do this if I wasn't debugging a firmware that I have already put months of work into. And no, I didn't write the core functionality, I started with a source code repository on Github and modified it. It has inherently has this issue since before I laid my hands on it, but as change things the problem becomes more pronounced. Thank you for the info, I will do what I can to process it. ~Gary Barbour -------- Original message --------From: ST Community Date: 7/14/20 8:25 PM (GMT-05:00) To: gwbarbour821@st.nashcc.edu Subject: clive1 (NFA Crew) answered: Setting up SDIO for the STM32F103ZC, fatfs FR_DISK_ERR error. @media only screen and (max-device-width: 480px) { html { -webkit-text-size-adjust: 100%; } table.container { width: 100% !important; } .hidden-for-mobile { display: none !important; } .callToAction, .callToAction td, .footer { font-size: 12px !important; } .button .text { font-size: 12px !important; } .defaultIndentation { width: 10 !important; } .text { font-size: 12px !important; } a.button { width: 96 !important; } a.buttonWide { width: 118 !important; } a.buttonAuto { padding-left:1em !important; padding-right:1em !important;} a.button, a.buttonWide, a.buttonAuto { height: 23 !important; line-height: 23px !important; border-radius: 4px !important; -webkit-border-radius: 4px !important; -moz-border-radius: 4px !important; } td.buttonWrapper { width: 98 !important; height: 23 !important; } img { display: inline !important; } .circle { border-radius: 50%; clip-path: circle(50px at center); } .square { border-radius: 10%; clip-path: circle(10px at center); } } @media only screen and (min-device-width: 600px) { table.container { width: 600px !important; } } @media screen { @font-face { font-family: 'Salesforce Sans'; font-style: normal; src: local('Salesforce Sans'), local('SalesforceSans'), url(https://community.st.com/fonts/SalesforceSans/SalesforceSans-Regular.woff) format('woff'); } } a, a:visited { color: #015BA7; text-decoration:none; } img { display: block; } .preheader { display: none !important; } table { border-collapse: collapse; mso-table-lspace: 0px; mso-table-rspace: 0px; } 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             clive1 (NFA Crew) (Community Member) 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 states1-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   View/Answer   or reply to this email             Replying to   Setting up SDIO for the STM32F103ZC, fatfs FR_DISK_ERR error.       GBarb.2 (Community Member) Tuesday, July 14, 2020 3: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?               clive1 (NFA Crew) (Community Member) 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 states1-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 Tuesday, July 14, 2020 5:25 PM               You're receiving emails when someone "Comments after me."   To change or turn off ST Community email, log in as gwbarbour821@st.nashcc.edu.st.   Are notifications about this post getting annoying? Reply to this email with the word " mute ".   STMicroelectronics N.V.        

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
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;
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Thank you. I found what I believe is the correct code based on that, though it looks like this project was based on a completely different foundation. I found this:/*Note: SDIO_BusWide_1b defined along side _4b and _8b denominators, so it's fair to assume that I'm already set to 1 bit mode */SDIO_InitStruct->SDIO_BusWide = SDIO_BusWide_1b;Honestly I'm not even sure if this has anything to do with the USB drive functionality, but I think this was a rabbit hole. Unless there is a different bus width for USB, but I can't fine anything like that. I'm leaning towards poor configuration, including the register bases and offsets for the USB. The base address was way off and I couldn't tell you what the offset variable names stand for in the stm32 documentation. There has also been articles abot a NAK flag having to be disabled due to a hardware issue. And I can't even locate the USB registers in memory, it's all zeroes so I know that isn't correct. I wish I could port this over to a CUBEMX project so that finding documentation would be practical. I have gotten pretty far with tracing all of the pins and making a template project for "eventually," but the code is so drastically different that it isn't practical to splice the core functionality of the USB into the existing project.This is my first experience with a microprocessor or firmware of any kind, and this week I ended up with an ST Link! I've learned so much in the past few months, so this temporary sense of defeat is disappointing. I wish I could pay someone to help me debug this hot garbage. How much do you think it would cost? Thank you for your help, but I think that this is over my head for now. It's going to take me weeks to learn how to even begin troubleshooting this nightmare. I don't even know how to trace an infinite Loop yet... ~Gary Barbour -------- Original message --------From: ST Community Date: 7/14/20 9:56 PM (GMT-05:00) To: gwbarbour821@st.nashcc.edu Subject: clive1 (NFA Crew) answered: Setting up SDIO for the STM32F103ZC, fatfs FR_DISK_ERR error. @media only screen and (max-device-width: 480px) { html { -webkit-text-size-adjust: 100%; } table.container { width: 100% !important; } .hidden-for-mobile { display: none !important; } .callToAction, .callToAction td, .footer { font-size: 12px !important; } .button .text { font-size: 12px !important; } .defaultIndentation { width: 10 !important; } .text { font-size: 12px !important; } a.button { width: 96 !important; } a.buttonWide { width: 118 !important; } a.buttonAuto { padding-left:1em !important; padding-right:1em !important;} a.button, a.buttonWide, a.buttonAuto { height: 23 !important; line-height: 23px !important; border-radius: 4px !important; -webkit-border-radius: 4px !important; -moz-border-radius: 4px !important; } td.buttonWrapper { width: 98 !important; height: 23 !important; } img { display: inline !important; } .circle { border-radius: 50%; clip-path: circle(50px at center); } .square { border-radius: 10%; clip-path: circle(10px at center); } } @media only screen and (min-device-width: 600px) { table.container { width: 600px !important; } } @media screen { @font-face { font-family: 'Salesforce Sans'; font-style: normal; src: local('Salesforce Sans'), local('SalesforceSans'), url(https://community.st.com/fonts/SalesforceSans/SalesforceSans-Regular.woff) format('woff'); } } a, a:visited { color: #015BA7; text-decoration:none; } img { display: block; } .preheader { display: none !important; } table { border-collapse: collapse; mso-table-lspace: 0px; mso-table-rspace: 0px; } [Start code block] 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; } [End code block]              clive1 (NFA Crew) (Community Member) 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; }    View/Answer   or reply to this email             Replying to   Setting up SDIO for the STM32F103ZC, fatfs FR_DISK_ERR error.       clive1 (NFA Crew) (Community Member) Tuesday, July 14, 2020 5: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 states1-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               clive1 (NFA Crew) (Community Member) 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. Tuesday, July 14, 2020 6:48 PM             clive1 (NFA Crew) (Community Member) 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; }  Tuesday, July 14, 2020 6:55 PM               You're receiving emails when someone "Comments after me."   To change or turn off ST Community email, log in as gwbarbour821@st.nashcc.edu.st.   Are notifications about this post getting annoying? Reply to this email with the word " mute ".   STMicroelectronics N.V.