Ver código fonte

STM32 Shared Media - USB Mass Storage Device (#20956)

Victor Oliveira 3 anos atrás
pai
commit
28b8bf566b
Nenhuma conta vinculada ao e-mail do autor do commit

+ 18
- 0
Marlin/src/HAL/STM32/HAL.cpp Ver arquivo

@@ -42,6 +42,11 @@
42 42
   #endif
43 43
 #endif
44 44
 
45
+#if HAS_SD_HOST_DRIVE
46
+  #include "msc_sd.h"
47
+  #include "usbd_cdc_if.h"
48
+#endif
49
+
45 50
 // ------------------------
46 51
 // Public Variables
47 52
 // ------------------------
@@ -88,6 +93,19 @@ void HAL_init() {
88 93
   #if ENABLED(EMERGENCY_PARSER) && USBD_USE_CDC
89 94
     USB_Hook_init();
90 95
   #endif
96
+
97
+  #if HAS_SD_HOST_DRIVE
98
+    MSC_SD_init();                         // Enable USB SD card access
99
+  #endif
100
+}
101
+
102
+// HAL idle task
103
+void HAL_idletask() {
104
+  #if HAS_SHARED_MEDIA
105
+    // Stm32duino currently doesn't have a "loop/idle" method
106
+    CDC_resume_receive();
107
+    CDC_continue_transmit();
108
+  #endif
91 109
 }
92 110
 
93 111
 void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }

+ 2
- 0
Marlin/src/HAL/STM32/HAL.h Ver arquivo

@@ -135,6 +135,8 @@ extern uint16_t HAL_adc_result;
135 135
 
136 136
 // Enable hooks into  setup for HAL
137 137
 void HAL_init();
138
+#define HAL_IDLETASK 1
139
+void HAL_idletask();
138 140
 
139 141
 // Clear reset reason
140 142
 void HAL_clear_reset_source();

+ 1
- 1
Marlin/src/HAL/STM32/inc/Conditionals_adv.h Ver arquivo

@@ -21,6 +21,6 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-#if defined(USBD_USE_CDC_COMPOSITE) && DISABLED(NO_SD_HOST_DRIVE)
24
+#if defined(USBD_USE_CDC_MSC) && DISABLED(NO_SD_HOST_DRIVE)
25 25
   #define HAS_SD_HOST_DRIVE 1
26 26
 #endif

+ 112
- 0
Marlin/src/HAL/STM32/msc_sd.cpp Ver arquivo

@@ -0,0 +1,112 @@
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 "../../inc/MarlinConfigPre.h"
17
+
18
+#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && HAS_SD_HOST_DRIVE
19
+
20
+#include "msc_sd.h"
21
+#include "../shared/Marduino.h"
22
+#include "usbd_core.h"
23
+#include <USB.h>
24
+#include <USBMscHandler.h>
25
+
26
+#define BLOCK_SIZE 512
27
+#define PRODUCT_ID 0x29
28
+
29
+#include "../../sd/cardreader.h"
30
+
31
+class Sd2CardUSBMscHandler : public USBMscHandler {
32
+public:
33
+  bool GetCapacity(uint32_t *pBlockNum, uint16_t *pBlockSize) {
34
+    *pBlockNum = card.getSd2Card().cardSize();
35
+    *pBlockSize = BLOCK_SIZE;
36
+    return true;
37
+  }
38
+
39
+  bool Write(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
40
+    auto sd2card = card.getSd2Card();
41
+    // single block
42
+    if (blkLen == 1) {
43
+      watchdog_refresh();
44
+      sd2card.writeBlock(blkAddr, pBuf);
45
+      return true;
46
+    }
47
+
48
+    // multi block optmization
49
+    sd2card.writeStart(blkAddr, blkLen);
50
+    while (blkLen--) {
51
+      watchdog_refresh();
52
+      sd2card.writeData(pBuf);
53
+      pBuf += BLOCK_SIZE;
54
+    }
55
+    sd2card.writeStop();
56
+    return true;
57
+  }
58
+
59
+  bool Read(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
60
+    auto sd2card = card.getSd2Card();
61
+    // single block
62
+    if (blkLen == 1) {
63
+      watchdog_refresh();
64
+      sd2card.readBlock(blkAddr, pBuf);
65
+      return true;
66
+    }
67
+
68
+    // multi block optmization
69
+    sd2card.readStart(blkAddr);
70
+    while (blkLen--) {
71
+      watchdog_refresh();
72
+      sd2card.readData(pBuf);
73
+      pBuf += BLOCK_SIZE;
74
+    }
75
+    sd2card.readStop();
76
+    return true;
77
+  }
78
+
79
+  bool IsReady() {
80
+    return card.isMounted();
81
+  }
82
+};
83
+
84
+Sd2CardUSBMscHandler usbMscHandler;
85
+
86
+/* USB Mass storage Standard Inquiry Data */
87
+uint8_t  Marlin_STORAGE_Inquirydata[] = { /* 36 */
88
+  /* LUN 0 */
89
+  0x00,
90
+  0x80,
91
+  0x02,
92
+  0x02,
93
+  (STANDARD_INQUIRY_DATA_LEN - 5),
94
+  0x00,
95
+  0x00,
96
+  0x00,
97
+  'M', 'A', 'R', 'L', 'I', 'N', ' ', ' ', /* Manufacturer : 8 bytes */
98
+  'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product      : 16 Bytes */
99
+  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
100
+  '0', '.', '0', '1',                     /* Version      : 4 Bytes */
101
+};
102
+
103
+USBMscHandler *pSingleMscHandler = &usbMscHandler;
104
+
105
+void MSC_SD_init() {
106
+  USBDevice.end();
107
+  delay(200);
108
+  USBDevice.begin();
109
+  USBDevice.registerMscHandlers(1, &pSingleMscHandler, Marlin_STORAGE_Inquirydata);
110
+}
111
+
112
+#endif // __STM32F1__ && HAS_SD_HOST_DRIVE

+ 18
- 0
Marlin/src/HAL/STM32/msc_sd.h Ver arquivo

@@ -0,0 +1,18 @@
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
+#pragma once
17
+
18
+void MSC_SD_init();

+ 16
- 0
platformio.ini Ver arquivo

@@ -1525,6 +1525,22 @@ build_flags       = ${stm32_flash_drive.build_flags}
1525 1525
   -DUSBD_USE_CDC
1526 1526
 
1527 1527
 #
1528
+# MKS Robin Nano V3 with USB Flash Drive Support and Shared Media
1529
+# Currently, using a STM32duino fork, until USB Host and USB Device MSC get merged
1530
+#
1531
+[env:mks_robin_nano_v3_usb_flash_drive_msc]
1532
+extends           = env:mks_robin_nano_v3
1533
+platform_packages = framework-arduinoststm32@https://github.com/rhapsodyv/Arduino_Core_STM32/archive/usb-host-msc-cdc-msc.zip
1534
+build_unflags     = ${common_stm32.build_unflags} -DUSBD_USE_CDC
1535
+build_flags       = ${stm32_flash_drive.build_flags}
1536
+  -DUSBCON
1537
+  -DUSE_USBHOST_HS
1538
+  -DUSBD_IRQ_PRIO=5
1539
+  -DUSBD_IRQ_SUBPRIO=6
1540
+  -DUSE_USB_HS_IN_FS
1541
+  -DUSBD_USE_CDC_MSC
1542
+
1543
+#
1528 1544
 # Mingda MPX_ARM_MINI
1529 1545
 #
1530 1546
 

Carregando…
Cancelar
Salvar