2024-03-15 12:01 PM
Hi Everyone,
I have been trying to get SDIO working (4-bit) using STM32CubeMX and a NUCLEO-F401RE dev board. I am using the Adafruit SDIO breakout board here: Adafruit Micro SD SPI or SDIO Card Breakout Board . Unfortunately, no matter what I change in the code or in the CubeMX software I can't even get a single clock pulse or blip on the data lines. The output on my logic analyser has absolutely no activity.
I've tried going back to a very basic project and still no luck. My configuration is:
*Note: I have a SDIOCLK clock divide factor of 5 (12MHz) currently on the SDIO config page (I have tried 12 as well to bring the SDIO clock down to 6MHz)
This is the code where I'm hoping to get some kind of activity, instead it just hangs within the HAL_SD_InitCard function.
static void MX_SDIO_SD_Init(void)
{
/* USER CODE BEGIN SDIO_Init 0 */
/* USER CODE END SDIO_Init 0 */
/* USER CODE BEGIN SDIO_Init 1 */
/* USER CODE END SDIO_Init 1 */
hsd.Instance = SDIO;
hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
hsd.Init.ClockDiv = 5;
/* USER CODE BEGIN SDIO_Init 2 */
HAL_StatusTypeDef sd_state = HAL_SD_InitCard(&hsd);
if (sd_state == HAL_OK)
{
HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B);
}
/* USER CODE END SDIO_Init 2 */
}
Has anyone seen this issue before?
Thanks
2024-03-15 01:41 PM
>>Has anyone seen this issue before?
Debugging is mostly about hitting the ball as it lies.. Can something be unique and common at the same time?
Make sure it's getting to this code.
Perhaps instrument it, and the DISKIO code so you can observe.
Make sure it's not breaking out early due to a Card Present GPIO flagging that's there's no card in the slot.
I'd avoid single-stepping, or observing the peripheral registers in a debug-view. Messes with the FIFO in my experience.
Make sure the peripheral, pins and clocks are properly enabled. Check RCC, GPIO, and SDIO. Check MSP code if that's where this is buried. Make sure that code gets called.
2024-03-15 03:52 PM
Thanks for the advice @Tesla DeLorean ,
I have recently found while digging through many of the SDIO related posts on this forum is the RCC configuration might be wrong... I am using the Nucleo board which doesn't have crystal X3 installed. From what I can tell I need to have HSEON enabled as well as the PLL to get SDIO working. However, other peripherals sharing the APB2 peripheral clocks seem to be working fine (I have a SPI TFT LCD that works fine). I know something is clocking for the LCD to work on SPI1 so I would expect something on the SDIO bus). My RCC config is here:
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
I also think SDIO is correctly configured in GPIO:
*No pullups because there are 10k pullups on the CMD and Data lines.
And finally, I think SDIO is also configured alright?
If I step into the code, the errorState comes back non-zero from SDMMC_CmdGoIdleState. Since nothing is written, nothing is sent, which results in an error.
failure point in stm32f4xx_hal_sd.c
If I had to guess I am not initialising something correctly so nothing can be written to or read from these data/cmd/clk lines. SD_DETECT is high when an SD card is inserted, low when removed.
I have tried a second SD card (brand new) and had no additional success.
Thanks for the help!