-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathArduino-STM32-8bitTFT.h
277 lines (242 loc) · 9.42 KB
/
Arduino-STM32-8bitTFT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* 8bit TFT Library for Arduino_Core_STM32
*
* based on MCUFRIEND_kbv.cpp by David Prentice
* https://github.com/prenticedavid/MCUFRIEND_kbv
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _Arduino-STM32-8bitTFTH_
#define _Arduino-STM32-8bitTFTH_
#include "Arduino.h"
#include "Print.h"
#include <Adafruit_GFX.h>
/*
To use a different resolution TFT, change this or use setResolution().
*/
#define TFTWIDTH 240
#define TFTHEIGHT 320
// Color definitions
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGRAY 0xC618 /* 192, 192, 192 */
#define DARKGRAY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F
#define GRAY 0x5AEB
//#define GPIO_INTERFACE 0 // Use LL_GPIO_WriteOutputPort fuction
#define GPIO_INTERFACE 1 // Use Port output data (ODR) Register
/*
Define pins and Output Data Registers
*/
#define PORT_LOW 0
#define PORT_HIGH 1
#if 1
#define TFT_DATA GPIOA
#define TFT_PORT PORT_LOW
#define TFT_D0 LL_GPIO_PIN_0
#define TFT_D1 LL_GPIO_PIN_1
#define TFT_D2 LL_GPIO_PIN_2
#define TFT_D3 LL_GPIO_PIN_3
#define TFT_D4 LL_GPIO_PIN_4
#define TFT_D5 LL_GPIO_PIN_5
#define TFT_D6 LL_GPIO_PIN_6
#define TFT_D7 LL_GPIO_PIN_7
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PA7|PA6|PA5|PA4|PA3|PA2|PA1|PA0|
#endif
#if 0
#define TFT_DATA GPIOA
#define TFT_PORT PORT_HIGH
#define TFT_D0 LL_GPIO_PIN_8
#define TFT_D1 LL_GPIO_PIN_9
#define TFT_D2 LL_GPIO_PIN_10
#define TFT_D3 LL_GPIO_PIN_11
#define TFT_D4 LL_GPIO_PIN_12
#define TFT_D5 LL_GPIO_PIN_13
#define TFT_D6 LL_GPIO_PIN_14
#define TFT_D7 LL_GPIO_PIN_15
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PA15|PA14|PA13|PA12|PA11|PA10|PA9|PA8|
#endif
#if 0
#define TFT_DATA GPIOB
#define TFT_PORT PORT_LOW
#define TFT_D0 LL_GPIO_PIN_0
#define TFT_D1 LL_GPIO_PIN_1
#define TFT_D2 LL_GPIO_PIN_2
#define TFT_D3 LL_GPIO_PIN_3
#define TFT_D4 LL_GPIO_PIN_4
#define TFT_D5 LL_GPIO_PIN_5
#define TFT_D6 LL_GPIO_PIN_6
#define TFT_D7 LL_GPIO_PIN_7
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PB7|PB6|PB5|PB4|PB3|PB2|PB1|PB0|
#endif
#if 0
#define TFT_DATA GPIOB
#define TFT_PORT PORT_HIGH
#define TFT_D0 LL_GPIO_PIN_8
#define TFT_D1 LL_GPIO_PIN_9
#define TFT_D2 LL_GPIO_PIN_10
#define TFT_D3 LL_GPIO_PIN_11
#define TFT_D4 LL_GPIO_PIN_12
#define TFT_D5 LL_GPIO_PIN_13
#define TFT_D6 LL_GPIO_PIN_14
#define TFT_D7 LL_GPIO_PIN_15
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PB15|PB14|PB13|PB12|PB11|PB10|PB9|PB8|
#endif
#if 0
#define TFT_DATA GPIOC
#define TFT_PORT PORT_LOW
#define TFT_D0 LL_GPIO_PIN_0
#define TFT_D1 LL_GPIO_PIN_1
#define TFT_D2 LL_GPIO_PIN_2
#define TFT_D3 LL_GPIO_PIN_3
#define TFT_D4 LL_GPIO_PIN_4
#define TFT_D5 LL_GPIO_PIN_5
#define TFT_D6 LL_GPIO_PIN_6
#define TFT_D7 LL_GPIO_PIN_7
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PC7|PC6|PC5|PC4|PC3|PC2|PC1|PC0|
#endif
#if 0
#define TFT_DATA GPIOC
#define TFT_PORT PORT_HIGH
#define TFT_D0 LL_GPIO_PIN_8
#define TFT_D1 LL_GPIO_PIN_9
#define TFT_D2 LL_GPIO_PIN_10
#define TFT_D3 LL_GPIO_PIN_11
#define TFT_D4 LL_GPIO_PIN_12
#define TFT_D5 LL_GPIO_PIN_13
#define TFT_D6 LL_GPIO_PIN_14
#define TFT_D7 LL_GPIO_PIN_15
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PC15|PC14|PC13|PC12|PC11|PC10|PC9|PC8|
#endif
#if 0
#define TFT_DATA GPIOD
#define TFT_PORT PORT_LOW
#define TFT_D0 LL_GPIO_PIN_0
#define TFT_D1 LL_GPIO_PIN_1
#define TFT_D2 LL_GPIO_PIN_2
#define TFT_D3 LL_GPIO_PIN_3
#define TFT_D4 LL_GPIO_PIN_4
#define TFT_D5 LL_GPIO_PIN_5
#define TFT_D6 LL_GPIO_PIN_6
#define TFT_D7 LL_GPIO_PIN_7
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PD7|PD6|PD5|PD4|PD3|PD2|PD1|PD0|
#endif
#if 0
#define TFT_DATA GPIOD
#define TFT_PORT PORT_HIGH
#define TFT_D0 LL_GPIO_PIN_8
#define TFT_D1 LL_GPIO_PIN_9
#define TFT_D2 LL_GPIO_PIN_10
#define TFT_D3 LL_GPIO_PIN_11
#define TFT_D4 LL_GPIO_PIN_12
#define TFT_D5 LL_GPIO_PIN_13
#define TFT_D6 LL_GPIO_PIN_14
#define TFT_D7 LL_GPIO_PIN_15
//Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
//Pin stm32 |PD15|PD14|PD13|PD12|PD11|PD10|PD9|PD8|
#endif
#define TFT_CNTRL GPIOB
#define LL_LOW(LL_GPIO_PIN) LL_GPIO_WriteOutputPort(TFT_CNTRL, (LL_GPIO_ReadOutputPort(TFT_CNTRL) & ~(LL_GPIO_PIN)))
#define LL_HIGH(LL_GPIO_PIN) LL_GPIO_WriteOutputPort(TFT_CNTRL, (LL_GPIO_ReadOutputPort(TFT_CNTRL) | LL_GPIO_PIN))
// Note:
// PA15 PB3 PB4 is assigned to JTAG debug port by default on some boards.
// Therefore, it may not be available by default.
#define TFT_RD LL_GPIO_PIN_0 // PB0
#define TFT_WR LL_GPIO_PIN_1 // PB1
#define TFT_RS LL_GPIO_PIN_5 // PB5
#define TFT_CS LL_GPIO_PIN_6 // PB6
#define TFT_RST LL_GPIO_PIN_7 // PB7
//#define DELAY delayMicroseconds(10);
#define DELAY (void)0 // NOP
#define RD_ACTIVE LL_LOW(TFT_RD)
#define RD_IDLE LL_HIGH(TFT_RD)
#define WR_ACTIVE LL_LOW(TFT_WR)
#define WR_IDLE LL_HIGH(TFT_WR)
#define CD_COMMAND LL_LOW(TFT_RS)
#define CD_DATA LL_HIGH(TFT_RS)
#define CS_ACTIVE LL_LOW(TFT_CS)
#define CS_IDLE LL_HIGH(TFT_CS)
#define RST_ACTIVE LL_LOW(TFT_RST)
#define RST_IDLE LL_HIGH(TFT_RST)
#define RD_STROBE {RD_ACTIVE; RD_IDLE;} // Not use
#define WR_STROBE {WR_ACTIVE; WR_IDLE;} // Not use
class STM32_TFT_8bit : public Adafruit_GFX {
public:
STM32_TFT_8bit(void);
void enablePortClock(GPIO_TypeDef *gpio);
void setResolution(int16_t width, int16_t height);
void setOffset(int16_t offset);
void begin(uint16_t ID);
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
void fillScreen(uint16_t color);
void drawLine(int16_t x0, int16_t y0,int16_t x1, int16_t y1, uint16_t color);
void drawPixel(int16_t x, int16_t y, uint16_t color);
void pushColors8(uint8_t * block, int16_t n, bool first);
void pushColors(uint16_t * block, int16_t n, bool first);
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
int16_t readGRAM(int16_t x, int16_t y, uint16_t * block, int16_t w, int16_t h);
uint16_t readPixel(int16_t x, int16_t y) { uint16_t color; readGRAM(x, y, &color, 1, 1); return color; }
void setRotation(uint8_t r);
void vertScroll(int16_t top, int16_t scrollines, int16_t offset);
void invertDisplay(boolean i);
uint16_t readID(void);
private:
void setWriteDataBus(void);
void setReadDataBus(void);
void write8(uint8_t);
void writeCmdByte(uint8_t c);
void WriteCmd(uint16_t c);
void writeDataByte(uint8_t d);
void writeDataWord(uint16_t d);
void WriteCmdData(uint16_t cmd, uint16_t dat);
void WriteCmdParamN(uint8_t cmd, int8_t N, uint8_t * block);
void WriteCmdParam4(uint8_t cmd, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4);
void init_table(const void *table, int16_t size);
void init_table16(const void *table, int16_t size);
void writeCmdWord(uint16_t c);
uint8_t read8(void);
uint8_t read8bits(void);
uint16_t read16bits(void);
uint16_t readReg16(uint16_t reg);
uint16_t readReg16Index(uint16_t reg, int8_t index);
uint32_t readReg32(uint16_t reg);
uint32_t readReg40(uint16_t reg);
uint16_t _lcd_ID, _lcd_capable, _lcd_rev, _lcd_madctl, _lcd_drivOut, _MC, _MP, _MW, _SC, _EC, _SP, _EP;
int16_t _lcd_width = 0, _lcd_height = 0, _lcd_offset=0;
uint32_t _pins[8];
};
#endif //endif of the header file