STM32L4xx_HAL_Driver  1.14.0

Functions

static uint32_t SD_InitCard (SD_HandleTypeDef *hsd)
 Initializes the sd card. More...
 
static uint32_t SD_PowerON (SD_HandleTypeDef *hsd)
 Enquires cards about their operating voltage and configures clock controls and stores SD information that will be needed in future in the SD handle. More...
 
static uint32_t SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
 Send Status info command. More...
 
static uint32_t SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
 Returns the current card's status. More...
 
static uint32_t SD_WideBus_Enable (SD_HandleTypeDef *hsd)
 Enables the SDMMC wide bus mode. More...
 
static uint32_t SD_WideBus_Disable (SD_HandleTypeDef *hsd)
 Disables the SDMMC wide bus mode. More...
 
static uint32_t SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR)
 Finds the SD card SCR register value. More...
 
static void SD_PowerOFF (SD_HandleTypeDef *hsd)
 Turns the SDMMC output signals off. More...
 
static void SD_Write_IT (SD_HandleTypeDef *hsd)
 Wrap up writing in non-blocking mode. More...
 
static void SD_Read_IT (SD_HandleTypeDef *hsd)
 Wrap up reading in non-blocking mode. More...
 
static void SD_DMATransmitCplt (DMA_HandleTypeDef *hdma)
 DMA SD transmit process complete callback. More...
 
static void SD_DMAReceiveCplt (DMA_HandleTypeDef *hdma)
 DMA SD receive process complete callback. More...
 
static void SD_DMAError (DMA_HandleTypeDef *hdma)
 DMA SD communication error callback. More...
 
static void SD_DMATxAbort (DMA_HandleTypeDef *hdma)
 DMA SD Tx Abort callback. More...
 
static void SD_DMARxAbort (DMA_HandleTypeDef *hdma)
 DMA SD Rx Abort callback. More...
 
uint32_t SD_HighSpeed (SD_HandleTypeDef *hsd)
 Switches the SD card to High Speed mode. This API must be used after "Transfer State". More...
 
static uint32_t SD_UltraHighSpeed (SD_HandleTypeDef *hsd)
 Switches the SD card to Ultra High Speed mode. This API must be used after "Transfer State". More...
 
static uint32_t SD_DDR_Mode (SD_HandleTypeDef *hsd)
 Switches the SD card to Double Data Rate (DDR) mode. This API must be used after "Transfer State". More...
 
 while (((sta_reg &(SDMMC_FLAG_CCRCFAIL|SDMMC_FLAG_CMDREND|SDMMC_FLAG_CTIMEOUT))==0U)||((sta_reg &SDMMC_FLAG_CMDACT) !=0U))
 
 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
 
else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
 
 if (SDMMC_GetCommandResponse(SDMMCx) !=SD_CMD)
 
 if ((response_r1 &SDMMC_OCR_ERRORBITS)==SDMMC_ALLZERO)
 
else if ((response_r1 &SDMMC_OCR_ADDR_OUT_OF_RANGE)==SDMMC_OCR_ADDR_OUT_OF_RANGE)
 
else if ((response_r1 &SDMMC_OCR_ADDR_MISALIGNED)==SDMMC_OCR_ADDR_MISALIGNED)
 
else if ((response_r1 &SDMMC_OCR_BLOCK_LEN_ERR)==SDMMC_OCR_BLOCK_LEN_ERR)
 
else if ((response_r1 &SDMMC_OCR_ERASE_SEQ_ERR)==SDMMC_OCR_ERASE_SEQ_ERR)
 
else if ((response_r1 &SDMMC_OCR_BAD_ERASE_PARAM)==SDMMC_OCR_BAD_ERASE_PARAM)
 
else if ((response_r1 &SDMMC_OCR_WRITE_PROT_VIOLATION)==SDMMC_OCR_WRITE_PROT_VIOLATION)
 
else if ((response_r1 &SDMMC_OCR_LOCK_UNLOCK_FAILED)==SDMMC_OCR_LOCK_UNLOCK_FAILED)
 
else if ((response_r1 &SDMMC_OCR_COM_CRC_FAILED)==SDMMC_OCR_COM_CRC_FAILED)
 
else if ((response_r1 &SDMMC_OCR_ILLEGAL_CMD)==SDMMC_OCR_ILLEGAL_CMD)
 
else if ((response_r1 &SDMMC_OCR_CARD_ECC_FAILED)==SDMMC_OCR_CARD_ECC_FAILED)
 
else if ((response_r1 &SDMMC_OCR_CC_ERROR)==SDMMC_OCR_CC_ERROR)
 
else if ((response_r1 &SDMMC_OCR_STREAM_READ_UNDERRUN)==SDMMC_OCR_STREAM_READ_UNDERRUN)
 
else if ((response_r1 &SDMMC_OCR_STREAM_WRITE_OVERRUN)==SDMMC_OCR_STREAM_WRITE_OVERRUN)
 
else if ((response_r1 &SDMMC_OCR_CID_CSD_OVERWRITE)==SDMMC_OCR_CID_CSD_OVERWRITE)
 
else if ((response_r1 &SDMMC_OCR_WP_ERASE_SKIP)==SDMMC_OCR_WP_ERASE_SKIP)
 
else if ((response_r1 &SDMMC_OCR_CARD_ECC_DISABLED)==SDMMC_OCR_CARD_ECC_DISABLED)
 
else if ((response_r1 &SDMMC_OCR_ERASE_RESET)==SDMMC_OCR_ERASE_RESET)
 
else if ((response_r1 &SDMMC_OCR_AKE_SEQ_ERROR)==SDMMC_OCR_AKE_SEQ_ERROR)
 

Variables

 else
 
 response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1)
 

Detailed Description

Function Documentation

◆ if() [1/22]

if ( __SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT)  )

Definition at line 1292 of file stm32l4xx_ll_sdmmc.c.

1293  {
1294  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1295 
1296  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1297  }

◆ if() [2/22]

else if ( __SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL)  )

Definition at line 1298 of file stm32l4xx_ll_sdmmc.c.

