forked from KenKenMkIISR/picoinvader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathili9341_spi.c
More file actions
276 lines (256 loc) · 6.08 KB
/
ili9341_spi.c
File metadata and controls
276 lines (256 loc) · 6.08 KB
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
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "LCDdriver.h"
static inline void lcd_cs_lo() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_CS, 0);
asm volatile("nop \n nop \n nop");
}
static inline void lcd_cs_hi() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_CS, 1);
asm volatile("nop \n nop \n nop");
}
static inline void lcd_dc_lo() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_DC, 0);
asm volatile("nop \n nop \n nop");
}
static inline void lcd_dc_hi() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_DC, 1);
asm volatile("nop \n nop \n nop");
}
static inline void lcd_reset_lo() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_RESET, 0);
asm volatile("nop \n nop \n nop");
}
static inline void lcd_reset_hi() {
asm volatile("nop \n nop \n nop");
gpio_put(LCD_RESET, 1);
asm volatile("nop \n nop \n nop");
}
void LCD_WriteComm(unsigned char comm){
// Write Command
lcd_dc_lo();
lcd_cs_lo();
spi_write_blocking(SPICH, &comm , 1);
lcd_cs_hi();
}
void LCD_WriteData(unsigned char data)
{
// Write Data
lcd_dc_hi();
lcd_cs_lo();
spi_write_blocking(SPICH, &data , 1);
lcd_cs_hi();
}
void LCD_WriteData2(unsigned short data)
{
// Write Data 2 bytes
unsigned short d;
lcd_dc_hi();
lcd_cs_lo();
d=(data>>8) | (data<<8);
spi_write_blocking(SPICH, (unsigned char *)&d, 2);
lcd_cs_hi();
}
void LCD_WriteDataN(unsigned char *b,int n)
{
// Write Data N bytes
lcd_dc_hi();
lcd_cs_lo();
spi_write_blocking(SPICH, b,n);
lcd_cs_hi();
}
void LCD_Read(unsigned char com,unsigned char *b,int n){
lcd_cs_lo();
// Write Command
lcd_dc_lo();
spi_write_blocking(SPICH, &com , 1);
// Read Data
lcd_dc_hi();
spi_read_blocking(SPICH, 0, b, 1); // dummy read
spi_read_blocking(SPICH, 0, b, n);
lcd_cs_hi();
}
void LCD_Init()
{
lcd_cs_hi();
lcd_dc_hi();
// Reset controller
lcd_reset_hi();
sleep_ms(1);
lcd_reset_lo();
sleep_ms(10);
lcd_reset_hi();
sleep_ms(120);
//************* Start Initial Sequence **********//
#ifdef PIMORONI_PICOSYSTEM
// really st7789, but the commands for updating the screen are the same
// TODO: steal more of the init from one of the SDKs?
LCD_WriteComm(0x35); // TEON
LCD_WriteComm(0x44); // STE
LCD_WriteData(0x00);
LCD_WriteData(0xDC); // 220 (a bit early)
LCD_WriteComm(0x3A); // COLMOD
LCD_WriteData(0x55);
LCD_WriteComm(0x21); // INVON
#else
LCD_WriteComm(0xCB);
LCD_WriteData(0x39);
LCD_WriteData(0x2C);
LCD_WriteData(0x00);
LCD_WriteData(0x34);
LCD_WriteData(0x02);
LCD_WriteComm(0xCF);
LCD_WriteData(0x00);
LCD_WriteData(0XC1);
LCD_WriteData(0X30);
LCD_WriteComm(0xE8);
LCD_WriteData(0x85);
LCD_WriteData(0x00);
LCD_WriteData(0x78);
LCD_WriteComm(0xEA);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
LCD_WriteComm(0xED);
LCD_WriteData(0x64);
LCD_WriteData(0x03);
LCD_WriteData(0X12);
LCD_WriteData(0X81);
LCD_WriteComm(0xF7);
LCD_WriteData(0x20);
LCD_WriteComm(0xC0);
LCD_WriteData(0x23);
LCD_WriteComm(0xC1);
LCD_WriteData(0x10);
LCD_WriteComm(0xC5);
LCD_WriteData(0x3e);
LCD_WriteData(0x28);
LCD_WriteComm(0xC7);
LCD_WriteData(0x86);
LCD_WriteComm(0x36);
#if LCD_ALIGNMENT == VERTICAL
LCD_WriteData(0x48);
#elif LCD_ALIGNMENT == HORIZONTAL
LCD_WriteData(0x0C);
#endif
LCD_WriteComm(0x37);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
LCD_WriteComm(0x3A);
LCD_WriteData(0x55);
LCD_WriteComm(0xB1);
LCD_WriteData(0x00);
LCD_WriteData(0x18);
LCD_WriteComm(0xB6);
LCD_WriteData(0x0A);
LCD_WriteData(0x82);
LCD_WriteData(0x27);
LCD_WriteData(0x00);
// LCD_WriteComm(0xF2);
// LCD_WriteData(0x00);
LCD_WriteComm(0x26);
LCD_WriteData(0x01);
LCD_WriteComm(0xE0);
LCD_WriteData(0x0F);
LCD_WriteData(0x3a);
LCD_WriteData(0x36);
LCD_WriteData(0x0b);
LCD_WriteData(0x0d);
LCD_WriteData(0x06);
LCD_WriteData(0x4c);
LCD_WriteData(0x91);
LCD_WriteData(0x31);
LCD_WriteData(0x08);
LCD_WriteData(0x10);
LCD_WriteData(0x04);
LCD_WriteData(0x11);
LCD_WriteData(0x0c);
LCD_WriteData(0x00);
LCD_WriteComm(0XE1);
LCD_WriteData(0x00);
LCD_WriteData(0x06);
LCD_WriteData(0x0a);
LCD_WriteData(0x05);
LCD_WriteData(0x12);
LCD_WriteData(0x09);
LCD_WriteData(0x2c);
LCD_WriteData(0x92);
LCD_WriteData(0x3f);
LCD_WriteData(0x08);
LCD_WriteData(0x0e);
LCD_WriteData(0x0b);
LCD_WriteData(0x2e);
LCD_WriteData(0x33);
LCD_WriteData(0x0F);
#endif
LCD_WriteComm(0x11);
sleep_ms(120);
LCD_WriteComm(0x29);
}
void LCD_setAddrWindow(unsigned short x,unsigned short y,unsigned short w,unsigned short h)
{
#if LCD_ALIGNMENT == VERTICAL
LCD_WriteComm(0x2a);
LCD_WriteData2(x);
LCD_WriteData2(x+w-1);
LCD_WriteComm(0x2b);
LCD_WriteData2(y);
LCD_WriteData2(y+h-1);
#elif LCD_ALIGNMENT == HORIZONTAL
LCD_WriteComm(0x2a);
LCD_WriteData2(y);
LCD_WriteData2(y+h-1);
LCD_WriteComm(0x2b);
LCD_WriteData2(x);
LCD_WriteData2(x+w-1);
#endif
LCD_WriteComm(0x2c);
}
void LCD_SetCursor(unsigned short x, unsigned short y)
{
LCD_setAddrWindow(x,y,X_RES-x,1);
}
void LCD_continuous_output(unsigned short x,unsigned short y,unsigned short color,int n)
{
//High speed continuous output
int i;
unsigned short d;
LCD_setAddrWindow(x,y,n,1);
lcd_dc_hi();
lcd_cs_lo();
d=(color>>8) | (color<<8);
for (i=0; i < n ; i++){
spi_write_blocking(SPICH, (unsigned char *)&d, 2);
}
lcd_cs_hi();
}
void LCD_Clear(unsigned short color)
{
int i;
unsigned short d;
LCD_setAddrWindow(0,0,X_RES,Y_RES);
lcd_dc_hi();
lcd_cs_lo();
d=(color>>8) | (color<<8);
for (i=0; i < X_RES*Y_RES ; i++){
spi_write_blocking(SPICH, (unsigned char *)&d, 2);
}
lcd_cs_hi();
}
void drawPixel(unsigned short x, unsigned short y, unsigned short color)
{
LCD_SetCursor(x,y);
LCD_WriteData2(color);
}
unsigned short getColor(unsigned short x, unsigned short y)
{
unsigned int d=0;
LCD_SetCursor(x,y);
LCD_Read(0x2e, (unsigned char *)&d, 3);
return ((d&0xf8)<<8)|((d&0xfc00)>>5)|((d&0xf80000)>>19); //RGB565 format
}