51 static char in_use = 0;
54 static uint16_t sd_mmc_nb_block_to_tranfer = 0;
56 static uint16_t sd_mmc_nb_block_remaining = 0;
64 const uint32_t
sd_trans_multipliers[16] = { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
79 printf(
"SD card not v2\n\r");
84 if (resp == 0xFFFFFFFF)
87 printf(
"SD card not v2\n\r");
110 uint32_t arg, retry, resp;
147 printf(
"Card is High Capacity\n\r");
172 clock = unit * mul * 1000;
190 printf(
"CSD version 2.0\n\r");
239 printf(
"SD spec. version 1.0\n\r");
244 printf(
"SD spec. version 1.1\n\r");
251 printf(
"SD spec. version 3.0\n\r");
256 printf(
"SD spec. version 2.0\n\r");
262 printf(
"SD spec. version 1.0\n\r");
290 HSMCI->HSMCI_SDCR = HSMCI_SDCR_SDCSEL_SLOTA | HSMCI_SDCR_SDCBUS_4;
291 printf(
"4-bit bus width enabled\n\r");
319 printf(
"adtc command failed\n\r");
324 printf(
"CMD6 start read blocks error\n\r");
329 printf(
"CMD6 wait end of read blocks error\n\r");
335 printf(
"%s: CMD6 CARD_STATUS_SWITCH_ERROR\n\r", __func__);
342 printf(
"SD_SW_STATUS_FUN_GRP_RC_ERROR\n\r");
346 printf(
"%s: CMD6 SD_SW_STATUS_FUN_GRP1_BUSY\n\r", __func__);
366 uint32_t nec_timeout;
373 nec_timeout = 200000;
385 if (nec_timeout-- == 0)
387 printf(
"%s: CMD13 Busy timeout\n\r", __func__);
408 if (nb_block >= maxsector)
410 printf(
"Block to read >= Maxsector\n\r");
416 printf(
"Device not initialized\n\r");
422 printf(
"CMD13 data ready error\n\r");
429 HSMCI->HSMCI_MR = HSMCI_MR_PDCMODE | HSMCI_MR_WRPROOF | HSMCI_MR_RDPROOF;
432 HSMCI->HSMCI_RPR = (uint32_t) dest;
435 HSMCI->HSMCI_PTCR = HSMCI_PTCR_RXTEN;
437 HSMCI->HSMCI_ARGR = s;
441 | HSMCI_CMDR_RSPTYP_48_BIT
442 | HSMCI_CMDR_TRTYP_SINGLE
443 | HSMCI_CMDR_TRDIR_READ
444 | HSMCI_CMDR_TRCMD_START_DATA
445 | HSMCI_CMDR_SPCMD_STD;
450 sr = HSMCI->HSMCI_SR;
453 if (sr & (HSMCI_SR_CSTOE | HSMCI_SR_RTOE | HSMCI_SR_RENDE | HSMCI_SR_RCRCE | HSMCI_SR_RDIRE | HSMCI_SR_RINDE))
456 printf(
"HSMCI CMDR CRC error \n\r");
460 }
while (!(sr & HSMCI_SR_CMDRDY));
466 printf(
"HSMCI CMDR wait busy error \n\r");
471 while(HSMCI_SR_ENDRX != (HSMCI->HSMCI_SR & HSMCI_SR_ENDRX));
488 if (nb_block >= maxsector)
490 printf(
"Block to write >= Maxsector\n\r");
496 printf(
"Device not initialized\n\r");
503 HSMCI->HSMCI_MR = HSMCI_MR_PDCMODE | HSMCI_MR_WRPROOF | HSMCI_MR_RDPROOF;
506 HSMCI->HSMCI_TPR = (uint32_t) src;
509 HSMCI->HSMCI_PTCR = HSMCI_PTCR_TXTEN;
511 HSMCI->HSMCI_ARGR = d;
515 | HSMCI_CMDR_RSPTYP_48_BIT
516 | HSMCI_CMDR_TRTYP_SINGLE
517 | HSMCI_CMDR_TRDIR_WRITE
518 | HSMCI_CMDR_TRCMD_START_DATA
519 | HSMCI_CMDR_SPCMD_STD;
524 sr = HSMCI->HSMCI_SR;
527 if (sr & (HSMCI_SR_CSTOE | HSMCI_SR_RTOE | HSMCI_SR_RENDE | HSMCI_SR_RCRCE | HSMCI_SR_RDIRE | HSMCI_SR_RINDE))
530 printf(
"HSMCI CMDR CRC error \n\r");
534 }
while (!(sr & HSMCI_SR_CMDRDY));
540 printf(
"HSMCI CMDR wait busy error \n\r");
545 while(HSMCI_SR_NOTBUSY != (HSMCI->HSMCI_SR & HSMCI_SR_NOTBUSY));
588 static void sd_release (
F_DRIVER * driver )
615 ( void ) driver_param;
622 s_driver.
getphy = sd_getphy;
634 printf(
"CMD0 failed\n\r");
642 printf(
"CMD8 failed\n\r");
650 printf(
"Not a valid SD card\n\r");
657 printf(
"CMD2 failed\n\r");
663 printf(
"CMD3 failed\n\r");
667 printf(
"Card relative address is : %d\n\r",
rca);
672 printf(
"CMD9 failed\n\r");
681 printf(
"Failed to execute CMD7\n\r");
689 printf(
"ACMD51 failed\n\r");
696 printf(
"High Speed not set\n\r");
704 printf(
"Card switched to HS mode\n\r");
711 printf(
"ACMD6 failed\n\r");
718 printf(
"CMD16 failed\n\r");
720 printf(
"Card relative address is : %d\n\r",
rca);
uint32_t sd_acmd51(void)
ACMD51 - Read the SD Configuration Register.
#define SD_CMD6_GRP5_NO_INFLUENCE
CMD6 arg[19:16] function group 5, 0xF or 0x0.
uint32_t hsmci_adtc_start(uint32_t cmd, uint32_t arg, uint16_t block_size, uint16_t nb_block, uint32_t access_block)
Send an ADTC command on the selected slot An ADTC (Addressed Data Transfer Commands) command is used ...
#define SD_MMC_VOLTAGE_SUPPORT
uint32_t hsmci_wait_end_of_read_blocks(void)
Wait the end of transfer initiated by mci_start_read_blocks()
const uint32_t sd_trans_multipliers[16]
SD/MMC protocol definitions.
#define SD_CSD_1_0_C_SIZE_MULT(csd)
uint32_t sd_cmd8(uint8_t *v2)
#define SD_ACMD51_SEND_SCR
#define SD_CMD8_MASK_PATTERN
#define SDMMC_CMD17_READ_SINGLE_BLOCK
uint32_t sd_mmc_cmd13(void)
CMD13 - Addressed card sends its status register. This function waits the clear of the busy flag...
#define SD_SW_STATUS_FUN_GRP1_BUSY(status)
int printf(const char *format,...)
#define SD_CMD8_SEND_IF_COND
#define SDMMC_MCI_CMD0_GO_IDLE_STATE
#define SD_CMD6_MODE_SWITCH
#define SD_ACMD6_SET_BUS_WIDTH
#define SDMMC_CMD55_APP_CMD
unsigned short bytes_per_sector
#define SD_MCI_ACMD41_SD_SEND_OP_COND
void hsmci_select_device(uint8_t slot, uint32_t clock, uint8_t bus_width, uint32_t high_speed)
Select a slot and initialize it.
#define CSD_REG_BSIZE
16 bytes
#define SD_SCR_SD_SPEC_1_10
#define SD_SW_STATUS_FUN_GRP_RC_ERROR
void hsmci_reset(void)
Reset the HSMCI interface.
#define SD_CMD8_HIGH_VOLTAGE
#define SD_CMD6_GRP4_NO_INFLUENCE
CMD6 arg[15:12] function group 4, 0xF or 0x0.
#define SDMMC_RESP_BUSY
Card may send busy.
uint32_t sd_mci_op_cond(uint8_t v2)
Ask to all cards to send their operations conditions (MCI only).
#define SD_SCR_SD_SPEC(scr)
const uint32_t sd_mmc_trans_units[7]
#define CARD_STATUS_READY_FOR_DATA
#define SDMMC_CMD24_WRITE_BLOCK
#define SDMMC_RESP_CRC
Expect valid crc (MCI only)
uint32_t sd_mmc_start_read_block(F_DRIVER *driver, void *dest, uint16_t nb_block)
#define CSD_TRAN_SPEED(csd)
#define SDMMC_CMD7_SELECT_CARD_CMD
#define SD_SW_STATUS_BSIZE
64 bytes
#define SD_SCR_SD_SPEC_2_00
#define SDMMC_CMD2_ALL_SEND_CID
#define CSD_STRUCTURE_VERSION(csd)
#define SDMMC_CMD16_SET_BLOCKLEN
#define SD_SCR_SD_SPEC_1_0_01
unsigned long number_of_sectors
#define SD_SCR_REG_BSIZE
8 bytes
uint32_t hsmci_start_read_blocks(void *dest, uint16_t nb_block)
Start a read blocks transfer on the line Note: The driver will use the DMA available to speed up the ...
uint32_t hsmci_get_response(void)
Return the 32 bits response of the last command.
uint8_t csd[CSD_REG_BSIZE]
#define SD_CMD6_GRP6_NO_INFLUENCE
CMD6 arg[23:20] function group 6, 0xF or 0x0.
#define SD_CSD_1_0_READ_BL_LEN(csd)
#define SD_CMD6_GRP1_HIGH_SPEED
CMD6 arg[ 3: 0] function group 1, access mode.
void hsmci_get_response_128(uint8_t *response)
Return the 128 bits response of the last command.
#define SD_SCR_SD_SPEC_3_00
F_WRITESECTOR writesector
#define SD_SW_STATUS_FUN_GRP1_RC(status)
#define SD_CMD6_GRP2_DEFAULT
#define SD_CMD6_SWITCH_FUNC
#define OCR_POWER_UP_BUSY
#define SD_MMC_BLOCK_SIZE
uint32_t sd_mmc_start_write_block(F_DRIVER *driver, void *src, uint16_t nb_block)
#define SD_CMD6_GRP3_NO_INFLUENCE
CMD6 arg[11: 8] function group 3, 0xF or 0x0.
F_DRIVER * sd_init(unsigned long driver_param)
#define SD_CSD_1_0_C_SIZE(csd)
void hsmci_send_clock(void)
Send 74 clock cycles on the line of selected slot Note: It is required after card plug and before car...
void sd_decode_csd(void)
Decodes SD CSD register.
#define SDMMC_MCI_CMD9_SEND_CSD
uint32_t hsmci_wait_busy(void)
Wait the end of busy signal on data line.
#define SD_CMD8_MASK_VOLTAGE
#define SDMMC_MCI_CMD13_SEND_STATUS
uint32_t hsmci_send_cmd(uint32_t cmd, uint32_t arg)
Send a command on the selected slot.
#define SD_SCR_SD_SPEC3(scr)
#define SD_CMD3_SEND_RELATIVE_ADDR
uint32_t sd_acmd6(void)
ACMD6 - Define the data bus width to 4 bits bus.
#define CARD_STATUS_SWITCH_ERROR
uint32_t sd_cm6_set_high_speed(void)
CMD6 for SD - Switch card in high speed mode.
#define SD_CSD_2_0_C_SIZE(csd)