Browse Source

Marlin Color UI for STM32F1 (SPI) (#18958)

Victor Oliveira 4 years ago
parent
commit
2ef6c8ba97
No account linked to committer's email address

+ 1
- 5
Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h View File

@@ -26,12 +26,8 @@
26 26
   #undef SD_CHECK_AND_RETRY
27 27
 #endif
28 28
 
29
-#if HAS_SPI_TFT
30
-  #error "Sorry! SPI TFT displays are not available for HAL/STM32F1 (yet)."
31
-#endif
32
-
33 29
 // This platform has 'touch/xpt2046', not 'tft/xpt2046'
34
-#if ENABLED(TOUCH_SCREEN) && !HAS_FSMC_TFT
30
+#if ENABLED(TOUCH_SCREEN) && !HAS_FSMC_TFT && !HAS_SPI_TFT
35 31
   #undef TOUCH_SCREEN
36 32
   #undef TOUCH_SCREEN_CALIBRATION
37 33
   #define HAS_TOUCH_XPT2046 1

+ 144
- 0
Marlin/src/HAL/STM32F1/tft/tft_spi.cpp View File

@@ -0,0 +1,144 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "../../../inc/MarlinConfig.h"
24
+
25
+#if HAS_SPI_TFT
26
+
27
+#include "tft_spi.h"
28
+
29
+// TFT_SPI tft;
30
+
31
+SPIClass TFT_SPI::SPIx(1);
32
+
33
+#define SPI_TFT_CS_H  OUT_WRITE(TFT_CS_PIN, HIGH)
34
+#define SPI_TFT_CS_L  OUT_WRITE(TFT_CS_PIN, LOW)
35
+
36
+#define SPI_TFT_DC_H  OUT_WRITE(TFT_DC_PIN, HIGH)
37
+#define SPI_TFT_DC_L  OUT_WRITE(TFT_DC_PIN, LOW)
38
+
39
+#define SPI_TFT_RST_H OUT_WRITE(TFT_RST_PIN, HIGH)
40
+#define SPI_TFT_RST_L OUT_WRITE(TFT_RST_PIN, LOW)
41
+
42
+#define SPI_TFT_BLK_H OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH)
43
+#define SPI_TFT_BLK_L OUT_WRITE(TFT_BACKLIGHT_PIN, LOW)
44
+
45
+void TFT_SPI::Init() {
46
+  #if PIN_EXISTS(TFT_RESET)
47
+    // OUT_WRITE(TFT_RESET_PIN, HIGH);
48
+    SPI_TFT_RST_H;
49
+    delay(100);
50
+  #endif
51
+
52
+  #if PIN_EXISTS(TFT_BACKLIGHT)
53
+    // OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH);
54
+    SPI_TFT_BLK_H;
55
+  #endif
56
+
57
+  SPI_TFT_DC_H;
58
+  SPI_TFT_CS_H;
59
+
60
+  /**
61
+   * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
62
+   * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
63
+   * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
64
+   */
65
+  #if SPI_DEVICE == 1
66
+    #define SPI_CLOCK_MAX SPI_CLOCK_DIV4
67
+  #else
68
+    #define SPI_CLOCK_MAX SPI_CLOCK_DIV2
69
+  #endif
70
+  uint8_t  clock;
71
+  uint8_t spiRate = SPI_FULL_SPEED;
72
+  switch (spiRate) {
73
+    case SPI_FULL_SPEED:    clock = SPI_CLOCK_MAX ;  break;
74
+    case SPI_HALF_SPEED:    clock = SPI_CLOCK_DIV4 ; break;
75
+    case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break;
76
+    case SPI_EIGHTH_SPEED:  clock = SPI_CLOCK_DIV16; break;
77
+    case SPI_SPEED_5:       clock = SPI_CLOCK_DIV32; break;
78
+    case SPI_SPEED_6:       clock = SPI_CLOCK_DIV64; break;
79
+    default:                clock = SPI_CLOCK_DIV2;  // Default from the SPI library
80
+  }
81
+  SPIx.setModule(1);
82
+  SPIx.setClockDivider(clock);
83
+  SPIx.setBitOrder(MSBFIRST);
84
+  SPIx.setDataMode(SPI_MODE0);
85
+}
86
+
87
+void TFT_SPI::DataTransferBegin(uint16_t DataSize) {
88
+  SPIx.setDataSize(DataSize);
89
+  SPIx.begin();
90
+  SPI_TFT_CS_L;
91
+}
92
+
93
+uint32_t TFT_SPI::GetID() {
94
+  uint32_t id;
95
+  id = ReadID(LCD_READ_ID);
96
+
97
+  if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF)
98
+    id = ReadID(LCD_READ_ID4);
99
+  return id;
100
+}
101
+
102
+uint32_t TFT_SPI::ReadID(uint16_t Reg) {
103
+  #if !PIN_EXISTS(TFT_MISO)
104
+    return 0;
105
+  #else
106
+    uint16_t d = 0;
107
+    DataTransferBegin(DATASIZE_8BIT);
108
+    WriteReg(Reg);
109
+
110
+    SPI.read((uint8_t*)&d, 1); //dummy read
111
+    SPI.read((uint8_t*)&d, 1);
112
+
113
+    DataTransferEnd();
114
+
115
+    return d >> 7;
116
+  #endif
117
+}
118
+
119
+bool TFT_SPI::isBusy() {
120
+  return false;
121
+}
122
+
123
+void TFT_SPI::Abort() {
124
+  DataTransferEnd();
125
+}
126
+
127
+void TFT_SPI::Transmit(uint16_t Data) {
128
+  SPIx.send(Data);
129
+}
130
+
131
+void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) {
132
+  DataTransferBegin();
133
+  SPI_TFT_DC_H;
134
+  if (MemoryIncrease == DMA_MINC_ENABLE) {
135
+    SPIx.dmaSend(Data, Count, true);
136
+  }
137
+  else {
138
+    SPIx.dmaSend(Data, Count, false);
139
+  }
140
+
141
+  DataTransferEnd();
142
+}
143
+
144
+#endif // HAS_SPI_TFT

