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 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_WRITERAM
);
117 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
118 DATA(x
); // Start Address
119 DATA(ssd1351Properties
.width
-1); // End Address (0x7F)
121 CMD(SSD1351_CMD_SETROWADDRESS
);
122 DATA(y
); // Start Address
123 DATA(ssd1351Properties
.height
-1); // End Address (0x7F)
124 CMD(SSD1351_CMD_WRITERAM
);
127 /*************************************************/
129 /*************************************************/
131 /**************************************************************************/
133 @brief Configures any pins or HW and initialises the LCD controller
135 /**************************************************************************/
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
);
144 #if !defined SSD1351_BUS_SPI3
145 gpioSetDir(SSD1351_DC_PORT
, SSD1351_DC_PIN
, gpioDirection_Output
);
157 SSD1351_DISABLEPULLUPS();
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
);
166 CMD(SSD1351_CMD_SETMUXRRATIO
);
168 CMD(SSD1351_CMD_COLORDEPTH
);
169 DATA(0x74); // Bit 7:6 = 65,536 Colors, Bit 3 = BGR or RGB
170 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
173 CMD(SSD1351_CMD_SETROWADDRESS
);
176 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE
);
178 CMD(SSD1351_CMD_SETDISPLAYOFFSET
);
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
);
186 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE
);
187 DATA(0xA0); // Enable External VSL
190 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE
);
192 CMD(SSD1351_CMD_SETVCOMHVOLTAGE
);
194 CMD(SSD1351_CMD_SETCONTRAST
);
198 CMD(SSD1351_CMD_MASTERCONTRAST
);
199 DATA(0x0F); // Maximum contrast
200 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD
);
202 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET
);
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);
272 lcdFillRGB(COLOR_BLACK
);
274 // Turn the display on
275 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON
);
278 /**************************************************************************/
280 @brief Enables or disables the LCD backlight
282 /**************************************************************************/
283 void lcdBacklight(bool state
)
285 // No backlight ... do nothing
288 /**************************************************************************/
290 @brief Renders a simple test pattern on the LCD
292 /**************************************************************************/
296 CMD(SSD1351_CMD_WRITERAM
);
297 ssd1351SetCursor(0, 0);
298 CMD(SSD1351_CMD_WRITERAM
);
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
);}
316 /**************************************************************************/
318 @brief Fills the LCD with the specified 16-bit color
320 /**************************************************************************/
321 void lcdFillRGB(uint16_t data
)
324 ssd1351SetCursor(0, 0);
325 for (i
=1; i
<=((ssd1351Properties
.width
)*(ssd1351Properties
.height
)) * 2;i
++)
332 /**************************************************************************/
334 @brief Draws a single pixel at the specified X/Y location
336 /**************************************************************************/
337 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
339 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
342 ssd1351SetCursor((uint8_t)x
, (uint8_t)y
);
347 /**************************************************************************/
349 @brief Draws an array of consecutive RGB565 pixels (much
350 faster than addressing each pixel individually)
352 /**************************************************************************/
353 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
358 /**************************************************************************/
360 @brief Optimised routine to draw a horizontal line faster than
361 setting individual pixels
363 /**************************************************************************/
364 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
369 /**************************************************************************/
371 @brief Optimised routine to draw a vertical line faster than
372 setting individual pixels
374 /**************************************************************************/
375 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
380 /**************************************************************************/
382 @brief Gets the 16-bit color of the pixel at the specified location
384 /**************************************************************************/
385 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
391 /**************************************************************************/
393 @brief Sets the LCD orientation to horizontal and vertical
395 /**************************************************************************/
396 void lcdSetOrientation(lcdOrientation_t orientation
)
401 /**************************************************************************/
403 @brief Gets the current screen orientation (horizontal or vertical)
405 /**************************************************************************/
406 lcdOrientation_t
lcdGetOrientation(void)
408 return lcdOrientation
;
411 /**************************************************************************/
413 @brief Gets the width in pixels of the LCD screen (varies depending
414 on the current screen orientation)
416 /**************************************************************************/
417 uint16_t lcdGetWidth(void)
419 return ssd1351Properties
.width
;
422 /**************************************************************************/
424 @brief Gets the height in pixels of the LCD screen (varies depending
425 on the current screen orientation)
427 /**************************************************************************/
428 uint16_t lcdGetHeight(void)
430 return ssd1351Properties
.height
;
433 /**************************************************************************/
435 @brief Scrolls the contents of the LCD screen vertically the
436 specified number of pixels using a HW optimised routine
438 /**************************************************************************/
439 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
444 /**************************************************************************/
446 @brief Gets the controller's 16-bit (4 hexdigit) ID
448 /**************************************************************************/
449 uint16_t lcdGetControllerID(void)
454 /**************************************************************************/
456 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
457 generic capabilities and dimensions
459 /**************************************************************************/
460 lcdProperties_t
lcdGetProperties(void)
462 return ssd1351Properties
;