stm32f7xx_ll_sdmmc.c
Go to the documentation of this file.
1 
158 /* Includes ------------------------------------------------------------------*/
159 #include "stm32f7xx_hal.h"
160 
161 #if defined(SDMMC1)
162 
172 #if defined(HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
173 
174 /* Private typedef -----------------------------------------------------------*/
175 /* Private define ------------------------------------------------------------*/
176 /* Private macro -------------------------------------------------------------*/
177 /* Private variables ---------------------------------------------------------*/
178 /* Private function prototypes -----------------------------------------------*/
179 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx);
180 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout);
181 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx);
182 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx);
183 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx);
184 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA);
185 
186 /* Exported functions --------------------------------------------------------*/
187 
212 HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
213 {
214  uint32_t tmpreg = 0;
215 
216  /* Check the parameters */
218  assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge));
219  assert_param(IS_SDMMC_CLOCK_BYPASS(Init.ClockBypass));
220  assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
221  assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
222  assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
223  assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
224 
225  /* Set SDMMC configuration parameters */
226  tmpreg |= (Init.ClockEdge |\
227  Init.ClockBypass |\
228  Init.ClockPowerSave |\
229  Init.BusWide |\
230  Init.HardwareFlowControl |\
231  Init.ClockDiv
232  );
233 
234  /* Write to SDMMC CLKCR */
235  MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
236 
237  return HAL_OK;
238 }
239 
240 
265 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
266 {
267  /* Read data from Rx FIFO */
268  return (SDMMCx->FIFO);
269 }
270 
277 HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
278 {
279  /* Write data to FIFO */
280  SDMMCx->FIFO = *pWriteData;
281 
282  return HAL_OK;
283 }
284 
309 HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
310 {
311  /* Set power state to ON */
312  SDMMCx->POWER = SDMMC_POWER_PWRCTRL;
313 
314  /* 1ms: required power up waiting time before starting the SD initialization
315  sequence */
316  HAL_Delay(2);
317 
318  return HAL_OK;
319 }
320 
326 HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
327 {
328  /* Set power state to OFF */
329  SDMMCx->POWER = (uint32_t)0x00000000;
330 
331  return HAL_OK;
332 }
333 
343 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
344 {
345  return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);
346 }
347 
356 HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)
357 {
358  uint32_t tmpreg = 0;
359 
360  /* Check the parameters */
361  assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));
362  assert_param(IS_SDMMC_RESPONSE(Command->Response));
363  assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));
364  assert_param(IS_SDMMC_CPSM(Command->CPSM));
365 
366  /* Set the SDMMC Argument value */
367  SDMMCx->ARG = Command->Argument;
368 
369  /* Set SDMMC command parameters */
370  tmpreg |= (uint32_t)(Command->CmdIndex |\
371  Command->Response |\
372  Command->WaitForInterrupt |\
373  Command->CPSM);
374 
375  /* Write to SDMMC CMD register */
376  MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);
377 
378  return HAL_OK;
379 }
380 
386 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
387 {
388  return (uint8_t)(SDMMCx->RESPCMD);
389 }
390 
391 
403 uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
404 {
405  uint32_t tmp;
406 
407  /* Check the parameters */
408  assert_param(IS_SDMMC_RESP(Response));
409 
410  /* Get the response */
411  tmp = (uint32_t)(&(SDMMCx->RESP1)) + Response;
412 
413  return (*(__IO uint32_t *) tmp);
414 }
415 
424 HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef* Data)
425 {
426  uint32_t tmpreg = 0;
427 
428  /* Check the parameters */
429  assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));
430  assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));
431  assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));
432  assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));
433  assert_param(IS_SDMMC_DPSM(Data->DPSM));
434 
435  /* Set the SDMMC Data TimeOut value */
436  SDMMCx->DTIMER = Data->DataTimeOut;
437 
438  /* Set the SDMMC DataLength value */
439  SDMMCx->DLEN = Data->DataLength;
440 
441  /* Set the SDMMC data configuration parameters */
442  tmpreg |= (uint32_t)(Data->DataBlockSize |\
443  Data->TransferDir |\
444  Data->TransferMode |\
445  Data->DPSM);
446 
447  /* Write to SDMMC DCTRL */
448  MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
449 
450  return HAL_OK;
451 
452 }
453 
459 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)
460 {
461  return (SDMMCx->DCOUNT);
462 }
463 
469 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)
470 {
471  return (SDMMCx->FIFO);
472 }
473 
483 HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)
484 {
485  /* Check the parameters */
486  assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));
487 
488  /* Set SDMMC read wait mode */
489  MODIFY_REG(SDMMCx->DCTRL, SDMMC_DCTRL_RWMOD, SDMMC_ReadWaitMode);
490 
491  return HAL_OK;
492 }
493 
518 uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
519 {
520  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
521  uint32_t errorstate;
522 
523  /* Set Block Size for Card */
524  sdmmc_cmdinit.Argument = (uint32_t)BlockSize;
525  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCKLEN;
526  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
527  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
528  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
529  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
530 
531  /* Check for error conditions */
532  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SET_BLOCKLEN, SDMMC_CMDTIMEOUT);
533 
534  return errorstate;
535 }
536 
542 uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
543 {
544  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
545  uint32_t errorstate;
546 
547  /* Set Block Size for Card */
548  sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
549  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_SINGLE_BLOCK;
550  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
551  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
552  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
553  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
554 
555  /* Check for error conditions */
556  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
557 
558  return errorstate;
559 }
560 
566 uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
567 {
568  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
569  uint32_t errorstate;
570 
571  /* Set Block Size for Card */
572  sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
573  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_MULT_BLOCK;
574  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
575  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
576  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
577  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
578 
579  /* Check for error conditions */
580  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_MULT_BLOCK, SDMMC_CMDTIMEOUT);
581 
582  return errorstate;
583 }
584 
590 uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
591 {
592  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
593  uint32_t errorstate;
594 
595  /* Set Block Size for Card */
596  sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
597  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_SINGLE_BLOCK;
598  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
599  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
600  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
601  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
602 
603  /* Check for error conditions */
604  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
605 
606  return errorstate;
607 }
608 
614 uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
615 {
616  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
617  uint32_t errorstate;
618 
619  /* Set Block Size for Card */
620  sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
621  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_MULT_BLOCK;
622  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
623  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
624  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
625  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
626 
627  /* Check for error conditions */
628  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_MULT_BLOCK, SDMMC_CMDTIMEOUT);
629 
630  return errorstate;
631 }
632 
638 uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
639 {
640  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
641  uint32_t errorstate;
642 
643  /* Set Block Size for Card */
644  sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
645  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_START;
646  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
647  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
648  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
649  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
650 
651  /* Check for error conditions */
652  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
653 
654  return errorstate;
655 }
656 
662 uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
663 {
664  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
665  uint32_t errorstate;
666 
667  /* Set Block Size for Card */
668  sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
669  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_END;
670  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
671  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
672  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
673  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
674 
675  /* Check for error conditions */
676  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
677 
678  return errorstate;
679 }
680 
686 uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
687 {
688  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
689  uint32_t errorstate;
690 
691  /* Set Block Size for Card */
692  sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
693  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_START;
694  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
695  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
696  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
697  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
698 
699  /* Check for error conditions */
700  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
701 
702  return errorstate;
703 }
704 
710 uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
711 {
712  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
713  uint32_t errorstate;
714 
715  /* Set Block Size for Card */
716  sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
717  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_END;
718  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
719  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
720  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
721  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
722 
723  /* Check for error conditions */
724  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
725 
726  return errorstate;
727 }
728 
734 uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
735 {
736  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
737  uint32_t errorstate;
738 
739  /* Set Block Size for Card */
740  sdmmc_cmdinit.Argument = 0U;
741  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE;
742  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
743  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
744  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
745  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
746 
747  /* Check for error conditions */
748  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE, SDMMC_MAXERASETIMEOUT);
749 
750  return errorstate;
751 }
752 
758 uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
759 {
760  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
761  uint32_t errorstate;
762 
763  /* Send CMD12 STOP_TRANSMISSION */
764  sdmmc_cmdinit.Argument = 0U;
765  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_STOP_TRANSMISSION;
766  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
767  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
768  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
769  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
770 
771  /* Check for error conditions */
772  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_STOP_TRANSMISSION, SDMMC_STOPTRANSFERTIMEOUT);
773 
774  return errorstate;
775 }
776 
783 uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint64_t Addr)
784 {
785  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
786  uint32_t errorstate;
787 
788  /* Send CMD7 SDMMC_SEL_DESEL_CARD */
789  sdmmc_cmdinit.Argument = (uint32_t)Addr;
790  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEL_DESEL_CARD;
791  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
792  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
793  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
794  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
795 
796  /* Check for error conditions */
797  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEL_DESEL_CARD, SDMMC_CMDTIMEOUT);
798 
799  return errorstate;
800 }
801 
807 uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
808 {
809  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
810  uint32_t errorstate;
811 
812  sdmmc_cmdinit.Argument = 0U;
813  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_GO_IDLE_STATE;
814  sdmmc_cmdinit.Response = SDMMC_RESPONSE_NO;
815  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
816  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
817  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
818 
819  /* Check for error conditions */
820  errorstate = SDMMC_GetCmdError(SDMMCx);
821 
822  return errorstate;
823 }
824 
830 uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef *SDMMCx)
831 {
832  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
833  uint32_t errorstate;
834 
835  /* Send CMD8 to verify SD card interface operating condition */
836  /* Argument: - [31:12]: Reserved (shall be set to '0')
837  - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
838  - [7:0]: Check Pattern (recommended 0xAA) */
839  /* CMD Response: R7 */
840  sdmmc_cmdinit.Argument = SDMMC_CHECK_PATTERN;
841  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
842  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
843  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
844  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
845  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
846 
847  /* Check for error conditions */
848  errorstate = SDMMC_GetCmdResp7(SDMMCx);
849 
850  return errorstate;
851 }
852 
861 uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
862 {
863  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
864  uint32_t errorstate;
865 
866  sdmmc_cmdinit.Argument = (uint32_t)Argument;
867  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
868  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
869  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
870  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
871  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
872 
873  /* Check for error conditions */
874  /* If there is a HAL_ERROR, it is a MMC card, else
875  it is a SD card: SD card 2.0 (voltage range mismatch)
876  or SD card 1.x */
877  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_CMD, SDMMC_CMDTIMEOUT);
878 
879  return errorstate;
880 }
881 
889 uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
890 {
891  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
892  uint32_t errorstate;
893 
894  sdmmc_cmdinit.Argument = SDMMC_VOLTAGE_WINDOW_SD | Argument;
895  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
896  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
897  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
898  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
899  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
900 
901  /* Check for error conditions */
902  errorstate = SDMMC_GetCmdResp3(SDMMCx);
903 
904  return errorstate;
905 }
906 
913 uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
914 {
915  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
916  uint32_t errorstate;
917 
918  sdmmc_cmdinit.Argument = (uint32_t)BusWidth;
919  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_SD_SET_BUSWIDTH;
920  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
921  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
922  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
923  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
924 
925  /* Check for error conditions */
926  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDMMC_CMDTIMEOUT);
927 
928  return errorstate;
929 }
930 
936 uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
937 {
938  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
939  uint32_t errorstate;
940 
941  /* Send CMD51 SD_APP_SEND_SCR */
942  sdmmc_cmdinit.Argument = 0U;
943  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
944  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
945  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
946  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
947  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
948 
949  /* Check for error conditions */
950  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_SEND_SCR, SDMMC_CMDTIMEOUT);
951 
952  return errorstate;
953 }
954 
960 uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
961 {
962  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
963  uint32_t errorstate;
964 
965  /* Send CMD2 ALL_SEND_CID */
966  sdmmc_cmdinit.Argument = 0U;
967  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
968  sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
969  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
970  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
971  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
972 
973  /* Check for error conditions */
974  errorstate = SDMMC_GetCmdResp2(SDMMCx);
975 
976  return errorstate;
977 }
978 
985 uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
986 {
987  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
988  uint32_t errorstate;
989 
990  /* Send CMD9 SEND_CSD */
991  sdmmc_cmdinit.Argument = Argument;
992  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
993  sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
994  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
995  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
996  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
997 
998  /* Check for error conditions */
999  errorstate = SDMMC_GetCmdResp2(SDMMCx);
1000 
1001  return errorstate;
1002 }
1003 
1010 uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
1011 {
1012  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1013  uint32_t errorstate;
1014 
1015  /* Send CMD3 SD_CMD_SET_REL_ADDR */
1016  sdmmc_cmdinit.Argument = 0U;
1017  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
1018  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1019  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1020  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1021  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1022 
1023  /* Check for error conditions */
1024  errorstate = SDMMC_GetCmdResp6(SDMMCx, SDMMC_CMD_SET_REL_ADDR, pRCA);
1025 
1026  return errorstate;
1027 }
1028 
1035 uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1036 {
1037  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1038  uint32_t errorstate;
1039 
1040  sdmmc_cmdinit.Argument = Argument;
1041  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
1042  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1043  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1044  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1045  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1046 
1047  /* Check for error conditions */
1048  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEND_STATUS, SDMMC_CMDTIMEOUT);
1049 
1050  return errorstate;
1051 }
1052 
1058 uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
1059 {
1060  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1061  uint32_t errorstate;
1062 
1063  sdmmc_cmdinit.Argument = 0U;
1064  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
1065  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1066  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1067  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1068  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1069 
1070  /* Check for error conditions */
1071  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_STATUS, SDMMC_CMDTIMEOUT);
1072 
1073  return errorstate;
1074 }
1075 
1083 uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1084 {
1085  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1086  uint32_t errorstate;
1087 
1088  sdmmc_cmdinit.Argument = Argument;
1089  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
1090  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1091  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1092  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1093  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1094 
1095  /* Check for error conditions */
1096  errorstate = SDMMC_GetCmdResp3(SDMMCx);
1097 
1098  return errorstate;
1099 }
1100 
1107 uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1108 {
1109  SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1110  uint32_t errorstate;
1111 
1112  /* Send CMD6 to activate SDR50 Mode and Power Limit 1.44W */
1113  /* CMD Response: R1 */
1114  sdmmc_cmdinit.Argument = Argument; /* SDMMC_SDR25_SWITCH_PATTERN */
1115  sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
1116  sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1117  sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1118  sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1119  (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1120 
1121  /* Check for error conditions */
1122  errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SWITCH, SDMMC_CMDTIMEOUT);
1123 
1124  return errorstate;
1125 }
1126 
1131 /* Private function ----------------------------------------------------------*/
1141 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx)
1142 {
1143  /* 8 is the number of required instructions cycles for the below loop statement.
1144  The SDMMC_CMDTIMEOUT is expressed in ms */
1145  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1146 
1147  do
1148  {
1149  if (count-- == 0U)
1150  {
1151  return SDMMC_ERROR_TIMEOUT;
1152  }
1153 
1154  }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
1155 
1156  /* Clear all the static flags */
1157  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1158 
1159  return SDMMC_ERROR_NONE;
1160 }
1161 
1168 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
1169 {
1170  uint32_t response_r1;
1171  uint32_t sta_reg;
1172 
1173  /* 8 is the number of required instructions cycles for the below loop statement.
1174  The Timeout is expressed in ms */
1175  register uint32_t count = Timeout * (SystemCoreClock / 8U /1000U);
1176 
1177  do
1178  {
1179  if (count-- == 0U)
1180  {
1181  return SDMMC_ERROR_TIMEOUT;
1182  }
1183  sta_reg = SDMMCx->STA;
1184  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1185  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1186 
1187  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1188  {
1189  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1190 
1191  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1192  }
1193  else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1194  {
1195  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1196 
1197  return SDMMC_ERROR_CMD_CRC_FAIL;
1198  }
1199  else
1200  {
1201  /* Nothing to do */
1202  }
1203 
1204  /* Clear all the static flags */
1205  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1206 
1207  /* Check response received is of desired command */
1208  if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1209  {
1210  return SDMMC_ERROR_CMD_CRC_FAIL;
1211  }
1212 
1213  /* We have received response, retrieve it for analysis */
1214  response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1215 
1216  if((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
1217  {
1218  return SDMMC_ERROR_NONE;
1219  }
1220  else if((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
1221  {
1222  return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
1223  }
1224  else if((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
1225  {
1226  return SDMMC_ERROR_ADDR_MISALIGNED;
1227  }
1228  else if((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
1229  {
1230  return SDMMC_ERROR_BLOCK_LEN_ERR;
1231  }
1232  else if((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
1233  {
1234  return SDMMC_ERROR_ERASE_SEQ_ERR;
1235  }
1236  else if((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
1237  {
1238  return SDMMC_ERROR_BAD_ERASE_PARAM;
1239  }
1240  else if((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
1241  {
1242  return SDMMC_ERROR_WRITE_PROT_VIOLATION;
1243  }
1244  else if((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
1245  {
1246  return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
1247  }
1248  else if((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
1249  {
1250  return SDMMC_ERROR_COM_CRC_FAILED;
1251  }
1252  else if((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
1253  {
1254  return SDMMC_ERROR_ILLEGAL_CMD;
1255  }
1256  else if((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
1257  {
1258  return SDMMC_ERROR_CARD_ECC_FAILED;
1259  }
1260  else if((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
1261  {
1262  return SDMMC_ERROR_CC_ERR;
1263  }
1264  else if((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
1265  {
1266  return SDMMC_ERROR_STREAM_READ_UNDERRUN;
1267  }
1268  else if((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
1269  {
1270  return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
1271  }
1272  else if((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
1273  {
1274  return SDMMC_ERROR_CID_CSD_OVERWRITE;
1275  }
1276  else if((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
1277  {
1278  return SDMMC_ERROR_WP_ERASE_SKIP;
1279  }
1280  else if((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
1281  {
1282  return SDMMC_ERROR_CARD_ECC_DISABLED;
1283  }
1284  else if((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
1285  {
1286  return SDMMC_ERROR_ERASE_RESET;
1287  }
1288  else if((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
1289  {
1290  return SDMMC_ERROR_AKE_SEQ_ERR;
1291  }
1292  else
1293  {
1294  return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1295  }
1296 }
1297 
1303 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx)
1304 {
1305  uint32_t sta_reg;
1306  /* 8 is the number of required instructions cycles for the below loop statement.
1307  The SDMMC_CMDTIMEOUT is expressed in ms */
1308  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1309 
1310  do
1311  {
1312  if (count-- == 0U)
1313  {
1314  return SDMMC_ERROR_TIMEOUT;
1315  }
1316  sta_reg = SDMMCx->STA;
1317  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1318  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1319 
1320  if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1321  {
1322  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1323 
1324  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1325  }
1326  else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1327  {
1328  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1329 
1330  return SDMMC_ERROR_CMD_CRC_FAIL;
1331  }
1332  else
1333  {
1334  /* No error flag set */
1335  /* Clear all the static flags */
1336  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1337  }
1338 
1339  return SDMMC_ERROR_NONE;
1340 }
1341 
1347 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx)
1348 {
1349  uint32_t sta_reg;
1350  /* 8 is the number of required instructions cycles for the below loop statement.
1351  The SDMMC_CMDTIMEOUT is expressed in ms */
1352  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1353 
1354  do
1355  {
1356  if (count-- == 0U)
1357  {
1358  return SDMMC_ERROR_TIMEOUT;
1359  }
1360  sta_reg = SDMMCx->STA;
1361  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1362  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1363 
1364  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1365  {
1366  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1367 
1368  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1369  }
1370  else
1371  {
1372  /* Clear all the static flags */
1373  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1374  }
1375 
1376  return SDMMC_ERROR_NONE;
1377 }
1378 
1387 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
1388 {
1389  uint32_t response_r1;
1390  uint32_t sta_reg;
1391 
1392  /* 8 is the number of required instructions cycles for the below loop statement.
1393  The SDMMC_CMDTIMEOUT is expressed in ms */
1394  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1395 
1396  do
1397  {
1398  if (count-- == 0U)
1399  {
1400  return SDMMC_ERROR_TIMEOUT;
1401  }
1402  sta_reg = SDMMCx->STA;
1403  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1404  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1405 
1406  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1407  {
1408  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1409 
1410  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1411  }
1412  else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1413  {
1414  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1415 
1416  return SDMMC_ERROR_CMD_CRC_FAIL;
1417  }
1418  else
1419  {
1420  /* Nothing to do */
1421  }
1422 
1423  /* Check response received is of desired command */
1424  if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1425  {
1426  return SDMMC_ERROR_CMD_CRC_FAIL;
1427  }
1428 
1429  /* Clear all the static flags */
1430  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1431 
1432  /* We have received response, retrieve it. */
1433  response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1434 
1435  if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
1436  {
1437  *pRCA = (uint16_t) (response_r1 >> 16);
1438 
1439  return SDMMC_ERROR_NONE;
1440  }
1441  else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
1442  {
1443  return SDMMC_ERROR_ILLEGAL_CMD;
1444  }
1445  else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
1446  {
1447  return SDMMC_ERROR_COM_CRC_FAILED;
1448  }
1449  else
1450  {
1451  return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1452  }
1453 }
1454 
1460 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx)
1461 {
1462  uint32_t sta_reg;
1463  /* 8 is the number of required instructions cycles for the below loop statement.
1464  The SDMMC_CMDTIMEOUT is expressed in ms */
1465  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1466 
1467  do
1468  {
1469  if (count-- == 0U)
1470  {
1471  return SDMMC_ERROR_TIMEOUT;
1472  }
1473  sta_reg = SDMMCx->STA;
1474  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1475  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1476 
1477  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1478  {
1479  /* Card is SD V2.0 compliant */
1480  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1481 
1482  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1483  }
1484  else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1485  {
1486  /* Card is SD V2.0 compliant */
1487  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1488 
1489  return SDMMC_ERROR_CMD_CRC_FAIL;
1490  }
1491  else
1492  {
1493  /* Nothing to do */
1494  }
1495 
1496  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
1497  {
1498  /* Card is SD V2.0 compliant */
1499  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
1500  }
1501 
1502  return SDMMC_ERROR_NONE;
1503 
1504 }
1505 
1510 #endif /* HAL_SD_MODULE_ENABLED || HAL_MMC_MODULE_ENABLED */
1511 
1519 #endif /* SDMMC1 */
1520 
1521 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
assert_param
#define assert_param(expr)
Include module's header file.
Definition: stm32f407/stm32f407g-disc1/Inc/stm32f4xx_hal_conf.h:353
__IO
#define __IO
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:237
Init
napi_value Init(napi_env env, napi_value exports)
Definition: porcupine/demo/c/pvrecorder/node/pv_recorder_napi.c:197
HAL_StatusTypeDef
HAL_StatusTypeDef
HAL Status structures definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:40
SDMMC_TypeDef::DTIMER
__IO uint32_t DTIMER
Definition: stm32f769xx.h:900
SDMMC_TypeDef::FIFO
__IO uint32_t FIFO
Definition: stm32f769xx.h:910
SDMMC_POWER_PWRCTRL
#define SDMMC_POWER_PWRCTRL
Definition: stm32f769xx.h:13420
SDMMC_TypeDef::DCOUNT
__I uint32_t DCOUNT
Definition: stm32f769xx.h:903
SDMMC_TypeDef::RESPCMD
__I uint32_t RESPCMD
Definition: stm32f769xx.h:895
SDMMC_DCTRL_RWMOD
#define SDMMC_DCTRL_RWMOD
Definition: stm32f769xx.h:13550
HAL_Delay
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c:389
HAL_OK
@ HAL_OK
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:42
SDMMC_TypeDef::CLKCR
__IO uint32_t CLKCR
Definition: stm32f769xx.h:892
SDMMC_TypeDef::CMD
__IO uint32_t CMD
Definition: stm32f769xx.h:894
SDMMC_TypeDef::ARG
__IO uint32_t ARG
Definition: stm32f769xx.h:893
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_MIMXRT1052.c:69
MODIFY_REG
#define MODIFY_REG(REG, CLEARMASK, SETMASK)
Definition: stm32f407/stm32f407g-disc1/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h:224
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
IS_SDMMC_ALL_INSTANCE
#define IS_SDMMC_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f769xx.h:21620
Command
static FMC_SDRAM_CommandTypeDef Command
Definition: stm32f769i_discovery_sdram.c:125
SDMMC_TypeDef::RESP1
__I uint32_t RESP1
Definition: stm32f769xx.h:896
SDMMC_TypeDef::DCTRL
__IO uint32_t DCTRL
Definition: stm32f769xx.h:902
SDMMC_TypeDef::POWER
__IO uint32_t POWER
Definition: stm32f769xx.h:891
SDMMC_TypeDef::DLEN
__IO uint32_t DLEN
Definition: stm32f769xx.h:901
SDMMC_TypeDef::STA
__I uint32_t STA
Definition: stm32f769xx.h:904
stm32f7xx_hal.h
This file contains all the functions prototypes for the HAL module driver.
SDMMC_TypeDef
SD host Interface.
Definition: stm32f769xx.h:889


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