Skip to main content
RRayb
Associate
April 25, 2019
Question

Attempting to read/write to SD card via SDIO on STM32F

  • April 25, 2019
  • 1 reply
  • 2281 views

I'm trying to read and write to a microSD card using this reader from sparkfun https://www.sparkfun.com/products/12941 and a nucleo board with a STM32F429ZIT6U MCU via SDIO interface. I created the project through CubeMX V 4.21 (I used the most current version 5.2 at first and ran into issues then saw some forums that said 4.21 was more stable). SDIO and FatFs middle ware were enabled in CubeMx and well as LED1 and LED2 (PB0 and PB7). I got the program from and followed these series of videos for the main.c code https://www.youtube.com/watch?v=Y5UMTGQDmog and https://www.youtube.com/watch?v=0NbBem8U80Y&t=218s. Here's a snip of main.c

/* USER CODE BEGIN 0 */
//define global variables
FATFS myFATFAS;
FIL myFILE;
UINT testByte;
 
/* USER CODE END 0 */
 
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_FATFS_Init();
 
 /* USER CODE BEGIN 2 */
 if(f_mount(&myFATFAS, SD_Path, 1) == FR_OK){
 	 HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_0); //toggle green LED
 	 char myPath[] = "WRITE2.txt\0"; //creates file
 	 f_open(&myFILE , myPath, FA_WRITE | FA_CREATE_ALWAYS);
 	 char myData[] = "HELLO WORLD\0"; //data that is writen to file
 	 f_write(&myFILE, myData, sizeof(myData), &testByte);
 	 f_close(&myFILE);
 	 HAL_Delay(1000);
 	 HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_0); //toggle green LED
 }
 
 else{
 	HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_7); //toggle blue LED if not mounting
 	HAL_Delay(1000);
 }

When I run my code in True studio f_mount doesn't work. More specifically res = find_volume(&fs, &path, 0) in ff.c line 2511 doesn't return FR_OK. I've attached the VCC and ground wires to 3.3V and ground on my board and DO, SCK,and DI to PC_8, PC_12, and PD_2 respectively. 0690X000008AvtzQAC.pngThe micro SD card is formatted to FAT32. No files are created on my MicroSD card and I've no idea what I'm doing wrong.

This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
April 25, 2019

So going to learn nothing from top-level code.

Used these same adapters wired in 4-bit mode on NUCLEO-144 boards.

They lack pull-up resistors, so going to be more dependent on pin configurations.

Make sure library code is not trying to switch to 4-bit mode.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
RRayb
RRaybAuthor
Associate
April 28, 2019

From stepping through the code for f_mount ,it eventually calls HAL_SD_ReadBlocks in stm32fxx_hal_sd.c and returns HAL_SD_ERROR_ADDR_OUT_OF_RANGE which eventually returns FR_DISK_ERR to f_mount. Here's a snip for that code from stm32fxx_hal_sd.c.

HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
{
 SDIO_DataInitTypeDef config;
 uint32_t errorstate = HAL_SD_ERROR_NONE;
 uint32_t tickstart = HAL_GetTick();
 uint32_t count = 0U, *tempbuff = (uint32_t *)pData;
 
 if(NULL == pData)
 {
 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
 return HAL_ERROR;
 }
 
 if(hsd->State == HAL_SD_STATE_READY)
 {
 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
 
 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
 {
 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
 return HAL_ERROR;
 }

So putting a 10k Ohm pull up resistor on all the pins will help? I'm not sure what you mean by its going to be more dependent on pin configuration. I set the SDIO pins on CubeMX to 1bit mode when generating the code. Where in the library code would it try to switch to 4 bit mode?

Tesla DeLorean
Guru
April 28, 2019

>>I'm not sure what you mean by its going to be more dependent on pin configuration.

Well for starters whether the code selects internal pull-ups on the pins, and the drive speed (slew rate) settings of the pins. Likely buried in the CubeMX MSP code.

>>HAL_SD_ReadBlocks in stm32fxx_hal_sd.c and returns HAL_SD_ERROR_ADDR_OUT_OF_RANGE 

What BlockAdd is being requested? Is it actually within the scope of the media, or a value FatFs has read from another structure containing some bogus data. What value is in SdCard.LogBlockNbr?

>>Where in the library code would it try to switch to 4 bit mode?

Depends on the driver versions and family, I'd look at and around the code calling HAL_SD_Init() and the HAL function itself. Frequently in BSP code.

 uSdHandle.Init.BusWide       = SDMMC_BUS_WIDE_4B;

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..