My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

msc_sd.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech]
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. *
  15. */
  16. #include "../platforms.h"
  17. #ifdef HAL_STM32
  18. #include "../../inc/MarlinConfigPre.h"
  19. #if HAS_SD_HOST_DRIVE
  20. #include "../shared/Marduino.h"
  21. #include "msc_sd.h"
  22. #include "usbd_core.h"
  23. #include "../../sd/cardreader.h"
  24. #include <USB.h>
  25. #include <USBMscHandler.h>
  26. #define BLOCK_SIZE 512
  27. #define PRODUCT_ID 0x29
  28. class Sd2CardUSBMscHandler : public USBMscHandler {
  29. public:
  30. DiskIODriver* diskIODriver() {
  31. #if ENABLED(MULTI_VOLUME)
  32. #if SHARED_VOLUME_IS(SD_ONBOARD)
  33. return &card.media_driver_sdcard;
  34. #elif SHARED_VOLUME_IS(USB_FLASH_DRIVE)
  35. return &card.media_driver_usbFlash;
  36. #endif
  37. #else
  38. return card.diskIODriver();
  39. #endif
  40. }
  41. bool GetCapacity(uint32_t *pBlockNum, uint16_t *pBlockSize) {
  42. *pBlockNum = diskIODriver()->cardSize();
  43. *pBlockSize = BLOCK_SIZE;
  44. return true;
  45. }
  46. bool Write(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
  47. auto sd2card = diskIODriver();
  48. // single block
  49. if (blkLen == 1) {
  50. hal.watchdog_refresh();
  51. sd2card->writeBlock(blkAddr, pBuf);
  52. return true;
  53. }
  54. // multi block optimization
  55. sd2card->writeStart(blkAddr, blkLen);
  56. while (blkLen--) {
  57. hal.watchdog_refresh();
  58. sd2card->writeData(pBuf);
  59. pBuf += BLOCK_SIZE;
  60. }
  61. sd2card->writeStop();
  62. return true;
  63. }
  64. bool Read(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
  65. auto sd2card = diskIODriver();
  66. // single block
  67. if (blkLen == 1) {
  68. hal.watchdog_refresh();
  69. sd2card->readBlock(blkAddr, pBuf);
  70. return true;
  71. }
  72. // multi block optimization
  73. sd2card->readStart(blkAddr);
  74. while (blkLen--) {
  75. hal.watchdog_refresh();
  76. sd2card->readData(pBuf);
  77. pBuf += BLOCK_SIZE;
  78. }
  79. sd2card->readStop();
  80. return true;
  81. }
  82. bool IsReady() {
  83. return diskIODriver()->isReady();
  84. }
  85. };
  86. Sd2CardUSBMscHandler usbMscHandler;
  87. /* USB Mass storage Standard Inquiry Data */
  88. uint8_t Marlin_STORAGE_Inquirydata[] = { /* 36 */
  89. /* LUN 0 */
  90. 0x00,
  91. 0x80,
  92. 0x02,
  93. 0x02,
  94. (STANDARD_INQUIRY_DATA_LEN - 5),
  95. 0x00,
  96. 0x00,
  97. 0x00,
  98. 'M', 'A', 'R', 'L', 'I', 'N', ' ', ' ', /* Manufacturer : 8 bytes */
  99. 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
  100. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  101. '0', '.', '0', '1', /* Version : 4 Bytes */
  102. };
  103. USBMscHandler *pSingleMscHandler = &usbMscHandler;
  104. void MSC_SD_init() {
  105. USBDevice.end();
  106. delay(200);
  107. USBDevice.registerMscHandlers(1, &pSingleMscHandler, Marlin_STORAGE_Inquirydata);
  108. USBDevice.begin();
  109. }
  110. #endif // HAS_SD_HOST_DRIVE
  111. #endif // HAL_STM32