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, true, 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 Sets the cursor to the specified X/Y position
110 /**************************************************************************/
111 void ssd1351SetCursor(uint8_t x
, uint8_t y
)
113 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
116 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
117 DATA(x
); // Start Address
118 DATA(ssd1351Properties
.width
-1); // End Address (0x7F)
120 CMD(SSD1351_CMD_SETROWADDRESS
);
121 DATA(y
); // Start Address
122 DATA(ssd1351Properties
.height
-1); // End Address (0x7F)
125 /**************************************************************************/
127 @brief Sets the cursor to 0, 0 and resets the window size
129 /**************************************************************************/
130 void ssd1351GoHome(void)
132 ssd1351SetCursor(0, 0);
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
);
156 // Make sure CS starts low
167 // Disable internal pullup resistors
168 SSD1351_DISABLEPULLUPS();
170 CMD(SSD1351_CMD_SETCOMMANDLOCK
);
171 DATA(0x12); // Unlocked to enter commands
173 CMD(SSD1351_CMD_SETCOMMANDLOCK
);
174 DATA(0xB1); // Make all commands accessible
176 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYOFF
);
178 CMD(SSD1351_CMD_SETFRONTCLOCKDIV
);
179 DATA(0xF1); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
181 CMD(SSD1351_CMD_SETMUXRRATIO
);
184 CMD(SSD1351_CMD_COLORDEPTH
);
185 DATA(0x74); // Bit 7:6 = 65,536 Colors, Bit 3 = BGR or RGB
187 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
191 CMD(SSD1351_CMD_SETROWADDRESS
);
195 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE
);
198 CMD(SSD1351_CMD_SETDISPLAYOFFSET
);
201 CMD(SSD1351_CMD_SETGPIO
);
202 DATA(0x00); // Disable GPIO pins
204 CMD(SSD1351_CMD_FUNCTIONSELECTION
);
205 DATA(0x01); // External VDD (1 = External bias using diode drop, 0 = internal)
207 CMD(SSD1351_CMD_SETPHASELENGTH
);
210 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE
);
211 DATA(0x32); // was 0x17
213 CMD(SSD1351_CMD_SETVCOMHVOLTAGE
);
216 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET
);
218 CMD(SSD1351_CMD_SETCONTRAST
);
223 CMD(SSD1351_CMD_MASTERCONTRAST
);
224 DATA(0x0F); // Maximum contrast
226 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE
);
227 DATA(0xA0); // Enable External VSL
231 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD
);
234 // Use default grayscale for now to save flash space, but here are
235 // the values if someone wants to change them ...
236 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
302 lcdFillRGB(COLOR_BLACK
);
304 // Turn the display on
305 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON
);
308 /**************************************************************************/
310 @brief Enables or disables the LCD backlight
312 /**************************************************************************/
313 void lcdBacklight(bool state
)
315 // No backlight ... do nothing
318 /**************************************************************************/
320 @brief Renders a simple test pattern on the LCD
322 /**************************************************************************/
326 CMD(SSD1351_CMD_WRITERAM
);
327 ssd1351SetCursor(0, 0);
328 CMD(SSD1351_CMD_WRITERAM
);
334 if(i
>111){DATA(COLOR_WHITE
>>8);DATA((uint8_t)COLOR_WHITE
);}
335 else if(i
>95){DATA(COLOR_BLUE
>>8);DATA((uint8_t)COLOR_BLUE
);}
336 else if(i
>79){DATA(COLOR_GREEN
>>8);DATA((uint8_t)COLOR_GREEN
);}
337 else if(i
>63){DATA(COLOR_CYAN
>>8);DATA((uint8_t)COLOR_CYAN
);}
338 else if(i
>47){DATA(COLOR_RED
>>8);DATA((uint8_t)COLOR_RED
);}
339 else if(i
>31){DATA(COLOR_MAGENTA
>>8);DATA((uint8_t)COLOR_MAGENTA
);}
340 else if(i
>15){DATA(COLOR_YELLOW
>>8);DATA((uint8_t)COLOR_YELLOW
);}
341 else {DATA(COLOR_BLACK
>>8);DATA((uint8_t)COLOR_BLACK
);}
346 /**************************************************************************/
348 @brief Fills the LCD with the specified 16-bit color
350 /**************************************************************************/
351 void lcdFillRGB(uint16_t data
)
354 ssd1351SetCursor(0, 0);
355 CMD(SSD1351_CMD_WRITERAM
);
356 for (i
=1; i
<=((ssd1351Properties
.width
)*(ssd1351Properties
.height
)) * 2;i
++)
363 /**************************************************************************/
365 @brief Draws a single pixel at the specified X/Y location
367 /**************************************************************************/
368 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
370 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
373 ssd1351SetCursor((uint8_t)x
, (uint8_t)y
);
374 CMD(SSD1351_CMD_WRITERAM
);
379 /**************************************************************************/
381 @brief Draws an array of consecutive RGB565 pixels (much
382 faster than addressing each pixel individually)
384 /**************************************************************************/
385 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
390 /**************************************************************************/
392 @brief Optimised routine to draw a horizontal line faster than
393 setting individual pixels
395 /**************************************************************************/
396 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
398 // Allows for slightly better performance than setting individual pixels
410 if (x1
>= ssd1351Properties
.width
)
412 x1
= ssd1351Properties
.width
- 1;
414 if (x0
>= ssd1351Properties
.width
)
416 x0
= ssd1351Properties
.width
- 1;
419 ssd1351SetCursor(x0
, y
);
420 CMD(SSD1351_CMD_WRITERAM
);
421 for (pixels
= 0; pixels
< x1
- x0
+ 1; pixels
++)
429 /**************************************************************************/
431 @brief Optimised routine to draw a vertical line faster than
432 setting individual pixels
434 /**************************************************************************/
435 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
440 /**************************************************************************/
442 @brief Gets the 16-bit color of the pixel at the specified location
444 /**************************************************************************/
445 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
451 /**************************************************************************/
453 @brief Sets the LCD orientation to horizontal and vertical
455 /**************************************************************************/
456 void lcdSetOrientation(lcdOrientation_t orientation
)
461 /**************************************************************************/
463 @brief Gets the current screen orientation (horizontal or vertical)
465 /**************************************************************************/
466 lcdOrientation_t
lcdGetOrientation(void)
468 return lcdOrientation
;
471 /**************************************************************************/
473 @brief Gets the width in pixels of the LCD screen (varies depending
474 on the current screen orientation)
476 /**************************************************************************/
477 uint16_t lcdGetWidth(void)
479 return ssd1351Properties
.width
;
482 /**************************************************************************/
484 @brief Gets the height in pixels of the LCD screen (varies depending
485 on the current screen orientation)
487 /**************************************************************************/
488 uint16_t lcdGetHeight(void)
490 return ssd1351Properties
.height
;
493 /**************************************************************************/
495 @brief Scrolls the contents of the LCD screen vertically the
496 specified number of pixels using a HW optimised routine
498 /**************************************************************************/
499 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
504 /**************************************************************************/
506 @brief Gets the controller's 16-bit (4 hexdigit) ID
508 /**************************************************************************/
509 uint16_t lcdGetControllerID(void)
514 /**************************************************************************/
516 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
517 generic capabilities and dimensions
519 /**************************************************************************/
520 lcdProperties_t
lcdGetProperties(void)
522 return ssd1351Properties
;