STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_mmc.c
Go to the documentation of this file.
1 
260 /* Includes ------------------------------------------------------------------*/
261 #include "stm32l4xx_hal.h"
262 
263 #ifdef HAL_MMC_MODULE_ENABLED
264 
265 #if defined(SDMMC1)
266 
275 /* Private typedef -----------------------------------------------------------*/
276 /* Private define ------------------------------------------------------------*/
285 /* Private macro -------------------------------------------------------------*/
286 /* Private variables ---------------------------------------------------------*/
287 /* Private function prototypes -----------------------------------------------*/
288 /* Private functions ---------------------------------------------------------*/
292 static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc);
293 static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc);
294 static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus);
295 static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc);
296 static void MMC_Write_IT(MMC_HandleTypeDef *hmmc);
297 static void MMC_Read_IT(MMC_HandleTypeDef *hmmc);
298 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
299 static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma);
300 static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
301 static void MMC_DMAError(DMA_HandleTypeDef *hdma);
302 static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma);
303 static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma);
304 #else
305 static HAL_StatusTypeDef MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pBlockNbr, uint32_t Timeout);
306 #endif
307 
311 /* Exported functions --------------------------------------------------------*/
337 HAL_StatusTypeDef HAL_MMC_Init(MMC_HandleTypeDef *hmmc)
338 {
339  /* Check the MMC handle allocation */
340  if(hmmc == NULL)
341  {
342  return HAL_ERROR;
343  }
344 
345  /* Check the parameters */
346  assert_param(IS_SDMMC_ALL_INSTANCE(hmmc->Instance));
347  assert_param(IS_SDMMC_CLOCK_EDGE(hmmc->Init.ClockEdge));
348 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
349  assert_param(IS_SDMMC_CLOCK_BYPASS(hmmc->Init.ClockBypass));
350 #endif
351  assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hmmc->Init.ClockPowerSave));
352  assert_param(IS_SDMMC_BUS_WIDE(hmmc->Init.BusWide));
353  assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hmmc->Init.HardwareFlowControl));
354  assert_param(IS_SDMMC_CLKDIV(hmmc->Init.ClockDiv));
355 
356  if(hmmc->State == HAL_MMC_STATE_RESET)
357  {
358  /* Allocate lock resource and initialize it */
359  hmmc->Lock = HAL_UNLOCKED;
360 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
361  /* Reset Callback pointers in HAL_MMC_STATE_RESET only */
362  hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
363  hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
364  hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
365  hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
366 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
367  hmmc->Read_DMADblBuf0CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback;
368  hmmc->Read_DMADblBuf1CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback;
369  hmmc->Write_DMADblBuf0CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback;
370  hmmc->Write_DMADblBuf1CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback;
371 #endif
372 
373  if(hmmc->MspInitCallback == NULL)
374  {
375  hmmc->MspInitCallback = HAL_MMC_MspInit;
376  }
377 
378  /* Init the low level hardware */
379  hmmc->MspInitCallback(hmmc);
380 #else
381  /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
382  HAL_MMC_MspInit(hmmc);
383 #endif
384  }
385 
386  hmmc->State = HAL_MMC_STATE_BUSY;
387 
388  /* Initialize the Card parameters */
389  if(HAL_MMC_InitCard(hmmc) == HAL_ERROR)
390  {
391  return HAL_ERROR;
392  }
393 
394  /* Initialize the error code */
395  hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
396 
397  /* Initialize the MMC operation */
398  hmmc->Context = MMC_CONTEXT_NONE;
399 
400  /* Initialize the MMC state */
401  hmmc->State = HAL_MMC_STATE_READY;
402 
403  return HAL_OK;
404 }
405 
413 HAL_StatusTypeDef HAL_MMC_InitCard(MMC_HandleTypeDef *hmmc)
414 {
415  uint32_t errorstate;
416  MMC_InitTypeDef Init;
417  HAL_StatusTypeDef status;
418 
419  /* Default SDMMC peripheral configuration for MMC card initialization */
420  Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
421 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
422  Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
423 #endif
424  Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
425  Init.BusWide = SDMMC_BUS_WIDE_1B;
426  Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
427  Init.ClockDiv = SDMMC_INIT_CLK_DIV;
428 
429  /* Initialize SDMMC peripheral interface with default configuration */
430  status = SDMMC_Init(hmmc->Instance, Init);
431  if(status == HAL_ERROR)
432  {
433  return HAL_ERROR;
434  }
435 
436 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
437  /* Disable SDMMC Clock */
438  __HAL_MMC_DISABLE(hmmc);
439 #endif
440 
441  /* Set Power State to ON */
442  status = SDMMC_PowerState_ON(hmmc->Instance);
443  if(status == HAL_ERROR)
444  {
445  return HAL_ERROR;
446  }
447 
448 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
449  /* Enable MMC Clock */
450  __HAL_MMC_ENABLE(hmmc);
451 #endif
452 
453  /* Identify card operating voltage */
454  errorstate = MMC_PowerON(hmmc);
455  if(errorstate != HAL_MMC_ERROR_NONE)
456  {
457  hmmc->State = HAL_MMC_STATE_READY;
458  hmmc->ErrorCode |= errorstate;
459  return HAL_ERROR;
460  }
461 
462  /* Card initialization */
463  errorstate = MMC_InitCard(hmmc);
464  if(errorstate != HAL_MMC_ERROR_NONE)
465  {
466  hmmc->State = HAL_MMC_STATE_READY;
467  hmmc->ErrorCode |= errorstate;
468  return HAL_ERROR;
469  }
470 
471  return HAL_OK;
472 }
473 
479 HAL_StatusTypeDef HAL_MMC_DeInit(MMC_HandleTypeDef *hmmc)
480 {
481  /* Check the MMC handle allocation */
482  if(hmmc == NULL)
483  {
484  return HAL_ERROR;
485  }
486 
487  /* Check the parameters */
488  assert_param(IS_SDMMC_ALL_INSTANCE(hmmc->Instance));
489 
490  hmmc->State = HAL_MMC_STATE_BUSY;
491 
492  /* Set MMC power state to off */
493  MMC_PowerOFF(hmmc);
494 
495 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
496  if(hmmc->MspDeInitCallback == NULL)
497  {
498  hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
499  }
500 
501  /* DeInit the low level hardware */
502  hmmc->MspDeInitCallback(hmmc);
503 #else
504  /* De-Initialize the MSP layer */
505  HAL_MMC_MspDeInit(hmmc);
506 #endif
507 
508  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
509  hmmc->State = HAL_MMC_STATE_RESET;
510 
511  return HAL_OK;
512 }
513 
514 
521 {
522  /* Prevent unused argument(s) compilation warning */
523  UNUSED(hmmc);
524 
525  /* NOTE : This function Should not be modified, when the callback is needed,
526  the HAL_MMC_MspInit could be implemented in the user file
527  */
528 }
529 
536 {
537  /* Prevent unused argument(s) compilation warning */
538  UNUSED(hmmc);
539 
540  /* NOTE : This function Should not be modified, when the callback is needed,
541  the HAL_MMC_MspDeInit could be implemented in the user file
542  */
543 }
544 
576 HAL_StatusTypeDef HAL_MMC_ReadBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
577 {
578  SDMMC_DataInitTypeDef config;
579  uint32_t errorstate;
580  uint32_t tickstart = HAL_GetTick();
581  uint32_t count, data, dataremaining;
582  uint32_t add = BlockAdd;
583  uint8_t *tempbuff = pData;
584 
585  if(NULL == pData)
586  {
587  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
588  return HAL_ERROR;
589  }
590 
591  if(hmmc->State == HAL_MMC_STATE_READY)
592  {
593  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
594 
595  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
596  {
597  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
598  return HAL_ERROR;
599  }
600 
601  hmmc->State = HAL_MMC_STATE_BUSY;
602 
603  /* Initialize data control register */
604  hmmc->Instance->DCTRL = 0U;
605 
606  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
607  {
608  add *= 512U;
609  }
610 
611  /* Set Block Size for Card */
612  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
613  if(errorstate != HAL_MMC_ERROR_NONE)
614  {
615  /* Clear all the static flags */
616  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
617  hmmc->ErrorCode |= errorstate;
618  hmmc->State = HAL_MMC_STATE_READY;
619  return HAL_ERROR;
620  }
621 
622  /* Configure the MMC DPSM (Data Path State Machine) */
623  config.DataTimeOut = SDMMC_DATATIMEOUT;
624  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
625  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
626  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
627  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
628 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
629  config.DPSM = SDMMC_DPSM_ENABLE;
630 #else
631  config.DPSM = SDMMC_DPSM_DISABLE;
632 #endif
633  (void)SDMMC_ConfigData(hmmc->Instance, &config);
634 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
635  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
636 #endif
637 
638  /* Read block(s) in polling mode */
639  if(NumberOfBlocks > 1U)
640  {
641  hmmc->Context = MMC_CONTEXT_READ_MULTIPLE_BLOCK;
642 
643  /* Read Multi Block command */
644  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
645  }
646  else
647  {
648  hmmc->Context = MMC_CONTEXT_READ_SINGLE_BLOCK;
649 
650  /* Read Single Block command */
651  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
652  }
653  if(errorstate != HAL_MMC_ERROR_NONE)
654  {
655  /* Clear all the static flags */
656  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
657  hmmc->ErrorCode |= errorstate;
658  hmmc->State = HAL_MMC_STATE_READY;
659  return HAL_ERROR;
660  }
661 
662  /* Poll on SDMMC flags */
663  dataremaining = config.DataLength;
664  while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
665  {
666  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
667  {
668  /* Read data from SDMMC Rx FIFO */
669  for(count = 0U; count < 8U; count++)
670  {
671  data = SDMMC_ReadFIFO(hmmc->Instance);
672  *tempbuff = (uint8_t)(data & 0xFFU);
673  tempbuff++;
674  dataremaining--;
675  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
676  tempbuff++;
677  dataremaining--;
678  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
679  tempbuff++;
680  dataremaining--;
681  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
682  tempbuff++;
683  dataremaining--;
684  }
685  }
686 
687  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
688  {
689  /* Clear all the static flags */
690  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
691  hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
692  hmmc->State= HAL_MMC_STATE_READY;
693  return HAL_TIMEOUT;
694  }
695  }
696 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
697  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
698 #endif
699 
700  /* Send stop transmission command in case of multiblock read */
701  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
702  {
703  /* Send stop transmission command */
704  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
705  if(errorstate != HAL_MMC_ERROR_NONE)
706  {
707  /* Clear all the static flags */
708  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
709  hmmc->ErrorCode |= errorstate;
710  hmmc->State = HAL_MMC_STATE_READY;
711  return HAL_ERROR;
712  }
713  }
714 
715  /* Get error state */
716  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
717  {
718  /* Clear all the static flags */
719  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
720  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
721  hmmc->State = HAL_MMC_STATE_READY;
722  return HAL_ERROR;
723  }
724  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
725  {
726  /* Clear all the static flags */
727  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
728  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
729  hmmc->State = HAL_MMC_STATE_READY;
730  return HAL_ERROR;
731  }
732  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR))
733  {
734  /* Clear all the static flags */
735  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
736  hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
737  hmmc->State = HAL_MMC_STATE_READY;
738  return HAL_ERROR;
739  }
740  else
741  {
742  /* Nothing to do */
743  }
744 
745 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
746  /* Empty FIFO if there is still any data */
747  while ((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
748  {
749  data = SDMMC_ReadFIFO(hmmc->Instance);
750  *tempbuff = (uint8_t)(data & 0xFFU);
751  tempbuff++;
752  dataremaining--;
753  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
754  tempbuff++;
755  dataremaining--;
756  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
757  tempbuff++;
758  dataremaining--;
759  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
760  tempbuff++;
761  dataremaining--;
762 
763  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
764  {
765  /* Clear all the static flags */
766  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
767  hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
768  hmmc->State= HAL_MMC_STATE_READY;
769  return HAL_ERROR;
770  }
771  }
772 #endif
773 
774  /* Clear all the static flags */
775  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
776 
777  hmmc->State = HAL_MMC_STATE_READY;
778 
779  return HAL_OK;
780  }
781  else
782  {
783  hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
784  return HAL_ERROR;
785  }
786 }
787 
800 HAL_StatusTypeDef HAL_MMC_WriteBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
801 {
802  SDMMC_DataInitTypeDef config;
803  uint32_t errorstate;
804  uint32_t tickstart = HAL_GetTick();
805  uint32_t count, data, dataremaining;
806  uint32_t add = BlockAdd;
807  uint8_t *tempbuff = pData;
808 
809  if(NULL == pData)
810  {
811  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
812  return HAL_ERROR;
813  }
814 
815  if(hmmc->State == HAL_MMC_STATE_READY)
816  {
817  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
818 
819  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
820  {
821  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
822  return HAL_ERROR;
823  }
824 
825  hmmc->State = HAL_MMC_STATE_BUSY;
826 
827  /* Initialize data control register */
828  hmmc->Instance->DCTRL = 0U;
829 
830  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
831  {
832  add *= 512U;
833  }
834 
835  /* Set Block Size for Card */
836  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
837  if(errorstate != HAL_MMC_ERROR_NONE)
838  {
839  /* Clear all the static flags */
840  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
841  hmmc->ErrorCode |= errorstate;
842  hmmc->State = HAL_MMC_STATE_READY;
843  return HAL_ERROR;
844  }
845 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
846  /* Configure the MMC DPSM (Data Path State Machine) */
847  config.DataTimeOut = SDMMC_DATATIMEOUT;
848  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
849  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
850  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
851  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
852  config.DPSM = SDMMC_DPSM_DISABLE;
853  (void)SDMMC_ConfigData(hmmc->Instance, &config);
854  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
855 #endif
856 
857  /* Write Blocks in Polling mode */
858  if(NumberOfBlocks > 1U)
859  {
860  hmmc->Context = MMC_CONTEXT_WRITE_MULTIPLE_BLOCK;
861 
862  /* Write Multi Block command */
863  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
864  }
865  else
866  {
867  hmmc->Context = MMC_CONTEXT_WRITE_SINGLE_BLOCK;
868 
869  /* Write Single Block command */
870  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
871  }
872  if(errorstate != HAL_MMC_ERROR_NONE)
873  {
874  /* Clear all the static flags */
875  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
876  hmmc->ErrorCode |= errorstate;
877  hmmc->State = HAL_MMC_STATE_READY;
878  return HAL_ERROR;
879  }
880 
881 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
882  /* Configure the MMC DPSM (Data Path State Machine) */
883  config.DataTimeOut = SDMMC_DATATIMEOUT;
884  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
885  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
886  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
887  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
888  config.DPSM = SDMMC_DPSM_ENABLE;
889  (void)SDMMC_ConfigData(hmmc->Instance, &config);
890 #endif
891 
892  /* Write block(s) in polling mode */
893  dataremaining = config.DataLength;
894  while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
895  {
896  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
897  {
898  /* Write data to SDMMC Tx FIFO */
899  for(count = 0U; count < 8U; count++)
900  {
901  data = (uint32_t)(*tempbuff);
902  tempbuff++;
903  dataremaining--;
904  data |= ((uint32_t)(*tempbuff) << 8U);
905  tempbuff++;
906  dataremaining--;
907  data |= ((uint32_t)(*tempbuff) << 16U);
908  tempbuff++;
909  dataremaining--;
910  data |= ((uint32_t)(*tempbuff) << 24U);
911  tempbuff++;
912  dataremaining--;
913  (void)SDMMC_WriteFIFO(hmmc->Instance, &data);
914  }
915  }
916 
917  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
918  {
919  /* Clear all the static flags */
920  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
921  hmmc->ErrorCode |= errorstate;
922  hmmc->State = HAL_MMC_STATE_READY;
923  return HAL_TIMEOUT;
924  }
925  }
926 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
927  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
928 #endif
929 
930  /* Send stop transmission command in case of multiblock write */
931  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
932  {
933  /* Send stop transmission command */
934  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
935  if(errorstate != HAL_MMC_ERROR_NONE)
936  {
937  /* Clear all the static flags */
938  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
939  hmmc->ErrorCode |= errorstate;
940  hmmc->State = HAL_MMC_STATE_READY;
941  return HAL_ERROR;
942  }
943  }
944 
945  /* Get error state */
946  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
947  {
948  /* Clear all the static flags */
949  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
950  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
951  hmmc->State = HAL_MMC_STATE_READY;
952  return HAL_ERROR;
953  }
954  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
955  {
956  /* Clear all the static flags */
957  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
958  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
959  hmmc->State = HAL_MMC_STATE_READY;
960  return HAL_ERROR;
961  }
962  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR))
963  {
964  /* Clear all the static flags */
965  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
966  hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
967  hmmc->State = HAL_MMC_STATE_READY;
968  return HAL_ERROR;
969  }
970  else
971  {
972  /* Nothing to do */
973  }
974 
975  /* Clear all the static flags */
976  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
977 
978  hmmc->State = HAL_MMC_STATE_READY;
979 
980  return HAL_OK;
981  }
982  else
983  {
984  hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
985  return HAL_ERROR;
986  }
987 }
988 
1002 HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1003 {
1004  SDMMC_DataInitTypeDef config;
1005  uint32_t errorstate;
1006  uint32_t add = BlockAdd;
1007 
1008  if(NULL == pData)
1009  {
1010  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1011  return HAL_ERROR;
1012  }
1013 
1014  if(hmmc->State == HAL_MMC_STATE_READY)
1015  {
1016  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1017 
1018  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1019  {
1020  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1021  return HAL_ERROR;
1022  }
1023 
1024  hmmc->State = HAL_MMC_STATE_BUSY;
1025 
1026  /* Initialize data control register */
1027  hmmc->Instance->DCTRL = 0U;
1028 
1029  hmmc->pRxBuffPtr = pData;
1030  hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1031 
1032  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1033 
1034  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1035  {
1036  add *= 512U;
1037  }
1038 
1039  /* Set Block Size for Card */
1040  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1041  if(errorstate != HAL_MMC_ERROR_NONE)
1042  {
1043  /* Clear all the static flags */
1044  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1045  hmmc->ErrorCode |= errorstate;
1046  hmmc->State = HAL_MMC_STATE_READY;
1047  return HAL_ERROR;
1048  }
1049 
1050  /* Configure the MMC DPSM (Data Path State Machine) */
1051  config.DataTimeOut = SDMMC_DATATIMEOUT;
1052  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1053  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1054  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1055  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1056 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1057  config.DPSM = SDMMC_DPSM_ENABLE;
1058 #else
1059  config.DPSM = SDMMC_DPSM_DISABLE;
1060 #endif
1061  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1062 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1063  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1064 #endif
1065  /* Read Blocks in IT mode */
1066  if(NumberOfBlocks > 1U)
1067  {
1068  hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_IT);
1069 
1070  /* Read Multi Block command */
1071  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
1072  }
1073  else
1074  {
1075  hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_IT);
1076 
1077  /* Read Single Block command */
1078  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
1079  }
1080 
1081  if(errorstate != HAL_MMC_ERROR_NONE)
1082  {
1083  /* Clear all the static flags */
1084  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1085  hmmc->ErrorCode |= errorstate;
1086  hmmc->State = HAL_MMC_STATE_READY;
1087  return HAL_ERROR;
1088  }
1089 
1090  return HAL_OK;
1091  }
1092  else
1093  {
1094  return HAL_BUSY;
1095  }
1096 }
1097 
1111 HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1112 {
1113  SDMMC_DataInitTypeDef config;
1114  uint32_t errorstate;
1115  uint32_t add = BlockAdd;
1116 
1117  if(NULL == pData)
1118  {
1119  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1120  return HAL_ERROR;
1121  }
1122 
1123  if(hmmc->State == HAL_MMC_STATE_READY)
1124  {
1125  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1126 
1127  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1128  {
1129  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1130  return HAL_ERROR;
1131  }
1132 
1133  hmmc->State = HAL_MMC_STATE_BUSY;
1134 
1135  /* Initialize data control register */
1136  hmmc->Instance->DCTRL = 0U;
1137 
1138  hmmc->pTxBuffPtr = pData;
1139  hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1140 
1141  /* Enable transfer interrupts */
1142  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1143 
1144  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1145  {
1146  add *= 512U;
1147  }
1148 
1149  /* Set Block Size for Card */
1150  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1151  if(errorstate != HAL_MMC_ERROR_NONE)
1152  {
1153  /* Clear all the static flags */
1154  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1155  hmmc->ErrorCode |= errorstate;
1156  hmmc->State = HAL_MMC_STATE_READY;
1157  return HAL_ERROR;
1158  }
1159 
1160 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1161  /* Configure the MMC DPSM (Data Path State Machine) */
1162  config.DataTimeOut = SDMMC_DATATIMEOUT;
1163  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1164  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1165  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1166  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1167  config.DPSM = SDMMC_DPSM_DISABLE;
1168  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1169 
1170  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1171 #endif
1172 
1173  /* Write Blocks in Polling mode */
1174  if(NumberOfBlocks > 1U)
1175  {
1176  hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK| MMC_CONTEXT_IT);
1177 
1178  /* Write Multi Block command */
1179  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
1180  }
1181  else
1182  {
1183  hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_IT);
1184 
1185  /* Write Single Block command */
1186  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
1187  }
1188  if(errorstate != HAL_MMC_ERROR_NONE)
1189  {
1190  /* Clear all the static flags */
1191  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1192  hmmc->ErrorCode |= errorstate;
1193  hmmc->State = HAL_MMC_STATE_READY;
1194  return HAL_ERROR;
1195  }
1196 
1197 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1198  /* Configure the MMC DPSM (Data Path State Machine) */
1199  config.DataTimeOut = SDMMC_DATATIMEOUT;
1200  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1201  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1202  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1203  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1204  config.DPSM = SDMMC_DPSM_ENABLE;
1205  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1206 #endif
1207 
1208  return HAL_OK;
1209  }
1210  else
1211  {
1212  return HAL_BUSY;
1213  }
1214 }
1215 
1229 HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1230 {
1231  SDMMC_DataInitTypeDef config;
1232  uint32_t errorstate;
1233  uint32_t add = BlockAdd;
1234 
1235  if(NULL == pData)
1236  {
1237  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1238  return HAL_ERROR;
1239  }
1240 
1241  if(hmmc->State == HAL_MMC_STATE_READY)
1242  {
1243  hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
1244 
1245  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1246  {
1247  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1248  return HAL_ERROR;
1249  }
1250 
1251  hmmc->State = HAL_MMC_STATE_BUSY;
1252 
1253  /* Initialize data control register */
1254  hmmc->Instance->DCTRL = 0U;
1255 
1256 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1257  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1258 
1259  /* Set the DMA transfer complete callback */
1260  hmmc->hdmarx->XferCpltCallback = MMC_DMAReceiveCplt;
1261 
1262  /* Set the DMA error callback */
1263  hmmc->hdmarx->XferErrorCallback = MMC_DMAError;
1264 
1265  /* Set the DMA Abort callback */
1266  hmmc->hdmarx->XferAbortCallback = NULL;
1267 
1268 #else
1269  hmmc->pRxBuffPtr = pData;
1270  hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1271 #endif
1272 
1273  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1274  {
1275  add *= 512U;
1276  }
1277 
1278  /* Set Block Size for Card */
1279  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1280  if(errorstate != HAL_MMC_ERROR_NONE)
1281  {
1282  /* Clear all the static flags */
1283  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1284  hmmc->ErrorCode = errorstate;
1285  hmmc->State = HAL_MMC_STATE_READY;
1286  return HAL_ERROR;
1287  }
1288 
1289 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1290  /* Configure the MMC DPSM (Data Path State Machine) */
1291  config.DataTimeOut = SDMMC_DATATIMEOUT;
1292  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1293  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1294  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1295  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1296  config.DPSM = SDMMC_DPSM_DISABLE;
1297  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1298 
1299  /* Enable transfer interrupts */
1300  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1301 
1302  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1303  hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
1304  hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1305 #else
1306  /* Enable the DMA Channel */
1307  if(HAL_DMA_Start_IT(hmmc->hdmarx, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)pData, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
1308  {
1309  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1310  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1311  hmmc->ErrorCode = HAL_MMC_ERROR_DMA;
1312  hmmc->State = HAL_MMC_STATE_READY;
1313  return HAL_ERROR;
1314  }
1315  else
1316  {
1317  /* Enable MMC DMA transfer */
1318  __HAL_MMC_DMA_ENABLE(hmmc);
1319 
1320  /* Configure the MMC DPSM (Data Path State Machine) */
1321  config.DataTimeOut = SDMMC_DATATIMEOUT;
1322  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1323  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1324  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1325  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1326  config.DPSM = SDMMC_DPSM_ENABLE;
1327  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1328 #endif
1329 
1330  /* Read Blocks in DMA mode */
1331  if(NumberOfBlocks > 1U)
1332  {
1333  hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
1334 
1335  /* Read Multi Block command */
1336  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
1337  }
1338  else
1339  {
1340  hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_DMA);
1341 
1342  /* Read Single Block command */
1343  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
1344  }
1345  if(errorstate != HAL_MMC_ERROR_NONE)
1346  {
1347  /* Clear all the static flags */
1348  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1349  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1350  hmmc->ErrorCode = errorstate;
1351  hmmc->State = HAL_MMC_STATE_READY;
1352  return HAL_ERROR;
1353  }
1354 
1355  return HAL_OK;
1356 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1357  }
1358 #endif
1359  }
1360  else
1361  {
1362  return HAL_BUSY;
1363  }
1364 }
1365 
1379 HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1380 {
1381  SDMMC_DataInitTypeDef config;
1382  uint32_t errorstate;
1383  uint32_t add = BlockAdd;
1384 
1385  if(NULL == pData)
1386  {
1387  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1388  return HAL_ERROR;
1389  }
1390 
1391  if(hmmc->State == HAL_MMC_STATE_READY)
1392  {
1393  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1394 
1395  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1396  {
1397  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1398  return HAL_ERROR;
1399  }
1400 
1401  hmmc->State = HAL_MMC_STATE_BUSY;
1402 
1403  /* Initialize data control register */
1404  hmmc->Instance->DCTRL = 0U;
1405 
1406 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1407  /* Enable MMC Error interrupts */
1408  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1409 
1410  /* Set the DMA transfer complete callback */
1411  hmmc->hdmatx->XferCpltCallback = MMC_DMATransmitCplt;
1412 
1413  /* Set the DMA error callback */
1414  hmmc->hdmatx->XferErrorCallback = MMC_DMAError;
1415 
1416  /* Set the DMA Abort callback */
1417  hmmc->hdmatx->XferAbortCallback = NULL;
1418 #else
1419  hmmc->pTxBuffPtr = pData;
1420  hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1421 #endif
1422 
1423  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1424  {
1425  add *= 512U;
1426  }
1427 
1428  /* Set Block Size for Card */
1429  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1430  if(errorstate != HAL_MMC_ERROR_NONE)
1431  {
1432  /* Clear all the static flags */
1433  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1434  hmmc->ErrorCode |= errorstate;
1435  hmmc->State = HAL_MMC_STATE_READY;
1436  return HAL_ERROR;
1437  }
1438 
1439 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1440  /* Configure the MMC DPSM (Data Path State Machine) */
1441  config.DataTimeOut = SDMMC_DATATIMEOUT;
1442  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1443  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1444  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1445  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1446  config.DPSM = SDMMC_DPSM_DISABLE;
1447  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1448 
1449  /* Enable transfer interrupts */
1450  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1451 
1452  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1453 
1454  hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
1455  hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1456 #endif
1457 
1458  /* Write Blocks in Polling mode */
1459  if(NumberOfBlocks > 1U)
1460  {
1461  hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
1462 
1463  /* Write Multi Block command */
1464  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
1465  }
1466  else
1467  {
1468  hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_DMA);
1469 
1470  /* Write Single Block command */
1471  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
1472  }
1473  if(errorstate != HAL_MMC_ERROR_NONE)
1474  {
1475  /* Clear all the static flags */
1476  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1477  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1478  hmmc->ErrorCode |= errorstate;
1479  hmmc->State = HAL_MMC_STATE_READY;
1480  return HAL_ERROR;
1481  }
1482 
1483 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1484  /* Enable SDMMC DMA transfer */
1485  __HAL_MMC_DMA_ENABLE(hmmc);
1486 
1487  /* Enable the DMA Channel */
1488  if(HAL_DMA_Start_IT(hmmc->hdmatx, (uint32_t)pData, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
1489  {
1490  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1491  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1492  hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
1493  hmmc->State = HAL_MMC_STATE_READY;
1494  return HAL_ERROR;
1495  }
1496  else
1497  {
1498  /* Configure the MMC DPSM (Data Path State Machine) */
1499  config.DataTimeOut = SDMMC_DATATIMEOUT;
1500  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1501  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1502  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1503  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1504  config.DPSM = SDMMC_DPSM_ENABLE;
1505  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1506 
1507  return HAL_OK;
1508  }
1509 #else
1510  return HAL_OK;
1511 #endif
1512  }
1513  else
1514  {
1515  return HAL_BUSY;
1516  }
1517 }
1518 
1528 HAL_StatusTypeDef HAL_MMC_Erase(MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1529 {
1530  uint32_t errorstate;
1531  uint32_t start_add = BlockStartAdd;
1532  uint32_t end_add = BlockEndAdd;
1533 
1534  if(hmmc->State == HAL_MMC_STATE_READY)
1535  {
1536  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1537 
1538  if(end_add < start_add)
1539  {
1540  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1541  return HAL_ERROR;
1542  }
1543 
1544  if(end_add > (hmmc->MmcCard.LogBlockNbr))
1545  {
1546  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1547  return HAL_ERROR;
1548  }
1549 
1550  hmmc->State = HAL_MMC_STATE_BUSY;
1551 
1552  /* Check if the card command class supports erase command */
1553  if(((hmmc->MmcCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1554  {
1555  /* Clear all the static flags */
1556  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1557  hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
1558  hmmc->State = HAL_MMC_STATE_READY;
1559  return HAL_ERROR;
1560  }
1561 
1562  if((SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1563  {
1564  /* Clear all the static flags */
1565  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1566  hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
1567  hmmc->State = HAL_MMC_STATE_READY;
1568  return HAL_ERROR;
1569  }
1570 
1571  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1572  {
1573  start_add *= 512U;
1574  end_add *= 512U;
1575  }
1576 
1577  /* Send CMD35 MMC_ERASE_GRP_START with argument as addr */
1578  errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, start_add);
1579  if(errorstate != HAL_MMC_ERROR_NONE)
1580  {
1581  /* Clear all the static flags */
1582  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1583  hmmc->ErrorCode |= errorstate;
1584  hmmc->State = HAL_MMC_STATE_READY;
1585  return HAL_ERROR;
1586  }
1587 
1588  /* Send CMD36 MMC_ERASE_GRP_END with argument as addr */
1589  errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, end_add);
1590  if(errorstate != HAL_MMC_ERROR_NONE)
1591  {
1592  /* Clear all the static flags */
1593  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1594  hmmc->ErrorCode |= errorstate;
1595  hmmc->State = HAL_MMC_STATE_READY;
1596  return HAL_ERROR;
1597  }
1598 
1599  /* Send CMD38 ERASE */
1600  errorstate = SDMMC_CmdErase(hmmc->Instance);
1601  if(errorstate != HAL_MMC_ERROR_NONE)
1602  {
1603  /* Clear all the static flags */
1604  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1605  hmmc->ErrorCode |= errorstate;
1606  hmmc->State = HAL_MMC_STATE_READY;
1607  return HAL_ERROR;
1608  }
1609 
1610  hmmc->State = HAL_MMC_STATE_READY;
1611 
1612  return HAL_OK;
1613  }
1614  else
1615  {
1616  return HAL_BUSY;
1617  }
1618 }
1619 
1626 {
1627  uint32_t errorstate;
1628  uint32_t context = hmmc->Context;
1629 
1630  /* Check for SDMMC interrupt flags */
1631  if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
1632  {
1633  MMC_Read_IT(hmmc);
1634  }
1635 
1636  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) != RESET)
1637  {
1638  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DATAEND);
1639 
1640  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1641  SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1642  SDMMC_IT_RXFIFOHF);
1643 
1644 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1645  hmmc->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
1646 #else
1647  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
1648  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
1649 #endif
1650 
1651  if((context & MMC_CONTEXT_DMA) != 0U)
1652  {
1653 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1654  hmmc->Instance->DLEN = 0;
1655  hmmc->Instance->DCTRL = 0;
1656  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA ;
1657 
1658  /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1659  if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1660  {
1661  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1662  if(errorstate != HAL_MMC_ERROR_NONE)
1663  {
1664  hmmc->ErrorCode |= errorstate;
1665 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1666  hmmc->ErrorCallback(hmmc);
1667 #else
1668  HAL_MMC_ErrorCallback(hmmc);
1669 #endif
1670  }
1671  }
1672 
1673  /* Clear all the static flags */
1674  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1675 
1676  hmmc->State = HAL_MMC_STATE_READY;
1677  if(((context & MMC_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1678  {
1679 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1680  hmmc->TxCpltCallback(hmmc);
1681 #else
1682  HAL_MMC_TxCpltCallback(hmmc);
1683 #endif
1684  }
1685  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1686  {
1687 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1688  hmmc->RxCpltCallback(hmmc);
1689 #else
1690  HAL_MMC_RxCpltCallback(hmmc);
1691 #endif
1692  }
1693 #else
1694  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1695  {
1696  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1697  if(errorstate != HAL_MMC_ERROR_NONE)
1698  {
1699  hmmc->ErrorCode |= errorstate;
1700 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1701  hmmc->ErrorCallback(hmmc);
1702 #else
1703  HAL_MMC_ErrorCallback(hmmc);
1704 #endif
1705  }
1706  }
1707  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1708  {
1709  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1710  in the MMC DCTRL register */
1711  hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1712 
1713  hmmc->State = HAL_MMC_STATE_READY;
1714 
1715 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1716  hmmc->TxCpltCallback(hmmc);
1717 #else
1718  HAL_MMC_TxCpltCallback(hmmc);
1719 #endif
1720  }
1721 #endif
1722  }
1723  else if((context & MMC_CONTEXT_IT) != 0U)
1724  {
1725  /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1726  if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1727  {
1728  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1729  if(errorstate != HAL_MMC_ERROR_NONE)
1730  {
1731  hmmc->ErrorCode |= errorstate;
1732 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1733  hmmc->ErrorCallback(hmmc);
1734 #else
1735  HAL_MMC_ErrorCallback(hmmc);
1736 #endif
1737  }
1738  }
1739 
1740  /* Clear all the static flags */
1741  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1742 
1743  hmmc->State = HAL_MMC_STATE_READY;
1744  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1745  {
1746 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1747  hmmc->RxCpltCallback(hmmc);
1748 #else
1749  HAL_MMC_RxCpltCallback(hmmc);
1750 #endif
1751  }
1752  else
1753  {
1754 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1755  hmmc->TxCpltCallback(hmmc);
1756 #else
1757  HAL_MMC_TxCpltCallback(hmmc);
1758 #endif
1759  }
1760  }
1761  else
1762  {
1763  /* Nothing to do */
1764  }
1765  }
1766 
1767  else if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
1768  {
1769  MMC_Write_IT(hmmc);
1770  }
1771 
1772  else if (__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL| SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1773  {
1774  /* Set Error code */
1775  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DCRCFAIL) != RESET)
1776  {
1777  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
1778  }
1779  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DTIMEOUT) != RESET)
1780  {
1781  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
1782  }
1783  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_RXOVERR) != RESET)
1784  {
1785  hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
1786  }
1787  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_TXUNDERR) != RESET)
1788  {
1789  hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
1790  }
1791 
1792  /* Clear All flags */
1793  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1794 
1795  /* Disable all interrupts */
1796  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1797  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1798 
1799 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1800  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
1801  hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1802  hmmc->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1803 #endif
1804  hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
1805 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1806  hmmc->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1807  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DABORT);
1808 #endif
1809 
1810  if((context & MMC_CONTEXT_IT) != 0U)
1811  {
1812  /* Set the MMC state to ready to be able to start again the process */
1813  hmmc->State = HAL_MMC_STATE_READY;
1814 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1815  hmmc->ErrorCallback(hmmc);
1816 #else
1817  HAL_MMC_ErrorCallback(hmmc);
1818 #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
1819  }
1820  else if((context & MMC_CONTEXT_DMA) != 0U)
1821  {
1822 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1823  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
1824  {
1825  /* Disable Internal DMA */
1826  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
1827  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1828 
1829  /* Set the MMC state to ready to be able to start again the process */
1830  hmmc->State = HAL_MMC_STATE_READY;
1831 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1832  hmmc->ErrorCallback(hmmc);
1833 #else
1834  HAL_MMC_ErrorCallback(hmmc);
1835 #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
1836  }
1837 #else
1838  /* Abort the MMC DMA Streams */
1839  if(hmmc->hdmatx != NULL)
1840  {
1841  /* Set the DMA Tx abort callback */
1842  hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
1843  /* Abort DMA in IT mode */
1844  if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
1845  {
1846  MMC_DMATxAbort(hmmc->hdmatx);
1847  }
1848  }
1849  else if(hmmc->hdmarx != NULL)
1850  {
1851  /* Set the DMA Rx abort callback */
1852  hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
1853  /* Abort DMA in IT mode */
1854  if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
1855  {
1856  MMC_DMARxAbort(hmmc->hdmarx);
1857  }
1858  }
1859  else
1860  {
1861  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1862  hmmc->State = HAL_MMC_STATE_READY;
1863 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1864  hmmc->AbortCpltCallback(hmmc);
1865 #else
1866  HAL_MMC_AbortCallback(hmmc);
1867 #endif
1868  }
1869 #endif
1870  }
1871  else
1872  {
1873  /* Nothing to do */
1874  }
1875  }
1876 
1877 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1878  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_IDMABTC) != RESET)
1879  {
1880  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_IT_IDMABTC);
1881  if(READ_BIT(hmmc->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1882  {
1883  /* Current buffer is buffer0, Transfer complete for buffer1 */
1884  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1885  {
1886 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1887  hmmc->Write_DMADblBuf1CpltCallback(hmmc);
1888 #else
1890 #endif
1891  }
1892  else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
1893  {
1894 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1895  hmmc->Read_DMADblBuf1CpltCallback(hmmc);
1896 #else
1898 #endif
1899  }
1900  }
1901  else /* MMC_DMA_BUFFER1 */
1902  {
1903  /* Current buffer is buffer1, Transfer complete for buffer0 */
1904  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1905  {
1906 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1907  hmmc->Write_DMADblBuf0CpltCallback(hmmc);
1908 #else
1910 #endif
1911  }
1912  else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
1913  {
1914 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1915  hmmc->Read_DMADblBuf0CpltCallback(hmmc);
1916 #else
1918 #endif
1919  }
1920  }
1921  }
1922 #endif
1923 
1924  else
1925  {
1926  /* Nothing to do */
1927  }
1928 }
1929 
1935 HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
1936 {
1937  return hmmc->State;
1938 }
1939 
1947 {
1948  return hmmc->ErrorCode;
1949 }
1950 
1957 {
1958  /* Prevent unused argument(s) compilation warning */
1959  UNUSED(hmmc);
1960 
1961  /* NOTE : This function should not be modified, when the callback is needed,
1962  the HAL_MMC_TxCpltCallback can be implemented in the user file
1963  */
1964 }
1965 
1972 {
1973  /* Prevent unused argument(s) compilation warning */
1974  UNUSED(hmmc);
1975 
1976  /* NOTE : This function should not be modified, when the callback is needed,
1977  the HAL_MMC_RxCpltCallback can be implemented in the user file
1978  */
1979 }
1980 
1987 {
1988  /* Prevent unused argument(s) compilation warning */
1989  UNUSED(hmmc);
1990 
1991  /* NOTE : This function should not be modified, when the callback is needed,
1992  the HAL_MMC_ErrorCallback can be implemented in the user file
1993  */
1994 }
1995 
2002 {
2003  /* Prevent unused argument(s) compilation warning */
2004  UNUSED(hmmc);
2005 
2006  /* NOTE : This function should not be modified, when the callback is needed,
2007  the HAL_MMC_AbortCallback can be implemented in the user file
2008  */
2009 }
2010 
2011 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2012 
2032 {
2033  HAL_StatusTypeDef status = HAL_OK;
2034 
2035  if(pCallback == NULL)
2036  {
2037  /* Update the error code */
2038  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2039  return HAL_ERROR;
2040  }
2041 
2042  /* Process locked */
2043  __HAL_LOCK(hmmc);
2044 
2045  if(hmmc->State == HAL_MMC_STATE_READY)
2046  {
2047  switch (CallbackId)
2048  {
2049  case HAL_MMC_TX_CPLT_CB_ID :
2050  hmmc->TxCpltCallback = pCallback;
2051  break;
2052  case HAL_MMC_RX_CPLT_CB_ID :
2053  hmmc->RxCpltCallback = pCallback;
2054  break;
2055  case HAL_MMC_ERROR_CB_ID :
2056  hmmc->ErrorCallback = pCallback;
2057  break;
2058  case HAL_MMC_ABORT_CB_ID :
2059  hmmc->AbortCpltCallback = pCallback;
2060  break;
2061 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2063  hmmc->Read_DMADblBuf0CpltCallback = pCallback;
2064  break;
2066  hmmc->Read_DMADblBuf1CpltCallback = pCallback;
2067  break;
2069  hmmc->Write_DMADblBuf0CpltCallback = pCallback;
2070  break;
2072  hmmc->Write_DMADblBuf1CpltCallback = pCallback;
2073  break;
2074 #endif
2075  case HAL_MMC_MSP_INIT_CB_ID :
2076  hmmc->MspInitCallback = pCallback;
2077  break;
2079  hmmc->MspDeInitCallback = pCallback;
2080  break;
2081  default :
2082  /* Update the error code */
2083  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2084  /* update return status */
2085  status = HAL_ERROR;
2086  break;
2087  }
2088  }
2089  else if (hmmc->State == HAL_MMC_STATE_RESET)
2090  {
2091  switch (CallbackId)
2092  {
2093  case HAL_MMC_MSP_INIT_CB_ID :
2094  hmmc->MspInitCallback = pCallback;
2095  break;
2097  hmmc->MspDeInitCallback = pCallback;
2098  break;
2099  default :
2100  /* Update the error code */
2101  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2102  /* update return status */
2103  status = HAL_ERROR;
2104  break;
2105  }
2106  }
2107  else
2108  {
2109  /* Update the error code */
2110  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2111  /* update return status */
2112  status = HAL_ERROR;
2113  }
2114 
2115  /* Release Lock */
2116  __HAL_UNLOCK(hmmc);
2117  return status;
2118 }
2119 
2139 {
2140  HAL_StatusTypeDef status = HAL_OK;
2141 
2142  /* Process locked */
2143  __HAL_LOCK(hmmc);
2144 
2145  if(hmmc->State == HAL_MMC_STATE_READY)
2146  {
2147  switch (CallbackId)
2148  {
2149  case HAL_MMC_TX_CPLT_CB_ID :
2150  hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
2151  break;
2152  case HAL_MMC_RX_CPLT_CB_ID :
2153  hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
2154  break;
2155  case HAL_MMC_ERROR_CB_ID :
2156  hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
2157  break;
2158  case HAL_MMC_ABORT_CB_ID :
2159  hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
2160  break;
2161 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2163  hmmc->Read_DMADblBuf0CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback;
2164  break;
2166  hmmc->Read_DMADblBuf1CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback;
2167  break;
2169  hmmc->Write_DMADblBuf0CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback;
2170  break;
2172  hmmc->Write_DMADblBuf1CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback;
2173  break;
2174 #endif
2175  case HAL_MMC_MSP_INIT_CB_ID :
2176  hmmc->MspInitCallback = HAL_MMC_MspInit;
2177  break;
2179  hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
2180  break;
2181  default :
2182  /* Update the error code */
2183  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2184  /* update return status */
2185  status = HAL_ERROR;
2186  break;
2187  }
2188  }
2189  else if (hmmc->State == HAL_MMC_STATE_RESET)
2190  {
2191  switch (CallbackId)
2192  {
2193  case HAL_MMC_MSP_INIT_CB_ID :
2194  hmmc->MspInitCallback = HAL_MMC_MspInit;
2195  break;
2197  hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
2198  break;
2199  default :
2200  /* Update the error code */
2201  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2202  /* update return status */
2203  status = HAL_ERROR;
2204  break;
2205  }
2206  }
2207  else
2208  {
2209  /* Update the error code */
2210  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2211  /* update return status */
2212  status = HAL_ERROR;
2213  }
2214 
2215  /* Release Lock */
2216  __HAL_UNLOCK(hmmc);
2217  return status;
2218 }
2219 #endif
2220 
2249 {
2250  pCID->ManufacturerID = (uint8_t)((hmmc->CID[0] & 0xFF000000U) >> 24U);
2251 
2252  pCID->OEM_AppliID = (uint16_t)((hmmc->CID[0] & 0x00FFFF00U) >> 8U);
2253 
2254  pCID->ProdName1 = (((hmmc->CID[0] & 0x000000FFU) << 24U) | ((hmmc->CID[1] & 0xFFFFFF00U) >> 8U));
2255 
2256  pCID->ProdName2 = (uint8_t)(hmmc->CID[1] & 0x000000FFU);
2257 
2258  pCID->ProdRev = (uint8_t)((hmmc->CID[2] & 0xFF000000U) >> 24U);
2259 
2260  pCID->ProdSN = (((hmmc->CID[2] & 0x00FFFFFFU) << 8U) | ((hmmc->CID[3] & 0xFF000000U) >> 24U));
2261 
2262  pCID->Reserved1 = (uint8_t)((hmmc->CID[3] & 0x00F00000U) >> 20U);
2263 
2264  pCID->ManufactDate = (uint16_t)((hmmc->CID[3] & 0x000FFF00U) >> 8U);
2265 
2266  pCID->CID_CRC = (uint8_t)((hmmc->CID[3] & 0x000000FEU) >> 1U);
2267 
2268  pCID->Reserved2 = 1U;
2269 
2270  return HAL_OK;
2271 }
2272 
2282 {
2283 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2284  uint32_t block_nbr = 0;
2285 #endif
2286 
2287  pCSD->CSDStruct = (uint8_t)((hmmc->CSD[0] & 0xC0000000U) >> 30U);
2288 
2289  pCSD->SysSpecVersion = (uint8_t)((hmmc->CSD[0] & 0x3C000000U) >> 26U);
2290 
2291  pCSD->Reserved1 = (uint8_t)((hmmc->CSD[0] & 0x03000000U) >> 24U);
2292 
2293  pCSD->TAAC = (uint8_t)((hmmc->CSD[0] & 0x00FF0000U) >> 16U);
2294 
2295  pCSD->NSAC = (uint8_t)((hmmc->CSD[0] & 0x0000FF00U) >> 8U);
2296 
2297  pCSD->MaxBusClkFrec = (uint8_t)(hmmc->CSD[0] & 0x000000FFU);
2298 
2299  pCSD->CardComdClasses = (uint16_t)((hmmc->CSD[1] & 0xFFF00000U) >> 20U);
2300 
2301  pCSD->RdBlockLen = (uint8_t)((hmmc->CSD[1] & 0x000F0000U) >> 16U);
2302 
2303  pCSD->PartBlockRead = (uint8_t)((hmmc->CSD[1] & 0x00008000U) >> 15U);
2304 
2305  pCSD->WrBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00004000U) >> 14U);
2306 
2307  pCSD->RdBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00002000U) >> 13U);
2308 
2309  pCSD->DSRImpl = (uint8_t)((hmmc->CSD[1] & 0x00001000U) >> 12U);
2310 
2311  pCSD->Reserved2 = 0U;
2313 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2314  if(MMC_ReadExtCSD(hmmc, &block_nbr, 0x0FFFFFFFU) != HAL_OK)
2315  {
2316  return HAL_ERROR;
2317  }
2318 
2319  if(hmmc->MmcCard.CardType == MMC_LOW_CAPACITY_CARD)
2320  {
2321 #endif
2322  pCSD->DeviceSize = (((hmmc->CSD[1] & 0x000003FFU) << 2U) | ((hmmc->CSD[2] & 0xC0000000U) >> 30U));
2323 
2324  pCSD->MaxRdCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x38000000U) >> 27U);
2325 
2326  pCSD->MaxRdCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x07000000U) >> 24U);
2327 
2328  pCSD->MaxWrCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x00E00000U) >> 21U);
2329 
2330  pCSD->MaxWrCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x001C0000U) >> 18U);
2331 
2332  pCSD->DeviceSizeMul = (uint8_t)((hmmc->CSD[2] & 0x00038000U) >> 15U);
2333 
2334  hmmc->MmcCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2335  hmmc->MmcCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2336  hmmc->MmcCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2337 
2338  hmmc->MmcCard.LogBlockNbr = (hmmc->MmcCard.BlockNbr) * ((hmmc->MmcCard.BlockSize) / 512U);
2339  hmmc->MmcCard.LogBlockSize = 512U;
2340 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2341  }
2342  else if(hmmc->MmcCard.CardType == MMC_HIGH_CAPACITY_CARD)
2343  {
2344  hmmc->MmcCard.BlockNbr = block_nbr;
2345  hmmc->MmcCard.LogBlockNbr = hmmc->MmcCard.BlockNbr;
2346  hmmc->MmcCard.BlockSize = 512U;
2347  hmmc->MmcCard.LogBlockSize = hmmc->MmcCard.BlockSize;
2348  }
2349  else
2350  {
2351  /* Clear all the static flags */
2352  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
2353  hmmc->ErrorCode |= HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
2354  hmmc->State = HAL_MMC_STATE_READY;
2355  return HAL_ERROR;
2356  }
2357 #endif
2358 
2359  pCSD->EraseGrSize = (uint8_t)((hmmc->CSD[2] & 0x00004000U) >> 14U);
2360 
2361  pCSD->EraseGrMul = (uint8_t)((hmmc->CSD[2] & 0x00003F80U) >> 7U);
2362 
2363  pCSD->WrProtectGrSize = (uint8_t)(hmmc->CSD[2] & 0x0000007FU);
2364 
2365  pCSD->WrProtectGrEnable = (uint8_t)((hmmc->CSD[3] & 0x80000000U) >> 31U);
2366 
2367  pCSD->ManDeflECC = (uint8_t)((hmmc->CSD[3] & 0x60000000U) >> 29U);
2368 
2369  pCSD->WrSpeedFact = (uint8_t)((hmmc->CSD[3] & 0x1C000000U) >> 26U);
2370 
2371  pCSD->MaxWrBlockLen= (uint8_t)((hmmc->CSD[3] & 0x03C00000U) >> 22U);
2372 
2373  pCSD->WriteBlockPaPartial = (uint8_t)((hmmc->CSD[3] & 0x00200000U) >> 21U);
2374 
2375  pCSD->Reserved3 = 0;
2376 
2377  pCSD->ContentProtectAppli = (uint8_t)((hmmc->CSD[3] & 0x00010000U) >> 16U);
2378 
2379  pCSD->FileFormatGroup = (uint8_t)((hmmc->CSD[3] & 0x00008000U) >> 15U);
2380 
2381  pCSD->CopyFlag = (uint8_t)((hmmc->CSD[3] & 0x00004000U) >> 14U);
2382 
2383  pCSD->PermWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00002000U) >> 13U);
2384 
2385  pCSD->TempWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00001000U) >> 12U);
2386 
2387  pCSD->FileFormat = (uint8_t)((hmmc->CSD[3] & 0x00000C00U) >> 10U);
2388 
2389  pCSD->ECC= (uint8_t)((hmmc->CSD[3] & 0x00000300U) >> 8U);
2390 
2391  pCSD->CSD_CRC = (uint8_t)((hmmc->CSD[3] & 0x000000FEU) >> 1U);
2392 
2393  pCSD->Reserved4 = 1;
2394 
2395  return HAL_OK;
2396 }
2397 
2406 {
2407  pCardInfo->CardType = (uint32_t)(hmmc->MmcCard.CardType);
2408  pCardInfo->Class = (uint32_t)(hmmc->MmcCard.Class);
2409  pCardInfo->RelCardAdd = (uint32_t)(hmmc->MmcCard.RelCardAdd);
2410  pCardInfo->BlockNbr = (uint32_t)(hmmc->MmcCard.BlockNbr);
2411  pCardInfo->BlockSize = (uint32_t)(hmmc->MmcCard.BlockSize);
2412  pCardInfo->LogBlockNbr = (uint32_t)(hmmc->MmcCard.LogBlockNbr);
2413  pCardInfo->LogBlockSize = (uint32_t)(hmmc->MmcCard.LogBlockSize);
2414 
2415  return HAL_OK;
2416 }
2417 
2429 HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
2430 {
2431  __IO uint32_t count = 0U;
2432  SDMMC_InitTypeDef Init;
2433  uint32_t errorstate;
2434  uint32_t response = 0U, busy = 0U;
2435 
2436  /* Check the parameters */
2437  assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2438 
2439  /* Chnage Satte */
2440  hmmc->State = HAL_MMC_STATE_BUSY;
2441 
2442 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2443  /* Update Clock for Bus mode update */
2444  Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
2445  Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
2446  Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
2447  Init.BusWide = WideMode;
2448  Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
2449  Init.ClockDiv = SDMMC_INIT_CLK_DIV;
2450  /* Initialize SDMMC*/
2451  (void)SDMMC_Init(hmmc->Instance, Init);
2452 #endif
2453 
2454  if(WideMode == SDMMC_BUS_WIDE_8B)
2455  {
2456  errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
2457  if(errorstate != HAL_MMC_ERROR_NONE)
2458  {
2459  hmmc->ErrorCode |= errorstate;
2460  }
2461  }
2462  else if(WideMode == SDMMC_BUS_WIDE_4B)
2463  {
2464  errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
2465  if(errorstate != HAL_MMC_ERROR_NONE)
2466  {
2467  hmmc->ErrorCode |= errorstate;
2468  }
2469  }
2470  else if(WideMode == SDMMC_BUS_WIDE_1B)
2471  {
2472  errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70000U);
2473  if(errorstate != HAL_MMC_ERROR_NONE)
2474  {
2475  hmmc->ErrorCode |= errorstate;
2476  }
2477  }
2478  else
2479  {
2480  /* WideMode is not a valid argument*/
2481  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
2482  }
2483 
2484  /* Check for switch error and violation of the trial number of sending CMD 13 */
2485  while(busy == 0U)
2486  {
2487  if(count == SDMMC_MAX_TRIAL)
2488  {
2489  hmmc->State = HAL_MMC_STATE_READY;
2490  hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
2491  return HAL_ERROR;
2492  }
2493  count++;
2494 
2495  /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
2496  errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
2497  if(errorstate != HAL_MMC_ERROR_NONE)
2498  {
2499  hmmc->ErrorCode |= errorstate;
2500  }
2501 
2502  /* Get command response */
2503  response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
2504 
2505  /* Get operating voltage*/
2506  busy = (((response >> 7U) == 1U) ? 0U : 1U);
2507  }
2508 
2509  /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
2510  count = SDMMC_DATATIMEOUT;
2511  while((response & 0x00000100U) == 0U)
2512  {
2513  if(count == 0U)
2514  {
2515  hmmc->State = HAL_MMC_STATE_READY;
2516  hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
2517  return HAL_ERROR;
2518  }
2519  count--;
2520 
2521  /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
2522  errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
2523  if(errorstate != HAL_MMC_ERROR_NONE)
2524  {
2525  hmmc->ErrorCode |= errorstate;
2526  }
2527 
2528  /* Get command response */
2529  response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
2530  }
2531 
2532  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
2533  {
2534  /* Clear all the static flags */
2535  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
2536  hmmc->State = HAL_MMC_STATE_READY;
2537  return HAL_ERROR;
2538  }
2539  else
2540  {
2541  /* Configure the SDMMC peripheral */
2542  Init.ClockEdge = hmmc->Init.ClockEdge;
2543 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2544  Init.ClockBypass = hmmc->Init.ClockBypass;
2545 #endif
2546  Init.ClockPowerSave = hmmc->Init.ClockPowerSave;
2547  Init.BusWide = WideMode;
2548  Init.HardwareFlowControl = hmmc->Init.HardwareFlowControl;
2549  Init.ClockDiv = hmmc->Init.ClockDiv;
2550  (void)SDMMC_Init(hmmc->Instance, Init);
2551  }
2552 
2553  /* Change State */
2554  hmmc->State = HAL_MMC_STATE_READY;
2555 
2556  return HAL_OK;
2557 }
2558 
2565 {
2566  uint32_t cardstate;
2567  uint32_t errorstate;
2568  uint32_t resp1 = 0U;
2569 
2570  errorstate = MMC_SendStatus(hmmc, &resp1);
2571  if(errorstate != HAL_MMC_ERROR_NONE)
2572  {
2573  hmmc->ErrorCode |= errorstate;
2574  }
2575 
2576  cardstate = ((resp1 >> 9U) & 0x0FU);
2577 
2578  return (HAL_MMC_CardStateTypeDef)cardstate;
2579 }
2580 
2587 HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
2588 {
2589  HAL_MMC_CardStateTypeDef CardState;
2590 
2591  /* DIsable All interrupts */
2592  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2593  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2594 
2595  /* Clear All flags */
2596  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
2597 
2598 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2599  if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
2600  {
2601  /* Disable the MMC DMA request */
2602  hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2603 
2604  /* Abort the MMC DMA Tx Stream */
2605  if(hmmc->hdmatx != NULL)
2606  {
2607  if(HAL_DMA_Abort(hmmc->hdmatx) != HAL_OK)
2608  {
2609  hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
2610  }
2611  }
2612  /* Abort the MMC DMA Rx Stream */
2613  if(hmmc->hdmarx != NULL)
2614  {
2615  if(HAL_DMA_Abort(hmmc->hdmarx) != HAL_OK)
2616  {
2617  hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
2618  }
2619  }
2620  }
2621 #else
2622  /* If IDMA Context, disable Internal DMA */
2623  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2624 #endif
2625 
2626  hmmc->State = HAL_MMC_STATE_READY;
2627 
2628  /* Initialize the MMC operation */
2629  hmmc->Context = MMC_CONTEXT_NONE;
2630 
2631  CardState = HAL_MMC_GetCardState(hmmc);
2632  if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
2633  {
2634  hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
2635  }
2636  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
2637  {
2638  return HAL_ERROR;
2639  }
2640  return HAL_OK;
2641 }
2642 
2649 HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
2650 {
2651  HAL_MMC_CardStateTypeDef CardState;
2652 
2653  /* DIsable All interrupts */
2654  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2655  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2656 
2657 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2658  /* If IDMA Context, disable Internal DMA */
2659  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2660 #endif
2661 
2662  /* Clear All flags */
2663  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
2664 
2665 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2666  if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
2667  {
2668  /* Disable the MMC DMA request */
2669  hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2670 
2671  /* Abort the MMC DMA Tx Stream */
2672  if(hmmc->hdmatx != NULL)
2673  {
2674  hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
2675  if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
2676  {
2677  hmmc->hdmatx = NULL;
2678  }
2679  }
2680  /* Abort the MMC DMA Rx Stream */
2681  if(hmmc->hdmarx != NULL)
2682  {
2683  hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
2684  if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
2685  {
2686  hmmc->hdmarx = NULL;
2687  }
2688  }
2689  }
2690 
2691  /* No transfer ongoing on both DMA channels*/
2692  if((hmmc->hdmatx == NULL) && (hmmc->hdmarx == NULL))
2693  {
2694 #endif
2695  CardState = HAL_MMC_GetCardState(hmmc);
2696  hmmc->State = HAL_MMC_STATE_READY;
2697 
2698  if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
2699  {
2700  hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
2701  }
2702  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
2703  {
2704  return HAL_ERROR;
2705  }
2706  else
2707  {
2708 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2709  hmmc->AbortCpltCallback(hmmc);
2710 #else
2711  HAL_MMC_AbortCallback(hmmc);
2712 #endif
2713 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2714  }
2715 #endif
2716  }
2717 
2718  return HAL_OK;
2719 }
2720 
2729 /* Private function ----------------------------------------------------------*/
2734 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2735 
2741 {
2742  MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
2743 
2744  /* Enable DATAEND Interrupt */
2745  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DATAEND));
2746 }
2747 
2754 {
2755  MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
2756  uint32_t errorstate;
2757 
2758  /* Send stop command in multiblock write */
2759  if(hmmc->Context == (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA))
2760  {
2761  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
2762  if(errorstate != HAL_MMC_ERROR_NONE)
2763  {
2764  hmmc->ErrorCode |= errorstate;
2765 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2766  hmmc->ErrorCallback(hmmc);
2767 #else
2768  HAL_MMC_ErrorCallback(hmmc);
2769 #endif
2770  }
2771  }
2772 
2773  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2774  in the MMC DCTRL register */
2775  hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2776 
2777  /* Clear all the static flags */
2778  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
2779 
2780  hmmc->State = HAL_MMC_STATE_READY;
2781 
2782 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2783  hmmc->RxCpltCallback(hmmc);
2784 #else
2785  HAL_MMC_RxCpltCallback(hmmc);
2786 #endif
2787 }
2788 
2794 static void MMC_DMAError(DMA_HandleTypeDef *hdma)
2795 {
2796  MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
2797  HAL_MMC_CardStateTypeDef CardState;
2798  uint32_t RxErrorCode, TxErrorCode;
2799 
2800  RxErrorCode = hmmc->hdmarx->ErrorCode;
2801  TxErrorCode = hmmc->hdmatx->ErrorCode;
2802  if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2803  {
2804  /* Clear All flags */
2805  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
2806 
2807  /* Disable All interrupts */
2808  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2809  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2810 
2811  hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
2812  CardState = HAL_MMC_GetCardState(hmmc);
2813  if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
2814  {
2815  hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
2816  }
2817 
2818  hmmc->State= HAL_MMC_STATE_READY;
2819  }
2820 
2821 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2822  hmmc->ErrorCallback(hmmc);
2823 #else
2824  HAL_MMC_ErrorCallback(hmmc);
2825 #endif
2826  }
2827 
2834 {
2835  MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
2836  HAL_MMC_CardStateTypeDef CardState;
2837 
2838  if(hmmc->hdmatx != NULL)
2839  {
2840  hmmc->hdmatx = NULL;
2841  }
2842 
2843  /* All DMA channels are aborted */
2844  if(hmmc->hdmarx == NULL)
2845  {
2846  CardState = HAL_MMC_GetCardState(hmmc);
2847  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
2848  hmmc->State = HAL_MMC_STATE_READY;
2849  if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
2850  {
2851  hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
2852 
2853  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
2854  {
2855 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2856  hmmc->AbortCpltCallback(hmmc);
2857 #else
2858  HAL_MMC_AbortCallback(hmmc);
2859 #endif
2860  }
2861  else
2862  {
2863 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2864  hmmc->ErrorCallback(hmmc);
2865 #else
2866  HAL_MMC_ErrorCallback(hmmc);
2867 #endif
2868  }
2869  }
2870  }
2871 }
2872 
2879 {
2880  MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
2881  HAL_MMC_CardStateTypeDef CardState;
2882 
2883  if(hmmc->hdmarx != NULL)
2884  {
2885  hmmc->hdmarx = NULL;
2886  }
2887 
2888  /* All DMA channels are aborted */
2889  if(hmmc->hdmatx == NULL)
2890  {
2891  CardState = HAL_MMC_GetCardState(hmmc);
2892  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
2893  hmmc->State = HAL_MMC_STATE_READY;
2894  if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
2895  {
2896  hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
2897 
2898  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
2899  {
2900 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2901  hmmc->AbortCpltCallback(hmmc);
2902 #else
2903  HAL_MMC_AbortCallback(hmmc);
2904 #endif
2905  }
2906  else
2907  {
2908 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
2909  hmmc->ErrorCallback(hmmc);
2910 #else
2911  HAL_MMC_ErrorCallback(hmmc);
2912 #endif
2913  }
2914  }
2915  }
2916 }
2917 #endif
2918 
2924 static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
2925 {
2927  uint32_t errorstate;
2928  uint16_t mmc_rca = 1U;
2929 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2930  MMC_InitTypeDef Init;
2931 #endif
2932 
2933  /* Check the power State */
2934  if(SDMMC_GetPowerState(hmmc->Instance) == 0U)
2935  {
2936  /* Power off */
2937  return HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
2938  }
2939 
2940  /* Send CMD2 ALL_SEND_CID */
2941  errorstate = SDMMC_CmdSendCID(hmmc->Instance);
2942  if(errorstate != HAL_MMC_ERROR_NONE)
2943  {
2944  return errorstate;
2945  }
2946  else
2947  {
2948  /* Get Card identification number data */
2949  hmmc->CID[0U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
2950  hmmc->CID[1U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2);
2951  hmmc->CID[2U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP3);
2952  hmmc->CID[3U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP4);
2953  }
2954 
2955  /* Send CMD3 SET_REL_ADDR with argument 0 */
2956  /* MMC Card publishes its RCA. */
2957  errorstate = SDMMC_CmdSetRelAdd(hmmc->Instance, &mmc_rca);
2958  if(errorstate != HAL_MMC_ERROR_NONE)
2959  {
2960  return errorstate;
2961  }
2962 
2963  /* Get the MMC card RCA */
2964  hmmc->MmcCard.RelCardAdd = mmc_rca;
2965 
2966  /* Send CMD9 SEND_CSD with argument as card's RCA */
2967  errorstate = SDMMC_CmdSendCSD(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
2968  if(errorstate != HAL_MMC_ERROR_NONE)
2969  {
2970  return errorstate;
2971  }
2972  else
2973  {
2974  /* Get Card Specific Data */
2975  hmmc->CSD[0U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
2976  hmmc->CSD[1U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2);
2977  hmmc->CSD[2U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP3);
2978  hmmc->CSD[3U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP4);
2979  }
2980 
2981  /* Get the Card Class */
2982  hmmc->MmcCard.Class = (SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2) >> 20U);
2983 
2984 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
2985  /* Get CSD parameters */
2986  if (HAL_MMC_GetCardCSD(hmmc, &CSD) != HAL_OK)
2987  {
2988  return hmmc->ErrorCode;
2989  }
2990 
2991  /* Select the Card */
2992  errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
2993  if(errorstate != HAL_MMC_ERROR_NONE)
2994  {
2995  return errorstate;
2996  }
2997 
2998  /* Configure SDMMC peripheral interface */
2999  (void)SDMMC_Init(hmmc->Instance, hmmc->Init);
3000 #else
3001  /* Select the Card */
3002  errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
3003  if(errorstate != HAL_MMC_ERROR_NONE)
3004  {
3005  return errorstate;
3006  }
3007 
3008  /* Get CSD parameters */
3009  if (HAL_MMC_GetCardCSD(hmmc, &CSD) != HAL_OK)
3010  {
3011  return hmmc->ErrorCode;
3012  }
3013 
3014  /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
3015  errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
3016  if(errorstate != HAL_MMC_ERROR_NONE)
3017  {
3018  hmmc->ErrorCode |= errorstate;
3019  }
3020 
3021  /* Configure the SDMMC peripheral */
3022  Init.ClockEdge = hmmc->Init.ClockEdge;
3023  Init.ClockPowerSave = hmmc->Init.ClockPowerSave;
3024  Init.BusWide = SDMMC_BUS_WIDE_1B;
3025  Init.HardwareFlowControl = hmmc->Init.HardwareFlowControl;
3026  Init.ClockDiv = hmmc->Init.ClockDiv;
3027  (void)SDMMC_Init(hmmc->Instance, Init);
3028 #endif
3029 
3030  /* All cards are initialized */
3031  return HAL_MMC_ERROR_NONE;
3032 }
3033 
3041 static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
3042 {
3043  __IO uint32_t count = 0U;
3044  uint32_t response = 0U, validvoltage = 0U;
3045  uint32_t errorstate;
3046 
3047  /* CMD0: GO_IDLE_STATE */
3048  errorstate = SDMMC_CmdGoIdleState(hmmc->Instance);
3049  if(errorstate != HAL_MMC_ERROR_NONE)
3050  {
3051  return errorstate;
3052  }
3053 
3054  while(validvoltage == 0U)
3055  {
3056  if(count++ == SDMMC_MAX_VOLT_TRIAL)
3057  {
3058  return HAL_MMC_ERROR_INVALID_VOLTRANGE;
3059  }
3060 
3061  /* SEND CMD1 APP_CMD with MMC_HIGH_VOLTAGE_RANGE(0xC0FF8000) as argument */
3062  errorstate = SDMMC_CmdOpCondition(hmmc->Instance, eMMC_HIGH_VOLTAGE_RANGE);
3063  if(errorstate != HAL_MMC_ERROR_NONE)
3064  {
3065  return HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
3066  }
3067 
3068  /* Get command response */
3069  response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
3070 
3071  /* Get operating voltage*/
3072  validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3073  }
3074 
3075  /* When power routine is finished and command returns valid voltage */
3076  if (((response & (0xFF000000U)) >> 24) == 0xC0U)
3077  {
3078  hmmc->MmcCard.CardType = MMC_HIGH_CAPACITY_CARD;
3079  }
3080  else
3081  {
3082  hmmc->MmcCard.CardType = MMC_LOW_CAPACITY_CARD;
3083  }
3084 
3085  return HAL_MMC_ERROR_NONE;
3086 }
3087 
3094 {
3095  /* Set Power State to OFF */
3096  (void)SDMMC_PowerState_OFF(hmmc->Instance);
3097 }
3098 
3106 static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
3107 {
3108  uint32_t errorstate;
3109 
3110  if(pCardStatus == NULL)
3111  {
3112  return HAL_MMC_ERROR_PARAM;
3113  }
3114 
3115  /* Send Status command */
3116  errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
3117  if(errorstate != HAL_MMC_ERROR_NONE)
3118  {
3119  return errorstate;
3120  }
3121 
3122  /* Get MMC card status */
3123  *pCardStatus = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
3124 
3125  return HAL_MMC_ERROR_NONE;
3126 }
3127 
3128 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
3129 
3136 HAL_StatusTypeDef MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pBlockNbr, uint32_t Timeout)
3137 {
3138  SDMMC_DataInitTypeDef config;
3139  uint32_t errorstate;
3140  uint32_t tickstart = HAL_GetTick();
3141  uint32_t count;
3142  uint32_t i = 0;
3143  uint32_t tmp_data;
3144 
3145  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
3146 
3147  /* Initialize data control register */
3148  hmmc->Instance->DCTRL = 0;
3149 
3150  /* Configure the MMC DPSM (Data Path State Machine) */
3151  config.DataTimeOut = SDMMC_DATATIMEOUT;
3152  config.DataLength = 0;
3153  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_1B;
3154  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3155  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3156  config.DPSM = SDMMC_DPSM_DISABLE;
3157  (void)SDMMC_ConfigData(hmmc->Instance, &config);
3158 
3159  /* Set Block Size for Card */
3160  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
3161  if(errorstate != HAL_MMC_ERROR_NONE)
3162  {
3163  /* Clear all the static flags */
3164  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
3165  hmmc->ErrorCode |= errorstate;
3166  hmmc->State = HAL_MMC_STATE_READY;
3167  return HAL_ERROR;
3168  }
3169 
3170  /* Configure the MMC DPSM (Data Path State Machine) */
3171  config.DataTimeOut = SDMMC_DATATIMEOUT;
3172  config.DataLength = 512;
3173  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
3174  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3175  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3176  config.DPSM = SDMMC_DPSM_ENABLE;
3177  (void)SDMMC_ConfigData(hmmc->Instance, &config);
3178 
3179  /* Set Block Size for Card */
3180  errorstate = SDMMC_CmdSendEXTCSD(hmmc->Instance, 0);
3181  if(errorstate != HAL_MMC_ERROR_NONE)
3182  {
3183  /* Clear all the static flags */
3184  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
3185  hmmc->ErrorCode |= errorstate;
3186  hmmc->State = HAL_MMC_STATE_READY;
3187  return HAL_ERROR;
3188  }
3189 
3190  /* Poll on SDMMC flags */
3191  while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3192  {
3193  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF))
3194  {
3195  /* Read data from SDMMC Rx FIFO */
3196  for(count = 0U; count < 8U; count++)
3197  {
3198  tmp_data = SDMMC_ReadFIFO(hmmc->Instance);
3199  if ((i == 48U) && (count == 5U))
3200  {
3201  *pBlockNbr = tmp_data;
3202  }
3203  }
3204  i += 8U;
3205  }
3206 
3207  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
3208  {
3209  /* Clear all the static flags */
3210  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
3211  hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
3212  hmmc->State= HAL_MMC_STATE_READY;
3213  return HAL_TIMEOUT;
3214  }
3215  }
3216 
3217  /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
3218  errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16));
3219  if(errorstate != HAL_MMC_ERROR_NONE)
3220  {
3221  hmmc->ErrorCode |= errorstate;
3222  }
3223 
3224  /* Clear all the static flags */
3225  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
3226 
3227  hmmc->State = HAL_MMC_STATE_READY;
3228 
3229  return HAL_OK;
3230 }
3231 #endif
3232 
3239 static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
3240 {
3241  uint32_t count, data, dataremaining;
3242  uint8_t* tmp;
3243 
3244  tmp = hmmc->pRxBuffPtr;
3245  dataremaining = hmmc->RxXferSize;
3246 
3247  if (dataremaining > 0U)
3248  {
3249  /* Read data from SDMMC Rx FIFO */
3250  for(count = 0U; count < 8U; count++)
3251  {
3252  data = SDMMC_ReadFIFO(hmmc->Instance);
3253  *tmp = (uint8_t)(data & 0xFFU);
3254  tmp++;
3255  dataremaining--;
3256  *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3257  tmp++;
3258  dataremaining--;
3259  *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3260  tmp++;
3261  dataremaining--;
3262  *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3263  tmp++;
3264  dataremaining--;
3265  }
3266 
3267  hmmc->pRxBuffPtr = tmp;
3268  hmmc->RxXferSize = dataremaining;
3269  }
3270 }
3271 
3279 {
3280  uint32_t count, data, dataremaining;
3281  uint8_t* tmp;
3282 
3283  tmp = hmmc->pTxBuffPtr;
3284  dataremaining = hmmc->TxXferSize;
3285 
3286  if (dataremaining > 0U)
3287  {
3288  /* Write data to SDMMC Tx FIFO */
3289  for(count = 0U; count < 8U; count++)
3290  {
3291  data = (uint32_t)(*tmp);
3292  tmp++;
3293  dataremaining--;
3294  data |= ((uint32_t)(*tmp) << 8U);
3295  tmp++;
3296  dataremaining--;
3297  data |= ((uint32_t)(*tmp) << 16U);
3298  tmp++;
3299  dataremaining--;
3300  data |= ((uint32_t)(*tmp) << 24U);
3301  tmp++;
3302  dataremaining--;
3303  (void)SDMMC_WriteFIFO(hmmc->Instance, &data);
3304  }
3305 
3306  hmmc->pTxBuffPtr = tmp;
3307  hmmc->TxXferSize = dataremaining;
3308  }
3309 }
3310 
3323 #endif /* HAL_MMC_MODULE_ENABLED */
3324 
3325 #endif /* SDMMC1 */
3326 
3327 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
MMC Abort callbacks.
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
Enables wide bus operation for the requested card if supported by card.
uint32_t SDMMC_CmdSendEXTCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Send EXT_CSD command and check the response.
HAL_StatusTypeDef HAL_MMC_DeInit(MMC_HandleTypeDef *hmmc)
De-Initializes the MMC card.
HAL_StatusTypeDef HAL_MMC_UnRegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId)
Unregister a User MMC Callback MMC Callback is redirected to the weak (surcharged) predefined callbac...
uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint64_t Addr)
Send the Select Deselect command and check the response.
HAL_StatusTypeDef HAL_MMC_RegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId, pMMC_CallbackTypeDef pCallback)
Register a User MMC Callback To be used instead of the weak (surcharged) predefined callback...
DMA handle Structure definition.
struct __MMC_HandleTypeDef else typedef struct endif MMC_HandleTypeDef
MMC handle Structure definition.
static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
Enquires cards about their operating voltage and configures clock controls and stores MMC information...
uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Single Block command and check the response.
void HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 0 Transfer completed callbacks.
HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
Abort the current transfer and disable the MMC.
uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Multi Block command and check the response.
This file contains all the functions prototypes for the HAL module driver.
void HAL_MMC_MspInit(MMC_HandleTypeDef *hmmc)
Initializes the MMC MSP.
uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Sends host capacity support information and activates the card&#39;s initialization process. Send SDMMC_CMD_SEND_OP_COND command.
SDMMC Data Control structure.
uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
Send the Erase command and check the response.
uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Status command and check the response.
uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand.
HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
Reads block(s) from a specified address in a card. The Data transfer is managed in interrupt mode...
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__IO uint8_t ContentProtectAppli
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
__HAL_UNLOCK(hrtc)
HAL_StatusTypeDef HAL_MMC_InitCard(MMC_HandleTypeDef *hmmc)
Initializes the MMC Card.
static void MMC_Write_IT(MMC_HandleTypeDef *hmmc)
Wrap up writing in non-blocking mode.
static void MMC_DMAError(DMA_HandleTypeDef *hdma)
DMA MMC communication error callback.
uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
Get SDMMC Power state.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
Send the Send CSD command and check the response.
HAL_StatusTypeDef HAL_MMC_Erase(MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
Erases the specified memory area of the given MMC card.
__HAL_LOCK(hrtc)
void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
MMC error callbacks.
void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
Tx Transfer completed callbacks.
HAL_MMC_CardStateTypeDef HAL_MMC_GetCardState(MMC_HandleTypeDef *hmmc)
Gets the current mmc card data state.
WWDG_InitTypeDef Init
HAL_StatusTypeDef HAL_MMC_WriteBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
Allows to write block(s) to a specified address in a card. The Data transfer is managed by polling mo...
void HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 0 Transfer completed callbacks.
HAL_StatusTypeDef HAL_MMC_ReadBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
Reads block(s) from a specified address in a card. The Data transfer is managed by polling mode...
void(* pMMC_CallbackTypeDef)(MMC_HandleTypeDef *hmmc)
HAL_MMC_CallbackIDTypeDef
static HAL_StatusTypeDef MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pBlockNbr, uint32_t Timeout)
Reads extended CSD register to get the sectors number of the device.
static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA MMC receive process complete callback.
return HAL_OK
HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
Writes block(s) to a specified address in a card. The Data transfer is managed by DMA mode...
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
Send the Send CSD command and check the response.
uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
Send the Send CID command and check the response.
static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
Wrap up reading in non-blocking mode.
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Abort the DMA Transfer.
static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
Returns the current card&#39;s status.
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
DMA MMC transmit process complete callback.
static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
Initializes the mmc card.
uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Single Block command and check the response.
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
DMA MMC Rx Abort callback.
uint32_t HAL_MMC_CardStateTypeDef
uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
Send the Go Idle State command and check the response.
HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
Abort the current transfer and disable the MMC (IT mode).
MMC Card Information Structure definition.
void HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 1 Transfer completed callbacks.
HAL_StatusTypeDef HAL_MMC_GetCardCID(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCIDTypeDef *pCID)
Returns information the information of the card which are stored on the CID register.
void HAL_MMC_MspDeInit(MMC_HandleTypeDef *hmmc)
De-Initialize MMC MSP.
void HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 1 Transfer completed callbacks.
uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
Send the End Address Erase command and check the response.
HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
Initializes the SDMMC according to the specified parameters in the SDMMC_InitTypeDef and create the a...
static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
DMA MMC Tx Abort callback.
uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Multi Block command and check the response.
uint32_t HAL_MMC_GetError(MMC_HandleTypeDef *hmmc)
Return the MMC error code.
uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
Send the Start Address Erase command and check the response.
HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
Set SDMMC Power state to ON.
HAL_StatusTypeDef HAL_MMC_GetCardCSD(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCSDTypeDef *pCSD)
Returns information the information of the card which are stored on the CSD register.
HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
Writes block(s) to a specified address in a card. The Data transfer is managed in interrupt mode...
HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
return the MMC state
HAL_StatusTypeDef HAL_MMC_GetCardInfo(MMC_HandleTypeDef *hmmc, HAL_MMC_CardInfoTypeDef *pCardInfo)
Gets the MMC card info.
HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
Write data (word) to Tx FIFO in blocking mode (polling)
__IO uint8_t WriteBlockPaPartial
HAL_StatusTypeDef HAL_MMC_Init(MMC_HandleTypeDef *hmmc)
Initializes the MMC according to the specified parameters in the MMC_HandleTypeDef and create the ass...
void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
Rx Transfer completed callbacks.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
Set SDMMC Power state to OFF.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.
void HAL_MMC_IRQHandler(MMC_HandleTypeDef *hmmc)
This function handles MMC card interrupt request.
HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
Reads block(s) from a specified address in a card. The Data transfer is managed by DMA mode...
static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc)
Turns the SDMMC output signals off.