271 #ifdef HAL_SD_MODULE_ENABLED
290 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
291 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
292 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
293 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
294 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
295 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
296 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
297 static void SD_PowerOFF(SD_HandleTypeDef *hsd);
298 static void SD_Write_IT(SD_HandleTypeDef *hsd);
299 static void SD_Read_IT(SD_HandleTypeDef *hsd);
346 assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
347 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
349 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
352 if(hsd->State == HAL_SD_STATE_RESET)
356 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
360 hsd->ErrorCallback = HAL_SD_ErrorCallback;
363 if(hsd->MspInitCallback ==
NULL)
365 hsd->MspInitCallback = HAL_SD_MspInit;
369 hsd->MspInitCallback(hsd);
376 hsd->State = HAL_SD_STATE_BUSY;
379 if (HAL_SD_InitCard(hsd) !=
HAL_OK)
385 hsd->ErrorCode = HAL_SD_ERROR_NONE;
388 hsd->Context = SD_CONTEXT_NONE;
391 hsd->State = HAL_SD_STATE_READY;
410 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
411 Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
412 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
413 Init.BusWide = SDMMC_BUS_WIDE_1B;
414 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
415 Init.ClockDiv = SDMMC_INIT_CLK_DIV;
418 status = SDMMC_Init(hsd->Instance,
Init);
425 __HAL_SD_DISABLE(hsd);
428 (void)SDMMC_PowerState_ON(hsd->Instance);
431 __HAL_SD_ENABLE(hsd);
434 errorstate = SD_PowerON(hsd);
435 if(errorstate != HAL_SD_ERROR_NONE)
437 hsd->State = HAL_SD_STATE_READY;
438 hsd->ErrorCode |= errorstate;
443 errorstate = SD_InitCard(hsd);
444 if(errorstate != HAL_SD_ERROR_NONE)
446 hsd->State = HAL_SD_STATE_READY;
447 hsd->ErrorCode |= errorstate;
470 hsd->State = HAL_SD_STATE_BUSY;
475 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
476 if(hsd->MspDeInitCallback ==
NULL)
478 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
482 hsd->MspDeInitCallback(hsd);
485 HAL_SD_MspDeInit(hsd);
488 hsd->ErrorCode = HAL_SD_ERROR_NONE;
489 hsd->State = HAL_SD_STATE_RESET;
500 __weak
void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
515 __weak
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
556 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
558 SDMMC_DataInitTypeDef
config;
561 uint32_t
count, data, dataremaining;
562 uint32_t
add = BlockAdd;
563 uint8_t *tempbuff = pData;
567 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
571 if(hsd->State == HAL_SD_STATE_READY)
573 hsd->ErrorCode = HAL_SD_ERROR_NONE;
575 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
577 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
581 hsd->State = HAL_SD_STATE_BUSY;
584 hsd->Instance->DCTRL = 0U;
586 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
592 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
593 if(errorstate != HAL_SD_ERROR_NONE)
596 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
597 hsd->ErrorCode |= errorstate;
598 hsd->State = HAL_SD_STATE_READY;
603 config.DataTimeOut = SDMMC_DATATIMEOUT;
604 config.DataLength = NumberOfBlocks * BLOCKSIZE;
605 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
606 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
607 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
608 config.DPSM = SDMMC_DPSM_ENABLE;
609 (void)SDMMC_ConfigData(hsd->Instance, &
config);
612 if(NumberOfBlocks > 1U)
614 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
617 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
621 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
624 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
626 if(errorstate != HAL_SD_ERROR_NONE)
629 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
630 hsd->ErrorCode |= errorstate;
631 hsd->State = HAL_SD_STATE_READY;
632 hsd->Context = SD_CONTEXT_NONE;
637 dataremaining =
config.DataLength;
638 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
640 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
645 data = SDMMC_ReadFIFO(hsd->Instance);
646 *tempbuff = (uint8_t)(data & 0xFFU);
649 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
652 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
655 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
661 if(((
HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
664 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
665 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
666 hsd->State= HAL_SD_STATE_READY;
667 hsd->Context = SD_CONTEXT_NONE;
673 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
675 if(hsd->SdCard.CardType != CARD_SECURED)
678 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
679 if(errorstate != HAL_SD_ERROR_NONE)
682 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
683 hsd->ErrorCode |= errorstate;
684 hsd->State = HAL_SD_STATE_READY;
685 hsd->Context = SD_CONTEXT_NONE;
692 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
695 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
696 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
697 hsd->State = HAL_SD_STATE_READY;
698 hsd->Context = SD_CONTEXT_NONE;
701 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
704 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
705 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
706 hsd->State = HAL_SD_STATE_READY;
707 hsd->Context = SD_CONTEXT_NONE;
710 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
713 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
714 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
715 hsd->State = HAL_SD_STATE_READY;
716 hsd->Context = SD_CONTEXT_NONE;
725 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
727 data = SDMMC_ReadFIFO(hsd->Instance);
728 *tempbuff = (uint8_t)(data & 0xFFU);
731 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
734 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
737 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
741 if(((
HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
744 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
745 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
746 hsd->State= HAL_SD_STATE_READY;
747 hsd->Context = SD_CONTEXT_NONE;
753 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
755 hsd->State = HAL_SD_STATE_READY;
761 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
778 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
780 SDMMC_DataInitTypeDef
config;
783 uint32_t
count, data, dataremaining;
784 uint32_t
add = BlockAdd;
785 uint8_t *tempbuff = pData;
789 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
793 if(hsd->State == HAL_SD_STATE_READY)
795 hsd->ErrorCode = HAL_SD_ERROR_NONE;
797 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
799 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
803 hsd->State = HAL_SD_STATE_BUSY;
806 hsd->Instance->DCTRL = 0U;
808 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
814 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
815 if(errorstate != HAL_SD_ERROR_NONE)
818 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
819 hsd->ErrorCode |= errorstate;
820 hsd->State = HAL_SD_STATE_READY;
825 config.DataTimeOut = SDMMC_DATATIMEOUT;
826 config.DataLength = NumberOfBlocks * BLOCKSIZE;
827 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
828 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
829 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
830 config.DPSM = SDMMC_DPSM_ENABLE;
831 (void)SDMMC_ConfigData(hsd->Instance, &
config);
834 if(NumberOfBlocks > 1U)
836 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
839 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
843 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
846 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
848 if(errorstate != HAL_SD_ERROR_NONE)
851 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
852 hsd->ErrorCode |= errorstate;
853 hsd->State = HAL_SD_STATE_READY;
854 hsd->Context = SD_CONTEXT_NONE;
859 dataremaining =
config.DataLength;
860 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
862 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
867 data = (uint32_t)(*tempbuff);
870 data |= ((uint32_t)(*tempbuff) << 8U);
873 data |= ((uint32_t)(*tempbuff) << 16U);
876 data |= ((uint32_t)(*tempbuff) << 24U);
879 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
883 if(((
HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
886 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
887 hsd->ErrorCode |= errorstate;
888 hsd->State = HAL_SD_STATE_READY;
889 hsd->Context = SD_CONTEXT_NONE;
895 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
897 if(hsd->SdCard.CardType != CARD_SECURED)
900 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
901 if(errorstate != HAL_SD_ERROR_NONE)
904 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
905 hsd->ErrorCode |= errorstate;
906 hsd->State = HAL_SD_STATE_READY;
907 hsd->Context = SD_CONTEXT_NONE;
914 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
917 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
918 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
919 hsd->State = HAL_SD_STATE_READY;
920 hsd->Context = SD_CONTEXT_NONE;
923 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
926 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
927 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
928 hsd->State = HAL_SD_STATE_READY;
929 hsd->Context = SD_CONTEXT_NONE;
932 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
935 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
936 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
937 hsd->State = HAL_SD_STATE_READY;
938 hsd->Context = SD_CONTEXT_NONE;
947 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
949 hsd->State = HAL_SD_STATE_READY;
955 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
973 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
975 SDMMC_DataInitTypeDef
config;
977 uint32_t
add = BlockAdd;
981 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
985 if(hsd->State == HAL_SD_STATE_READY)
987 hsd->ErrorCode = HAL_SD_ERROR_NONE;
989 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
991 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
995 hsd->State = HAL_SD_STATE_BUSY;
998 hsd->Instance->DCTRL = 0U;
1000 hsd->pRxBuffPtr = pData;
1001 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1003 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1005 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1011 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1012 if(errorstate != HAL_SD_ERROR_NONE)
1015 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1016 hsd->ErrorCode |= errorstate;
1017 hsd->State = HAL_SD_STATE_READY;
1022 config.DataTimeOut = SDMMC_DATATIMEOUT;
1023 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1024 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1025 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1026 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1027 config.DPSM = SDMMC_DPSM_ENABLE;
1028 (void)SDMMC_ConfigData(hsd->Instance, &
config);
1031 if(NumberOfBlocks > 1U)
1033 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1036 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1040 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1043 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1045 if(errorstate != HAL_SD_ERROR_NONE)
1048 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1049 hsd->ErrorCode |= errorstate;
1050 hsd->State = HAL_SD_STATE_READY;
1051 hsd->Context = SD_CONTEXT_NONE;
1076 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1078 SDMMC_DataInitTypeDef
config;
1079 uint32_t errorstate;
1080 uint32_t
add = BlockAdd;
1084 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1088 if(hsd->State == HAL_SD_STATE_READY)
1090 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1092 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1094 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1098 hsd->State = HAL_SD_STATE_BUSY;
1101 hsd->Instance->DCTRL = 0U;
1103 hsd->pTxBuffPtr = pData;
1104 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1107 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1109 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1115 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1116 if(errorstate != HAL_SD_ERROR_NONE)
1119 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1120 hsd->ErrorCode |= errorstate;
1121 hsd->State = HAL_SD_STATE_READY;
1126 if(NumberOfBlocks > 1U)
1128 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1131 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1135 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1138 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1140 if(errorstate != HAL_SD_ERROR_NONE)
1143 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1144 hsd->ErrorCode |= errorstate;
1145 hsd->State = HAL_SD_STATE_READY;
1146 hsd->Context = SD_CONTEXT_NONE;
1151 config.DataTimeOut = SDMMC_DATATIMEOUT;
1152 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1153 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1154 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1155 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1156 config.DPSM = SDMMC_DPSM_ENABLE;
1157 (void)SDMMC_ConfigData(hsd->Instance, &
config);
1180 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1182 SDMMC_DataInitTypeDef
config;
1183 uint32_t errorstate;
1184 uint32_t
add = BlockAdd;
1188 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1192 if(hsd->State == HAL_SD_STATE_READY)
1194 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1196 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1198 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1202 hsd->State = HAL_SD_STATE_BUSY;
1205 hsd->Instance->DCTRL = 0U;
1207 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1210 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1213 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1216 hsd->hdmarx->XferAbortCallback =
NULL;
1219 if(
HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) !=
HAL_OK)
1221 __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1222 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1223 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1224 hsd->State = HAL_SD_STATE_READY;
1230 __HAL_SD_DMA_ENABLE(hsd);
1232 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1238 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1239 if(errorstate != HAL_SD_ERROR_NONE)
1242 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1243 hsd->ErrorCode |= errorstate;
1244 hsd->State = HAL_SD_STATE_READY;
1249 config.DataTimeOut = SDMMC_DATATIMEOUT;
1250 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1251 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1252 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1253 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1254 config.DPSM = SDMMC_DPSM_ENABLE;
1255 (void)SDMMC_ConfigData(hsd->Instance, &
config);
1258 if(NumberOfBlocks > 1U)
1260 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1263 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1267 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1270 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1272 if(errorstate != HAL_SD_ERROR_NONE)
1275 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1276 hsd->ErrorCode |= errorstate;
1277 hsd->State = HAL_SD_STATE_READY;
1278 hsd->Context = SD_CONTEXT_NONE;
1304 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1306 SDMMC_DataInitTypeDef
config;
1307 uint32_t errorstate;
1308 uint32_t
add = BlockAdd;
1312 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1316 if(hsd->State == HAL_SD_STATE_READY)
1318 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1320 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1322 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1326 hsd->State = HAL_SD_STATE_BUSY;
1329 hsd->Instance->DCTRL = 0U;
1332 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1335 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1338 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1341 hsd->hdmatx->XferAbortCallback =
NULL;
1343 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1349 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1350 if(errorstate != HAL_SD_ERROR_NONE)
1353 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1354 hsd->ErrorCode |= errorstate;
1355 hsd->State = HAL_SD_STATE_READY;
1360 if(NumberOfBlocks > 1U)
1362 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1365 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1369 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1372 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1374 if(errorstate != HAL_SD_ERROR_NONE)
1377 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1378 hsd->ErrorCode |= errorstate;
1379 hsd->State = HAL_SD_STATE_READY;
1380 hsd->Context = SD_CONTEXT_NONE;
1385 __HAL_SD_DMA_ENABLE(hsd);
1388 if(
HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) !=
HAL_OK)
1390 __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1391 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1392 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1393 hsd->State = HAL_SD_STATE_READY;
1394 hsd->Context = SD_CONTEXT_NONE;
1400 config.DataTimeOut = SDMMC_DATATIMEOUT;
1401 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1402 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1403 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1404 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1405 config.DPSM = SDMMC_DPSM_ENABLE;
1406 (void)SDMMC_ConfigData(hsd->Instance, &
config);
1426 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1428 uint32_t errorstate;
1429 uint32_t start_add = BlockStartAdd;
1430 uint32_t end_add = BlockEndAdd;
1432 if(hsd->State == HAL_SD_STATE_READY)
1434 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1436 if(end_add < start_add)
1438 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1442 if(end_add > (hsd->SdCard.LogBlockNbr))
1444 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1448 hsd->State = HAL_SD_STATE_BUSY;
1451 if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1454 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1455 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1456 hsd->State = HAL_SD_STATE_READY;
1460 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1463 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1464 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1465 hsd->State = HAL_SD_STATE_READY;
1470 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1477 if(hsd->SdCard.CardType != CARD_SECURED)
1480 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1481 if(errorstate != HAL_SD_ERROR_NONE)
1484 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1485 hsd->ErrorCode |= errorstate;
1486 hsd->State = HAL_SD_STATE_READY;
1491 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1492 if(errorstate != HAL_SD_ERROR_NONE)
1495 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1496 hsd->ErrorCode |= errorstate;
1497 hsd->State = HAL_SD_STATE_READY;
1503 errorstate = SDMMC_CmdErase(hsd->Instance);
1504 if(errorstate != HAL_SD_ERROR_NONE)
1507 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1508 hsd->ErrorCode |= errorstate;
1509 hsd->State = HAL_SD_STATE_READY;
1513 hsd->State = HAL_SD_STATE_READY;
1528 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1530 uint32_t errorstate;
1531 uint32_t
context = hsd->Context;
1534 if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) !=
RESET) && ((
context & SD_CONTEXT_IT) != 0U))
1539 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) !=
RESET)
1541 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1543 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1544 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1549 if((
context & SD_CONTEXT_IT) != 0U)
1551 if(((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1553 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1554 if(errorstate != HAL_SD_ERROR_NONE)
1556 hsd->ErrorCode |= errorstate;
1557 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1558 hsd->ErrorCallback(hsd);
1560 HAL_SD_ErrorCallback(hsd);
1566 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1568 hsd->State = HAL_SD_STATE_READY;
1569 hsd->Context = SD_CONTEXT_NONE;
1570 if(((
context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1572 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1573 hsd->RxCpltCallback(hsd);
1580 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1581 hsd->TxCpltCallback(hsd);
1587 else if((
context & SD_CONTEXT_DMA) != 0U)
1589 if((
context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1591 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1592 if(errorstate != HAL_SD_ERROR_NONE)
1594 hsd->ErrorCode |= errorstate;
1595 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1596 hsd->ErrorCallback(hsd);
1598 HAL_SD_ErrorCallback(hsd);
1602 if(((
context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1608 hsd->State = HAL_SD_STATE_READY;
1610 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1611 hsd->TxCpltCallback(hsd);
1623 else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) !=
RESET) && ((
context & SD_CONTEXT_IT) != 0U))
1628 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) !=
RESET)
1631 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL) !=
RESET)
1633 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1635 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT) !=
RESET)
1637 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1639 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR) !=
RESET)
1641 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1643 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR) !=
RESET)
1645 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1649 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1652 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1653 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1655 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1657 if((
context & SD_CONTEXT_IT) != 0U)
1660 hsd->State = HAL_SD_STATE_READY;
1661 hsd->Context = SD_CONTEXT_NONE;
1662 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1663 hsd->ErrorCallback(hsd);
1665 HAL_SD_ErrorCallback(hsd);
1668 else if((
context & SD_CONTEXT_DMA) != 0U)
1671 if(((
context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1674 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1678 SD_DMATxAbort(hsd->hdmatx);
1681 else if(((
context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1684 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1688 SD_DMARxAbort(hsd->hdmarx);
1693 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1694 hsd->State = HAL_SD_STATE_READY;
1695 hsd->Context = SD_CONTEXT_NONE;
1696 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1697 hsd->AbortCpltCallback(hsd);
1719 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1730 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1732 return hsd->ErrorCode;
1770 __weak
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1795 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1811 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1815 if(pCallback ==
NULL)
1818 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1825 if(hsd->State == HAL_SD_STATE_READY)
1829 case HAL_SD_TX_CPLT_CB_ID :
1830 hsd->TxCpltCallback = pCallback;
1832 case HAL_SD_RX_CPLT_CB_ID :
1833 hsd->RxCpltCallback = pCallback;
1835 case HAL_SD_ERROR_CB_ID :
1836 hsd->ErrorCallback = pCallback;
1838 case HAL_SD_ABORT_CB_ID :
1839 hsd->AbortCpltCallback = pCallback;
1841 case HAL_SD_MSP_INIT_CB_ID :
1842 hsd->MspInitCallback = pCallback;
1844 case HAL_SD_MSP_DEINIT_CB_ID :
1845 hsd->MspDeInitCallback = pCallback;
1849 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1855 else if (hsd->State == HAL_SD_STATE_RESET)
1859 case HAL_SD_MSP_INIT_CB_ID :
1860 hsd->MspInitCallback = pCallback;
1862 case HAL_SD_MSP_DEINIT_CB_ID :
1863 hsd->MspDeInitCallback = pCallback;
1867 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1876 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1900 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1907 if(hsd->State == HAL_SD_STATE_READY)
1911 case HAL_SD_TX_CPLT_CB_ID :
1914 case HAL_SD_RX_CPLT_CB_ID :
1917 case HAL_SD_ERROR_CB_ID :
1918 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1920 case HAL_SD_ABORT_CB_ID :
1923 case HAL_SD_MSP_INIT_CB_ID :
1924 hsd->MspInitCallback = HAL_SD_MspInit;
1926 case HAL_SD_MSP_DEINIT_CB_ID :
1927 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1931 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1937 else if (hsd->State == HAL_SD_STATE_RESET)
1941 case HAL_SD_MSP_INIT_CB_ID :
1942 hsd->MspInitCallback = HAL_SD_MspInit;
1944 case HAL_SD_MSP_DEINIT_CB_ID :
1945 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1949 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1958 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1996 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
1998 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2000 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2002 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2004 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2006 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2008 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2010 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2012 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2014 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2016 pCID->Reserved2 = 1U;
2029 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2031 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2033 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2035 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2037 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2039 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2041 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2043 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2045 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2047 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2049 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2051 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2053 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2055 pCSD->Reserved2 = 0U;
2057 if(hsd->SdCard.CardType == CARD_SDSC)
2059 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2061 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2063 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2065 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2067 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2069 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2071 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2072 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2073 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2075 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2076 hsd->SdCard.LogBlockSize = 512U;
2078 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2081 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2083 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2084 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2085 hsd->SdCard.BlockSize = 512U;
2086 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2091 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2092 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2093 hsd->State = HAL_SD_STATE_READY;
2097 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2099 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2101 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2103 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2105 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2107 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2109 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2111 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2113 pCSD->Reserved3 = 0;
2115 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2117 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2119 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2121 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2123 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2125 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2127 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2129 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2131 pCSD->Reserved4 = 1;
2143 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2145 uint32_t sd_status[16];
2146 uint32_t errorstate;
2148 errorstate = SD_SendSDStatus(hsd, sd_status);
2149 if(errorstate != HAL_SD_ERROR_NONE)
2152 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2153 hsd->ErrorCode |= errorstate;
2154 hsd->State = HAL_SD_STATE_READY;
2159 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2161 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2163 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2165 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2166 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2168 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2170 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2172 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2174 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2176 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2178 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2191 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2193 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2194 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2195 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2196 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2197 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2198 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2199 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2200 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2216 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2218 SDMMC_InitTypeDef
Init;
2219 uint32_t errorstate;
2225 hsd->State = HAL_SD_STATE_BUSY;
2227 if(hsd->SdCard.CardType != CARD_SECURED)
2229 if(WideMode == SDMMC_BUS_WIDE_8B)
2231 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2233 else if(WideMode == SDMMC_BUS_WIDE_4B)
2235 errorstate = SD_WideBus_Enable(hsd);
2237 hsd->ErrorCode |= errorstate;
2239 else if(WideMode == SDMMC_BUS_WIDE_1B)
2241 errorstate = SD_WideBus_Disable(hsd);
2243 hsd->ErrorCode |= errorstate;
2248 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2254 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2257 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2260 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2261 hsd->State = HAL_SD_STATE_READY;
2267 Init.ClockEdge = hsd->Init.ClockEdge;
2268 Init.ClockBypass = hsd->Init.ClockBypass;
2269 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2270 Init.BusWide = WideMode;
2271 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2272 Init.ClockDiv = hsd->Init.ClockDiv;
2273 (void)SDMMC_Init(hsd->Instance,
Init);
2277 hsd->State = HAL_SD_STATE_READY;
2287 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2290 uint32_t errorstate;
2293 errorstate = SD_SendStatus(hsd, &resp1);
2294 if(errorstate != HAL_SD_ERROR_NONE)
2296 hsd->ErrorCode |= errorstate;
2299 cardstate = ((resp1 >> 9U) & 0x0FU);
2301 return (HAL_SD_CardStateTypeDef)cardstate;
2312 HAL_SD_CardStateTypeDef CardState;
2313 uint32_t
context = hsd->Context;
2316 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2317 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2320 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2324 if ((
context & SD_CONTEXT_DMA) != 0U)
2330 if (((
context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2334 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2338 else if (((
context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2342 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2351 hsd->State = HAL_SD_STATE_READY;
2354 hsd->Context = SD_CONTEXT_NONE;
2356 CardState = HAL_SD_GetCardState(hsd);
2357 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2359 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2361 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2376 HAL_SD_CardStateTypeDef CardState;
2377 uint32_t
context = hsd->Context;
2380 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2381 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2385 if ((
context & SD_CONTEXT_DMA) != 0U)
2391 if (((
context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2393 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2400 else if (((
context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((
context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2402 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2417 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2419 CardState = HAL_SD_GetCardState(hsd);
2420 hsd->State = HAL_SD_STATE_READY;
2421 hsd->Context = SD_CONTEXT_NONE;
2422 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2424 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2426 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2432 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2433 hsd->AbortCpltCallback(hsd);
2463 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->
Parent);
2466 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
2476 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->
Parent);
2477 uint32_t errorstate;
2480 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2482 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2483 if(errorstate != HAL_SD_ERROR_NONE)
2485 hsd->ErrorCode |= errorstate;
2486 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2487 hsd->ErrorCallback(hsd);
2489 HAL_SD_ErrorCallback(hsd);
2499 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2501 hsd->State = HAL_SD_STATE_READY;
2502 hsd->Context = SD_CONTEXT_NONE;
2504 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2505 hsd->RxCpltCallback(hsd);
2518 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->
Parent);
2519 HAL_SD_CardStateTypeDef CardState;
2520 uint32_t RxErrorCode, TxErrorCode;
2525 RxErrorCode = hsd->hdmarx->ErrorCode;
2526 TxErrorCode = hsd->hdmatx->ErrorCode;
2530 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2533 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2534 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2536 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2537 CardState = HAL_SD_GetCardState(hsd);
2538 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2540 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2543 hsd->State= HAL_SD_STATE_READY;
2544 hsd->Context = SD_CONTEXT_NONE;
2547 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2548 hsd->ErrorCallback(hsd);
2550 HAL_SD_ErrorCallback(hsd);
2562 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->
Parent);
2563 HAL_SD_CardStateTypeDef CardState;
2566 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2568 CardState = HAL_SD_GetCardState(hsd);
2569 hsd->State = HAL_SD_STATE_READY;
2570 hsd->Context = SD_CONTEXT_NONE;
2571 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2573 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2576 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2578 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2579 hsd->AbortCpltCallback(hsd);
2586 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2587 hsd->ErrorCallback(hsd);
2589 HAL_SD_ErrorCallback(hsd);
2601 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->
Parent);
2602 HAL_SD_CardStateTypeDef CardState;
2605 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2607 CardState = HAL_SD_GetCardState(hsd);
2608 hsd->State = HAL_SD_STATE_READY;
2609 hsd->Context = SD_CONTEXT_NONE;
2610 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2612 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2615 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2617 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2618 hsd->AbortCpltCallback(hsd);
2625 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2626 hsd->ErrorCallback(hsd);
2628 HAL_SD_ErrorCallback(hsd);
2638 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2640 HAL_SD_CardCSDTypeDef CSD;
2641 uint32_t errorstate;
2642 uint16_t sd_rca = 1U;
2645 if(SDMMC_GetPowerState(hsd->Instance) == 0U)
2648 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2651 if(hsd->SdCard.CardType != CARD_SECURED)
2654 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2655 if(errorstate != HAL_SD_ERROR_NONE)
2662 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2663 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2664 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2665 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2669 if(hsd->SdCard.CardType != CARD_SECURED)
2673 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2674 if(errorstate != HAL_SD_ERROR_NONE)
2679 if(hsd->SdCard.CardType != CARD_SECURED)
2682 hsd->SdCard.RelCardAdd = sd_rca;
2685 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2686 if(errorstate != HAL_SD_ERROR_NONE)
2693 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2694 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2695 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2696 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2701 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
2704 if (HAL_SD_GetCardCSD(hsd, &CSD) !=
HAL_OK)
2706 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2710 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2711 if(errorstate != HAL_SD_ERROR_NONE)
2717 (void)SDMMC_Init(hsd->Instance, hsd->Init);
2720 return HAL_SD_ERROR_NONE;
2730 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2733 uint32_t
response = 0U, validvoltage = 0U;
2734 uint32_t errorstate;
2737 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2738 if(errorstate != HAL_SD_ERROR_NONE)
2744 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2745 if(errorstate != HAL_SD_ERROR_NONE)
2747 hsd->SdCard.CardVersion = CARD_V1_X;
2749 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2750 if(errorstate != HAL_SD_ERROR_NONE)
2758 hsd->SdCard.CardVersion = CARD_V2_X;
2761 if( hsd->SdCard.CardVersion == CARD_V2_X)
2764 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2765 if(errorstate != HAL_SD_ERROR_NONE)
2767 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2772 while((
count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2775 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2776 if(errorstate != HAL_SD_ERROR_NONE)
2782 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2783 if(errorstate != HAL_SD_ERROR_NONE)
2785 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2789 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2792 validvoltage = (((
response >> 31U) == 1U) ? 1U : 0U);
2797 if(
count >= SDMMC_MAX_VOLT_TRIAL)
2799 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2802 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY)
2804 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2808 hsd->SdCard.CardType = CARD_SDSC;
2812 return HAL_SD_ERROR_NONE;
2820 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2823 (void)SDMMC_PowerState_OFF(hsd->Instance);
2833 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2835 SDMMC_DataInitTypeDef
config;
2836 uint32_t errorstate;
2839 uint32_t *pData = pSDstatus;
2842 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2844 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2848 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2849 if(errorstate != HAL_SD_ERROR_NONE)
2851 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2856 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2857 if(errorstate != HAL_SD_ERROR_NONE)
2859 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2864 config.DataTimeOut = SDMMC_DATATIMEOUT;
2866 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
2867 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
2868 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
2869 config.DPSM = SDMMC_DPSM_ENABLE;
2870 (void)SDMMC_ConfigData(hsd->Instance, &
config);
2873 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2874 if(errorstate != HAL_SD_ERROR_NONE)
2876 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2881 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
2883 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
2887 *pData = SDMMC_ReadFIFO(hsd->Instance);
2892 if((
HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2894 return HAL_SD_ERROR_TIMEOUT;
2898 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
2900 return HAL_SD_ERROR_DATA_TIMEOUT;
2902 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
2904 return HAL_SD_ERROR_DATA_CRC_FAIL;
2906 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
2908 return HAL_SD_ERROR_RX_OVERRUN;
2915 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
2917 *pData = SDMMC_ReadFIFO(hsd->Instance);
2920 if((
HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2922 return HAL_SD_ERROR_TIMEOUT;
2927 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2929 return HAL_SD_ERROR_NONE;
2939 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2941 uint32_t errorstate;
2943 if(pCardStatus ==
NULL)
2945 return HAL_SD_ERROR_PARAM;
2949 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2950 if(errorstate != HAL_SD_ERROR_NONE)
2956 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2958 return HAL_SD_ERROR_NONE;
2966 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2968 uint32_t scr[2U] = {0U, 0U};
2969 uint32_t errorstate;
2971 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2973 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2977 errorstate = SD_FindSCR(hsd, scr);
2978 if(errorstate != HAL_SD_ERROR_NONE)
2984 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
2987 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2988 if(errorstate != HAL_SD_ERROR_NONE)
2994 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
2995 if(errorstate != HAL_SD_ERROR_NONE)
3000 return HAL_SD_ERROR_NONE;
3004 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3013 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3015 uint32_t scr[2U] = {0U, 0U};
3016 uint32_t errorstate;
3018 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3020 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3024 errorstate = SD_FindSCR(hsd, scr);
3025 if(errorstate != HAL_SD_ERROR_NONE)
3031 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3034 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3035 if(errorstate != HAL_SD_ERROR_NONE)
3041 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3042 if(errorstate != HAL_SD_ERROR_NONE)
3047 return HAL_SD_ERROR_NONE;
3051 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3062 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3064 SDMMC_DataInitTypeDef
config;
3065 uint32_t errorstate;
3067 uint32_t index = 0U;
3068 uint32_t tempscr[2U] = {0U, 0U};
3069 uint32_t *scr = pSCR;
3072 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3073 if(errorstate != HAL_SD_ERROR_NONE)
3079 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3080 if(errorstate != HAL_SD_ERROR_NONE)
3085 config.DataTimeOut = SDMMC_DATATIMEOUT;
3087 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3088 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3089 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3090 config.DPSM = SDMMC_DPSM_ENABLE;
3091 (void)SDMMC_ConfigData(hsd->Instance, &
config);
3094 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3095 if(errorstate != HAL_SD_ERROR_NONE)
3100 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3102 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3104 *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3108 if((
HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3110 return HAL_SD_ERROR_TIMEOUT;
3114 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3116 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3118 return HAL_SD_ERROR_DATA_TIMEOUT;
3120 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3122 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3124 return HAL_SD_ERROR_DATA_CRC_FAIL;
3126 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3128 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3130 return HAL_SD_ERROR_RX_OVERRUN;
3136 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3138 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3139 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3141 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3142 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3146 return HAL_SD_ERROR_NONE;
3155 static void SD_Read_IT(SD_HandleTypeDef *hsd)
3157 uint32_t
count, data, dataremaining;
3160 tmp = hsd->pRxBuffPtr;
3161 dataremaining = hsd->RxXferSize;
3163 if (dataremaining > 0U)
3168 data = SDMMC_ReadFIFO(hsd->Instance);
3169 *tmp = (uint8_t)(data & 0xFFU);
3172 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3175 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3178 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3183 hsd->pRxBuffPtr = tmp;
3184 hsd->RxXferSize = dataremaining;
3194 static void SD_Write_IT(SD_HandleTypeDef *hsd)
3196 uint32_t
count, data, dataremaining;
3199 tmp = hsd->pTxBuffPtr;
3200 dataremaining = hsd->TxXferSize;
3202 if (dataremaining > 0U)
3207 data = (uint32_t)(*tmp);
3210 data |= ((uint32_t)(*tmp) << 8U);
3213 data |= ((uint32_t)(*tmp) << 16U);
3216 data |= ((uint32_t)(*tmp) << 24U);
3219 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
3222 hsd->pTxBuffPtr = tmp;
3223 hsd->TxXferSize = dataremaining;