2018-07-02 15:06:17 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 21:06:53 +00:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-07-02 15:06:17 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
|
|
|
|
|
|
|
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
#include "../../core/serial.h"
|
|
|
|
|
2018-07-02 15:06:17 +00:00
|
|
|
#include "lib/Usb.h"
|
|
|
|
#include "lib/masstorage.h"
|
|
|
|
|
|
|
|
#include "Sd2Card_FlashDrive.h"
|
|
|
|
|
2019-01-01 21:17:48 +00:00
|
|
|
#if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
|
|
|
|
#include "../../lcd/ultralcd.h"
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 15:06:17 +00:00
|
|
|
USB usb;
|
|
|
|
BulkOnly bulk(&usb);
|
|
|
|
|
|
|
|
Sd2Card::state_t Sd2Card::state;
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
// The USB library needs to be called periodically to detect USB thumbdrive
|
|
|
|
// insertion and removals. Call this idle() function periodically to allow
|
2018-10-19 02:20:56 +00:00
|
|
|
// the USB library to monitor for such events. This function also takes care
|
2018-10-18 16:34:07 +00:00
|
|
|
// of initializing the USB library for the first time.
|
2018-07-02 15:06:17 +00:00
|
|
|
|
|
|
|
void Sd2Card::idle() {
|
|
|
|
static uint32_t next_retry;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case USB_HOST_DELAY_INIT:
|
2019-01-01 21:17:48 +00:00
|
|
|
next_retry = millis() + 2000;
|
2018-07-02 15:06:17 +00:00
|
|
|
state = USB_HOST_WAITING;
|
|
|
|
break;
|
|
|
|
case USB_HOST_WAITING:
|
2018-10-21 04:21:27 +00:00
|
|
|
if (ELAPSED(millis(), next_retry)) {
|
2019-01-01 21:17:48 +00:00
|
|
|
next_retry = millis() + 2000;
|
2018-07-02 15:06:17 +00:00
|
|
|
state = USB_HOST_UNINITIALIZED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_HOST_UNINITIALIZED:
|
2019-01-01 21:17:48 +00:00
|
|
|
SERIAL_ECHOPGM("Starting USB host...");
|
2018-07-02 15:06:17 +00:00
|
|
|
if (!usb.start()) {
|
2019-01-01 21:17:48 +00:00
|
|
|
SERIAL_ECHOPGM(" Failed. Retrying in 2s.");
|
|
|
|
#if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
|
|
|
|
LCD_MESSAGEPGM("USB start failed");
|
|
|
|
#endif
|
2018-07-02 15:06:17 +00:00
|
|
|
state = USB_HOST_DELAY_INIT;
|
|
|
|
}
|
2019-01-01 21:17:48 +00:00
|
|
|
else
|
2018-07-02 15:06:17 +00:00
|
|
|
state = USB_HOST_INITIALIZED;
|
2019-01-01 21:17:48 +00:00
|
|
|
SERIAL_EOL();
|
2018-07-02 15:06:17 +00:00
|
|
|
break;
|
|
|
|
case USB_HOST_INITIALIZED:
|
|
|
|
const uint8_t lastUsbTaskState = usb.getUsbTaskState();
|
|
|
|
usb.Task();
|
|
|
|
const uint8_t newUsbTaskState = usb.getUsbTaskState();
|
|
|
|
|
|
|
|
if (lastUsbTaskState == USB_STATE_RUNNING && newUsbTaskState != USB_STATE_RUNNING) {
|
|
|
|
// the user pulled the flash drive. Make sure the bulk storage driver releases the address
|
2018-10-18 16:34:07 +00:00
|
|
|
#ifdef USB_DEBUG
|
|
|
|
SERIAL_ECHOLNPGM("USB drive removed");
|
|
|
|
#endif
|
2018-07-02 15:06:17 +00:00
|
|
|
//bulk.Release();
|
|
|
|
}
|
2018-10-18 16:34:07 +00:00
|
|
|
if (lastUsbTaskState != USB_STATE_RUNNING && newUsbTaskState == USB_STATE_RUNNING) {
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
SERIAL_ECHOLNPGM("USB drive inserted");
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-02 15:06:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
// Marlin calls this function to check whether an USB drive is inserted.
|
|
|
|
// This is equivalent to polling the SD_DETECT when using SD cards.
|
|
|
|
bool Sd2Card::isInserted() {
|
|
|
|
return usb.getUsbTaskState() == USB_STATE_RUNNING;
|
2019-01-01 21:17:48 +00:00
|
|
|
}
|
2018-07-02 15:06:17 +00:00
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
// Marlin calls this to initialize an SD card once it is inserted.
|
2019-01-01 21:17:48 +00:00
|
|
|
bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) {
|
2018-10-18 16:34:07 +00:00
|
|
|
if (!ready()) return false;
|
2018-07-02 15:06:17 +00:00
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
if (!bulk.LUNIsGood(0)) {
|
|
|
|
SERIAL_ECHOLNPGM("LUN zero is not good");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-02 15:06:17 +00:00
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
const uint32_t sectorSize = bulk.GetSectorSize(0);
|
|
|
|
if (sectorSize != 512) {
|
2019-01-19 22:56:12 +00:00
|
|
|
SERIAL_ECHOLNPAIR("Expecting sector size of 512. Got: ", sectorSize);
|
2018-07-02 15:06:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
#ifdef USB_DEBUG
|
|
|
|
lun0_capacity = bulk.GetCapacity(0);
|
|
|
|
SERIAL_ECHOLNPAIR("LUN Capacity (in blocks): ", lun0_capacity);
|
|
|
|
#endif
|
|
|
|
return true;
|
2018-07-02 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
// Returns the capacity of the card in blocks.
|
|
|
|
uint32_t Sd2Card::cardSize() {
|
|
|
|
if (!ready()) return 0;
|
|
|
|
#ifndef USB_DEBUG
|
|
|
|
const uint32_t
|
|
|
|
#endif
|
2019-01-01 21:17:48 +00:00
|
|
|
lun0_capacity = bulk.GetCapacity(0);
|
2018-10-18 16:34:07 +00:00
|
|
|
return lun0_capacity;
|
2018-07-02 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
bool Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
|
|
|
|
if (!ready()) return false;
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
if (block >= lun0_capacity) {
|
|
|
|
SERIAL_ECHOLNPAIR("Attempt to read past end of LUN: ", block);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if USB_DEBUG > 1
|
|
|
|
SERIAL_ECHOLNPAIR("Read block ", block);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return bulk.Read(0, block, 512, 1, dst) == 0;
|
2018-07-02 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:34:07 +00:00
|
|
|
bool Sd2Card::writeBlock(uint32_t block, const uint8_t* src) {
|
|
|
|
if (!ready()) return false;
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
if (block >= lun0_capacity) {
|
|
|
|
SERIAL_ECHOLNPAIR("Attempt to write past end of LUN: ", block);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if USB_DEBUG > 1
|
|
|
|
SERIAL_ECHOLNPAIR("Write block ", block);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return bulk.Write(0, block, 512, 1, src) == 0;
|
2018-07-02 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USB_FLASH_DRIVE_SUPPORT
|