1299  {
1300  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1301 
1302  return SDMMC_ERROR_CMD_CRC_FAIL;
1303  }

◆ if() [3/22]

if ( SDMMC_GetCommandResponse(SDMMCx) !  = SD_CMD)

Definition at line 1313 of file stm32l4xx_ll_sdmmc.c.

1314  {
1315  return SDMMC_ERROR_CMD_CRC_FAIL;
1316  }

◆ if() [4/22]

if ( (response_r1 &SDMMC_OCR_ERRORBITS)  = = SDMMC_ALLZERO)

Definition at line 1321 of file stm32l4xx_ll_sdmmc.c.

1322  {
1323  return SDMMC_ERROR_NONE;
1324  }

◆ if() [5/22]

else if ( (response_r1 &SDMMC_OCR_ADDR_OUT_OF_RANGE)  = = SDMMC_OCR_ADDR_OUT_OF_RANGE)

Definition at line 1325 of file stm32l4xx_ll_sdmmc.c.

1326  {
1327  return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
1328  }

◆ if() [6/22]

else if ( (response_r1 &SDMMC_OCR_ADDR_MISALIGNED)  = = SDMMC_OCR_ADDR_MISALIGNED)

Definition at line 1329 of file stm32l4xx_ll_sdmmc.c.

1330  {
1331  return SDMMC_ERROR_ADDR_MISALIGNED;
1332  }

◆ if() [7/22]

else if ( (response_r1 &SDMMC_OCR_BLOCK_LEN_ERR)  = = SDMMC_OCR_BLOCK_LEN_ERR)

Definition at line 1333 of file stm32l4xx_ll_sdmmc.c.

1334  {
1335  return SDMMC_ERROR_BLOCK_LEN_ERR;
1336  }

◆ if() [8/22]

else if ( (response_r1 &SDMMC_OCR_ERASE_SEQ_ERR)  = = SDMMC_OCR_ERASE_SEQ_ERR)

Definition at line 1337 of file stm32l4xx_ll_sdmmc.c.

1338  {
1339  return SDMMC_ERROR_ERASE_SEQ_ERR;
1340  }

◆ if() [9/22]

else if ( (response_r1 &SDMMC_OCR_BAD_ERASE_PARAM)  = = SDMMC_OCR_BAD_ERASE_PARAM)

Definition at line 1341 of file stm32l4xx_ll_sdmmc.c.

1342  {
1343  return SDMMC_ERROR_BAD_ERASE_PARAM;
1344  }

◆ if() [10/22]

else if ( (response_r1 &SDMMC_OCR_WRITE_PROT_VIOLATION)  = = SDMMC_OCR_WRITE_PROT_VIOLATION)

Definition at line 1345 of file stm32l4xx_ll_sdmmc.c.

1346  {
1347  return SDMMC_ERROR_WRITE_PROT_VIOLATION;
1348  }

◆ if() [11/22]

else if ( (response_r1 &SDMMC_OCR_LOCK_UNLOCK_FAILED)  = = SDMMC_OCR_LOCK_UNLOCK_FAILED)

Definition at line 1349 of file stm32l4xx_ll_sdmmc.c.

1350  {
1351  return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
1352  }

◆ if() [12/22]

else if ( (response_r1 &SDMMC_OCR_COM_CRC_FAILED)  = = SDMMC_OCR_COM_CRC_FAILED)

Definition at line 1353 of file stm32l4xx_ll_sdmmc.c.

1354  {
1355  return SDMMC_ERROR_COM_CRC_FAILED;
1356  }

◆ if() [13/22]

else if ( (response_r1 &SDMMC_OCR_ILLEGAL_CMD)  = = SDMMC_OCR_ILLEGAL_CMD)

Definition at line 1357 of file stm32l4xx_ll_sdmmc.c.

1358  {
1359  return SDMMC_ERROR_ILLEGAL_CMD;
1360  }

◆ if() [14/22]

else if ( (response_r1 &SDMMC_OCR_CARD_ECC_FAILED)  = = SDMMC_OCR_CARD_ECC_FAILED)

Definition at line 1361 of file stm32l4xx_ll_sdmmc.c.

1362  {
1363  return SDMMC_ERROR_CARD_ECC_FAILED;
1364  }

◆ if() [15/22]

else if ( (response_r1 &SDMMC_OCR_CC_ERROR)  = = SDMMC_OCR_CC_ERROR)

Definition at line 1365 of file stm32l4xx_ll_sdmmc.c.

1366  {
1367  return SDMMC_ERROR_CC_ERR;
1368  }

◆ if() [16/22]

else if ( (response_r1 &SDMMC_OCR_STREAM_READ_UNDERRUN)  = = SDMMC_OCR_STREAM_READ_UNDERRUN)

Definition at line 1369 of file stm32l4xx_ll_sdmmc.c.

1370  {
1371  return SDMMC_ERROR_STREAM_READ_UNDERRUN;
1372  }

◆ if() [17/22]

else if ( (response_r1 &SDMMC_OCR_STREAM_WRITE_OVERRUN)  = = SDMMC_OCR_STREAM_WRITE_OVERRUN)

Definition at line 1373 of file stm32l4xx_ll_sdmmc.c.

1374  {
1375  return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
1376  }

◆ if() [18/22]

else if ( (response_r1 &SDMMC_OCR_CID_CSD_OVERWRITE)  = = SDMMC_OCR_CID_CSD_OVERWRITE)

Definition at line 1377 of file stm32l4xx_ll_sdmmc.c.

1378  {
1379  return SDMMC_ERROR_CID_CSD_OVERWRITE;
1380  }

◆ if() [19/22]

else if ( (response_r1 &SDMMC_OCR_WP_ERASE_SKIP)  = = SDMMC_OCR_WP_ERASE_SKIP)

Definition at line 1381 of file stm32l4xx_ll_sdmmc.c.

1382  {
1383  return SDMMC_ERROR_WP_ERASE_SKIP;
1384  }

◆ if() [20/22]

else if ( (response_r1 &SDMMC_OCR_CARD_ECC_DISABLED)  = = SDMMC_OCR_CARD_ECC_DISABLED)

