2017-12-21 05:42:46 +00:00
|
|
|
/**
|
|
|
|
* Interface from Atmel USB MSD to Marlin SD card
|
|
|
|
*/
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
#ifdef ARDUINO_ARCH_SAM
|
2017-12-21 05:42:46 +00:00
|
|
|
|
|
|
|
#include "../../../inc/MarlinConfig.h"
|
2018-01-10 06:46:34 +00:00
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
2017-12-21 05:42:46 +00:00
|
|
|
#include "../../../sd/cardreader.h"
|
|
|
|
extern "C" {
|
|
|
|
#include "sd_mmc_spi_mem.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SD_MMC_BLOCK_SIZE 512
|
|
|
|
|
2019-09-17 01:31:08 +00:00
|
|
|
void sd_mmc_spi_mem_init() {
|
2017-12-21 05:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:31:08 +00:00
|
|
|
Ctrl_status sd_mmc_spi_test_unit_ready() {
|
2019-11-10 03:17:18 +00:00
|
|
|
#ifdef DISABLE_DUE_SD_MMC
|
2019-11-11 00:49:41 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
#endif
|
2019-09-15 08:10:59 +00:00
|
|
|
if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted())
|
2017-12-21 05:42:46 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
return CTRL_GOOD;
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
// NOTE: This function is defined as returning the address of the last block
|
|
|
|
// in the card, which is cardSize() - 1
|
2018-01-10 06:46:34 +00:00
|
|
|
Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) {
|
2019-09-15 08:10:59 +00:00
|
|
|
if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted())
|
2017-12-21 05:42:46 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
2018-10-18 16:34:07 +00:00
|
|
|
*nb_sector = card.getSd2Card().cardSize() - 1;
|
2017-12-21 05:42:46 +00:00
|
|
|
return CTRL_GOOD;
|
|
|
|
}
|
|
|
|
|
2019-10-01 02:44:07 +00:00
|
|
|
bool sd_mmc_spi_unload(bool) { return true; }
|
2017-12-21 05:42:46 +00:00
|
|
|
|
2019-10-01 02:44:07 +00:00
|
|
|
bool sd_mmc_spi_wr_protect() { return false; }
|
2017-12-21 05:42:46 +00:00
|
|
|
|
2019-09-17 01:31:08 +00:00
|
|
|
bool sd_mmc_spi_removal() {
|
2019-10-01 02:44:07 +00:00
|
|
|
return (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted());
|
2017-12-21 05:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ACCESS_USB == true
|
|
|
|
/**
|
|
|
|
* \name MEM <-> USB Interface
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "udi_msc.h"
|
|
|
|
|
|
|
|
COMPILER_WORD_ALIGNED
|
|
|
|
uint8_t sector_buf[SD_MMC_BLOCK_SIZE];
|
|
|
|
|
|
|
|
// #define DEBUG_MMC
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
|
2019-11-10 03:17:18 +00:00
|
|
|
#ifdef DISABLE_DUE_SD_MMC
|
2019-11-11 00:49:41 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
#endif
|
2019-09-15 08:10:59 +00:00
|
|
|
if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted())
|
2017-12-21 05:42:46 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
#ifdef DEBUG_MMC
|
2019-02-24 04:53:01 +00:00
|
|
|
{
|
2018-01-10 06:46:34 +00:00
|
|
|
char buffer[80];
|
2019-01-19 22:56:12 +00:00
|
|
|
sprintf_P(buffer, PSTR("SDRD: %d @ 0x%08x\n"), nb_sector, addr);
|
2019-02-24 04:53:01 +00:00
|
|
|
PORT_REDIRECT(0);
|
|
|
|
SERIAL_ECHO(buffer);
|
|
|
|
}
|
2018-01-10 06:46:34 +00:00
|
|
|
#endif
|
2017-12-21 05:42:46 +00:00
|
|
|
|
|
|
|
// Start reading
|
|
|
|
if (!card.getSd2Card().readStart(addr))
|
|
|
|
return CTRL_FAIL;
|
|
|
|
|
|
|
|
// For each specified sector
|
|
|
|
while (nb_sector--) {
|
|
|
|
|
|
|
|
// Read a sector
|
|
|
|
card.getSd2Card().readData(sector_buf);
|
|
|
|
|
|
|
|
// RAM -> USB
|
2020-10-24 22:13:10 +00:00
|
|
|
if (!udi_msc_trans_block(true, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
|
2017-12-21 05:42:46 +00:00
|
|
|
card.getSd2Card().readStop();
|
|
|
|
return CTRL_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop reading
|
|
|
|
card.getSd2Card().readStop();
|
|
|
|
|
|
|
|
// Done
|
|
|
|
return CTRL_GOOD;
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
|
2019-11-10 03:17:18 +00:00
|
|
|
#ifdef DISABLE_DUE_SD_MMC
|
2019-11-11 00:49:41 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
#endif
|
2019-09-15 08:10:59 +00:00
|
|
|
if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted())
|
2017-12-21 05:42:46 +00:00
|
|
|
return CTRL_NO_PRESENT;
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
#ifdef DEBUG_MMC
|
2019-02-24 04:53:01 +00:00
|
|
|
{
|
2018-01-10 06:46:34 +00:00
|
|
|
char buffer[80];
|
2019-01-19 22:56:12 +00:00
|
|
|
sprintf_P(buffer, PSTR("SDWR: %d @ 0x%08x\n"), nb_sector, addr);
|
2019-02-24 04:53:01 +00:00
|
|
|
PORT_REDIRECT(0);
|
|
|
|
SERIAL_ECHO(buffer);
|
|
|
|
}
|
2018-01-10 06:46:34 +00:00
|
|
|
#endif
|
2017-12-21 05:42:46 +00:00
|
|
|
|
|
|
|
if (!card.getSd2Card().writeStart(addr, nb_sector))
|
|
|
|
return CTRL_FAIL;
|
|
|
|
|
|
|
|
// For each specified sector
|
|
|
|
while (nb_sector--) {
|
|
|
|
|
|
|
|
// USB -> RAM
|
2020-10-24 22:13:10 +00:00
|
|
|
if (!udi_msc_trans_block(false, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
|
2017-12-21 05:42:46 +00:00
|
|
|
card.getSd2Card().writeStop();
|
|
|
|
return CTRL_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a sector
|
|
|
|
card.getSd2Card().writeData(sector_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop writing
|
|
|
|
card.getSd2Card().writeStop();
|
|
|
|
|
|
|
|
// Done
|
|
|
|
return CTRL_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ACCESS_USB == true
|
|
|
|
|
2018-01-10 06:46:34 +00:00
|
|
|
#endif // SDSUPPORT
|
|
|
|
#endif // ARDUINO_ARCH_SAM
|