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.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 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 "../../inc/MarlinConfigPre.h"
  17. #if defined(__STM32F1__) && HAS_SD_HOST_DRIVE
  18. #include "msc_sd.h"
  19. #include "SPI.h"
  20. #include "usb_reg_map.h"
  21. #define PRODUCT_ID 0x29
  22. USBMassStorage MarlinMSC;
  23. Serial1Class<USBCompositeSerial> MarlinCompositeSerial(true);
  24. #include "../../inc/MarlinConfig.h"
  25. #if SD_CONNECTION_IS(ONBOARD)
  26. #include "onboard_sd.h"
  27. static bool MSC_Write(const uint8_t *writebuff, uint32_t startSector, uint16_t numSectors) {
  28. return (disk_write(0, writebuff, startSector, numSectors) == RES_OK);
  29. }
  30. static bool MSC_Read(uint8_t *readbuff, uint32_t startSector, uint16_t numSectors) {
  31. return (disk_read(0, readbuff, startSector, numSectors) == RES_OK);
  32. }
  33. #endif
  34. #if ENABLED(EMERGENCY_PARSER)
  35. // The original callback is not called (no way to retrieve address).
  36. // That callback detects a special STM32 reset sequence: this functionality is not essential
  37. // as M997 achieves the same.
  38. void my_rx_callback(unsigned int, void*) {
  39. // max length of 16 is enough to contain all emergency commands
  40. uint8 buf[16];
  41. //rx is usbSerialPart.endpoints[2]
  42. uint16 len = usb_get_ep_rx_count(usbSerialPart.endpoints[2].address);
  43. uint32 total = composite_cdcacm_data_available();
  44. if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
  45. return;
  46. // cannot get character by character due to bug in composite_cdcacm_peek_ex
  47. len = composite_cdcacm_peek(buf, total);
  48. for (uint32 i = 0; i < len; i++)
  49. emergency_parser.update(MarlinCompositeSerial.emergency_state, buf[i+total-len]);
  50. }
  51. #endif
  52. void MSC_SD_init() {
  53. USBComposite.setProductId(PRODUCT_ID);
  54. // Just set MarlinCompositeSerial enabled to true
  55. // because when MarlinCompositeSerial.begin() is used in setup()
  56. // it clears all USBComposite devices.
  57. MarlinCompositeSerial.begin();
  58. USBComposite.end();
  59. USBComposite.clear();
  60. // Set api and register mass storage
  61. #if SD_CONNECTION_IS(ONBOARD)
  62. uint32_t cardSize;
  63. if (disk_initialize(0) == RES_OK) {
  64. if (disk_ioctl(0, GET_SECTOR_COUNT, (void *)(&cardSize)) == RES_OK) {
  65. MarlinMSC.setDriveData(0, cardSize, MSC_Read, MSC_Write);
  66. MarlinMSC.registerComponent();
  67. }
  68. }
  69. #endif
  70. // Register composite Serial
  71. MarlinCompositeSerial.registerComponent();
  72. USBComposite.begin();
  73. #if ENABLED(EMERGENCY_PARSER)
  74. composite_cdcacm_set_hooks(USBHID_CDCACM_HOOK_RX, my_rx_callback);
  75. #endif
  76. }
  77. #endif // __STM32F1__ && HAS_SD_HOST_DRIVE