SSD1306 OLED display driver  1.5.0
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
oled_ssd1351.c
1 /*
2  MIT License
3 
4  Copyright (c) 2018, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
24 
25 #include "oled_ssd1351.h"
26 #include "lcd_common.h"
27 #include "ssd1351_commands.h"
28 #include "intf/ssd1306_interface.h"
29 #include "spi/ssd1306_spi.h"
30 #include "hal/io.h"
31 #include "nano_gfx_types.h"
32 
33 #define CMD_ARG 0xFF
34 
35 extern uint16_t ssd1306_color;
36 extern uint32_t s_ssd1306_spi_clock;
37 
38 static const PROGMEM uint8_t s_oled128x128_initData[] =
39 {
40  SSD1351_UNLOCK, CMD_ARG, 0x12,
41  SSD1351_UNLOCK, CMD_ARG, 0xB1,
42  SSD1351_SLEEP_ON,
43  SSD1351_CLOCKDIV, CMD_ARG, 0xF1, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
44  SSD1351_SETMULTIPLEX, CMD_ARG, 127, // Reset to default MUX. See datasheet
45  SSD1351_SEGREMAP, CMD_ARG, 0B00110101, // 16-bit rgb color mode
46  SSD1351_SETSTARTLINE, CMD_ARG, 0x00, // First line to start scanning from
47  SSD1351_SETDISPLAYOFFSET, CMD_ARG, 0x00, // Set display offset
48  SSD1351_SETGPIO, CMD_ARG, 0x00, // GPIO OFF
49  SSD1351_SETFUNCTION, CMD_ARG, 0x01,
50  SSD1351_SETPRECHARGE, CMD_ARG, 0x32, // Phase 1 and Phase 2 periods
51  SSD1351_VCOMH, CMD_ARG, 0x05, //
52  SSD1351_PRECHARGELEVEL, CMD_ARG, 0x17,
53  SSD1351_NORMALDISPLAY,
54  SSD1351_CONTRAST, CMD_ARG, 0xC8, // RED
55  CMD_ARG, 0x80, // GREEN
56  CMD_ARG, 0xC8, // BLUE
57  SSD1351_MASTERCURRENT, CMD_ARG, 0x0F, //
58  SSD1351_EXTVSL, CMD_ARG, 0xA0, CMD_ARG, 0xB5, CMD_ARG, 0x55,
59  SSD1351_PRECHARGESECOND, CMD_ARG, 0x01, //
60  SSD1351_SLEEP_OFF, // Disable power-safe mode
61  SSD1351_NORMALDISPLAY,
62 };
63 
64 static uint8_t s_column;
65 static uint8_t s_page;
66 
67 static void ssd1351_setBlock(uint8_t x, uint8_t y, uint8_t w)
68 {
69  uint8_t rx = w ? (x + w - 1) : (s_displayWidth - 1);
70  s_column = x;
71  s_page = y;
73  ssd1306_sendByte(SSD1351_COLUMNADDR);
74  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
78  ssd1306_sendByte(SSD1351_ROWADDR);
79  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
80  ssd1306_sendByte(y<<3);
81  ssd1306_sendByte(((y<<3) + 7) < s_displayHeight ? ((y<<3) + 7) : (s_displayHeight - 1));
83  ssd1306_sendByte(SSD1331_WRITEDATA);
85 }
86 
87 static void ssd1351_setBlock2(uint8_t x, uint8_t y, uint8_t w)
88 {
89  uint8_t rx = w ? (x + w - 1) : (s_displayWidth - 1);
91  ssd1306_sendByte(SSD1351_COLUMNADDR);
92  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
96  ssd1306_sendByte(SSD1351_ROWADDR);
97  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
101  ssd1306_sendByte(SSD1331_WRITEDATA);
103 }
104 
105 static void ssd1351_nextPage(void)
106 {
108  ssd1351_setBlock(s_column,s_page+1,0);
109 }
110 
111 void ssd1351_setMode(uint8_t vertical)
112 {
114  ssd1306_sendByte( SSD1351_SEGREMAP );
116  ssd1306_sendByte( 0B00110100 | vertical );
118  if (vertical)
119  {
120  ssd1306_setRamBlock = ssd1351_setBlock;
121  }
122  else
123  {
124  ssd1306_setRamBlock = ssd1351_setBlock2;
125  }
126 }
127 
128 static void ssd1351_sendPixels(uint8_t data)
129 {
130  for (uint8_t i=8; i>0; i--)
131  {
132  if ( data & 0x01 )
133  {
134  ssd1306_sendByte( (uint8_t)(ssd1306_color>>8) );
135  ssd1306_sendByte( (uint8_t)(ssd1306_color) );
136  }
137  else
138  {
139  ssd1306_sendByte( 0B00000000 );
140  ssd1306_sendByte( 0B00000000 );
141  }
142  data >>= 1;
143  }
144 }
145 
146 static void ssd1351_sendPixel8(uint8_t data)
147 {
148  uint16_t color = RGB8_TO_RGB16(data);
149  ssd1306_sendByte( color >> 8 );
150  ssd1306_sendByte( color & 0xFF );
151 }
152 
154 {
156  s_displayHeight = 128;
157  s_displayWidth = 128;
158  ssd1306_setRamBlock = ssd1351_setBlock;
159  ssd1306_nextRamPage = ssd1351_nextPage;
160  ssd1306_sendPixels = ssd1351_sendPixels;
161  ssd1306_sendPixel8 = ssd1351_sendPixel8;
163  for( uint8_t i=0; i<sizeof(s_oled128x128_initData); i++)
164  {
165  uint8_t data = pgm_read_byte(&s_oled128x128_initData[i]);
166  if (data == CMD_ARG)
167  {
168  data = pgm_read_byte(&s_oled128x128_initData[++i]);
170  ssd1306_sendByte(data);
172  }
173  else
174  {
175  ssd1306_sendByte(data);
176  }
177  }
179 }
180 
181 void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
182 {
183  if (rstPin >=0)
184  {
185  pinMode(rstPin, OUTPUT);
186  digitalWrite(rstPin, HIGH);
187  /* Wait at least 1ms after VCC is up for LCD */
188  delay(1);
189  /* Perform reset operation of LCD display */
190  digitalWrite(rstPin, LOW);
191  delay(20);
192  digitalWrite(rstPin, HIGH);
193  }
194  /* ssd1351 cannot work faster than at 4MHz per datasheet */
195  s_ssd1306_spi_clock = 4000000;
196  ssd1306_spiInit(cesPin, dcPin);
198 }
uint8_t s_displayWidth
uint32_t s_ssd1306_spi_clock
Definition: ssd1306_spi.c:37
void ssd1351_128x128_init(void)
Inits 128x128 RGB OLED display (based on SSD1351 controller).
Definition: oled_ssd1351.c:153
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:65
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:39
void(* ssd1306_endTransmission)(void)
void ssd1351_setMode(uint8_t vertical)
Sets GDRAM autoincrement mode.
Definition: oled_ssd1351.c:111
uint8_t g_lcd_type
void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 128x128 RGB OLED display over spi (based on SSD1351 controller).
Definition: oled_ssd1351.c:181
void(* ssd1306_sendByte)(uint8_t data)
void(* ssd1306_commandStart)(void)
void(* ssd1306_setRamBlock)(uint8_t x, uint8_t y, uint8_t w)
Definition: lcd_common.c:29
#define RGB8_TO_RGB16(c)
void(* ssd1306_sendPixel8)(uint8_t data)
Sends RGB pixel encoded in 3-3-2 format to OLED driver. Sends RGB pixel encoded in 3-3-2 format to OL...
Definition: lcd_common.c:32
uint8_t s_displayHeight
void(* ssd1306_nextRamPage)(void)
Definition: lcd_common.c:30
void(* ssd1306_sendPixels)(uint8_t data)
Definition: lcd_common.c:31