1 /**************************************************************************/
4 @author K. Townsend (www.adafruit.com)
8 Driver for SSD1351 128x128 pixel RGB OLED displays.
10 This driver uses a 3 or 4-pin SPI interface and 16-bit RGB565 colours.
14 Software License Agreement (BSD License)
16 Copyright (c) 2012, Adafruit Industries
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.
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.
41 /**************************************************************************/
43 #include "core/systick/systick.h"
45 static volatile lcdOrientation_t lcdOrientation
= LCD_ORIENTATION_PORTRAIT
;
46 static lcdProperties_t ssd1351Properties
= { 128, 128, false, false, false };
48 /*************************************************/
50 /*************************************************/
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);
56 /**************************************************************************/
58 @brief Simulates an SPI write using GPIO.
63 1 if this is a command, 0 if it is data
65 /**************************************************************************/
66 void ssd1351SendByte(uint8_t byte
, uint8_t command
)
70 // Make sure clock pin starts high
73 #if defined SSD1351_BUS_SPI3
74 // Prepend D/C bit (cmd = 0, data = 1)
87 // Write from MSB to LSB
92 // Set data pin high or low depending on the value of the current bit
101 // Set clock pin high
106 /**************************************************************************/
108 @brief Returns the 16-bit (4-hexdigit) controller code
110 /**************************************************************************/
111 uint16_t ssd1351Type(void)
116 /**************************************************************************/
118 @brief Sets the cursor to the specified X/Y position
120 /**************************************************************************/
121 void ssd1351SetCursor(uint8_t x
, uint8_t y
)
123 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
126 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
127 DATA(x
); // Start Address
128 DATA(ssd1351Properties
.width
-1); // End Address (0x7F)
130 CMD(SSD1351_CMD_SETROWADDRESS
);
131 DATA(y
); // Start Address
132 DATA(ssd1351Properties
.height
-1); // End Address (0x7F)
135 /*************************************************/
137 /*************************************************/
139 /**************************************************************************/
141 @brief Configures any pins or HW and initialises the LCD controller
143 /**************************************************************************/
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
);
152 #if !defined SSD1351_BUS_SPI3
153 gpioSetDir(SSD1351_DC_PORT
, SSD1351_DC_PIN
, gpioDirection_Output
);
165 SSD1351_DISABLEPULLUPS();
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
);
174 CMD(SSD1351_CMD_SETMUXRRATIO
);
176 CMD(SSD1351_CMD_COLORDEPTH
);
177 DATA(0x74); // 65,536 Colors
178 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
181 CMD(SSD1351_CMD_SETROWADDRESS
);
184 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE
);
186 CMD(SSD1351_CMD_SETDISPLAYOFFSET
);
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
);
195 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE
);
196 DATA(0xA0); // Enable External VSL
199 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE
);
201 CMD(SSD1351_CMD_SETVCOMHVOLTAGE
);
203 CMD(SSD1351_CMD_SETCONTRAST
);
207 CMD(SSD1351_CMD_MASTERCONTRAST
);
208 DATA(0x0F); // Maximum contrast
209 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD
);
211 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET
);
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);
281 lcdFillRGB(COLOR_RED
);
283 // Turn the display on
284 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON
);
287 /**************************************************************************/
289 @brief Enables or disables the LCD backlight
291 /**************************************************************************/
292 void lcdBacklight(bool state
)
294 // No backlight ... do nothing
297 /**************************************************************************/
299 @brief Renders a simple test pattern on the LCD
301 /**************************************************************************/
305 CMD(SSD1351_CMD_WRITERAM
);
306 ssd1351SetCursor(0, 0);
307 CMD(SSD1351_CMD_WRITERAM
);
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
);}
325 /**************************************************************************/
327 @brief Fills the LCD with the specified 16-bit color
329 /**************************************************************************/
330 void lcdFillRGB(uint16_t data
)
333 ssd1351SetCursor(0, 0);
334 CMD(SSD1351_CMD_WRITERAM
);
335 for (i
=1; i
<=((ssd1351Properties
.width
)*(ssd1351Properties
.height
)) * 2;i
++)
342 /**************************************************************************/
344 @brief Draws a single pixel at the specified X/Y location
346 /**************************************************************************/
347 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
349 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
352 ssd1351SetCursor((uint8_t)x
, (uint8_t)y
);
357 /**************************************************************************/
359 @brief Draws an array of consecutive RGB565 pixels (much
360 faster than addressing each pixel individually)
362 /**************************************************************************/
363 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
368 /**************************************************************************/
370 @brief Optimised routine to draw a horizontal line faster than
371 setting individual pixels
373 /**************************************************************************/
374 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
379 /**************************************************************************/
381 @brief Optimised routine to draw a vertical line faster than
382 setting individual pixels
384 /**************************************************************************/
385 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
390 /**************************************************************************/
392 @brief Gets the 16-bit color of the pixel at the specified location
394 /**************************************************************************/
395 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
401 /**************************************************************************/
403 @brief Sets the LCD orientation to horizontal and vertical
405 /**************************************************************************/
406 void lcdSetOrientation(lcdOrientation_t orientation
)
411 /**************************************************************************/
413 @brief Gets the current screen orientation (horizontal or vertical)
415 /**************************************************************************/
416 lcdOrientation_t
lcdGetOrientation(void)
418 return lcdOrientation
;
421 /**************************************************************************/
423 @brief Gets the width in pixels of the LCD screen (varies depending
424 on the current screen orientation)
426 /**************************************************************************/
427 uint16_t lcdGetWidth(void)
429 return ssd1351Properties
.width
;
432 /**************************************************************************/
434 @brief Gets the height in pixels of the LCD screen (varies depending
435 on the current screen orientation)
437 /**************************************************************************/
438 uint16_t lcdGetHeight(void)
440 return ssd1351Properties
.height
;
443 /**************************************************************************/
445 @brief Scrolls the contents of the LCD screen vertically the
446 specified number of pixels using a HW optimised routine
448 /**************************************************************************/
449 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
454 /**************************************************************************/
456 @brief Gets the controller's 16-bit (4 hexdigit) ID
458 /**************************************************************************/
459 uint16_t lcdGetControllerID(void)
461 return ssd1351Type();
464 /**************************************************************************/
466 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
467 generic capabilities and dimensions
469 /**************************************************************************/
470 lcdProperties_t
lcdGetProperties(void)
472 return ssd1351Properties
;