OLED: update to new I2C library

This commit is contained in:
King Kévin 2018-03-20 09:38:06 +01:00
parent 14cdeba690
commit 82e33023de
1 changed files with 22 additions and 13 deletions

View File

@ -16,13 +16,16 @@
* @file oled_ssd1306.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: I2C @ref i2c_master_i2c, timer @ref i2c_master_timer
* @note peripherals used: I2C @ref oled_ssd1306_i2c, timer @ref i2c_master_timer
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdbool.h> // boolean type
#include <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/i2c.h> // I2C library
/* own libraries */
#include "global.h" // global utilities
#include "oled_ssd1306.h" // OLED definitions
@ -31,9 +34,15 @@
/** SSD1306 OLED display I2C slave address */
#define OLED_SSD1306_SLAVE 0x3c
/** @defgroup oled_ssd1306_i2c I2C peripheral to communicate with the SSD1306 OLED
* @{
*/
#define OLED_SSD1306_I2C I2C1 /**< I2C peripheral */
/** @} */
bool oled_ssd1306_setup(void)
{
i2c_master_setup(true);
i2c_master_setup(OLED_SSD1306_I2C, true);
const uint8_t oled_init[] = {
0x00, // control byte: continuous (multiple byes), command
0xae, // Set Display ON/OFF: OFF
@ -56,7 +65,7 @@ bool oled_ssd1306_setup(void)
// Addressing Setting
0x20, 0x00 // Set Memory Addressing Mode: Horizontal Addressing Mode
}; // command to initialize the display
return i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_init, LENGTH(oled_init)); // send command to initialize display
return i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_init, LENGTH(oled_init)); // send command to initialize display
}
void oled_ssd1306_on(void)
@ -65,7 +74,7 @@ void oled_ssd1306_on(void)
0x80, // control byte: no continuation, command
0xaf, // Set Display ON/OFF: ON
}; // command to switch on display
i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
}
void oled_ssd1306_off(void)
@ -74,7 +83,7 @@ void oled_ssd1306_off(void)
0x80, // control byte: no continuation, command
0xae, // Set Display ON/OFF: OFF
}; // command to switch off display
i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
}
void oled_ssd1306_test(void)
@ -83,7 +92,7 @@ void oled_ssd1306_test(void)
0x80, // control byte: no continuation, command
0xa5 // Entire Display ON: Entire display ON Output ignores RAM content
}; // command to set entire display on
i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
oled_ssd1306_on(); // set display on
sleep_ms(200); // let is on for a bit (enough for the user to see it is completely on
oled_ssd1306_off(); // set display off
@ -91,7 +100,7 @@ void oled_ssd1306_test(void)
0x80, // control byte: no continuation, command
0xa4 // Entire Display ON: Resume to RAM content display
}; // command to display RAM
i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
}
void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
@ -107,22 +116,22 @@ void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
0x00, // Set Lower Column Start Address for Page Addressing Mode: 0
0x10 // Set Higher Column Start Address for Page Addressing Mode: 0
}; // command to set addressing mode
i2c_master_slave_write(OLED_SSD1306_SLAVE, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
if (!i2c_master_start()) { // send start condition
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
if (!i2c_master_start(OLED_SSD1306_I2C)) { // send start condition
return;
}
if (!i2c_master_select_slave(OLED_SSD1306_SLAVE, true)) { // select OLED display
if (!i2c_master_select_slave(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, true)) { // select OLED display
return;
}
const uint8_t oled_data[] = {
0x40, // control byte: continuous (multiple byes), data
};
if (!i2c_master_write(oled_data, LENGTH(oled_data))) { // send data header
if (!i2c_master_write(OLED_SSD1306_I2C, oled_data, LENGTH(oled_data))) { // send data header
return;
}
if (!i2c_master_write(display_data, display_length)) { // send template picture to display
if (!i2c_master_write(OLED_SSD1306_I2C, display_data, display_length)) { // send template picture to display
return;
}
i2c_master_stop(); // send stop condition
i2c_master_stop(OLED_SSD1306_I2C); // send stop condition
}