STM32L4xx_HAL_Driver  1.14.0
Initialization and de-initialization functions

Initialization and de-initialization functions. More...

Functions

HAL_StatusTypeDef HAL_SD_Init (SD_HandleTypeDef *hsd)
 Initializes the SD according to the specified parameters in the SD_HandleTypeDef and create the associated handle. More...
 
HAL_StatusTypeDef HAL_SD_InitCard (SD_HandleTypeDef *hsd)
 Initializes the SD Card. More...
 
HAL_StatusTypeDef HAL_SD_DeInit (SD_HandleTypeDef *hsd)
 De-Initializes the SD card. More...
 
void HAL_SD_MspInit (SD_HandleTypeDef *hsd)
 Initializes the SD MSP. More...
 
void HAL_SD_MspDeInit (SD_HandleTypeDef *hsd)
 De-Initialize SD MSP. More...
 

Detailed Description

Initialization and de-initialization functions.

  ==============================================================================
          ##### Initialization and de-initialization functions #####
  ==============================================================================
  [..]
    This section provides functions allowing to initialize/de-initialize the SD
    card device to be ready for use.

Function Documentation

◆ HAL_SD_DeInit()

HAL_StatusTypeDef HAL_SD_DeInit ( SD_HandleTypeDef hsd)

De-Initializes the SD card.

Parameters
hsdPointer to SD handle
Return values
HALstatus

Definition at line 554 of file stm32l4xx_hal_sd.c.

555 {
556  /* Check the SD handle allocation */
557  if(hsd == NULL)
558  {
559  return HAL_ERROR;
560  }
561 
562  /* Check the parameters */
563  assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
564 
565  hsd->State = HAL_SD_STATE_BUSY;
566 
567 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
568  /* Desactivate the 1.8V Mode */
569  if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
570  {
571 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
572  if(hsd->DriveTransceiver_1_8V_Callback == NULL)
573  {
574  hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
575  }
576  hsd->DriveTransceiver_1_8V_Callback(RESET);
577 #else
579 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
580  }
581 #endif
582 
583  /* Set SD power state to off */
584  SD_PowerOFF(hsd);
585 
586 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
587  if(hsd->MspDeInitCallback == NULL)
588  {
589  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
590  }
591 
592  /* DeInit the low level hardware */
593  hsd->MspDeInitCallback(hsd);
594 #else
595  /* De-Initialize the MSP layer */
596  HAL_SD_MspDeInit(hsd);
597 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
598 
599  hsd->ErrorCode = HAL_SD_ERROR_NONE;
600  hsd->State = HAL_SD_STATE_RESET;
601 
602  return HAL_OK;
603 }
return HAL_OK
static void SD_PowerOFF(SD_HandleTypeDef *hsd)
Turns the SDMMC output signals off.
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
De-Initialize SD MSP.
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SD_Init()

HAL_StatusTypeDef HAL_SD_Init ( SD_HandleTypeDef hsd)

Initializes the SD according to the specified parameters in the SD_HandleTypeDef and create the associated handle.

Parameters
hsdPointer to the SD handle
Return values
HALstatus

Definition at line 354 of file stm32l4xx_hal_sd.c.

355 {
356 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
357  HAL_SD_CardStatusTypeDef CardStatus;
358  uint32_t speedgrade, unitsize;
359  uint32_t tickstart;
360 #endif
361 
362  /* Check the SD handle allocation */
363  if(hsd == NULL)
364  {
365  return HAL_ERROR;
366  }
367 
368  /* Check the parameters */
369  assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
370  assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
371 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
372  assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
373 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
374  assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
375  assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
376  assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
377  assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
378 
379  if(hsd->State == HAL_SD_STATE_RESET)
380  {
381  /* Allocate lock resource and initialize it */
382  hsd->Lock = HAL_UNLOCKED;
383 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
384  /* Reset Callback pointers in HAL_SD_STATE_RESET only */
385  hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
386  hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
387  hsd->ErrorCallback = HAL_SD_ErrorCallback;
388  hsd->AbortCpltCallback = HAL_SD_AbortCallback;
389 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
390  hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
391  hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
392  hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
393  hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
394  hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
395 #endif
396 
397  if(hsd->MspInitCallback == NULL)
398  {
399  hsd->MspInitCallback = HAL_SD_MspInit;
400  }
401 
402  /* Init the low level hardware */
403  hsd->MspInitCallback(hsd);
404 #else
405  /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
406  HAL_SD_MspInit(hsd);
407 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
408  }
409 
410  hsd->State = HAL_SD_STATE_BUSY;
411 
412  /* Initialize the Card parameters */
413  if (HAL_SD_InitCard(hsd) != HAL_OK)
414  {
415  return HAL_ERROR;
416  }
417 
418 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
419  if( HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK)
420  {
421  return HAL_ERROR;
422  }
423  /* Get Initial Card Speed from Card Status*/
424  speedgrade = CardStatus.UhsSpeedGrade;
425  unitsize = CardStatus.UhsAllocationUnitSize;
426  if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U)))
427  {
428  hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
429  }
430  else
431  {
432  if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
433  {
434  hsd->SdCard.CardSpeed = CARD_HIGH_SPEED;
435  }
436  else
437  {
438  hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED;
439  }
440 
441  }
442  /* Configure the bus wide */
443  if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
444  {
445  return HAL_ERROR;
446  }
447 
448  /* Verify that SD card is ready to use after Initialization */
449  tickstart = HAL_GetTick();
450  while((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
451  {
452  if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT)
453  {
454  hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
455  hsd->State= HAL_SD_STATE_READY;
456  return HAL_TIMEOUT;
457  }
458  }
459 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
460 
461  /* Initialize the error code */
462  hsd->ErrorCode = HAL_SD_ERROR_NONE;
463 
464  /* Initialize the SD operation */
465  hsd->Context = SD_CONTEXT_NONE;
466 
467  /* Initialize the SD state */
468  hsd->State = HAL_SD_STATE_READY;
469 
470  return HAL_OK;
471 }
void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
Initializes the SD MSP.
HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
Gets the SD status info.
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
Enables wide bus operation for the requested card if supported by card.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
Initializes the SD Card.
HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
Gets the current sd card data state.
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Rx Transfer completed callbacks.
__IO uint8_t UhsAllocationUnitSize
void HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 0 Transfer completed callbacks.
void HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 0 Transfer completed callbacks.
void HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 1 Transfer completed callbacks.
return HAL_OK
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
Tx Transfer completed callbacks.
void HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 1 Transfer completed callbacks.
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SD_InitCard()

