2021-02-02 17:55:11 -03:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
* Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech]
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && HAS_SD_HOST_DRIVE
|
|
|
|
|
|
|
|
#include "msc_sd.h"
|
|
|
|
#include "../shared/Marduino.h"
|
|
|
|
#include "usbd_core.h"
|
|
|
|
#include <USB.h>
|
|
|
|
#include <USBMscHandler.h>
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 512
|
|
|
|
#define PRODUCT_ID 0x29
|
|
|
|
|
|
|
|
#include "../../sd/cardreader.h"
|
|
|
|
|
|
|
|
class Sd2CardUSBMscHandler : public USBMscHandler {
|
|
|
|
public:
|
2021-04-13 19:34:19 -03:00
|
|
|
DiskIODriver* diskIODriver() {
|
|
|
|
#if ENABLED(MULTI_VOLUME)
|
|
|
|
#if SHARED_VOLUME_IS(SD_ONBOARD)
|
2021-06-14 02:39:16 -03:00
|
|
|
return &card.media_driver_sdcard;
|
2021-04-13 19:34:19 -03:00
|
|
|
#elif SHARED_VOLUME_IS(USB_FLASH_DRIVE)
|
2021-06-14 02:39:16 -03:00
|
|
|
return &card.media_driver_usbFlash;
|
2021-04-13 19:34:19 -03:00
|
|
|
#endif
|
|
|
|
#else
|
2021-05-02 18:06:44 -03:00
|
|
|
return card.diskIODriver();
|
2021-04-13 19:34:19 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:55:11 -03:00
|
|
|
bool GetCapacity(uint32_t *pBlockNum, uint16_t *pBlockSize) {
|
2021-04-13 19:34:19 -03:00
|
|
|
*pBlockNum = diskIODriver()->cardSize();
|
2021-02-02 17:55:11 -03:00
|
|
|
*pBlockSize = BLOCK_SIZE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Write(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
|
2021-04-13 19:34:19 -03:00
|
|
|
auto sd2card = diskIODriver();
|
2021-02-02 17:55:11 -03:00
|
|
|
// single block
|
|
|
|
if (blkLen == 1) {
|
|
|
|
watchdog_refresh();
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->writeBlock(blkAddr, pBuf);
|
2021-02-02 17:55:11 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi block optmization
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->writeStart(blkAddr, blkLen);
|
2021-02-02 17:55:11 -03:00
|
|
|
while (blkLen--) {
|
|
|
|
watchdog_refresh();
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->writeData(pBuf);
|
2021-02-02 17:55:11 -03:00
|
|
|
pBuf += BLOCK_SIZE;
|
|
|
|
}
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->writeStop();
|
2021-02-02 17:55:11 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
|
2021-04-13 19:34:19 -03:00
|
|
|
auto sd2card = diskIODriver();
|
2021-02-02 17:55:11 -03:00
|
|
|
// single block
|
|
|
|
if (blkLen == 1) {
|
|
|
|
watchdog_refresh();
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->readBlock(blkAddr, pBuf);
|
2021-02-02 17:55:11 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi block optmization
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->readStart(blkAddr);
|
2021-02-02 17:55:11 -03:00
|
|
|
while (blkLen--) {
|
|
|
|
watchdog_refresh();
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->readData(pBuf);
|
2021-02-02 17:55:11 -03:00
|
|
|
pBuf += BLOCK_SIZE;
|
|
|
|
}
|
2021-04-13 19:34:19 -03:00
|
|
|
sd2card->readStop();
|
2021-02-02 17:55:11 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReady() {
|
2021-04-13 19:34:19 -03:00
|
|
|
return diskIODriver()->isReady();
|
2021-02-02 17:55:11 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Sd2CardUSBMscHandler usbMscHandler;
|
|
|
|
|
|
|
|
/* USB Mass storage Standard Inquiry Data */
|
|
|
|
uint8_t Marlin_STORAGE_Inquirydata[] = { /* 36 */
|
|
|
|
/* LUN 0 */
|
|
|
|
0x00,
|
|
|
|
0x80,
|
|
|
|
0x02,
|
|
|
|
0x02,
|
|
|
|
(STANDARD_INQUIRY_DATA_LEN - 5),
|
|
|
|
0x00,
|
|
|
|
0x00,
|
|
|
|
0x00,
|
|
|
|
'M', 'A', 'R', 'L', 'I', 'N', ' ', ' ', /* Manufacturer : 8 bytes */
|
|
|
|
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
'0', '.', '0', '1', /* Version : 4 Bytes */
|
|
|
|
};
|
|
|
|
|
|
|
|
USBMscHandler *pSingleMscHandler = &usbMscHandler;
|
|
|
|
|
|
|
|
void MSC_SD_init() {
|
|
|
|
USBDevice.end();
|
|
|
|
delay(200);
|
|
|
|
USBDevice.registerMscHandlers(1, &pSingleMscHandler, Marlin_STORAGE_Inquirydata);
|
2021-04-13 19:34:19 -03:00
|
|
|
USBDevice.begin();
|
2021-02-02 17:55:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // __STM32F1__ && HAS_SD_HOST_DRIVE
|