Create init user data functions for I2C/SPI HW/SW and optimize SW I2C

This commit is contained in:
servadmin 2021-11-11 13:43:37 -05:00
parent e823a8316c
commit 22bb2654ad
11 changed files with 172 additions and 117 deletions

View File

@ -8,40 +8,35 @@
#define OLED_SPI_PIN_DC 198
// CS pin is controlled by linux spi driver, thus not defined here, but need to be wired
// #define OLED_SPI_PIN_CS 8
// #define OLED_SPI_PIN_CS 0
int main(void)
{
u8g2_t u8g2;
int main(void) {
u8g2_t u8g2;
// Initialization
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_arm_linux_hw_spi, u8x8_arm_linux_gpio_and_delay);
init_user_data(&u8g2, GPIO_CHIP_NUM, SPI_BUS);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES);
// Initialization
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0,
u8x8_byte_arm_linux_hw_spi, u8x8_arm_linux_gpio_and_delay);
init_spi_hw(&u8g2, GPIO_CHIP_NUM, SPI_BUS, OLED_SPI_PIN_DC, OLED_SPI_PIN_RES);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_InitDisplay(&u8g2);
u8g2_ClearBuffer(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
// Draw
/* full buffer example, setup procedure ends in _f */
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
u8g2_DrawStr(&u8g2, 1, 18, "U8g2 HW SPI");
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
u8g2_DrawStr(&u8g2, 1, 18, "U8g2 HW SPI");
u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603);
u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 );
u8g2_SendBuffer(&u8g2);
u8g2_SendBuffer(&u8g2);
printf("Initialized ...\n");
sleep_ms(5000);
u8g2_SetPowerSave(&u8g2, 1);
// Close and deallocate SPI resources
done_spi();
// Close and deallocate GPIO resources
done_user_data(&u8g2);
printf("Done\n");
return 0;
printf("Initialized ...\n");
sleep_ms(5000);
u8g2_SetPowerSave(&u8g2, 1);
// Close and deallocate SPI resources
done_spi();
// Close and deallocate GPIO resources
done_user_data(&u8g2);
printf("Done\n");
return 0;
}

View File

@ -3,50 +3,44 @@
// GPIO chip number for character device
#define GPIO_CHIP_NUM 0
#define OLED_SPI_PIN_MOSI 13
#define OLED_SPI_PIN_SCK 26
#define OLED_SPI_PIN_MOSI 15
#define OLED_SPI_PIN_SCK 14
// Use a different pin here rather than default SPI pins, which may cause issue
#define OLED_SPI_PIN_RES 25
#define OLED_SPI_PIN_DC 24
#define OLED_SPI_PIN_CS 23
#define OLED_SPI_PIN_RES 199
#define OLED_SPI_PIN_DC 198
#define OLED_SPI_PIN_CS 13
int main(void)
{
u8g2_t u8g2;
int main(void) {
u8g2_t u8g2;
// Initialization
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_arm_linux_gpio_and_delay);
// Bus arg doesn't matter for software SPI
init_user_data(&u8g2, GPIO_CHIP_NUM, 0);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_DATA, OLED_SPI_PIN_MOSI);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_CLOCK, OLED_SPI_PIN_SCK);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS);
// Initialization
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi,
u8x8_arm_linux_gpio_and_delay);
init_spi_sw(&u8g2, GPIO_CHIP_NUM, OLED_SPI_PIN_DC, OLED_SPI_PIN_RES,
OLED_SPI_PIN_MOSI, OLED_SPI_PIN_SCK, OLED_SPI_PIN_CS, 0);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
// Draw
/* full buffer example, setup procedure ends in _f */
u8g2_ClearBuffer(&u8g2);
// Draw
/* full buffer example, setup procedure ends in _f */
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
u8g2_DrawStr(&u8g2, 1, 18, "U8g2 SW SPI");
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
u8g2_DrawStr(&u8g2, 1, 18, "U8g2 SW SPI");
u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 );
u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603);
u8g2_SendBuffer(&u8g2);
u8g2_SendBuffer(&u8g2);
printf("Initialized ...\n");
sleep_ms(5000);
u8g2_SetPowerSave(&u8g2, 1);
// Close and deallocate GPIO resources
done_user_data(&u8g2);
printf("Done\n");
printf("Initialized ...\n");
sleep_ms(5000);
u8g2_SetPowerSave(&u8g2, 1);
// Close and deallocate GPIO resources
done_user_data(&u8g2);
printf("Done\n");
return 0;
return 0;
}

