Fixed typoes
[hackover2013-badge-firmware.git] / drivers / displays / tft / hw / ssd1351.c
1 /**************************************************************************/
2 /*!
3 @file SSD1351.c
4 @author K. Townsend (www.adafruit.com)
5
6 @section DESCRIPTION
7
8 Driver for SSD1351 128x128 pixel RGB OLED displays.
9
10 This driver uses a 3 or 4-pin SPI interface and 16-bit RGB565 colours.
11
12 @section LICENSE
13
14 Software License Agreement (BSD License)
15
16 Copyright (c) 2012, Adafruit Industries
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26 3. Neither the name of the copyright holders nor the
27 names of its contributors may be used to endorse or promote products
28 derived from this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
31 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41 /**************************************************************************/
42 #include "ssd1351.h"
43 #include "core/systick/systick.h"
44
45 static volatile lcdOrientation_t lcdOrientation = LCD_ORIENTATION_PORTRAIT;
46 static lcdProperties_t ssd1351Properties = { 128, 128, false, false, false };
47
48 /*************************************************/
49 /* Private Methods */
50 /*************************************************/
51
52 #define CMD(c) do { SET_CS; CLR_CS; CLR_DC; ssd1351SendByte( c, 1 ); SET_CS; } while (0)
53 #define DATA(c) do { SET_CS; CLR_CS; SET_DC; ssd1351SendByte( c, 0 ); SET_CS; } while (0);
54 #define DELAY(mS) do { systickDelay( mS / CFG_SYSTICK_DELAY_IN_MS ); } while(0);
55
56 /**************************************************************************/
57 /*!
58 @brief Simulates an SPI write using GPIO.
59
60 @param[in] byte
61 The byte to send
62 @param[in] command
63 1 if this is a command, 0 if it is data
64 */
65 /**************************************************************************/
66 void ssd1351SendByte(uint8_t byte, uint8_t command)
67 {
68 int8_t i;
69
70 // Make sure clock pin starts high
71 SET_SCK;
72
73 #if defined SSD1351_BUS_SPI3
74 // Prepend D/C bit (cmd = 0, data = 1)
75 CLR_SCK;
76 if (command)
77 {
78 CLR_SID;
79 }
80 else
81 {
82 SET_SID;
83 }
84 SET_SCK;
85 #endif
86
87 // Write from MSB to LSB
88 for (i=7; i>=0; i--)
89 {
90 // Set clock pin low
91 CLR_SCK;
92 // Set data pin high or low depending on the value of the current bit
93 if (byte & (1 << i))
94 {
95 SET_SID;
96 }
97 else
98 {
99 CLR_SID;
100 }
101 // Set clock pin high
102 SET_SCK;
103 }
104 }
105
106 /**************************************************************************/
107 /*!
108 @brief Sets the cursor to the specified X/Y position
109 */
110 /**************************************************************************/
111 void ssd1351SetCursor(uint8_t x, uint8_t y)
112 {
113 if ((x >= ssd1351Properties.width) || (y >= ssd1351Properties.height))
114 return;
115
116 CMD(SSD1351_CMD_WRITERAM);
117 CMD(SSD1351_CMD_SETCOLUMNADDRESS);
118 DATA(x); // Start Address
119 DATA(ssd1351Properties.width-1); // End Address (0x7F)
120
121 CMD(SSD1351_CMD_SETROWADDRESS);
122 DATA(y); // Start Address
123 DATA(ssd1351Properties.height-1); // End Address (0x7F)
124 CMD(SSD1351_CMD_WRITERAM);
125 }
126
127 /*************************************************/
128 /* Public Methods */
129 /*************************************************/
130
131 /**************************************************************************/
132 /*!
133 @brief Configures any pins or HW and initialises the LCD controller
134 */
135 /**************************************************************************/
136 void lcdInit(void)
137 {
138 // Set all pins to output
139 gpioSetDir(SSD1351_SCK_PORT, SSD1351_SCK_PIN, gpioDirection_Output);
140 gpioSetDir(SSD1351_SID_PORT, SSD1351_SID_PIN, gpioDirection_Output);
141 gpioSetDir(SSD1351_RST_PORT, SSD1351_RST_PIN, gpioDirection_Output);
142 gpioSetDir(SSD1351_CS_PORT, SSD1351_CS_PIN, gpioDirection_Output);
143
144 #if !defined SSD1351_BUS_SPI3
145 gpioSetDir(SSD1351_DC_PORT, SSD1351_DC_PIN, gpioDirection_Output);
146 #endif
147
148 // Reset the LCD
149 SET_RST;
150 DELAY(20);
151 CLR_RST;
152 DELAY(200);
153 SET_RST;
154 DELAY(20);
155
156 // Disable pullups
157 SSD1351_DISABLEPULLUPS();
158
159 CMD(SSD1351_CMD_SETCOMMANDLOCK);
160 DATA(0x12); // Unlocked to enter commands
161 CMD(SSD1351_CMD_SETCOMMANDLOCK);
162 DATA(0xB1); // Make all commands accessible
163 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYOFF);
164 CMD(SSD1351_CMD_SETFRONTCLOCKDIV);
165 DATA(0xF1);
166 CMD(SSD1351_CMD_SETMUXRRATIO);
167 DATA(0x7F);
168 CMD(SSD1351_CMD_COLORDEPTH);
169 DATA(0x74); // Bit 7:6 = 65,536 Colors, Bit 3 = BGR or RGB
170 CMD(SSD1351_CMD_SETCOLUMNADDRESS);
171 DATA(0x00);
172 DATA(0x7F);
173 CMD(SSD1351_CMD_SETROWADDRESS);
174 DATA(0x00);
175 DATA(0x7F);
176 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE);
177 DATA(0x00);
178 CMD(SSD1351_CMD_SETDISPLAYOFFSET);
179 DATA(0x00);
180 CMD(SSD1351_CMD_SETGPIO);
181 DATA(0x00); // Disable GPIO pins
182 CMD(SSD1351_CMD_FUNCTIONSELECTION);
183 DATA(0x01); // External VDD (0 = External, 1 = Internal)
184 CMD(SSD1351_CMD_SETPHASELENGTH);
185 DATA(0x32);
186 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE);
187 DATA(0xA0); // Enable External VSL
188 DATA(0xB5);
189 DATA(0x55);
190 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE);
191 DATA(0x17);
192 CMD(SSD1351_CMD_SETVCOMHVOLTAGE);
193 DATA(0x05);
194 CMD(SSD1351_CMD_SETCONTRAST);
195 DATA(0xC8);
196 DATA(0x80);
197 DATA(0xC8);
198 CMD(SSD1351_CMD_MASTERCONTRAST);
199 DATA(0x0F); // Maximum contrast
200 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD);
201 DATA(0x01);
202 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET);
203
204 // Use default grayscale for now to save flash space, but here are
205 // the values if someone wants to change them ...
206 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
207 // DATA(0x02);
208 // DATA(0x03);
209 // DATA(0x04);
210 // DATA(0x05);
211 // DATA(0x06);
212 // DATA(0x07);
213 // DATA(0x08);
214 // DATA(0x09);
215 // DATA(0x0A);
216 // DATA(0x0B);
217 // DATA(0x0C);
218 // DATA(0x0D);
219 // DATA(0x0E);
220 // DATA(0x0F);
221 // DATA(0x10);
222 // DATA(0x11);
223 // DATA(0x12);
224 // DATA(0x13);
225 // DATA(0x15);
226 // DATA(0x17);
227 // DATA(0x19);
228 // DATA(0x1B);
229 // DATA(0x1D);
230 // DATA(0x1F);
231 // DATA(0x21);
232 // DATA(0x23);
233 // DATA(0x25);
234 // DATA(0x27);
235 // DATA(0x2A);
236 // DATA(0x2D);
237 // DATA(0x30);
238 // DATA(0x33);
239 // DATA(0x36);
240 // DATA(0x39);
241 // DATA(0x3C);
242 // DATA(0x3F);
243 // DATA(0x42);
244 // DATA(0x45);
245 // DATA(0x48);
246 // DATA(0x4C);
247 // DATA(0x50);
248 // DATA(0x54);
249 // DATA(0x58);
250 // DATA(0x5C);
251 // DATA(0x60);
252 // DATA(0x64);
253 // DATA(0x68);
254 // DATA(0x6C);
255 // DATA(0x70);
256 // DATA(0x74);
257 // DATA(0x78);
258 // DATA(0x7D);
259 // DATA(0x82);
260 // DATA(0x87);
261 // DATA(0x8C);
262 // DATA(0x91);
263 // DATA(0x96);
264 // DATA(0x9B);
265 // DATA(0xA0);
266 // DATA(0xA5);
267 // DATA(0xAA);
268 // DATA(0xAF);
269 // DATA(0xBF);
270
271 // Clear screen
272 lcdFillRGB(COLOR_BLACK);
273
274 // Turn the display on
275 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON);
276 }
277
278 /**************************************************************************/
279 /*!
280 @brief Enables or disables the LCD backlight
281 */
282 /**************************************************************************/
283 void lcdBacklight(bool state)
284 {
285 // No backlight ... do nothing
286 }
287
288 /**************************************************************************/
289 /*!
290 @brief Renders a simple test pattern on the LCD
291 */
292 /**************************************************************************/
293 void lcdTest(void)
294 {
295 uint32_t i,j;
296 CMD(SSD1351_CMD_WRITERAM);
297 ssd1351SetCursor(0, 0);
298 CMD(SSD1351_CMD_WRITERAM);
299
300 for(i=0;i<128;i++)
301 {
302 for(j=0;j<128;j++)
303 {
304 if(i>111){DATA(COLOR_WHITE>>8);DATA((uint8_t)COLOR_WHITE);}
305 else if(i>95){DATA(COLOR_BLUE>>8);DATA((uint8_t)COLOR_BLUE);}
306 else if(i>79){DATA(COLOR_GREEN>>8);DATA((uint8_t)COLOR_GREEN);}
307 else if(i>63){DATA(COLOR_CYAN>>8);DATA((uint8_t)COLOR_CYAN);}
308 else if(i>47){DATA(COLOR_RED>>8);DATA((uint8_t)COLOR_RED);}
309 else if(i>31){DATA(COLOR_MAGENTA>>8);DATA((uint8_t)COLOR_MAGENTA);}
310 else if(i>15){DATA(COLOR_YELLOW>>8);DATA((uint8_t)COLOR_YELLOW);}
311 else {DATA(COLOR_BLACK>>8);DATA((uint8_t)COLOR_BLACK);}
312 }
313 }
314 }
315
316 /**************************************************************************/
317 /*!
318 @brief Fills the LCD with the specified 16-bit color
319 */
320 /**************************************************************************/
321 void lcdFillRGB(uint16_t data)
322 {
323 uint16_t i;
324 ssd1351SetCursor(0, 0);
325 for (i=1; i<=((ssd1351Properties.width)*(ssd1351Properties.height)) * 2;i++)
326 {
327 DATA(data >> 8);
328 DATA(data);
329 }
330 }
331
332 /**************************************************************************/
333 /*!
334 @brief Draws a single pixel at the specified X/Y location
335 */
336 /**************************************************************************/
337 void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color)
338 {
339 if ((x >= ssd1351Properties.width) || (y >= ssd1351Properties.height))
340 return;
341
342 ssd1351SetCursor((uint8_t)x, (uint8_t)y);
343 DATA(color >> 8);
344 DATA(color);
345 }
346
347 /**************************************************************************/
348 /*!
349 @brief Draws an array of consecutive RGB565 pixels (much
350 faster than addressing each pixel individually)
351 */
352 /**************************************************************************/
353 void lcdDrawPixels(uint16_t x, uint16_t y, uint16_t *data, uint32_t len)
354 {
355 // ToDo
356 }
357
358 /**************************************************************************/
359 /*!
360 @brief Optimised routine to draw a horizontal line faster than
361 setting individual pixels
362 */
363 /**************************************************************************/
364 void lcdDrawHLine(uint16_t x0, uint16_t x1, uint16_t y, uint16_t color)
365 {
366 // ToDo
367 }
368
369 /**************************************************************************/
370 /*!
371 @brief Optimised routine to draw a vertical line faster than
372 setting individual pixels
373 */
374 /**************************************************************************/
375 void lcdDrawVLine(uint16_t x, uint16_t y0, uint16_t y1, uint16_t color)
376 {
377 // ToDo
378 }
379
380 /**************************************************************************/
381 /*!
382 @brief Gets the 16-bit color of the pixel at the specified location
383 */
384 /**************************************************************************/
385 uint16_t lcdGetPixel(uint16_t x, uint16_t y)
386 {
387 // ToDo
388 return 0;
389 }
390
391 /**************************************************************************/
392 /*!
393 @brief Sets the LCD orientation to horizontal and vertical
394 */
395 /**************************************************************************/
396 void lcdSetOrientation(lcdOrientation_t orientation)
397 {
398 // Not supported
399 }
400
401 /**************************************************************************/
402 /*!
403 @brief Gets the current screen orientation (horizontal or vertical)
404 */
405 /**************************************************************************/
406 lcdOrientation_t lcdGetOrientation(void)
407 {
408 return lcdOrientation;
409 }
410
411 /**************************************************************************/
412 /*!
413 @brief Gets the width in pixels of the LCD screen (varies depending
414 on the current screen orientation)
415 */
416 /**************************************************************************/
417 uint16_t lcdGetWidth(void)
418 {
419 return ssd1351Properties.width;
420 }
421
422 /**************************************************************************/
423 /*!
424 @brief Gets the height in pixels of the LCD screen (varies depending
425 on the current screen orientation)
426 */
427 /**************************************************************************/
428 uint16_t lcdGetHeight(void)
429 {
430 return ssd1351Properties.height;
431 }
432
433 /**************************************************************************/
434 /*!
435 @brief Scrolls the contents of the LCD screen vertically the
436 specified number of pixels using a HW optimised routine
437 */
438 /**************************************************************************/
439 void lcdScroll(int16_t pixels, uint16_t fillColor)
440 {
441 // ToDo
442 }
443
444 /**************************************************************************/
445 /*!
446 @brief Gets the controller's 16-bit (4 hexdigit) ID
447 */
448 /**************************************************************************/
449 uint16_t lcdGetControllerID(void)
450 {
451 return 0x1351;
452 }
453
454 /**************************************************************************/
455 /*!
456 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
457 generic capabilities and dimensions
458 */
459 /**************************************************************************/
460 lcdProperties_t lcdGetProperties(void)
461 {
462 return ssd1351Properties;
463 }
This page took 0.073265 seconds and 5 git commands to generate.