Fixed typos
[hackover2013-badge-firmware.git] / drivers / lcd / 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 Returns the 16-bit (4-hexdigit) controller code
109 */
110 /**************************************************************************/
111 uint16_t ssd1351Type(void)
112 {
113 return 0x1351;
114 }
115
116 /**************************************************************************/
117 /*!
118 @brief Sets the cursor to the specified X/Y position
119 */
120 /**************************************************************************/
121 void ssd1351SetCursor(uint8_t x, uint8_t y)
122 {
123 if ((x >= ssd1351Properties.width) || (y >= ssd1351Properties.height))
124 return;
125
126 CMD(SSD1351_CMD_SETCOLUMNADDRESS);
127 DATA(x); // Start Address
128 DATA(ssd1351Properties.width-1); // End Address (0x7F)
129
130 CMD(SSD1351_CMD_SETROWADDRESS);
131 DATA(y); // Start Address
132 DATA(ssd1351Properties.height-1); // End Address (0x7F)
133 }
134
135 /*************************************************/
136 /* Public Methods */
137 /*************************************************/
138
139 /**************************************************************************/
140 /*!
141 @brief Configures any pins or HW and initialises the LCD controller
142 */
143 /**************************************************************************/
144 void lcdInit(void)
145 {
146 // Set all pins to output
147 gpioSetDir(SSD1351_SCK_PORT, SSD1351_SCK_PIN, gpioDirection_Output);
148 gpioSetDir(SSD1351_SID_PORT, SSD1351_SID_PIN, gpioDirection_Output);
149 gpioSetDir(SSD1351_RST_PORT, SSD1351_RST_PIN, gpioDirection_Output);
150 gpioSetDir(SSD1351_CS_PORT, SSD1351_CS_PIN, gpioDirection_Output);
151
152 #if !defined SSD1351_BUS_SPI3
153 gpioSetDir(SSD1351_DC_PORT, SSD1351_DC_PIN, gpioDirection_Output);
154 #endif
155
156 // Reset the LCD
157 SET_RST;
158 DELAY(20);
159 CLR_RST;
160 DELAY(200);
161 SET_RST;
162 DELAY(20);
163
164 // Disable pullups
165 SSD1351_DISABLEPULLUPS();
166
167 CMD(SSD1351_CMD_SETCOMMANDLOCK);
168 DATA(0x12); // Unlocked to enter commands
169 CMD(SSD1351_CMD_SETCOMMANDLOCK);
170 DATA(0xB1); // Make all commands accessible
171 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYOFF);
172 CMD(SSD1351_CMD_SETFRONTCLOCKDIV);
173 DATA(0xF1);
174 CMD(SSD1351_CMD_SETMUXRRATIO);
175 DATA(0x7F);
176 CMD(SSD1351_CMD_COLORDEPTH);
177 DATA(0x74); // 65,536 Colors
178 CMD(SSD1351_CMD_SETCOLUMNADDRESS);
179 DATA(0x00);
180 DATA(0x7F);
181 CMD(SSD1351_CMD_SETROWADDRESS);
182 DATA(0x00);
183 DATA(0x7F);
184 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE);
185 DATA(0x00);
186 CMD(SSD1351_CMD_SETDISPLAYOFFSET);
187 DATA(0x00);
188 CMD(SSD1351_CMD_SETGPIO);
189 DATA(0x00); // Disable GPIO pins
190 CMD(SSD1351_CMD_FUNCTIONSELECTION);
191 DATA(0x00); // External VDD
192 CMD(SSD1351_CMD_SETPHASELENGTH);
193 DATA(0x32);
194 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE);
195 DATA(0xA0); // Enable External VSL
196 DATA(0xB5);
197 DATA(0x55);
198 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE);
199 DATA(0x17);
200 CMD(SSD1351_CMD_SETVCOMHVOLTAGE);
201 DATA(0x05);
202 CMD(SSD1351_CMD_SETCONTRAST);
203 DATA(0xC8);
204 DATA(0x80);
205 DATA(0xC8);
206 CMD(SSD1351_CMD_MASTERCONTRAST);
207 DATA(0x0F); // Maximum contrast
208 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD);
209 DATA(0x01);
210 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET);
211
212 // Use default grayscale for now to save flash space, but here are
213 // the values if someone wants to change them ...
214 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
215 // DATA(0x02);
216 // DATA(0x03);
217 // DATA(0x04);
218 // DATA(0x05);
219 // DATA(0x06);
220 // DATA(0x07);
221 // DATA(0x08);
222 // DATA(0x09);
223 // DATA(0x0A);
224 // DATA(0x0B);
225 // DATA(0x0C);
226 // DATA(0x0D);
227 // DATA(0x0E);
228 // DATA(0x0F);
229 // DATA(0x10);
230 // DATA(0x11);
231 // DATA(0x12);
232 // DATA(0x13);
233 // DATA(0x15);
234 // DATA(0x17);
235 // DATA(0x19);
236 // DATA(0x1B);
237 // DATA(0x1D);
238 // DATA(0x1F);
239 // DATA(0x21);
240 // DATA(0x23);
241 // DATA(0x25);
242 // DATA(0x27);
243 // DATA(0x2A);
244 // DATA(0x2D);
245 // DATA(0x30);
246 // DATA(0x33);
247 // DATA(0x36);
248 // DATA(0x39);
249 // DATA(0x3C);
250 // DATA(0x3F);
251 // DATA(0x42);
252 // DATA(0x45);
253 // DATA(0x48);
254 // DATA(0x4C);
255 // DATA(0x50);
256 // DATA(0x54);
257 // DATA(0x58);
258 // DATA(0x5C);
259 // DATA(0x60);
260 // DATA(0x64);
261 // DATA(0x68);
262 // DATA(0x6C);
263 // DATA(0x70);
264 // DATA(0x74);
265 // DATA(0x78);
266 // DATA(0x7D);
267 // DATA(0x82);
268 // DATA(0x87);
269 // DATA(0x8C);
270 // DATA(0x91);
271 // DATA(0x96);
272 // DATA(0x9B);
273 // DATA(0xA0);
274 // DATA(0xA5);
275 // DATA(0xAA);
276 // DATA(0xAF);
277 // DATA(0xBF);
278
279 // Clear screen
280 lcdFillRGB(COLOR_RED);
281
282 // Turn the display on
283 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON);
284 }
285
286 /**************************************************************************/
287 /*!
288 @brief Enables or disables the LCD backlight
289 */
290 /**************************************************************************/
291 void lcdBacklight(bool state)
292 {
293 // No backlight ... do nothing
294 }
295
296 /**************************************************************************/
297 /*!
298 @brief Renders a simple test pattern on the LCD
299 */
300 /**************************************************************************/
301 void lcdTest(void)
302 {
303 uint32_t i,j;
304 CMD(SSD1351_CMD_WRITERAM);
305 ssd1351SetCursor(0, 0);
306 CMD(SSD1351_CMD_WRITERAM);
307
308 for(i=0;i<128;i++)
309 {
310 for(j=0;j<128;j++)
311 {
312 if(i>111){DATA(COLOR_WHITE>>8);DATA((uint8_t)COLOR_WHITE);}
313 else if(i>95){DATA(COLOR_BLUE>>8);DATA((uint8_t)COLOR_BLUE);}
314 else if(i>79){DATA(COLOR_GREEN>>8);DATA((uint8_t)COLOR_GREEN);}
315 else if(i>63){DATA(COLOR_CYAN>>8);DATA((uint8_t)COLOR_CYAN);}
316 else if(i>47){DATA(COLOR_RED>>8);DATA((uint8_t)COLOR_RED);}
317 else if(i>31){DATA(COLOR_MAGENTA>>8);DATA((uint8_t)COLOR_MAGENTA);}
318 else if(i>15){DATA(COLOR_YELLOW>>8);DATA((uint8_t)COLOR_YELLOW);}
319 else {DATA(COLOR_BLACK>>8);DATA((uint8_t)COLOR_BLACK);}
320 }
321 }
322 }
323
324 /**************************************************************************/
325 /*!
326 @brief Fills the LCD with the specified 16-bit color
327 */
328 /**************************************************************************/
329 void lcdFillRGB(uint16_t data)
330 {
331 uint16_t i;
332 ssd1351SetCursor(0, 0);
333 CMD(SSD1351_CMD_WRITERAM);
334 for (i=1; i<=((ssd1351Properties.width)*(ssd1351Properties.height)) * 2;i++)
335 {
336 DATA(data);
337 DATA((data >> 8));
338 }
339 }
340
341 /**************************************************************************/
342 /*!
343 @brief Draws a single pixel at the specified X/Y location
344 */
345 /**************************************************************************/
346 void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color)
347 {
348 if ((x >= ssd1351Properties.width) || (y >= ssd1351Properties.height))
349 return;
350
351 ssd1351SetCursor((uint8_t)x, (uint8_t)y);
352 DATA(color >> 8);
353 DATA(color);
354 }
355
356 /**************************************************************************/
357 /*!
358 @brief Draws an array of consecutive RGB565 pixels (much
359 faster than addressing each pixel individually)
360 */
361 /**************************************************************************/
362 void lcdDrawPixels(uint16_t x, uint16_t y, uint16_t *data, uint32_t len)
363 {
364 // ToDo
365 }
366
367 /**************************************************************************/
368 /*!
369 @brief Optimised routine to draw a horizontal line faster than
370 setting individual pixels
371 */
372 /**************************************************************************/
373 void lcdDrawHLine(uint16_t x0, uint16_t x1, uint16_t y, uint16_t color)
374 {
375 // ToDo
376 }
377
378 /**************************************************************************/
379 /*!
380 @brief Optimised routine to draw a vertical line faster than
381 setting individual pixels
382 */
383 /**************************************************************************/
384 void lcdDrawVLine(uint16_t x, uint16_t y0, uint16_t y1, uint16_t color)
385 {
386 // ToDo
387 }
388
389 /**************************************************************************/
390 /*!
391 @brief Gets the 16-bit color of the pixel at the specified location
392 */
393 /**************************************************************************/
394 uint16_t lcdGetPixel(uint16_t x, uint16_t y)
395 {
396 // ToDo
397 return 0;
398 }
399
400 /**************************************************************************/
401 /*!
402 @brief Sets the LCD orientation to horizontal and vertical
403 */
404 /**************************************************************************/
405 void lcdSetOrientation(lcdOrientation_t orientation)
406 {
407 // Not supported
408 }
409
410 /**************************************************************************/
411 /*!
412 @brief Gets the current screen orientation (horizontal or vertical)
413 */
414 /**************************************************************************/
415 lcdOrientation_t lcdGetOrientation(void)
416 {
417 return lcdOrientation;
418 }
419
420 /**************************************************************************/
421 /*!
422 @brief Gets the width in pixels of the LCD screen (varies depending
423 on the current screen orientation)
424 */
425 /**************************************************************************/
426 uint16_t lcdGetWidth(void)
427 {
428 return ssd1351Properties.width;
429 }
430
431 /**************************************************************************/
432 /*!
433 @brief Gets the height in pixels of the LCD screen (varies depending
434 on the current screen orientation)
435 */
436 /**************************************************************************/
437 uint16_t lcdGetHeight(void)
438 {
439 return ssd1351Properties.height;
440 }
441
442 /**************************************************************************/
443 /*!
444 @brief Scrolls the contents of the LCD screen vertically the
445 specified number of pixels using a HW optimised routine
446 */
447 /**************************************************************************/
448 void lcdScroll(int16_t pixels, uint16_t fillColor)
449 {
450 // ToDo
451 }
452
453 /**************************************************************************/
454 /*!
455 @brief Gets the controller's 16-bit (4 hexdigit) ID
456 */
457 /**************************************************************************/
458 uint16_t lcdGetControllerID(void)
459 {
460 return ssd1351Type();
461 }
462
463 /**************************************************************************/
464 /*!
465 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
466 generic capabilities and dimensions
467 */
468 /**************************************************************************/
469 lcdProperties_t lcdGetProperties(void)
470 {
471 return ssd1351Properties;
472 }
This page took 0.072953 seconds and 5 git commands to generate.