+ 65
- 0
Marlin/src/HAL/STM32F1/tft/tft_spi.h View File

@@ -0,0 +1,65 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "../../../inc/MarlinConfig.h"
25
+
26
+#include <SPI.h>
27
+
28
+#ifndef LCD_READ_ID
29
+  #define LCD_READ_ID 0x04   // Read display identification information (0xD3 on ILI9341)
30
+#endif
31
+#ifndef LCD_READ_ID4
32
+  #define LCD_READ_ID4 0xD3   // Read display identification information (0xD3 on ILI9341)
33
+#endif
34
+
35
+#define DATASIZE_8BIT    DATA_SIZE_8BIT
36
+#define DATASIZE_16BIT   DATA_SIZE_16BIT
37
+#define TFT_IO TFT_SPI
38
+
39
+#define DMA_MINC_ENABLE 1
40
+#define DMA_MINC_DISABLE 0
41
+
42
+class TFT_SPI {
43
+private:
44
+  static uint32_t ReadID(uint16_t Reg);
45
+  static void Transmit(uint16_t Data);
46
+  static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count);
47
+
48
+public:
49
+  static SPIClass SPIx;
50
+
51
+  static void Init();
52
+  static uint32_t GetID();
53
+  static bool isBusy();
54
+  static void Abort();
55
+
56
+  static void DataTransferBegin(uint16_t DataWidth = DATA_SIZE_16BIT);
57
+  static void DataTransferEnd() { WRITE(TFT_CS_PIN, HIGH); SPIx.end(); };
58
+  static void DataTransferAbort();
59
+
60
+  static void WriteData(uint16_t Data) { Transmit(Data); }
61
+  static void WriteReg(uint16_t Reg) { WRITE(TFT_A0_PIN, LOW); Transmit(Reg); WRITE(TFT_A0_PIN, HIGH); }
62
+
63
+  static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); }
64
+  static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
65
+};

+ 4
- 6
Marlin/src/HAL/STM32F1/tft/xpt2046.cpp View File

@@ -65,9 +65,7 @@ void XPT2046::Init() {
65 65
     SET_INPUT(TOUCH_INT_PIN);
66 66
   #endif
67 67
 
68
-  #if ENABLED(TOUCH_BUTTONS_HW_SPI)
69
-    touch_spi_init(SPI_SPEED_6);
70
-  #endif
68
+  TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6));
71 69
 
72 70
   // Read once to enable pendrive status pin
73 71
   getRawData(XPT2046_X);
@@ -95,12 +93,14 @@ uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) {
95 93
   uint16_t data[3];
96 94
 
97 95
   DataTransferBegin();
96
+  TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.begin());
98 97
 
99 98
   for (uint16_t i = 0; i < 3 ; i++) {
100 99
     IO(coordinate);
101 100
     data[i] = (IO() << 4) | (IO() >> 4);
102 101
   }
103 102
 
103
+  TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.end());
104 104
   DataTransferEnd();
105 105
 
106 106
   uint16_t delta01 = delta(data[0], data[1]),
@@ -114,14 +114,12 @@ uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) {
114 114
 }
115 115
 
116 116
 uint16_t XPT2046::IO(uint16_t data) {
117
-  TERN(TOUCH_BUTTONS_HW_SPI, HardwareIO, SoftwareIO)(data);
117
+  return TERN(TOUCH_BUTTONS_HW_SPI, HardwareIO, SoftwareIO)(data);
118 118
 }
