static BYTE send_cmd ( BYTE cmd, /* Command byte */ DWORD arg /* Argument */ ) { BYTE n, res; if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ cmd &= 0x7F; res = send_cmd(CMD55, 0); if (res > 1) return res; } #if AXI_SPI XStatus Status = XSpi_SetSlaveSelect(&Spi, 0); if(Status != XST_SUCCESS) { xil_printf("Failure Slave Select 0\r\n"); xil_printf("Status = %d\r\n", Status); } rcvr_mmc(); Status = XSpi_SetSlaveSelect(&Spi, 1); if(Status != XST_SUCCESS) { xil_printf("Failure Slave Select 1\r\n"); } rcvr_mmc(); #else /* Select the card */ CS_H(); rcvr_mmc(); CS_L(); rcvr_mmc(); #endif /* Send a command packet */ xmit_mmc(cmd); /* Start + Command index */ xmit_mmc((BYTE)(arg >> 24)); /* Argument[31..24] */ xmit_mmc((BYTE)(arg >> 16)); /* Argument[23..16] */ xmit_mmc((BYTE)(arg >> 8)); /* Argument[15..8] */ xmit_mmc((BYTE)arg); /* Argument[7..0] */ n = 0x01; /* Dummy CRC + Stop */ if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */ if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */ xmit_mmc(n); /* Receive a command response */ n = 10; /* Wait for a valid response in timeout of 10 attempts */ do { res = rcvr_mmc(); } while ((res & 0x80) && --n); return res; /* Return with the response value */ }
DRESULT disk_writep ( const BYTE *buff, /* Pointer to the bytes to be written (NULL:Initiate/Finalize sector write) */ DWORD sa /* Number of bytes to send, Sector number (LBA) or zero */ ) { DRESULT res; WORD bc, tmr; static WORD wc; res = RES_ERROR; if (buff) { /* Send data bytes */ bc = (WORD)sa; while (bc && wc) { /* Send data bytes to the card */ xmit_mmc(*buff++); wc--; bc--; } res = RES_OK; } else { if (sa) { /* Initiate sector write process */ if (!(CardType & CT_BLOCK)) sa *= 512; /* Convert to byte address if needed */ if (send_cmd(CMD24, sa) == 0) { /* WRITE_SINGLE_BLOCK */ xmit_mmc(0xFF); xmit_mmc(0xFE); /* Data block header */ wc = 512; /* Set byte counter */ res = RES_OK; } } else { /* Finalize sector write process */ bc = wc + 2; while (bc--) xmit_mmc(0); /* Fill left bytes and CRC with zeros */ if ((rcvr_mmc() & 0x1F) == 0x05) { /* Receive data resp and wait for end of write process in timeout of 300ms */ for (tmr = 10000; rcvr_mmc() != 0xFF && tmr; tmr--) /* Wait for ready (max 1000ms) */ // DLY_US(100); mmc_spi_Wait100us(); if (tmr) res = RES_OK; } release_spi(); } } return res; }
static BYTE send_cmd ( BYTE cmd, /* Command byte */ DWORD arg /* Argument */ ) { BYTE n, res; if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ cmd &= 0x7F; res = send_cmd(CMD55, 0); if (res > 1) return res; } /* Select the card */ // CS_H(); rcvr_mmc(); release_spi(); // CS_L(); rcvr_mmc(); mmc_spi_SetCardSelect(); /* Send a command packet */ xmit_mmc(cmd); /* Start + Command index */ xmit_mmc((BYTE)(arg >> 24)); /* Argument[31..24] */ xmit_mmc((BYTE)(arg >> 16)); /* Argument[23..16] */ xmit_mmc((BYTE)(arg >> 8)); /* Argument[15..8] */ xmit_mmc((BYTE)arg); /* Argument[7..0] */ n = 0x01; /* Dummy CRC + Stop */ if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */ if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */ xmit_mmc(n); /* Receive a command response */ n = 10; /* Wait for a valid response in timeout of 10 attempts */ do { res = rcvr_mmc(); } while ((res & 0x80) && --n); return res; /* Return with the response value */ }
static int xmit_datablock ( /* 1:OK, 0:Failed */ const BYTE *buff, /* 512 byte data block to be transmitted */ BYTE token /* Data/Stop token */ ) { BYTE d[2]; if (!wait_ready()) return 0; d[0] = token; xmit_mmc(d, 1); /* Xmit a token */ if (token != 0xFD) { /* Is it data token? */ xmit_mmc(buff, 512); /* Xmit the 512 byte data block to MMC */ rcvr_mmc(d, 2); /* Xmit dummy CRC (0xFF,0xFF) */ rcvr_mmc(d, 1); /* Receive data response */ if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */ return 0; } return 1; }
static BYTE send_cmd ( /* Returns command response (bit7==1:Send failed)*/ BYTE cmd, /* Command byte */ DWORD arg /* Argument */ ) { BYTE n, d, buf[6]; if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ cmd &= 0x7F; n = send_cmd(CMD55, 0); if (n > 1) return n; } /* Select the card and wait for ready */ deselect(); if (!SPI_select()) return 0xFF; /* Send a command packet */ buf[0] = 0x40 | cmd; /* Start + Command index */ buf[1] = (BYTE)(arg >> 24); /* Argument[31..24] */ buf[2] = (BYTE)(arg >> 16); /* Argument[23..16] */ buf[3] = (BYTE)(arg >> 8); /* Argument[15..8] */ buf[4] = (BYTE)arg; /* Argument[7..0] */ n = 0x01; /* Dummy CRC + Stop */ if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */ if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */ buf[5] = n; xmit_mmc(buf, 6); /* Receive command response */ if (cmd == CMD12) rcvr_mmc(&d, 1); /* Skip a stuff byte when stop reading */ n = 10; /* Wait for a valid response in timeout of 10 attempts */ do rcvr_mmc(&d, 1); while ((d & 0x80) && --n); return d; /* Return with the response value */ }