CARME-M4 BSP  V1.5
stm32f4_sdio_sd.c
Go to the documentation of this file.
1 
229 /* Includes ------------------------------------------------------------------*/
230 #include "stm32f4_sdio_sd.h"
231 
244 #define NULL 0
245 #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
246 #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
247 
251 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
252 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
253 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
254 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
255 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
256 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
257 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
258 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
259 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
260 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
261 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
262 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
263 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
264 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
265 #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
266 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
267 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
268 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
269 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
270 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
271 
275 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
276 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
277 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
278 
279 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
280 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
281 #define SD_STD_CAPACITY ((uint32_t)0x00000000)
282 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
283 
284 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
285 #define SD_ALLZERO ((uint32_t)0x00000000)
286 
287 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
288 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
289 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
290 
291 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
292 #define SD_0TO7BITS ((uint32_t)0x000000FF)
293 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
294 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
295 #define SD_24TO31BITS ((uint32_t)0xFF000000)
296 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
297 
298 #define SD_HALFFIFO ((uint32_t)0x00000008)
299 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
300 
304 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
305 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
306 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
307 
312 #define SDIO_SEND_IF_COND ((uint32_t)0x00000008)
313 
314 static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
315 static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0;
316 static uint8_t SDSTATUS_Tab[16];
317 __IO uint32_t StopCondition = 0;
318 __IO SD_Error TransferError = SD_OK;
319 __IO uint32_t TransferEnd = 0, DMAEndOfTransfer = 0;
320 SD_CardInfo SDCardInfo;
321 
322 SDIO_InitTypeDef SDIO_InitStructure;
323 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
324 SDIO_DataInitTypeDef SDIO_DataInitStructure;
325 
326 static SD_Error CmdError(void);
327 static SD_Error CmdResp1Error(uint8_t cmd);
328 static SD_Error CmdResp7Error(void);
329 static SD_Error CmdResp3Error(void);
330 static SD_Error CmdResp2Error(void);
331 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
332 static SD_Error SDEnWideBus(FunctionalState NewState);
333 static SD_Error IsCardProgramming(uint8_t *pstatus);
334 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
335 uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
336 
342 void SD_DeInit(void)
343 {
345 }
346 
354 {
355  __IO SD_Error errorstatus = SD_OK;
356 
357  /* SDIO Peripheral Low Level Init */
359 
360  SDIO_DeInit();
361 
362  errorstatus = SD_PowerON();
363 
364  if (errorstatus != SD_OK)
365  {
367  return(errorstatus);
368  }
369 
370  errorstatus = SD_InitializeCards();
371 
372  if (errorstatus != SD_OK)
373  {
375  return(errorstatus);
376  }
377 
381  SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
382  SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
383  SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
384  SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
385  SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
386  SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
387  SDIO_Init(&SDIO_InitStructure);
388 
389  /*----------------- Read CSD/CID MSD registers ------------------*/
390  errorstatus = SD_GetCardInfo(&SDCardInfo);
391 
392  if (errorstatus == SD_OK)
393  {
394  /*----------------- Select Card --------------------------------*/
395  errorstatus = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
396  }
397 
398  if (errorstatus == SD_OK)
399  {
400  errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
401  }
402 
403  return(errorstatus);
404 }
405 
415 {
416  SDCardState cardstate = SD_CARD_TRANSFER;
417 
418  cardstate = SD_GetState();
419 
420  if (cardstate == SD_CARD_TRANSFER)
421  {
422  return(SD_TRANSFER_OK);
423  }
424  else if(cardstate == SD_CARD_ERROR)
425  {
426  return (SD_TRANSFER_ERROR);
427  }
428  else
429  {
430  return(SD_TRANSFER_BUSY);
431  }
432 }
433 
440 {
441  uint32_t resp1 = 0;
442 
443  if(SD_Detect()== SD_PRESENT)
444  {
445  if (SD_SendStatus(&resp1) != SD_OK)
446  {
447  return SD_CARD_ERROR;
448  }
449  else
450  {
451  return (SDCardState)((resp1 >> 9) & 0x0F);
452  }
453  }
454  else
455  {
456  return SD_CARD_ERROR;
457  }
458 }
459 
465 uint8_t SD_Detect(void)
466 {
467  __IO uint8_t status = SD_PRESENT;
468 
470 #if SD_DETECT_USE
472  {
473  status = SD_NOT_PRESENT;
474  }
475 #endif
476  return status;
477 }
478 
486 {
487  __IO SD_Error errorstatus = SD_OK;
488  uint32_t response = 0, count = 0, validvoltage = 0;
489  uint32_t SDType = SD_STD_CAPACITY;
490 
496  SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
497  SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
498  SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
499  SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
500  SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
501  SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
502  SDIO_Init(&SDIO_InitStructure);
503 
505  SDIO_SetPowerState(SDIO_PowerState_ON);
506 
508  SDIO_ClockCmd(ENABLE);
509 
512  SDIO_CmdInitStructure.SDIO_Argument = 0x0;
513  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
514  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
515  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
516  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
517  SDIO_SendCommand(&SDIO_CmdInitStructure);
518 
519  errorstatus = CmdError();
520 
521  if (errorstatus != SD_OK)
522  {
524  return(errorstatus);
525  }
526 
533  SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
534  SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
535  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
536  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
537  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
538  SDIO_SendCommand(&SDIO_CmdInitStructure);
539 
540  errorstatus = CmdResp7Error();
541 
542  if (errorstatus == SD_OK)
543  {
544  CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0;
545  SDType = SD_HIGH_CAPACITY;
546  }
547  else
548  {
550  SDIO_CmdInitStructure.SDIO_Argument = 0x00;
551  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
552  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
553  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
554  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
555  SDIO_SendCommand(&SDIO_CmdInitStructure);
556  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
557  }
559  SDIO_CmdInitStructure.SDIO_Argument = 0x00;
560  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
561  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
562  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
563  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
564  SDIO_SendCommand(&SDIO_CmdInitStructure);
565  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
566 
570  if (errorstatus == SD_OK)
571  {
574  while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
575  {
576 
578  SDIO_CmdInitStructure.SDIO_Argument = 0x00;
579  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
580  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
581  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
582  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
583  SDIO_SendCommand(&SDIO_CmdInitStructure);
584 
585  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
586 
587  if (errorstatus != SD_OK)
588  {
589  return(errorstatus);
590  }
591  SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
592  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
593  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
594  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
595  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
596  SDIO_SendCommand(&SDIO_CmdInitStructure);
597 
598  errorstatus = CmdResp3Error();
599  if (errorstatus != SD_OK)
600  {
601  return(errorstatus);
602  }
603 
604  response = SDIO_GetResponse(SDIO_RESP1);
605  validvoltage = (((response >> 31) == 1) ? 1 : 0);
606  count++;
607  }
608  if (count >= SD_MAX_VOLT_TRIAL)
609  {
610  errorstatus = SD_INVALID_VOLTRANGE;
611  return(errorstatus);
612  }
613 
614  if (response &= SD_HIGH_CAPACITY)
615  {
616  CardType = SDIO_HIGH_CAPACITY_SD_CARD;
617  }
618 
619  }
621  return(errorstatus);
622 }
623 
630 {
631  SD_Error errorstatus = SD_OK;
632 
634  SDIO_SetPowerState(SDIO_PowerState_OFF);
635 
636  return(errorstatus);
637 }
638 
646 {
647  SD_Error errorstatus = SD_OK;
648  uint16_t rca = 0x01;
649 
650  if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
651  {
652  errorstatus = SD_REQUEST_NOT_APPLICABLE;
653  return(errorstatus);
654  }
655 
656  if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
657  {
659  SDIO_CmdInitStructure.SDIO_Argument = 0x0;
660  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
661  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
662  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
663  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
664  SDIO_SendCommand(&SDIO_CmdInitStructure);
665 
666  errorstatus = CmdResp2Error();
667 
668  if (SD_OK != errorstatus)
669  {
670  return(errorstatus);
671  }
672 
673  CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
674  CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
675  CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
676  CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
677  }
678  if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType)
679  || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
680  {
683  SDIO_CmdInitStructure.SDIO_Argument = 0x00;
684  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
685  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
686  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
687  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
688  SDIO_SendCommand(&SDIO_CmdInitStructure);
689 
690  errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca);
691 
692  if (SD_OK != errorstatus)
693  {
694  return(errorstatus);
695  }
696  }
697 
698  if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
699  {
700  RCA = rca;
701 
703  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
704  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
705  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
706  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
707  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
708  SDIO_SendCommand(&SDIO_CmdInitStructure);
709 
710  errorstatus = CmdResp2Error();
711 
712  if (SD_OK != errorstatus)
713  {
714  return(errorstatus);
715  }
716 
717  CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
718  CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
719  CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
720  CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
721  }
722 
723  errorstatus = SD_OK;
725  return(errorstatus);
726 }
727 
736 {
737  SD_Error errorstatus = SD_OK;
738  uint8_t tmp = 0;
739 
740  cardinfo->CardType = (uint8_t)CardType;
741  cardinfo->RCA = (uint16_t)RCA;
742 
744  tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24);
745  cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6;
746  cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2;
747  cardinfo->SD_csd.Reserved1 = tmp & 0x03;
748 
750  tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16);
751  cardinfo->SD_csd.TAAC = tmp;
752 
754  tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8);
755  cardinfo->SD_csd.NSAC = tmp;
756 
758  tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF);
759  cardinfo->SD_csd.MaxBusClkFrec = tmp;
760 
762  tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24);
763  cardinfo->SD_csd.CardComdClasses = tmp << 4;
764 
766  tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16);
767  cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4;
768  cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
769 
771  tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8);
772  cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7;
773  cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6;
774  cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5;
775  cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4;
776  cardinfo->SD_csd.Reserved2 = 0;
778  if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
779  {
780  cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
781 
783  tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
784  cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
785 
787  tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
788  cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
789 
790  cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
791  cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
792 
794  tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
795  cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
796  cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
797  cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
799  tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
800  cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
801 
802  cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
803  cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
804  cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
805  cardinfo->CardCapacity *= cardinfo->CardBlockSize;
806  }
807  else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
808  {
810  tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
811  cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
812 
814  tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
815 
816  cardinfo->SD_csd.DeviceSize |= (tmp << 8);
817 
819  tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
820 
821  cardinfo->SD_csd.DeviceSize |= (tmp);
822 
824  tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
825 
826  cardinfo->CardCapacity = (uint64_t)(cardinfo->SD_csd.DeviceSize + 1) * (uint64_t)(512 * 1024);
827  cardinfo->CardBlockSize = 512;
828  }
829 
830 
831  cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
832  cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
833 
835  tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF);
836  cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
837  cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
838 
840  tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24);
841  cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
842  cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
843  cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
844  cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
845 
847  tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16);
848  cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
849  cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
850  cardinfo->SD_csd.Reserved3 = 0;
851  cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
852 
854  tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8);
855  cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
856  cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
857  cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
858  cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
859  cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
860  cardinfo->SD_csd.ECC = (tmp & 0x03);
861 
863  tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF);
864  cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
865  cardinfo->SD_csd.Reserved4 = 1;
866 
867 
869  tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24);
870  cardinfo->SD_cid.ManufacturerID = tmp;
871 
873  tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16);
874  cardinfo->SD_cid.OEM_AppliID = tmp << 8;
875 
877  tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8);
878  cardinfo->SD_cid.OEM_AppliID |= tmp;
879 
881  tmp = (uint8_t)(CID_Tab[0] & 0x000000FF);
882  cardinfo->SD_cid.ProdName1 = tmp << 24;
883 
885  tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24);
886  cardinfo->SD_cid.ProdName1 |= tmp << 16;
887 
889  tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16);
890  cardinfo->SD_cid.ProdName1 |= tmp << 8;
891 
893  tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8);
894  cardinfo->SD_cid.ProdName1 |= tmp;
895 
897  tmp = (uint8_t)(CID_Tab[1] & 0x000000FF);
898  cardinfo->SD_cid.ProdName2 = tmp;
899 
901  tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24);
902  cardinfo->SD_cid.ProdRev = tmp;
903 
905  tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16);
906  cardinfo->SD_cid.ProdSN = tmp << 24;
907 
909  tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8);
910  cardinfo->SD_cid.ProdSN |= tmp << 16;
911 
913  tmp = (uint8_t)(CID_Tab[2] & 0x000000FF);
914  cardinfo->SD_cid.ProdSN |= tmp << 8;
915 
917  tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24);
918  cardinfo->SD_cid.ProdSN |= tmp;
919 
921  tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16);
922  cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
923  cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
924 
926  tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8);
927  cardinfo->SD_cid.ManufactDate |= tmp;
928 
930  tmp = (uint8_t)(CID_Tab[3] & 0x000000FF);
931  cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
932  cardinfo->SD_cid.Reserved2 = 1;
933 
934  return(errorstatus);
935 }
936 
945 {
946  SD_Error errorstatus = SD_OK;
947  uint8_t tmp = 0;
948 
949  errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab);
950 
951  if (errorstatus != SD_OK)
952  {
953  return(errorstatus);
954  }
955 
957  tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6);
958  cardstatus->DAT_BUS_WIDTH = tmp;
959 
961  tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5);
962  cardstatus->SECURED_MODE = tmp;
963 
965  tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF));
966  cardstatus->SD_CARD_TYPE = tmp << 8;
967 
969  tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF));
970  cardstatus->SD_CARD_TYPE |= tmp;
971 
973  tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF);
974  cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24;
975 
977  tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF);
978  cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16;
979 
981  tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF);
982  cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8;
983 
985  tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF);
986  cardstatus->SIZE_OF_PROTECTED_AREA |= tmp;
987 
989  tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF));
990  cardstatus->SPEED_CLASS = tmp;
991 
993  tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF));
994  cardstatus->PERFORMANCE_MOVE = tmp;
995 
997  tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4);
998  cardstatus->AU_SIZE = tmp;
999 
1001  tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF);
1002  cardstatus->ERASE_SIZE = tmp << 8;
1003 
1005  tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF);
1006  cardstatus->ERASE_SIZE |= tmp;
1007 
1009  tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2);
1010  cardstatus->ERASE_TIMEOUT = tmp;
1011 
1013  tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3));
1014  cardstatus->ERASE_OFFSET = tmp;
1015 
1016  return(errorstatus);
1017 }
1018 
1031 {
1032  SD_Error errorstatus = SD_OK;
1033 
1035  if (SDIO_MULTIMEDIA_CARD == CardType)
1036  {
1037  errorstatus = SD_UNSUPPORTED_FEATURE;
1038  return(errorstatus);
1039  }
1040  else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
1041  {
1042  if (SDIO_BusWide_8b == WideMode)
1043  {
1044  errorstatus = SD_UNSUPPORTED_FEATURE;
1045  return(errorstatus);
1046  }
1047  else if (SDIO_BusWide_4b == WideMode)
1048  {
1049  errorstatus = SDEnWideBus(ENABLE);
1050 
1051  if (SD_OK == errorstatus)
1052  {
1054  SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
1055  SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
1056  SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
1057  SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
1058  SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b;
1059  SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
1060  SDIO_Init(&SDIO_InitStructure);
1061  }
1062  }
1063  else
1064  {
1065  errorstatus = SDEnWideBus(DISABLE);
1066 
1067  if (SD_OK == errorstatus)
1068  {
1070  SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
1071  SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
1072  SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
1073  SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
1074  SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
1075  SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
1076  SDIO_Init(&SDIO_InitStructure);
1077  }
1078  }
1079  }
1080 
1081  return(errorstatus);
1082 }
1083 
1091 {
1092  SD_Error errorstatus = SD_OK;
1093 
1095  SDIO_CmdInitStructure.SDIO_Argument = addr;
1096  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
1097  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1098  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1099  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1100  SDIO_SendCommand(&SDIO_CmdInitStructure);
1101 
1102  errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
1103 
1104  return(errorstatus);
1105 }
1106 
1123 SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
1124 {
1125  SD_Error errorstatus = SD_OK;
1126 #if defined (SD_POLLING_MODE)
1127  uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
1128 #endif
1129 
1130  TransferError = SD_OK;
1131  TransferEnd = 0;
1132  StopCondition = 0;
1133 
1134  SDIO->DCTRL = 0x0;
1135 
1136 
1137  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1138  {
1139  BlockSize = 512;
1140  ReadAddr /= 512;
1141  }
1142 
1143  /* Set Block Size for Card */
1144  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1145  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1146  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1147  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1148  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1149  SDIO_SendCommand(&SDIO_CmdInitStructure);
1150 
1151  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1152 
1153  if (SD_OK != errorstatus)
1154  {
1155  return(errorstatus);
1156  }
1157 
1158  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1159  SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
1160  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1161  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1162  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1163  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1164  SDIO_DataConfig(&SDIO_DataInitStructure);
1165 
1167  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
1168  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
1169  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1170  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1171  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1172  SDIO_SendCommand(&SDIO_CmdInitStructure);
1173 
1174  errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);
1175 
1176  if (errorstatus != SD_OK)
1177  {
1178  return(errorstatus);
1179  }
1180 
1181 #if defined (SD_POLLING_MODE)
1182 
1184  while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
1185  {
1186  if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
1187  {
1188  for (count = 0; count < 8; count++)
1189  {
1190  *(tempbuff + count) = SDIO_ReadData();
1191  }
1192  tempbuff += 8;
1193  }
1194  }
1195 
1196  if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
1197  {
1198  SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
1199  errorstatus = SD_DATA_TIMEOUT;
1200  return(errorstatus);
1201  }
1202  else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
1203  {
1204  SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
1205  errorstatus = SD_DATA_CRC_FAIL;
1206  return(errorstatus);
1207  }
1208  else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
1209  {
1210  SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
1211  errorstatus = SD_RX_OVERRUN;
1212  return(errorstatus);
1213  }
1214  else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
1215  {
1216  SDIO_ClearFlag(SDIO_FLAG_STBITERR);
1217  errorstatus = SD_START_BIT_ERR;
1218  return(errorstatus);
1219  }
1220  count = SD_DATATIMEOUT;
1221  while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
1222  {
1223  *tempbuff = SDIO_ReadData();
1224  tempbuff++;
1225  count--;
1226  }
1227 
1229  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1230 
1231 #elif defined (SD_DMA_MODE)
1232  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1233  SDIO_DMACmd(ENABLE);
1234  SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
1235 #endif
1236 
1237  return(errorstatus);
1238 }
1239 
1257 SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
1258 {
1259  SD_Error errorstatus = SD_OK;
1260  TransferError = SD_OK;
1261  TransferEnd = 0;
1262  StopCondition = 1;
1263 
1264  SDIO->DCTRL = 0x0;
1265 
1266  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1267  {
1268  BlockSize = 512;
1269  ReadAddr /= 512;
1270  }
1271 
1273  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1274  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1275  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1276  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1277  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1278  SDIO_SendCommand(&SDIO_CmdInitStructure);
1279 
1280  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1281 
1282  if (SD_OK != errorstatus)
1283  {
1284  return(errorstatus);
1285  }
1286 
1287  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1288  SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1289  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1290  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1291  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1292  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1293  SDIO_DataConfig(&SDIO_DataInitStructure);
1294 
1296  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
1297  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
1298  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1299  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1300  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1301  SDIO_SendCommand(&SDIO_CmdInitStructure);
1302 
1303  errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
1304 
1305  if (errorstatus != SD_OK)
1306  {
1307  return(errorstatus);
1308  }
1309 
1310  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1311  SDIO_DMACmd(ENABLE);
1312  SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
1313 
1314  return(errorstatus);
1315 }
1316 
1317 // FIXED Version where ReadAddr is in BLOCKS NOT BYTES, permits SDHC media >4GB
1318 
1319 SD_Error SD_ReadMultiBlocksFIXED(uint8_t *readbuff, uint32_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
1320 {
1321  SD_Error errorstatus = SD_OK;
1322  TransferError = SD_OK;
1323  TransferEnd = 0;
1324  StopCondition = 1;
1325 
1326  SDIO->DCTRL = 0x0;
1327 
1328  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1329  BlockSize = 512;
1330  else
1331  ReadAddr *= BlockSize; // Convert to Bytes for NON SDHC
1332 
1334  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1335  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1336  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1337  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1338  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1339  SDIO_SendCommand(&SDIO_CmdInitStructure);
1340 
1341  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1342 
1343  if (SD_OK != errorstatus)
1344  {
1345  return(errorstatus);
1346  }
1347 
1348  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1349  SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1350  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1351  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
1352  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1353  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1354  SDIO_DataConfig(&SDIO_DataInitStructure);
1355 
1357  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
1358  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
1359  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1360  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1361  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1362  SDIO_SendCommand(&SDIO_CmdInitStructure);
1363 
1364  errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
1365 
1366  if (errorstatus != SD_OK)
1367  {
1368  return(errorstatus);
1369  }
1370 
1371  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1372  SDIO_DMACmd(ENABLE);
1373  SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
1374 
1375  return(errorstatus);
1376 }
1377 
1387 {
1388  SD_Error errorstatus = SD_OK;
1389  uint32_t timeout;
1390 
1391  timeout = SD_DATATIMEOUT;
1392 
1393  while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
1394  {
1395  timeout--;
1396  }
1397 
1398  DMAEndOfTransfer = 0x00;
1399 
1400  timeout = SD_DATATIMEOUT;
1401 
1402  while(((SDIO->STA & SDIO_FLAG_RXACT)) && (timeout > 0))
1403  {
1404  timeout--;
1405  }
1406 
1407  if (StopCondition == 1)
1408  {
1409  errorstatus = SD_StopTransfer();
1410  }
1411 
1412  if ((timeout == 0) && (errorstatus == SD_OK))
1413  {
1414  errorstatus = SD_DATA_TIMEOUT;
1415  }
1416 
1418  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1419 
1420  if (TransferError != SD_OK)
1421  {
1422  return(TransferError);
1423  }
1424  else
1425  {
1426  return(errorstatus);
1427  }
1428 }
1429 
1446 SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
1447 {
1448  SD_Error errorstatus = SD_OK;
1449 
1450 #if defined (SD_POLLING_MODE)
1451  uint32_t bytestransferred = 0, count = 0, restwords = 0;
1452  uint32_t *tempbuff = (uint32_t *)writebuff;
1453 #endif
1454 
1455  TransferError = SD_OK;
1456  TransferEnd = 0;
1457  StopCondition = 0;
1458 
1459  SDIO->DCTRL = 0x0;
1460 
1461 
1462  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1463  {
1464  BlockSize = 512;
1465  WriteAddr /= 512;
1466  }
1467 
1468  /* Set Block Size for Card */
1469  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1470  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1471  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1472  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1473  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1474  SDIO_SendCommand(&SDIO_CmdInitStructure);
1475 
1476  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1477 
1478  if (SD_OK != errorstatus)
1479  {
1480  return(errorstatus);
1481  }
1482 
1484  SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
1485  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
1486  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1487  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1488  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1489  SDIO_SendCommand(&SDIO_CmdInitStructure);
1490 
1491  errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);
1492 
1493  if (errorstatus != SD_OK)
1494  {
1495  return(errorstatus);
1496  }
1497 
1498  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1499  SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
1500  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1501  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
1502  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1503  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1504  SDIO_DataConfig(&SDIO_DataInitStructure);
1505 
1507 #if defined (SD_POLLING_MODE)
1508  while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
1509  {
1510  if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
1511  {
1512  if ((512 - bytestransferred) < 32)
1513  {
1514  restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : (( 512 - bytestransferred) / 4 + 1);
1515  for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
1516  {
1517  SDIO_WriteData(*tempbuff);
1518  }
1519  }
1520  else
1521  {
1522  for (count = 0; count < 8; count++)
1523  {
1524  SDIO_WriteData(*(tempbuff + count));
1525  }
1526  tempbuff += 8;
1527  bytestransferred += 32;
1528  }
1529  }
1530  }
1531  if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
1532  {
1533  SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
1534  errorstatus = SD_DATA_TIMEOUT;
1535  return(errorstatus);
1536  }
1537  else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
1538  {
1539  SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
1540  errorstatus = SD_DATA_CRC_FAIL;
1541  return(errorstatus);
1542  }
1543  else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
1544  {
1545  SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
1546  errorstatus = SD_TX_UNDERRUN;
1547  return(errorstatus);
1548  }
1549  else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
1550  {
1551  SDIO_ClearFlag(SDIO_FLAG_STBITERR);
1552  errorstatus = SD_START_BIT_ERR;
1553  return(errorstatus);
1554  }
1555 #elif defined (SD_DMA_MODE)
1556  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1557  SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
1558  SDIO_DMACmd(ENABLE);
1559 #endif
1560 
1561  return(errorstatus);
1562 }
1563 
1581 SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
1582 {
1583  SD_Error errorstatus = SD_OK;
1584 
1585  TransferError = SD_OK;
1586  TransferEnd = 0;
1587  StopCondition = 1;
1588 
1589  SDIO->DCTRL = 0x0;
1590 
1591  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1592  {
1593  BlockSize = 512;
1594  WriteAddr /= 512;
1595  }
1596 
1597  /* Set Block Size for Card */
1598  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1599  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1600  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1601  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1602  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1603  SDIO_SendCommand(&SDIO_CmdInitStructure);
1604 
1605  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1606 
1607  if (SD_OK != errorstatus)
1608  {
1609  return(errorstatus);
1610  }
1611 
1613  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
1614  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
1615  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1616  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1617  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1618  SDIO_SendCommand(&SDIO_CmdInitStructure);
1619 
1620 
1621  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
1622 
1623  if (errorstatus != SD_OK)
1624  {
1625  return(errorstatus);
1626  }
1628  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
1629  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
1630  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1631  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1632  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1633  SDIO_SendCommand(&SDIO_CmdInitStructure);
1634 
1635  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
1636 
1637  if (errorstatus != SD_OK)
1638  {
1639  return(errorstatus);
1640  }
1641 
1642 
1644  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
1645  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
1646  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1647  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1648  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1649  SDIO_SendCommand(&SDIO_CmdInitStructure);
1650 
1651  errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
1652 
1653  if (SD_OK != errorstatus)
1654  {
1655  return(errorstatus);
1656  }
1657 
1658  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1659  SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1660  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1661  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
1662  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1663  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1664  SDIO_DataConfig(&SDIO_DataInitStructure);
1665 
1666  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1667  SDIO_DMACmd(ENABLE);
1668  SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
1669 
1670  return(errorstatus);
1671 }
1672 
1673 // FIXED Version where WriteAddr is in BLOCKS NOT BYTES, permits SDHC media >4GB
1674 
1675 SD_Error SD_WriteMultiBlocksFIXED(uint8_t *writebuff, uint32_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
1676 {
1677  SD_Error errorstatus = SD_OK;
1678 
1679  TransferError = SD_OK;
1680  TransferEnd = 0;
1681  StopCondition = 1;
1682 
1683  SDIO->DCTRL = 0x0;
1684 
1685  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1686  BlockSize = 512;
1687  else
1688  WriteAddr *= BlockSize; // Convert to Bytes for NON SDHC
1689 
1690  /* Set Block Size for Card */
1691  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
1692  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
1693  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1694  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1695  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1696  SDIO_SendCommand(&SDIO_CmdInitStructure);
1697 
1698  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
1699 
1700  if (SD_OK != errorstatus)
1701  {
1702  return(errorstatus);
1703  }
1704 
1706  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
1707  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
1708  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1709  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1710  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1711  SDIO_SendCommand(&SDIO_CmdInitStructure);
1712 
1713 
1714  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
1715 
1716  if (errorstatus != SD_OK)
1717  {
1718  return(errorstatus);
1719  }
1721  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
1722  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
1723  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1724  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1725  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1726  SDIO_SendCommand(&SDIO_CmdInitStructure);
1727 
1728  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
1729 
1730  if (errorstatus != SD_OK)
1731  {
1732  return(errorstatus);
1733  }
1734 
1735 
1737  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
1738  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
1739  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1740  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1741  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1742  SDIO_SendCommand(&SDIO_CmdInitStructure);
1743 
1744  errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
1745 
1746  if (SD_OK != errorstatus)
1747  {
1748  return(errorstatus);
1749  }
1750 
1751  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
1752  SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
1753  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;
1754  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
1755  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
1756  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
1757  SDIO_DataConfig(&SDIO_DataInitStructure);
1758 
1759  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
1760  SDIO_DMACmd(ENABLE);
1761  SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
1762 
1763  return(errorstatus);
1764 }
1765 
1775 {
1776  SD_Error errorstatus = SD_OK;
1777  uint32_t timeout;
1778 
1779  timeout = SD_DATATIMEOUT;
1780 
1781  while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
1782  {
1783  timeout--;
1784  }
1785 
1786  DMAEndOfTransfer = 0x00;
1787 
1788  timeout = SD_DATATIMEOUT;
1789 
1790  while(((SDIO->STA & SDIO_FLAG_TXACT)) && (timeout > 0))
1791  {
1792  timeout--;
1793  }
1794 
1795  if (StopCondition == 1)
1796  {
1797  errorstatus = SD_StopTransfer();
1798  }
1799 
1800  if ((timeout == 0) && (errorstatus == SD_OK))
1801  {
1802  errorstatus = SD_DATA_TIMEOUT;
1803  }
1804 
1806  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
1807 
1808  if (TransferError != SD_OK)
1809  {
1810  return(TransferError);
1811  }
1812  else
1813  {
1814  return(errorstatus);
1815  }
1816 }
1817 
1827 {
1828  if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
1829  {
1830  return(SD_TRANSFER_BUSY);
1831  }
1832  else
1833  {
1834  return(SD_TRANSFER_OK);
1835  }
1836 }
1837 
1844 {
1845  SD_Error errorstatus = SD_OK;
1846 
1848  SDIO_CmdInitStructure.SDIO_Argument = 0x0;
1849  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
1850  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1851  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1852  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1853  SDIO_SendCommand(&SDIO_CmdInitStructure);
1854 
1855  errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
1856 
1857  return(errorstatus);
1858 }
1859 
1867 SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
1868 {
1869  SD_Error errorstatus = SD_OK;
1870  uint32_t delay = 0;
1871  __IO uint32_t maxdelay = 0;
1872  uint8_t cardstate = 0;
1873 
1875  if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0)
1876  {
1877  errorstatus = SD_REQUEST_NOT_APPLICABLE;
1878  return(errorstatus);
1879  }
1880 
1881  maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2);
1882 
1883  if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
1884  {
1885  errorstatus = SD_LOCK_UNLOCK_FAILED;
1886  return(errorstatus);
1887  }
1888 
1889  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
1890  {
1891  startaddr /= 512;
1892  endaddr /= 512;
1893  }
1894 
1896  if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
1897  {
1899  SDIO_CmdInitStructure.SDIO_Argument = startaddr;
1900  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
1901  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1902  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1903  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1904  SDIO_SendCommand(&SDIO_CmdInitStructure);
1905 
1907  if (errorstatus != SD_OK)
1908  {
1909  return(errorstatus);
1910  }
1911 
1913  SDIO_CmdInitStructure.SDIO_Argument = endaddr;
1914  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
1915  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1916  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1917  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1918  SDIO_SendCommand(&SDIO_CmdInitStructure);
1919 
1920  errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END);
1921  if (errorstatus != SD_OK)
1922  {
1923  return(errorstatus);
1924  }
1925  }
1926 
1928  SDIO_CmdInitStructure.SDIO_Argument = 0;
1929  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
1930  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1931  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1932  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1933  SDIO_SendCommand(&SDIO_CmdInitStructure);
1934 
1935  errorstatus = CmdResp1Error(SD_CMD_ERASE);
1936 
1937  if (errorstatus != SD_OK)
1938  {
1939  return(errorstatus);
1940  }
1941 
1942  for (delay = 0; delay < maxdelay; delay++)
1943  {}
1944 
1946  errorstatus = IsCardProgramming(&cardstate);
1947  delay = SD_DATATIMEOUT;
1948  while ((delay > 0) && (errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
1949  {
1950  errorstatus = IsCardProgramming(&cardstate);
1951  delay--;
1952  }
1953 
1954  return(errorstatus);
1955 }
1956 
1964 SD_Error SD_SendStatus(uint32_t *pcardstatus)
1965 {
1966  SD_Error errorstatus = SD_OK;
1967 
1968  if (pcardstatus == NULL)
1969  {
1970  errorstatus = SD_INVALID_PARAMETER;
1971  return(errorstatus);
1972  }
1973 
1974  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
1975  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
1976  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
1977  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
1978  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
1979  SDIO_SendCommand(&SDIO_CmdInitStructure);
1980 
1981  errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
1982 
1983  if (errorstatus != SD_OK)
1984  {
1985  return(errorstatus);
1986  }
1987 
1988  *pcardstatus = SDIO_GetResponse(SDIO_RESP1);
1989 
1990  return(errorstatus);
1991 }
1992 
2000 SD_Error SD_SendSDStatus(uint32_t *psdstatus)
2001 {
2002  SD_Error errorstatus = SD_OK;
2003  uint32_t count = 0;
2004 
2005  if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
2006  {
2007  errorstatus = SD_LOCK_UNLOCK_FAILED;
2008  return(errorstatus);
2009  }
2010 
2012  SDIO_CmdInitStructure.SDIO_Argument = 64;
2013  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
2014  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2015  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2016  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2017  SDIO_SendCommand(&SDIO_CmdInitStructure);
2018 
2019  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
2020 
2021  if (errorstatus != SD_OK)
2022  {
2023  return(errorstatus);
2024  }
2025 
2027  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2028  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2029  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2030  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2031  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2032  SDIO_SendCommand(&SDIO_CmdInitStructure);
2033  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2034 
2035  if (errorstatus != SD_OK)
2036  {
2037  return(errorstatus);
2038  }
2039 
2040  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
2041  SDIO_DataInitStructure.SDIO_DataLength = 64;
2042  SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
2043  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
2044  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
2045  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
2046  SDIO_DataConfig(&SDIO_DataInitStructure);
2047 
2049  SDIO_CmdInitStructure.SDIO_Argument = 0;
2050  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
2051  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2052  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2053  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2054  SDIO_SendCommand(&SDIO_CmdInitStructure);
2055  errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS);
2056 
2057  if (errorstatus != SD_OK)
2058  {
2059  return(errorstatus);
2060  }
2061 
2062  while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
2063  {
2064  if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
2065  {
2066  for (count = 0; count < 8; count++)
2067  {
2068  *(psdstatus + count) = SDIO_ReadData();
2069  }
2070  psdstatus += 8;
2071  }
2072  }
2073 
2074  if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
2075  {
2076  SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
2077  errorstatus = SD_DATA_TIMEOUT;
2078  return(errorstatus);
2079  }
2080  else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
2081  {
2082  SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
2083  errorstatus = SD_DATA_CRC_FAIL;
2084  return(errorstatus);
2085  }
2086  else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
2087  {
2088  SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
2089  errorstatus = SD_RX_OVERRUN;
2090  return(errorstatus);
2091  }
2092  else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
2093  {
2094  SDIO_ClearFlag(SDIO_FLAG_STBITERR);
2095  errorstatus = SD_START_BIT_ERR;
2096  return(errorstatus);
2097  }
2098 
2099  count = SD_DATATIMEOUT;
2100  while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
2101  {
2102  *psdstatus = SDIO_ReadData();
2103  psdstatus++;
2104  count--;
2105  }
2107  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2108 
2109  return(errorstatus);
2110 }
2111 
2118 {
2119  if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
2120  {
2121  TransferError = SD_OK;
2122  SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
2123  TransferEnd = 1;
2124  }
2125  else if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET)
2126  {
2127  SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL);
2128  TransferError = SD_DATA_CRC_FAIL;
2129  }
2130  else if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET)
2131  {
2132  SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT);
2133  TransferError = SD_DATA_TIMEOUT;
2134  }
2135  else if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET)
2136  {
2137  SDIO_ClearITPendingBit(SDIO_IT_RXOVERR);
2138  TransferError = SD_RX_OVERRUN;
2139  }
2140  else if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET)
2141  {
2142  SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR);
2143  TransferError = SD_TX_UNDERRUN;
2144  }
2145  else if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET)
2146  {
2147  SDIO_ClearITPendingBit(SDIO_IT_STBITERR);
2148  TransferError = SD_START_BIT_ERR;
2149  }
2150 
2151  SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
2152  SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
2153  SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE);
2154  return(TransferError);
2155 }
2156 
2163 {
2164  if(DMA2->LISR & SD_SDIO_DMA_FLAG_TCIF)
2165  {
2166  DMAEndOfTransfer = 0x01;
2167  DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_TCIF|SD_SDIO_DMA_FLAG_FEIF);
2168  }
2169 }
2170 
2176 static SD_Error CmdError(void)
2177 {
2178  SD_Error errorstatus = SD_OK;
2179  uint32_t timeout;
2180 
2181  timeout = SDIO_CMD0TIMEOUT;
2183  while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET))
2184  {
2185  timeout--;
2186  }
2187 
2188  if (timeout == 0)
2189  {
2190  errorstatus = SD_CMD_RSP_TIMEOUT;
2191  return(errorstatus);
2192  }
2193 
2195  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2196 
2197  return(errorstatus);
2198 }
2199 
2206 {
2207  SD_Error errorstatus = SD_OK;
2208  uint32_t status;
2209  uint32_t timeout = SDIO_CMD0TIMEOUT;
2210 
2211  status = SDIO->STA;
2212 
2213  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
2214  {
2215  timeout--;
2216  status = SDIO->STA;
2217  }
2218 
2219  if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
2220  {
2222  errorstatus = SD_CMD_RSP_TIMEOUT;
2223  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2224  return(errorstatus);
2225  }
2226 
2227  if (status & SDIO_FLAG_CMDREND)
2228  {
2230  errorstatus = SD_OK;
2231  SDIO_ClearFlag(SDIO_FLAG_CMDREND);
2232  return(errorstatus);
2233  }
2234  return(errorstatus);
2235 }
2236 
2243 static SD_Error CmdResp1Error(uint8_t cmd)
2244 {
2245  SD_Error errorstatus = SD_OK;
2246  uint32_t status;
2247  uint32_t response_r1;
2248 
2249  status = SDIO->STA;
2250 
2251  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
2252  {
2253  status = SDIO->STA;
2254  }
2255 
2256  if (status & SDIO_FLAG_CTIMEOUT)
2257  {
2258  errorstatus = SD_CMD_RSP_TIMEOUT;
2259  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2260  return(errorstatus);
2261  }
2262  else if (status & SDIO_FLAG_CCRCFAIL)
2263  {
2264  errorstatus = SD_CMD_CRC_FAIL;
2265  SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2266  return(errorstatus);
2267  }
2268 
2270  if (SDIO_GetCommandResponse() != cmd)
2271  {
2272  errorstatus = SD_ILLEGAL_CMD;
2273  return(errorstatus);
2274  }
2275 
2277  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2278 
2280  response_r1 = SDIO_GetResponse(SDIO_RESP1);
2281 
2282  if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
2283  {
2284  return(errorstatus);
2285  }
2286 
2287  if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
2288  {
2289  return(SD_ADDR_OUT_OF_RANGE);
2290  }
2291 
2292  if (response_r1 & SD_OCR_ADDR_MISALIGNED)
2293  {
2294  return(SD_ADDR_MISALIGNED);
2295  }
2296 
2297  if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
2298  {
2299  return(SD_BLOCK_LEN_ERR);
2300  }
2301 
2302  if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
2303  {
2304  return(SD_ERASE_SEQ_ERR);
2305  }
2306 
2307  if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
2308  {
2309  return(SD_BAD_ERASE_PARAM);
2310  }
2311 
2312  if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
2313  {
2314  return(SD_WRITE_PROT_VIOLATION);
2315  }
2316 
2317  if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
2318  {
2319  return(SD_LOCK_UNLOCK_FAILED);
2320  }
2321 
2322  if (response_r1 & SD_OCR_COM_CRC_FAILED)
2323  {
2324  return(SD_COM_CRC_FAILED);
2325  }
2326 
2327  if (response_r1 & SD_OCR_ILLEGAL_CMD)
2328  {
2329  return(SD_ILLEGAL_CMD);
2330  }
2331 
2332  if (response_r1 & SD_OCR_CARD_ECC_FAILED)
2333  {
2334  return(SD_CARD_ECC_FAILED);
2335  }
2336 
2337  if (response_r1 & SD_OCR_CC_ERROR)
2338  {
2339  return(SD_CC_ERROR);
2340  }
2341 
2342  if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
2343  {
2344  return(SD_GENERAL_UNKNOWN_ERROR);
2345  }
2346 
2347  if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
2348  {
2349  return(SD_STREAM_READ_UNDERRUN);
2350  }
2351 
2352  if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
2353  {
2354  return(SD_STREAM_WRITE_OVERRUN);
2355  }
2356 
2357  if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
2358  {
2359  return(SD_CID_CSD_OVERWRITE);
2360  }
2361 
2362  if (response_r1 & SD_OCR_WP_ERASE_SKIP)
2363  {
2364  return(SD_WP_ERASE_SKIP);
2365  }
2366 
2367  if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
2368  {
2369  return(SD_CARD_ECC_DISABLED);
2370  }
2371 
2372  if (response_r1 & SD_OCR_ERASE_RESET)
2373  {
2374  return(SD_ERASE_RESET);
2375  }
2376 
2377  if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
2378  {
2379  return(SD_AKE_SEQ_ERROR);
2380  }
2381  return(errorstatus);
2382 }
2383 
2390 {
2391  SD_Error errorstatus = SD_OK;
2392  uint32_t status;
2393 
2394  status = SDIO->STA;
2395 
2396  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
2397  {
2398  status = SDIO->STA;
2399  }
2400 
2401  if (status & SDIO_FLAG_CTIMEOUT)
2402  {
2403  errorstatus = SD_CMD_RSP_TIMEOUT;
2404  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2405  return(errorstatus);
2406  }
2408  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2409  return(errorstatus);
2410 }
2411 
2418 {
2419  SD_Error errorstatus = SD_OK;
2420  uint32_t status;
2421 
2422  status = SDIO->STA;
2423 
2424  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
2425  {
2426  status = SDIO->STA;
2427  }
2428 
2429  if (status & SDIO_FLAG_CTIMEOUT)
2430  {
2431  errorstatus = SD_CMD_RSP_TIMEOUT;
2432  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2433  return(errorstatus);
2434  }
2435  else if (status & SDIO_FLAG_CCRCFAIL)
2436  {
2437  errorstatus = SD_CMD_CRC_FAIL;
2438  SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2439  return(errorstatus);
2440  }
2441 
2443  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2444 
2445  return(errorstatus);
2446 }
2447 
2456 static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
2457 {
2458  SD_Error errorstatus = SD_OK;
2459  uint32_t status;
2460  uint32_t response_r1;
2461 
2462  status = SDIO->STA;
2463 
2464  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
2465  {
2466  status = SDIO->STA;
2467  }
2468 
2469  if (status & SDIO_FLAG_CTIMEOUT)
2470  {
2471  errorstatus = SD_CMD_RSP_TIMEOUT;
2472  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2473  return(errorstatus);
2474  }
2475  else if (status & SDIO_FLAG_CCRCFAIL)
2476  {
2477  errorstatus = SD_CMD_CRC_FAIL;
2478  SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2479  return(errorstatus);
2480  }
2481 
2483  if (SDIO_GetCommandResponse() != cmd)
2484  {
2485  errorstatus = SD_ILLEGAL_CMD;
2486  return(errorstatus);
2487  }
2488 
2490  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2491 
2493  response_r1 = SDIO_GetResponse(SDIO_RESP1);
2494 
2495  if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
2496  {
2497  *prca = (uint16_t) (response_r1 >> 16);
2498  return(errorstatus);
2499  }
2500 
2501  if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
2502  {
2503  return(SD_GENERAL_UNKNOWN_ERROR);
2504  }
2505 
2506  if (response_r1 & SD_R6_ILLEGAL_CMD)
2507  {
2508  return(SD_ILLEGAL_CMD);
2509  }
2510 
2511  if (response_r1 & SD_R6_COM_CRC_FAILED)
2512  {
2513  return(SD_COM_CRC_FAILED);
2514  }
2515 
2516  return(errorstatus);
2517 }
2518 
2526 static SD_Error SDEnWideBus(FunctionalState NewState)
2527 {
2528  SD_Error errorstatus = SD_OK;
2529 
2530  uint32_t scr[2] = {0, 0};
2531 
2532  if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
2533  {
2534  errorstatus = SD_LOCK_UNLOCK_FAILED;
2535  return(errorstatus);
2536  }
2537 
2539  errorstatus = FindSCR(RCA, scr);
2540 
2541  if (errorstatus != SD_OK)
2542  {
2543  return(errorstatus);
2544  }
2545 
2547  if (NewState == ENABLE)
2548  {
2550  if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
2551  {
2553  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2554  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2555  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2556  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2557  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2558  SDIO_SendCommand(&SDIO_CmdInitStructure);
2559 
2560  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2561 
2562  if (errorstatus != SD_OK)
2563  {
2564  return(errorstatus);
2565  }
2566 
2568  SDIO_CmdInitStructure.SDIO_Argument = 0x2;
2569  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2570  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2571  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2572  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2573  SDIO_SendCommand(&SDIO_CmdInitStructure);
2574 
2576 
2577  if (errorstatus != SD_OK)
2578  {
2579  return(errorstatus);
2580  }
2581  return(errorstatus);
2582  }
2583  else
2584  {
2585  errorstatus = SD_REQUEST_NOT_APPLICABLE;
2586  return(errorstatus);
2587  }
2588  }
2589  else
2590  {
2592  if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
2593  {
2595  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2596  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2597  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2598  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2599  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2600  SDIO_SendCommand(&SDIO_CmdInitStructure);
2601 
2602 
2603  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2604 
2605  if (errorstatus != SD_OK)
2606  {
2607  return(errorstatus);
2608  }
2609 
2611  SDIO_CmdInitStructure.SDIO_Argument = 0x00;
2612  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
2613  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2614  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2615  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2616  SDIO_SendCommand(&SDIO_CmdInitStructure);
2617 
2619 
2620  if (errorstatus != SD_OK)
2621  {
2622  return(errorstatus);
2623  }
2624 
2625  return(errorstatus);
2626  }
2627  else
2628  {
2629  errorstatus = SD_REQUEST_NOT_APPLICABLE;
2630  return(errorstatus);
2631  }
2632  }
2633 }
2634 
2641 static SD_Error IsCardProgramming(uint8_t *pstatus)
2642 {
2643  SD_Error errorstatus = SD_OK;
2644  __IO uint32_t respR1 = 0, status = 0;
2645 
2646  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2647  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
2648  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2649  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2650  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2651  SDIO_SendCommand(&SDIO_CmdInitStructure);
2652 
2653  status = SDIO->STA;
2654  while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
2655  {
2656  status = SDIO->STA;
2657  }
2658 
2659  if (status & SDIO_FLAG_CTIMEOUT)
2660  {
2661  errorstatus = SD_CMD_RSP_TIMEOUT;
2662  SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
2663  return(errorstatus);
2664  }
2665  else if (status & SDIO_FLAG_CCRCFAIL)
2666  {
2667  errorstatus = SD_CMD_CRC_FAIL;
2668  SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
2669  return(errorstatus);
2670  }
2671 
2672  status = (uint32_t)SDIO_GetCommandResponse();
2673 
2675  if (status != SD_CMD_SEND_STATUS)
2676  {
2677  errorstatus = SD_ILLEGAL_CMD;
2678  return(errorstatus);
2679  }
2680 
2682  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2683 
2684 
2686  respR1 = SDIO_GetResponse(SDIO_RESP1);
2687 
2689  *pstatus = (uint8_t) ((respR1 >> 9) & 0x0000000F);
2690 
2691  if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
2692  {
2693  return(errorstatus);
2694  }
2695 
2696  if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
2697  {
2698  return(SD_ADDR_OUT_OF_RANGE);
2699  }
2700 
2701  if (respR1 & SD_OCR_ADDR_MISALIGNED)
2702  {
2703  return(SD_ADDR_MISALIGNED);
2704  }
2705 
2706  if (respR1 & SD_OCR_BLOCK_LEN_ERR)
2707  {
2708  return(SD_BLOCK_LEN_ERR);
2709  }
2710 
2711  if (respR1 & SD_OCR_ERASE_SEQ_ERR)
2712  {
2713  return(SD_ERASE_SEQ_ERR);
2714  }
2715 
2716  if (respR1 & SD_OCR_BAD_ERASE_PARAM)
2717  {
2718  return(SD_BAD_ERASE_PARAM);
2719  }
2720 
2721  if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
2722  {
2723  return(SD_WRITE_PROT_VIOLATION);
2724  }
2725 
2726  if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
2727  {
2728  return(SD_LOCK_UNLOCK_FAILED);
2729  }
2730 
2731  if (respR1 & SD_OCR_COM_CRC_FAILED)
2732  {
2733  return(SD_COM_CRC_FAILED);
2734  }
2735 
2736  if (respR1 & SD_OCR_ILLEGAL_CMD)
2737  {
2738  return(SD_ILLEGAL_CMD);
2739  }
2740 
2741  if (respR1 & SD_OCR_CARD_ECC_FAILED)
2742  {
2743  return(SD_CARD_ECC_FAILED);
2744  }
2745 
2746  if (respR1 & SD_OCR_CC_ERROR)
2747  {
2748  return(SD_CC_ERROR);
2749  }
2750 
2751  if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
2752  {
2753  return(SD_GENERAL_UNKNOWN_ERROR);
2754  }
2755 
2756  if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
2757  {
2758  return(SD_STREAM_READ_UNDERRUN);
2759  }
2760 
2761  if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
2762  {
2763  return(SD_STREAM_WRITE_OVERRUN);
2764  }
2765 
2766  if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
2767  {
2768  return(SD_CID_CSD_OVERWRITE);
2769  }
2770 
2771  if (respR1 & SD_OCR_WP_ERASE_SKIP)
2772  {
2773  return(SD_WP_ERASE_SKIP);
2774  }
2775 
2776  if (respR1 & SD_OCR_CARD_ECC_DISABLED)
2777  {
2778  return(SD_CARD_ECC_DISABLED);
2779  }
2780 
2781  if (respR1 & SD_OCR_ERASE_RESET)
2782  {
2783  return(SD_ERASE_RESET);
2784  }
2785 
2786  if (respR1 & SD_OCR_AKE_SEQ_ERROR)
2787  {
2788  return(SD_AKE_SEQ_ERROR);
2789  }
2790 
2791  return(errorstatus);
2792 }
2793 
2801 static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
2802 {
2803  uint32_t index = 0;
2804  SD_Error errorstatus = SD_OK;
2805  uint32_t tempscr[2] = {0, 0};
2806 
2809  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
2810  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
2811  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2812  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2813  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2814  SDIO_SendCommand(&SDIO_CmdInitStructure);
2815 
2816  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
2817 
2818  if (errorstatus != SD_OK)
2819  {
2820  return(errorstatus);
2821  }
2822 
2824  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
2825  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
2826  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2827  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2828  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2829  SDIO_SendCommand(&SDIO_CmdInitStructure);
2830 
2831  errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
2832 
2833  if (errorstatus != SD_OK)
2834  {
2835  return(errorstatus);
2836  }
2837  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
2838  SDIO_DataInitStructure.SDIO_DataLength = 8;
2839  SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
2840  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
2841  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
2842  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
2843  SDIO_DataConfig(&SDIO_DataInitStructure);
2844 
2845 
2847  SDIO_CmdInitStructure.SDIO_Argument = 0x0;
2848  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
2849  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
2850  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
2851  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
2852  SDIO_SendCommand(&SDIO_CmdInitStructure);
2853 
2854  errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
2855 
2856  if (errorstatus != SD_OK)
2857  {
2858  return(errorstatus);
2859  }
2860 
2861  while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
2862  {
2863  if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
2864  {
2865  *(tempscr + index) = SDIO_ReadData();
2866  index++;
2867  }
2868  }
2869 
2870  if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
2871  {
2872  SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
2873  errorstatus = SD_DATA_TIMEOUT;
2874  return(errorstatus);
2875  }
2876  else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
2877  {
2878  SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
2879  errorstatus = SD_DATA_CRC_FAIL;
2880  return(errorstatus);
2881  }
2882  else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
2883  {
2884  SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
2885  errorstatus = SD_RX_OVERRUN;
2886  return(errorstatus);
2887  }
2888  else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
2889  {
2890  SDIO_ClearFlag(SDIO_FLAG_STBITERR);
2891  errorstatus = SD_START_BIT_ERR;
2892  return(errorstatus);
2893  }
2894 
2896  SDIO_ClearFlag(SDIO_STATIC_FLAGS);
2897 
2898  *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
2899 
2900  *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
2901 
2902  return(errorstatus);
2903 }
2904 
2911 uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
2912 {
2913  uint8_t count = 0;
2914 
2915  while (NumberOfBytes != 1)
2916  {
2917  NumberOfBytes >>= 1;
2918  count++;
2919  }
2920  return(count);
2921 }
2922 
2931 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define SD_CMD_SET_BLOCK_COUNT
#define SD_DETECT_GPIO_PORT
static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
Checks for error conditions for R6 (RCA) response.
void SDIO_ClearITPendingBit(uint32_t SDIO_IT)
Clears the SDIO's interrupt pending bits.
__IO uint8_t RdBlockMisalign
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
Allows to read blocks from a specified address in a card. The Data transfer can be managed by DMA mod...
SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
Allows to erase memory area specified for the given card.
__IO uint8_t RdBlockLen
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Reads the specified input port pin.
__IO uint8_t Reserved1
SD_Error SD_Init(void)
Initializes the SD Card and put it into StandBy State (Ready for data transfer).
__IO uint8_t WriteBlockPaPartial
void DMA_ClearFlag(DMA_Stream_TypeDef *DMAy_Streamx, uint32_t DMA_FLAG)
Clears the DMAy Streamx's pending flags.
static SD_Error CmdResp1Error(uint8_t cmd)
Checks for error conditions for R1 response.
SD Card Status.
__IO uint8_t PermWrProtect
void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState)
Enables or disables the SDIO interrupts.
uint32_t SDIO_HardwareFlowControl
__IO uint8_t MaxBusClkFrec
SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
Allows to write one block starting from a specified address in a card. The Data transfer can be manag...
__IO uint8_t ProdRev
__IO uint8_t WrProtectGrSize
#define SD_CMD_SD_ERASE_GRP_START
static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
Find the SD card SCR register value.
__IO uint8_t MaxRdCurrentVDDMax
uint32_t SDIO_ClockPowerSave
__IO uint8_t TempWrProtect
__IO uint32_t ProdSN
void SD_LowLevel_DeInit(void)
DeInitializes the SDIO interface. Disable SDIO Clock, Set Power State to OFF, DeInitializes the SDIO...
#define SDIO_Wait_No
SD_Error SD_WaitWriteOperation(void)
This function waits until the SDIO DMA data transfer is finished. This function should be called afte...
This file contains all the functions prototypes for the SD Card stm324xg_eval_sdio_sd driver firmware...
__IO uint8_t Reserved1
#define SD_OCR_ADDR_OUT_OF_RANGE
Mask for errors Card Status R1 (OCR Register)
__IO uint16_t OEM_AppliID
SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
Enables wide bus opeartion for the requeseted card if supported by card.
#define SD_CMD_SD_APP_SEND_SCR
#define SD_CMD_SD_ERASE_GRP_END
SD_Error SD_WaitReadOperation(void)
This function waits until the SDIO DMA data transfer is finished. This function should be called afte...
__IO uint8_t ProdName2
__IO uint8_t FileFormatGrouop
SD_Error SD_PowerOFF(void)
Turns the SDIO output signals off.
void SDIO_Init(SDIO_InitTypeDef *SDIO_InitStruct)
Initializes the SDIO peripheral according to the specified parameters in the SDIO_InitStruct.
void SD_LowLevel_Init(void)
Initializes the SD Card and put it into StandBy State (Ready for data transfer).
uint64_t CardCapacity
__IO uint8_t DSRImpl
__IO uint8_t EraseGrSize
__IO uint8_t ManDeflECC
#define SDIO_TRANSFER_CLK_DIV
SDIO Data Transfer Frequency (25MHz max)
uint32_t SDIO_ClockEdge
__IO uint8_t SysSpecVersion
#define NULL
SDIO Static flags, TimeOut, FIFO Address.
SD_Error SD_PowerON(void)
Enquires cards about their operating voltage and configures clock controls.
SD Card information.
uint32_t SDIO_ClockBypass
static SD_Error IsCardProgramming(uint8_t *pstatus)
Checks if the SD card is in programming state.
__IO uint8_t EraseGrMul
SD_Error SD_InitializeCards(void)
Intialises all cards or single card as the case may be Card(s) come into standby state.
__IO uint8_t Reserved3
__IO uint8_t MaxWrCurrentVDDMax
__IO uint8_t WrProtectGrEnable
void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize)
Configures the DMA2 Channel4 for SDIO Rx request.
__IO uint8_t MaxRdCurrentVDDMin
uint32_t CardBlockSize
SD_Error SD_ReadMultiBlocksFIXED(uint8_t *readbuff, uint32_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
void SDIO_ClearFlag(uint32_t SDIO_FLAG)
Clears the SDIO's pending flags.
SDCardState SD_GetState(void)
Returns the current card's state.
__IO uint8_t TAAC
uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
Returns response received from the card for the last command.
ITStatus SDIO_GetITStatus(uint32_t SDIO_IT)
Checks whether the specified SDIO interrupt has occurred or not.
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
Allows to write blocks starting from a specified address in a card. The Data transfer can be managed ...
uint32_t SDIO_BusWide
#define SD_R6_GENERAL_UNKNOWN_ERROR
Masks for R6 Response.
#define SD_CMD_SD_APP_STAUS
#define SD_CMD_APP_SD_SET_BUSWIDTH
Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these co...
SD_Error SD_SendStatus(uint32_t *pcardstatus)
Returns the current card's status.
void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
Initializes the SDIO Command according to the specified parameters in the SDIO_CmdInitStruct and send...
__IO uint32_t DeviceSize
__IO uint16_t CardComdClasses
__IO uint8_t ContentProtectAppli
void SDIO_DeInit(void)
Deinitializes the SDIO peripheral registers to their default reset values.
#define SD_CMD_SD_APP_OP_COND
FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG)
Checks whether the specified SDIO flag is set or not.
__IO uint8_t WrSpeedFact
void SDIO_DataConfig(SDIO_DataInitTypeDef *SDIO_DataInitStruct)
Initializes the SDIO data path according to the specified parameters in the SDIO_DataInitStruct.
static SD_Error CmdError(void)
Checks for error conditions for CMD0.
uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
Converts the number of bytes in power of two and returns the power.
__IO uint8_t Reserved4
__IO uint8_t CSD_CRC
uint32_t SDIO_ReadData(void)
Read one data word from Rx FIFO.
SDIO specific error defines.
#define SDIO_INIT_CLK_DIV
SDIO Intialization Frequency (400KHz max)
static SD_Error CmdResp2Error(void)
Checks for error conditions for R2 (CID or CSD) response.
__IO uint8_t DeviceSizeMul
__IO uint8_t CSDStruct
__IO uint8_t Reserved2
SD_Error
__IO uint8_t Reserved2
#define SD_CMD_SET_REL_ADDR
__IO uint8_t CID_CRC
void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize)
Configures the DMA2 Channel4 for SDIO Tx request.
SDTransferState SD_GetStatus(void)
Gets the cuurent sd card data transfer status.
void SDIO_WriteData(uint32_t Data)
Write one data word to Tx FIFO.
#define SDIO_STD_CAPACITY_SD_CARD_V1_1
Supported SD Memory Cards.
#define SD_DETECT_PIN
uint32_t SDIO_GetPowerState(void)
Gets the power status of the controller.
__IO uint8_t NSAC
static SD_Error CmdResp7Error(void)
Checks for error conditions for R7 response.
SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
Allows to read one block from a specified address in a card. The Data transfer can be managed by DMA ...
__IO uint8_t ManufacturerID
void SDIO_SetPowerState(uint32_t SDIO_PowerState)
Sets the power status of the controller.
__IO uint8_t MaxWrBlockLen
SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
Enables wide bus opeartion for the requeseted card if supported by card.
__IO uint16_t ManufactDate
#define SD_PRESENT
SD detection on its memory slot.
SD_Error SD_StopTransfer(void)
Aborts an ongoing data transfer.
uint8_t SDIO_GetCommandResponse(void)
Returns command index of last command for which response received.
__IO uint8_t MaxWrCurrentVDDMin
SDTransferState
SDIO Transfer state.
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
Returns information about specific card.
static SD_Error SDEnWideBus(FunctionalState NewState)
Enables or disables the SDIO wide bus mode.
static SD_Error CmdResp3Error(void)
Checks for error conditions for R3 (OCR) response.
SDCardState
SD Card States.
uint8_t SD_Detect(void)
Detect if SD card is correctly plugged in the memory slot.
SD_Error SD_SendSDStatus(uint32_t *psdstatus)
Returns the current SD card's status.
SDTransferState SD_GetTransferState(void)
Gets the cuurent data transfer state.
void SD_DeInit(void)
DeInitializes the SDIO interface.
__IO uint8_t CopyFlag
void SDIO_DMACmd(FunctionalState NewState)
Enables or disables the SDIO DMA request.
SD_Error SD_ProcessIRQSrc(void)
Allows to process all the interrupts that are high.
#define SD_CMD_GO_IDLE_STATE
SDIO Commands Index.
__IO uint8_t ECC
void SDIO_ClockCmd(FunctionalState NewState)
Enables or disables the SDIO Clock.
__IO uint8_t FileFormat
__IO uint32_t ProdName1
__IO uint8_t WrBlockMisalign
__IO uint8_t PartBlockRead
void SD_ProcessDMAIRQ(void)
This function waits until the SDIO DMA data transfer is finished.
SD_Error SD_SelectDeselect(uint32_t addr)
Selects od Deselects the corresponding card.
#define SDIO_SEND_IF_COND
Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these co...