Browse Source

⚡️ SPI+DMA+interrupt method (STM32 / MKS UI) (#23464)

Sola 2 years ago
parent
commit
c79174e862
No account linked to committer's email address

+ 24
- 0
Marlin/src/HAL/STM32/tft/tft_spi.cpp View File

242
   Abort();
242
   Abort();
243
 }
243
 }
244
 
244
 
245
+#if ENABLED(USE_SPI_DMA_TC)
246
+
247
+  void TFT_SPI::TransmitDMA_IT(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) {
248
+
249
+    DMAtx.Init.MemInc = MemoryIncrease;
250
+    HAL_DMA_Init(&DMAtx);
251
+
252
+    if (TFT_MISO_PIN == TFT_MOSI_PIN)
253
+      SPI_1LINE_TX(&SPIx);
254
+
255
+    DataTransferBegin();
256
+
257
+    HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 5, 0);
258
+    HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
259
+    HAL_DMA_Start_IT(&DMAtx, (uint32_t)Data, (uint32_t)&(SPIx.Instance->DR), Count);
260
+    __HAL_SPI_ENABLE(&SPIx);
261
+
262
+    SET_BIT(SPIx.Instance->CR2, SPI_CR2_TXDMAEN);   // Enable Tx DMA Request
263
+  }
264
+
265
+  extern "C" void DMA2_Stream3_IRQHandler(void) { HAL_DMA_IRQHandler(&TFT_SPI::DMAtx); }
266
+
267
+#endif
268
+
245
 #endif // HAS_SPI_TFT
269
 #endif // HAS_SPI_TFT
246
 #endif // HAL_STM32
270
 #endif // HAL_STM32

+ 14
- 4
Marlin/src/HAL/STM32/tft/tft_spi.h View File

36
   #define LCD_READ_ID4 0xD3   // Read display identification information (0xD3 on ILI9341)
36
   #define LCD_READ_ID4 0xD3   // Read display identification information (0xD3 on ILI9341)
37
 #endif
37
 #endif
38
 
38
 
39
-#define DATASIZE_8BIT    SPI_DATASIZE_8BIT
40
-#define DATASIZE_16BIT   SPI_DATASIZE_16BIT
41
-#define TFT_IO_DRIVER TFT_SPI
39
+#define DATASIZE_8BIT  SPI_DATASIZE_8BIT
40
+#define DATASIZE_16BIT SPI_DATASIZE_16BIT
41
+#define TFT_IO_DRIVER  TFT_SPI
42
 
42
 
43
 class TFT_SPI {
43
 class TFT_SPI {
44
 private:
44
 private:
45
   static SPI_HandleTypeDef SPIx;
45
   static SPI_HandleTypeDef SPIx;
46
-  static DMA_HandleTypeDef DMAtx;
46
+
47
 
47
 
48
   static uint32_t ReadID(uint16_t Reg);
48
   static uint32_t ReadID(uint16_t Reg);
49
   static void Transmit(uint16_t Data);
49
   static void Transmit(uint16_t Data);
50
   static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count);
50
   static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count);
51
+  #if ENABLED(USE_SPI_DMA_TC)
52
+    static void TransmitDMA_IT(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count);
53
+  #endif
51
 
54
 
52
 public:
55
 public:
56
+  static DMA_HandleTypeDef DMAtx;
57
+
53
   static void Init();
58
   static void Init();
54
   static uint32_t GetID();
59
   static uint32_t GetID();
55
   static bool isBusy();
60
   static bool isBusy();
63
   static void WriteReg(uint16_t Reg) { WRITE(TFT_A0_PIN, LOW); Transmit(Reg); WRITE(TFT_A0_PIN, HIGH); }
68
   static void WriteReg(uint16_t Reg) { WRITE(TFT_A0_PIN, LOW); Transmit(Reg); WRITE(TFT_A0_PIN, HIGH); }
64
 
69
 
65
   static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); }
70
   static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); }
71
+
72
+  #if ENABLED(USE_SPI_DMA_TC)
73
+    static void WriteSequenceIT(uint16_t *Data, uint16_t Count) { TransmitDMA_IT(DMA_MINC_ENABLE, Data, Count); }
74
+  #endif
75
+
66
   static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
76
   static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
