st7735: added new spi lcd module (missing bitmap and text)
[contrib/upm.git] / src / st7735 / st7735.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Credits to Adafrut.com
6  * {Based on,Inspired from} original <libname> from <author>. See original license in <filepath>.
7
8  * 
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #pragma once
29
30 #include <string>
31 #include <maa/aio.h>
32 #include <maa/gpio.h>
33 #include <maa/spi.h>
34 #include <gfx.h>
35
36 #define INITR_GREENTAB      0x0
37 #define INITR_REDTAB        0x1
38 #define INITR_BLACKTAB      0x2
39
40 #define ST7735_TFTWIDTH     128
41 #define ST7735_TFTHEIGHT    160
42
43 #define ST7735_NOP          0x00
44 #define ST7735_SWRESET      0x01
45 #define ST7735_RDDID        0x04
46 #define ST7735_RDDST        0x09
47
48 #define ST7735_SLPIN        0x10
49 #define ST7735_SLPOUT       0x11
50 #define ST7735_PTLON        0x12
51 #define ST7735_NORON        0x13
52
53 #define ST7735_INVOFF       0x20
54 #define ST7735_INVON        0x21
55 #define ST7735_DISPOFF      0x28
56 #define ST7735_DISPON       0x29
57 #define ST7735_CASET        0x2A
58 #define ST7735_RASET        0x2B
59 #define ST7735_RAMWR        0x2C
60 #define ST7735_RAMRD        0x2E
61
62 #define ST7735_PTLAR        0x30
63 #define ST7735_COLMOD       0x3A
64 #define ST7735_MADCTL       0x36
65
66 #define ST7735_FRMCTR1      0xB1
67 #define ST7735_FRMCTR2      0xB2
68 #define ST7735_FRMCTR3      0xB3
69 #define ST7735_INVCTR       0xB4
70 #define ST7735_DISSET5      0xB6
71
72 #define ST7735_PWCTR1       0xC0
73 #define ST7735_PWCTR2       0xC1
74 #define ST7735_PWCTR3       0xC2
75 #define ST7735_PWCTR4       0xC3
76 #define ST7735_PWCTR5       0xC4
77 #define ST7735_VMCTR1       0xC5
78
79 #define ST7735_RDID1        0xDA
80 #define ST7735_RDID2        0xDB
81 #define ST7735_RDID3        0xDC
82 #define ST7735_RDID4        0xDD
83
84 #define ST7735_PWCTR6       0xFC
85
86 #define ST7735_GMCTRP1      0xE0
87 #define ST7735_GMCTRN1      0xE1
88
89 // Color definitions
90 #define ST7735_BLACK        0x0000
91 #define ST7735_BLUE         0x001F
92 #define ST7735_RED          0xF800
93 #define ST7735_GREEN        0x07E0
94 #define ST7735_CYAN         0x07FF
95 #define ST7735_MAGENTA      0xF81F
96 #define ST7735_YELLOW       0xFFE0  
97 #define ST7735_WHITE        0xFFFF
98
99 #define HIGH                1
100 #define LOW                 0
101
102 #define DELAY               0x80
103
104 namespace upm {
105
106 static const uint8_t Bcmd[] = {                  
107     // Initialization commands for 7735B screens
108       18,                     // 18 commands in list:
109     ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
110       50,                     //     50 ms delay
111     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
112       255,                    //     255 = 500 ms delay
113     ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
114       0x05,                   //     16-bit color
115       10,                     //     10 ms delay
116     ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
117       0x00,                   //     fastest refresh
118       0x06,                   //     6 lines front porch
119       0x03,                   //     3 lines back porch
120       10,                     //     10 ms delay
121     ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
122       0x08,                   //     Row addr/col addr, bottom to top refresh
123     ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
124       0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
125                               //     rise, 3 cycle osc equalize
126       0x02,                   //     Fix on VTL
127     ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
128       0x0,                    //     Line inversion
129     ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
130       0x02,                   //     GVDD = 4.7V
131       0x70,                   //     1.0uA
132       10,                     //     10 ms delay
133     ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
134       0x05,                   //     VGH = 14.7V, VGL = -7.35V
135     ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
136       0x01,                   //     Opamp current small
137       0x02,                   //     Boost frequency
138     ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
139       0x3C,                   //     VCOMH = 4V
140       0x38,                   //     VCOML = -1.1V
141       10,                     //     10 ms delay
142     ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
143       0x11, 0x15,
144     ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
145       0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
146       0x21, 0x1B, 0x13, 0x19, //      these config values represent)
147       0x17, 0x15, 0x1E, 0x2B,
148       0x04, 0x05, 0x02, 0x0E,
149     ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
150       0x0B, 0x14, 0x08, 0x1E, //     (ditto)
151       0x22, 0x1D, 0x18, 0x1E,
152       0x1B, 0x1A, 0x24, 0x2B,
153       0x06, 0x06, 0x02, 0x0F,
154       10,                     //     10 ms delay
155     ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
156       0x00, 0x02,             //     XSTART = 2
157       0x00, 0x81,             //     XEND = 129
158     ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
159       0x00, 0x02,             //     XSTART = 1
160       0x00, 0x81,             //     XEND = 160
161     ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
162       10,                     //     10 ms delay
163     ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
164       255 },                  //     255 = 500 ms delay
165
166   Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
167       15,                     // 15 commands in list:
168     ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
169       150,                    //     150 ms delay
170     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
171       255,                    //     500 ms delay
172     ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
173       0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
174     ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
175       0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
176     ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
177       0x01, 0x2C, 0x2D,       //     Dot inversion mode
178       0x01, 0x2C, 0x2D,       //     Line inversion mode
179     ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
180       0x07,                   //     No inversion
181     ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
182       0xA2,
183       0x02,                   //     -4.6V
184       0x84,                   //     AUTO mode
185     ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
186       0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
187     ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
188       0x0A,                   //     Opamp current small
189       0x00,                   //     Boost frequency
190     ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
191       0x8A,                   //     BCLK/2, Opamp current small & Medium low
192       0x2A,  
193     ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
194       0x8A, 0xEE,
195     ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
196       0x0E,
197     ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
198     ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
199       0xC8,                   //     row addr/col addr, bottom to top refresh
200     ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
201       0x05 },                 //     16-bit color
202
203   Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
204       2,                      //  2 commands in list:
205     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
206       0x00, 0x02,             //     XSTART = 0
207       0x00, 0x7F+0x02,        //     XEND = 127
208     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
209       0x00, 0x01,             //     XSTART = 0
210       0x00, 0x9F+0x01 },      //     XEND = 159
211   Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
212       2,                      //  2 commands in list:
213     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
214       0x00, 0x00,             //     XSTART = 0
215       0x00, 0x7F,             //     XEND = 127
216     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
217       0x00, 0x00,             //     XSTART = 0
218       0x00, 0x9F },           //     XEND = 159
219
220   Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
221       4,                      //  4 commands in list:
222     ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
223       0x02, 0x1c, 0x07, 0x12,
224       0x37, 0x32, 0x29, 0x2d,
225       0x29, 0x25, 0x2B, 0x39,
226       0x00, 0x01, 0x03, 0x10,
227     ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
228       0x03, 0x1d, 0x07, 0x06,
229       0x2E, 0x2C, 0x29, 0x2D,
230       0x2E, 0x2E, 0x37, 0x3F,
231       0x00, 0x00, 0x02, 0x10,
232     ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
233       10,                     //     10 ms delay
234     ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
235       100 };                  //     100 ms delay
236
237 #define swap(a, b) { int16_t t = a; a = b; b = t; }
238
239
240
241 /**
242  * @brief C++ API for NRF24l01 transceiver module
243  *
244  * This file defines the NRF24l01 C++ interface for libnrf24l01
245  *
246  * @snippet nrf_receiver.cxx Interesting
247  * @snippet nrf_transmitter.cxx Interesting
248  */
249 class ST7735 : public GFX {
250     public:
251         /**
252          * Instanciates a NRF24l01 object
253          *
254          * @param cs chip select pin
255          */
256         ST7735 (uint8_t csLCD, uint8_t cSD, uint8_t rs, uint8_t rst);
257
258         /**
259          * NRF24l01 object destructor
260          */
261         ~ST7735 ();
262
263         /**
264          * Return name of the component
265          */
266         std::string name()
267         {
268             return m_name;
269         }
270
271         /**
272          * 
273          */
274         void initModule ();
275
276         /**
277          * 
278          */
279         void configModule ();
280         
281         /**
282          * 
283          */
284         void write (uint8_t value);
285         
286         /**
287          * 
288          */
289         void data (uint8_t value);
290         
291         /**
292          * 
293          */
294         void executeCMDList (const uint8_t *addr);
295         
296         /**
297          * 
298          */
299         void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
300         
301         /**
302          * 
303          */
304         void drawPixel (int16_t x, int16_t y, uint16_t color);
305         
306         /**
307          * 
308          */
309         void refresh ();
310         
311         /**
312          * 
313          */
314         maa_result_t lcdCSOn ();
315
316         /**
317          * 
318          */
319         maa_result_t lcdCSOff ();
320
321         /**
322          * 
323          */
324         maa_result_t sdCSOn ();
325
326         /**
327          * 
328          */
329         maa_result_t sdCSOff ();
330         
331         /**
332          * 
333          */
334         maa_result_t rsHIGH ();
335
336         /**
337          * 
338          */
339         maa_result_t rsLOW ();
340         
341         void fill (int col, uint16_t* buff, int len);
342     private:
343         maa_spi_context      m_spi;
344         uint8_t              m_csLCD;
345         uint8_t              m_cSD;
346         uint8_t              m_rST;
347         uint8_t              m_rS;
348
349         maa_gpio_context     m_csLCDPinCtx;
350         maa_gpio_context     m_cSDPinCtx;
351         maa_gpio_context     m_rSTPinCtx;
352         maa_gpio_context     m_rSPinCtx;
353                 
354         uint8_t              m_spiBuffer[128];
355
356         std::string          m_name;
357 };
358
359 }