SSD1306 OLED display driver  1.8.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
oled_ssd1351.c
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2019, 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 "intf/spi/ssd1306_spi.h"
30 #include "ssd1306_hal/io.h"
31 #include "nano_gfx_types.h"
32 #ifdef SDL_EMULATION
33 #include "sdl_core.h"
34 #endif
35 
36 #define CMD_ARG 0xFF
37 
38 extern uint16_t ssd1306_color;
39 extern uint32_t s_ssd1306_spi_clock;
40 
41 static const PROGMEM uint8_t s_oled128x128_initData[] =
42 {
43 #ifdef SDL_EMULATION
44  SDL_LCD_SSD1351,
45  0x00,
46 #endif
47  SSD1351_UNLOCK, CMD_ARG, 0x12,
48  SSD1351_UNLOCK, CMD_ARG, 0xB1,
49  SSD1351_SLEEP_ON,
50  SSD1351_CLOCKDIV, CMD_ARG, 0xF1, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
51  SSD1351_SETMULTIPLEX, CMD_ARG, 127, // Reset to default MUX. See datasheet
52  SSD1351_SEGREMAP, CMD_ARG, 0B00110101, // 16-bit rgb color mode
53  SSD1351_SETSTARTLINE, CMD_ARG, 0x00, // First line to start scanning from
54  SSD1351_SETDISPLAYOFFSET, CMD_ARG, 0x00, // Set display offset
55  SSD1351_SETGPIO, CMD_ARG, 0x00, // GPIO OFF
56  SSD1351_SETFUNCTION, CMD_ARG, 0x01,
57  SSD1351_SETPRECHARGE, CMD_ARG, 0x32, // Phase 1 and Phase 2 periods
58  SSD1351_VCOMH, CMD_ARG, 0x05, //
59  SSD1351_PRECHARGELEVEL, CMD_ARG, 0x17,
60  SSD1351_NORMALDISPLAY,
61  SSD1351_CONTRAST, CMD_ARG, 0xC8, // RED
62  CMD_ARG, 0x80, // GREEN
63  CMD_ARG, 0xC8, // BLUE
64  SSD1351_MASTERCURRENT, CMD_ARG, 0x0F, //
65  SSD1351_EXTVSL, CMD_ARG, 0xA0, CMD_ARG, 0xB5, CMD_ARG, 0x55,
66  SSD1351_PRECHARGESECOND, CMD_ARG, 0x01, //
67  SSD1351_SLEEP_OFF, // Disable power-safe mode
68  SSD1351_NORMALDISPLAY,
69 };
70 
71 static uint8_t s_column;
72 static uint8_t s_page;
73 
74 static void ssd1351_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
75 {
76  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
77  s_column = x;
78  s_page = y;
81  ssd1306_intf.send(SSD1351_COLUMNADDR);
82  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
83  ssd1306_intf.send(x);
86  ssd1306_intf.send(SSD1351_ROWADDR);
87  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
88  ssd1306_intf.send(y<<3);
89  ssd1306_intf.send(((y<<3) + 7) < ssd1306_lcd.height ? ((y<<3) + 7) : (ssd1306_lcd.height - 1));
91  ssd1306_intf.send(SSD1331_WRITEDATA);
93 }
94 
95 static void ssd1351_setBlock2(lcduint_t x, lcduint_t y, lcduint_t w)
96 {
97  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
100  ssd1306_intf.send(SSD1351_COLUMNADDR);
101  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
102  ssd1306_intf.send(x);
103  ssd1306_intf.send(rx < ssd1306_lcd.width ? rx : (ssd1306_lcd.width - 1));
105  ssd1306_intf.send(SSD1351_ROWADDR);
106  ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
107  ssd1306_intf.send(y);
110  ssd1306_intf.send(SSD1331_WRITEDATA);
112 }
113 
114 static void ssd1351_nextPage(void)
115 {
116  ssd1306_intf.stop();
117  ssd1351_setBlock(s_column,s_page+1,0);
118 }
119 
120 static void ssd1351_nextPage2(void)
121 {
122 }
123 
125 {
128  ssd1306_intf.send( SSD1351_SEGREMAP );
130  ssd1306_intf.send( 0B00110100 | mode );
131  ssd1306_intf.stop();
132  if (mode == LCD_MODE_SSD1306_COMPAT)
133  {
134  ssd1306_lcd.set_block = ssd1351_setBlock;
135  ssd1306_lcd.next_page = ssd1351_nextPage;
136  }
137  else if (mode == LCD_MODE_NORMAL )
138  {
139  ssd1306_lcd.set_block = ssd1351_setBlock2;
140  ssd1306_lcd.next_page = ssd1351_nextPage2;
141  }
142 }
143 
144 static void ssd1351_sendPixels(uint8_t data)
145 {
146  for (uint8_t i=8; i>0; i--)
147  {
148  if ( data & 0x01 )
149  {
150  ssd1306_intf.send( (uint8_t)(ssd1306_color>>8) );
151  ssd1306_intf.send( (uint8_t)(ssd1306_color) );
152  }
153  else
154  {
155  ssd1306_intf.send( 0B00000000 );
156  ssd1306_intf.send( 0B00000000 );
157  }
158  data >>= 1;
159  }
160 }
161 
162 static void ssd1351_sendPixelsBuffer(const uint8_t *buffer, uint16_t len)
163 {
164  while(len--)
165  {
166  ssd1351_sendPixels(*buffer);
167  buffer++;
168  }
169 }
170 
171 static void ssd1351_sendPixel8(uint8_t data)
172 {
173  uint16_t color = RGB8_TO_RGB16(data);
174  ssd1306_intf.send( color >> 8 );
175  ssd1306_intf.send( color & 0xFF );
176 }
177 
178 static void ssd1351_sendPixel16(uint16_t color)
179 {
180  ssd1306_intf.send( color >> 8 );
181  ssd1306_intf.send( color & 0xFF );
182 }
183 
185 {
187  ssd1306_lcd.height = 128;
188  ssd1306_lcd.width = 128;
189  ssd1306_lcd.set_block = ssd1351_setBlock;
190  ssd1306_lcd.next_page = ssd1351_nextPage;
191  ssd1306_lcd.send_pixels1 = ssd1351_sendPixels;
192  ssd1306_lcd.send_pixels_buffer1 = ssd1351_sendPixelsBuffer;
193  ssd1306_lcd.send_pixels8 = ssd1351_sendPixel8;
194  ssd1306_lcd.send_pixels16 = ssd1351_sendPixel16;
198  for( uint8_t i=0; i<sizeof(s_oled128x128_initData); i++)
199  {
200  uint8_t data = pgm_read_byte(&s_oled128x128_initData[i]);
201  if (data == CMD_ARG)
202  {
203  data = pgm_read_byte(&s_oled128x128_initData[++i]);
205  ssd1306_intf.send(data);
207  }
208  else
209  {
210  ssd1306_intf.send(data);
211  }
212  }
213  ssd1306_intf.stop();
214 }
215 
216 void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
217 {
218  if (rstPin >=0)
219  {
220  ssd1306_resetController( rstPin, 20 );
221  }
222  /* ssd1351 cannot work faster than at 4MHz per datasheet */
223  s_ssd1306_spi_clock = 4400000;
224  ssd1306_spiInit(cesPin, dcPin);
226 }
void ssd1351_setMode(lcd_mode_t mode)
Sets GDRAM autoincrement mode.
Definition: oled_ssd1351.c:124
void(* send)(uint8_t data)
uint32_t s_ssd1306_spi_clock
Definition: ssd1306_spi.c:35
void(* send_pixels16)(uint16_t data)
Sends RGB pixel encoded in 5-6-5 format to OLED driver. Sends RGB pixel encoded in 5-6-5 format to OL...
Definition: lcd_common.h:149
void(* set_block)(lcduint_t x, lcduint_t y, lcduint_t w)
Sets block in RAM of lcd display controller to write data to.
Definition: lcd_common.h:114
void ssd1351_128x128_init(void)
Inits 128x128 RGB OLED display (based on SSD1351 controller).
Definition: oled_ssd1351.c:184
void(* send_pixels8)(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.h:142
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:50
lcd_mode_t
Definition: lcd_common.h:69
void(* send_pixels_buffer1)(const uint8_t *buffer, uint16_t len)
Definition: lcd_common.h:135
void ssd1306_resetController(int8_t rstPin, uint8_t delayMs)
Does hardware reset for oled controller.
Definition: lcd_common.c:139
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:37
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:33
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:216
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
#define RGB8_TO_RGB16(c)
void(* next_page)(void)
Definition: lcd_common.h:122
lcduint_t width
Definition: lcd_common.h:94
lcd_type_t type
Definition: lcd_common.h:91
void(* set_mode)(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.h:164