HAL_StatusTypeDef HAL_SD_InitCard ( SD_HandleTypeDef hsd)

Initializes the SD Card.

Parameters
hsdPointer to SD handle
Note
This function initializes the SD card. It could be used when a card re-initialization is needed.
Return values
HALstatus

Definition at line 480 of file stm32l4xx_hal_sd.c.

481 {
482  uint32_t errorstate;
483  HAL_StatusTypeDef status;
484  SD_InitTypeDef Init;
485 
486  /* Default SDMMC peripheral configuration for SD card initialization */
487  Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
488 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
489  Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
490 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
491  Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
492  Init.BusWide = SDMMC_BUS_WIDE_1B;
493  Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
494  Init.ClockDiv = SDMMC_INIT_CLK_DIV;
495 
496 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
497  if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
498  {
499  /* Set Transceiver polarity */
500  hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
501  }
502 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
503 
504  /* Initialize SDMMC peripheral interface with default configuration */
505  status = SDMMC_Init(hsd->Instance, Init);
506  if(status != HAL_OK)
507  {
508  return HAL_ERROR;
509  }
510 
511 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
512  /* Disable SDMMC Clock */
513  __HAL_SD_DISABLE(hsd);
514 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
515 
516  /* Set Power State to ON */
517  status = SDMMC_PowerState_ON(hsd->Instance);
518  if(status != HAL_OK)
519  {
520  return HAL_ERROR;
521  }
522 
523 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
524  /* Enable SDMMC Clock */
525  __HAL_SD_ENABLE(hsd);
526 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
527 
528  /* Identify card operating voltage */
529  errorstate = SD_PowerON(hsd);
530  if(errorstate != HAL_SD_ERROR_NONE)
531  {
532  hsd->State = HAL_SD_STATE_READY;
533  hsd->ErrorCode |= errorstate;
534  return HAL_ERROR;
535  }
536 
537  /* Card initialization */
538  errorstate = SD_InitCard(hsd);
539  if(errorstate != HAL_SD_ERROR_NONE)
540  {
541  hsd->State = HAL_SD_STATE_READY;
542  hsd->ErrorCode |= errorstate;
543  return HAL_ERROR;
544  }
545 
546  return HAL_OK;
547 }
WWDG_InitTypeDef Init
return HAL_OK
static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
Enquires cards about their operating voltage and configures clock controls and stores SD information ...
HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
Initializes the SDMMC according to the specified parameters in the SDMMC_InitTypeDef and create the a...
HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
Set SDMMC Power state to ON.
static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
Initializes the sd card.

◆ HAL_SD_MspDeInit()

__weak void HAL_SD_MspDeInit ( SD_HandleTypeDef hsd)

De-Initialize SD MSP.

Parameters
hsdPointer to SD handle
Return values
None

Definition at line 626 of file stm32l4xx_hal_sd.c.

627 {
628  /* Prevent unused argument(s) compilation warning */
629  UNUSED(hsd);
630 
631  /* NOTE : This function should not be modified, when the callback is needed,
632  the HAL_SD_MspDeInit could be implemented in the user file
633  */
634 }

◆ HAL_SD_MspInit()

__weak void HAL_SD_MspInit ( SD_HandleTypeDef hsd)

Initializes the SD MSP.

Parameters
hsdPointer to SD handle
Return values
None

Definition at line 611 of file stm32l4xx_hal_sd.c.

612 {
613  /* Prevent unused argument(s) compilation warning */
614  UNUSED(hsd);
615 
616  /* NOTE : This function should not be modified, when the callback is needed,
617  the HAL_SD_MspInit could be implemented in the user file
618  */
619 }