Definition at line 1385 of file stm32l4xx_ll_sdmmc.c.

1386  {
1387  return SDMMC_ERROR_CARD_ECC_DISABLED;
1388  }

◆ if() [21/22]

else if ( (response_r1 &SDMMC_OCR_ERASE_RESET)  = = SDMMC_OCR_ERASE_RESET)

Definition at line 1389 of file stm32l4xx_ll_sdmmc.c.

1390  {
1391  return SDMMC_ERROR_ERASE_RESET;
1392  }

◆ if() [22/22]

else if ( (response_r1 &SDMMC_OCR_AKE_SEQ_ERROR)  = = SDMMC_OCR_AKE_SEQ_ERROR)

Definition at line 1393 of file stm32l4xx_ll_sdmmc.c.

1394  {
1395  return SDMMC_ERROR_AKE_SEQ_ERR;
1396  }

◆ SD_DDR_Mode()

static uint32_t SD_DDR_Mode ( SD_HandleTypeDef hsd)
static

Switches the SD card to Double Data Rate (DDR) mode. This API must be used after "Transfer State".

Note
This operation should be followed by the configuration of PLL to have SDMMCCK clock less than 50MHz
Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 4290 of file stm32l4xx_hal_sd.c.

4291 {
4292  uint32_t errorstate = HAL_SD_ERROR_NONE;
4293  SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4294  uint32_t SD_hs[16] = {0};
4295  uint32_t count, loop = 0 ;
4296  uint32_t Timeout = HAL_GetTick();
4297 
4298  if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4299  {
4300  /* Standard Speed Card <= 12.5Mhz */
4301  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4302  }
4303 
4304  if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
4305  (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
4306  {
4307  /* Initialize the Data control register */
4308  hsd->Instance->DCTRL = 0;
4309  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
4310 
4311  if (errorstate != HAL_SD_ERROR_NONE)
4312  {
4313  return errorstate;
4314  }
4315 
4316  /* Configure the SD DPSM (Data Path State Machine) */
4317  sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4318  sdmmc_datainitstructure.DataLength = 64;
4319  sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4320  sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4321  sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4322  sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4323 
4324  if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
4325  {
4326  return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4327  }
4328 
4329  errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
4330  if(errorstate != HAL_SD_ERROR_NONE)
4331  {
4332  return errorstate;
4333  }
4334 
4335  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4336  {
4337  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4338  {
4339  for (count = 0U; count < 8U; count++)
4340  {
4341  SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4342  }
4343  loop ++;
4344  }
4345 
4346  if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4347  {
4348  hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4349  hsd->State= HAL_SD_STATE_READY;
4350  return HAL_SD_ERROR_TIMEOUT;
4351  }
4352  }
4353 
4354  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4355  {
4356  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4357 
4358  errorstate = 0;
4359 
4360  return errorstate;
4361  }
4362  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4363  {
4364  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4365 
4366  errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4367 
4368  return errorstate;
4369  }
4370  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4371  {
4372  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4373 
4374  errorstate = SDMMC_ERROR_RX_OVERRUN;
4375 
4376  return errorstate;
4377  }
4378  else
4379  {
4380  /* No error flag set */
4381  }
4382 
4383  /* Clear all the static flags */
4384  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4385 
4386  /* Test if the switch mode is ok */
4387  if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4388  {
4389  errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4390  }
4391  else
4392  {
4393 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
4394  hsd->DriveTransceiver_1_8V_Callback(SET);
4395 #else
4397 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
4398 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
4399  /* Enable DelayBlock Peripheral */
4400  /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
4401  MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
4402  if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
4403  {
4404  return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4405  }
4406 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
4407  }
4408  }
4409 
4410  return errorstate;
4411 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
SDMMC Data Control structure.
uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
return HAL_OK
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ SD_DMAError()

static void SD_DMAError ( DMA_HandleTypeDef hdma)
static

DMA SD communication error callback.

Parameters
hdmaDMA handle
Return values
None

Definition at line 3218 of file stm32l4xx_hal_sd.c.

3219 {
3220  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3221  HAL_SD_CardStateTypeDef CardState;
3222  uint32_t RxErrorCode, TxErrorCode;
3223 
3224  RxErrorCode = hsd->hdmarx->ErrorCode;
3225  TxErrorCode = hsd->hdmatx->ErrorCode;
3226  if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
3227  {
3228  /* Clear All flags */
3229  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
3230 
3231  /* Disable All interrupts */
3232  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
3233  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
3234 
3235  hsd->ErrorCode |= HAL_SD_ERROR_DMA;
3236  CardState = HAL_SD_GetCardState(hsd);
3237  if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3238  {
3239  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3240  }
3241 
3242  hsd->State= HAL_SD_STATE_READY;
3243  hsd->Context = SD_CONTEXT_NONE;
3244  }
3245 
3246 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3247  hsd->ErrorCallback(hsd);
3248 #else
3249  HAL_SD_ErrorCallback(hsd);
3250 #endif
3251 }
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
struct __SD_HandleTypeDef else typedef struct endif SD_HandleTypeDef
SD handle Structure definition.
HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
Gets the current sd card data state.
uint32_t HAL_SD_CardStateTypeDef
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.

◆ SD_DMAReceiveCplt()

static void SD_DMAReceiveCplt ( DMA_HandleTypeDef hdma)
static

DMA SD receive process complete callback.

Parameters
hdmaDMA handle
Return values
None

Definition at line 3176 of file stm32l4xx_hal_sd.c.

3177 {
3178  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3179  uint32_t errorstate;
3180 
3181  /* Send stop command in multiblock write */
3182  if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
3183  {
3184  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
3185  if(errorstate != HAL_SD_ERROR_NONE)
3186  {
3187  hsd->ErrorCode |= errorstate;
3188 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3189  hsd->ErrorCallback(hsd);
3190 #else
3191  HAL_SD_ErrorCallback(hsd);
3192 #endif
3193  }
3194  }
3195 
3196  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
3197  in the SD DCTRL register */
3198  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
3199 
3200  /* Clear all the static flags */
3201  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3202 
3203  hsd->State = HAL_SD_STATE_READY;
3204  hsd->Context = SD_CONTEXT_NONE;
3205 
3206 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3207  hsd->RxCpltCallback(hsd);
3208 #else
3209  HAL_SD_RxCpltCallback(hsd);
3210 #endif
3211 }
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
struct __SD_HandleTypeDef else typedef struct endif SD_HandleTypeDef
SD handle Structure definition.
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Rx Transfer completed callbacks.
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.

◆ SD_DMARxAbort()

static void SD_DMARxAbort ( DMA_HandleTypeDef hdma)
static

DMA SD Rx Abort callback.

Parameters
hdmaDMA handle
Return values
None

Definition at line 3297 of file stm32l4xx_hal_sd.c.

3298 {
3299  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3300  HAL_SD_CardStateTypeDef CardState;
3301 
3302  /* Clear All flags */
3303  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3304 
3305  CardState = HAL_SD_GetCardState(hsd);
3306  hsd->State = HAL_SD_STATE_READY;
3307  hsd->Context = SD_CONTEXT_NONE;
3308  if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3309  {
3310  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3311  }
3312 
3313  if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
3314  {
3315 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3316  hsd->AbortCpltCallback(hsd);
3317 #else
3318  HAL_SD_AbortCallback(hsd);
3319 #endif
3320  }
3321  else
3322  {
3323 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3324  hsd->ErrorCallback(hsd);
3325 #else
3326  HAL_SD_ErrorCallback(hsd);
3327 #endif
3328  }
3329 }
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
struct __SD_HandleTypeDef else typedef struct endif SD_HandleTypeDef
SD handle Structure definition.
HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
Gets the current sd card data state.
uint32_t HAL_SD_CardStateTypeDef
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.

◆ SD_DMATransmitCplt()

static void SD_DMATransmitCplt ( DMA_HandleTypeDef hdma)
static

DMA SD transmit process complete callback.

Parameters
hdmaDMA handle
Return values
None

Definition at line 3163 of file stm32l4xx_hal_sd.c.

3164 {
3165  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3166 
3167  /* Enable DATAEND Interrupt */
3168  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
3169 }
struct __SD_HandleTypeDef else typedef struct endif SD_HandleTypeDef
SD handle Structure definition.

◆ SD_DMATxAbort()

static void SD_DMATxAbort ( DMA_HandleTypeDef hdma)
static

DMA SD Tx Abort callback.

Parameters
hdmaDMA handle
Return values
None

Definition at line 3258 of file stm32l4xx_hal_sd.c.

3259 {
3260  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
3261  HAL_SD_CardStateTypeDef CardState;
3262 
3263  /* Clear All flags */
3264  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3265 
3266  CardState = HAL_SD_GetCardState(hsd);
3267  hsd->State = HAL_SD_STATE_READY;
3268  hsd->Context = SD_CONTEXT_NONE;
3269  if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3270  {
3271  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
3272  }
3273 
3274  if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
3275  {
3276 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3277  hsd->AbortCpltCallback(hsd);
3278 #else
3279  HAL_SD_AbortCallback(hsd);
3280 #endif
3281  }
3282  else
3283  {
3284 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
3285  hsd->ErrorCallback(hsd);
3286 #else
3287  HAL_SD_ErrorCallback(hsd);
3288 #endif
3289  }
3290 }
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
struct __SD_HandleTypeDef else typedef struct endif SD_HandleTypeDef
SD handle Structure definition.
HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
Gets the current sd card data state.
uint32_t HAL_SD_CardStateTypeDef
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.

◆ SD_FindSCR()

static uint32_t SD_FindSCR ( SD_HandleTypeDef hsd,
uint32_t *  pSCR 
)
static

Finds the SD card SCR register value.

Parameters
hsdPointer to SD handle
pSCRpointer to the buffer that will contain the SCR value
Return values
errorstate

Definition at line 3852 of file stm32l4xx_hal_sd.c.

3853 {
3854  SDMMC_DataInitTypeDef config;
3855  uint32_t errorstate;
3856  uint32_t tickstart = HAL_GetTick();
3857  uint32_t index = 0U;
3858  uint32_t tempscr[2U] = {0U, 0U};
3859  uint32_t *scr = pSCR;
3860 
3861  /* Set Block Size To 8 Bytes */
3862  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3863  if(errorstate != HAL_SD_ERROR_NONE)
3864  {
3865  return errorstate;
3866  }
3867 
3868  /* Send CMD55 APP_CMD with argument as card's RCA */
3869  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3870  if(errorstate != HAL_SD_ERROR_NONE)
3871  {
3872  return errorstate;
3873  }
3874 
3875  config.DataTimeOut = SDMMC_DATATIMEOUT;
3876  config.DataLength = 8U;
3877  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3878  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3879  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3880  config.DPSM = SDMMC_DPSM_ENABLE;
3881  (void)SDMMC_ConfigData(hsd->Instance, &config);
3882 
3883  /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3884  errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3885  if(errorstate != HAL_SD_ERROR_NONE)
3886  {
3887  return errorstate;
3888  }
3889 
3890 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3891  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
3892  {
3893  if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
3894  {
3895  tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
3896  tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
3897  index++;
3898  }
3899 
3900 
3901  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3902  {
3903  return HAL_SD_ERROR_TIMEOUT;
3904  }
3905  }
3906 #else
3907  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3908  {
3909  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3910  {
3911  *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3912  index++;
3913  }
3914 
3915  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3916  {
3917  return HAL_SD_ERROR_TIMEOUT;
3918  }
3919  }
3920 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3921 
3922  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3923  {
3924  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3925 
3926  return HAL_SD_ERROR_DATA_TIMEOUT;
3927  }
3928  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3929  {
3930  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3931 
3932  return HAL_SD_ERROR_DATA_CRC_FAIL;
3933  }
3934  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3935  {
3936  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3937 
3938  return HAL_SD_ERROR_RX_OVERRUN;
3939  }
3940  else
3941  {
3942  /* No error flag set */
3943  /* Clear all the static flags */
3944  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3945 
3946  *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3947  ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3948  scr++;
3949  *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3950  ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3951 
3952  }
3953 
3954  return HAL_SD_ERROR_NONE;
3955 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
SDMMC Data Control structure.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
Send the Send SCR command and check the response.
uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Application command to verify that that the next command is an application specific com-mand...
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ SD_HighSpeed()

uint32_t SD_HighSpeed ( SD_HandleTypeDef hsd)

Switches the SD card to High Speed mode. This API must be used after "Transfer State".

Note
This operation should be followed by the configuration of PLL to have SDMMCCK clock between 50 and 120 MHz
Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 4044 of file stm32l4xx_hal_sd.c.

4045 {
4046  uint32_t errorstate = HAL_SD_ERROR_NONE;
4047  SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4048  uint32_t SD_hs[16] = {0};
4049  uint32_t count, loop = 0 ;
4050  uint32_t Timeout = HAL_GetTick();
4051 
4052  if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4053  {
4054  /* Standard Speed Card <= 12.5Mhz */
4055  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4056  }
4057 
4058  if(hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
4059  {
4060  /* Initialize the Data control register */
4061  hsd->Instance->DCTRL = 0;
4062  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
4063 
4064  if (errorstate != HAL_SD_ERROR_NONE)
4065  {
4066  return errorstate;
4067  }
4068 
4069  /* Configure the SD DPSM (Data Path State Machine) */
4070  sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4071  sdmmc_datainitstructure.DataLength = 64;
4072  sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4073  sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4074  sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4075  sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4076 
4077  if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
4078  {
4079  return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4080  }
4081 
4082 
4083  errorstate = SDMMC_CmdSwitch(hsd->Instance,SDMMC_SDR25_SWITCH_PATTERN);
4084  if(errorstate != HAL_SD_ERROR_NONE)
4085  {
4086  return errorstate;
4087  }
4088 
4089  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4090  {
4091  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4092  {
4093  for (count = 0U; count < 8U; count++)
4094  {
4095  SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4096  }
4097  loop ++;
4098  }
4099 
4100  if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4101  {
4102  hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4103  hsd->State= HAL_SD_STATE_READY;
4104  return HAL_SD_ERROR_TIMEOUT;
4105  }
4106  }
4107 
4108  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4109  {
4110  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4111 
4112  errorstate = 0;
4113 
4114  return errorstate;
4115  }
4116  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4117  {
4118  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4119 
4120  errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4121 
4122  return errorstate;
4123  }
4124  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4125  {
4126  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4127 
4128  errorstate = SDMMC_ERROR_RX_OVERRUN;
4129 
4130  return errorstate;
4131  }
4132  else
4133  {
4134  /* No error flag set */
4135  }
4136 
4137  /* Clear all the static flags */
4138  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4139 
4140  /* Test if the switch mode HS is ok */
4141  if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4142  {
4143  errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4144  }
4145 
4146  }
4147 
4148  return errorstate;
4149 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
SDMMC Data Control structure.
uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
return HAL_OK
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ SD_InitCard()

static uint32_t SD_InitCard ( SD_HandleTypeDef hsd)
static

Initializes the sd card.

Parameters
hsdPointer to SD handle
Return values
SDCard error state

Definition at line 3337 of file stm32l4xx_hal_sd.c.

3338 {
3340  uint32_t errorstate;
3341  uint16_t sd_rca = 1U;
3342 
3343  /* Check the power State */
3344  if(SDMMC_GetPowerState(hsd->Instance) == 0U)
3345  {
3346  /* Power off */
3347  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3348  }
3349 
3350  if(hsd->SdCard.CardType != CARD_SECURED)
3351  {
3352  /* Send CMD2 ALL_SEND_CID */
3353  errorstate = SDMMC_CmdSendCID(hsd->Instance);
3354  if(errorstate != HAL_SD_ERROR_NONE)
3355  {
3356  return errorstate;
3357  }
3358  else
3359  {
3360  /* Get Card identification number data */
3361  hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3362  hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3363  hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3364  hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3365  }
3366  }
3367 
3368  if(hsd->SdCard.CardType != CARD_SECURED)
3369  {
3370  /* Send CMD3 SET_REL_ADDR with argument 0 */
3371  /* SD Card publishes its RCA. */
3372  errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
3373  if(errorstate != HAL_SD_ERROR_NONE)
3374  {
3375  return errorstate;
3376  }
3377  }
3378  if(hsd->SdCard.CardType != CARD_SECURED)
3379  {
3380  /* Get the SD card RCA */
3381  hsd->SdCard.RelCardAdd = sd_rca;
3382 
3383  /* Send CMD9 SEND_CSD with argument as card's RCA */
3384  errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3385  if(errorstate != HAL_SD_ERROR_NONE)
3386  {
3387  return errorstate;
3388  }
3389  else
3390  {
3391  /* Get Card Specific Data */
3392  hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3393  hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3394  hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3395  hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3396  }
3397  }
3398 
3399  /* Get the Card Class */
3400  hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
3401 
3402  /* Get CSD parameters */
3403  if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
3404  {
3405  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3406  }
3407 
3408  /* Select the Card */
3409  errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
3410  if(errorstate != HAL_SD_ERROR_NONE)
3411  {
3412  return errorstate;
3413  }
3414 
3415 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
3416  /* Configure SDMMC peripheral interface */
3417  (void)SDMMC_Init(hsd->Instance, hsd->Init);
3418 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
3419 
3420  /* All cards are initialized */
3421  return HAL_SD_ERROR_NONE;
3422 }
uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint64_t Addr)
Send the Select Deselect command and check the response.
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.
uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
Get SDMMC Power state.
uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Send CSD command and check the response.
return HAL_OK
uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
Send the Send CSD command and check the response.
uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
Send the Send CID command and check the response.
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
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...

◆ SD_PowerOFF()

static void SD_PowerOFF ( SD_HandleTypeDef hsd)
static

Turns the SDMMC output signals off.

Parameters
hsdPointer to SD handle
Return values
None

Definition at line 3602 of file stm32l4xx_hal_sd.c.

3603 {
3604  /* Set Power State to OFF */
3605  (void)SDMMC_PowerState_OFF(hsd->Instance);
3606 }
HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
Set SDMMC Power state to OFF.

◆ SD_PowerON()

static uint32_t SD_PowerON ( SD_HandleTypeDef hsd)
static

Enquires cards about their operating voltage and configures clock controls and stores SD information that will be needed in future in the SD handle.

Parameters
hsdPointer to SD handle
Return values
errorstate

Definition at line 3431 of file stm32l4xx_hal_sd.c.

3432 {
3433  __IO uint32_t count = 0U;
3434  uint32_t response = 0U, validvoltage = 0U;
3435  uint32_t errorstate;
3436 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3437  uint32_t tickstart = HAL_GetTick();
3438 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3439 
3440  /* CMD0: GO_IDLE_STATE */
3441  errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3442  if(errorstate != HAL_SD_ERROR_NONE)
3443  {
3444  return errorstate;
3445  }
3446 
3447  /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
3448  errorstate = SDMMC_CmdOperCond(hsd->Instance);
3449  if(errorstate != HAL_SD_ERROR_NONE)
3450  {
3451  hsd->SdCard.CardVersion = CARD_V1_X;
3452  /* CMD0: GO_IDLE_STATE */
3453  errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3454  if(errorstate != HAL_SD_ERROR_NONE)
3455  {
3456  return errorstate;
3457  }
3458 
3459  }
3460  else
3461  {
3462  hsd->SdCard.CardVersion = CARD_V2_X;
3463  }
3464 
3465  if( hsd->SdCard.CardVersion == CARD_V2_X)
3466  {
3467  /* SEND CMD55 APP_CMD with RCA as 0 */
3468  errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3469  if(errorstate != HAL_SD_ERROR_NONE)
3470  {
3471  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3472  }
3473  }
3474  /* SD CARD */
3475  /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
3476  while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
3477  {
3478  /* SEND CMD55 APP_CMD with RCA as 0 */
3479  errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3480  if(errorstate != HAL_SD_ERROR_NONE)
3481  {
3482  return errorstate;
3483  }
3484 
3485  /* Send CMD41 */
3486  errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
3487  if(errorstate != HAL_SD_ERROR_NONE)
3488  {
3489  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3490  }
3491 
3492  /* Get command response */
3493  response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3494 
3495  /* Get operating voltage*/
3496  validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3497 
3498  count++;
3499  }
3500 
3501  if(count >= SDMMC_MAX_VOLT_TRIAL)
3502  {
3503  return HAL_SD_ERROR_INVALID_VOLTRANGE;
3504  }
3505 
3506  if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
3507  {
3508  hsd->SdCard.CardType = CARD_SDHC_SDXC;
3509 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3510  if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
3511  {
3512  if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
3513  {
3514  hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3515 
3516  /* Start switching procedue */
3517  hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
3518 
3519  /* Send CMD11 to switch 1.8V mode */
3520  errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
3521  if(errorstate != HAL_SD_ERROR_NONE)
3522  {
3523  return errorstate;
3524  }
3525 
3526  /* Check to CKSTOP */
3527  while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
3528  {
3529  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3530  {
3531  return HAL_SD_ERROR_TIMEOUT;
3532  }
3533  }
3534 
3535  /* Clear CKSTOP Flag */
3536  hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
3537 
3538  /* Check to BusyD0 */
3539  if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
3540  {
3541  /* Error when activate Voltage Switch in SDMMC Peripheral */
3542  return SDMMC_ERROR_UNSUPPORTED_FEATURE;
3543  }
3544  else
3545  {
3546  /* Enable Transceiver Switch PIN */
3547 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3548  hsd->DriveTransceiver_1_8V_Callback(SET);
3549 #else
3551 #endif
3552 
3553  /* Switch ready */
3554  hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
3555 
3556  /* Check VSWEND Flag */
3557  while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
3558  {
3559  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3560  {
3561  return HAL_SD_ERROR_TIMEOUT;
3562  }
3563  }
3564 
3565  /* Clear VSWEND Flag */
3566  hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
3567 
3568  /* Check BusyD0 status */
3569  if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
3570  {
3571  /* Error when enabling 1.8V mode */
3572  return HAL_SD_ERROR_INVALID_VOLTRANGE;
3573  }
3574  /* Switch to 1.8V OK */
3575 
3576  /* Disable VSWITCH FLAG from SDMMC Peripheral */
3577  hsd->Instance->POWER = 0x13U;
3578 
3579  /* Clean Status flags */
3580  hsd->Instance->ICR = 0xFFFFFFFFU;
3581  }
3582 
3583  hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3584  }
3585  }
3586 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3587  }
3588  else
3589  {
3590  hsd->SdCard.CardType = CARD_SDSC;
3591  }
3592 
3593 
3594  return HAL_SD_ERROR_NONE;
3595 }
uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef *SDMMCx)
Send the Operating Condition command and check the response.
uint32_t SDMMC_CmdVoltageSwitch(SDMMC_TypeDef *SDMMCx)
Send the command asking the accessed card to send its operating condition register (OCR) ...
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the command asking the accessed card to send its operating condition register (OCR) ...
uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
Send the Go Idle State command and check the response.
uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Application command to verify that that the next command is an application specific com-mand...
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.

◆ SD_Read_IT()

static void SD_Read_IT ( SD_HandleTypeDef hsd)
static

Wrap up reading in non-blocking mode.

Parameters
hsdpointer to a SD_HandleTypeDef structure that contains the configuration information.
Return values
None

Definition at line 3963 of file stm32l4xx_hal_sd.c.

3964 {
3965  uint32_t count, data, dataremaining;
3966  uint8_t* tmp;
3967 
3968  tmp = hsd->pRxBuffPtr;
3969  dataremaining = hsd->RxXferSize;
3970 
3971  if (dataremaining > 0U)
3972  {
3973  /* Read data from SDMMC Rx FIFO */
3974  for(count = 0U; count < 8U; count++)
3975  {
3976  data = SDMMC_ReadFIFO(hsd->Instance);
3977  *tmp = (uint8_t)(data & 0xFFU);
3978  tmp++;
3979  dataremaining--;
3980  *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3981  tmp++;
3982  dataremaining--;
3983  *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3984  tmp++;
3985  dataremaining--;
3986  *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3987  tmp++;
3988  dataremaining--;
3989  }
3990 
3991  hsd->pRxBuffPtr = tmp;
3992  hsd->RxXferSize = dataremaining;
3993  }
3994 }
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)

◆ SD_SendSDStatus()

static uint32_t SD_SendSDStatus ( SD_HandleTypeDef hsd,
uint32_t *  pSDstatus 
)
static

Send Status info command.

Parameters
hsdpointer to SD handle
pSDstatusPointer to the buffer that will contain the SD card status SD Status register)
Return values
errorstate

Definition at line 3615 of file stm32l4xx_hal_sd.c.

3616 {
3617  SDMMC_DataInitTypeDef config;
3618  uint32_t errorstate;
3619  uint32_t tickstart = HAL_GetTick();
3620  uint32_t count;
3621  uint32_t *pData = pSDstatus;
3622 
3623  /* Check SD response */
3624  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3625  {
3626  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3627  }
3628 
3629  /* Set block size for card if it is not equal to current block size for card */
3630  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3631  if(errorstate != HAL_SD_ERROR_NONE)
3632  {
3633  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3634  return errorstate;
3635  }
3636 
3637  /* Send CMD55 */
3638  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3639  if(errorstate != HAL_SD_ERROR_NONE)
3640  {
3641  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3642  return errorstate;
3643  }
3644 
3645  /* Configure the SD DPSM (Data Path State Machine) */
3646  config.DataTimeOut = SDMMC_DATATIMEOUT;
3647  config.DataLength = 64U;
3648  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
3649  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3650  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3651  config.DPSM = SDMMC_DPSM_ENABLE;
3652  (void)SDMMC_ConfigData(hsd->Instance, &config);
3653 
3654  /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
3655  errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
3656  if(errorstate != HAL_SD_ERROR_NONE)
3657  {
3658  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3659  return errorstate;
3660  }
3661 
3662  /* Get status data */
3663 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3664  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3665 #else
3666  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3667 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3668  {
3669  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3670  {
3671  for(count = 0U; count < 8U; count++)
3672  {
3673  *pData = SDMMC_ReadFIFO(hsd->Instance);
3674  pData++;
3675  }
3676  }
3677 
3678  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3679  {
3680  return HAL_SD_ERROR_TIMEOUT;
3681  }
3682  }
3683 
3684  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3685  {
3686  return HAL_SD_ERROR_DATA_TIMEOUT;
3687  }
3688  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3689  {
3690  return HAL_SD_ERROR_DATA_CRC_FAIL;
3691  }
3692  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3693  {
3694  return HAL_SD_ERROR_RX_OVERRUN;
3695  }
3696  else
3697  {
3698  /* Nothing to do */
3699  }
3700 
3701 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3702  while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
3703 #else
3704  while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
3705 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
3706  {
3707  *pData = SDMMC_ReadFIFO(hsd->Instance);
3708  pData++;
3709 
3710  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3711  {
3712  return HAL_SD_ERROR_TIMEOUT;
3713  }
3714  }
3715 
3716  /* Clear all the static status flags*/
3717  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3718 
3719  return HAL_SD_ERROR_NONE;
3720 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
SDMMC Data Control structure.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
Send the Status register command and check the response.
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Application command to verify that that the next command is an application specific com-mand...
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ SD_SendStatus()

static uint32_t SD_SendStatus ( SD_HandleTypeDef hsd,
uint32_t *  pCardStatus 
)
static

Returns the current card's status.

Parameters
hsdPointer to SD handle
pCardStatuspointer to the buffer that will contain the SD card status (Card Status register)
Return values
errorstate

Definition at line 3729 of file stm32l4xx_hal_sd.c.

3730 {
3731  uint32_t errorstate;
3732 
3733  if(pCardStatus == NULL)
3734  {
3735  return HAL_SD_ERROR_PARAM;
3736  }
3737 
3738  /* Send Status command */
3739  errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3740  if(errorstate != HAL_SD_ERROR_NONE)
3741  {
3742  return errorstate;
3743  }
3744 
3745  /* Get SD card status */
3746  *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3747 
3748  return HAL_SD_ERROR_NONE;
3749 }
uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Status command and check the response.
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.

◆ SD_UltraHighSpeed()

static uint32_t SD_UltraHighSpeed ( SD_HandleTypeDef hsd)
static

Switches the SD card to Ultra High Speed mode. This API must be used after "Transfer State".

Note
This operation should be followed by the configuration of PLL to have SDMMCCK clock between 50 and 120 MHz
Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 4159 of file stm32l4xx_hal_sd.c.

4160 {
4161  uint32_t errorstate = HAL_SD_ERROR_NONE;
4162  SDMMC_DataInitTypeDef sdmmc_datainitstructure;
4163  uint32_t SD_hs[16] = {0};
4164  uint32_t count, loop = 0 ;
4165  uint32_t Timeout = HAL_GetTick();
4166 
4167  if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
4168  {
4169  /* Standard Speed Card <= 12.5Mhz */
4170  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
4171  }
4172 
4173  if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
4174  (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
4175  {
4176  /* Initialize the Data control register */
4177  hsd->Instance->DCTRL = 0;
4178  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
4179 
4180  if (errorstate != HAL_SD_ERROR_NONE)
4181  {
4182  return errorstate;
4183  }
4184 
4185  /* Configure the SD DPSM (Data Path State Machine) */
4186  sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
4187  sdmmc_datainitstructure.DataLength = 64;
4188  sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
4189  sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
4190  sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
4191  sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
4192 
4193  if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
4194  {
4195  return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4196  }
4197 
4198  errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN);
4199  if(errorstate != HAL_SD_ERROR_NONE)
4200  {
4201  return errorstate;
4202  }
4203 
4204  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
4205  {
4206  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4207  {
4208  for (count = 0U; count < 8U; count++)
4209  {
4210  SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
4211  }
4212  loop ++;
4213  }
4214 
4215  if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
4216  {
4217  hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4218  hsd->State= HAL_SD_STATE_READY;
4219  return HAL_SD_ERROR_TIMEOUT;
4220  }
4221  }
4222 
4223  if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4224  {
4225  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4226 
4227  errorstate = 0;
4228 
4229  return errorstate;
4230  }
4231  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4232  {
4233  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4234 
4235  errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4236 
4237  return errorstate;
4238  }
4239  else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4240  {
4241  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4242 
4243  errorstate = SDMMC_ERROR_RX_OVERRUN;
4244 
4245  return errorstate;
4246  }
4247  else
4248  {
4249  /* No error flag set */
4250  }
4251 
4252  /* Clear all the static flags */
4253  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4254 
4255  /* Test if the switch mode HS is ok */
4256  if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
4257  {
4258  errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4259  }
4260  else
4261  {
4262 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
4263  hsd->DriveTransceiver_1_8V_Callback(SET);
4264 #else
4266 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
4267 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
4268  /* Enable DelayBlock Peripheral */
4269  /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
4270  MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
4271  if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
4272  {
4273  return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4274  }
4275 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
4276  }
4277  }
4278 
4279  return errorstate;
4280 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
SDMMC Data Control structure.
uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
return HAL_OK
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ SD_WideBus_Disable()

static uint32_t SD_WideBus_Disable ( SD_HandleTypeDef hsd)
static

Disables the SDMMC wide bus mode.

Parameters
hsdPointer to SD handle
Return values
errorstate

Definition at line 3803 of file stm32l4xx_hal_sd.c.

3804 {
3805  uint32_t scr[2U] = {0U, 0U};
3806  uint32_t errorstate;
3807 
3808  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3809  {
3810  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3811  }
3812 
3813  /* Get SCR Register */
3814  errorstate = SD_FindSCR(hsd, scr);
3815  if(errorstate != HAL_SD_ERROR_NONE)
3816  {
3817  return errorstate;
3818  }
3819 
3820  /* If requested card supports 1 bit mode operation */
3821  if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3822  {
3823  /* Send CMD55 APP_CMD with argument as card's RCA */
3824  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3825  if(errorstate != HAL_SD_ERROR_NONE)
3826  {
3827  return errorstate;
3828  }
3829 
3830  /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3831  errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3832  if(errorstate != HAL_SD_ERROR_NONE)
3833  {
3834  return errorstate;
3835  }
3836 
3837  return HAL_SD_ERROR_NONE;
3838  }
3839  else
3840  {
3841  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3842  }
3843 }
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
Send the Bus Width command and check the response.
uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Application command to verify that that the next command is an application specific com-mand...
static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
Finds the SD card SCR register value.

◆ SD_WideBus_Enable()

static uint32_t SD_WideBus_Enable ( SD_HandleTypeDef hsd)
static

Enables the SDMMC wide bus mode.

Parameters
hsdpointer to SD handle
Return values
errorstate

Definition at line 3756 of file stm32l4xx_hal_sd.c.

3757 {
3758  uint32_t scr[2U] = {0U, 0U};
3759  uint32_t errorstate;
3760 
3761  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3762  {
3763  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3764  }
3765 
3766  /* Get SCR Register */
3767  errorstate = SD_FindSCR(hsd, scr);
3768  if(errorstate != HAL_SD_ERROR_NONE)
3769  {
3770  return errorstate;
3771  }
3772 
3773  /* If requested card supports wide bus operation */
3774  if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3775  {
3776  /* Send CMD55 APP_CMD with argument as card's RCA.*/
3777  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3778  if(errorstate != HAL_SD_ERROR_NONE)
3779  {
3780  return errorstate;
3781  }
3782 
3783  /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3784  errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3785  if(errorstate != HAL_SD_ERROR_NONE)
3786  {
3787  return errorstate;
3788  }
3789 
3790  return HAL_SD_ERROR_NONE;
3791  }
3792  else
3793  {
3794  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3795  }
3796 }
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
Send the Bus Width command and check the response.
uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Application command to verify that that the next command is an application specific com-mand...
static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
Finds the SD card SCR register value.

◆ SD_Write_IT()

static void SD_Write_IT ( SD_HandleTypeDef hsd)
static

Wrap up writing in non-blocking mode.

Parameters
hsdpointer to a SD_HandleTypeDef structure that contains the configuration information.
Return values
None

Definition at line 4002 of file stm32l4xx_hal_sd.c.

4003 {
4004  uint32_t count, data, dataremaining;
4005  uint8_t* tmp;
4006 
4007  tmp = hsd->pTxBuffPtr;
4008  dataremaining = hsd->TxXferSize;
4009 
4010  if (dataremaining > 0U)
4011  {
4012  /* Write data to SDMMC Tx FIFO */
4013  for(count = 0U; count < 8U; count++)
4014  {
4015  data = (uint32_t)(*tmp);
4016  tmp++;
4017  dataremaining--;
4018  data |= ((uint32_t)(*tmp) << 8U);
4019  tmp++;
4020  dataremaining--;
4021  data |= ((uint32_t)(*tmp) << 16U);
4022  tmp++;
4023  dataremaining--;
4024  data |= ((uint32_t)(*tmp) << 24U);
4025  tmp++;
4026  dataremaining--;
4027  (void)SDMMC_WriteFIFO(hsd->Instance, &data);
4028  }
4029 
4030  hsd->pTxBuffPtr = tmp;
4031  hsd->TxXferSize = dataremaining;
4032  }
4033 }
HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
Write data (word) to Tx FIFO in blocking mode (polling)

◆ while()

while ( ((sta_reg &(SDMMC_FLAG_CCRCFAIL|SDMMC_FLAG_CMDREND|SDMMC_FLAG_CTIMEOUT))==0U)||((sta_reg &SDMMC_FLAG_CMDACT) !=0U)  )

Variable Documentation

◆ else

else
Initial value:
{
}
__SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS)

Definition at line 1305 of file stm32l4xx_ll_sdmmc.c.

◆ response_r1

response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1)

Definition at line 1319 of file stm32l4xx_ll_sdmmc.c.