Skip to content

Commit

Permalink
Split examples (sw_i2c,hw_i2c,sw_spi,hw_spi)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Han committed Dec 27, 2018
1 parent f2cbd70 commit 7df33e5
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 123 deletions.
13 changes: 11 additions & 2 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
37 changes: 37 additions & 0 deletions examples/ssd1306_4wire_hw_spi_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <u8g2_port.h>

// 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);
41 changes: 41 additions & 0 deletions examples/ssd1306_4wire_sw_spi_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <u8g2_port.h>

// 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);
108 changes: 0 additions & 108 deletions examples/ssd1306_example.c

This file was deleted.

26 changes: 26 additions & 0 deletions examples/ssd1306_hw_i2c_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <u8g2_port.h>

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);
32 changes: 32 additions & 0 deletions examples/ssd1306_sw_i2c_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <u8g2_port.h>

#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);
18 changes: 5 additions & 13 deletions port/u8g2_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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);
Expand Down

0 comments on commit 7df33e5

Please # to comment.