From 7df33e50bf160d72f9befe1164c30905e19131df Mon Sep 17 00:00:00 2001 From: Wu Han Date: Thu, 27 Dec 2018 20:15:20 +0800 Subject: [PATCH] Split examples (sw_i2c,hw_i2c,sw_spi,hw_spi) --- SConscript | 13 ++- examples/ssd1306_4wire_hw_spi_example.c | 37 ++++++++ examples/ssd1306_4wire_sw_spi_example.c | 41 +++++++++ examples/ssd1306_example.c | 108 ------------------------ examples/ssd1306_hw_i2c_example.c | 26 ++++++ examples/ssd1306_sw_i2c_example.c | 32 +++++++ port/u8g2_port.c | 18 ++-- 7 files changed, 152 insertions(+), 123 deletions(-) create mode 100644 examples/ssd1306_4wire_hw_spi_example.c create mode 100644 examples/ssd1306_4wire_sw_spi_example.c delete mode 100644 examples/ssd1306_example.c create mode 100644 examples/ssd1306_hw_i2c_example.c create mode 100644 examples/ssd1306_sw_i2c_example.c diff --git a/SConscript b/SConscript index d0af32f3..d0bccba9 100644 --- a/SConscript +++ b/SConscript @@ -8,8 +8,17 @@ src = Glob('inc/*.h') src = Glob('src/*.c') src += Glob('port/*.c') -if GetDepend('U8G2_USING_I2C_SSD1306'): - src += Glob('examples/ssd1306_example.c') +if GetDepend('U8G2_USING_SW_I2C_SSD1306'): + src += Glob('examples/ssd1306_sw_i2c_example.c') + +if GetDepend('U8G2_USING_HW_I2C_SSD1306'): + src += Glob('examples/ssd1306_hw_i2c_example.c') + +if GetDepend('U8G2_USING_SW_SPI_SSD1306'): + src += Glob('examples/ssd1306_4wire_sw_spi_example.c') + +if GetDepend('U8G2_USING_HW_SPI_SSD1306'): + src += Glob('examples/ssd1306_4wire_hw_spi_example.c') if GetDepend('U8G2_USING_I2C_YL40'): src += Glob('examples/yl_40_example.c') diff --git a/examples/ssd1306_4wire_hw_spi_example.c b/examples/ssd1306_4wire_hw_spi_example.c new file mode 100644 index 00000000..92831668 --- /dev/null +++ b/examples/ssd1306_4wire_hw_spi_example.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +// You may reference Drivers/drv_gpio.c for pinout +// In u8x8.h #define U8X8_USE_PINS + +#define OLED_SPI_PIN_RES 16 // PA2 +#define OLED_SPI_PIN_DC 15 // PA1 +#define OLED_SPI_PIN_CS 14 // PA0 + +static void ssd1306_example_4wire_hw_spi(int argc,char *argv[]) +{ + u8g2_t u8g2; + + // Initialization + u8g2_Setup_ssd1306_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rt_4wire_hw_spi, u8x8_rt_gpio_and_delay); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES); + + u8g2_InitDisplay(&u8g2); + u8g2_SetPowerSave(&u8g2, 0); + + // Draw Graphics + /* full buffer example, setup procedure ends in _f */ + u8g2_ClearBuffer(&u8g2); + u8g2_SetFont(&u8g2, u8g2_font_baby_tf); + u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); + u8g2_SendBuffer(&u8g2); + + u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); + u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); + u8g2_SendBuffer(&u8g2); +} +MSH_CMD_EXPORT(ssd1306_example_4wire_hw_spi, sw 4wire spi ssd1306 sample); diff --git a/examples/ssd1306_4wire_sw_spi_example.c b/examples/ssd1306_4wire_sw_spi_example.c new file mode 100644 index 00000000..f3d2f75e --- /dev/null +++ b/examples/ssd1306_4wire_sw_spi_example.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +// You may reference Drivers/drv_gpio.c for pinout +// In u8x8.h #define U8X8_USE_PINS + +#define OLED_SPI_PIN_CLK 21 // PA5 +#define OLED_SPI_PIN_MOSI 23 // PA7 +#define OLED_SPI_PIN_RES 16 // PA2 +#define OLED_SPI_PIN_DC 15 // PA1 +#define OLED_SPI_PIN_CS 14 // PA0 + +static void ssd1306_example_4wire_sw_spi(int argc,char *argv[]) +{ + u8g2_t u8g2; + + // Initialization + u8g2_Setup_ssd1306_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_rt_gpio_and_delay); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_CLOCK, OLED_SPI_PIN_CLK); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_DATA, OLED_SPI_PIN_MOSI); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC); + u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES); + + u8g2_InitDisplay(&u8g2); + u8g2_SetPowerSave(&u8g2, 0); + + // Draw Graphics + /* full buffer example, setup procedure ends in _f */ + u8g2_ClearBuffer(&u8g2); + u8g2_SetFont(&u8g2, u8g2_font_baby_tf); + u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); + u8g2_SendBuffer(&u8g2); + + u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); + u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); + u8g2_SendBuffer(&u8g2); +} +MSH_CMD_EXPORT(ssd1306_example_4wire_sw_spi, sw 4wire spi ssd1306 sample); diff --git a/examples/ssd1306_example.c b/examples/ssd1306_example.c deleted file mode 100644 index ddf2633f..00000000 --- a/examples/ssd1306_example.c +++ /dev/null @@ -1,108 +0,0 @@ -#include -#include -#include -#include - -// You may reference Drivers/drv_gpio.c for pinout -// In u8x8.h #define U8X8_USE_PINS - -#define OLED_I2C_PIN_SCL 58 // PB6 -#define OLED_I2C_PIN_SDA 59 // PB7 - -#define OLED_SPI_PIN_CLK 21 // PA5 -#define OLED_SPI_PIN_MOSI 23 // PA7 -#define OLED_SPI_PIN_RES 16 // PA2 -#define OLED_SPI_PIN_DC 15 // PA1 -#define OLED_SPI_PIN_CS 14 // PA0 - -static void ssd1306_example_sw_i2c(int argc,char *argv[]) -{ - u8g2_t u8g2; - u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_rt_gpio_and_delay); - 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); - - u8g2_InitDisplay(&u8g2); - u8g2_SetPowerSave(&u8g2, 0); - - /* full buffer example, setup procedure ends in _f */ - u8g2_ClearBuffer(&u8g2); - u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr); - u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); - u8g2_SendBuffer(&u8g2); - - u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); - u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); - u8g2_SendBuffer(&u8g2); -} -MSH_CMD_EXPORT(ssd1306_example_sw_i2c, i2c ssd1306 software i2c sample); - -static void ssd1306_example_hw_i2c(int argc,char *argv[]) -{ - u8g2_t u8g2; - u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rt_hw_i2c, u8x8_rt_gpio_and_delay); - - u8g2_InitDisplay(&u8g2); - u8g2_SetPowerSave(&u8g2, 0); - - /* full buffer example, setup procedure ends in _f */ - u8g2_ClearBuffer(&u8g2); - u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr); - u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); - u8g2_SendBuffer(&u8g2); - - u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); - u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); - u8g2_SendBuffer(&u8g2); -} -MSH_CMD_EXPORT(ssd1306_example_hw_i2c, i2c ssd1306 sample); - -static void ssd1306_example_4wire_sw_spi(int argc,char *argv[]) -{ - u8g2_t u8g2; - - u8g2_Setup_ssd1306_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_rt_gpio_and_delay); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_CLOCK, OLED_SPI_PIN_CLK); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_SPI_DATA, OLED_SPI_PIN_MOSI); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES); - - u8g2_InitDisplay(&u8g2); - u8g2_SetPowerSave(&u8g2, 0); - - /* full buffer example, setup procedure ends in _f */ - u8g2_ClearBuffer(&u8g2); - u8g2_SetFont(&u8g2, u8g2_font_baby_tf); - u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); - u8g2_SendBuffer(&u8g2); - - u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); - u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); - u8g2_SendBuffer(&u8g2); -} -MSH_CMD_EXPORT(ssd1306_example_4wire_sw_spi, sw 4wire spi ssd1306 sample); - -static void ssd1306_example_4wire_hw_spi(int argc,char *argv[]) -{ - u8g2_t u8g2; - - u8g2_Setup_ssd1306_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rt_4wire_hw_spi, u8x8_rt_gpio_and_delay); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC); - u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES); - - u8g2_InitDisplay(&u8g2); - u8g2_SetPowerSave(&u8g2, 0); - - /* full buffer example, setup procedure ends in _f */ - u8g2_ClearBuffer(&u8g2); - u8g2_SetFont(&u8g2, u8g2_font_baby_tf); - u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); - u8g2_SendBuffer(&u8g2); - - u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); - u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); - u8g2_SendBuffer(&u8g2); -} -MSH_CMD_EXPORT(ssd1306_example_4wire_hw_spi, sw 4wire spi ssd1306 sample); diff --git a/examples/ssd1306_hw_i2c_example.c b/examples/ssd1306_hw_i2c_example.c new file mode 100644 index 00000000..94521149 --- /dev/null +++ b/examples/ssd1306_hw_i2c_example.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +static void ssd1306_example_hw_i2c(int argc,char *argv[]) +{ + u8g2_t u8g2; + + // Initialization + u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rt_hw_i2c, u8x8_rt_gpio_and_delay); + u8g2_InitDisplay(&u8g2); + u8g2_SetPowerSave(&u8g2, 0); + + /* full buffer example, setup procedure ends in _f */ + u8g2_ClearBuffer(&u8g2); + u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr); + u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); + u8g2_SendBuffer(&u8g2); + + // Draw Graphics + u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); + u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); + u8g2_SendBuffer(&u8g2); +} +MSH_CMD_EXPORT(ssd1306_example_hw_i2c, i2c ssd1306 sample); diff --git a/examples/ssd1306_sw_i2c_example.c b/examples/ssd1306_sw_i2c_example.c new file mode 100644 index 00000000..8f895dc9 --- /dev/null +++ b/examples/ssd1306_sw_i2c_example.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#define OLED_I2C_PIN_SCL 58 // PB6 +#define OLED_I2C_PIN_SDA 59 // PB7 + +static void ssd1306_example_sw_i2c(int argc,char *argv[]) +{ + u8g2_t u8g2; + + // Initialization + u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_rt_gpio_and_delay); + 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); + + u8g2_InitDisplay(&u8g2); + u8g2_SetPowerSave(&u8g2, 0); + + // Draw Graphics + /* full buffer example, setup procedure ends in _f */ + u8g2_ClearBuffer(&u8g2); + u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr); + u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread"); + u8g2_SendBuffer(&u8g2); + + u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); + u8g2_DrawGlyph(&u8g2, 112, 56, 0x2603 ); + u8g2_SendBuffer(&u8g2); +} +MSH_CMD_EXPORT(ssd1306_example_sw_i2c, i2c ssd1306 software i2c sample); diff --git a/port/u8g2_port.c b/port/u8g2_port.c index 2f022c32..aa37d261 100644 --- a/port/u8g2_port.c +++ b/port/u8g2_port.c @@ -12,13 +12,12 @@ struct stm32_hw_spi_cs }; static struct stm32_hw_spi_cs spi_cs; -static int rt_hw_ssd1351_config(uint8_t spi_mode, uint32_t max_hz) +static int rt_hw_ssd1351_config(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin ) { rt_err_t res; - // Attach Device - spi_cs.pin = 14; + spi_cs.pin = cs_pin; rt_pin_mode(spi_cs.pin, PIN_MODE_OUTPUT); res = rt_spi_bus_attach_device(&spi_dev_ssd1306, SPI_SSD1306_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs); if (res != RT_EOK) @@ -215,16 +214,13 @@ uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, vo case U8X8_MSG_BYTE_SEND: data = (uint8_t *)arg_ptr; - // printf("Buffering Data %d \n", arg_int); while( arg_int > 0) { - // printf("%.2X ", (uint8_t)*data); buffer_tx[buf_idx++] = (uint8_t)*data; rt_spi_send(&spi_dev_ssd1306, (uint8_t*)data, 1); data++; arg_int--; } - // printf("\n"); break; case U8X8_MSG_BYTE_INIT: @@ -234,9 +230,8 @@ uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, vo /* 2: clock active low, data out on rising edge */ /* 3: clock active low, data out on falling edge */ u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level); - rt_hw_ssd1351_config(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz); - - // printf("SPI Device Mode Set\n"); + rt_hw_ssd1351_config(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]); + break; case U8X8_MSG_BYTE_SET_DC: @@ -251,14 +246,11 @@ uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, vo case U8X8_MSG_BYTE_END_TRANSFER: memset( tx, 0, ARRAY_SIZE(tx)*sizeof(uint8_t) ); memset( rx, 0, ARRAY_SIZE(rx)*sizeof(uint8_t) ); - - // printf("SPI Data Sending %d\n", buf_idx); + for (i = 0; i < buf_idx; ++i) { - // printf("%.2X ", buffer_tx[i]); tx[i] = buffer_tx[i]; } - // printf("\n"); u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL); u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);