stm32f7xx_hal_sd.c
Go to the documentation of this file.
1 
258 /* Includes ------------------------------------------------------------------*/
259 #include "stm32f7xx_hal.h"
260 
261 #if defined(SDMMC1)
262 
271 #ifdef HAL_SD_MODULE_ENABLED
272 
273 /* Private typedef -----------------------------------------------------------*/
274 /* Private define ------------------------------------------------------------*/
283 /* Private macro -------------------------------------------------------------*/
284 /* Private variables ---------------------------------------------------------*/
285 /* Private function prototypes -----------------------------------------------*/
286 /* Private functions ---------------------------------------------------------*/
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);
300 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
301 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
302 static void SD_DMAError(DMA_HandleTypeDef *hdma);
303 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
304 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
309 /* Exported functions --------------------------------------------------------*/
335 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
336 {
337  /* Check the SD handle allocation */
338  if(hsd == NULL)
339  {
340  return HAL_ERROR;
341  }
342 
343  /* Check the parameters */
344  assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
345  assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
346  assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
347  assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
348  assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
349  assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
350  assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
351 
352  if(hsd->State == HAL_SD_STATE_RESET)
353  {
354  /* Allocate lock resource and initialize it */
355  hsd->Lock = HAL_UNLOCKED;
356 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
357  /* Reset Callback pointers in HAL_SD_STATE_RESET only */
358  hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
359  hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
360  hsd->ErrorCallback = HAL_SD_ErrorCallback;
361  hsd->AbortCpltCallback = HAL_SD_AbortCallback;
362 
363  if(hsd->MspInitCallback == NULL)
364  {
365  hsd->MspInitCallback = HAL_SD_MspInit;
366  }
367 
368  /* Init the low level hardware */
369  hsd->MspInitCallback(hsd);
370 #else
371  /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
372  HAL_SD_MspInit(hsd);
373 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
374  }
375 
376  hsd->State = HAL_SD_STATE_BUSY;
377 
378  /* Initialize the Card parameters */
379  if (HAL_SD_InitCard(hsd) != HAL_OK)
380  {
381  return HAL_ERROR;
382  }
383 
384  /* Initialize the error code */
385  hsd->ErrorCode = HAL_SD_ERROR_NONE;
386 
387  /* Initialize the SD operation */
388  hsd->Context = SD_CONTEXT_NONE;
389 
390  /* Initialize the SD state */
391  hsd->State = HAL_SD_STATE_READY;
392 
393  return HAL_OK;
394 }
395 
403 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
404 {
405  uint32_t errorstate;
406  HAL_StatusTypeDef status;
407  SD_InitTypeDef Init;
408 
409  /* Default SDMMC peripheral configuration for SD card initialization */
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;
416 
417  /* Initialize SDMMC peripheral interface with default configuration */
418  status = SDMMC_Init(hsd->Instance, Init);
419  if(status != HAL_OK)
420  {
421  return HAL_ERROR;
422  }
423 
424  /* Disable SDMMC Clock */
425  __HAL_SD_DISABLE(hsd);
426 
427  /* Set Power State to ON */
428  (void)SDMMC_PowerState_ON(hsd->Instance);
429 
430  /* Enable SDMMC Clock */
431  __HAL_SD_ENABLE(hsd);
432 
433  /* Identify card operating voltage */
434  errorstate = SD_PowerON(hsd);
435  if(errorstate != HAL_SD_ERROR_NONE)
436  {
437  hsd->State = HAL_SD_STATE_READY;
438  hsd->ErrorCode |= errorstate;
439  return HAL_ERROR;
440  }
441 
442  /* Card initialization */
443  errorstate = SD_InitCard(hsd);
444  if(errorstate != HAL_SD_ERROR_NONE)
445  {
446  hsd->State = HAL_SD_STATE_READY;
447  hsd->ErrorCode |= errorstate;
448  return HAL_ERROR;
449  }
450 
451  return HAL_OK;
452 }
453 
459 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
460 {
461  /* Check the SD handle allocation */
462  if(hsd == NULL)
463  {
464  return HAL_ERROR;
465  }
466 
467  /* Check the parameters */
468  assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
469 
470  hsd->State = HAL_SD_STATE_BUSY;
471 
472  /* Set SD power state to off */
473  SD_PowerOFF(hsd);
474 
475 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
476  if(hsd->MspDeInitCallback == NULL)
477  {
478  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
479  }
480 
481  /* DeInit the low level hardware */
482  hsd->MspDeInitCallback(hsd);
483 #else
484  /* De-Initialize the MSP layer */
485  HAL_SD_MspDeInit(hsd);
486 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
487 
488  hsd->ErrorCode = HAL_SD_ERROR_NONE;
489  hsd->State = HAL_SD_STATE_RESET;
490 
491  return HAL_OK;
492 }
493 
494 
500 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
501 {
502  /* Prevent unused argument(s) compilation warning */
503  UNUSED(hsd);
504 
505  /* NOTE : This function should not be modified, when the callback is needed,
506  the HAL_SD_MspInit could be implemented in the user file
507  */
508 }
509 
515 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
516 {
517  /* Prevent unused argument(s) compilation warning */
518  UNUSED(hsd);
519 
520  /* NOTE : This function should not be modified, when the callback is needed,
521  the HAL_SD_MspDeInit could be implemented in the user file
522  */
523 }
524 
556 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
557 {
558  SDMMC_DataInitTypeDef config;
559  uint32_t errorstate;
560  uint32_t tickstart = HAL_GetTick();
561  uint32_t count, data, dataremaining;
562  uint32_t add = BlockAdd;
563  uint8_t *tempbuff = pData;
564 
565  if(NULL == pData)
566  {
567  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
568  return HAL_ERROR;
569  }
570 
571  if(hsd->State == HAL_SD_STATE_READY)
572  {
573  hsd->ErrorCode = HAL_SD_ERROR_NONE;
574 
575  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
576  {
577  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
578  return HAL_ERROR;
579  }
580 
581  hsd->State = HAL_SD_STATE_BUSY;
582 
583  /* Initialize data control register */
584  hsd->Instance->DCTRL = 0U;
585 
586  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
587  {
588  add *= 512U;
589  }
590 
591  /* Set Block Size for Card */
592  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
593  if(errorstate != HAL_SD_ERROR_NONE)
594  {
595  /* Clear all the static flags */
596  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
597  hsd->ErrorCode |= errorstate;
598  hsd->State = HAL_SD_STATE_READY;
599  return HAL_ERROR;
600  }
601 
602  /* Configure the SD DPSM (Data Path State Machine) */
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);
610 
611  /* Read block(s) in polling mode */
612  if(NumberOfBlocks > 1U)
613  {
614  hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
615 
616  /* Read Multi Block command */
617  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
618  }
619  else
620  {
621  hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
622 
623  /* Read Single Block command */
624  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
625  }
626  if(errorstate != HAL_SD_ERROR_NONE)
627  {
628  /* Clear all the static flags */
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;
633  return HAL_ERROR;
634  }
635 
636  /* Poll on SDMMC flags */
637  dataremaining = config.DataLength;
638  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
639  {
640  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
641  {
642  /* Read data from SDMMC Rx FIFO */
643  for(count = 0U; count < 8U; count++)
644  {
645  data = SDMMC_ReadFIFO(hsd->Instance);
646  *tempbuff = (uint8_t)(data & 0xFFU);
647  tempbuff++;
648  dataremaining--;
649  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
650  tempbuff++;
651  dataremaining--;
652  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
653  tempbuff++;
654  dataremaining--;
655  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
656  tempbuff++;
657  dataremaining--;
658  }
659  }
660 
661  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
662  {
663  /* Clear all the static flags */
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;
668  return HAL_TIMEOUT;
669  }
670  }
671 
672  /* Send stop transmission command in case of multiblock read */
673  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
674  {
675  if(hsd->SdCard.CardType != CARD_SECURED)
676  {
677  /* Send stop transmission command */
678  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
679  if(errorstate != HAL_SD_ERROR_NONE)
680  {
681  /* Clear all the static flags */
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;
686  return HAL_ERROR;
687  }
688  }
689  }
690 
691  /* Get error state */
692  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
693  {
694  /* Clear all the static flags */
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;
699  return HAL_ERROR;
700  }
701  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
702  {
703  /* Clear all the static flags */
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;
708  return HAL_ERROR;
709  }
710  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
711  {
712  /* Clear all the static flags */
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;
717  return HAL_ERROR;
718  }
719  else
720  {
721  /* Nothing to do */
722  }
723 
724  /* Empty FIFO if there is still any data */
725  while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
726  {
727  data = SDMMC_ReadFIFO(hsd->Instance);
728  *tempbuff = (uint8_t)(data & 0xFFU);
729  tempbuff++;
730  dataremaining--;
731  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
732  tempbuff++;
733  dataremaining--;
734  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
735  tempbuff++;
736  dataremaining--;
737  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
738  tempbuff++;
739  dataremaining--;
740 
741  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
742  {
743  /* Clear all the static flags */
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;
748  return HAL_ERROR;
749  }
750  }
751 
752  /* Clear all the static flags */
753  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
754 
755  hsd->State = HAL_SD_STATE_READY;
756 
757  return HAL_OK;
758  }
759  else
760  {
761  hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
762  return HAL_ERROR;
763  }
764 }
765 
778 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
779 {
780  SDMMC_DataInitTypeDef config;
781  uint32_t errorstate;
782  uint32_t tickstart = HAL_GetTick();
783  uint32_t count, data, dataremaining;
784  uint32_t add = BlockAdd;
785  uint8_t *tempbuff = pData;
786 
787  if(NULL == pData)
788  {
789  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
790  return HAL_ERROR;
791  }
792 
793  if(hsd->State == HAL_SD_STATE_READY)
794  {
795  hsd->ErrorCode = HAL_SD_ERROR_NONE;
796 
797  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
798  {
799  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
800  return HAL_ERROR;
801  }
802 
803  hsd->State = HAL_SD_STATE_BUSY;
804 
805  /* Initialize data control register */
806  hsd->Instance->DCTRL = 0U;
807 
808  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
809  {
810  add *= 512U;
811  }
812 
813  /* Set Block Size for Card */
814  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
815  if(errorstate != HAL_SD_ERROR_NONE)
816  {
817  /* Clear all the static flags */
818  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
819  hsd->ErrorCode |= errorstate;
820  hsd->State = HAL_SD_STATE_READY;
821  return HAL_ERROR;
822  }
823 
824  /* Configure the SD DPSM (Data Path State Machine) */
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);
832 
833  /* Write Blocks in Polling mode */
834  if(NumberOfBlocks > 1U)
835  {
836  hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
837 
838  /* Write Multi Block command */
839  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
840  }
841  else
842  {
843  hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
844 
845  /* Write Single Block command */
846  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
847  }
848  if(errorstate != HAL_SD_ERROR_NONE)
849  {
850  /* Clear all the static flags */
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;
855  return HAL_ERROR;
856  }
857 
858  /* Write block(s) in polling mode */
859  dataremaining = config.DataLength;
860  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
861  {
862  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
863  {
864  /* Write data to SDMMC Tx FIFO */
865  for(count = 0U; count < 8U; count++)
866  {
867  data = (uint32_t)(*tempbuff);
868  tempbuff++;
869  dataremaining--;
870  data |= ((uint32_t)(*tempbuff) << 8U);
871  tempbuff++;
872  dataremaining--;
873  data |= ((uint32_t)(*tempbuff) << 16U);
874  tempbuff++;
875  dataremaining--;
876  data |= ((uint32_t)(*tempbuff) << 24U);
877  tempbuff++;
878  dataremaining--;
879  (void)SDMMC_WriteFIFO(hsd->Instance, &data);
880  }
881  }
882 
883  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
884  {
885  /* Clear all the static flags */
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;
890  return HAL_TIMEOUT;
891  }
892  }
893 
894  /* Send stop transmission command in case of multiblock write */
895  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
896  {
897  if(hsd->SdCard.CardType != CARD_SECURED)
898  {
899  /* Send stop transmission command */
900  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
901  if(errorstate != HAL_SD_ERROR_NONE)
902  {
903  /* Clear all the static flags */
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;
908  return HAL_ERROR;
909  }
910  }
911  }
912 
913  /* Get error state */
914  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
915  {
916  /* Clear all the static flags */
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;
921  return HAL_ERROR;
922  }
923  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
924  {
925  /* Clear all the static flags */
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;
930  return HAL_ERROR;
931  }
932  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
933  {
934  /* Clear all the static flags */
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;
939  return HAL_ERROR;
940  }
941  else
942  {
943  /* Nothing to do */
944  }
945 
946  /* Clear all the static flags */
947  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
948 
949  hsd->State = HAL_SD_STATE_READY;
950 
951  return HAL_OK;
952  }
953  else
954  {
955  hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
956  return HAL_ERROR;
957  }
958 }
959 
973 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
974 {
975  SDMMC_DataInitTypeDef config;
976  uint32_t errorstate;
977  uint32_t add = BlockAdd;
978 
979  if(NULL == pData)
980  {
981  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
982  return HAL_ERROR;
983  }
984 
985  if(hsd->State == HAL_SD_STATE_READY)
986  {
987  hsd->ErrorCode = HAL_SD_ERROR_NONE;
988 
989  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
990  {
991  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
992  return HAL_ERROR;
993  }
994 
995  hsd->State = HAL_SD_STATE_BUSY;
996 
997  /* Initialize data control register */
998  hsd->Instance->DCTRL = 0U;
999 
1000  hsd->pRxBuffPtr = pData;
1001  hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1002 
1003  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1004 
1005  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1006  {
1007  add *= 512U;
1008  }
1009 
1010  /* Set Block Size for Card */
1011  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1012  if(errorstate != HAL_SD_ERROR_NONE)
1013  {
1014  /* Clear all the static flags */
1015  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1016  hsd->ErrorCode |= errorstate;
1017  hsd->State = HAL_SD_STATE_READY;
1018  return HAL_ERROR;
1019  }
1020 
1021  /* Configure the SD DPSM (Data Path State Machine) */
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);
1029 
1030  /* Read Blocks in IT mode */
1031  if(NumberOfBlocks > 1U)
1032  {
1033  hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1034 
1035  /* Read Multi Block command */
1036  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1037  }
1038  else
1039  {
1040  hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1041 
1042  /* Read Single Block command */
1043  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1044  }
1045  if(errorstate != HAL_SD_ERROR_NONE)
1046  {
1047  /* Clear all the static flags */
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;
1052  return HAL_ERROR;
1053  }
1054 
1055  return HAL_OK;
1056  }
1057  else
1058  {
1059  return HAL_BUSY;
1060  }
1061 }
1062 
1076 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1077 {
1078  SDMMC_DataInitTypeDef config;
1079  uint32_t errorstate;
1080  uint32_t add = BlockAdd;
1081 
1082  if(NULL == pData)
1083  {
1084  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1085  return HAL_ERROR;
1086  }
1087 
1088  if(hsd->State == HAL_SD_STATE_READY)
1089  {
1090  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1091 
1092  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1093  {
1094  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1095  return HAL_ERROR;
1096  }
1097 
1098  hsd->State = HAL_SD_STATE_BUSY;
1099 
1100  /* Initialize data control register */
1101  hsd->Instance->DCTRL = 0U;
1102 
1103  hsd->pTxBuffPtr = pData;
1104  hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1105 
1106  /* Enable transfer interrupts */
1107  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1108 
1109  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1110  {
1111  add *= 512U;
1112  }
1113 
1114  /* Set Block Size for Card */
1115  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1116  if(errorstate != HAL_SD_ERROR_NONE)
1117  {
1118  /* Clear all the static flags */
1119  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1120  hsd->ErrorCode |= errorstate;
1121  hsd->State = HAL_SD_STATE_READY;
1122  return HAL_ERROR;
1123  }
1124 
1125  /* Write Blocks in Polling mode */
1126  if(NumberOfBlocks > 1U)
1127  {
1128  hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1129 
1130  /* Write Multi Block command */
1131  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1132  }
1133  else
1134  {
1135  hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1136 
1137  /* Write Single Block command */
1138  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1139  }
1140  if(errorstate != HAL_SD_ERROR_NONE)
1141  {
1142  /* Clear all the static flags */
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;
1147  return HAL_ERROR;
1148  }
1149 
1150  /* Configure the SD DPSM (Data Path State Machine) */
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);
1158 
1159  return HAL_OK;
1160  }
1161  else
1162  {
1163  return HAL_BUSY;
1164  }
1165 }
1166 
1180 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1181 {
1182  SDMMC_DataInitTypeDef config;
1183  uint32_t errorstate;
1184  uint32_t add = BlockAdd;
1185 
1186  if(NULL == pData)
1187  {
1188  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1189  return HAL_ERROR;
1190  }
1191 
1192  if(hsd->State == HAL_SD_STATE_READY)
1193  {
1194  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1195 
1196  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1197  {
1198  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1199  return HAL_ERROR;
1200  }
1201 
1202  hsd->State = HAL_SD_STATE_BUSY;
1203 
1204  /* Initialize data control register */
1205  hsd->Instance->DCTRL = 0U;
1206 
1207  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1208 
1209  /* Set the DMA transfer complete callback */
1210  hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1211 
1212  /* Set the DMA error callback */
1213  hsd->hdmarx->XferErrorCallback = SD_DMAError;
1214 
1215  /* Set the DMA Abort callback */
1216  hsd->hdmarx->XferAbortCallback = NULL;
1217 
1218  /* Enable the DMA Channel */
1219  if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1220  {
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;
1225  return HAL_ERROR;
1226  }
1227  else
1228  {
1229  /* Enable SD DMA transfer */
1230  __HAL_SD_DMA_ENABLE(hsd);
1231 
1232  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1233  {
1234  add *= 512U;
1235  }
1236 
1237  /* Set Block Size for Card */
1238  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1239  if(errorstate != HAL_SD_ERROR_NONE)
1240  {
1241  /* Clear all the static flags */
1242  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1243  hsd->ErrorCode |= errorstate;
1244  hsd->State = HAL_SD_STATE_READY;
1245  return HAL_ERROR;
1246  }
1247 
1248  /* Configure the SD DPSM (Data Path State Machine) */
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);
1256 
1257  /* Read Blocks in DMA mode */
1258  if(NumberOfBlocks > 1U)
1259  {
1260  hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1261 
1262  /* Read Multi Block command */
1263  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1264  }
1265  else
1266  {
1267  hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1268 
1269  /* Read Single Block command */
1270  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1271  }
1272  if(errorstate != HAL_SD_ERROR_NONE)
1273  {
1274  /* Clear all the static flags */
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;
1279  return HAL_ERROR;
1280  }
1281 
1282  return HAL_OK;
1283  }
1284  }
1285  else
1286  {
1287  return HAL_BUSY;
1288  }
1289 }
1290 
1304 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1305 {
1306  SDMMC_DataInitTypeDef config;
1307  uint32_t errorstate;
1308  uint32_t add = BlockAdd;
1309 
1310  if(NULL == pData)
1311  {
1312  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1313  return HAL_ERROR;
1314  }
1315 
1316  if(hsd->State == HAL_SD_STATE_READY)
1317  {
1318  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1319 
1320  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1321  {
1322  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1323  return HAL_ERROR;
1324  }
1325 
1326  hsd->State = HAL_SD_STATE_BUSY;
1327 
1328  /* Initialize data control register */
1329  hsd->Instance->DCTRL = 0U;
1330 
1331  /* Enable SD Error interrupts */
1332  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1333 
1334  /* Set the DMA transfer complete callback */
1335  hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1336 
1337  /* Set the DMA error callback */
1338  hsd->hdmatx->XferErrorCallback = SD_DMAError;
1339 
1340  /* Set the DMA Abort callback */
1341  hsd->hdmatx->XferAbortCallback = NULL;
1342 
1343  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1344  {
1345  add *= 512U;
1346  }
1347 
1348  /* Set Block Size for Card */
1349  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1350  if(errorstate != HAL_SD_ERROR_NONE)
1351  {
1352  /* Clear all the static flags */
1353  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1354  hsd->ErrorCode |= errorstate;
1355  hsd->State = HAL_SD_STATE_READY;
1356  return HAL_ERROR;
1357  }
1358 
1359  /* Write Blocks in Polling mode */
1360  if(NumberOfBlocks > 1U)
1361  {
1362  hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1363 
1364  /* Write Multi Block command */
1365  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1366  }
1367  else
1368  {
1369  hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1370 
1371  /* Write Single Block command */
1372  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1373  }
1374  if(errorstate != HAL_SD_ERROR_NONE)
1375  {
1376  /* Clear all the static flags */
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;
1381  return HAL_ERROR;
1382  }
1383 
1384  /* Enable SDMMC DMA transfer */
1385  __HAL_SD_DMA_ENABLE(hsd);
1386 
1387  /* Enable the DMA Channel */
1388  if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1389  {
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;
1395  return HAL_ERROR;
1396  }
1397  else
1398  {
1399  /* Configure the SD DPSM (Data Path State Machine) */
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);
1407 
1408  return HAL_OK;
1409  }
1410  }
1411  else
1412  {
1413  return HAL_BUSY;
1414  }
1415 }
1416 
1426 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1427 {
1428  uint32_t errorstate;
1429  uint32_t start_add = BlockStartAdd;
1430  uint32_t end_add = BlockEndAdd;
1431 
1432  if(hsd->State == HAL_SD_STATE_READY)
1433  {
1434  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1435 
1436  if(end_add < start_add)
1437  {
1438  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1439  return HAL_ERROR;
1440  }
1441 
1442  if(end_add > (hsd->SdCard.LogBlockNbr))
1443  {
1444  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1445  return HAL_ERROR;
1446  }
1447 
1448  hsd->State = HAL_SD_STATE_BUSY;
1449 
1450  /* Check if the card command class supports erase command */
1451  if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1452  {
1453  /* Clear all the static flags */
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;
1457  return HAL_ERROR;
1458  }
1459 
1460  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1461  {
1462  /* Clear all the static flags */
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;
1466  return HAL_ERROR;
1467  }
1468 
1469  /* Get start and end block for high capacity cards */
1470  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1471  {
1472  start_add *= 512U;
1473  end_add *= 512U;
1474  }
1475 
1476  /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1477  if(hsd->SdCard.CardType != CARD_SECURED)
1478  {
1479  /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1480  errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1481  if(errorstate != HAL_SD_ERROR_NONE)
1482  {
1483  /* Clear all the static flags */
1484  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1485  hsd->ErrorCode |= errorstate;
1486  hsd->State = HAL_SD_STATE_READY;
1487  return HAL_ERROR;
1488  }
1489 
1490  /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1491  errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1492  if(errorstate != HAL_SD_ERROR_NONE)
1493  {
1494  /* Clear all the static flags */
1495  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1496  hsd->ErrorCode |= errorstate;
1497  hsd->State = HAL_SD_STATE_READY;
1498  return HAL_ERROR;
1499  }
1500  }
1501 
1502  /* Send CMD38 ERASE */
1503  errorstate = SDMMC_CmdErase(hsd->Instance);
1504  if(errorstate != HAL_SD_ERROR_NONE)
1505  {
1506  /* Clear all the static flags */
1507  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1508  hsd->ErrorCode |= errorstate;
1509  hsd->State = HAL_SD_STATE_READY;
1510  return HAL_ERROR;
1511  }
1512 
1513  hsd->State = HAL_SD_STATE_READY;
1514 
1515  return HAL_OK;
1516  }
1517  else
1518  {
1519  return HAL_BUSY;
1520  }
1521 }
1522 
1528 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1529 {
1530  uint32_t errorstate;
1531  uint32_t context = hsd->Context;
1532 
1533  /* Check for SDMMC interrupt flags */
1534  if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1535  {
1536  SD_Read_IT(hsd);
1537  }
1538 
1539  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1540  {
1541  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1542 
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 |\
1545  SDMMC_IT_RXFIFOHF);
1546 
1547  hsd->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
1548 
1549  if((context & SD_CONTEXT_IT) != 0U)
1550  {
1551  if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1552  {
1553  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1554  if(errorstate != HAL_SD_ERROR_NONE)
1555  {
1556  hsd->ErrorCode |= errorstate;
1557 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1558  hsd->ErrorCallback(hsd);
1559 #else
1560  HAL_SD_ErrorCallback(hsd);
1561 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1562  }
1563  }
1564 
1565  /* Clear all the static flags */
1566  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1567 
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))
1571  {
1572 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1573  hsd->RxCpltCallback(hsd);
1574 #else
1575  HAL_SD_RxCpltCallback(hsd);
1576 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1577  }
1578  else
1579  {
1580 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1581  hsd->TxCpltCallback(hsd);
1582 #else
1583  HAL_SD_TxCpltCallback(hsd);
1584 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1585  }
1586  }
1587  else if((context & SD_CONTEXT_DMA) != 0U)
1588  {
1589  if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1590  {
1591  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1592  if(errorstate != HAL_SD_ERROR_NONE)
1593  {
1594  hsd->ErrorCode |= errorstate;
1595 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1596  hsd->ErrorCallback(hsd);
1597 #else
1598  HAL_SD_ErrorCallback(hsd);
1599 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1600  }
1601  }
1602  if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1603  {
1604  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1605  in the SD DCTRL register */
1606  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1607 
1608  hsd->State = HAL_SD_STATE_READY;
1609 
1610 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1611  hsd->TxCpltCallback(hsd);
1612 #else
1613  HAL_SD_TxCpltCallback(hsd);
1614 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1615  }
1616  }
1617  else
1618  {
1619  /* Nothing to do */
1620  }
1621  }
1622 
1623  else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1624  {
1625  SD_Write_IT(hsd);
1626  }
1627 
1628  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1629  {
1630  /* Set Error code */
1631  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL) != RESET)
1632  {
1633  hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1634  }
1635  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT) != RESET)
1636  {
1637  hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1638  }
1639  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR) != RESET)
1640  {
1641  hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1642  }
1643  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR) != RESET)
1644  {
1645  hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1646  }
1647 
1648  /* Clear All flags */
1649  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1650 
1651  /* Disable all interrupts */
1652  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1653  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1654 
1655  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1656 
1657  if((context & SD_CONTEXT_IT) != 0U)
1658  {
1659  /* Set the SD state to ready to be able to start again the process */
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);
1664 #else
1665  HAL_SD_ErrorCallback(hsd);
1666 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1667  }
1668  else if((context & SD_CONTEXT_DMA) != 0U)
1669  {
1670  /* Abort the SD DMA channel */
1671  if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1672  {
1673  /* Set the DMA Tx abort callback */
1674  hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1675  /* Abort DMA in IT mode */
1676  if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1677  {
1678  SD_DMATxAbort(hsd->hdmatx);
1679  }
1680  }
1681  else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1682  {
1683  /* Set the DMA Rx abort callback */
1684  hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1685  /* Abort DMA in IT mode */
1686  if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1687  {
1688  SD_DMARxAbort(hsd->hdmarx);
1689  }
1690  }
1691  else
1692  {
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);
1698 #else
1699  HAL_SD_AbortCallback(hsd);
1700 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1701  }
1702  }
1703  else
1704  {
1705  /* Nothing to do */
1706  }
1707  }
1708  else
1709  {
1710  /* Nothing to do */
1711  }
1712 }
1713 
1719 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1720 {
1721  return hsd->State;
1722 }
1723 
1730 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1731 {
1732  return hsd->ErrorCode;
1733 }
1734 
1740 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1741 {
1742  /* Prevent unused argument(s) compilation warning */
1743  UNUSED(hsd);
1744 
1745  /* NOTE : This function should not be modified, when the callback is needed,
1746  the HAL_SD_TxCpltCallback can be implemented in the user file
1747  */
1748 }
1749 
1755 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1756 {
1757  /* Prevent unused argument(s) compilation warning */
1758  UNUSED(hsd);
1759 
1760  /* NOTE : This function should not be modified, when the callback is needed,
1761  the HAL_SD_RxCpltCallback can be implemented in the user file
1762  */
1763 }
1764 
1770 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1771 {
1772  /* Prevent unused argument(s) compilation warning */
1773  UNUSED(hsd);
1774 
1775  /* NOTE : This function should not be modified, when the callback is needed,
1776  the HAL_SD_ErrorCallback can be implemented in the user file
1777  */
1778 }
1779 
1785 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1786 {
1787  /* Prevent unused argument(s) compilation warning */
1788  UNUSED(hsd);
1789 
1790  /* NOTE : This function should not be modified, when the callback is needed,
1791  the HAL_SD_AbortCallback can be implemented in the user file
1792  */
1793 }
1794 
1795 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1796 
1811 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1812 {
1813  HAL_StatusTypeDef status = HAL_OK;
1814 
1815  if(pCallback == NULL)
1816  {
1817  /* Update the error code */
1818  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1819  return HAL_ERROR;
1820  }
1821 
1822  /* Process locked */
1823  __HAL_LOCK(hsd);
1824 
1825  if(hsd->State == HAL_SD_STATE_READY)
1826  {
1827  switch (CallbackID)
1828  {
1829  case HAL_SD_TX_CPLT_CB_ID :
1830  hsd->TxCpltCallback = pCallback;
1831  break;
1832  case HAL_SD_RX_CPLT_CB_ID :
1833  hsd->RxCpltCallback = pCallback;
1834  break;
1835  case HAL_SD_ERROR_CB_ID :
1836  hsd->ErrorCallback = pCallback;
1837  break;
1838  case HAL_SD_ABORT_CB_ID :
1839  hsd->AbortCpltCallback = pCallback;
1840  break;
1841  case HAL_SD_MSP_INIT_CB_ID :
1842  hsd->MspInitCallback = pCallback;
1843  break;
1844  case HAL_SD_MSP_DEINIT_CB_ID :
1845  hsd->MspDeInitCallback = pCallback;
1846  break;
1847  default :
1848  /* Update the error code */
1849  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1850  /* update return status */
1851  status = HAL_ERROR;
1852  break;
1853  }
1854  }
1855  else if (hsd->State == HAL_SD_STATE_RESET)
1856  {
1857  switch (CallbackID)
1858  {
1859  case HAL_SD_MSP_INIT_CB_ID :
1860  hsd->MspInitCallback = pCallback;
1861  break;
1862  case HAL_SD_MSP_DEINIT_CB_ID :
1863  hsd->MspDeInitCallback = pCallback;
1864  break;
1865  default :
1866  /* Update the error code */
1867  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1868  /* update return status */
1869  status = HAL_ERROR;
1870  break;
1871  }
1872  }
1873  else
1874  {
1875  /* Update the error code */
1876  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1877  /* update return status */
1878  status = HAL_ERROR;
1879  }
1880 
1881  /* Release Lock */
1882  __HAL_UNLOCK(hsd);
1883  return status;
1884 }
1885 
1900 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1901 {
1902  HAL_StatusTypeDef status = HAL_OK;
1903 
1904  /* Process locked */
1905  __HAL_LOCK(hsd);
1906 
1907  if(hsd->State == HAL_SD_STATE_READY)
1908  {
1909  switch (CallbackID)
1910  {
1911  case HAL_SD_TX_CPLT_CB_ID :
1912  hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1913  break;
1914  case HAL_SD_RX_CPLT_CB_ID :
1915  hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1916  break;
1917  case HAL_SD_ERROR_CB_ID :
1918  hsd->ErrorCallback = HAL_SD_ErrorCallback;
1919  break;
1920  case HAL_SD_ABORT_CB_ID :
1921  hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1922  break;
1923  case HAL_SD_MSP_INIT_CB_ID :
1924  hsd->MspInitCallback = HAL_SD_MspInit;
1925  break;
1926  case HAL_SD_MSP_DEINIT_CB_ID :
1927  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1928  break;
1929  default :
1930  /* Update the error code */
1931  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1932  /* update return status */
1933  status = HAL_ERROR;
1934  break;
1935  }
1936  }
1937  else if (hsd->State == HAL_SD_STATE_RESET)
1938  {
1939  switch (CallbackID)
1940  {
1941  case HAL_SD_MSP_INIT_CB_ID :
1942  hsd->MspInitCallback = HAL_SD_MspInit;
1943  break;
1944  case HAL_SD_MSP_DEINIT_CB_ID :
1945  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1946  break;
1947  default :
1948  /* Update the error code */
1949  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1950  /* update return status */
1951  status = HAL_ERROR;
1952  break;
1953  }
1954  }
1955  else
1956  {
1957  /* Update the error code */
1958  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1959  /* update return status */
1960  status = HAL_ERROR;
1961  }
1962 
1963  /* Release Lock */
1964  __HAL_UNLOCK(hsd);
1965  return status;
1966 }
1967 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1968 
1996 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
1997 {
1998  pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
1999 
2000  pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2001 
2002  pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2003 
2004  pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2005 
2006  pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2007 
2008  pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2009 
2010  pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2011 
2012  pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2013 
2014  pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2015 
2016  pCID->Reserved2 = 1U;
2017 
2018  return HAL_OK;
2019 }
2020 
2029 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2030 {
2031  pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2032 
2033  pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2034 
2035  pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2036 
2037  pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2038 
2039  pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2040 
2041  pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2042 
2043  pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2044 
2045  pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2046 
2047  pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2048 
2049  pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2050 
2051  pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2052 
2053  pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2054 
2055  pCSD->Reserved2 = 0U;
2057  if(hsd->SdCard.CardType == CARD_SDSC)
2058  {
2059  pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2060 
2061  pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2062 
2063  pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2064 
2065  pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2066 
2067  pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2068 
2069  pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2070 
2071  hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2072  hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2073  hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2074 
2075  hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2076  hsd->SdCard.LogBlockSize = 512U;
2077  }
2078  else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2079  {
2080  /* Byte 7 */
2081  pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2082 
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;
2087  }
2088  else
2089  {
2090  /* Clear all the static flags */
2091  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2092  hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2093  hsd->State = HAL_SD_STATE_READY;
2094  return HAL_ERROR;
2095  }
2096 
2097  pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2098 
2099  pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2100 
2101  pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2102 
2103  pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2104 
2105  pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2106 
2107  pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2108 
2109  pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2110 
2111  pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2112 
2113  pCSD->Reserved3 = 0;
2114 
2115  pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2116 
2117  pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2118 
2119  pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2120 
2121  pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2122 
2123  pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2124 
2125  pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2126 
2127  pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2128 
2129  pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2130 
2131  pCSD->Reserved4 = 1;
2132 
2133  return HAL_OK;
2134 }
2135 
2143 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2144 {
2145  uint32_t sd_status[16];
2146  uint32_t errorstate;
2147 
2148  errorstate = SD_SendSDStatus(hsd, sd_status);
2149  if(errorstate != HAL_SD_ERROR_NONE)
2150  {
2151  /* Clear all the static flags */
2152  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2153  hsd->ErrorCode |= errorstate;
2154  hsd->State = HAL_SD_STATE_READY;
2155  return HAL_ERROR;
2156  }
2157  else
2158  {
2159  pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2160 
2161  pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2162 
2163  pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2164 
2165  pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2166  ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2167 
2168  pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2169 
2170  pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2171 
2172  pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2173 
2174  pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2175 
2176  pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2177 
2178  pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2179  }
2180 
2181  return HAL_OK;
2182 }
2183 
2191 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2192 {
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);
2201 
2202  return HAL_OK;
2203 }
2204 
2216 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2217 {
2218  SDMMC_InitTypeDef Init;
2219  uint32_t errorstate;
2220 
2221  /* Check the parameters */
2222  assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2223 
2224  /* Change State */
2225  hsd->State = HAL_SD_STATE_BUSY;
2226 
2227  if(hsd->SdCard.CardType != CARD_SECURED)
2228  {
2229  if(WideMode == SDMMC_BUS_WIDE_8B)
2230  {
2231  hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2232  }
2233  else if(WideMode == SDMMC_BUS_WIDE_4B)
2234  {
2235  errorstate = SD_WideBus_Enable(hsd);
2236 
2237  hsd->ErrorCode |= errorstate;
2238  }
2239  else if(WideMode == SDMMC_BUS_WIDE_1B)
2240  {
2241  errorstate = SD_WideBus_Disable(hsd);
2242 
2243  hsd->ErrorCode |= errorstate;
2244  }
2245  else
2246  {
2247  /* WideMode is not a valid argument*/
2248  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2249  }
2250  }
2251  else
2252  {
2253  /* MMC Card does not support this feature */
2254  hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2255  }
2256 
2257  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2258  {
2259  /* Clear all the static flags */
2260  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2261  hsd->State = HAL_SD_STATE_READY;
2262  return HAL_ERROR;
2263  }
2264  else
2265  {
2266  /* Configure the SDMMC peripheral */
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);
2274  }
2275 
2276  /* Change State */
2277  hsd->State = HAL_SD_STATE_READY;
2278 
2279  return HAL_OK;
2280 }
2281 
2287 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2288 {
2289  uint32_t cardstate;
2290  uint32_t errorstate;
2291  uint32_t resp1 = 0;
2292 
2293  errorstate = SD_SendStatus(hsd, &resp1);
2294  if(errorstate != HAL_SD_ERROR_NONE)
2295  {
2296  hsd->ErrorCode |= errorstate;
2297  }
2298 
2299  cardstate = ((resp1 >> 9U) & 0x0FU);
2300 
2301  return (HAL_SD_CardStateTypeDef)cardstate;
2302 }
2303 
2310 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2311 {
2312  HAL_SD_CardStateTypeDef CardState;
2313  uint32_t context = hsd->Context;
2314 
2315  /* DIsable All interrupts */
2316  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2317  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2318 
2319  /* Clear All flags */
2320  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2321 
2322  CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
2323 
2324  if ((context & SD_CONTEXT_DMA) != 0U)
2325  {
2326  /* Disable the SD DMA request */
2327  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2328 
2329  /* Abort the SD DMA Tx channel */
2330  if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2331  {
2332  if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
2333  {
2334  hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2335  }
2336  }
2337  /* Abort the SD DMA Rx channel */
2338  else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2339  {
2340  if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
2341  {
2342  hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2343  }
2344  }
2345  else
2346  {
2347  /* Nothing to do */
2348  }
2349  }
2350 
2351  hsd->State = HAL_SD_STATE_READY;
2352 
2353  /* Initialize the SD operation */
2354  hsd->Context = SD_CONTEXT_NONE;
2355 
2356  CardState = HAL_SD_GetCardState(hsd);
2357  if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2358  {
2359  hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2360  }
2361  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2362  {
2363  return HAL_ERROR;
2364  }
2365  return HAL_OK;
2366 }
2367 
2374 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2375 {
2376  HAL_SD_CardStateTypeDef CardState;
2377  uint32_t context = hsd->Context;
2378 
2379  /* Disable All interrupts */
2380  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2381  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2382 
2383  CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
2384 
2385  if ((context & SD_CONTEXT_DMA) != 0U)
2386  {
2387  /* Disable the SD DMA request */
2388  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2389 
2390  /* Abort the SD DMA Tx channel */
2391  if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2392  {
2393  hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2394  if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2395  {
2396  hsd->hdmatx = NULL;
2397  }
2398  }
2399  /* Abort the SD DMA Rx channel */
2400  else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2401  {
2402  hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2403  if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2404  {
2405  hsd->hdmarx = NULL;
2406  }
2407  }
2408  else
2409  {
2410  /* Nothing to do */
2411  }
2412  }
2413  /* No transfer ongoing on both DMA channels*/
2414  else
2415  {
2416  /* Clear All flags */
2417  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2418 
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))
2423  {
2424  hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2425  }
2426  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2427  {
2428  return HAL_ERROR;
2429  }
2430  else
2431  {
2432 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2433  hsd->AbortCpltCallback(hsd);
2434 #else
2435  HAL_SD_AbortCallback(hsd);
2436 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2437  }
2438  }
2439 
2440  return HAL_OK;
2441 }
2442 
2451 /* Private function ----------------------------------------------------------*/
2461 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2462 {
2463  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2464 
2465  /* Enable DATAEND Interrupt */
2466  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
2467 }
2468 
2474 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2475 {
2476  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2477  uint32_t errorstate;
2478 
2479  /* Send stop command in multiblock write */
2480  if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2481  {
2482  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2483  if(errorstate != HAL_SD_ERROR_NONE)
2484  {
2485  hsd->ErrorCode |= errorstate;
2486 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2487  hsd->ErrorCallback(hsd);
2488 #else
2489  HAL_SD_ErrorCallback(hsd);
2490 #endif
2491  }
2492  }
2493 
2494  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2495  in the SD DCTRL register */
2496  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2497 
2498  /* Clear all the static flags */
2499  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2500 
2501  hsd->State = HAL_SD_STATE_READY;
2502  hsd->Context = SD_CONTEXT_NONE;
2503 
2504 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2505  hsd->RxCpltCallback(hsd);
2506 #else
2507  HAL_SD_RxCpltCallback(hsd);
2508 #endif
2509 }
2510 
2516 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2517 {
2518  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2519  HAL_SD_CardStateTypeDef CardState;
2520  uint32_t RxErrorCode, TxErrorCode;
2521 
2522  /* if DMA error is FIFO error ignore it */
2523  if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2524  {
2525  RxErrorCode = hsd->hdmarx->ErrorCode;
2526  TxErrorCode = hsd->hdmatx->ErrorCode;
2527  if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2528  {
2529  /* Clear All flags */
2530  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2531 
2532  /* Disable All interrupts */
2533  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2534  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2535 
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))
2539  {
2540  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2541  }
2542 
2543  hsd->State= HAL_SD_STATE_READY;
2544  hsd->Context = SD_CONTEXT_NONE;
2545  }
2546 
2547 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2548  hsd->ErrorCallback(hsd);
2549 #else
2550  HAL_SD_ErrorCallback(hsd);
2551 #endif
2552  }
2553 }
2554 
2560 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2561 {
2562  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2563  HAL_SD_CardStateTypeDef CardState;
2564 
2565  /* Clear All flags */
2566  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2567 
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))
2572  {
2573  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2574  }
2575 
2576  if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2577  {
2578 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2579  hsd->AbortCpltCallback(hsd);
2580 #else
2581  HAL_SD_AbortCallback(hsd);
2582 #endif
2583  }
2584  else
2585  {
2586 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2587  hsd->ErrorCallback(hsd);
2588 #else
2589  HAL_SD_ErrorCallback(hsd);
2590 #endif
2591  }
2592 }
2593 
2599 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2600 {
2601  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2602  HAL_SD_CardStateTypeDef CardState;
2603 
2604  /* Clear All flags */
2605  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2606 
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))
2611  {
2612  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2613  }
2614 
2615  if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2616  {
2617 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2618  hsd->AbortCpltCallback(hsd);
2619 #else
2620  HAL_SD_AbortCallback(hsd);
2621 #endif
2622  }
2623  else
2624  {
2625 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2626  hsd->ErrorCallback(hsd);
2627 #else
2628  HAL_SD_ErrorCallback(hsd);
2629 #endif
2630  }
2631 }
2632 
2638 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2639 {
2640  HAL_SD_CardCSDTypeDef CSD;
2641  uint32_t errorstate;
2642  uint16_t sd_rca = 1U;
2643 
2644  /* Check the power State */
2645  if(SDMMC_GetPowerState(hsd->Instance) == 0U)
2646  {
2647  /* Power off */
2648  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2649  }
2650 
2651  if(hsd->SdCard.CardType != CARD_SECURED)
2652  {
2653  /* Send CMD2 ALL_SEND_CID */
2654  errorstate = SDMMC_CmdSendCID(hsd->Instance);
2655  if(errorstate != HAL_SD_ERROR_NONE)
2656  {
2657  return errorstate;
2658  }
2659  else
2660  {
2661  /* Get Card identification number data */
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);
2666  }
2667  }
2668 
2669  if(hsd->SdCard.CardType != CARD_SECURED)
2670  {
2671  /* Send CMD3 SET_REL_ADDR with argument 0 */
2672  /* SD Card publishes its RCA. */
2673  errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2674  if(errorstate != HAL_SD_ERROR_NONE)
2675  {
2676  return errorstate;
2677  }
2678  }
2679  if(hsd->SdCard.CardType != CARD_SECURED)
2680  {
2681  /* Get the SD card RCA */
2682  hsd->SdCard.RelCardAdd = sd_rca;
2683 
2684  /* Send CMD9 SEND_CSD with argument as card's RCA */
2685  errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2686  if(errorstate != HAL_SD_ERROR_NONE)
2687  {
2688  return errorstate;
2689  }
2690  else
2691  {
2692  /* Get Card Specific Data */
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);
2697  }
2698  }
2699 
2700  /* Get the Card Class */
2701  hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
2702 
2703  /* Get CSD parameters */
2704  if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2705  {
2706  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2707  }
2708 
2709  /* Select the Card */
2710  errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2711  if(errorstate != HAL_SD_ERROR_NONE)
2712  {
2713  return errorstate;
2714  }
2715 
2716  /* Configure SDMMC peripheral interface */
2717  (void)SDMMC_Init(hsd->Instance, hsd->Init);
2718 
2719  /* All cards are initialized */
2720  return HAL_SD_ERROR_NONE;
2721 }
2722 
2730 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2731 {
2732  __IO uint32_t count = 0U;
2733  uint32_t response = 0U, validvoltage = 0U;
2734  uint32_t errorstate;
2735 
2736  /* CMD0: GO_IDLE_STATE */
2737  errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2738  if(errorstate != HAL_SD_ERROR_NONE)
2739  {
2740  return errorstate;
2741  }
2742 
2743  /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2744  errorstate = SDMMC_CmdOperCond(hsd->Instance);
2745  if(errorstate != HAL_SD_ERROR_NONE)
2746  {
2747  hsd->SdCard.CardVersion = CARD_V1_X;
2748  /* CMD0: GO_IDLE_STATE */
2749  errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2750  if(errorstate != HAL_SD_ERROR_NONE)
2751  {
2752  return errorstate;
2753  }
2754 
2755  }
2756  else
2757  {
2758  hsd->SdCard.CardVersion = CARD_V2_X;
2759  }
2760 
2761  if( hsd->SdCard.CardVersion == CARD_V2_X)
2762  {
2763  /* SEND CMD55 APP_CMD with RCA as 0 */
2764  errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2765  if(errorstate != HAL_SD_ERROR_NONE)
2766  {
2767  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2768  }
2769  }
2770  /* SD CARD */
2771  /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2772  while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2773  {
2774  /* SEND CMD55 APP_CMD with RCA as 0 */
2775  errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2776  if(errorstate != HAL_SD_ERROR_NONE)
2777  {
2778  return errorstate;
2779  }
2780 
2781  /* Send CMD41 */
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)
2784  {
2785  return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2786  }
2787 
2788  /* Get command response */
2789  response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2790 
2791  /* Get operating voltage*/
2792  validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2793 
2794  count++;
2795  }
2796 
2797  if(count >= SDMMC_MAX_VOLT_TRIAL)
2798  {
2799  return HAL_SD_ERROR_INVALID_VOLTRANGE;
2800  }
2801 
2802  if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2803  {
2804  hsd->SdCard.CardType = CARD_SDHC_SDXC;
2805  }
2806  else
2807  {
2808  hsd->SdCard.CardType = CARD_SDSC;
2809  }
2810 
2811 
2812  return HAL_SD_ERROR_NONE;
2813 }
2814 
2820 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2821 {
2822  /* Set Power State to OFF */
2823  (void)SDMMC_PowerState_OFF(hsd->Instance);
2824 }
2825 
2833 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2834 {
2835  SDMMC_DataInitTypeDef config;
2836  uint32_t errorstate;
2837  uint32_t tickstart = HAL_GetTick();
2838  uint32_t count;
2839  uint32_t *pData = pSDstatus;
2840 
2841  /* Check SD response */
2842  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2843  {
2844  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2845  }
2846 
2847  /* Set block size for card if it is not equal to current block size for card */
2848  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2849  if(errorstate != HAL_SD_ERROR_NONE)
2850  {
2851  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2852  return errorstate;
2853  }
2854 
2855  /* Send CMD55 */
2856  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2857  if(errorstate != HAL_SD_ERROR_NONE)
2858  {
2859  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2860  return errorstate;
2861  }
2862 
2863  /* Configure the SD DPSM (Data Path State Machine) */
2864  config.DataTimeOut = SDMMC_DATATIMEOUT;
2865  config.DataLength = 64U;
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);
2871 
2872  /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2873  errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2874  if(errorstate != HAL_SD_ERROR_NONE)
2875  {
2876  hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2877  return errorstate;
2878  }
2879 
2880  /* Get status data */
2881  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
2882  {
2883  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
2884  {
2885  for(count = 0U; count < 8U; count++)
2886  {
2887  *pData = SDMMC_ReadFIFO(hsd->Instance);
2888  pData++;
2889  }
2890  }
2891 
2892  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2893  {
2894  return HAL_SD_ERROR_TIMEOUT;
2895  }
2896  }
2897 
2898  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
2899  {
2900  return HAL_SD_ERROR_DATA_TIMEOUT;
2901  }
2902  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
2903  {
2904  return HAL_SD_ERROR_DATA_CRC_FAIL;
2905  }
2906  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
2907  {
2908  return HAL_SD_ERROR_RX_OVERRUN;
2909  }
2910  else
2911  {
2912  /* Nothing to do */
2913  }
2914 
2915  while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
2916  {
2917  *pData = SDMMC_ReadFIFO(hsd->Instance);
2918  pData++;
2919 
2920  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2921  {
2922  return HAL_SD_ERROR_TIMEOUT;
2923  }
2924  }
2925 
2926  /* Clear all the static status flags*/
2927  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2928 
2929  return HAL_SD_ERROR_NONE;
2930 }
2931 
2939 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2940 {
2941  uint32_t errorstate;
2942 
2943  if(pCardStatus == NULL)
2944  {
2945  return HAL_SD_ERROR_PARAM;
2946  }
2947 
2948  /* Send Status command */
2949  errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2950  if(errorstate != HAL_SD_ERROR_NONE)
2951  {
2952  return errorstate;
2953  }
2954 
2955  /* Get SD card status */
2956  *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2957 
2958  return HAL_SD_ERROR_NONE;
2959 }
2960 
2966 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2967 {
2968  uint32_t scr[2U] = {0U, 0U};
2969  uint32_t errorstate;
2970 
2971  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2972  {
2973  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2974  }
2975 
2976  /* Get SCR Register */
2977  errorstate = SD_FindSCR(hsd, scr);
2978  if(errorstate != HAL_SD_ERROR_NONE)
2979  {
2980  return errorstate;
2981  }
2982 
2983  /* If requested card supports wide bus operation */
2984  if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
2985  {
2986  /* Send CMD55 APP_CMD with argument as card's RCA.*/
2987  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2988  if(errorstate != HAL_SD_ERROR_NONE)
2989  {
2990  return errorstate;
2991  }
2992 
2993  /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2994  errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
2995  if(errorstate != HAL_SD_ERROR_NONE)
2996  {
2997  return errorstate;
2998  }
2999 
3000  return HAL_SD_ERROR_NONE;
3001  }
3002  else
3003  {
3004  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3005  }
3006 }
3007 
3013 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3014 {
3015  uint32_t scr[2U] = {0U, 0U};
3016  uint32_t errorstate;
3017 
3018  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3019  {
3020  return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3021  }
3022 
3023  /* Get SCR Register */
3024  errorstate = SD_FindSCR(hsd, scr);
3025  if(errorstate != HAL_SD_ERROR_NONE)
3026  {
3027  return errorstate;
3028  }
3029 
3030  /* If requested card supports 1 bit mode operation */
3031  if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3032  {
3033  /* Send CMD55 APP_CMD with argument as card's RCA */
3034  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3035  if(errorstate != HAL_SD_ERROR_NONE)
3036  {
3037  return errorstate;
3038  }
3039 
3040  /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3041  errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3042  if(errorstate != HAL_SD_ERROR_NONE)
3043  {
3044  return errorstate;
3045  }
3046 
3047  return HAL_SD_ERROR_NONE;
3048  }
3049  else
3050  {
3051  return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3052  }
3053 }
3054 
3055 
3062 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3063 {
3064  SDMMC_DataInitTypeDef config;
3065  uint32_t errorstate;
3066  uint32_t tickstart = HAL_GetTick();
3067  uint32_t index = 0U;
3068  uint32_t tempscr[2U] = {0U, 0U};
3069  uint32_t *scr = pSCR;
3070 
3071  /* Set Block Size To 8 Bytes */
3072  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3073  if(errorstate != HAL_SD_ERROR_NONE)
3074  {
3075  return errorstate;
3076  }
3077 
3078  /* Send CMD55 APP_CMD with argument as card's RCA */
3079  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3080  if(errorstate != HAL_SD_ERROR_NONE)
3081  {
3082  return errorstate;
3083  }
3084 
3085  config.DataTimeOut = SDMMC_DATATIMEOUT;
3086  config.DataLength = 8U;
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);
3092 
3093  /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3094  errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3095  if(errorstate != HAL_SD_ERROR_NONE)
3096  {
3097  return errorstate;
3098  }
3099 
3100  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3101  {
3102  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3103  {
3104  *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3105  index++;
3106  }
3107 
3108  if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3109  {
3110  return HAL_SD_ERROR_TIMEOUT;
3111  }
3112  }
3113 
3114  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3115  {
3116  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3117 
3118  return HAL_SD_ERROR_DATA_TIMEOUT;
3119  }
3120  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3121  {
3122  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3123 
3124  return HAL_SD_ERROR_DATA_CRC_FAIL;
3125  }
3126  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3127  {
3128  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3129 
3130  return HAL_SD_ERROR_RX_OVERRUN;
3131  }
3132  else
3133  {
3134  /* No error flag set */
3135  /* Clear all the static flags */
3136  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3137 
3138  *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3139  ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3140  scr++;
3141  *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3142  ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3143 
3144  }
3145 
3146  return HAL_SD_ERROR_NONE;
3147 }
3148 
3155 static void SD_Read_IT(SD_HandleTypeDef *hsd)
3156 {
3157  uint32_t count, data, dataremaining;
3158  uint8_t* tmp;
3159 
3160  tmp = hsd->pRxBuffPtr;
3161  dataremaining = hsd->RxXferSize;
3162 
3163  if (dataremaining > 0U)
3164  {
3165  /* Read data from SDMMC Rx FIFO */
3166  for(count = 0U; count < 8U; count++)
3167  {
3168  data = SDMMC_ReadFIFO(hsd->Instance);
3169  *tmp = (uint8_t)(data & 0xFFU);
3170  tmp++;
3171  dataremaining--;
3172  *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3173  tmp++;
3174  dataremaining--;
3175  *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3176  tmp++;
3177  dataremaining--;
3178  *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3179  tmp++;
3180  dataremaining--;
3181  }
3182 
3183  hsd->pRxBuffPtr = tmp;
3184  hsd->RxXferSize = dataremaining;
3185  }
3186 }
3187 
3194 static void SD_Write_IT(SD_HandleTypeDef *hsd)
3195 {
3196  uint32_t count, data, dataremaining;
3197  uint8_t* tmp;
3198 
3199  tmp = hsd->pTxBuffPtr;
3200  dataremaining = hsd->TxXferSize;
3201 
3202  if (dataremaining > 0U)
3203  {
3204  /* Write data to SDMMC Tx FIFO */
3205  for(count = 0U; count < 8U; count++)
3206  {
3207  data = (uint32_t)(*tmp);
3208  tmp++;
3209  dataremaining--;
3210  data |= ((uint32_t)(*tmp) << 8U);
3211  tmp++;
3212  dataremaining--;
3213  data |= ((uint32_t)(*tmp) << 16U);
3214  tmp++;
3215  dataremaining--;
3216  data |= ((uint32_t)(*tmp) << 24U);
3217  tmp++;
3218  dataremaining--;
3219  (void)SDMMC_WriteFIFO(hsd->Instance, &data);
3220  }
3221 
3222  hsd->pTxBuffPtr = tmp;
3223  hsd->TxXferSize = dataremaining;
3224  }
3225 }
3226 
3231 #endif /* HAL_SD_MODULE_ENABLED */
3232 
3241 #endif /* SDMMC1 */
3242 
3243 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
response
const std::string response
assert_param
#define assert_param(expr)
Include module's header file.
Definition: stm32f407/stm32f407g-disc1/Inc/stm32f4xx_hal_conf.h:353
__IO
#define __IO
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:237
Init
napi_value Init(napi_env env, napi_value exports)
Definition: porcupine/demo/c/pvrecorder/node/pv_recorder_napi.c:197
HAL_StatusTypeDef
HAL_StatusTypeDef
HAL Status structures definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:40
context
ma_context context
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:56
__DMA_HandleTypeDef
DMA handle Structure definition.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h:139
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
HAL_UNLOCKED
@ HAL_UNLOCKED
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:53
HAL_DMA_Abort_IT
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
HAL_ERROR
@ HAL_ERROR
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:43
CLEAR_BIT
#define CLEAR_BIT(REG, BIT)
Definition: stm32f407/stm32f407g-disc1/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h:214
HAL_GetTick
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c:323
HAL_OK
@ HAL_OK
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:42
UNUSED
#define UNUSED(x)
Definition: porcupine/demo/c/dr_libs/old/dr.h:92
__HAL_LOCK
#define __HAL_LOCK(__HANDLE__)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:93
RESET
@ RESET
Definition: stm32f407/stm32f407g-disc1/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h:187
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
HAL_BUSY
@ HAL_BUSY
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:44
HAL_DMA_Start_IT
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
HAL_SD_AbortCallback
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.
Definition: stm32f769i_discovery_sd.c:543
__HAL_UNLOCK
#define __HAL_UNLOCK(__HANDLE__)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:105
IS_SDMMC_ALL_INSTANCE
#define IS_SDMMC_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f769xx.h:21620
HAL_DMA_Abort
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
add
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
HAL_TIMEOUT
@ HAL_TIMEOUT
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:45
__DMA_HandleTypeDef::Parent
void * Parent
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h:149
HAL_DMA_ERROR_TE
#define HAL_DMA_ERROR_TE
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h:187
SDMMC_DCTRL_DTEN
#define SDMMC_DCTRL_DTEN
Definition: stm32f769xx.h:13523
SDMMC_DCTRL_DMAEN
#define SDMMC_DCTRL_DMAEN
Definition: stm32f769xx.h:13532
HAL_DMA_GetError
uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
config
static sai_transceiver_t config
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:75
HAL_DMA_ERROR_FE
#define HAL_DMA_ERROR_FE
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h:188
stm32f7xx_hal.h
This file contains all the functions prototypes for the HAL module driver.
HAL_SD_TxCpltCallback
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
Tx Transfer completed callbacks.
Definition: stm32f769i_discovery_sd.c:553
HAL_SD_RxCpltCallback
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Rx Transfer completed callbacks.
Definition: stm32f769i_discovery_sd.c:563


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:53