View File

@ -9,9 +9,8 @@ int main(void)
u8g2_t u8g2;
// Initialization
u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_arm_linux_hw_i2c, u8x8_arm_linux_gpio_and_delay);
// GPIO chip doesn't matter for hardware I2C
init_user_data(&u8g2, 0, I2C_BUS);
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_arm_linux_hw_i2c, u8x8_arm_linux_gpio_and_delay);
init_i2c_hw(&u8g2, I2C_BUS);
u8g2_SetI2CAddress(&u8g2, I2C_ADDRESS);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);

View File

@ -2,20 +2,16 @@
// GPIO chip number for character device
#define GPIO_CHIP_NUM 0
#define OLED_I2C_PIN_SCL 20
#define OLED_I2C_PIN_SDA 21
#define OLED_I2C_PIN_SCL 11
#define OLED_I2C_PIN_SDA 12
int main(void)
{
u8g2_t u8g2;
// Initialization
u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_arm_linux_gpio_and_delay);
// Bus doesn't matter for software I2C
init_user_data(&u8g2, GPIO_CHIP_NUM, 0);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_CLOCK, OLED_I2C_PIN_SCL);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_DATA, OLED_I2C_PIN_SDA);
u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, U8X8_PIN_NONE);
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_arm_linux_gpio_and_delay);
init_i2c_sw(&u8g2, GPIO_CHIP_NUM, OLED_I2C_PIN_SCL, OLED_I2C_PIN_SDA, 0);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);

View File

