Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
TFT_ILI9163C.h
1 /*
2  ILI9163C - A fast SPI driver for TFT that use Ilitek ILI9163C.
3 
4  Features:
5  - Very FAST!, expecially with Teensy 3.x where uses DMA SPI.
6  - It uses just 4 or 5 wires.
7  - Compatible at command level with Adafruit display series so it's easy to adapt existing code.
8  - It uses the standard Adafruit_GFX Library (you need to install).
9 
10  Background:
11  I got one of those displays from a chinese ebay seller but unfortunatly I cannot get
12  any working library so I decided to hack it. ILI9163C looks pretty similar to other
13  display driver but it uses it's own commands so it's tricky to work with it unlsess you
14  carefully fight with his gigantic and not so clever datasheet.
15  My display it's a 1.44"", 128x128 that suppose to substitute Nokia 5110 LCD and here's the
16  first confusion! Many sellers claim that it's compatible with Nokia 5110 (that use a philips
17  controller) but the only similarity it's the pin names since that this one it's color and
18  have totally different controller that's not compatible.
19  http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/141196897388
20  http://www.elecrow.com/144-128x-128-tft-lcd-with-spi-interface-p-855.html
21  Pay attention that can drive different resolutions and your display can be
22  160*128 or whatever, also there's a strain of this display with a black PCB that a friend of mine
23  got some weeks ago and need some small changes in library to get working.
24  If you look at TFT_ILI9163C.h file you can add your modifications and let me know so I
25  can include for future versions.
26 
27  Code Optimizations:
28  The purpose of this library it's SPEED. I have tried to use hardware optimized calls
29  where was possible and results are quite good for most applications, actually nly filled circles
30  are still a bit slow. Many SPI call has been optimized by reduce un-needed triggers to RS and CS
31  lines. Of course it can be improved so feel free to add suggestions.
32  -------------------------------------------------------------------------------
33  Copyright (c) 2014, .S.U.M.O.T.O.Y., coded by Max MC Costa.
34 
35  TFT_ILI9163C Library is free software: you can redistribute it and/or modify
36  it under the terms of the GNU General Public License as published by
37  the Free Software Foundation, either version 3 of the License, or
38  (at your option) any later version.
39 
40  TFT_ILI9163C Library is distributed in the hope that it will be useful,
41  but WITHOUT ANY WARRANTY; without even the implied warranty of
42  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  GNU General Public License for more details.
44 
45  You should have received a copy of the GNU General Public License
46  along with Foobar. If not, see <http://www.gnu.org/licenses/>.
47  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
48  This file needs the following Libraries:
49 
50  Adafruit_GFX by Adafruit:
51  https://github.com/adafruit/Adafruit-GFX-Library
52  Remember to update GFX library often to have more features with this library!
53  From this version I'm using my version of Adafruit_GFX library:
54  https://github.com/sumotoy/Adafruit-GFX-Library
55  It has faster char rendering and some small little optimizations but you can
56  choose one of the two freely since are both fully compatible.
57  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
58  Special Thanks:
59  Thanks Adafruit for his Adafruit_GFX!
60  Thanks to Paul Stoffregen for his beautiful Teensy3 and DMA SPI.
61 
62  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
63  Version:
64  0.1a1: First release, compile correctly. Altrough not fully working!
65  0.1a3: Better but still some addressing problems.
66  0.1b1: Beta! Addressing solved, now rotation works and boundaries ok.
67  0.2b1: Cleaned up.
68  0.2b3: Added 2.2" Red PCB parameters
69  0.2b4: Bug fixes, added colorSpace (for future send image)
70  0.2b5: Cleaning
71  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72  BugList of the current version:
73 
74  - Actually no scroll commands (only in release will be included).
75 */
76 #ifndef _TFT_ILI9163CLIB_H_
77 #define _TFT_ILI9163CLIB_H_
78 
79 #if ARDUINO >= 100
80  #include "Arduino.h"
81  #include "Print.h"
82 #else
83  #include "WProgram.h"
84 #endif
85 
86 #define BLACK 0x0000
87 #define BLUE 0x001F
88 #define RED 0xF800
89 #define GREEN 0x07E0
90 #define CYAN 0x07FF
91 #define MAGENTA 0xF81F
92 #define YELLOW 0xFFE0
93 #define WHITE 0xFFFF
94 
95 #include "../Adafruit_GFX/Adafruit_GFX.h"
96 
97 //----- Define here witch display you own
98 //#define __144_RED_PCB__//128x128
99 //#define __22_RED_PCB__//240x320
100 #define __128x160_BLUE_PCB__ //1.8 TFT MODULE 160x128 INTEFORM (with SDC slot)
101 //---------------------------------------
102 
103 #if defined(__SAM3X8E__)
104  #include <include/pio.h>
105  #define PROGMEM
106  #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
107  #define pgm_read_word(addr) (*(const unsigned short *)(addr))
108  typedef unsigned char prog_uchar;
109 #endif
110 #ifdef __AVR__
111  #include <avr/pgmspace.h>
112 #endif
113 #if defined(__MK20DX128__) || defined(__MK20DX256__)
114  #define __DMASPI
115  #define CTAR_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0) | SPI_CTAR_DBR)
116  #define CTAR_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0) | SPI_CTAR_DBR)
117  #define CTAR_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0))
118  #define CTAR_8MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0))
119  #define CTAR_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1))
120  #define CTAR_4MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1))
121 #endif
122 
123 
124 #if defined(__128x160_BLUE_PCB__)
125  /*
126  This display:
127  1.8 TFT MODULE 160x128 INTEFORM (with SDC slot)
128  http://www.banggood.com/1_8-Inch-Serial-SPI-TFT-LCD-Display-Module-With-Power-IC-SD-Socket-p-909802.html
129  Note: there are the various drivers
130  */
131  #define _TFTWIDTH 128//the REAL W resolution of the TFT
132  #define _TFTHEIGHT 160//the REAL H resolution of the TFT
133  #define _GRAMWIDTH 128
134  #define _GRAMHEIGH 160
135  #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1
136  #define __COLORSPC 0// 1:GBR - 0:RGB
137  #define __GAMMASET1 //uncomment for another gamma
138  #define __OFFSET 0//*see note 2
139  //Tested!!
140 
141 //ILI9163C versions------------------------
142 #elif defined(__144_RED_PCB__)
143 
144 /*
145 This display:
146 http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/271422122271
147 This particular display has a design error! The controller has 3 pins to configure to constrain
148 the memory and resolution to a fixed dimension (in that case 128x128) but they leaved those pins
149 configured for 128x160 so there was several pixel memory addressing problems.
150 I solved by setup several parameters that dinamically fix the resolution as needit so below
151 the parameters for this diplay. If you have a strain or a correct display (can happen with chinese)
152 you can copy those parameters and create setup for different displays.
153 */
154  #define _TFTWIDTH 128//the REAL W resolution of the TFT
155  #define _TFTHEIGHT 128//the REAL H resolution of the TFT
156  #define _GRAMWIDTH 128
157  #define _GRAMHEIGH 160
158  #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1
159  #define __COLORSPC 1// 1:GBR - 0:RGB
160  #define __GAMMASET1 //uncomment for another gamma
161  #define __OFFSET 32//*see note 2
162  //Tested!
163 #elif defined (__22_RED_PCB__)
164 /*
165 Like this one:
166 http://www.ebay.it/itm/2-2-Serial-SPI-TFT-LCD-Display-Module-240x320-Chip-ILI9340C-PCB-Adapter-SD-Card-/281304733556
167 Not tested!
168 */
169  #define _TFTWIDTH 240//the REAL W resolution of the TFT
170  #define _TFTHEIGHT 320//the REAL H resolution of the TFT
171  #define _GRAMWIDTH 240
172  #define _GRAMHEIGH 320
173  #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
174  #define __COLORSPC 1// 1:GBR - 0:RGB
175  #define __GAMMASET1 //uncomment for another gamma
176  #define __OFFSET 0
177 #else
178  #define _TFTWIDTH 128//128
179  #define _TFTHEIGHT 160//160
180  #define _GRAMWIDTH 128
181  #define _GRAMHEIGH 160
182  #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
183  #define __COLORSPC 1// 1:GBR - 0:RGB
184  #define __GAMMASET1
185  #define __OFFSET 0
186 #endif
187 /*
188  Note 1: The __144_RED_PCB__ display has hardware addressing of 128 x 160
189  but the tft resolution it's 128 x 128 so the dram should be set correctly
190 
191  Note 2: This is the offset between image in RAM and TFT. In that case 160 - 128 = 32;
192 */
193 
194 //ILI9163C registers-----------------------
195 #define CMD_NOP 0x00//Non operation
196 #define CMD_SWRESET 0x01//Soft Reset
197 #define CMD_SLPIN 0x10//Sleep ON
198 #define CMD_SLPOUT 0x11//Sleep OFF
199 #define CMD_PTLON 0x12//Partial Mode ON
200 #define CMD_NORML 0x13//Normal Display ON
201 #define CMD_DINVOF 0x20//Display Inversion OFF
202 #define CMD_DINVON 0x21//Display Inversion ON
203 #define CMD_GAMMASET 0x26//Gamma Set (0x01[1],0x02[2],0x04[3],0x08[4])
204 #define CMD_DISPOFF 0x28//Display OFF
205 #define CMD_DISPON 0x29//Display ON
206 #define CMD_IDLEON 0x39//Idle Mode ON
207 #define CMD_IDLEOF 0x38//Idle Mode OFF
208 #define CMD_CLMADRS 0x2A//Column Address Set
209 #define CMD_PGEADRS 0x2B//Page Address Set
210 
211 #define CMD_RAMWR 0x2C//Memory Write
212 #define CMD_RAMRD 0x2E//Memory Read
213 #define CMD_CLRSPACE 0x2D//Color Space : 4K/65K/262K
214 #define CMD_PARTAREA 0x30//Partial Area
215 #define CMD_VSCLLDEF 0x33//Vertical Scroll Definition
216 #define CMD_TEFXLON 0x34//Tearing Effect Line ON
217 #define CMD_TEFXLOF 0x35//Tearing Effect Line OFF
218 #define CMD_MADCTL 0x36//Memory Access Control
219 
220 #define CMD_PIXFMT 0x3A//Interface Pixel Format
221 #define CMD_FRMCTR1 0xB1//Frame Rate Control (In normal mode/Full colors)
222 #define CMD_FRMCTR2 0xB2//Frame Rate Control(In Idle mode/8-colors)
223 #define CMD_FRMCTR3 0xB3//Frame Rate Control(In Partial mode/full colors)
224 #define CMD_DINVCTR 0xB4//Display Inversion Control
225 #define CMD_RGBBLK 0xB5//RGB Interface Blanking Porch setting
226 #define CMD_DFUNCTR 0xB6//Display Fuction set 5
227 #define CMD_SDRVDIR 0xB7//Source Driver Direction Control
228 #define CMD_GDRVDIR 0xB8//Gate Driver Direction Control
229 
230 #define CMD_PWCTR1 0xC0//Power_Control1
231 #define CMD_PWCTR2 0xC1//Power_Control2
232 #define CMD_PWCTR3 0xC2//Power_Control3
233 #define CMD_PWCTR4 0xC3//Power_Control4
234 #define CMD_PWCTR5 0xC4//Power_Control5
235 #define CMD_VCOMCTR1 0xC5//VCOM_Control 1
236 #define CMD_VCOMCTR2 0xC6//VCOM_Control 2
237 #define CMD_VCOMOFFS 0xC7//VCOM Offset Control
238 #define CMD_PGAMMAC 0xE0//Positive Gamma Correction Setting
239 #define CMD_NGAMMAC 0xE1//Negative Gamma Correction Setting
240 #define CMD_GAMRSEL 0xF2//GAM_R_SEL
241 
242 
243 class TFT_ILI9163C : public Adafruit_GFX {
244 
245  public:
246 
247  TFT_ILI9163C(uint8_t cspin,uint8_t dcpin,uint8_t rstpin);
248  TFT_ILI9163C(uint8_t CS, uint8_t DC);//connect rst pin to VDD
249 
250  void begin(void),
251  setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),//graphic Addressing
252  setCursor(int16_t x,int16_t y),//char addressing
253  pushColor(uint16_t color),
254  fillScreen(uint16_t color=0x0000),
255  clearScreen(uint16_t color=0x0000),//same as fillScreen
256  drawPixel(int16_t x, int16_t y, uint16_t color),
257  drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
258  drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
259  fillRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color),
260  setRotation(uint8_t r),
261  invertDisplay(boolean i);
262  uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
263  void setBitrate(uint32_t n);
264 
265  private:
266  uint8_t _Mactrl_Data;//container for the memory access control data
267  uint8_t _colorspaceData;
268  void colorSpace(uint8_t cspace);
269  void writecommand(uint8_t c);
270  void writedata(uint8_t d);
271  void writedata16(uint16_t d);
272  void chipInit();
273  bool boundaryCheck(int16_t x,int16_t y);
274  void homeAddress();
275  #if defined(__AVR__)
276  void spiwrite(uint8_t);
277  volatile uint8_t *dataport, *clkport, *csport, *rsport;
278  uint8_t _cs,_rs,_sid,_sclk,_rst;
279  uint8_t datapinmask, clkpinmask, cspinmask, rspinmask;
280  #endif // #ifdef __AVR__
281 
282  #if defined(__SAM3X8E__)
283  void spiwrite(uint8_t);
284  Pio *dataport, *clkport, *csport, *rsport;
285  uint8_t _cs,_rs,_sid,_sclk,_rst;
286  uint32_t datapinmask, clkpinmask, cspinmask, rspinmask;
287  #endif // #if defined(__SAM3X8E__)
288 
289  #if defined(__MK20DX128__) || defined(__MK20DX256__)
290  uint8_t _cs,_rs,_sid,_sclk,_rst;
291  uint8_t pcs_data, pcs_command;
292  uint32_t ctar;
293  volatile uint8_t *datapin, *clkpin, *cspin, *rspin;
294  #endif
295 
296  #if defined(__ESP8266_EX__)
297  void spiwrite(uint8_t);
298  volatile GPIO_REG_TYPE *csport, *rsport;
299  uint8_t _cs,_rs,_sid,_sclk,_rst;
300  GPIO_REG_TYPE cspinmask, rspinmask;
301  #endif // #ifdef __ESP8266_EX__
302 };
303 #endif
Definition: Adafruit_GFX.h:13
Definition: TFT_ILI9163C.h:243