1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
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) 2010, microBuilder SARL
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
192 CMD(SSD1351_CMD_SETPHASELENGTH
);
194 CMD(SSD1351_CMD_SETSEGMENTLOWVOLTAGE
);
195 DATA(0xA0); // Enable External VSL
198 CMD(SSD1351_CMD_SETPRECHARGEVOLTAGE
);
200 CMD(SSD1351_CMD_SETVCOMHVOLTAGE
);
202 CMD(SSD1351_CMD_SETCONTRAST
);
206 CMD(SSD1351_CMD_MASTERCONTRAST
);
207 DATA(0x0F); // Maximum contrast
208 CMD(SSD1351_CMD_SETSECONDPRECHARGEPERIOD
);
210 CMD(SSD1351_CMD_SETDISPLAYMODE_RESET
);
212 // Use default grayscale for now to save flash space, but here are
213 // the values if someone wants to change them ...
214 // CMD(SSD1351_CMD_GRAYSCALELOOKUP);
280 lcdFillRGB(COLOR_RED
);
282 // Turn the display on
283 CMD(SSD1351_CMD_SLEEPMODE_DISPLAYON
);
286 /**************************************************************************/
288 @brief Enables or disables the LCD backlight
290 /**************************************************************************/
291 void lcdBacklight(bool state
)
293 // No backlight ... do nothing
296 /**************************************************************************/
298 @brief Renders a simple test pattern on the LCD
300 /**************************************************************************/
304 CMD(SSD1351_CMD_WRITERAM
);
305 ssd1351SetCursor(0, 0);
306 CMD(SSD1351_CMD_WRITERAM
);
312 if(i
>111){DATA(COLOR_WHITE
>>8);DATA((uint8_t)COLOR_WHITE
);}
313 else if(i
>95){DATA(COLOR_BLUE
>>8);DATA((uint8_t)COLOR_BLUE
);}
314 else if(i
>79){DATA(COLOR_GREEN
>>8);DATA((uint8_t)COLOR_GREEN
);}
315 else if(i
>63){DATA(COLOR_CYAN
>>8);DATA((uint8_t)COLOR_CYAN
);}
316 else if(i
>47){DATA(COLOR_RED
>>8);DATA((uint8_t)COLOR_RED
);}
317 else if(i
>31){DATA(COLOR_MAGENTA
>>8);DATA((uint8_t)COLOR_MAGENTA
);}
318 else if(i
>15){DATA(COLOR_YELLOW
>>8);DATA((uint8_t)COLOR_YELLOW
);}
319 else {DATA(COLOR_BLACK
>>8);DATA((uint8_t)COLOR_BLACK
);}
324 /**************************************************************************/
326 @brief Fills the LCD with the specified 16-bit color
328 /**************************************************************************/
329 void lcdFillRGB(uint16_t data
)
332 ssd1351SetCursor(0, 0);
333 CMD(SSD1351_CMD_WRITERAM
);
334 for (i
=1; i
<=((ssd1351Properties
.width
)*(ssd1351Properties
.height
)) * 2;i
++)
341 /**************************************************************************/
343 @brief Draws a single pixel at the specified X/Y location
345 /**************************************************************************/
346 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
348 if ((x
>= ssd1351Properties
.width
) || (y
>= ssd1351Properties
.height
))
351 ssd1351SetCursor((uint8_t)x
, (uint8_t)y
);
356 /**************************************************************************/
358 @brief Draws an array of consecutive RGB565 pixels (much
359 faster than addressing each pixel individually)
361 /**************************************************************************/
362 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
367 /**************************************************************************/
369 @brief Optimised routine to draw a horizontal line faster than
370 setting individual pixels
372 /**************************************************************************/
373 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
378 /**************************************************************************/
380 @brief Optimised routine to draw a vertical line faster than
381 setting individual pixels
383 /**************************************************************************/
384 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
389 /**************************************************************************/
391 @brief Gets the 16-bit color of the pixel at the specified location
393 /**************************************************************************/
394 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
400 /**************************************************************************/
402 @brief Sets the LCD orientation to horizontal and vertical
404 /**************************************************************************/
405 void lcdSetOrientation(lcdOrientation_t orientation
)
410 /**************************************************************************/
412 @brief Gets the current screen orientation (horizontal or vertical)
414 /**************************************************************************/
415 lcdOrientation_t
lcdGetOrientation(void)
417 return lcdOrientation
;
420 /**************************************************************************/
422 @brief Gets the width in pixels of the LCD screen (varies depending
423 on the current screen orientation)
425 /**************************************************************************/
426 uint16_t lcdGetWidth(void)
428 return ssd1351Properties
.width
;
431 /**************************************************************************/
433 @brief Gets the height in pixels of the LCD screen (varies depending
434 on the current screen orientation)
436 /**************************************************************************/
437 uint16_t lcdGetHeight(void)
439 return ssd1351Properties
.height
;
442 /**************************************************************************/
444 @brief Scrolls the contents of the LCD screen vertically the
445 specified number of pixels using a HW optimised routine
447 /**************************************************************************/
448 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
453 /**************************************************************************/
455 @brief Gets the controller's 16-bit (4 hexdigit) ID
457 /**************************************************************************/
458 uint16_t lcdGetControllerID(void)
460 return ssd1351Type();
463 /**************************************************************************/
465 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
466 generic capabilities and dimensions
468 /**************************************************************************/
469 lcdProperties_t
lcdGetProperties(void)
471 return ssd1351Properties
;