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_WRITERAM
);
127 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
128 DATA(x
); // Start Address
129 DATA(ssd1351Properties
.width
-1); // End Address (0x7F)
131 CMD(SSD1351_CMD_SETROWADDRESS
);
132 DATA(y
); // Start Address
133 DATA(ssd1351Properties
.height
-1); // End Address (0x7F)
134 CMD(SSD1351_CMD_WRITERAM
);
137 /*************************************************/
139 /*************************************************/
141 /**************************************************************************/
143 @brief Configures any pins or HW and initialises the LCD controller
145 /**************************************************************************/
148 // Set all pins to output
149 gpioSetDir(SSD1351_SCK_PORT
, SSD1351_SCK_PIN
, gpioDirection_Output
);
150 gpioSetDir(SSD1351_SID_PORT
, SSD1351_SID_PIN
, gpioDirection_Output
);
151 gpioSetDir(SSD1351_RST_PORT
, SSD1351_RST_PIN
, gpioDirection_Output
);
152 gpioSetDir(SSD1351_CS_PORT
, SSD1351_CS_PIN
, gpioDirection_Output
);
154 #if !defined SSD1351_BUS_SPI3
155 gpioSetDir(SSD1351_DC_PORT
, SSD1351_DC_PIN
, gpioDirection_Output
);
167 SSD1351_DISABLEPULLUPS();
169 CMD(SSD1351_CMD_SETCOMMANDLOCK
);
170 DATA(0x12); // Unlocked to enter commands
171 CMD(SSD1351_CMD_SETCOMMANDLOCK
);
172 DATA(0xB1); // Make all commands accessible
173 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYOFF
);
174 CMD(SSD1351_CMD_SETFRONTCLOCKDIV
);
176 CMD(SSD1351_CMD_SETMUXRRATIO
);
178 CMD(SSD1351_CMD_COLORDEPTH
);
179 DATA(0x74); // Bit 7:6 = 65,536 Colors, Bit 3 = BGR or RGB
180 CMD(SSD1351_CMD_SETCOLUMNADDRESS
);
183 CMD(SSD1351_CMD_SETROWADDRESS
);
186 CMD(SSD1351_CMD_SETDISPLAYSTARTLINE
);
188 CMD(SSD1351_CMD_SETDISPLAYOFFSET
);
190 CMD(SSD1351_CMD_SETGPIO
);
191 DATA(0x00); // Disable GPIO pins
192 CMD(SSD1351_CMD_FUNCTIONSELECTION
);
193 DATA(0x01); // External VDD (0 = External, 1 = Internal)
194 CMD(SSD1351_CMD_SETPHASELENGTH
);
196 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE
);
197 DATA(0xA0); // Enable External VSL
200 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE
);
202 CMD(SSD1351_CMD_SETVCOMHVOLTAGE
);
204 CMD(SSD1351_CMD_SETCONTRAST
);
208 CMD(SSD1351_CMD_MASTERCONTRAST
);
209 DATA(0x0F); // Maximum contrast
210 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD
);
212 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET
);
214 // Use default grayscale for now to save flash space, but here are
215 // the values if someone wants to change them ...
216 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
282 lcdFillRGB(COLOR_RED
);
284 // Turn the display on
285 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON
);
288 /**************************************************************************/
290 @brief Enables or disables the LCD backlight
292 /**************************************************************************/
293 void lcdBacklight(bool state
)
295 // No backlight ... do nothing
298 /**************************************************************************/
300 @brief Renders a simple test pattern on the LCD
302 /**************************************************************************/
306 CMD(SSD1351_CMD_WRITERAM
);
307 ssd1351SetCursor(0, 0);
308 CMD(SSD1351_CMD_WRITERAM
);
314 if(i
>111){DATA(COLOR_WHITE
>>8);DATA((uint8_t)COLOR_WHITE
);}
315 else if(i
>95){DATA(COLOR_BLUE
>>8);DATA((uint8_t)COLOR_BLUE
);}
316 else if(i
>79){DATA(COLOR_GREEN
>>8);DATA((uint8_t)COLOR_GREEN
);}
317 else if(i
>63){DATA(COLOR_CYAN
>>8);DATA((uint8_t)COLOR_CYAN
);}
318 else if(i
>47){DATA(COLOR_RED
>>8);DATA((uint8_t)COLOR_RED
);}
319 else if(i
>31){DATA(COLOR_MAGENTA
>>8);DATA((uint8_t)COLOR_MAGENTA
);}
320 else if(i
>15){DATA(COLOR_YELLOW
>>8);DATA((uint8_t)COLOR_YELLOW
);}
321 else {DATA(COLOR_BLACK
>>8);DATA((uint8_t)COLOR_BLACK
);}
326 /**************************************************************************/
328 @brief Fills the LCD with the specified 16-bit color
330 /**************************************************************************/
331 void lcdFillRGB(uint16_t data
)
334 ssd1351SetCursor(0, 0);
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
;