1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Driver for HX8340B 176x220 pixel TFT LCD displays.
10 This driver uses a bit-banged SPI interface and a 16-bit RGB565
15 Software License Agreement (BSD License)
17 Copyright (c) 2012 Kevin Townsend
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. Neither the name of the copyright holders nor the
28 names of its contributors may be used to endorse or promote products
29 derived from this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
32 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 /**************************************************************************/
44 #include "core/systick/systick.h"
45 #include "core/gpio/gpio.h"
47 static lcdOrientation_t lcdOrientation
= LCD_ORIENTATION_PORTRAIT
;
48 static lcdProperties_t hx8340bProperties
= { 176, 220, false, false, false, true, true };
50 /*************************************************/
52 /*************************************************/
54 /*************************************************/
55 void hx8340bWriteCmd(uint8_t command
)
81 /*************************************************/
82 void hx8340bWriteData(uint8_t data
)
108 /*************************************************/
109 void hx8340bWriteData16(uint16_t data
)
111 hx8340bWriteData((data
>>8) & 0xFF);
112 hx8340bWriteData(data
& 0xFF);
115 /*************************************************/
116 void hx8340bWriteRegister(uint8_t reg
, uint8_t value
)
118 hx8340bWriteCmd(reg
);
119 hx8340bWriteCmd(value
);
122 /*************************************************/
123 void hx8340bInitDisplay(void)
125 #if defined HX8340B_ICVERSION_T
127 hx8340bWriteRegister(HX8340B_OSCCONTROLA
, 0x02); // 0x18: Frequency = 100% (60fps)
128 hx8340bWriteRegister(HX8340B_OSCCONTROLB
, 0x00); // 0x21: Disable Internal Oscillator
129 hx8340bWriteRegister(HX8340B_VCOMCONTROL1
, 0x30); // 0x23: VCOM Offset voltage
130 hx8340bWriteRegister(HX8340B_VCOMCONTROL2
, 0x36); // 0x24: VCOM High Voltage
131 hx8340bWriteRegister(HX8340B_VCOMCONTROL3
, 0x1a); // 0x25: VCOM Low Voltage
133 // Power Supply Settings
134 hx8340bWriteRegister(HX8340B_OSCCONTROLA
, 0x44); // 0x18: Frequency = 100% (60fps) (Internal 2.52MHz RC oscillator is +/-5% and made need to be tuned)
135 hx8340bWriteRegister(HX8340B_OSCCONTROLB
, 0x01); // 0x21: Enable Internal Oscillator
136 hx8340bWriteRegister(HX8340B_DISPLAYMODECONTROL
, 0x00); // 0x01: Partial mode off, Sleep off, Scroll off, Invert off, Idle off
137 hx8340bWriteRegister(HX8340B_MEMACCESSCONTROL
, 0x08); // 0x16: Read/Write scanning direction and color order (0x08 = BGR, 0x00 = RGB)
138 hx8340bWriteRegister(HX8340B_COLMOD
, 0x55); // 0x17: RGB Data Format - 0x55 = 16bpp, 0x66 = 18bpp
139 hx8340bWriteRegister(HX8340B_POWERCONTROL5
, 0x03); // 0x1C: Current driving the power-supply op-amp, 0x03 = medium
140 hx8340bWriteRegister(HX8340B_POWERCONTROL1
, 0x06); // 0x19: B0 = DDVDH Step-up on/off, B1 = VGH/VGL step-up off/on, B2 = VCL step-up off/on (0x06 = all on)
144 hx8340bWriteRegister(HX8340B_POWERCONTROL_INT1
, 0x00); // 0x60
145 hx8340bWriteRegister(HX8340B_POWERCONTROL_INT2
, 0x06); // 0x61
146 hx8340bWriteRegister(HX8340B_SOURCECONTROL_INT1
, 0x00); // 0x62
147 hx8340bWriteRegister(HX8340B_SOURCECONTROL_INT2
, 0xC8); // 0x63
150 hx8340bWriteRegister(0x40,0x70);
151 hx8340bWriteRegister(0x41,0x51);
152 hx8340bWriteRegister(0x42,0x36);
153 hx8340bWriteRegister(0x43,0x04);
154 hx8340bWriteRegister(0x44,0x3B);
155 hx8340bWriteRegister(0x45,0x0E);
156 hx8340bWriteRegister(0x46,0x01);
157 hx8340bWriteRegister(0x47,0x1D);
158 hx8340bWriteRegister(0x48,0x09);
159 hx8340bWriteRegister(0x50,0x72);
160 hx8340bWriteRegister(0x51,0x20);
161 hx8340bWriteRegister(0x52,0x60);
162 hx8340bWriteRegister(0x53,0x01);
163 hx8340bWriteRegister(0x54,0x33);
164 hx8340bWriteRegister(0x55,0x0E);
165 hx8340bWriteRegister(0x56,0x02);
166 hx8340bWriteRegister(0x57,0x73);
167 hx8340bWriteRegister(HX8340B_DISPLAYCONTROL1
, 0x84); // 0x26: B3 = display off/on (1 = on, 0 = off), also controls gate for VGH/VGL
169 hx8340bWriteRegister(HX8340B_DISPLAYCONTROL1
, 0xB8); // 0x26: Display on
171 hx8340bWriteRegister(HX8340B_DISPLAYCONTROL1
, 0xBC);
174 hx8340bWriteRegister(HX8340B_COLADDR_START2
, 0x00);
175 hx8340bWriteRegister(HX8340B_COLADDR_START1
, 0x00);
176 hx8340bWriteRegister(HX8340B_COLADDR_END2
, 0x00);
177 hx8340bWriteRegister(HX8340B_COLADDR_END1
, 0xAF); // 175
178 hx8340bWriteRegister(HX8340B_ROWADDR_START2
, 0x00);
179 hx8340bWriteRegister(HX8340B_ROWADDR_START1
, 0x00);
180 hx8340bWriteRegister(HX8340B_ROWADDR_END2
, 0x00);
181 hx8340bWriteRegister(HX8340B_ROWADDR_END1
, 0xDB); // 219
183 hx8340bWriteCmd(HX8340B_SRAMWRITECONTROL
);
186 #if defined HX8340B_ICVERSION_N
187 hx8340bWriteCmd(HX8340B_N_SETEXTCMD
);
188 hx8340bWriteData(0xFF);
189 hx8340bWriteData(0x83);
190 hx8340bWriteData(0x40);
192 hx8340bWriteCmd(HX8340B_N_SPLOUT
);
195 hx8340bWriteCmd(0xCA); // Undocumented register?
196 hx8340bWriteData(0x70);
197 hx8340bWriteData(0x00);
198 hx8340bWriteData(0xD9);
199 hx8340bWriteData(0x01);
200 hx8340bWriteData(0x11);
202 hx8340bWriteCmd(0xC9); // Undocumented register?
203 hx8340bWriteData(0x90);
204 hx8340bWriteData(0x49);
205 hx8340bWriteData(0x10);
206 hx8340bWriteData(0x28);
207 hx8340bWriteData(0x28);
208 hx8340bWriteData(0x10);
209 hx8340bWriteData(0x00);
210 hx8340bWriteData(0x06);
213 hx8340bWriteCmd(HX8340B_N_SETGAMMAP
);
214 hx8340bWriteData(0x60);
215 hx8340bWriteData(0x71);
216 hx8340bWriteData(0x01);
217 hx8340bWriteData(0x0E);
218 hx8340bWriteData(0x05);
219 hx8340bWriteData(0x02);
220 hx8340bWriteData(0x09);
221 hx8340bWriteData(0x31);
222 hx8340bWriteData(0x0A);
224 hx8340bWriteCmd(HX8340B_N_SETGAMMAN
);
225 hx8340bWriteData(0x67);
226 hx8340bWriteData(0x30);
227 hx8340bWriteData(0x61);
228 hx8340bWriteData(0x17);
229 hx8340bWriteData(0x48);
230 hx8340bWriteData(0x07);
231 hx8340bWriteData(0x05);
232 hx8340bWriteData(0x33);
235 hx8340bWriteCmd(HX8340B_N_SETPWCTR5
);
236 hx8340bWriteData(0x35);
237 hx8340bWriteData(0x20);
238 hx8340bWriteData(0x45);
240 hx8340bWriteCmd(HX8340B_N_SETPWCTR4
);
241 hx8340bWriteData(0x33);
242 hx8340bWriteData(0x25);
243 hx8340bWriteData(0x4c);
246 hx8340bWriteCmd(HX8340B_N_COLMOD
); // Color Mode
247 hx8340bWriteData(0x05); // 0x05 = 16bpp, 0x06 = 18bpp
249 hx8340bWriteCmd(HX8340B_N_DISPON
);
252 hx8340bWriteCmd(HX8340B_N_CASET
);
253 hx8340bWriteData(0x00);
254 hx8340bWriteData(0x00);
255 hx8340bWriteData(0x00);
256 hx8340bWriteData(0xaf); // 175
258 hx8340bWriteCmd(HX8340B_N_PASET
);
259 hx8340bWriteData(0x00);
260 hx8340bWriteData(0x00);
261 hx8340bWriteData(0x00);
262 hx8340bWriteData(0xdb); // 219
264 hx8340bWriteCmd(HX8340B_N_RAMWR
);
268 /*************************************************/
269 void hx8340bHome(void)
271 #if defined HX8340B_ICVERSION_N
272 hx8340bWriteCmd(HX8340B_N_CASET
);
273 hx8340bWriteData(0x00);
274 hx8340bWriteData(0x00);
275 hx8340bWriteData(0x00);
276 hx8340bWriteData(0xaf);
277 hx8340bWriteCmd(HX8340B_N_PASET
);
278 hx8340bWriteData(0x00);
279 hx8340bWriteData(0x00);
280 hx8340bWriteData(0x00);
281 hx8340bWriteData(0xdb);
285 /*************************************************/
286 static inline void hx8340bSetPosition(uint32_t x0
, uint32_t y0
, uint32_t x1
, uint32_t y1
)
288 #if defined HX8340B_ICVERSION_N
289 hx8340bWriteCmd(HX8340B_N_CASET
);
290 hx8340bWriteData(x0
>>8);
291 hx8340bWriteData(x0
);
292 hx8340bWriteData(x1
>>8);
293 hx8340bWriteData(x1
);
295 hx8340bWriteCmd(HX8340B_N_PASET
);
296 hx8340bWriteData(y0
>>8);
297 hx8340bWriteData(y0
);
298 hx8340bWriteData(y1
>>8);
299 hx8340bWriteData(y1
);
301 hx8340bWriteCmd(HX8340B_N_RAMWR
);
305 /*************************************************/
307 /*************************************************/
309 /*************************************************/
312 // Set control pins to output
313 gpioSetDir(HX8340B_PORT
, HX8340B_SDI_PIN
, 1);
314 gpioSetDir(HX8340B_PORT
, HX8340B_SCL_PIN
, 1);
315 gpioSetDir(HX8340B_PORT
, HX8340B_CS_PIN
, 1);
316 gpioSetDir(HX8340B_PORT
, HX8340B_RES_PIN
, 1);
317 gpioSetDir(HX8340B_PORT
, HX8340B_BL_PIN
, 1);
319 // Set pins low by default (except reset)
337 // Run LCD init sequence
338 hx8340bInitDisplay();
341 lcdFillRGB(COLOR_BLACK
);
344 /*************************************************/
345 void lcdBacklight(bool state
)
348 // Note: Depending on the type of transistor used
349 // to control the backlight, you made need to invert
359 /*************************************************/
362 #if defined HX8340B_ICVERSION_N
363 lcdFillRGB(COLOR_GREEN
);
367 /*************************************************/
368 void lcdFillRGB(uint16_t color
)
370 #if defined HX8340B_ICVERSION_N
376 hx8340bWriteData16(color
);
382 /*************************************************/
383 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
385 hx8340bSetPosition(x
, y
, x
+1, y
+1);
386 hx8340bWriteData16(color
);
389 /**************************************************************************/
391 @brief Draws an array of consecutive RGB565 pixels (much
392 faster than addressing each pixel individually)
394 /**************************************************************************/
395 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
397 // ToDo: Optimise this function ... currently only a placeholder
401 lcdDrawPixel(x
+i
, y
, data
[i
]);
406 /*************************************************/
407 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
409 // Allows for slightly better performance than setting individual pixels
421 if (x1
>= lcdGetWidth())
423 x1
= lcdGetWidth() - 1;
425 if (x0
>= lcdGetWidth())
427 x0
= lcdGetWidth() - 1;
430 hx8340bSetPosition(x0
, y
, x1
, y
+1);
431 for (pixels
= 0; pixels
< x1
- x0
+ 1; pixels
++)
433 hx8340bWriteData16(color
);
437 /*************************************************/
438 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
440 // Allows for slightly better performance than setting individual pixels
452 if (y1
>= lcdGetHeight())
454 y1
= lcdGetHeight() - 1;
456 if (y0
>= lcdGetHeight())
458 y0
= lcdGetHeight() - 1;
461 for (pixels
= 0; pixels
< y1
- y0
+ 1; pixels
++)
463 hx8340bSetPosition(x
, y0
+pixels
, x
+1, y0
+pixels
+1);
464 hx8340bWriteData16(color
);
468 /*************************************************/
469 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
475 /*************************************************/
476 void lcdSetOrientation(lcdOrientation_t orientation
)
481 /*************************************************/
482 lcdOrientation_t
lcdGetOrientation(void)
484 return lcdOrientation
;
487 /*************************************************/
488 uint16_t lcdGetWidth(void)
490 return hx8340bProperties
.width
;
493 /*************************************************/
494 uint16_t lcdGetHeight(void)
496 return hx8340bProperties
.height
;
499 /*************************************************/
500 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
505 /*************************************************/
506 uint16_t lcdGetControllerID(void)
511 /*************************************************/
512 lcdProperties_t
lcdGetProperties(void)
514 return hx8340bProperties
;