Fixed cast warning
[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 (0 = Internal, 1 = External???)*
192 // Which is it ... DS is contradictory here!
193 CMD(SSD1351_CMD_SETPHASELENGTH);
194 DATA(0x32);
195 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE);
196 DATA(0xA0); // Enable External VSL
197 DATA(0xB5);
198 DATA(0x55);
199 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE);
200 DATA(0x17);
201 CMD(SSD1351_CMD_SETVCOMHVOLTAGE);
202 DATA(0x05);
203 CMD(SSD1351_CMD_SETCONTRAST);
204 DATA(0xC8);
205 DATA(0x80);
206 DATA(0xC8);
207 CMD(SSD1351_CMD_MASTERCONTRAST);
208 DATA(0x0F); // Maximum contrast
209 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD);
210 DATA(0x01);
211 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET);
212
213 // Use default grayscale for now to save flash space, but here are
214 // the values if someone wants to change them ...
215 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
216 // DATA(0x02);
217 // DATA(0x03);
218 // DATA(0x04);
219 // DATA(0x05);
220 // DATA(0x06);
221 // DATA(0x07);
222 // DATA(0x08);
223 // DATA(0x09);
224 // DATA(0x0A);
225 // DATA(0x0B);
226 // DATA(0x0C);
227 // DATA(0x0D);
228 // DATA(0x0E);
229 // DATA(0x0F);
230 // DATA(0x10);
231 // DATA(0x11);
232 // DATA(0x12);
233 // DATA(0x13);
234 // DATA(0x15);
235 // DATA(0x17);
236 // DATA(0x19);
237 // DATA(0x1B);
238 // DATA(0x1D);
239 // DATA(0x1F);
240 // DATA(0x21);
241 // DATA(0x23);
242 // DATA(0x25);
243 // DATA(0x27);
244 // DATA(0x2A);
245 // DATA(0x2D);
246 // DATA(0x30);
247 // DATA(0x33);
248 // DATA(0x36);
249 // DATA(0x39);
250 // DATA(0x3C);
251 // DATA(0x3F);
252 // DATA(0x42);
253 // DATA(0x45);
254 // DATA(0x48);
255 // DATA(0x4C);
256 // DATA(0x50);
257 // DATA(0x54);
258 // DATA(0x58);
259 // DATA(0x5C);
260 // DATA(0x60);
261 // DATA(0x64);
262 // DATA(0x68);
263 // DATA(0x6C);
264 // DATA(0x70);
265 // DATA(0x74);
266 // DATA(0x78);
267 // DATA(0x7D);
268 // DATA(0x82);
269 // DATA(0x87);
270 // DATA(0x8C);
271 // DATA(0x91);
272 // DATA(0x96);
273 // DATA(0x9B);
274 // DATA(0xA0);
275 // DATA(0xA5);
276 // DATA(0xAA);
277 // DATA(0xAF);
278 // DATA(0xBF);
279
280 // Clear screen
281 lcdFillRGB(COLOR_RED);
282
283 // Turn the display on
284 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON);
285 }
286
287 /**************************************************************************/
288 /*!
289 @brief Enables or disables the LCD backlight
290 */
291 /**************************************************************************/
292 void lcdBacklight(bool state)
293 {
294 // No backlight ... do nothing
295 }
296
297 /**************************************************************************/
298 /*!
299 @brief Renders a simple test pattern on the LCD
300 */
301 /**************************************************************************/
302 void lcdTest(void)
303 {
304 uint32_t i,j;
305 CMD(SSD1351_CMD_WRITERAM);
306 ssd1351SetCursor(0, 0);
307 CMD(SSD1351_CMD_WRITERAM);
308
309 for(i=0;i<128;i++)
310 {
311 for(j=0;j<128;j++)
312 {
313 if(i>111){DATA(COLOR_WHITE>>8);DATA((uint8_t)COLOR_WHITE);}
314 else if(i>95){DATA(COLOR_BLUE>>8);DATA((uint8_t)COLOR_BLUE);}
315 else if(i>79){DATA(COLOR_GREEN>>8);DATA((uint8_t)COLOR_GREEN);}
316 else if(i>63){DATA(COLOR_CYAN>>8);DATA((uint8_t)COLOR_CYAN);}
317 else if(i>47){DATA(COLOR_RED>>8);DATA((uint8_t)COLOR_RED);}
318 else if(i>31){DATA(COLOR_MAGENTA>>8);DATA((uint8_t)COLOR_MAGENTA);}
319 else if(i>15){DATA(COLOR_YELLOW>>8);DATA((uint8_t)COLOR_YELLOW);}
320 else {DATA(COLOR_BLACK>>8);DATA((uint8_t)COLOR_BLACK);}
321 }
322 }
323 }
324
325 /**************************************************************************/
326 /*!
327 @brief Fills the LCD with the specified 16-bit color
328 */
329 /**************************************************************************/
330 void lcdFillRGB(uint16_t data)
331 {
332 uint16_t i;
333 ssd1351SetCursor(0, 0);
334 CMD(SSD1351_CMD_WRITERAM);
335 for (i=1; i<=((ssd1351Properties.width)*(ssd1351Properties.height)) * 2;i++)
336 {
337 DATA(data);
338 DATA((data >> 8));
339 }
340 }
341
342 /**************************************************************************/
343 /*!
344 @brief Draws a single pixel at the specified X/Y location
345 */
346 /**************************************************************************/
347 void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color)
348 {
349 if ((x >= ssd1351Properties.width) || (y >= ssd1351Properties.height))
350 return;
351
352 ssd1351SetCursor((uint8_t)x, (uint8_t)y);
353 DATA(color >> 8);
354 DATA(color);
355 }
356
357 /**************************************************************************/
358 /*!
359 @brief Draws an array of consecutive RGB565 pixels (much
360 faster than addressing each pixel individually)
361 */
362 /**************************************************************************/
363 void lcdDrawPixels(uint16_t x, uint16_t y, uint16_t *data, uint32_t len)
364 {
365 // ToDo
366 }
367
368 /**************************************************************************/
369 /*!
370 @brief Optimised routine to draw a horizontal line faster than
371 setting individual pixels
372 */
373 /**************************************************************************/
374 void lcdDrawHLine(uint16_t x0, uint16_t x1, uint16_t y, uint16_t color)
375 {
376 // ToDo
377 }
378
379 /**************************************************************************/
380 /*!
381 @brief Optimised routine to draw a vertical line faster than
382 setting individual pixels
383 */
384 /**************************************************************************/
385 void lcdDrawVLine(uint16_t x, uint16_t y0, uint16_t y1, uint16_t color)
386 {
387 // ToDo
388 }
389
390 /**************************************************************************/
391 /*!
392 @brief Gets the 16-bit color of the pixel at the specified location
393 */
394 /**************************************************************************/
395 uint16_t lcdGetPixel(uint16_t x, uint16_t y)
396 {
397 // ToDo
398 return 0;
399 }
400
401 /**************************************************************************/
402 /*!
403 @brief Sets the LCD orientation to horizontal and vertical
404 */
405 /**************************************************************************/
406 void lcdSetOrientation(lcdOrientation_t orientation)
407 {
408 // Not supported
409 }
410
411 /**************************************************************************/
412 /*!
413 @brief Gets the current screen orientation (horizontal or vertical)
414 */
415 /**************************************************************************/
416 lcdOrientation_t lcdGetOrientation(void)
417 {
418 return lcdOrientation;
419 }
420
421 /**************************************************************************/
422 /*!
423 @brief Gets the width in pixels of the LCD screen (varies depending
424 on the current screen orientation)
425 */
426 /**************************************************************************/
427 uint16_t lcdGetWidth(void)
428 {
429 return ssd1351Properties.width;
430 }
431
432 /**************************************************************************/
433 /*!
434 @brief Gets the height in pixels of the LCD screen (varies depending
435 on the current screen orientation)
436 */
437 /**************************************************************************/
438 uint16_t lcdGetHeight(void)
439 {
440 return ssd1351Properties.height;
441 }
442
443 /**************************************************************************/
444 /*!
445 @brief Scrolls the contents of the LCD screen vertically the
446 specified number of pixels using a HW optimised routine
447 */
448 /**************************************************************************/
449 void lcdScroll(int16_t pixels, uint16_t fillColor)
450 {
451 // ToDo
452 }
453
454 /**************************************************************************/
455 /*!
456 @brief Gets the controller's 16-bit (4 hexdigit) ID
457 */
458 /**************************************************************************/
459 uint16_t lcdGetControllerID(void)
460 {
461 return ssd1351Type();
462 }
463
464 /**************************************************************************/
465 /*!
466 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
467 generic capabilities and dimensions
468 */
469 /**************************************************************************/
470 lcdProperties_t lcdGetProperties(void)
471 {
472 return ssd1351Properties;
473 }
This page took 0.069515 seconds and 5 git commands to generate.