119 119
 
120 120
 #if ENABLED(TOUCH_BUTTONS_HW_SPI)
121 121
   uint16_t XPT2046::HardwareIO(uint16_t data) {
122
-    SPIx.begin();
123 122
     uint16_t result = SPIx.transfer(data);
124
-    SPIx.end();
125 123
     return result;
126 124
   }
127 125
 #endif

+ 3
- 1
Marlin/src/lcd/tft/ili9488.h View File

@@ -39,7 +39,9 @@
39 39
 #define ILI9488_ORIENTATION_DOWN  ILI9488_MADCTL_MX                                         // 320x480 ; Cable on the upper side
40 40
 
41 41
 #define ILI9488_COLOR_BGR
42
-#define ILI9488_ORIENTATION       ILI9488_ORIENTATION_LEFT
42
+#ifndef ILI9488_ORIENTATION
43
+  #define ILI9488_ORIENTATION     ILI9488_ORIENTATION_LEFT
44
+#endif
43 45
 #define ILI9488_MADCTL_DATA       (ILI9488_ORIENTATION | TERN(ILI9488_COLOR_BGR, ILI9488_MADCTL_BGR, ILI9488_MADCTL_RGB))
44 46
 
45 47
 #define ILI9488_NOP               0x00 // No Operation

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

@@ -225,10 +225,10 @@
225 225
   #define FSMC_DMA_DEV                      DMA2
226 226
   #define FSMC_DMA_CHANNEL               DMA_CH5
227 227
 
228
-  #define XPT2046_X_CALIBRATION           -17181
229
-  #define XPT2046_Y_CALIBRATION            11434
230
-  #define XPT2046_X_OFFSET                   501
231
-  #define XPT2046_Y_OFFSET                    -9
228
+  #define XPT2046_X_CALIBRATION            17880
229
+  #define XPT2046_Y_CALIBRATION           -12234
230
+  #define XPT2046_X_OFFSET                   -45
231
+  #define XPT2046_Y_OFFSET                   349
232 232
 
233 233
   #define TOUCH_CS_PIN                      PA7   // SPI2_NSS
234 234
   #define TOUCH_SCK_PIN                     PB13   // SPI2_SCK
@@ -237,6 +237,7 @@
237 237
 
238 238
   #define TFT_DRIVER                        ILI9488
239 239
   #define TFT_BUFFER_SIZE                   14400
240
+  #define ILI9488_ORIENTATION               ILI9488_MADCTL_MX | ILI9488_MADCTL_MV
240 241
 #endif
241 242
 
242 243
 #define SPI_FLASH

+ 31
- 0
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_NANO_V2.h View File

@@ -344,6 +344,37 @@
344 344
     #define MKS_LCD12864B
345 345
     #undef SHOW_BOOTSCREEN
346 346
 
347
+  #elif ENABLED(TFT_480x320_SPI)
348
+    #define TFT_CS_PIN                      PD11
349
+    #define TFT_SCK_PIN                     PA5
350
+    #define TFT_MISO_PIN                    PA6
351
+    #define TFT_MOSI_PIN                    PA7
352
+    #define TFT_DC_PIN                      PD10
353
+    #define TFT_RST_PIN                     PC6
354
+    #define TFT_A0_PIN                TFT_DC_PIN
355
+
356
+    #define TFT_RESET_PIN                   PC6
357
+    #define TFT_BACKLIGHT_PIN               PD13
358
+
359
+    #define XPT2046_X_CALIBRATION         -17253
360
+    #define XPT2046_Y_CALIBRATION          11579
361
+    #define XPT2046_X_OFFSET                 514
362
+    #define XPT2046_Y_OFFSET                 -24
363
+
364
+    #define TOUCH_CS_PIN                    PE14  // SPI1_NSS
365
+    #define TOUCH_SCK_PIN                   PA5   // SPI1_SCK
366
+    #define TOUCH_MISO_PIN                  PA6   // SPI1_MISO
367
+    #define TOUCH_MOSI_PIN                  PA7   // SPI1_MOSI
368
+
369
+    #define TFT_DRIVER                    ST7796
370
+    #define TFT_BUFFER_SIZE                14400
371
+
372
+    #define LCD_READ_ID                     0xD3
373
+    #define LCD_USE_DMA_SPI
374
+
375
+    #define TOUCH_BUTTONS_HW_SPI
376
+    #define TOUCH_BUTTONS_HW_SPI_DEVICE        1
377
+
347 378
   #else                                           // !MKS_MINI_12864
348 379
 
349 380
     #define LCD_PINS_D4                     PE14

Loading…
Cancel
Save