CuVoodoo STM32F1 firmware template
oled_ssd1306.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdbool.h> // boolean type
24 #include <stdlib.h> // general utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/i2c.h> // I2C library
28 
29 /* own libraries */
30 #include "global.h" // global utilities
31 #include "oled_ssd1306.h" // OLED definitions
32 #include "i2c_master.h" // I2C header and definitions
33 
35 #define OLED_SSD1306_SLAVE 0x3c
36 
40 #define OLED_SSD1306_I2C I2C1
43 bool oled_ssd1306_setup(void)
44 {
45  i2c_master_setup(OLED_SSD1306_I2C, 400); // setup I2C bus ( SSD1306 supports an I2C cleck up to 400 kHz)
46  const uint8_t oled_init[] = {
47  0x00, // control byte: continuous (multiple byes), command
48  0xae, // Set Display ON/OFF: OFF
49  // hardware configuration
50  0xa8, 0x3f, // Set Multiplex Ratio: 64
51  0xd3, 0x00, // Set Display Offset: 0
52  0xa1, // Set Segment Re-map: column address 0 is mapped to SEG127
53  0xc8, // Set COM Output Scan Direction: normal mode (RESET) Scan from COM[N-1] to COM[0]
54  0xda, 0x12, // Set COM Pins Hardware Configuration: Alternative COM pin configuration, Disable COM Left/Right remap
55  0x40, // Set Display Start Line: start line register from 0
56  // fundamental commands
57  0x81, 0xff, // Set Contrast Control: 256
58  0xa6, // Set Normal/Inverse Display: Normal display (RESET)
59  // Timing & Driving Scheme Setting
60  0xd5, 0xf0, // Set Display Clock Divide Ratio/Oscillator Frequency: Divide ratio=129, F_OSC=1
61  0xd9, 0x22, // Set Pre-charge Period: Phase 1=2 DCLK, Phase 2=2DCLK
62  0xdb, 0x20, // Set V_COMH Deselect Level: ~0.77xV_CC
63  // Charge Pump
64  0x8d, 0x14, // Charge Pump Setting: Enable Charge Pump
65  // Addressing Setting
66  0x20, 0x00 // Set Memory Addressing Mode: Horizontal Addressing Mode
67  }; // command to initialize the display
68  return I2C_MASTER_RC_NONE==i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_init, LENGTH(oled_init)); // send command to initialize display
69 }
70 
71 void oled_ssd1306_on(void)
72 {
73  const uint8_t oled_display_on[] = {
74  0x80, // control byte: no continuation, command
75  0xaf, // Set Display ON/OFF: ON
76  }; // command to switch on display
77  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
78 }
79 
80 void oled_ssd1306_off(void)
81 {
82  const uint8_t oled_display_off[] = {
83  0x80, // control byte: no continuation, command
84  0xae, // Set Display ON/OFF: OFF
85  }; // command to switch off display
86  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
87 }
88 
90 {
91  const uint8_t oled_entire_display_on[] = {
92  0x80, // control byte: no continuation, command
93  0xa5 // Entire Display ON: Entire display ON Output ignores RAM content
94  }; // command to set entire display on
95  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
96  oled_ssd1306_on(); // set display on
97  sleep_ms(200); // let is on for a bit (enough for the user to see it is completely on
98  oled_ssd1306_off(); // set display off
99  const uint8_t oled_entire_display_ram[] = {
100  0x80, // control byte: no continuation, command
101  0xa4 // Entire Display ON: Resume to RAM content display
102  }; // command to display RAM
103  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
104 }
105 
106 void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
107 {
108  // verify input
109  if (0==display_length || NULL==display_data) {
110  return;
111  }
112 
113  const uint8_t oled_start_page[] = {
114  0x00, // control byte: continuous (multiple byes), command
115  0xb0, // Set Page Start Address for Page Addressing Mode: PAGE0
116  0x00, // Set Lower Column Start Address for Page Addressing Mode: 0
117  0x10 // Set Higher Column Start Address for Page Addressing Mode: 0
118  }; // command to set addressing mode
119  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
120  if (I2C_MASTER_RC_NONE!=i2c_master_start(OLED_SSD1306_I2C)) { // send start condition
121  return;
122  }
123  if (I2C_MASTER_RC_NONE!=i2c_master_select_slave(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, true)) { // select OLED display
124  return;
125  }
126  const uint8_t oled_data[] = {
127  0x40, // control byte: continuous (multiple byes), data
128  };
129  if (I2C_MASTER_RC_NONE!=i2c_master_write(OLED_SSD1306_I2C, oled_data, LENGTH(oled_data))) { // send data header
130  return;
131  }
132  if (I2C_MASTER_RC_NONE!=i2c_master_write(OLED_SSD1306_I2C, display_data, display_length)) { // send template picture to display
133  return;
134  }
135  i2c_master_stop(OLED_SSD1306_I2C); // send stop condition
136 }
137 
library to communicate using I2C as master (API)
enum i2c_master_rc i2c_master_stop(uint32_t i2c)
sent stop condition
Definition: i2c_master.c:425
enum i2c_master_rc i2c_master_select_slave(uint32_t i2c, uint16_t slave, bool address_10bit, bool write)
select I2C slave device
Definition: i2c_master.c:287
void oled_ssd1306_on(void)
switch OLED display on
Definition: oled_ssd1306.c:71
enum i2c_master_rc i2c_master_start(uint32_t i2c)
send start condition
Definition: i2c_master.c:265
#define OLED_SSD1306_I2C
I2C peripheral.
Definition: oled_ssd1306.c:40
global definitions and methods (API)
void oled_ssd1306_off(void)
switch OLED display off
Definition: oled_ssd1306.c:80
enum i2c_master_rc i2c_master_write(uint32_t i2c, const uint8_t *data, size_t data_size)
write data over I2C
Definition: i2c_master.c:391
void oled_ssd1306_test(void)
test OLED display: switch entire screen on for a brief time
Definition: oled_ssd1306.c:89
void i2c_master_setup(uint32_t i2c, uint16_t frequency)
setup I2C peripheral
Definition: i2c_master.c:173
void oled_ssd1306_display(const uint8_t *display_data, uint16_t display_length)
send data to display to OLED display
Definition: oled_ssd1306.c:106
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
enum i2c_master_rc i2c_master_slave_write(uint32_t i2c, uint16_t slave, bool address_10bit, const uint8_t *data, size_t data_size)
write data to slave device
Definition: i2c_master.c:477
#define OLED_SSD1306_SLAVE
SSD1306 OLED display I2C slave address.
Definition: oled_ssd1306.c:35
#define LENGTH(x)
get the length of an array
Definition: global.h:26
SSD1306 OLED library (API)