File indexing completed on 2025-05-11 08:23:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 #include "stm32h7xx_hal.h"
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 #if defined (HAL_SD_MODULE_ENABLED) || defined (HAL_MMC_MODULE_ENABLED)
0167
0168
0169
0170
0171
0172
0173 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx);
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203 HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
0204 {
0205 uint32_t tmpreg = 0;
0206
0207
0208 assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx));
0209 assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge));
0210 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
0211 assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
0212 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
0213 assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
0214
0215
0216 tmpreg |= (Init.ClockEdge | \
0217 Init.ClockPowerSave | \
0218 Init.BusWide | \
0219 Init.HardwareFlowControl | \
0220 Init.ClockDiv
0221 );
0222
0223
0224 MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
0225
0226 return HAL_OK;
0227 }
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
0256 {
0257
0258 return (SDMMCx->FIFO);
0259 }
0260
0261
0262
0263
0264
0265
0266
0267 HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
0268 {
0269
0270 SDMMCx->FIFO = *pWriteData;
0271
0272 return HAL_OK;
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300 HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
0301 {
0302
0303 SDMMCx->POWER |= SDMMC_POWER_PWRCTRL;
0304
0305 return HAL_OK;
0306 }
0307
0308
0309
0310
0311
0312
0313 HAL_StatusTypeDef SDMMC_PowerState_Cycle(SDMMC_TypeDef *SDMMCx)
0314 {
0315
0316 SDMMCx->POWER |= SDMMC_POWER_PWRCTRL_1;
0317
0318 return HAL_OK;
0319 }
0320
0321
0322
0323
0324
0325
0326 HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
0327 {
0328
0329 SDMMCx->POWER &= ~(SDMMC_POWER_PWRCTRL);
0330
0331 return HAL_OK;
0332 }
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
0344 {
0345 return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);
0346 }
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356 HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)
0357 {
0358 uint32_t tmpreg = 0;
0359
0360
0361 assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));
0362 assert_param(IS_SDMMC_RESPONSE(Command->Response));
0363 assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));
0364 assert_param(IS_SDMMC_CPSM(Command->CPSM));
0365
0366
0367 SDMMCx->ARG = Command->Argument;
0368
0369
0370 tmpreg |= (uint32_t)(Command->CmdIndex | \
0371 Command->Response | \
0372 Command->WaitForInterrupt | \
0373 Command->CPSM);
0374
0375
0376 MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);
0377
0378 return HAL_OK;
0379 }
0380
0381
0382
0383
0384
0385
0386 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
0387 {
0388 return (uint8_t)(SDMMCx->RESPCMD);
0389 }
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403 uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
0404 {
0405 uint32_t tmp;
0406
0407
0408 assert_param(IS_SDMMC_RESP(Response));
0409
0410
0411 tmp = (uint32_t)(&(SDMMCx->RESP1)) + Response;
0412
0413 return (*(__IO uint32_t *) tmp);
0414 }
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
0425 {
0426 uint32_t tmpreg = 0;
0427
0428
0429 assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));
0430 assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));
0431 assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));
0432 assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));
0433 assert_param(IS_SDMMC_DPSM(Data->DPSM));
0434
0435
0436 SDMMCx->DTIMER = Data->DataTimeOut;
0437
0438
0439 SDMMCx->DLEN = Data->DataLength;
0440
0441
0442 tmpreg |= (uint32_t)(Data->DataBlockSize | \
0443 Data->TransferDir | \
0444 Data->TransferMode | \
0445 Data->DPSM);
0446
0447
0448 MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
0449
0450 return HAL_OK;
0451
0452 }
0453
0454
0455
0456
0457
0458
0459 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)
0460 {
0461 return (SDMMCx->DCOUNT);
0462 }
0463
0464
0465
0466
0467
0468
0469 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)
0470 {
0471 return (SDMMCx->FIFO);
0472 }
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483 HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)
0484 {
0485
0486 assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));
0487
0488
0489 MODIFY_REG(SDMMCx->DCTRL, SDMMC_DCTRL_RWMOD, SDMMC_ReadWaitMode);
0490
0491 return HAL_OK;
0492 }
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519 uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
0520 {
0521 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0522 uint32_t errorstate;
0523
0524
0525 sdmmc_cmdinit.Argument = (uint32_t)BlockSize;
0526 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCKLEN;
0527 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0528 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0529 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0530 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0531
0532
0533 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SET_BLOCKLEN, SDMMC_CMDTIMEOUT);
0534
0535 return errorstate;
0536 }
0537
0538
0539
0540
0541
0542
0543 uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
0544 {
0545 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0546 uint32_t errorstate;
0547
0548
0549 sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
0550 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_SINGLE_BLOCK;
0551 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0552 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0553 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0554 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0555
0556
0557 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
0558
0559 return errorstate;
0560 }
0561
0562
0563
0564
0565
0566
0567 uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
0568 {
0569 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0570 uint32_t errorstate;
0571
0572
0573 sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
0574 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_MULT_BLOCK;
0575 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0576 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0577 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0578 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0579
0580
0581 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_MULT_BLOCK, SDMMC_CMDTIMEOUT);
0582
0583 return errorstate;
0584 }
0585
0586
0587
0588
0589
0590
0591 uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
0592 {
0593 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0594 uint32_t errorstate;
0595
0596
0597 sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
0598 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_SINGLE_BLOCK;
0599 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0600 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0601 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0602 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0603
0604
0605 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
0606
0607 return errorstate;
0608 }
0609
0610
0611
0612
0613
0614
0615 uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
0616 {
0617 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0618 uint32_t errorstate;
0619
0620
0621 sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
0622 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_MULT_BLOCK;
0623 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0624 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0625 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0626 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0627
0628
0629 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_MULT_BLOCK, SDMMC_CMDTIMEOUT);
0630
0631 return errorstate;
0632 }
0633
0634
0635
0636
0637
0638
0639 uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
0640 {
0641 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0642 uint32_t errorstate;
0643
0644
0645 sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
0646 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_START;
0647 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0648 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0649 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0650 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0651
0652
0653 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
0654
0655 return errorstate;
0656 }
0657
0658
0659
0660
0661
0662
0663 uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
0664 {
0665 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0666 uint32_t errorstate;
0667
0668
0669 sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
0670 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_END;
0671 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0672 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0673 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0674 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0675
0676
0677 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
0678
0679 return errorstate;
0680 }
0681
0682
0683
0684
0685
0686
0687 uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
0688 {
0689 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0690 uint32_t errorstate;
0691
0692
0693 sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
0694 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_START;
0695 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0696 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0697 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0698 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0699
0700
0701 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
0702
0703 return errorstate;
0704 }
0705
0706
0707
0708
0709
0710
0711 uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
0712 {
0713 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0714 uint32_t errorstate;
0715
0716
0717 sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
0718 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_END;
0719 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0720 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0721 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0722 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0723
0724
0725 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
0726
0727 return errorstate;
0728 }
0729
0730
0731
0732
0733
0734
0735
0736 uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx, uint32_t EraseType)
0737 {
0738 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0739 uint32_t errorstate;
0740
0741
0742 sdmmc_cmdinit.Argument = EraseType;
0743 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE;
0744 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0745 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0746 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0747 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0748
0749
0750 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE, SDMMC_MAXERASETIMEOUT);
0751
0752 return errorstate;
0753 }
0754
0755
0756
0757
0758
0759
0760 uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
0761 {
0762 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0763 uint32_t errorstate;
0764
0765
0766 sdmmc_cmdinit.Argument = 0U;
0767 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_STOP_TRANSMISSION;
0768 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0769 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0770 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0771
0772 __SDMMC_CMDSTOP_ENABLE(SDMMCx);
0773 __SDMMC_CMDTRANS_DISABLE(SDMMCx);
0774
0775 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0776
0777
0778 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_STOP_TRANSMISSION, SDMMC_STOPTRANSFERTIMEOUT);
0779
0780 __SDMMC_CMDSTOP_DISABLE(SDMMCx);
0781
0782
0783 if (errorstate == SDMMC_ERROR_ADDR_OUT_OF_RANGE)
0784 {
0785 errorstate = SDMMC_ERROR_NONE;
0786 }
0787
0788 return errorstate;
0789 }
0790
0791
0792
0793
0794
0795
0796
0797 uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint32_t Addr)
0798 {
0799 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0800 uint32_t errorstate;
0801
0802
0803 sdmmc_cmdinit.Argument = (uint32_t)Addr;
0804 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEL_DESEL_CARD;
0805 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0806 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0807 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0808 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0809
0810
0811 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEL_DESEL_CARD, SDMMC_CMDTIMEOUT);
0812
0813 return errorstate;
0814 }
0815
0816
0817
0818
0819
0820
0821 uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
0822 {
0823 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0824 uint32_t errorstate;
0825
0826 sdmmc_cmdinit.Argument = 0U;
0827 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_GO_IDLE_STATE;
0828 sdmmc_cmdinit.Response = SDMMC_RESPONSE_NO;
0829 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0830 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0831 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0832
0833
0834 errorstate = SDMMC_GetCmdError(SDMMCx);
0835
0836 return errorstate;
0837 }
0838
0839
0840
0841
0842
0843
0844 uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef *SDMMCx)
0845 {
0846 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0847 uint32_t errorstate;
0848
0849
0850
0851
0852
0853
0854 sdmmc_cmdinit.Argument = SDMMC_CHECK_PATTERN;
0855 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
0856 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0857 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0858 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0859 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0860
0861
0862 errorstate = SDMMC_GetCmdResp7(SDMMCx);
0863
0864 return errorstate;
0865 }
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875 uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
0876 {
0877 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0878 uint32_t errorstate;
0879
0880 sdmmc_cmdinit.Argument = (uint32_t)Argument;
0881 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
0882 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0883 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0884 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0885 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0886
0887
0888
0889
0890
0891 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_CMD, SDMMC_CMDTIMEOUT);
0892
0893 return errorstate;
0894 }
0895
0896
0897
0898
0899
0900
0901
0902
0903 uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
0904 {
0905 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0906 uint32_t errorstate;
0907
0908 sdmmc_cmdinit.Argument = Argument;
0909 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
0910 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0911 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0912 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0913 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0914
0915
0916 errorstate = SDMMC_GetCmdResp3(SDMMCx);
0917
0918 return errorstate;
0919 }
0920
0921
0922
0923
0924
0925
0926
0927 uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
0928 {
0929 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0930 uint32_t errorstate;
0931
0932 sdmmc_cmdinit.Argument = (uint32_t)BusWidth;
0933 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_SD_SET_BUSWIDTH;
0934 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0935 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0936 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0937 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0938
0939
0940 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDMMC_CMDTIMEOUT);
0941
0942 return errorstate;
0943 }
0944
0945
0946
0947
0948
0949
0950 uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
0951 {
0952 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0953 uint32_t errorstate;
0954
0955
0956 sdmmc_cmdinit.Argument = 0U;
0957 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
0958 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
0959 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0960 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0961 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0962
0963
0964 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_SEND_SCR, SDMMC_CMDTIMEOUT);
0965
0966 return errorstate;
0967 }
0968
0969
0970
0971
0972
0973
0974 uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
0975 {
0976 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
0977 uint32_t errorstate;
0978
0979
0980 sdmmc_cmdinit.Argument = 0U;
0981 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
0982 sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
0983 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
0984 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
0985 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
0986
0987
0988 errorstate = SDMMC_GetCmdResp2(SDMMCx);
0989
0990 return errorstate;
0991 }
0992
0993
0994
0995
0996
0997
0998
0999 uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1000 {
1001 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1002 uint32_t errorstate;
1003
1004
1005 sdmmc_cmdinit.Argument = Argument;
1006 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
1007 sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
1008 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1009 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1010 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1011
1012
1013 errorstate = SDMMC_GetCmdResp2(SDMMCx);
1014
1015 return errorstate;
1016 }
1017
1018
1019
1020
1021
1022
1023
1024 uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
1025 {
1026 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1027 uint32_t errorstate;
1028
1029
1030 sdmmc_cmdinit.Argument = 0U;
1031 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
1032 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1033 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1034 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1035 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1036
1037
1038 errorstate = SDMMC_GetCmdResp6(SDMMCx, SDMMC_CMD_SET_REL_ADDR, pRCA);
1039
1040 return errorstate;
1041 }
1042
1043
1044
1045
1046
1047
1048
1049 uint32_t SDMMC_CmdSetRelAddMmc(SDMMC_TypeDef *SDMMCx, uint16_t RCA)
1050 {
1051 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1052 uint32_t errorstate;
1053
1054
1055 sdmmc_cmdinit.Argument = ((uint32_t)RCA << 16U);
1056 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
1057 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1058 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1059 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1060 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1061
1062
1063 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SET_REL_ADDR, SDMMC_CMDTIMEOUT);
1064
1065 return errorstate;
1066 }
1067
1068
1069
1070
1071
1072
1073
1074 uint32_t SDMMC_CmdSleepMmc(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1075 {
1076 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1077 uint32_t errorstate;
1078
1079
1080 sdmmc_cmdinit.Argument = Argument;
1081 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_MMC_SLEEP_AWAKE;
1082 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1083 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1084 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1085 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1086
1087
1088 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_MMC_SLEEP_AWAKE, SDMMC_CMDTIMEOUT);
1089
1090 return errorstate;
1091 }
1092
1093
1094
1095
1096
1097
1098
1099 uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1100 {
1101 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1102 uint32_t errorstate;
1103
1104 sdmmc_cmdinit.Argument = Argument;
1105 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
1106 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1107 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1108 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1109 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1110
1111
1112 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEND_STATUS, SDMMC_CMDTIMEOUT);
1113
1114 return errorstate;
1115 }
1116
1117
1118
1119
1120
1121
1122 uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
1123 {
1124 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1125 uint32_t errorstate;
1126
1127 sdmmc_cmdinit.Argument = 0U;
1128 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
1129 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1130 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1131 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1132 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1133
1134
1135 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_STATUS, SDMMC_CMDTIMEOUT);
1136
1137 return errorstate;
1138 }
1139
1140
1141
1142
1143
1144
1145
1146
1147 uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1148 {
1149 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1150 uint32_t errorstate;
1151
1152 sdmmc_cmdinit.Argument = Argument;
1153 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
1154 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1155 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1156 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1157 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1158
1159
1160 errorstate = SDMMC_GetCmdResp3(SDMMCx);
1161
1162 return errorstate;
1163 }
1164
1165
1166
1167
1168
1169
1170
1171 uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1172 {
1173 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1174 uint32_t errorstate;
1175
1176
1177
1178 sdmmc_cmdinit.Argument = Argument;
1179 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
1180 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1181 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1182 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1183 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1184
1185
1186 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SWITCH, SDMMC_CMDTIMEOUT);
1187
1188 return errorstate;
1189 }
1190
1191
1192
1193
1194
1195
1196
1197 uint32_t SDMMC_CmdVoltageSwitch(SDMMC_TypeDef *SDMMCx)
1198 {
1199 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1200 uint32_t errorstate;
1201
1202 sdmmc_cmdinit.Argument = 0x00000000;
1203 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_VOLTAGE_SWITCH;
1204 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1205 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1206 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1207 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1208
1209
1210 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_VOLTAGE_SWITCH, SDMMC_CMDTIMEOUT);
1211
1212 return errorstate;
1213 }
1214
1215
1216
1217
1218
1219
1220
1221 uint32_t SDMMC_CmdSendEXTCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1222 {
1223 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1224 uint32_t errorstate;
1225
1226
1227 sdmmc_cmdinit.Argument = Argument;
1228 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
1229 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1230 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1231 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1232 (void)SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1233
1234
1235 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SEND_EXT_CSD, SDMMC_CMDTIMEOUT);
1236
1237 return errorstate;
1238 }
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265 uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
1266 {
1267 uint32_t response_r1;
1268 uint32_t sta_reg;
1269
1270
1271
1272 uint32_t count = Timeout * (SystemCoreClock / 8U / 1000U);
1273
1274 do
1275 {
1276 if (count-- == 0U)
1277 {
1278 return SDMMC_ERROR_TIMEOUT;
1279 }
1280 sta_reg = SDMMCx->STA;
1281 } while (((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT |
1282 SDMMC_FLAG_BUSYD0END)) == 0U) || ((sta_reg & SDMMC_FLAG_CMDACT) != 0U));
1283
1284 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1285 {
1286 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1287
1288 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1289 }
1290 else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1291 {
1292 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1293
1294 return SDMMC_ERROR_CMD_CRC_FAIL;
1295 }
1296 else
1297 {
1298
1299 }
1300
1301
1302 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1303
1304
1305 if (SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1306 {
1307 return SDMMC_ERROR_CMD_CRC_FAIL;
1308 }
1309
1310
1311 response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1312
1313 if ((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
1314 {
1315 return SDMMC_ERROR_NONE;
1316 }
1317 else if ((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
1318 {
1319 return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
1320 }
1321 else if ((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
1322 {
1323 return SDMMC_ERROR_ADDR_MISALIGNED;
1324 }
1325 else if ((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
1326 {
1327 return SDMMC_ERROR_BLOCK_LEN_ERR;
1328 }
1329 else if ((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
1330 {
1331 return SDMMC_ERROR_ERASE_SEQ_ERR;
1332 }
1333 else if ((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
1334 {
1335 return SDMMC_ERROR_BAD_ERASE_PARAM;
1336 }
1337 else if ((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
1338 {
1339 return SDMMC_ERROR_WRITE_PROT_VIOLATION;
1340 }
1341 else if ((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
1342 {
1343 return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
1344 }
1345 else if ((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
1346 {
1347 return SDMMC_ERROR_COM_CRC_FAILED;
1348 }
1349 else if ((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
1350 {
1351 return SDMMC_ERROR_ILLEGAL_CMD;
1352 }
1353 else if ((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
1354 {
1355 return SDMMC_ERROR_CARD_ECC_FAILED;
1356 }
1357 else if ((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
1358 {
1359 return SDMMC_ERROR_CC_ERR;
1360 }
1361 else if ((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
1362 {
1363 return SDMMC_ERROR_STREAM_READ_UNDERRUN;
1364 }
1365 else if ((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
1366 {
1367 return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
1368 }
1369 else if ((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
1370 {
1371 return SDMMC_ERROR_CID_CSD_OVERWRITE;
1372 }
1373 else if ((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
1374 {
1375 return SDMMC_ERROR_WP_ERASE_SKIP;
1376 }
1377 else if ((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
1378 {
1379 return SDMMC_ERROR_CARD_ECC_DISABLED;
1380 }
1381 else if ((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
1382 {
1383 return SDMMC_ERROR_ERASE_RESET;
1384 }
1385 else if ((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
1386 {
1387 return SDMMC_ERROR_AKE_SEQ_ERR;
1388 }
1389 else
1390 {
1391 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1392 }
1393 }
1394
1395
1396
1397
1398
1399
1400 uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx)
1401 {
1402 uint32_t sta_reg;
1403
1404
1405 uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
1406
1407 do
1408 {
1409 if (count-- == 0U)
1410 {
1411 return SDMMC_ERROR_TIMEOUT;
1412 }
1413 sta_reg = SDMMCx->STA;
1414 } while (((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1415 ((sta_reg & SDMMC_FLAG_CMDACT) != 0U));
1416
1417 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1418 {
1419 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1420
1421 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1422 }
1423 else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1424 {
1425 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1426
1427 return SDMMC_ERROR_CMD_CRC_FAIL;
1428 }
1429 else
1430 {
1431
1432
1433 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1434 }
1435
1436 return SDMMC_ERROR_NONE;
1437 }
1438
1439
1440
1441
1442
1443
1444 uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx)
1445 {
1446 uint32_t sta_reg;
1447
1448
1449 uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
1450
1451 do
1452 {
1453 if (count-- == 0U)
1454 {
1455 return SDMMC_ERROR_TIMEOUT;
1456 }
1457 sta_reg = SDMMCx->STA;
1458 } while (((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1459 ((sta_reg & SDMMC_FLAG_CMDACT) != 0U));
1460
1461 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1462 {
1463 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1464
1465 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1466 }
1467 else
1468 {
1469
1470 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1471 }
1472
1473 return SDMMC_ERROR_NONE;
1474 }
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
1485 {
1486 uint32_t response_r1;
1487 uint32_t sta_reg;
1488
1489
1490
1491 uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
1492
1493 do
1494 {
1495 if (count-- == 0U)
1496 {
1497 return SDMMC_ERROR_TIMEOUT;
1498 }
1499 sta_reg = SDMMCx->STA;
1500 } while (((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1501 ((sta_reg & SDMMC_FLAG_CMDACT) != 0U));
1502
1503 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1504 {
1505 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1506
1507 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1508 }
1509 else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1510 {
1511 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1512
1513 return SDMMC_ERROR_CMD_CRC_FAIL;
1514 }
1515 else
1516 {
1517
1518 }
1519
1520
1521 if (SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1522 {
1523 return SDMMC_ERROR_CMD_CRC_FAIL;
1524 }
1525
1526
1527 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1528
1529
1530 response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1531
1532 if ((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD |
1533 SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
1534 {
1535 *pRCA = (uint16_t)(response_r1 >> 16);
1536
1537 return SDMMC_ERROR_NONE;
1538 }
1539 else if ((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
1540 {
1541 return SDMMC_ERROR_ILLEGAL_CMD;
1542 }
1543 else if ((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
1544 {
1545 return SDMMC_ERROR_COM_CRC_FAILED;
1546 }
1547 else
1548 {
1549 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1550 }
1551 }
1552
1553
1554
1555
1556
1557
1558 uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx)
1559 {
1560 uint32_t sta_reg;
1561
1562
1563 uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
1564
1565 do
1566 {
1567 if (count-- == 0U)
1568 {
1569 return SDMMC_ERROR_TIMEOUT;
1570 }
1571 sta_reg = SDMMCx->STA;
1572 } while (((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1573 ((sta_reg & SDMMC_FLAG_CMDACT) != 0U));
1574
1575 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1576 {
1577
1578 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1579
1580 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1581 }
1582
1583 else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1584 {
1585
1586 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1587
1588 return SDMMC_ERROR_CMD_CRC_FAIL;
1589 }
1590 else
1591 {
1592
1593 }
1594
1595 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
1596 {
1597
1598 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
1599 }
1600
1601 return SDMMC_ERROR_NONE;
1602
1603 }
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx)
1620 {
1621
1622
1623 uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
1624
1625 do
1626 {
1627 if (count-- == 0U)
1628 {
1629 return SDMMC_ERROR_TIMEOUT;
1630 }
1631
1632 } while (!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
1633
1634
1635 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1636
1637 return SDMMC_ERROR_NONE;
1638 }
1639
1640
1641
1642
1643
1644 #endif
1645
1646
1647
1648
1649
1650
1651