67
   static void WriteMultiple(uint16_t Color, uint32_t Count) {
77
   static void WriteMultiple(uint16_t Color, uint32_t Count) {
68
     static uint16_t Data; Data = Color;
78
     static uint16_t Data; Data = Color;

+ 3
- 2
Marlin/src/lcd/extui/mks_ui/draw_ui.cpp View File

1331
   lv_label_set_text((lv_obj_t*)btn->child_ll.head, curValue ? machine_menu.enable : machine_menu.disable);
1331
   lv_label_set_text((lv_obj_t*)btn->child_ll.head, curValue ? machine_menu.enable : machine_menu.disable);
1332
 }
1332
 }
1333
 
1333
 
1334
-
1335
 #if ENABLED(SDSUPPORT)
1334
 #if ENABLED(SDSUPPORT)
1336
 
1335
 
1337
   void sd_detection() {
1336
   void sd_detection() {
1360
 }
1359
 }
1361
 
1360
 
1362
 void LV_TASK_HANDLER() {
1361
 void LV_TASK_HANDLER() {
1363
-  lv_task_handler();
1362
+
1363
+  if (TERN1(USE_SPI_DMA_TC, !get_lcd_dma_lock()))
1364
+    lv_task_handler();
1364
 
1365
 
1365
   #if BOTH(MKS_TEST, SDSUPPORT)
1366
   #if BOTH(MKS_TEST, SDSUPPORT)
1366
     if (mks_test_flag == 0x1E) mks_hardware_test();
1367
     if (mks_test_flag == 0x1E) mks_hardware_test();

+ 49
- 3
Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp View File

133
 
133
 
134
   watchdog_refresh();     // LVGL init takes time
134
   watchdog_refresh();     // LVGL init takes time
135
 
135
 
136
+  #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
137
+    uint16_t usb_flash_loop = 1000;
138
+    #if ENABLED(MULTI_VOLUME) && !HAS_SD_HOST_DRIVE
139
+      SET_INPUT_PULLUP(SD_DETECT_PIN);
140
+      if (READ(SD_DETECT_PIN) == LOW) card.changeMedia(&card.media_driver_sdcard);
141
+      else card.changeMedia(&card.media_driver_usbFlash);
142
+    #endif
143
+    do {
144
+      card.media_driver_usbFlash.idle();
145
+      watchdog_refresh();
146
+      delay(2);
147
+    } while (!card.media_driver_usbFlash.isInserted() && usb_flash_loop--);
148
+    card.mount();
149
+  #elif HAS_LOGO_IN_FLASH
150
+    delay(1000);
151
+    watchdog_refresh();
152
+    delay(1000);
153
+  #endif
154
+
155
+  watchdog_refresh();     // LVGL init takes time
156
+
136
   #if ENABLED(SDSUPPORT)
157
   #if ENABLED(SDSUPPORT)
137
     UpdateAssets();
158
     UpdateAssets();
138
     watchdog_refresh();   // LVGL init takes time
159
     watchdog_refresh();   // LVGL init takes time
231
   #endif
252
   #endif
232
 }
253
 }
233
 
254
 
255
+static lv_disp_drv_t* disp_drv_p;
256
+
257
+#if ENABLED(USE_SPI_DMA_TC)
258
+  bool lcd_dma_trans_lock = false;
259
+#endif
260
+
261
+void dmc_tc_handler(struct __DMA_HandleTypeDef * hdma) {
262
+  #if ENABLED(USE_SPI_DMA_TC)
263
+    lv_disp_flush_ready(disp_drv_p);
264
+    lcd_dma_trans_lock = false;
265
+    TFT_SPI::Abort();
266
+  #endif
267
+}
268
+
234
 void my_disp_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) {
269
 void my_disp_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) {
235
   uint16_t width = area->x2 - area->x1 + 1,
270
   uint16_t width = area->x2 - area->x1 + 1,
236
           height = area->y2 - area->y1 + 1;
271
           height = area->y2 - area->y1 + 1;
237
 
272
 
238
-  SPI_TFT.setWindow((uint16_t)area->x1, (uint16_t)area->y1, width, height);
273
+  TERN_(USE_SPI_DMA_TC, disp_drv_p = disp);
239
 
274
 
240
-  SPI_TFT.tftio.WriteSequence((uint16_t*)color_p, width * height);
275
+  SPI_TFT.setWindow((uint16_t)area->x1, (uint16_t)area->y1, width, height);
241
 
276
 
242
-  lv_disp_flush_ready(disp); // Indicate you are ready with the flushing
277
+  #if ENABLED(USE_SPI_DMA_TC)
278
+    lcd_dma_trans_lock = true;
279
+    SPI_TFT.tftio.WriteSequenceIT((uint16_t*)color_p, width * height);
280
+    TFT_SPI::DMAtx.XferCpltCallback = dmc_tc_handler;
281
+  #else
282
+    SPI_TFT.tftio.WriteSequence((uint16_t*)color_p, width * height);
283
+    lv_disp_flush_ready(disp_drv_p); // Indicate you are ready with the flushing
284
+  #endif
243
 
285
 
244
   W25QXX.init(SPI_QUARTER_SPEED);
286
   W25QXX.init(SPI_QUARTER_SPEED);
245
 }
