oled_ssd1306: adapt to ported I²C library

This commit is contained in:
King Kévin 2020-12-12 14:09:35 +01:00
parent b0f5f127f6
commit ad52abc26b
2 changed files with 16 additions and 23 deletions

View File

@ -2,8 +2,8 @@
* @file
* @author King Kévin <kingkevin@cuvoodoo.info>
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
* @date 2018-2019
* @note peripherals used: I2C @ref oled_ssd1306_i2c
* @date 2018-2020
* @note peripherals used: I2C @ref i2c_master_i2c
*/
/* standard libraries */
#include <stdint.h> // standard integer types
@ -21,19 +21,13 @@
/** SSD1306 OLED display I²C slave address */
static uint8_t oled_ssd1306_slave_addr = 0x3c;
/** @defgroup oled_ssd1306_i2c I²C peripheral to communicate with the SSD1306 OLED
* @{
*/
#define OLED_SSD1306_I2C I2C1 /**< I²C peripheral */
/** @} */
bool oled_ssd1306_setup(uint8_t slave_addr)
{
if (!i2c_master_check_signals(OLED_SSD1306_I2C)) { // check if there are pull-ups to operator I²C
if (!i2c_master_check_signals()) { // check if there are pull-ups to operator I²C
return false;
}
oled_ssd1306_slave_addr = slave_addr; // save I²C slave address of SSD1306
i2c_master_setup(OLED_SSD1306_I2C, 400); // setup I²C bus (SSD1306 supports an I²C clock up to 400 kHz)
i2c_master_setup(400); // setup I²C bus (SSD1306 supports an I²C clock up to 400 kHz)
const uint8_t oled_init[] = {
0x00, // control byte: continuous (multiple byes), command
0xae, // Set Display ON/OFF: OFF
@ -56,7 +50,7 @@ bool oled_ssd1306_setup(uint8_t slave_addr)
// Addressing Setting
0x20, 0x00 // Set Memory Addressing Mode: Horizontal Addressing Mode
}; // command to initialize the display
return I2C_MASTER_RC_NONE == i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_init, LENGTH(oled_init)); // send command to initialize display
return I2C_MASTER_RC_NONE == i2c_master_slave_write(oled_ssd1306_slave_addr, false, oled_init, LENGTH(oled_init)); // send command to initialize display
}
void oled_ssd1306_on(void)
@ -65,7 +59,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_I2C, oled_ssd1306_slave_addr, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
i2c_master_slave_write(oled_ssd1306_slave_addr, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
}
void oled_ssd1306_off(void)
@ -74,7 +68,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_I2C, oled_ssd1306_slave_addr, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch font display
i2c_master_slave_write(oled_ssd1306_slave_addr, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch font display
}
void oled_ssd1306_test(void)
@ -83,7 +77,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_I2C, oled_ssd1306_slave_addr, false, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
i2c_master_slave_write(oled_ssd1306_slave_addr, false, 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 +85,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_I2C, oled_ssd1306_slave_addr, false, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
i2c_master_slave_write(oled_ssd1306_slave_addr, false, 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,11 +101,11 @@ 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_I2C, oled_ssd1306_slave_addr, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
if (I2C_MASTER_RC_NONE != i2c_master_start(OLED_SSD1306_I2C)) { // send start condition
i2c_master_slave_write(oled_ssd1306_slave_addr, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
if (I2C_MASTER_RC_NONE != i2c_master_start()) { // send start condition
return;
}
if (I2C_MASTER_RC_NONE != i2c_master_select_slave(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, true)) { // select OLED display
if (I2C_MASTER_RC_NONE != i2c_master_select_slave(oled_ssd1306_slave_addr, false, true)) { // select OLED display
return;
}
uint8_t oled_data[display_length + 1]; // we have to copy the data because we need to send a single block since i2c_master_write sends a stop
@ -119,9 +113,9 @@ void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
for (uint16_t i = 0; i < display_length; i++) {
oled_data[i + 1] = display_data[i];
}
if (I2C_MASTER_RC_NONE != i2c_master_write(OLED_SSD1306_I2C, oled_data, display_length + 1)) { // send data header
if (I2C_MASTER_RC_NONE != i2c_master_write(oled_data, display_length + 1)) { // send data header
return;
}
i2c_master_stop(OLED_SSD1306_I2C); // send stop condition
i2c_master_stop(); // send stop condition
}

View File

@ -2,11 +2,10 @@
* @file
* @author King Kévin <kingkevin@cuvoodoo.info>
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
* @date 2018-2019
* @note peripherals used: I2C @ref oled_ssd1306_i2c
* @date 2018-2020
* @note peripherals used: I2C @ref i2c_master_i2c
*/
#pragma once
#error not converted for STM32F4
/** setup OLED display
* @param[in] slave_addr I²C slave address of SSD1306 device (least significant 7-bit)