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  if (!i2c_master_check_signals(OLED_SSD1306_I2C)) { // check if there are pull-ups to operator I2C
46  return false;
47  }
48  i2c_master_setup(OLED_SSD1306_I2C, 400); // setup I2C bus ( SSD1306 supports an I2C cleck up to 400 kHz)
49  const uint8_t oled_init[] = {
50  0x00, // control byte: continuous (multiple byes), command
51  0xae, // Set Display ON/OFF: OFF
52  // hardware configuration
53  0xa8, 0x3f, // Set Multiplex Ratio: 64
54  0xd3, 0x00, // Set Display Offset: 0
55  0xa1, // Set Segment Re-map: column address 0 is mapped to SEG127
56  0xc8, // Set COM Output Scan Direction: normal mode (RESET) Scan from COM[N-1] to COM[0]
57  0xda, 0x12, // Set COM Pins Hardware Configuration: Alternative COM pin configuration, Disable COM Left/Right remap
58  0x40, // Set Display Start Line: start line register from 0
59  // fundamental commands
60  0x81, 0xff, // Set Contrast Control: 256
61  0xa6, // Set Normal/Inverse Display: Normal display (RESET)
62  // Timing & Driving Scheme Setting
63  0xd5, 0xf0, // Set Display Clock Divide Ratio/Oscillator Frequency: Divide ratio=129, F_OSC=1
64  0xd9, 0x22, // Set Pre-charge Period: Phase 1=2 DCLK, Phase 2=2DCLK
65  0xdb, 0x20, // Set V_COMH Deselect Level: ~0.77xV_CC
66  // Charge Pump
67  0x8d, 0x14, // Charge Pump Setting: Enable Charge Pump
68  // Addressing Setting
69  0x20, 0x00 // Set Memory Addressing Mode: Horizontal Addressing Mode
70  }; // command to initialize the display
71  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
72 }
73 
74 void oled_ssd1306_on(void)
75 {
76  const uint8_t oled_display_on[] = {
77  0x80, // control byte: no continuation, command
78  0xaf, // Set Display ON/OFF: ON
79  }; // command to switch on display
80  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
81 }
82 
83 void oled_ssd1306_off(void)
84 {
85  const uint8_t oled_display_off[] = {
86  0x80, // control byte: no continuation, command
87  0xae, // Set Display ON/OFF: OFF
88  }; // command to switch off display
89  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
90 }
91 
93 {
94  const uint8_t oled_entire_display_on[] = {
95  0x80, // control byte: no continuation, command
96  0xa5 // Entire Display ON: Entire display ON Output ignores RAM content
97  }; // command to set entire display on
98  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
99  oled_ssd1306_on(); // set display on
100  sleep_ms(200); // let is on for a bit (enough for the user to see it is completely on
101  oled_ssd1306_off(); // set display off
102  const uint8_t oled_entire_display_ram[] = {
103  0x80, // control byte: no continuation, command
104  0xa4 // Entire Display ON: Resume to RAM content display
105  }; // command to display RAM
106  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
107 }
108 
109 void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
110 {
111  // verify input
112  if (0==display_length || NULL==display_data) {
113  return;
114  }
115 
116  const uint8_t oled_start_page[] = {
117  0x00, // control byte: continuous (multiple byes), command
118  0xb0, // Set Page Start Address for Page Addressing Mode: PAGE0
119  0x00, // Set Lower Column Start Address for Page Addressing Mode: 0
120  0x10 // Set Higher Column Start Address for Page Addressing Mode: 0
121  }; // command to set addressing mode
122  i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
123  if (I2C_MASTER_RC_NONE!=i2c_master_start(OLED_SSD1306_I2C)) { // send start condition
124  return;
125  }
126  if (I2C_MASTER_RC_NONE!=i2c_master_select_slave(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, true)) { // select OLED display
127  return;
128  }
129  const uint8_t oled_data[] = {
130  0x40, // control byte: continuous (multiple byes), data
131  };
132  if (I2C_MASTER_RC_NONE!=i2c_master_write(OLED_SSD1306_I2C, oled_data, LENGTH(oled_data))) { // send data header
133  return;
134  }
135  if (I2C_MASTER_RC_NONE!=i2c_master_write(OLED_SSD1306_I2C, display_data, display_length)) { // send template picture to display
136  return;
137  }
138  i2c_master_stop(OLED_SSD1306_I2C); // send stop condition
139 }
140 
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:464
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:311
void oled_ssd1306_on(void)
switch OLED display on
Definition: oled_ssd1306.c:74
enum i2c_master_rc i2c_master_start(uint32_t i2c)
send start condition
Definition: i2c_master.c:284
#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:83
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:427
void oled_ssd1306_test(void)
test OLED display: switch entire screen on for a brief time
Definition: oled_ssd1306.c:92
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:109
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
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:522
#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
bool i2c_master_check_signals(uint32_t i2c)
check if SDA and SCL signals are high
Definition: i2c_master.c:225
SSD1306 OLED library (API)