From febdff20134082857ff642d6d909d165536990c9 Mon Sep 17 00:00:00 2001 From: Marcio Teixeira Date: Wed, 21 Mar 2018 15:24:08 -0600 Subject: [PATCH] USB removal/insertion now works for Quiver. --- Marlin/AO_UI_Marlin_LCD_API.h | 12 ++- Marlin/Conditionals_LulzBot.h | 2 +- Marlin/usb-flashdrive/Fake_Sd2Card.h | 24 +++--- Marlin/usb-flashdrive/Fake_Sd2Card_impl.h | 98 +++++++++------------- Marlin/usb-flashdrive/lib/UsbCore.h | 3 - Marlin/usb-flashdrive/lib/address.h | 30 ------- Marlin/usb-flashdrive/lib/confdescparser.h | 45 ---------- Marlin/usb-flashdrive/lib/parsetools.cpp | 67 --------------- Marlin/usb-flashdrive/lib/parsetools.h | 76 ++++------------- Marlin/usb-flashdrive/lib/usb_ch9.h | 16 ---- 10 files changed, 77 insertions(+), 296 deletions(-) delete mode 100644 Marlin/usb-flashdrive/lib/parsetools.cpp diff --git a/Marlin/AO_UI_Marlin_LCD_API.h b/Marlin/AO_UI_Marlin_LCD_API.h index 1c6032bb9..57fc80919 100644 --- a/Marlin/AO_UI_Marlin_LCD_API.h +++ b/Marlin/AO_UI_Marlin_LCD_API.h @@ -269,8 +269,8 @@ void Marlin_LCD_API::initMedia() { } void Marlin_LCD_API::checkMedia() { - #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT) - const bool sd_status = IS_SD_INSERTED; + #if (ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)) || defined(LULZBOT_USE_USB_STICK) + const bool sd_status = isMediaInserted(); if (sd_status != lcd_sd_status) { SERIAL_PROTOCOLLNPAIR("SD Status: ", sd_status); @@ -309,8 +309,12 @@ bool Marlin_LCD_API::isPrinting() { } bool Marlin_LCD_API::isMediaInserted() { - #if ENABLED(SDSUPPORT) - return IS_SD_INSERTED; + #if (ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)) || defined(LULZBOT_USE_USB_STICK) + #if defined(LULZBOT_USE_USB_STICK) + return Sd2Card::isInserted(); + #else + return IS_SD_INSERTED; + #endif #else return false; #endif diff --git a/Marlin/Conditionals_LulzBot.h b/Marlin/Conditionals_LulzBot.h index 6eb00d2f0..f3a7e609d 100644 --- a/Marlin/Conditionals_LulzBot.h +++ b/Marlin/Conditionals_LulzBot.h @@ -193,7 +193,7 @@ #define LULZBOT_USE_EINSYRAMBO #define LULZBOT_USE_EINSY_RETRO #define LULZBOT_USE_TOUCH_UI - #define LULZBOT_USE_HIGH_RES + //#define LULZBOT_USE_HIGH_RES #define LULZBOT_USE_PORTRAIT_UI #define LULZBOT_TWO_PIECE_BED #define LULZBOT_USE_AUTOLEVELING diff --git a/Marlin/usb-flashdrive/Fake_Sd2Card.h b/Marlin/usb-flashdrive/Fake_Sd2Card.h index 9d228d518..d906b6518 100644 --- a/Marlin/usb-flashdrive/Fake_Sd2Card.h +++ b/Marlin/usb-flashdrive/Fake_Sd2Card.h @@ -67,19 +67,21 @@ uint8_t const SPI_FULL_SPEED = 0, // Set SCK to max rate of F_CPU/2. See * \brief Raw access to SD and SDHC flash memory cards. */ class Sd2Card { - public: + private: + static bool usbHostReady(); - Sd2Card(); + public: + static bool isInserted(); - /** - * Initialize an SD flash memory card with default clock rate and chip - * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). - * - * \return true for success or false for failure. - */ - bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); - bool readBlock(uint32_t block, uint8_t* dst); - bool writeBlock(uint32_t blockNumber, const uint8_t* src); + /** + * Initialize an SD flash memory card with default clock rate and chip + * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). + * + * \return true for success or false for failure. + */ + bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); + bool readBlock(uint32_t block, uint8_t* dst); + bool writeBlock(uint32_t blockNumber, const uint8_t* src); }; #endif // _FAKE_SD2CARD_H_ diff --git a/Marlin/usb-flashdrive/Fake_Sd2Card_impl.h b/Marlin/usb-flashdrive/Fake_Sd2Card_impl.h index 6d44d53af..81ea28bfd 100644 --- a/Marlin/usb-flashdrive/Fake_Sd2Card_impl.h +++ b/Marlin/usb-flashdrive/Fake_Sd2Card_impl.h @@ -21,11 +21,6 @@ */ /******************************************************************************************** - * This program/sketch is used to run a USB Thumb Drive. * - * * - * NOTE - This Arduino Sketch has been modified to initialize a MAX3421E USB Host Interface * - * chip, write 3 test files, print out the directory of the thumb drive and print out the * - * contents of a short .txt file. * * * * The code is leveraged from the following: * * * @@ -53,73 +48,61 @@ #include -//#define _usb_h_ - #include "Marlin.h" -#include "../watchdog.h" - -#undef MACROS_H #define USB_HOST_SERIAL customizedSerial -#include "lib/masstorage.h" -#include "lib/masstorage.cpp" -#include "lib/message.cpp" -#include "lib/parsetools.cpp" -#include "lib/Usb.cpp" - +#if defined(MACROS_H) + #undef MACROS_H // The USB host library wants to redefine this. + #include "lib/masstorage.cpp" + #include "lib/message.cpp" + #include "lib/Usb.cpp" +#else + #include "lib/masstorage.cpp" + #include "lib/message.cpp" + #include "lib/Usb.cpp" + #undef MACROS_H +#endif #include "Fake_Sd2Card.h" -#define MAX_USB_RST 7 - -// USB host objects.v USB usb; BulkOnly bulk(&usb); -#define error(msg) {Serial.print("Error: "); Serial.println(msg);} - -#define TIMEOUT_MILLIS 4000 - -//------------------------------------------------------------------------------ -bool initUSB(USB* usb) { - uint8_t current_state = 0; - uint32_t m = millis(); - - for (uint8_t i = 0; usb->Init(1000) == -1; i++) - { - SERIAL_ECHOLNPGM("No USB HOST Shield?"); - watchdog_reset(); - if (i > 10) { - return false; - } +bool Sd2Card::usbHostReady() { + static enum { + USB_HOST_UNINITIALIZED, + USB_HOST_FAILED_INIT, + USB_HOST_INITIALIZED + } state = USB_HOST_UNINITIALIZED; + + if(state == USB_HOST_UNINITIALIZED) { + if(usb.Init(1000) == -1) { + SERIAL_ECHOLNPGM("USB host failed to initialize"); + state = USB_HOST_FAILED_INIT; + } else { + usb.vbusPower(vbus_on); + SERIAL_ECHOLNPGM("USB host initialized"); + state = USB_HOST_INITIALIZED; + } } - usb->vbusPower(vbus_on); - - while ((millis() - m) < TIMEOUT_MILLIS) { - usb->Task(); - current_state = usb->getUsbTaskState(); - if(current_state == USB_STATE_RUNNING) { - return true; - } - watchdog_reset(); - } - return false; + return state == USB_HOST_INITIALIZED; } -Sd2Card::Sd2Card() { -}; +// Marlin will call this to learn whether an SD card is inserted. +bool Sd2Card::isInserted() { + if(usbHostReady()) { + usb.Task(); + return usb.getUsbTaskState() == USB_STATE_RUNNING; + } +} +// Marlin calls this whenever an SD card is detected, so this method +// should not be used to initialize the USB host library bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { - if (!initUSB(&usb)) - { - SERIAL_ECHOLNPGM("initUSB failed"); - } - else - { - SERIAL_ECHOLNPGM("USB Initialized\n"); - } + if(!usbHostReady()) + return false; if(!bulk.LUNIsGood(0)) { SERIAL_ECHOLNPGM("LUN zero is not good\n"); @@ -143,5 +126,4 @@ bool Sd2Card::readBlock(uint32_t block, uint8_t* dst) { bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { return bulk.Write(0, blockNumber, 512, 1, src) == 0; -} - +} \ No newline at end of file diff --git a/Marlin/usb-flashdrive/lib/UsbCore.h b/Marlin/usb-flashdrive/lib/UsbCore.h index f514195cd..c3f8e6c34 100644 --- a/Marlin/usb-flashdrive/lib/UsbCore.h +++ b/Marlin/usb-flashdrive/lib/UsbCore.h @@ -222,9 +222,6 @@ public: return USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS; }; - void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) { - addrPool.ForEachUsbDevice(pfunc); - }; uint8_t getUsbTaskState(void); void setUsbTaskState(uint8_t state); diff --git a/Marlin/usb-flashdrive/lib/address.h b/Marlin/usb-flashdrive/lib/address.h index 74473dc93..58071046f 100644 --- a/Marlin/usb-flashdrive/lib/address.h +++ b/Marlin/usb-flashdrive/lib/address.h @@ -87,8 +87,6 @@ public: virtual void FreeAddress(uint8_t addr) = 0; }; -typedef void (*UsbDeviceHandleFunc)(UsbDevice *pdev); - #define ADDR_ERROR_INVALID_INDEX 0xFF #define ADDR_ERROR_INVALID_ADDRESS 0xFF @@ -186,17 +184,6 @@ public: return (!index) ? NULL : thePool + index; }; - // Performs an operation specified by pfunc for each addressed device - - void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) { - if(!pfunc) - return; - - for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) - if(thePool[i].address.devAddress) - pfunc(thePool + i); - }; - // Allocates new address virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) { @@ -260,23 +247,6 @@ public: uint8_t index = FindAddressIndex(addr); FreeAddressByIndex(index); }; - - // Returns number of hubs attached - // It can be rather helpfull to find out if there are hubs attached than getting the exact number of hubs. - //uint8_t GetNumHubs() - //{ - // return hubCounter; - //}; - //uint8_t GetNumDevices() - //{ - // uint8_t counter = 0; - - // for (uint8_t i=1; i::ParseDescriptor if(theXtractor) theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer); break; - //case HID_DESCRIPTOR_HID: - // if (!valParser.Parse(pp, pcntdn)) - // return false; - // PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer); - // break; default: if(!theSkipper.Skip(pp, pcntdn, dscrLen - 2)) return false; @@ -177,41 +169,4 @@ bool ConfigDescParser::ParseDescriptor return true; } -template -void ConfigDescParser::PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc) { - Notify(PSTR("\r\n\r\nHID Descriptor:\r\n"), 0x80); - Notify(PSTR("bDescLength:\t\t"), 0x80); - PrintHex (pDesc->bLength, 0x80); - - Notify(PSTR("\r\nbDescriptorType:\t"), 0x80); - PrintHex (pDesc->bDescriptorType, 0x80); - - Notify(PSTR("\r\nbcdHID:\t\t\t"), 0x80); - PrintHex (pDesc->bcdHID, 0x80); - - Notify(PSTR("\r\nbCountryCode:\t\t"), 0x80); - PrintHex (pDesc->bCountryCode, 0x80); - - Notify(PSTR("\r\nbNumDescriptors:\t"), 0x80); - PrintHex (pDesc->bNumDescriptors, 0x80); - - //Notify(PSTR("\r\nbDescrType:\t\t")); - //PrintHex(pDesc->bDescrType); - // - //Notify(PSTR("\r\nwDescriptorLength:\t")); - //PrintHex(pDesc->wDescriptorLength); - - for(uint8_t i = 0; i < pDesc->bNumDescriptors; i++) { - HID_CLASS_DESCRIPTOR_LEN_AND_TYPE *pLT = (HID_CLASS_DESCRIPTOR_LEN_AND_TYPE*)&(pDesc->bDescrType); - - Notify(PSTR("\r\nbDescrType:\t\t"), 0x80); - PrintHex (pLT[i].bDescrType, 0x80); - - Notify(PSTR("\r\nwDescriptorLength:\t"), 0x80); - PrintHex (pLT[i].wDescriptorLength, 0x80); - } - Notify(PSTR("\r\n"), 0x80); -} - - #endif // __CONFDESCPARSER_H__ diff --git a/Marlin/usb-flashdrive/lib/parsetools.cpp b/Marlin/usb-flashdrive/lib/parsetools.cpp deleted file mode 100644 index 74a861059..000000000 --- a/Marlin/usb-flashdrive/lib/parsetools.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. - -This software may be distributed and modified under the terms of the GNU -General Public License version 2 (GPL2) as published by the Free Software -Foundation and appearing in the file GPL2.TXT included in the packaging of -this file. Please note that GPL2 Section 2[b] requires that all works based -on this software must also be made publicly available under the terms of -the GPL2 ("Copyleft"). - -Contact information -------------------- - -Circuits At Home, LTD -Web : http://www.circuitsathome.com -e-mail : support@circuitsathome.com - */ -#include "Usb.h" - -bool MultiByteValueParser::Parse(uint8_t **pp, uint16_t *pcntdn) { - if(!pBuf) { - Notify(PSTR("Buffer pointer is NULL!\r\n"), 0x80); - return false; - } - for(; countDown && (*pcntdn); countDown--, (*pcntdn)--, (*pp)++) - pBuf[valueSize - countDown] = (**pp); - - if(countDown) - return false; - - countDown = valueSize; - return true; -} - -bool PTPListParser::Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me) { - switch(nStage) { - case 0: - pBuf->valueSize = lenSize; - theParser.Initialize(pBuf); - nStage = 1; - - case 1: - if(!theParser.Parse(pp, pcntdn)) - return false; - - arLen = 0; - arLen = (pBuf->valueSize >= 4) ? *((uint32_t*)pBuf->pValue) : (uint32_t)(*((uint16_t*)pBuf->pValue)); - arLenCntdn = arLen; - nStage = 2; - - case 2: - pBuf->valueSize = valSize; - theParser.Initialize(pBuf); - nStage = 3; - - case 3: - for(; arLenCntdn; arLenCntdn--) { - if(!theParser.Parse(pp, pcntdn)) - return false; - - if(pf) - pf(pBuf, (arLen - arLenCntdn), me); - } - - nStage = 0; - } - return true; -} diff --git a/Marlin/usb-flashdrive/lib/parsetools.h b/Marlin/usb-flashdrive/lib/parsetools.h index 66e9531c3..812f1e932 100644 --- a/Marlin/usb-flashdrive/lib/parsetools.h +++ b/Marlin/usb-flashdrive/lib/parsetools.h @@ -44,9 +44,23 @@ public: countDown = valueSize = pbuf->valueSize; }; - bool Parse(uint8_t **pp, uint16_t *pcntdn); + bool Parse(uint8_t **pp, uint16_t *pcntdn) { + if(!pBuf) { + Notify(PSTR("Buffer pointer is NULL!\r\n"), 0x80); + return false; + } + for(; countDown && (*pcntdn); countDown--, (*pcntdn)--, (*pp)++) + pBuf[valueSize - countDown] = (**pp); + + if(countDown) + return false; + + countDown = valueSize; + return true; + } }; + class ByteSkipper { uint8_t *pBuf; uint8_t nStage; @@ -77,64 +91,4 @@ public: }; }; -// Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser -typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me); - -class PTPListParser { -public: - - enum ParseMode { - modeArray, modeRange/*, modeEnum*/ - }; - -private: - uint8_t nStage; - uint8_t enStage; - - uint32_t arLen; - uint32_t arLenCntdn; - - uint8_t lenSize; // size of the array length field in bytes - uint8_t valSize; // size of the array element in bytes - - MultiValueBuffer *pBuf; - - // The only parser for both size and array element parsing - MultiByteValueParser theParser; - - uint8_t /*ParseMode*/ prsMode; - -public: - - PTPListParser() : - nStage(0), - enStage(0), - arLen(0), - arLenCntdn(0), - lenSize(0), - valSize(0), - pBuf(NULL), - prsMode(modeArray) { - }; - - void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) { - pBuf = p; - lenSize = len_size; - valSize = val_size; - prsMode = mode; - - if(prsMode == modeRange) { - arLenCntdn = arLen = 3; - nStage = 2; - } else { - arLenCntdn = arLen = 0; - nStage = 0; - } - enStage = 0; - theParser.Initialize(p); - }; - - bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL); -}; - #endif // __PARSETOOLS_H__ diff --git a/Marlin/usb-flashdrive/lib/usb_ch9.h b/Marlin/usb-flashdrive/lib/usb_ch9.h index aded2152c..60f8a1144 100644 --- a/Marlin/usb-flashdrive/lib/usb_ch9.h +++ b/Marlin/usb-flashdrive/lib/usb_ch9.h @@ -147,20 +147,4 @@ typedef struct { uint8_t bInterval; // Polling interval in frames. } __attribute__((packed)) USB_ENDPOINT_DESCRIPTOR; -/* HID descriptor */ -typedef struct { - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdHID; // HID class specification release - uint8_t bCountryCode; - uint8_t bNumDescriptors; // Number of additional class specific descriptors - uint8_t bDescrType; // Type of class descriptor - uint16_t wDescriptorLength; // Total size of the Report descriptor -} __attribute__((packed)) USB_HID_DESCRIPTOR; - -typedef struct { - uint8_t bDescrType; // Type of class descriptor - uint16_t wDescriptorLength; // Total size of the Report descriptor -} __attribute__((packed)) HID_CLASS_DESCRIPTOR_LEN_AND_TYPE; - #endif // _ch9_h_