STM32L4xx_HAL_Driver  1.14.0
SD card related functions

Functions

HAL_StatusTypeDef HAL_SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
 
HAL_SD_CardStateTypeDef HAL_SD_GetCardState (SD_HandleTypeDef *hsd)
 Gets the current sd card data state. More...
 
HAL_StatusTypeDef HAL_SD_GetCardCID (SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
 Returns information the information of the card which are stored on the CID register. More...
 
HAL_StatusTypeDef HAL_SD_GetCardCSD (SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
 Returns information the information of the card which are stored on the CSD register. More...
 
HAL_StatusTypeDef HAL_SD_GetCardStatus (SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
 Gets the SD status info. More...
 
HAL_StatusTypeDef HAL_SD_GetCardInfo (SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
 Gets the SD card info. More...
 

Detailed Description

Function Documentation

◆ HAL_SD_GetCardCID()

HAL_StatusTypeDef HAL_SD_GetCardCID ( SD_HandleTypeDef hsd,
HAL_SD_CardCIDTypeDef pCID 
)

Returns information the information of the card which are stored on the CID register.

Parameters
hsdPointer to SD handle
pCIDPointer to a HAL_SD_CardCIDTypeDef structure that contains all CID register parameters
Return values
HALstatus

Definition at line 2428 of file stm32l4xx_hal_sd.c.

2429 {
2430  pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2431 
2432  pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2433 
2434  pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2435 
2436  pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2437 
2438  pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2439 
2440  pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2441 
2442  pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2443 
2444  pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2445 
2446  pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2447 
2448  pCID->Reserved2 = 1U;
2449 
2450  return HAL_OK;
2451 }
return HAL_OK
__IO uint16_t ManufactDate

◆ HAL_SD_GetCardCSD()

HAL_StatusTypeDef HAL_SD_GetCardCSD ( SD_HandleTypeDef hsd,
HAL_SD_CardCSDTypeDef pCSD 
)

Returns information the information of the card which are stored on the CSD register.

Parameters
hsdPointer to SD handle
pCSDPointer to a HAL_SD_CardCSDTypeDef structure that contains all CSD register parameters
Return values
HALstatus

< Reserved

Definition at line 2461 of file stm32l4xx_hal_sd.c.

2462 {
2463  pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2464 
2465  pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2466 
2467  pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2468 
2469  pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2470 
2471  pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2472 
2473  pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2474 
2475  pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2476 
2477  pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2478 
2479  pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2480 
2481  pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2482 
2483  pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2484 
2485  pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2486 
2487  pCSD->Reserved2 = 0U;
2489  if(hsd->SdCard.CardType == CARD_SDSC)
2490  {
2491  pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2492 
2493  pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2494 
2495  pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2496 
2497  pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2498 
2499  pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2500 
2501  pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2502 
2503  hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2504  hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2505  hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2506 
2507  hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2508  hsd->SdCard.LogBlockSize = 512U;
2509  }
2510  else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2511  {
2512  /* Byte 7 */
2513  pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2514 
2515  hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2516  hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2517  hsd->SdCard.BlockSize = 512U;
2518  hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2519  }
2520  else
2521  {
2522  /* Clear all the static flags */
2523  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2524  hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2525  hsd->State = HAL_SD_STATE_READY;
2526  return HAL_ERROR;
2527  }
2528 
2529  pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2530 
2531  pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2532 
2533  pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2534 
2535  pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2536 
2537  pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2538 
2539  pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2540 
2541  pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2542 
2543  pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2544 
2545  pCSD->Reserved3 = 0;
2546 
2547  pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2548 
2549  pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2550 
2551  pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2552 
2553  pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2554 
2555  pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2556 
2557  pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2558 
2559  pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2560 
2561  pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2562 
2563  pCSD->Reserved4 = 1;
2564 
2565  return HAL_OK;
2566 }
__IO uint16_t CardComdClasses
__IO uint8_t MaxRdCurrentVDDMax
__IO uint8_t ContentProtectAppli
__IO uint8_t MaxWrCurrentVDDMax
__IO uint8_t WriteBlockPaPartial
__IO uint8_t WrProtectGrSize
__IO uint8_t MaxRdCurrentVDDMin
return HAL_OK
__IO uint8_t MaxWrCurrentVDDMin
__IO uint8_t FileFormatGroup
__IO uint8_t RdBlockMisalign
__IO uint8_t WrBlockMisalign
__IO uint8_t WrProtectGrEnable

◆ HAL_SD_GetCardInfo()

HAL_StatusTypeDef HAL_SD_GetCardInfo ( SD_HandleTypeDef hsd,
HAL_SD_CardInfoTypeDef pCardInfo 
)

Gets the SD card info.

Parameters
hsdPointer to SD handle
pCardInfoPointer to the HAL_SD_CardInfoTypeDef structure that will contain the SD card status information
Return values
HALstatus

Definition at line 2629 of file stm32l4xx_hal_sd.c.

2630 {
2631  pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2632  pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2633  pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2634  pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2635  pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2636  pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2637  pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2638  pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2639 
2640  return HAL_OK;
2641 }
return HAL_OK

◆ HAL_SD_GetCardState()

HAL_SD_CardStateTypeDef HAL_SD_GetCardState ( SD_HandleTypeDef hsd)

Gets the current sd card data state.

Parameters
hsdpointer to SD handle
Return values
Cardstate

Definition at line 2950 of file stm32l4xx_hal_sd.c.

2951 {
2952  uint32_t cardstate;
2953  uint32_t errorstate;
2954  uint32_t resp1 = 0;
2955 
2956  errorstate = SD_SendStatus(hsd, &resp1);
2957  if(errorstate != HAL_SD_ERROR_NONE)
2958  {
2959  hsd->ErrorCode |= errorstate;
2960  }
2961 
2962  cardstate = ((resp1 >> 9U) & 0x0FU);
2963 
2964  return (HAL_SD_CardStateTypeDef)cardstate;
2965 }
uint32_t HAL_SD_CardStateTypeDef
static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
Returns the current card&#39;s status.

◆ HAL_SD_GetCardStatus()

HAL_StatusTypeDef HAL_SD_GetCardStatus ( SD_HandleTypeDef hsd,
HAL_SD_CardStatusTypeDef pStatus 
)

Gets the SD status info.

Parameters
hsdPointer to SD handle
pStatusPointer to the HAL_SD_CardStatusTypeDef structure that will contain the SD card status information
Return values
HALstatus

Definition at line 2575 of file stm32l4xx_hal_sd.c.

2576 {
2577  uint32_t sd_status[16];
2578  uint32_t errorstate;
2579 
2580  errorstate = SD_SendSDStatus(hsd, sd_status);
2581  if(errorstate != HAL_SD_ERROR_NONE)
2582  {
2583  /* Clear all the static flags */
2584  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2585  hsd->ErrorCode |= errorstate;
2586  hsd->State = HAL_SD_STATE_READY;
2587  return HAL_ERROR;
2588  }
2589  else
2590  {
2591  pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2592 
2593  pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2594 
2595  pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2596 
2597  pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2598  ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2599 
2600  pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2601 
2602  pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2603 
2604  pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2605 
2606  pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2607 
2608  pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2609 
2610  pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2611 
2612 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2613  pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
2614  pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
2615  pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
2616 #endif
2617  }
2618 
2619  return HAL_OK;
2620 }
__IO uint8_t UhsAllocationUnitSize
static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
Send Status info command.
return HAL_OK

◆ HAL_SD_SendSDStatus()

HAL_StatusTypeDef HAL_SD_SendSDStatus ( SD_HandleTypeDef hsd,
uint32_t *  pSDstatus 
)