@ -8,27 +8,26 @@
#define OLED_SPI_PIN_DC 198
// CS pin is controlled by linux spi driver
#define OLED_SPI_PIN_CS 8
#define OLED_SPI_PIN_CS 0
// Check https://github.com/olikraus/u8g2/wiki/u8g2setupcpp for all supported devices
static U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0,
/* cs=*/ OLED_SPI_PIN_CS,
/* dc=*/ OLED_SPI_PIN_DC,
/* reset=*/ OLED_SPI_PIN_RES);
// same as the NONAME variant, but may solve the "every 2nd line skipped" problem
/* cs=*/OLED_SPI_PIN_CS,
/* dc=*/OLED_SPI_PIN_DC,
/* reset=*/OLED_SPI_PIN_RES);
// same as the NONAME variant, but may solve the "every 2nd line skipped" problem
int main()
{
u8g2.intUserData(GPIO_CHIP_NUM, SPI_BUS);
u8g2.begin();
u8g2.setFont(u8g2_font_6x13_tr); // choose a suitable font
u8g2.drawStr(1, 18, "U8g2 on HW SPI"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawGlyph(112, 56, 0x2603 );
u8g2.sendBuffer();
u8g2.sleepMs(5000);
u8g2.setPowerSave(1);
u8g2.doneSpi();
u8g2.doneUserData();
int main() {
u8g2.initSpiHw(GPIO_CHIP_NUM, SPI_BUS, OLED_SPI_PIN_DC, OLED_SPI_PIN_RES);
u8g2.begin();
u8g2.setFont(u8g2_font_6x13_tr); // choose a suitable font
u8g2.drawStr(1, 18, "U8g2 on HW SPI"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawGlyph(112, 56, 0x2603);
u8g2.sendBuffer();
u8g2.sleepMs(5000);
u8g2.setPowerSave(1);
u8g2.doneSpi();
u8g2.doneUserData();
}

View File

@ -2,13 +2,15 @@
// GPIO chip number for character device
#define GPIO_CHIP_NUM 0
#define OLED_SPI_PIN_MOSI 13
#define OLED_SPI_PIN_SCK 26
#define OLED_SPI_PIN_MOSI 15
#define OLED_SPI_PIN_SCK 14
// Use a different pin here rather than default SPI pins, which may cause issue
#define OLED_SPI_PIN_RES 25
#define OLED_SPI_PIN_DC 24
#define OLED_SPI_PIN_CS 23
#define OLED_SPI_PIN_RES 199
#define OLED_SPI_PIN_DC 198
#define OLED_SPI_PIN_CS 13
// Check https://github.com/olikraus/u8g2/wiki/u8g2setupcpp for all supported devices
static U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0,\
@ -20,8 +22,7 @@ static U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0,\
int main()
{
// Bus arg doesn't matter for software SPI
u8g2.intUserData(GPIO_CHIP_NUM, 0);
u8g2.initSpiSw(GPIO_CHIP_NUM, OLED_SPI_PIN_DC, OLED_SPI_PIN_RES, OLED_SPI_PIN_MOSI, OLED_SPI_PIN_SCK, OLED_SPI_PIN_CS, 0);
u8g2.begin();
u8g2.setFont(u8g2_font_6x13_tr); // choose a suitable font
u8g2.drawStr(1, 18, "U8g2 on SW SPI"); // write something to the internal memory

View File

@ -9,7 +9,7 @@ static U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_N
int main(void)
{
// GPIO chip doesn't matter for hardware I2C
u8g2.intUserData(0, I2C_BUS);
u8g2.initI2cHw(I2C_BUS);
u8g2.setI2CAddress(I2C_ADDRESS);
u8g2.begin();
u8g2.setFont(u8g2_font_6x13_tr); // choose a suitable font

View File

@ -2,8 +2,8 @@
// GPIO chip number for character device
#define GPIO_CHIP_NUM 0
#define OLED_I2C_PIN_SCL 20
#define OLED_I2C_PIN_SDA 21
#define OLED_I2C_PIN_SCL 11
#define OLED_I2C_PIN_SDA 12
// Check https://github.com/olikraus/u8g2/wiki/u8g2setupcpp for all supported devices
static U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,
@ -14,8 +14,7 @@ static U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,
int main(void)
{
// Bus doesn't matter for software I2C
u8g2.intUserData(GPIO_CHIP_NUM, 0);
u8g2.initI2cSw(GPIO_CHIP_NUM, OLED_I2C_PIN_SCL, OLED_I2C_PIN_SDA, 0);
u8g2.begin();
u8g2.setFont(u8g2_font_6x13_tr); // choose a suitable font
u8g2.drawStr(1, 18, "U8g2 on SW I2C"); // write something to the internal memory

View File

@ -133,8 +133,19 @@ public:
}
#endif
void intUserData(uint8_t gpio_chip, uint8_t bus) {
init_user_data(&u8g2, gpio_chip, bus);
void initI2cHw(uint8_t bus) {
init_i2c_hw(&u8g2, bus);
}
void initI2cSw(uint8_t gpio_chip, uint8_t scl, uint8_t sda,
unsigned long delay) {
init_i2c_sw(&u8g2, gpio_chip, scl, sda, delay);
}
void initSpiHw(uint8_t gpio_chip, uint8_t bus, uint8_t dc, uint8_t res) {
init_spi_hw(&u8g2, gpio_chip, bus, dc, res);
}
void initSpiSw(uint8_t gpio_chip, uint8_t dc, uint8_t res,
uint8_t mosi, uint8_t sck, uint8_t cs, unsigned long delay) {
init_spi_sw(&u8g2, gpio_chip, dc, res, mosi, sck, cs, delay);
}
void doneUserData(void) {
done_user_data(&u8g2);

View File

@ -43,9 +43,38 @@ void sleep_ns(unsigned long nanoseconds) {
}
/*
* Allocate user_data_struct, set values and set user_ptr.
* Allocate user_data_struct for I2C hardware.
*/
void init_user_data(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t bus) {
void init_i2c_hw(u8g2_t *u8g2, uint8_t bus) {
// Dynamically allocate u8x8_buffer_struct
user_data_t *user_data = (user_data_t*) malloc(sizeof(user_data_t));
user_data->bus = bus;
u8g2_SetUserPtr(u8g2, user_data);
}
/*
* Allocate user_data_struct for I2C software.
*/
void init_i2c_sw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t scl, uint8_t sda,
unsigned long delay) {
// Dynamically allocate u8x8_buffer_struct
user_data_t *user_data = (user_data_t*) malloc(sizeof(user_data_t));
user_data->gpio_chip = gpio_chip;
for (int i = 0; i < U8X8_PIN_CNT; ++i) {
user_data->pins[i] = NULL;
}
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_CLOCK, scl);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_DATA, sda);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, U8X8_PIN_NONE);
user_data->delay = delay;
u8g2_SetUserPtr(u8g2, user_data);
}
/*
* Allocate user_data_struct for hardware SPI.
*/
void init_spi_hw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t bus, uint8_t dc,
uint8_t res) {
// Dynamically allocate u8x8_buffer_struct
user_data_t *user_data = (user_data_t*) malloc(sizeof(user_data_t));
user_data->gpio_chip = gpio_chip;
@ -53,6 +82,29 @@ void init_user_data(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t bus) {
for (int i = 0; i < U8X8_PIN_CNT; ++i) {
user_data->pins[i] = NULL;
}
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, dc);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, res);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, U8X8_PIN_NONE);
u8g2_SetUserPtr(u8g2, user_data);
}
/*
* Allocate user_data_struct for hardware SPI.
*/
void init_spi_sw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t dc, uint8_t res,
uint8_t mosi, uint8_t sck, uint8_t cs, unsigned long delay) {
// Dynamically allocate u8x8_buffer_struct
user_data_t *user_data = (user_data_t*) malloc(sizeof(user_data_t));
user_data->gpio_chip = gpio_chip;
for (int i = 0; i < U8X8_PIN_CNT; ++i) {
user_data->pins[i] = NULL;
}
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, dc);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, res);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_SPI_DATA, mosi);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_SPI_CLOCK, sck);
u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, cs);
user_data->delay = delay;
u8g2_SetUserPtr(u8g2, user_data);
}
@ -191,6 +243,8 @@ void done_spi() {
*/
uint8_t u8x8_arm_linux_gpio_and_delay(u8x8_t *u8x8, uint8_t msg,
uint8_t arg_int, void *arg_ptr) {
user_data_t *user_data;
(void) arg_ptr; /* suppress unused parameter warning */
switch (msg) {
case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
@ -210,12 +264,11 @@ uint8_t u8x8_arm_linux_gpio_and_delay(u8x8_t *u8x8, uint8_t msg,
break;
case U8X8_MSG_DELAY_I2C:
// arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
// arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
if (arg_int == 1) {
sleep_us(5);
} else if (arg_int == 4) {
sleep_ns(1250);
// arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz, but we ignore
// that and use user_data->delay
user_data = u8x8_GetUserPtr(u8x8);
if (user_data->delay != 0) {
sleep_ns(user_data->delay);
}
break;

View File

@ -30,6 +30,8 @@ struct user_data_struct {
uint8_t index;
// Display buffer, I2C should send 32 bytes max and SPI 128 bytes max
uint8_t buffer[128];
// Nanosecond delay for U8X8_MSG_DELAY_I2C
unsigned long delay;
};
typedef struct user_data_struct user_data_t;
@ -37,7 +39,13 @@ typedef struct user_data_struct user_data_t;
void sleep_ms(unsigned long milliseconds);
void sleep_us(unsigned long microseconds);
void sleep_ns(unsigned long nanoseconds);
void init_user_data(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t bus);
void init_i2c_hw(u8g2_t *u8g2, uint8_t bus);
void init_i2c_sw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t scl, uint8_t sda,
unsigned long delay);
void init_spi_hw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t bus, uint8_t dc,
uint8_t res);
void init_spi_sw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t dc, uint8_t res,
uint8_t mosi, uint8_t sck, uint8_t cs, unsigned long delay);
void done_user_data(u8g2_t *u8g2);
void init_pin(u8x8_t *u8x8, uint8_t pin);
void write_pin(u8x8_t *u8x8, uint8_t pin, uint8_t value);