byte MMC_ReadSector(byte *buffer, uint32_t sector) { if (!(_mmcState & MMC_INITED)) return MMC_NOT_INITED; if (!(_mmcState & MMC_HIGH_DENSITY)) sector <<= 9; MMC_SS_LOW(); if (MMC_Command(17,sector) != 0 || MMC_Token() != 0xFE) return MMC_Release(READ_FAILED); SPI_Receive(buffer,512); // WARNING! Will strip 2 CRC bytes as well return MMC_Release(0); }
byte MMC_WriteSector(byte *buffer, uint32_t sector) { if (!(_mmcState & MMC_INITED)) return MMC_NOT_INITED; if (!(_mmcState & MMC_HIGH_DENSITY)) sector <<= 9; u8 r = WRITE_FAILED; SPI_Enable(); MMC_SS_LOW(); if (MMC_Command2(24,sector) != 0) { u8 d = 16; while (d--) SPI_ReceiveByte(0xFF); // pad SPI_ReceiveByte(0xFE); // Send token SPI_Send(buffer,512); SPI_ReceiveByte(0xFF); // CRC SPI_ReceiveByte(0xFF); // CRC u8 status; while ((status = SPI_ReceiveByte(0xFF)) == 0xFF) ; if (status == 0xE5) r = 0; else r = status; while (SPI_ReceiveByte(0xFF) == 0) ; r = MMC_Release(r); } else { r = MMC_Release(WRITE_FAILED); } return r; }
byte MMC_Init() { return MMC_Release(MMC_Init2()); }