sd_mmc.c
Go to the documentation of this file.
1 
33 /*
34  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
35  */
36 
37 #include <string.h>
38 #include "conf_board.h"
39 #include "board.h"
40 #include "conf_sd_mmc.h"
41 #include "sd_mmc_protocol.h"
42 #include "sd_mmc.h"
43 #include "delay.h"
44 #include "ioport.h"
45 
46 #ifdef FREERTOS_USED
47 #include "FreeRTOS.h"
48 #include "task.h"
49 #include "portmacro.h"
50 #include "projdefs.h"
51 #endif
52 
59 // Enable debug information for SD/MMC module
60 #ifdef SD_MMC_DEBUG
61 # include <stdio.h>
62 # define sd_mmc_debug(...) printf(__VA_ARGS__)
63 #else
64 # define sd_mmc_debug(...)
65 #endif
66 
67 #ifndef SD_MMC_SPI_MEM_CNT
68 # define SD_MMC_SPI_MEM_CNT 0
69 #endif
70 #ifndef SD_MMC_MCI_MEM_CNT
71 # define SD_MMC_MCI_MEM_CNT 0
72 #endif
73 #ifndef SD_MMC_HSMCI_MEM_CNT
74 # define SD_MMC_HSMCI_MEM_CNT 0
75 #endif
76 
77 // Macros to switch SD MMC stack to the correct driver (MCI or SPI)
78 #ifdef SD_MMC_SPI_MODE
79 # if (SD_MMC_SPI_MEM_CNT != 0)
80 # include "sd_mmc_spi.h"
81 # define driver sd_mmc_spi
82 # define SD_MMC_MEM_CNT SD_MMC_SPI_MEM_CNT
83 # define sd_mmc_is_spi() true
84 # else
85 # error No SPI interface is defined for SD MMC stack. \
86  SD_MMC_SPI_MEM_CNT must be added in board.h file.
87 # endif
88 #else
89 # if (SD_MMC_HSMCI_MEM_CNT != 0)
90 # include "hsmci.h"
91 # define driver hsmci
92 # define SD_MMC_MEM_CNT SD_MMC_HSMCI_MEM_CNT
93 # define sd_mmc_is_spi() false
94 # elif (SD_MMC_MCI_MEM_CNT != 0)
95 # include "mci.h"
96 # define driver mci
97 # define SD_MMC_MEM_CNT SD_MMC_MCI_MEM_CNT
98 # define sd_mmc_is_spi() false
99 # else
100 # error No MCI or HSMCI interfaces are defined for SD MMC stack. \
101  SD_MMC_MCI_MEM_CNT or SD_MMC_HSMCI_MEM_CNT must be added in board.h file.
102 # endif
103 #endif
104 
105 
106 #define driver_init ATPASTE2(driver, _init)
107 #define driver_select_device ATPASTE2(driver, _select_device)
108 #define driver_deselect_device ATPASTE2(driver, _deselect_device)
109 #define driver_get_bus_width ATPASTE2(driver, _get_bus_width)
110 #define driver_is_high_speed_capable ATPASTE2(driver, _is_high_speed_capable)
111 #define driver_send_clock ATPASTE2(driver, _send_clock)
112 #define driver_send_cmd ATPASTE2(driver, _send_cmd)
113 #define driver_get_response ATPASTE2(driver, _get_response)
114 #define driver_get_response_128 ATPASTE2(driver, _get_response_128)
115 #define driver_adtc_start ATPASTE2(driver, _adtc_start)
116 #define driver_adtc_stop ATPASTE2(driver, _send_cmd)
117 #define driver_read_word ATPASTE2(driver, _read_word)
118 #define driver_write_word ATPASTE2(driver, _write_word)
119 #define driver_start_read_blocks ATPASTE2(driver, _start_read_blocks)
120 #define driver_wait_end_of_read_blocks ATPASTE2(driver, _wait_end_of_read_blocks)
121 #define driver_start_write_blocks ATPASTE2(driver, _start_write_blocks)
122 #define driver_wait_end_of_write_blocks ATPASTE2(driver, _wait_end_of_write_blocks)
123 
124 
125 #if (!defined SD_MMC_0_CD_GPIO) || (!defined SD_MMC_0_CD_DETECT_VALUE)
126 # warning No pin for card detection has been defined in board.h. \
127  The define SD_MMC_0_CD_GPIO, SD_MMC_0_CD_DETECT_VALUE must be added in board.h file.
128 #endif
129 
130 #ifdef SDIO_SUPPORT_ENABLE
131 # define IS_SDIO() (sd_mmc_card->type & CARD_TYPE_SDIO)
132 #else
133 # define IS_SDIO() false
134 #endif
135 
136 #define sd_mmc_is_mci() (!sd_mmc_is_spi())
137 
139 #define SD_MMC_VOLTAGE_SUPPORT \
140  (OCR_VDD_27_28 | OCR_VDD_28_29 | \
141  OCR_VDD_29_30 | OCR_VDD_30_31 | \
142  OCR_VDD_31_32 | OCR_VDD_32_33)
143 
151 };
152 
154 struct sd_mmc_card {
155  uint32_t clock;
156  uint32_t capacity;
157 #if (defined SD_MMC_0_CD_GPIO)
158  uint32_t cd_gpio;
159 # if (defined SD_MMC_0_WP_GPIO)
160  uint32_t wp_gpio;
161 # endif
162 #endif
163  uint16_t rca;
167  uint8_t bus_width;
168  uint8_t csd[CSD_REG_BSIZE];
169  uint8_t high_speed;
170 };
171 
174 static struct sd_mmc_card sd_mmc_cards[SD_MMC_MEM_CNT]
175 #if (defined SD_MMC_0_CD_GPIO) && (defined SD_MMC_0_WP_GPIO)
176  = {
177 # define SD_MMC_CD_WP(slot, unused) \
178  {.cd_gpio = SD_MMC_##slot##_CD_GPIO, \
179  .wp_gpio = SD_MMC_##slot##_WP_GPIO},
180  MREPEAT(SD_MMC_MEM_CNT, SD_MMC_CD_WP, ~)
181 # undef SD_MMC_CD_WP
182 }
183 #elif (defined SD_MMC_0_CD_GPIO)
184  = {
185 # define SD_MMC_CD(slot, unused) \
186  {.cd_gpio = SD_MMC_##slot##_CD_GPIO},
187  MREPEAT(SD_MMC_MEM_CNT, SD_MMC_CD, ~)
188 # undef SD_MMC_CD
189 }
190 #endif
191 ;
192 
194 static uint8_t sd_mmc_slot_sel;
196 static struct sd_mmc_card *sd_mmc_card;
198 static uint16_t sd_mmc_nb_block_to_tranfer = 0;
200 static uint16_t sd_mmc_nb_block_remaining = 0;
201 
203 const uint32_t sd_mmc_trans_units[7] = {
204  10, 100, 1000, 10000, 0, 0, 0
205 };
207 const uint32_t sd_trans_multipliers[16] = {
208  0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
209 };
211 const uint32_t mmc_trans_multipliers[16] = {
212  0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
213 };
214 
217 static bool mmc_spi_op_cond(void);
218 static bool mmc_mci_op_cond(void);
219 static bool sd_spi_op_cond(uint8_t v2);
220 static bool sd_mci_op_cond(uint8_t v2);
221 static bool sdio_op_cond(void);
222 static bool sdio_get_max_speed(void);
223 static bool sdio_cmd52_set_bus_width(void);
224 static bool sdio_cmd52_set_high_speed(void);
225 static bool sd_cm6_set_high_speed(void);
226 static bool mmc_cmd6_set_bus_width(uint8_t bus_width);
227 static bool mmc_cmd6_set_high_speed(void);
228 static bool sd_cmd8(uint8_t * v2);
229 static bool mmc_cmd8(uint8_t *b_authorize_high_speed);
230 static bool sd_mmc_cmd9_spi(void);
231 static bool sd_mmc_cmd9_mci(void);
232 static void mmc_decode_csd(void);
233 static void sd_decode_csd(void);
234 static bool sd_mmc_cmd13(void);
235 #ifdef SDIO_SUPPORT_ENABLE
236 static bool sdio_cmd52(uint8_t rw_flag, uint8_t func_nb,
237  uint32_t reg_addr, uint8_t rd_after_wr, uint8_t *io_data);
238 static bool sdio_cmd53(uint8_t rw_flag, uint8_t func_nb, uint32_t reg_addr,
239  uint8_t inc_addr, uint32_t size, bool access_block);
240 #endif // SDIO_SUPPORT_ENABLE
241 static bool sd_acmd6(void);
242 static bool sd_acmd51(void);
244 
247 static sd_mmc_err_t sd_mmc_select_slot(uint8_t slot);
248 static void sd_mmc_configure_slot(void);
249 static void sd_mmc_deselect_slot(void);
250 static bool sd_mmc_spi_card_init(void);
251 static bool sd_mmc_mci_card_init(void);
252 static bool sd_mmc_spi_install_mmc(void);
253 static bool sd_mmc_mci_install_mmc(void);
255 
256 
259 #define SD_MMC_DEBOUNCE_TIMEOUT 1000 // Unit ms
260 
261 #if XMEGA
262 # define SD_MMC_START_TIMEOUT() delay_ms(SD_MMC_DEBOUNCE_TIMEOUT)
263 # define SD_MMC_IS_TIMEOUT() true
264 # define SD_MMC_STOP_TIMEOUT()
265 #endif
266 
267 #if UC3
268 static t_cpu_time timer;
269 # define SD_MMC_START_TIMEOUT() \
270  cpu_set_timeout(cpu_ms_2_cy(SD_MMC_DEBOUNCE_TIMEOUT, sysclk_get_cpu_hz()), &timer)
271 # define SD_MMC_IS_TIMEOUT() \
272  cpu_is_timeout(&timer)
273 # define SD_MMC_STOP_TIMEOUT()
274 #endif
275 
276 #if SAM
277 static bool sd_mmc_sam_systick_used;
278 # ifdef FREERTOS_USED
279  static xTimeOutType xTimeOut;
280 #endif
281 
282 static inline void SD_MMC_START_TIMEOUT(void)
283 {
284  if (!SysTick->CTRL) {
285  sd_mmc_sam_systick_used = true;
286  SysTick->LOAD = (sysclk_get_cpu_hz() / (8 * 1000))
289  } else {
290  sd_mmc_sam_systick_used = false;
291 #ifdef FREERTOS_USED
292  // Note: the define INCLUDE_vTaskDelay must be set to one
293  // in FreeRTOSConfig.h file.
294  vTaskSetTimeOutState(&xTimeOut);
295 #else
297 #endif
298  }
299 }
300 
301 static inline bool SD_MMC_IS_TIMEOUT(void)
302 {
303  if (!sd_mmc_sam_systick_used) {
304 #ifdef FREERTOS_USED
305  portTickType xTicksToWait =
307  return (xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdTRUE);
308 #else
309  return true;
310 #endif
311  }
312  if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
313  SysTick->CTRL = 0;
314  return true;
315  }
316  return false;
317 }
318 
319 static inline void SD_MMC_STOP_TIMEOUT(void)
320 {
321  if (sd_mmc_sam_systick_used) {
322  SysTick->CTRL = 0;
323  }
324 }
325 #endif
326 
335 static bool mmc_spi_op_cond(void)
336 {
337  uint32_t retry, resp;
338 
339  /*
340  * Timeout 1s = 400KHz / ((6+1)*8) cylces = 7150 retry
341  * 6 = cmd byte size
342  * 1 = response byte size
343  */
344  retry = 7150;
345  do {
347  sd_mmc_debug("%s: CMD1 SPI Fail - Busy retry %d\n\r",
348  __func__, (int)(7150 - retry));
349  return false;
350  }
351  // Check busy flag
352  resp = driver_get_response();
353  if (!(resp & R1_SPI_IDLE)) {
354  break;
355  }
356  if (retry-- == 0) {
357  sd_mmc_debug("%s: CMD1 Timeout on busy\n\r", __func__);
358  return false;
359  }
360  } while (1);
361 
362  // Read OCR for SPI mode
364  sd_mmc_debug("%s: CMD58 Fail\n\r", __func__);
365  return false;
366  }
367  // Check OCR value
370  sd_mmc_card->type |= CARD_TYPE_HC;
371  }
372  return true;
373 }
374 
382 static bool mmc_mci_op_cond(void)
383 {
384  uint32_t retry, resp;
385 
386  /*
387  * Timeout 1s = 400KHz / ((6+6)*8) cylces = 4200 retry
388  * 6 = cmd byte size
389  * 6 = response byte size
390  */
391  retry = 4200;
392  do {
395  sd_mmc_debug("%s: CMD1 MCI Fail - Busy retry %d\n\r",
396  __func__, (int)(4200 - retry));
397  return false;
398  }
399  // Check busy flag
400  resp = driver_get_response();
401  if (resp & OCR_POWER_UP_BUSY) {
402  // Check OCR value
403  if ((resp & OCR_ACCESS_MODE_MASK)
405  sd_mmc_card->type |= CARD_TYPE_HC;
406  }
407  break;
408  }
409  if (retry-- == 0) {
410  sd_mmc_debug("%s: CMD1 Timeout on busy\n\r", __func__);
411  return false;
412  }
413  } while (1);
414  return true;
415 }
416 
426 static bool sd_spi_op_cond(uint8_t v2)
427 {
428  uint32_t arg, retry, resp;
429 
430  /*
431  * Timeout 1s = 400KHz / ((6+1)*8) cylces = 7150 retry
432  * 6 = cmd byte size
433  * 1 = response byte size
434  */
435  retry = 7150;
436  do {
437  // CMD55 - Indicate to the card that the next command is an
438  // application specific command rather than a standard command.
440  sd_mmc_debug("%s: CMD55 Fail\n\r", __func__);
441  return false;
442  }
443 
444  // (ACMD41) Sends host OCR register
445  arg = 0;
446  if (v2) {
447  arg |= SD_ACMD41_HCS;
448  }
449  // Check response
451  sd_mmc_debug("%s: ACMD41 Fail\n\r", __func__);
452  return false;
453  }
454  resp = driver_get_response();
455  if (!(resp & R1_SPI_IDLE)) {
456  // Card is ready
457  break;
458  }
459  if (retry-- == 0) {
460  sd_mmc_debug("%s: ACMD41 Timeout on busy, resp32 0x%08x \n\r",
461  __func__, resp);
462  return false;
463  }
464  } while (1);
465 
466  // Read OCR for SPI mode
468  sd_mmc_debug("%s: CMD58 Fail\n\r", __func__);
469  return false;
470  }
471  if ((driver_get_response() & OCR_CCS) != 0) {
472  sd_mmc_card->type |= CARD_TYPE_HC;
473  }
474  return true;
475 }
476 
486 static bool sd_mci_op_cond(uint8_t v2)
487 {
488  uint32_t arg, retry, resp;
489 
490  /*
491  * Timeout 1s = 400KHz / ((6+6+6+6)*8) cylces = 2100 retry
492  * 6 = cmd byte size
493  * 6 = response byte size
494  * 6 = cmd byte size
495  * 6 = response byte size
496  */
497  retry = 2100;
498  do {
499  // CMD55 - Indicate to the card that the next command is an
500  // application specific command rather than a standard command.
502  sd_mmc_debug("%s: CMD55 Fail\n\r", __func__);
503  return false;
504  }
505 
506  // (ACMD41) Sends host OCR register
508  if (v2) {
509  arg |= SD_ACMD41_HCS;
510  }
511  // Check response
513  sd_mmc_debug("%s: ACMD41 Fail\n\r", __func__);
514  return false;
515  }
516  resp = driver_get_response();
517  if (resp & OCR_POWER_UP_BUSY) {
518  // Card is ready
519  if ((resp & OCR_CCS) != 0) {
520  sd_mmc_card->type |= CARD_TYPE_HC;
521  }
522  break;
523  }
524  if (retry-- == 0) {
525  sd_mmc_debug("%s: ACMD41 Timeout on busy, resp32 0x%08x \n\r",
526  __func__, resp);
527  return false;
528  }
529  } while (1);
530  return true;
531 }
532 
533 #ifdef SDIO_SUPPORT_ENABLE
534 
543 static bool sdio_op_cond(void)
544 {
545  uint32_t resp;
546 
547  // CMD5 - SDIO send operation condition (OCR) command.
549  sd_mmc_debug("%s: CMD5 Fail\n\r", __func__);
550  return true; // No error but card type not updated
551  }
552  resp = driver_get_response();
553  if ((resp & OCR_SDIO_NF) == 0) {
554  return true; // No error but card type not updated
555  }
556 
557  /*
558  * Wait card ready
559  * Timeout 1s = 400KHz / ((6+4)*8) cylces = 5000 retry
560  * 6 = cmd byte size
561  * 4(SPI) 6(MCI) = response byte size
562  */
563  uint32_t cmd5_retry = 5000;
564  while (1) {
565  // CMD5 - SDIO send operation condition (OCR) command.
567  resp & SD_MMC_VOLTAGE_SUPPORT)) {
568  sd_mmc_debug("%s: CMD5 Fail\n\r", __func__);
569  return false;
570  }
571  resp = driver_get_response();
572  if ((resp & OCR_POWER_UP_BUSY) == OCR_POWER_UP_BUSY) {
573  break;
574  }
575  if (cmd5_retry-- == 0) {
576  sd_mmc_debug("%s: CMD5 Timeout on busy\n\r", __func__);
577  return false;
578  }
579  }
580  // Update card type at the end of busy
581  if ((resp & OCR_SDIO_MP) > 0) {
582  sd_mmc_card->type = CARD_TYPE_SD_COMBO;
583  } else {
584  sd_mmc_card->type = CARD_TYPE_SDIO;
585  }
586  return true; // No error and card type updated with SDIO type
587 }
588 
599 static bool sdio_get_max_speed(void)
600 {
601  uint32_t addr_new, addr_old;
602  uint8_t buf[6];
603  uint32_t unit;
604  uint32_t mul;
605  uint8_t tplfe_max_tran_speed, i;
606  uint8_t addr_cis[4];
607 
608  /* Read CIS area address in CCCR area */
609  addr_old = SDIO_CCCR_CIS_PTR;
610  for(i = 0; i < 4; i++) {
611  sdio_cmd52(SDIO_CMD52_READ_FLAG, SDIO_CIA, addr_old, 0, &addr_cis[i]);
612  addr_old++;
613  }
614  addr_old = addr_cis[0] + (addr_cis[1] << 8) + \
615  (addr_cis[2] << 16) + (addr_cis[3] << 24);
616  addr_new = addr_old;
617 
618  while (1) {
619  /* Read a sample of CIA area */
620  for(i=0; i<3; i++) {
621  sdio_cmd52(SDIO_CMD52_READ_FLAG, SDIO_CIA, addr_new, 0, &buf[i]);
622  addr_new++;
623  }
624  if (buf[0] == SDIO_CISTPL_END) {
625  return false; /* Tuple error */
626  }
627  if (buf[0] == SDIO_CISTPL_FUNCE && buf[2] == 0x00) {
628  break; /* Fun0 tuple found */
629  }
630  if (buf[1] == 0) {
631  return false; /* Tuple error */
632  }
633  /* Next address */
634  addr_new += buf[1]-1;
635  if (addr_new > (addr_old + 256)) {
636  return false; /* Outoff CIS area */
637  }
638  }
639 
640  /* Read all Fun0 tuple fields: fn0_blk_siz & max_tran_speed */
641  addr_new -= 3;
642  for(i = 0; i < 6; i++) {
643  sdio_cmd52(SDIO_CMD52_READ_FLAG, SDIO_CIA, addr_new, 0, &buf[i]);
644  addr_new++;
645  }
646 
647  tplfe_max_tran_speed = buf[5];
648  if (tplfe_max_tran_speed > 0x32) {
649  /* Error on SDIO register, the high speed is not activated
650  * and the clock can not be more than 25MHz.
651  * This error is present on specific SDIO card
652  * (H&D wireless card - HDG104 WiFi SIP).
653  */
654  tplfe_max_tran_speed = 0x32; /* 25Mhz */
655  }
656 
657  /* Decode transfer speed in Hz.*/
658  unit = sd_mmc_trans_units[tplfe_max_tran_speed & 0x7];
659  mul = sd_trans_multipliers[(tplfe_max_tran_speed >> 3) & 0xF];
660  sd_mmc_card->clock = unit * mul * 1000;
668  return true;
669 }
670 
678 static bool sdio_cmd52_set_bus_width(void)
679 {
686  uint8_t u8_value;
687 
688  // Check 4bit support in 4BLS of "Card Capability" register
689  if (!sdio_cmd52(SDIO_CMD52_READ_FLAG, SDIO_CIA, SDIO_CCCR_CAP,
690  0, &u8_value)) {
691  return false;
692  }
693  if ((u8_value & SDIO_CAP_4BLS) != SDIO_CAP_4BLS) {
694  // No supported, it is not a protocol error
695  return true;
696  }
697  // HS mode possible, then enable
698  u8_value = SDIO_BUSWIDTH_4B;
700  1, &u8_value)) {
701  return false;
702  }
703  sd_mmc_card->bus_width = 4;
704  sd_mmc_debug("%d-bit bus width enabled.\n\r", (int)sd_mmc_card->bus_width);
705  return true;
706 }
707 
716 static bool sdio_cmd52_set_high_speed(void)
717 {
718  uint8_t u8_value;
719 
720  // Check CIA.HS
721  if (!sdio_cmd52(SDIO_CMD52_READ_FLAG, SDIO_CIA, SDIO_CCCR_HS, 0, &u8_value)) {
722  return false;
723  }
724  if ((u8_value & SDIO_SHS) != SDIO_SHS) {
725  // No supported, it is not a protocol error
726  return true;
727  }
728  // HS mode possible, then enable
729  u8_value = SDIO_EHS;
730  if (!sdio_cmd52(SDIO_CMD52_WRITE_FLAG, SDIO_CIA, SDIO_CCCR_HS,
731  1, &u8_value)) {
732  return false;
733  }
734  sd_mmc_card->high_speed = 1;
735  sd_mmc_card->clock *= 2;
736  return true;
737 }
738 
739 #else
740 static bool sdio_op_cond(void)
741 {
742  return true; // No error but card type not updated
743 }
744 static bool sdio_get_max_speed(void)
745 {
746  return false;
747 }
748 static bool sdio_cmd52_set_bus_width(void)
749 {
750  return false;
751 }
752 static bool sdio_cmd52_set_high_speed(void)
753 {
754  return false;
755 }
756 #endif // SDIO_SUPPORT_ENABLE
757 
767 static bool sd_cm6_set_high_speed(void)
768 {
769  uint8_t switch_status[SD_SW_STATUS_BSIZE] = {0};
770 
779  SD_SW_STATUS_BSIZE, 1, true)) {
780  return false;
781  }
782  if (!driver_start_read_blocks(switch_status, 1)) {
783  return false;
784  }
786  return false;
787  }
788 
790  sd_mmc_debug("%s: CMD6 CARD_STATUS_SWITCH_ERROR\n\r", __func__);
791  return false;
792  }
793  if (SD_SW_STATUS_FUN_GRP1_RC(switch_status)
795  // No supported, it is not a protocol error
796  return true;
797  }
798  if (SD_SW_STATUS_FUN_GRP1_BUSY(switch_status)) {
799  sd_mmc_debug("%s: CMD6 SD_SW_STATUS_FUN_GRP1_BUSY\n\r", __func__);
800  return false;
801  }
802  // CMD6 function switching period is within 8 clocks
803  // after the end bit of status data.
805  sd_mmc_card->high_speed = 1;
806  sd_mmc_card->clock *= 2;
807  return true;
808 }
809 
820 static bool mmc_cmd6_set_bus_width(uint8_t bus_width)
821 {
822  uint32_t arg;
823 
824  switch (bus_width) {
825  case 8:
829  break;
830  case 4:
834  break;
835  default:
839  break;
840  }
841  if (!driver_send_cmd(MMC_CMD6_SWITCH, arg)) {
842  return false;
843  }
845  // No supported, it is not a protocol error
846  sd_mmc_debug("%s: CMD6 CARD_STATUS_SWITCH_ERROR\n\r", __func__);
847  return false;
848  }
849  sd_mmc_card->bus_width = bus_width;
850  sd_mmc_debug("%d-bit bus width enabled.\n\r", (int)sd_mmc_card->bus_width);
851  return true;
852 }
853 
863 static bool mmc_cmd6_set_high_speed(void)
864 {
869  return false;
870  }
872  // No supported, it is not a protocol error
873  sd_mmc_debug("%s: CMD6 CARD_STATUS_SWITCH_ERROR\n\r", __func__);
874  return false;
875  }
876  sd_mmc_card->high_speed = 1;
877  sd_mmc_card->clock = 52000000lu;
878  return true;
879 }
880 
894 static bool sd_cmd8(uint8_t * v2)
895 {
896  uint32_t resp;
897 
898  *v2 = 0;
899  // Test for SD version 2
902  return true; // It is not a V2
903  }
904  // Check R7 response
905  resp = driver_get_response();
906  if (resp == 0xFFFFFFFF) {
907  // No compliance R7 value
908  return true; // It is not a V2
909  }
912  sd_mmc_debug("%s: CMD8 resp32 0x%08x UNUSABLE CARD\n\r",
913  __func__, resp);
914  return false;
915  }
916  sd_mmc_debug("SD card V2\n\r");
917  *v2 = 1;
918  return true;
919 }
920 
929 static bool mmc_cmd8(uint8_t *b_authorize_high_speed)
930 {
931  uint16_t i;
932  uint32_t ext_csd;
933  uint32_t sec_count;
934 
936  EXT_CSD_BSIZE, 1, false)) {
937  return false;
938  }
939  //** Read and decode Extended Extended CSD
940  // Note: The read access is done in byte to avoid a buffer
941  // of EXT_CSD_BSIZE Byte in stack.
942 
943  // Read card type
944  for (i = 0; i < (EXT_CSD_CARD_TYPE_INDEX + 4) / 4; i++) {
945  if (!driver_read_word(&ext_csd)) {
946  return false;
947  }
948  }
949  *b_authorize_high_speed = (ext_csd >> ((EXT_CSD_CARD_TYPE_INDEX % 4) * 8))
950  & MMC_CTYPE_52MHZ;
951 
952  if (MMC_CSD_C_SIZE(sd_mmc_card->csd) == 0xFFF) {
953  // For high capacity SD/MMC card,
954  // memory capacity = SEC_COUNT * 512 byte
955  for (; i <(EXT_CSD_SEC_COUNT_INDEX + 4) / 4; i++) {
956  if (!driver_read_word(&sec_count)) {
957  return false;
958  }
959  }
960  sd_mmc_card->capacity = sec_count / 2;
961  }
962  for (; i < EXT_CSD_BSIZE / 4; i++) {
963  if (!driver_read_word(&sec_count)) {
964  return false;
965  }
966  }
967  return true;
968 }
969 
976 static bool sd_mmc_cmd9_spi(void)
977 {
978  if (!driver_adtc_start(SDMMC_SPI_CMD9_SEND_CSD, (uint32_t)sd_mmc_card->rca << 16,
979  CSD_REG_BSIZE, 1, true)) {
980  return false;
981  }
982  if (!driver_start_read_blocks(sd_mmc_card->csd, 1)) {
983  return false;
984  }
986 }
987 
994 static bool sd_mmc_cmd9_mci(void)
995 {
996  if (!driver_send_cmd(SDMMC_MCI_CMD9_SEND_CSD, (uint32_t)sd_mmc_card->rca << 16)) {
997  return false;
998  }
999  driver_get_response_128(sd_mmc_card->csd);
1000  return true;
1001 }
1002 
1006 static void mmc_decode_csd(void)
1007 {
1008  uint32_t unit;
1009  uint32_t mul;
1010  uint32_t tran_speed;
1011 
1012  // Get MMC System Specification version supported by the card
1013  switch (MMC_CSD_SPEC_VERS(sd_mmc_card->csd)) {
1014  default:
1015  case 0:
1016  sd_mmc_card->version = CARD_VER_MMC_1_2;
1017  break;
1018 
1019  case 1:
1020  sd_mmc_card->version = CARD_VER_MMC_1_4;
1021  break;
1022 
1023  case 2:
1024  sd_mmc_card->version = CARD_VER_MMC_2_2;
1025  break;
1026 
1027  case 3:
1028  sd_mmc_card->version = CARD_VER_MMC_3;
1029  break;
1030 
1031  case 4:
1032  sd_mmc_card->version = CARD_VER_MMC_4;
1033  break;
1034  }
1035 
1036  // Get MMC memory max transfer speed in Hz.
1037  tran_speed = CSD_TRAN_SPEED(sd_mmc_card->csd);
1038  unit = sd_mmc_trans_units[tran_speed & 0x7];
1039  mul = mmc_trans_multipliers[(tran_speed >> 3) & 0xF];
1040  sd_mmc_card->clock = unit * mul * 1000;
1041 
1042  /*
1043  * Get card capacity.
1044  * ----------------------------------------------------
1045  * For normal SD/MMC card:
1046  * memory capacity = BLOCKNR * BLOCK_LEN
1047  * Where
1048  * BLOCKNR = (C_SIZE+1) * MULT
1049  * MULT = 2 ^ (C_SIZE_MULT+2) (C_SIZE_MULT < 8)
1050  * BLOCK_LEN = 2 ^ READ_BL_LEN (READ_BL_LEN < 12)
1051  * ----------------------------------------------------
1052  * For high capacity SD/MMC card:
1053  * memory capacity = SEC_COUNT * 512 byte
1054  */
1055  if (MMC_CSD_C_SIZE(sd_mmc_card->csd) != 0xFFF) {
1056  uint32_t blocknr = ((MMC_CSD_C_SIZE(sd_mmc_card->csd) + 1) *
1057  (1 << (MMC_CSD_C_SIZE_MULT(sd_mmc_card->csd) + 2)));
1058  sd_mmc_card->capacity = blocknr *
1059  (1 << MMC_CSD_READ_BL_LEN(sd_mmc_card->csd)) / 1024;
1060  }
1061 }
1062 
1066 static void sd_decode_csd(void)
1067 {
1068  uint32_t unit;
1069  uint32_t mul;
1070  uint32_t tran_speed;
1071 
1072  // Get SD memory maximum transfer speed in Hz.
1073  tran_speed = CSD_TRAN_SPEED(sd_mmc_card->csd);
1074  unit = sd_mmc_trans_units[tran_speed & 0x7];
1075  mul = sd_trans_multipliers[(tran_speed >> 3) & 0xF];
1076  sd_mmc_card->clock = unit * mul * 1000;
1077 
1078  /*
1079  * Get card capacity.
1080  * ----------------------------------------------------
1081  * For normal SD/MMC card:
1082  * memory capacity = BLOCKNR * BLOCK_LEN
1083  * Where
1084  * BLOCKNR = (C_SIZE+1) * MULT
1085  * MULT = 2 ^ (C_SIZE_MULT+2) (C_SIZE_MULT < 8)
1086  * BLOCK_LEN = 2 ^ READ_BL_LEN (READ_BL_LEN < 12)
1087  * ----------------------------------------------------
1088  * For high capacity SD card:
1089  * memory capacity = (C_SIZE+1) * 512K byte
1090  */
1091  if (CSD_STRUCTURE_VERSION(sd_mmc_card->csd) >= SD_CSD_VER_2_0) {
1092  sd_mmc_card->capacity =
1093  (SD_CSD_2_0_C_SIZE(sd_mmc_card->csd) + 1)
1094  * 512;
1095  } else {
1096  uint32_t blocknr = ((SD_CSD_1_0_C_SIZE(sd_mmc_card->csd) + 1) *
1097  (1 << (SD_CSD_1_0_C_SIZE_MULT(sd_mmc_card->csd) + 2)));
1098  sd_mmc_card->capacity = blocknr *
1099  (1 << SD_CSD_1_0_READ_BL_LEN(sd_mmc_card->csd))
1100  / 1024;
1101  }
1102 }
1103 
1110 static bool sd_mmc_cmd13(void)
1111 {
1112  uint32_t nec_timeout;
1113 
1114  /* Wait for data ready status.
1115  * Nec timing: 0 to unlimited
1116  * However a timeout is used.
1117  * 200 000 * 8 cycles
1118  */
1119  nec_timeout = 200000;
1120  do {
1121  if (sd_mmc_is_spi()) {
1123  return false;
1124  }
1125  // Check busy flag
1126  if (!(driver_get_response() & 0xFF)) {
1127  break;
1128  }
1129  } else {
1131  (uint32_t)sd_mmc_card->rca << 16)) {
1132  return false;
1133  }
1134  // Check busy flag
1136  break;
1137  }
1138  }
1139  if (nec_timeout-- == 0) {
1140  sd_mmc_debug("%s: CMD13 Busy timeout\n\r", __func__);
1141  return false;
1142  }
1143  } while (1);
1144 
1145  return true;
1146 }
1147 
1148 #ifdef SDIO_SUPPORT_ENABLE
1149 
1160 #if defined(__GNUC__)
1161 #pragma GCC diagnostic push
1162 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
1163 #endif
1164 
1165 static bool sdio_cmd52(uint8_t rw_flag, uint8_t func_nb,
1166  uint32_t reg_addr, uint8_t rd_after_wr, uint8_t *io_data)
1167 {
1168  Assert(io_data != NULL);
1170  ((uint32_t)*io_data << SDIO_CMD52_WR_DATA)
1171  | ((uint32_t)rw_flag << SDIO_CMD52_RW_FLAG)
1172  | ((uint32_t)func_nb << SDIO_CMD52_FUNCTION_NUM)
1173  | ((uint32_t)rd_after_wr << SDIO_CMD52_RAW_FLAG)
1174  | ((uint32_t)reg_addr << SDIO_CMD52_REG_ADRR))) {
1175  return false;
1176  }
1177  *io_data = driver_get_response() & 0xFF;
1178  return true;
1179 }
1180 
1181 #if defined(__GNUC__)
1182 #pragma GCC diagnostic pop
1183 #endif
1184 
1200 static bool sdio_cmd53(uint8_t rw_flag, uint8_t func_nb, uint32_t reg_addr,
1201  uint8_t inc_addr, uint32_t size, bool access_block)
1202 {
1203  Assert(size != 0);
1204  Assert(size <= 512);
1205 
1206  return driver_adtc_start((rw_flag == SDIO_CMD53_READ_FLAG)?
1209  ((size % 512) << SDIO_CMD53_COUNT)
1210  | ((uint32_t)reg_addr << SDIO_CMD53_REG_ADDR)
1211  | ((uint32_t)inc_addr << SDIO_CMD53_OP_CODE)
1212  | ((uint32_t)0 << SDIO_CMD53_BLOCK_MODE)
1213  | ((uint32_t)func_nb << SDIO_CMD53_FUNCTION_NUM)
1214  | ((uint32_t)rw_flag << SDIO_CMD53_RW_FLAG),
1215  size, 1, access_block);
1216 }
1217 #endif // SDIO_SUPPORT_ENABLE
1218 
1224 static bool sd_acmd6(void)
1225 {
1226  // CMD55 - Indicate to the card that the next command is an
1227  // application specific command rather than a standard command.
1228  if (!driver_send_cmd(SDMMC_CMD55_APP_CMD, (uint32_t)sd_mmc_card->rca << 16)) {
1229  return false;
1230  }
1231  // 10b = 4 bits bus
1233  return false;
1234  }
1235  sd_mmc_card->bus_width = 4;
1236  sd_mmc_debug("%d-bit bus width enabled.\n\r", (int)sd_mmc_card->bus_width);
1237  return true;
1238 }
1239 
1251 static bool sd_acmd51(void)
1252 {
1253  uint8_t scr[SD_SCR_REG_BSIZE];
1254 
1255  // CMD55 - Indicate to the card that the next command is an
1256  // application specific command rather than a standard command.
1257  if (!driver_send_cmd(SDMMC_CMD55_APP_CMD, (uint32_t)sd_mmc_card->rca << 16)) {
1258  return false;
1259  }
1261  SD_SCR_REG_BSIZE, 1, true)) {
1262  return false;
1263  }
1264  if (!driver_start_read_blocks(scr, 1)) {
1265  return false;
1266  }
1268  return false;
1269  }
1270 
1271  // Get SD Memory Card - Spec. Version
1272  switch (SD_SCR_SD_SPEC(scr)) {
1273  case SD_SCR_SD_SPEC_1_0_01:
1274  sd_mmc_card->version = CARD_VER_SD_1_0;
1275  break;
1276 
1277  case SD_SCR_SD_SPEC_1_10:
1278  sd_mmc_card->version = CARD_VER_SD_1_10;
1279  break;
1280 
1281  case SD_SCR_SD_SPEC_2_00:
1282  if (SD_SCR_SD_SPEC3(scr) == SD_SCR_SD_SPEC_3_00) {
1283  sd_mmc_card->version = CARD_VER_SD_3_0;
1284  } else {
1285  sd_mmc_card->version = CARD_VER_SD_2_0;
1286  }
1287  break;
1288 
1289  default:
1290  sd_mmc_card->version = CARD_VER_SD_1_0;
1291  break;
1292  }
1293  return true;
1294 }
1295 
1307 static sd_mmc_err_t sd_mmc_select_slot(uint8_t slot)
1308 {
1309  if (slot >= SD_MMC_MEM_CNT) {
1310  return SD_MMC_ERR_SLOT;
1311  }
1313 
1314 #if (defined SD_MMC_0_CD_GPIO)
1315  if (ioport_get_pin_level(sd_mmc_cards[slot].cd_gpio)
1319  SD_MMC_STOP_TIMEOUT();
1320  }
1322  return SD_MMC_ERR_NO_CARD;
1323  }
1325  // A card plug on going, but this is not initialized
1327  // Debounce + Power On Setup
1328  SD_MMC_START_TIMEOUT();
1329  return SD_MMC_ERR_NO_CARD;
1330  }
1332  if (!SD_MMC_IS_TIMEOUT()) {
1333  // Debounce on going
1334  return SD_MMC_ERR_NO_CARD;
1335  }
1336  // Card is not initialized
1338  // Set 1-bit bus width and low clock for initialization
1340  sd_mmc_cards[slot].bus_width = 1;
1341  sd_mmc_cards[slot].high_speed = 0;
1342  }
1344  return SD_MMC_ERR_UNUSABLE;
1345  }
1346 #else
1347  // No pin card detection, then always try to install it
1350  // Card is not initialized
1352  // Set 1-bit bus width and low clock for initialization
1354  sd_mmc_cards[slot].bus_width = 1;
1355  sd_mmc_cards[slot].high_speed = 0;
1356  }
1357 #endif
1358 
1359  // Initialize interface
1360  sd_mmc_slot_sel = slot;
1361  sd_mmc_card = &sd_mmc_cards[slot];
1363  return (sd_mmc_cards[slot].state == SD_MMC_CARD_STATE_INIT) ?
1365 }
1366 
1370 static void sd_mmc_configure_slot(void)
1371 {
1373  sd_mmc_card->bus_width, sd_mmc_card->high_speed);
1374 }
1375 
1379 static void sd_mmc_deselect_slot(void)
1380 {
1381  if (sd_mmc_slot_sel < SD_MMC_MEM_CNT) {
1383  }
1384 }
1385 
1396 static bool sd_mmc_spi_card_init(void)
1397 {
1398  uint8_t v2 = 0;
1399 
1400  // In first, try to install SD/SDIO card
1401  sd_mmc_card->type = CARD_TYPE_SD;
1402  sd_mmc_card->version = CARD_VER_UNKNOWN;
1403  sd_mmc_card->rca = 0;
1404  sd_mmc_debug("Start SD card install\n\r");
1405 
1406  // Card need of 74 cycles clock minimum to start
1408 
1409  // CMD0 - Reset all cards to idle state.
1411  return false;
1412  }
1413  if (!sd_cmd8(&v2)) {
1414  return false;
1415  }
1416  // Try to get the SDIO card's operating condition
1417  if (!sdio_op_cond()) {
1418  return false;
1419  }
1420 
1421  if (sd_mmc_card->type & CARD_TYPE_SD) {
1422  // Try to get the SD card's operating condition
1423  if (!sd_spi_op_cond(v2)) {
1424  // It is not a SD card
1425  sd_mmc_debug("Start MMC Install\n\r");
1426  sd_mmc_card->type = CARD_TYPE_MMC;
1427  return sd_mmc_spi_install_mmc();
1428  }
1429 
1430  /* The CRC on card is disabled by default.
1431  * However, to be sure, the CRC OFF command is send.
1432  * Unfortunately, specific SDIO card does not support it
1433  * (H&D wireless card - HDG104 WiFi SIP)
1434  * and the command is send only on SD card.
1435  */
1437  return false;
1438  }
1439  }
1440  // SD MEMORY
1441  if (sd_mmc_card->type & CARD_TYPE_SD) {
1442  // Get the Card-Specific Data
1443  if (!sd_mmc_cmd9_spi()) {
1444  return false;
1445  }
1446  sd_decode_csd();
1447  // Read the SCR to get card version
1448  if (!sd_acmd51()) {
1449  return false;
1450  }
1451  }
1452  if (IS_SDIO()) {
1453  if (!sdio_get_max_speed()) {
1454  return false;
1455  }
1456  }
1457  // SD MEMORY not HC, Set default block size
1458  if ((sd_mmc_card->type & CARD_TYPE_SD) &&
1459  (0 == (sd_mmc_card->type & CARD_TYPE_HC))) {
1461  return false;
1462  }
1463  }
1464  // Check communication
1465  if (sd_mmc_card->type & CARD_TYPE_SD) {
1466  if (!sd_mmc_cmd13()) {
1467  return false;
1468  }
1469  }
1470  // Reinitialize the slot with the new speed
1472  return true;
1473 }
1474 
1485 static bool sd_mmc_mci_card_init(void)
1486 {
1487  uint8_t v2 = 0;
1488 #ifdef SDIO_SUPPORT_ENABLE
1489  uint8_t data = 0x08;
1490 #endif
1491 
1492  // In first, try to install SD/SDIO card
1493  sd_mmc_card->type = CARD_TYPE_SD;
1494  sd_mmc_card->version = CARD_VER_UNKNOWN;
1495  sd_mmc_card->rca = 0;
1496  sd_mmc_debug("Start SD card install\n\r");
1497 
1498  // Card need of 74 cycles clock minimum to start
1500 
1501 #ifdef SDIO_SUPPORT_ENABLE
1502  /* CMD52 Reset SDIO */
1503  sdio_cmd52(SDIO_CMD52_WRITE_FLAG, SDIO_CIA,SDIO_CCCR_IOA, 0, &data);
1504 #endif
1505 
1506  // CMD0 - Reset all cards to idle state.
1508  return false;
1509  }
1510  if (!sd_cmd8(&v2)) {
1511  return false;
1512  }
1513  // Try to get the SDIO card's operating condition
1514  if (!sdio_op_cond()) {
1515  return false;
1516  }
1517 
1518  if (sd_mmc_card->type & CARD_TYPE_SD) {
1519  // Try to get the SD card's operating condition
1520  if (!sd_mci_op_cond(v2)) {
1521  // It is not a SD card
1522  sd_mmc_debug("Start MMC Install\n\r");
1523  sd_mmc_card->type = CARD_TYPE_MMC;
1524  return sd_mmc_mci_install_mmc();
1525  }
1526  }
1527 
1528  if (sd_mmc_card->type & CARD_TYPE_SD) {
1529  // SD MEMORY, Put the Card in Identify Mode
1530  // Note: The CID is not used in this stack
1532  return false;
1533  }
1534  }
1535  // Ask the card to publish a new relative address (RCA).
1537  return false;
1538  }
1539  sd_mmc_card->rca = (driver_get_response() >> 16) & 0xFFFF;
1540 
1541  // SD MEMORY, Get the Card-Specific Data
1542  if (sd_mmc_card->type & CARD_TYPE_SD) {
1543  if (!sd_mmc_cmd9_mci()) {
1544  return false;
1545  }
1546  sd_decode_csd();
1547  }
1548  // Select the and put it into Transfer Mode
1550  (uint32_t)sd_mmc_card->rca << 16)) {
1551  return false;
1552  }
1553  // SD MEMORY, Read the SCR to get card version
1554  if (sd_mmc_card->type & CARD_TYPE_SD) {
1555  if (!sd_acmd51()) {
1556  return false;
1557  }
1558  }
1559  if (IS_SDIO()) {
1560  if (!sdio_get_max_speed()) {
1561  return false;
1562  }
1563  }
1564  if ((4 <= driver_get_bus_width(sd_mmc_slot_sel))) {
1565  // TRY to enable 4-bit mode
1566  if (IS_SDIO()) {
1567  if (!sdio_cmd52_set_bus_width()) {
1568  return false;
1569  }
1570  }
1571  if (sd_mmc_card->type & CARD_TYPE_SD) {
1572  if (!sd_acmd6()) {
1573  return false;
1574  }
1575  }
1576  // Switch to selected bus mode
1578  }
1580  // TRY to enable High-Speed Mode
1581  if (IS_SDIO()) {
1582  if (!sdio_cmd52_set_high_speed()) {
1583  return false;
1584  }
1585  }
1586  if (sd_mmc_card->type & CARD_TYPE_SD) {
1587  if (sd_mmc_card->version > CARD_VER_SD_1_0) {
1588  if (!sd_cm6_set_high_speed()) {
1589  return false;
1590  }
1591  }
1592  }
1593  // Valid new configuration
1595  }
1596  // SD MEMORY, Set default block size
1597  if (sd_mmc_card->type & CARD_TYPE_SD) {
1599  return false;
1600  }
1601  }
1602  return true;
1603 }
1604 
1615 static bool sd_mmc_spi_install_mmc(void)
1616 {
1617  uint8_t b_authorize_high_speed;
1618 
1619  // CMD0 - Reset all cards to idle state.
1621  return false;
1622  }
1623 
1624  if (!mmc_spi_op_cond()) {
1625  return false;
1626  }
1627 
1628  // Disable CRC check for SPI mode
1630  return false;
1631  }
1632  // Get the Card-Specific Data
1633  if (!sd_mmc_cmd9_spi()) {
1634  return false;
1635  }
1636  mmc_decode_csd();
1637  // For MMC 4.0 Higher version
1638  if (sd_mmc_card->version >= CARD_VER_MMC_4) {
1639  // Get EXT_CSD
1640  if (!mmc_cmd8(&b_authorize_high_speed)) {
1641  return false;
1642  }
1643  }
1644  // Set default block size
1646  return false;
1647  }
1648  // Check communication
1649  if (!sd_mmc_cmd13()) {
1650  return false;
1651  }
1652  // Reinitialize the slot with the new speed
1654  return true;
1655 }
1656 
1657 
1668 static bool sd_mmc_mci_install_mmc(void)
1669 {
1670  uint8_t b_authorize_high_speed;
1671 
1672  // CMD0 - Reset all cards to idle state.
1674  return false;
1675  }
1676 
1677  if (!mmc_mci_op_cond()) {
1678  return false;
1679  }
1680 
1681  // Put the Card in Identify Mode
1682  // Note: The CID is not used in this stack
1684  return false;
1685  }
1686  // Assign relative address to the card.
1687  sd_mmc_card->rca = 1;
1689  (uint32_t)sd_mmc_card->rca << 16)) {
1690  return false;
1691  }
1692  // Get the Card-Specific Data
1693  if (!sd_mmc_cmd9_mci()) {
1694  return false;
1695  }
1696  mmc_decode_csd();
1697  // Select the and put it into Transfer Mode
1699  (uint32_t)sd_mmc_card->rca << 16)) {
1700  return false;
1701  }
1702  if (sd_mmc_card->version >= CARD_VER_MMC_4) {
1703  // For MMC 4.0 Higher version
1704  // Get EXT_CSD
1705  if (!mmc_cmd8(&b_authorize_high_speed)) {
1706  return false;
1707  }
1709  // Enable more bus width
1711  return false;
1712  }
1713  // Reinitialize the slot with the bus width
1715  }
1716  if (driver_is_high_speed_capable() && b_authorize_high_speed) {
1717  // Enable HS
1718  if (!mmc_cmd6_set_high_speed()) {
1719  return false;
1720  }
1721  // Reinitialize the slot with the new speed
1723  }
1724  } else {
1725  // Reinitialize the slot with the new speed
1727  }
1728 
1729  uint8_t retry = 10;
1730  while (retry--) {
1731  // Retry is a WORKAROUND for no compliance card (Atmel Internal ref. MMC19):
1732  // These cards seem not ready immediatly
1733  // after the end of busy of mmc_cmd6_set_high_speed()
1734 
1735  // Set default block size
1737  return true;
1738  }
1739  }
1740  return false;
1741 }
1742 
1743 //-------------------------------------------------------------------
1744 //--------------------- PUBLIC FUNCTIONS ----------------------------
1745 
1746 void sd_mmc_init(void)
1747 {
1749 #if (defined SD_MMC_0_CD_GPIO) && (SAM) && (!SAM4L)
1750 # include "pmc.h"
1751 # define SD_MMC_ENABLE_CD_PIN(slot, unused) \
1752  pmc_enable_periph_clk(SD_MMC_##slot##_CD_PIO_ID);
1753  MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_CD_PIN, ~)
1754 # undef SD_MMC_ENABLE_CD_PIN
1755 #endif
1756 #if (defined SD_MMC_0_WP_GPIO) && (SAM) && (!SAM4L)
1758 # include "pmc.h"
1759 # define SD_MMC_ENABLE_WP_PIN(slot, unused) \
1760  pmc_enable_periph_clk(SD_MMC_##slot##_WP_PIO_ID);
1761  MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_WP_PIN, ~)
1762 # undef SD_MMC_ENABLE_WP_PIN
1763 #endif
1764  for (uint8_t slot = 0; slot < SD_MMC_MEM_CNT; slot++) {
1766  }
1767  sd_mmc_slot_sel = 0xFF; // No slot configurated
1768  driver_init();
1769 }
1770 
1771 uint8_t sd_mmc_nb_slot(void)
1772 {
1773  return SD_MMC_MEM_CNT;
1774 }
1775 
1777 {
1778  sd_mmc_err_t sd_mmc_err;
1779 
1780  sd_mmc_err = sd_mmc_select_slot(slot);
1781  if (sd_mmc_err != SD_MMC_INIT_ONGOING) {
1783  return sd_mmc_err;
1784  }
1785 
1786  // Initialization of the card requested
1787  if (sd_mmc_is_spi()? sd_mmc_spi_card_init()
1788  : sd_mmc_mci_card_init()) {
1789  sd_mmc_debug("SD/MMC card ready\n\r");
1790  sd_mmc_card->state = SD_MMC_CARD_STATE_READY;
1792  // To notify that the card has been just initialized
1793  // It is necessary for USB Device MSC
1794  return SD_MMC_INIT_ONGOING;
1795  }
1796  sd_mmc_debug("SD/MMC card initialization failed\n\r");
1797  sd_mmc_card->state = SD_MMC_CARD_STATE_UNUSABLE;
1799  return SD_MMC_ERR_UNUSABLE;
1800 }
1801 
1803 {
1804  if (SD_MMC_OK != sd_mmc_select_slot(slot)) {
1805  return CARD_TYPE_UNKNOWN;
1806  }
1808  return sd_mmc_card->type;
1809 }
1810 
1812 {
1813  if (SD_MMC_OK != sd_mmc_select_slot(slot)) {
1814  return CARD_VER_UNKNOWN;
1815  }
1817  return sd_mmc_card->version;
1818 }
1819 
1820 uint32_t sd_mmc_get_capacity(uint8_t slot)
1821 {
1822  if (SD_MMC_OK != sd_mmc_select_slot(slot)) {
1823  return 0;
1824  }
1826  return sd_mmc_card->capacity;
1827 }
1828 
1829 bool sd_mmc_is_write_protected(uint8_t slot)
1830 {
1831  UNUSED(slot);
1832 #if (defined SD_MMC_0_WP_GPIO)
1833  if (ioport_get_pin_level(sd_mmc_cards[slot].wp_gpio)
1835  == SD_MMC_0_WP_DETECT_VALUE) {
1836  return true;
1837  }
1838 #endif
1839  return false;
1840 }
1841 
1842 sd_mmc_err_t sd_mmc_init_read_blocks(uint8_t slot, uint32_t start,
1843  uint16_t nb_block)
1844 {
1845  sd_mmc_err_t sd_mmc_err;
1846  uint32_t cmd, arg, resp;
1847 
1848  sd_mmc_err = sd_mmc_select_slot(slot);
1849  if (sd_mmc_err != SD_MMC_OK) {
1850  return sd_mmc_err;
1851  }
1852 
1853  // Wait for data ready status
1854  if (!sd_mmc_cmd13()) {
1856  return SD_MMC_ERR_COMM;
1857  }
1858 
1859  if (nb_block > 1) {
1861  } else {
1863  }
1864  /*
1865  * SDSC Card (CCS=0) uses byte unit address,
1866  * SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit).
1867  */
1868  if (sd_mmc_card->type & CARD_TYPE_HC) {
1869  arg = start;
1870  } else {
1871  arg = (start * SD_MMC_BLOCK_SIZE);
1872  }
1873 
1874  if (!driver_adtc_start(cmd, arg, SD_MMC_BLOCK_SIZE, nb_block, true)) {
1876  return SD_MMC_ERR_COMM;
1877  }
1878  // Check response
1879  if (sd_mmc_is_mci()) {
1880  resp = driver_get_response();
1881  if (resp & CARD_STATUS_ERR_RD_WR) {
1882  sd_mmc_debug("%s: Read blocks %02d resp32 0x%08x CARD_STATUS_ERR_RD_WR\n\r",
1883  __func__, (int)SDMMC_CMD_GET_INDEX(cmd), resp);
1885  return SD_MMC_ERR_COMM;
1886  }
1887  }
1888  sd_mmc_nb_block_remaining = nb_block;
1889  sd_mmc_nb_block_to_tranfer = nb_block;
1890  return SD_MMC_OK;
1891 }
1892 
1893 sd_mmc_err_t sd_mmc_start_read_blocks(void *dest, uint16_t nb_block)
1894 {
1895  Assert(sd_mmc_nb_block_remaining >= nb_block);
1896 
1897  if (!driver_start_read_blocks(dest, nb_block)) {
1899  return SD_MMC_ERR_COMM;
1900  }
1901  sd_mmc_nb_block_remaining -= nb_block;
1902  return SD_MMC_OK;
1903 }
1904 
1906 {
1908  return SD_MMC_ERR_COMM;
1909  }
1910  if (abort) {
1912  } else if (sd_mmc_nb_block_remaining) {
1913  return SD_MMC_OK;
1914  }
1915 
1916  // All blocks are transfered then stop read operation
1917  if (sd_mmc_nb_block_to_tranfer == 1) {
1918  // Single block transfer, then nothing to do
1920  return SD_MMC_OK;
1921  }
1922  // WORKAROUND for no compliance card (Atmel Internal ref. !MMC7 !SD19):
1923  // The errors on this command must be ignored
1924  // and one retry can be necessary in SPI mode for no compliance card.
1927  }
1929  return SD_MMC_OK;
1930 }
1931 
1932 sd_mmc_err_t sd_mmc_init_write_blocks(uint8_t slot, uint32_t start,
1933  uint16_t nb_block)
1934 {
1935  sd_mmc_err_t sd_mmc_err;
1936  uint32_t cmd, arg, resp;
1937 
1938  sd_mmc_err = sd_mmc_select_slot(slot);
1939  if (sd_mmc_err != SD_MMC_OK) {
1940  return sd_mmc_err;
1941  }
1942  if (sd_mmc_is_write_protected(slot)) {
1944  return SD_MMC_ERR_WP;
1945  }
1946 
1947  if (nb_block > 1) {
1949  } else {
1951  }
1952  /*
1953  * SDSC Card (CCS=0) uses byte unit address,
1954  * SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit).
1955  */
1956  if (sd_mmc_card->type & CARD_TYPE_HC) {
1957  arg = start;
1958  } else {
1959  arg = (start * SD_MMC_BLOCK_SIZE);
1960  }
1961  if (!driver_adtc_start(cmd, arg, SD_MMC_BLOCK_SIZE, nb_block, true)) {
1963  return SD_MMC_ERR_COMM;
1964  }
1965  // Check response
1966  if (sd_mmc_is_mci()) {
1967  resp = driver_get_response();
1968  if (resp & CARD_STATUS_ERR_RD_WR) {
1969  sd_mmc_debug("%s: Write blocks %02d r1 0x%08x CARD_STATUS_ERR_RD_WR\n\r",
1970  __func__, (int)SDMMC_CMD_GET_INDEX(cmd), resp);
1972  return SD_MMC_ERR_COMM;
1973  }
1974  }
1975  sd_mmc_nb_block_remaining = nb_block;
1976  sd_mmc_nb_block_to_tranfer = nb_block;
1977  return SD_MMC_OK;
1978 }
1979 
1980 sd_mmc_err_t sd_mmc_start_write_blocks(const void *src, uint16_t nb_block)
1981 {
1982  Assert(sd_mmc_nb_block_remaining >= nb_block);
1983  if (!driver_start_write_blocks(src, nb_block)) {
1985  return SD_MMC_ERR_COMM;
1986  }
1987  sd_mmc_nb_block_remaining -= nb_block;
1988  return SD_MMC_OK;
1989 }
1990 
1992 {
1994  return SD_MMC_ERR_COMM;
1995  }
1996  if (abort) {
1998  } else if (sd_mmc_nb_block_remaining) {
1999  return SD_MMC_OK;
2000  }
2001 
2002  // All blocks are transfered then stop write operation
2003  if (sd_mmc_nb_block_to_tranfer == 1) {
2004  // Single block transfer, then nothing to do
2006  return SD_MMC_OK;
2007  }
2008 
2009  if (sd_mmc_is_mci()) {
2010  // Note: SPI multiblock writes terminate using a special
2011  // token, not a STOP_TRANSMISSION request.
2014  return SD_MMC_ERR_COMM;
2015  }
2016  }
2018  return SD_MMC_OK;
2019 }
2020 
2021 #ifdef SDIO_SUPPORT_ENABLE
2022 sd_mmc_err_t sdio_read_direct(uint8_t slot, uint8_t func_num, uint32_t addr,
2023  uint8_t *dest)
2024 {
2025  sd_mmc_err_t sd_mmc_err;
2026 
2027  if (dest == NULL) {
2028  return SD_MMC_ERR_PARAM;
2029  }
2030 
2031  sd_mmc_err = sd_mmc_select_slot(slot);
2032  if (sd_mmc_err != SD_MMC_OK) {
2033  return sd_mmc_err;
2034  }
2035 
2036  if (!sdio_cmd52(SDIO_CMD52_READ_FLAG, func_num, addr, 0, dest)) {
2038  return SD_MMC_ERR_COMM;
2039  }
2041  return SD_MMC_OK;
2042 }
2043 
2044 sd_mmc_err_t sdio_write_direct(uint8_t slot, uint8_t func_num, uint32_t addr,
2045  uint8_t data)
2046 {
2047  sd_mmc_err_t sd_mmc_err;
2048 
2049  sd_mmc_err = sd_mmc_select_slot(slot);
2050  if (sd_mmc_err != SD_MMC_OK) {
2051  return sd_mmc_err;
2052  }
2053 
2054  if (!sdio_cmd52(SDIO_CMD52_WRITE_FLAG, func_num, addr, 0, &data)) {
2056  return SD_MMC_ERR_COMM;
2057  }
2058 
2060  return SD_MMC_OK;
2061 }
2062 
2063 sd_mmc_err_t sdio_read_extended(uint8_t slot, uint8_t func_num, uint32_t addr,
2064  uint8_t inc_addr, uint8_t *dest, uint16_t size)
2065 {
2066  sd_mmc_err_t sd_mmc_err;
2067 
2068  if ((size == 0) || (size > 512)) {
2069  return SD_MMC_ERR_PARAM;
2070  }
2071 
2072  sd_mmc_err = sd_mmc_select_slot(slot);
2073  if (sd_mmc_err != SD_MMC_OK) {
2074  return sd_mmc_err;
2075  }
2076 
2077  if (!sdio_cmd53(SDIO_CMD53_READ_FLAG, func_num, addr, inc_addr,
2078  size, true)) {
2080  return SD_MMC_ERR_COMM;
2081  }
2082  if (!driver_start_read_blocks(dest, 1)) {
2084  return SD_MMC_ERR_COMM;
2085  }
2088  return SD_MMC_ERR_COMM;
2089  }
2090 
2092  return SD_MMC_OK;
2093 }
2094 
2095 sd_mmc_err_t sdio_write_extended(uint8_t slot, uint8_t func_num, uint32_t addr,
2096  uint8_t inc_addr, uint8_t *src, uint16_t size)
2097 {
2098  sd_mmc_err_t sd_mmc_err;
2099 
2100  if ((size == 0) || (size > 512)) {
2101  return SD_MMC_ERR_PARAM;
2102  }
2103 
2104  sd_mmc_err = sd_mmc_select_slot(slot);
2105  if (sd_mmc_err != SD_MMC_OK) {
2106  return sd_mmc_err;
2107  }
2108 
2109  if (!sdio_cmd53(SDIO_CMD53_WRITE_FLAG, func_num, addr, inc_addr,
2110  size, true)) {
2112  return SD_MMC_ERR_COMM;
2113  }
2114  if (!driver_start_write_blocks(src, 1)) {
2116  return SD_MMC_ERR_COMM;
2117  }
2120  return SD_MMC_ERR_COMM;
2121  }
2122 
2124  return SD_MMC_OK;
2125 }
2126 #endif // SDIO_SUPPORT_ENABLE
2127 
#define pdTRUE
Definition: projdefs.h:46
#define SD_MMC_ERR_UNUSABLE
Definition: sd_mmc.h:69
#define SDMMC_CMD_GET_INDEX(cmd)
#define SDIO_CMD53_BLOCK_MODE
CMD53 arg[ 27] (Optional) 1:block mode.
#define SDMMC_SPI_CMD13_SEND_STATUS
#define SD_CMD6_GRP5_NO_INFLUENCE
CMD6 arg[19:16] function group 5, 0xF or 0x0.
#define MMC_MCI_CMD1_SEND_OP_COND
#define MMC_CMD6_INDEX_HS_TIMING
static sd_mmc_err_t sd_mmc_select_slot(uint8_t slot)
Select a card slot and initialize the associated driver.
Definition: sd_mmc.c:1307
#define SDIO_CCCR_HS
#define driver_send_clock
Definition: sd_mmc.c:111
#define CARD_VER_SD_1_10
Definition: sd_mmc.h:95
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
static bool sd_cmd8(uint8_t *v2)
CMD8 for SD card - Send Interface Condition Command.
Definition: sd_mmc.c:894
#define SDIO_SHS
static uint16_t sd_mmc_nb_block_remaining
Number of block remaining to read or write on the current transfer.
Definition: sd_mmc.c:200
static bool sd_mci_op_cond(uint8_t v2)
Ask to all cards to send their operations conditions (MCI only).
Definition: sd_mmc.c:486
SD/MMC protocol definitions.
#define SD_CSD_1_0_C_SIZE_MULT(csd)
#define driver_get_response
Definition: sd_mmc.c:113
#define SD_ACMD51_SEND_SCR
sd_mmc_err_t sd_mmc_init_write_blocks(uint8_t slot, uint32_t start, uint16_t nb_block)
Initialize the write blocks of data.
Definition: sd_mmc.c:1932
#define SDMMC_SPI_CMD0_GO_IDLE_STATE
#define SD_CMD8_MASK_PATTERN
#define driver_start_read_blocks
Definition: sd_mmc.c:119
#define portTICK_RATE_MS
Definition: FreeRTOS.h:913
#define driver_get_response_128
Definition: sd_mmc.c:114
#define OCR_SDIO_MP
#define SDMMC_CMD17_READ_SINGLE_BLOCK
static bool mmc_mci_op_cond(void)
Sends operation condition command and read OCR (MCI only)
Definition: sd_mmc.c:382
static uint32_t sysclk_get_cpu_hz(void)
Return the current rate in Hz of the CPU clock.
#define driver_is_high_speed_capable
Definition: sd_mmc.c:110
static bool sd_acmd51(void)
ACMD51 - Read the SD Configuration Register.
Definition: sd_mmc.c:1251
#define MMC_CSD_C_SIZE(csd)
#define SDIO_CAP_4BLS
string cmd
static bool sd_mmc_cmd13(void)
CMD13 - Addressed card sends its status register. This function waits the clear of the busy flag...
Definition: sd_mmc.c:1110
#define SD_SPI_ACMD41_SD_SEND_OP_COND
#define OCR_ACCESS_MODE_MASK
#define SD_SW_STATUS_FUN_GRP1_BUSY(status)
#define SDMMC_SPI_CMD59_CRC_ON_OFF
#define SDIO_CCCR_CIS_PTR
card_state
SD/MMC card states.
Definition: sd_mmc.c:145
#define SD_MMC_ERR_NO_CARD
Definition: sd_mmc.h:68
static bool sd_mmc_spi_card_init(void)
Initialize the SD card in SPI mode.
Definition: sd_mmc.c:1396
static bool sd_cm6_set_high_speed(void)
CMD6 for SD - Switch card in high speed mode.
Definition: sd_mmc.c:767
Ready to use.
Definition: sd_mmc.c:146
#define SDIO_CMD53_RW_FLAG
CMD53 arg[ 31] Direction, 1:WR, 0:RD.
#define CARD_VER_MMC_2_2
Definition: sd_mmc.h:100
#define SDIO_CMD52_RAW_FLAG
CMD52 arg[ 27] Read after Write flag.
#define SD_CMD8_SEND_IF_COND
uint8_t high_speed
High speed card (1)
Definition: sd_mmc.c:169
if(udd_ctrl_interrupt())
Definition: usbhs_device.c:688
#define EXT_CSD_CARD_TYPE_INDEX
#define SDMMC_MCI_CMD0_GO_IDLE_STATE
#define SD_CMD6_MODE_SWITCH
static struct sd_mmc_card * sd_mmc_card
Pointer on current slot configurated.
Definition: sd_mmc.c:196
#define SD_ACMD6_SET_BUS_WIDTH
#define SDMMC_CMD55_APP_CMD
#define driver_select_device
Definition: sd_mmc.c:107
uint8_t card_version_t
Type of card version.
Definition: sd_mmc.h:89
#define CARD_TYPE_HC
Definition: sd_mmc.h:84
#define CARD_TYPE_SDIO
SDIO card.
Definition: sd_mmc.h:83
#define SD_MCI_ACMD41_SD_SEND_OP_COND
static bool sd_mmc_mci_install_mmc(void)
Initialize the MMC card in MCI mode.
Definition: sd_mmc.c:1668
sd_mmc_err_t sd_mmc_wait_end_of_write_blocks(bool abort)
Wait the end of write blocks of data.
Definition: sd_mmc.c:1991
uint8_t bus_width
Number of DATA lin on bus (MCI only)
Definition: sd_mmc.c:167
sd_mmc_err_t sd_mmc_check(uint8_t slot)
Performs a card checks.
Definition: sd_mmc.c:1776
#define SD_MMC_BLOCK_SIZE
This SD MMC stack uses the maximum block size autorized (512 bytes)
Definition: sd_mmc.h:106
Common IOPORT service main header file for AVR, UC3 and ARM architectures.
#define CSD_REG_BSIZE
16 bytes
#define NULL
Definition: nm_bsp.h:52
#define SDMMC_CMD25_WRITE_MULTIPLE_BLOCK
static bool mmc_spi_op_cond(void)
Sends operation condition command and read OCR (SPI only)
Definition: sd_mmc.c:335
#define driver_read_word
Definition: sd_mmc.c:117
#define SD_MMC_ERR_COMM
Definition: sd_mmc.h:71
#define SD_MMC_DEBOUNCE_TIMEOUT
Definition: sd_mmc.c:259
#define MMC_CSD_C_SIZE_MULT(csd)
#define MMC_CMD6_INDEX_BUS_WIDTH
#define SD_SCR_SD_SPEC_1_10
#define SD_SW_STATUS_FUN_GRP_RC_ERROR
#define SDIO_CMD53_IO_W_BYTE_EXTENDED
#define driver_adtc_stop
Definition: sd_mmc.c:116
enum card_state state
Card state.
Definition: sd_mmc.c:164
#define SDIO_CMD52_RW_FLAG
CMD52 arg[ 31] Direction, 1:write, 0:read.
#define SD_CMD8_HIGH_VOLTAGE
#define SDIO_CIA
#define SD_CMD6_GRP4_NO_INFLUENCE
CMD6 arg[15:12] function group 4, 0xF or 0x0.
#define CARD_VER_SD_3_0
Definition: sd_mmc.h:97
No SD/MMC card inserted.
Definition: sd_mmc.c:150
#define xTimeOutType
Definition: FreeRTOS.h:906
#define MMC_CSD_READ_BL_LEN(csd)
#define sd_mmc_is_mci()
Definition: sd_mmc.c:136
static void sd_decode_csd(void)
Decodes SD CSD register.
Definition: sd_mmc.c:1066
#define SD_SCR_SD_SPEC(scr)
static bool sd_acmd6(void)
ACMD6 - Define the data bus width to 4 bits bus.
Definition: sd_mmc.c:1224
#define SDIO_CISTPL_END
Unusable card.
Definition: sd_mmc.c:149
#define MMC_CMD6_SWITCH
static bool ioport_get_pin_level(ioport_pin_t pin)
Get current value of an IOPORT pin, which has been configured as an input.
Definition: ioport.h:301
#define SDMMC_SPI_CMD9_SEND_CSD
#define CARD_STATUS_READY_FOR_DATA
Initialization on going.
Definition: sd_mmc.c:148
sd_mmc_err_t sd_mmc_start_read_blocks(void *dest, uint16_t nb_block)
Start the read blocks of data from the card.
Definition: sd_mmc.c:1893
const uint32_t mmc_trans_multipliers[16]
MMC transfer multiplier factor codes (1/10) list.
Definition: sd_mmc.c:211
#define SDIO_CMD53_OP_CODE
CMD53 arg[ 26] 1:Incrementing address, 0: fixed.
#define CARD_VER_SD_1_0
Definition: sd_mmc.h:94
#define MMC_CMD6_VALUE_HS_TIMING_ENABLE
#define SDMMC_CMD24_WRITE_BLOCK
#define SDIO_CMD52_FUNCTION_NUM
CMD52 arg[30:28] Number of the function.
#define CARD_VER_MMC_3
Definition: sd_mmc.h:101
#define driver_init
Definition: sd_mmc.c:106
#define MREPEAT(count, macro, data)
Macro repeat.
Definition: mrepeat.h:65
#define driver_wait_end_of_read_blocks
Definition: sd_mmc.c:120
#define SDMMC_CMD12_STOP_TRANSMISSION
#define SD_MMC_ERR_PARAM
Definition: sd_mmc.h:72
#define SDIO_EHS
#define CSD_TRAN_SPEED(csd)
static bool sdio_cmd52_set_high_speed(void)
Definition: sd_mmc.c:752
#define SDMMC_CMD7_SELECT_CARD_CMD
#define SD_SW_STATUS_BSIZE
64 bytes
#define SD_SCR_SD_SPEC_2_00
static uint16_t sd_mmc_nb_block_to_tranfer
Number of block to read or write on the current transfer.
Definition: sd_mmc.c:198
#define SDMMC_CMD2_ALL_SEND_CID
#define SDIO_CMD52_WR_DATA
CMD52 arg[ 7: 0] Write data or stuff bits.
card_type_t sd_mmc_get_type(uint8_t slot)
Get the card type.
Definition: sd_mmc.c:1802
#define CARD_STATUS_ERR_RD_WR
#define driver_start_write_blocks
Definition: sd_mmc.c:121
static bool mmc_cmd6_set_high_speed(void)
CMD6 for MMC - Switches in high speed mode.
Definition: sd_mmc.c:863
#define SDIO_CCCR_CAP
#define CSD_STRUCTURE_VERSION(csd)
#define SD_MMC_ERR_SLOT
Definition: sd_mmc.h:70
#define SDIO_CMD53_FUNCTION_NUM
CMD53 arg[30:28] Number of the function.
static bool sdio_get_max_speed(void)
Definition: sd_mmc.c:744
#define MMC_CSD_SPEC_VERS(csd)
#define SysTick
Definition: core_cm7.h:1600
static bool sdio_cmd52_set_bus_width(void)
Definition: sd_mmc.c:748
#define driver_wait_end_of_write_blocks
Definition: sd_mmc.c:122
static void sd_mmc_deselect_slot(void)
Deselect the current card slot.
Definition: sd_mmc.c:1379
#define sd_mmc_debug(...)
Definition: sd_mmc.c:64
#define SDMMC_CMD16_SET_BLOCKLEN
Board configuration.
#define MMC_SPI_CMD1_SEND_OP_COND
#define OCR_CCS
#define MMC_CMD8_SEND_EXT_CSD
#define SD_SCR_SD_SPEC_1_0_01
static bool sd_mmc_mci_card_init(void)
Initialize the SD card in MCI mode.
Definition: sd_mmc.c:1485
#define CARD_TYPE_MMC
MMC card.
Definition: sd_mmc.h:82
#define MMC_CMD6_VALUE_BUS_WIDTH_1BIT
[15:8] Value
uint32_t capacity
Card capacity in KBytes.
Definition: sd_mmc.c:156
#define CARD_VER_MMC_1_2
Definition: sd_mmc.h:98
#define SDIO_CISTPL_FUNCE
#define SD_MMC_0_CD_GPIO
Definition: user_board.h:395
#define SDIO_CMD53_READ_FLAG
card_type_t type
Card type.
Definition: sd_mmc.c:165
#define SD_SCR_REG_BSIZE
8 bytes
#define SDIO_CMD53_COUNT
#define SysTick_CTRL_COUNTFLAG_Msk
Definition: core_cm7.h:843
const uint32_t sd_trans_multipliers[16]
SD transfer multiplier factor codes (1/10) list.
Definition: sd_mmc.c:207
Common Delay Service.
#define SDIO_CMD52_IO_RW_DIRECT
uint16_t rca
Relative card address.
Definition: sd_mmc.c:163
SD/MMC card information structure.
Definition: sd_mmc.c:154
USBInterfaceDescriptor data
#define portTickType
Definition: FreeRTOS.h:900
#define CARD_TYPE_SD
SD card.
Definition: sd_mmc.h:81
const uint32_t sd_mmc_trans_units[7]
SD/MMC transfer rate unit codes (10K) list.
Definition: sd_mmc.c:203
#define MMC_CTYPE_52MHZ
#define SDIO_CMD5_SEND_OP_COND
#define driver_deselect_device
Definition: sd_mmc.c:108
#define OCR_SDIO_NF
#define SD_CMD6_GRP6_NO_INFLUENCE
CMD6 arg[23:20] function group 6, 0xF or 0x0.
#define CARD_VER_UNKNOWN
Definition: sd_mmc.h:93
#define MMC_CMD6_VALUE_BUS_WIDTH_4BIT
#define SD_CSD_1_0_READ_BL_LEN(csd)
#define EXT_CSD_BSIZE
SD/MMC stack configuration file.
#define SysTick_CTRL_ENABLE_Msk
Definition: core_cm7.h:852
#define CARD_VER_MMC_4
Definition: sd_mmc.h:102
uint8_t sd_mmc_nb_slot(void)
Return the number of slot available.
Definition: sd_mmc.c:1771
#define IS_SDIO()
Definition: sd_mmc.c:133
#define SDIO_CMD53_WRITE_FLAG
#define SD_CMD6_GRP1_HIGH_SPEED
CMD6 arg[ 3: 0] function group 1, access mode.
sd_mmc_err_t sd_mmc_init_read_blocks(uint8_t slot, uint32_t start, uint16_t nb_block)
Initialize the read blocks of data from the card.
Definition: sd_mmc.c:1842
Common SD/MMC stack header file.
#define CARD_TYPE_UNKNOWN
Unknown type card.
Definition: sd_mmc.h:80
void vTaskSetTimeOutState(TimeOut_t *const pxTimeOut) PRIVILEGED_FUNCTION
Definition: tasks.c:3088
#define SDMMC_SPI_CMD58_READ_OCR
#define SD_CMD8_PATTERN
#define SD_SCR_SD_SPEC_3_00
#define SD_MMC_VOLTAGE_SUPPORT
This SD MMC stack supports only the high voltage.
Definition: sd_mmc.c:139
static bool sd_spi_op_cond(uint8_t v2)
Ask to all cards to send their operations conditions (SPI only).
Definition: sd_mmc.c:426
#define SDIO_BUSWIDTH_4B
static void sd_mmc_configure_slot(void)
Configures the driver with the selected card configuration.
Definition: sd_mmc.c:1370
#define SDIO_CMD52_READ_FLAG
#define EXT_CSD_SEC_COUNT_INDEX
#define OCR_ACCESS_MODE_SECTOR
Debounce on going.
Definition: sd_mmc.c:147
uint8_t csd[CSD_REG_BSIZE]
CSD register.
Definition: sd_mmc.c:168
static bool sd_mmc_cmd9_spi(void)
CMD9: Addressed card sends its card-specific data (CSD) on the CMD line spi.
Definition: sd_mmc.c:976
uint8_t card_type_t
Type of card type.
Definition: sd_mmc.h:76
#define delay_ms(delay)
Delay in milliseconds.
Definition: delay.h:112
#define SD_MMC_OK
Definition: sd_mmc.h:66
#define SD_SW_STATUS_FUN_GRP1_RC(status)
sd_mmc_err_t sd_mmc_start_write_blocks(const void *src, uint16_t nb_block)
Start the write blocks of data.
Definition: sd_mmc.c:1980
static struct sd_mmc_card sd_mmc_cards[SD_MMC_MEM_CNT]
Definition: sd_mmc.c:174
#define SD_CMD6_GRP2_DEFAULT
#define SDIO_CMD53_REG_ADDR
CMD53 arg[25: 9] Start Address I/O register.
card_version_t sd_mmc_get_version(uint8_t slot)
Get the card version.
Definition: sd_mmc.c:1811
#define SD_MMC_ERR_WP
Definition: sd_mmc.h:73
uint32_t sd_mmc_get_capacity(uint8_t slot)
Get the memory capacity.
Definition: sd_mmc.c:1820
#define R1_SPI_IDLE
#define SDMMC_CMD18_READ_MULTIPLE_BLOCK
#define SD_CMD6_SWITCH_FUNC
BaseType_t xTaskCheckForTimeOut(TimeOut_t *const pxTimeOut, TickType_t *const pxTicksToWait) PRIVILEGED_FUNCTION
Definition: tasks.c:3108
#define OCR_POWER_UP_BUSY
#define SD_CSD_VER_2_0
#define CARD_TYPE_SD_COMBO
SD combo card (io + memory)
Definition: sd_mmc.h:86
static bool mmc_cmd8(uint8_t *b_authorize_high_speed)
CMD8 - The card sends its EXT_CSD register as a block of data.
Definition: sd_mmc.c:929
#define SDIO_CMD52_REG_ADRR
CMD52 arg[25: 9] Register address.
static bool sd_mmc_spi_install_mmc(void)
Initialize the MMC card in SPI mode.
Definition: sd_mmc.c:1615
#define SDIO_CMD53_IO_R_BYTE_EXTENDED
#define driver_get_bus_width
Definition: sd_mmc.c:109
static bool mmc_cmd6_set_bus_width(uint8_t bus_width)
CMD6 for MMC - Switches the bus width mode.
Definition: sd_mmc.c:820
#define SD_CMD6_GRP3_NO_INFLUENCE
CMD6 arg[11: 8] function group 3, 0xF or 0x0.
#define driver_send_cmd
Definition: sd_mmc.c:112
#define SDIO_CMD52_WRITE_FLAG
static bool sdio_op_cond(void)
Definition: sd_mmc.c:740
#define SDIO_CCCR_IOA
static void mmc_decode_csd(void)
Decodes MMC CSD register.
Definition: sd_mmc.c:1006
uint32_t clock
Card access clock.
Definition: sd_mmc.c:155
card_version_t version
Card version.
Definition: sd_mmc.c:166
static bool sd_mmc_cmd9_mci(void)
CMD9: Addressed card sends its card-specific data (CSD) on the CMD line mci.
Definition: sd_mmc.c:994
#define MMC_CMD3_SET_RELATIVE_ADDR
#define SD_ACMD41_HCS
#define SD_CSD_1_0_C_SIZE(csd)
#define SDMMC_MCI_CMD9_SEND_CSD
#define MMC_CMD6_ACCESS_WRITE_BYTE
sd_mmc_err_t sd_mmc_wait_end_of_read_blocks(bool abort)
Wait the end of read blocks of data from the card.
Definition: sd_mmc.c:1905
#define Assert(expr)
This macro is used to test fatal errors.
Definition: compiler.h:196
Standard board header file.
#define SDIO_CCCR_BUS_CTRL
#define SDMMC_CLOCK_INIT
#define MMC_CMD6_VALUE_BUS_WIDTH_8BIT
#define SD_CMD8_MASK_VOLTAGE
static uint8_t sd_mmc_slot_sel
Index of current slot configurated.
Definition: sd_mmc.c:194
void sd_mmc_init(void)
Initialize the SD/MMC stack and low level driver required.
Definition: sd_mmc.c:1746
#define SDMMC_MCI_CMD13_SEND_STATUS
#define SD_SCR_SD_SPEC3(scr)
#define SD_MMC_0_CD_DETECT_VALUE
Definition: user_board.h:398
#define SD_CMD3_SEND_RELATIVE_ADDR
#define CARD_STATUS_SWITCH_ERROR
uint8_t sd_mmc_err_t
Type of return error code.
Definition: sd_mmc.h:62
#define CARD_VER_MMC_1_4
Definition: sd_mmc.h:99
#define driver_adtc_start
Definition: sd_mmc.c:115
#define MMC_CMD6_ACCESS_SET_BITS
#define SD_CSD_2_0_C_SIZE(csd)
#define SD_MMC_INIT_ONGOING
Definition: sd_mmc.h:67
bool sd_mmc_is_write_protected(uint8_t slot)
Get the card write protection status.
Definition: sd_mmc.c:1829
#define CARD_VER_SD_2_0
Definition: sd_mmc.h:96


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05