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

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