int mmc_readsector(uint32_t lba, uint8_t *buffer) { uint16_t i,retval; printf("\nreadsector\n"); // send read command and logical sector address mmc_send_command(17,(lba>>7) & 0xffff, (lba<<9) & 0xffff); //mmc_send_command(17,(lba>>16) & 0xffff, (lba) & 0xffff); //wjr retval=mmc_get(); printf("\ngot rc %d\n",retval); if (mmc_datatoken() != 0xfe) // if no valid token { mmc_clock_and_release(); // cleanup and return -1; // return error code } spiReceiveN(buffer,512); // for (i=0;i<512;i++) // read sector data // *buffer++ = spixfer(0xff); (void)spixfer(0xff); // ignore dummy checksum (void)spixfer(0xff); // ignore dummy checksum mmc_clock_and_release(); // cleanup return 0; // return success }
/* write a single 512 byte sector to the SD card */ unsigned int mmc_writesector(uint32_t lba, uint8_t *buffer) { uint16_t i; uint8_t r; CS_ASSERT; // send command and sector mmc_send_command(WRITE_SINGLE_BLOCK, lba<<9); mmc_datatoken(); // get response spi_byte(0xfe); // send start block token for (i=0;i<512;i++) // write sector data spi_byte(*buffer++); spi_byte(0xff); // ignore checksum spi_byte(0xff); // ignore checksum r = spi_byte(0xff); // check for error if ((r & 0x1f) != 0x05) return r; // wait for SD card to complete writing and become idle i = 0xffff; while (!spi_byte(0xff) && --i) ; // wait for card to finish writing mmc_release(); // cleanup if (!i) return -1; // timeout error return 0; }
/** Read MMC/SD sector. Read a single 512 byte sector from the MMC/SD card \param lba Logical sectornumber to read \param buffer Pointer to buffer for received data \return 0 on success, -1 on error */ int mmc_readsector(uint32_t lba, uint8_t *buffer) { uint16_t i; // send read command and logical sector address mmc_send_command(17,(lba>>7) & 0xffff, (lba<<9) & 0xffff); if (mmc_datatoken() != 0xfe) // if no valid token { mmc_clock_and_release(); // cleanup and return -1; // return error code } for (i=0;i<512;i++) // read sector data *buffer++ = spi_byte(0xff); spi_byte(0xff); // ignore dummy checksum spi_byte(0xff); // ignore dummy checksum mmc_clock_and_release(); // cleanup return 0; // return success }
/* reads a single 512 bytes sector from the SD card */ int mmc_readsector(uint32_t lba, uint8_t *buffer) { uint16_t i; // send command and sector mmc_send_command(READ_SINGLE_BLOCK, lba<<9); if (mmc_datatoken() != 0xfe) // wait for start of block token { mmc_release(); // error return -1; } for (i=0;i<512;i++) *buffer++ = spi_byte(0xff); // read data spi_byte(0xff); // ignore checksum spi_byte(0xff); // ignore checksum mmc_release(); return 0; }