287
 }
246
 
288
 
289
+#if ENABLED(USE_SPI_DMA_TC)
290
+  bool get_lcd_dma_lock() { return lcd_dma_trans_lock; }
291
+#endif
292
+
247
 void lv_fill_rect(lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2, lv_color_t bk_color) {
293
 void lv_fill_rect(lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2, lv_color_t bk_color) {
248
   uint16_t width, height;
294
   uint16_t width, height;
249
   width = x2 - x1 + 1;
295
   width = x2 - x1 + 1;

+ 2
- 0
Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.h View File

64
 
64
 
65
 void lv_fill_rect(lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2, lv_color_t bk_color);
65
 void lv_fill_rect(lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2, lv_color_t bk_color);
66
 
66
 
67
+bool get_lcd_dma_lock();
68
+
67
 #ifdef __cplusplus
69
 #ifdef __cplusplus
68
   } /* C-declarations for C++ */
70
   } /* C-declarations for C++ */
69
 #endif
71
 #endif

+ 5
- 0
Marlin/src/lcd/tft_io/tft_io.h View File

121
   inline static void WriteReg(uint16_t Reg) { io.WriteReg(Reg); };
121
   inline static void WriteReg(uint16_t Reg) { io.WriteReg(Reg); };
122
 
122
 
123
   inline static void WriteSequence(uint16_t *Data, uint16_t Count) { io.WriteSequence(Data, Count); };
123
   inline static void WriteSequence(uint16_t *Data, uint16_t Count) { io.WriteSequence(Data, Count); };
124
+
125
+  #if ENABLED(USE_SPI_DMA_TC)
126
+    inline static void WriteSequenceIT(uint16_t *Data, uint16_t Count) { io.WriteSequenceIT(Data, Count); };
127
+  #endif
128
+
124
   // static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
129
   // static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
125
   inline static void WriteMultiple(uint16_t Color, uint32_t Count) { io.WriteMultiple(Color, Count); };
130
   inline static void WriteMultiple(uint16_t Color, uint32_t Count) { io.WriteMultiple(Color, Count); };
126
 
131
 

+ 4
- 0
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_NANO.h View File

44
 
44
 
45
 //#define LED_PIN                           PB2
45
 //#define LED_PIN                           PB2
46
 
46
 
47
+#if HAS_TFT_LVGL_UI
48
+  #define BOARD_INIT OUT_WRITE(PB0, LOW)
49
+#endif
50
+
47
 #include "pins_MKS_ROBIN_NANO_common.h"
51
 #include "pins_MKS_ROBIN_NANO_common.h"
48
 
52
 
49
 #if HAS_TFT_LVGL_UI && FAN1_PIN != PB0 && HEATER_1_PIN != PB0
53
 #if HAS_TFT_LVGL_UI && FAN1_PIN != PB0 && HEATER_1_PIN != PB0

+ 9
- 3
Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3_common.h View File

366
 
366
 
367
 #endif // HAS_WIRED_LCD
367
 #endif // HAS_WIRED_LCD
368
 
368
 
369
+#if HAS_TFT_LVGL_UI
370
+  #define USE_SPI_DMA_TC
371
+#endif
372
+
369
 #if ANY(TFT_COLOR_UI, TFT_LVGL_UI, TFT_CLASSIC_UI, HAS_WIRED_LCD)
373
 #if ANY(TFT_COLOR_UI, TFT_LVGL_UI, TFT_CLASSIC_UI, HAS_WIRED_LCD)
370
   #define BEEPER_PIN                 EXP1_10_PIN
374
   #define BEEPER_PIN                 EXP1_10_PIN
371
-  #define BTN_EN1                    EXP2_08_PIN
372
-  #define BTN_EN2                    EXP2_06_PIN
373
-  #define BTN_ENC                    EXP1_09_PIN
375
+  #if DISABLED(USE_SPI_DMA_TC)
376
+    #define BTN_EN1                  EXP2_08_PIN
377
+    #define BTN_EN2                  EXP2_06_PIN
378
+    #define BTN_ENC                  EXP1_09_PIN
379
+  #endif
374
 #endif
380
 #endif

Loading…
Cancel
Save