From 14452c93db24fcb80529a34e5189cf70e1039b82 Mon Sep 17 00:00:00 2001 From: Wu Han Date: Sun, 7 Apr 2019 22:41:36 +0800 Subject: [PATCH] Get device name from Kconfig --- port/u8g2_port.c | 516 ++++++++++++++++++++++++----------------------- port/u8g2_port.h | 30 ++- 2 files changed, 292 insertions(+), 254 deletions(-) diff --git a/port/u8g2_port.c b/port/u8g2_port.c index 397ff7b8..baa01872 100644 --- a/port/u8g2_port.c +++ b/port/u8g2_port.c @@ -3,256 +3,277 @@ #define MAX_RETRY 3 -static struct rt_i2c_bus_device *i2c_bus = RT_NULL; +#if defined U8G2_USE_HW_I2C +static struct rt_i2c_bus_device *i2c_bus = RT_NULL; +#endif -static struct rt_spi_device spi_dev_ssd1306; -struct stm32_hw_spi_cs +#if defined U8G2_USE_HW_SPI +static struct rt_spi_device u8g2_spi_dev; +struct rt_hw_spi_cs { rt_uint32_t pin; }; -static struct stm32_hw_spi_cs spi_cs; +static struct rt_hw_spi_cs spi_cs; -static int rt_hw_ssd1351_config(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin ) +int rt_hw_spi_config(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin ) { - rt_err_t res; - - // Attach Device - 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) - { - return res; - } - - // Set device SPI Mode - struct rt_spi_configuration cfg; - cfg.data_width = 8; - switch(spi_mode) - { - case 0: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; break; - case 1: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_1 | RT_SPI_MSB; break; - case 2: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_2 | RT_SPI_MSB; break; - case 3: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB; break; - } - cfg.max_hz = max_hz; /* 20M,SPI max 42MHz,ssd1351 4-wire spi */ - - rt_spi_configure(&spi_dev_ssd1306, &cfg); - - return RT_EOK; + rt_err_t res; + + // Attach Device + spi_cs.pin = cs_pin; + rt_pin_mode(spi_cs.pin, PIN_MODE_OUTPUT); + res = rt_spi_bus_attach_device(&u8g2_spi_dev, U8G2_SPI_DEVICE_NAME, U8G2_SPI_BUS_NAME, (void*)&spi_cs); + if (res != RT_EOK) + { + rt_kprintf("[u8g2] Failed to attach device %s\n", U8G2_SPI_DEVICE_NAME); + return res; + } + + // Set device SPI Mode + struct rt_spi_configuration cfg; + cfg.data_width = 8; + switch(spi_mode) + { + case 0: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; break; + case 1: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_1 | RT_SPI_MSB; break; + case 2: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_2 | RT_SPI_MSB; break; + case 3: cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB; break; + } + cfg.max_hz = max_hz; /* 20M,SPI max 42MHz,ssd1351 4-wire spi */ + rt_spi_configure(&u8g2_spi_dev, &cfg); + + return RT_EOK; } +#endif /* U8G2_USE_HW_SPI */ uint8_t u8x8_rt_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { - switch(msg) - { - case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second - __asm__ volatile("nop"); - break; - - case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds - __asm__ volatile("nop"); - break; - - case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds - for (uint16_t n = 0; n < 320; n++) - { - __asm__ volatile("nop"); - } - break; - - case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second - rt_thread_delay(arg_int); - break; - - case U8X8_MSG_GPIO_AND_DELAY_INIT: - // Function which implements a delay, arg_int contains the amount of ms - // set spi pin mode - rt_pin_mode(u8x8->pins[U8X8_PIN_SPI_CLOCK],PIN_MODE_OUTPUT);//d0 a5 15 d1 a7 17 res b0 18 dc b1 19 cs a4 14 - rt_pin_mode(u8x8->pins[U8X8_PIN_SPI_DATA],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_RESET],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_DC],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_CS],PIN_MODE_OUTPUT); - // set i2c pin mode - rt_pin_mode(u8x8->pins[U8X8_PIN_I2C_DATA],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],PIN_MODE_OUTPUT); - // set 8080 pin mode - rt_pin_mode(u8x8->pins[U8X8_PIN_D0],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D1],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D2],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D3],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D4],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D5],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D6],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_D7],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_E],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_DC],PIN_MODE_OUTPUT); - rt_pin_mode(u8x8->pins[U8X8_PIN_RESET],PIN_MODE_OUTPUT); - - // set value - rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],1); - rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],1); - rt_pin_write(u8x8->pins[U8X8_PIN_RESET],1); - rt_pin_write(u8x8->pins[U8X8_PIN_DC],1); - rt_pin_write(u8x8->pins[U8X8_PIN_CS],1); - break; - - case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz - for (uint16_t n = 0; n < (arg_int<=2?160:40); n++) - { - __asm__ volatile("nop"); - } - break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us - - //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int - //case U8X8_MSG_GPIO_SPI_CLOCK: - - //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int - //case U8X8_MSG_GPIO_SPI_DATA: - - case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D2],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D2],0); - break; - - case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D3],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D3],0); - break; - - case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D4],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D4],0); - break; - - case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D5],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D5],0); - break; - - case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D6],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D6],0); - break; - - case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D7],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_D7],0); - break; - - case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_E],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_E],0); - break; - - case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_I2C_CLOCK],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_I2C_CLOCK],0); - break; // arg_int=1: Input dir with pullup high for I2C clock pin - - case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_I2C_DATA],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_I2C_DATA],0); - break; // arg_int=1: Input dir with pullup high for I2C data pin - - case U8X8_MSG_GPIO_SPI_CLOCK: - //Function to define the logic level of the clockline - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],0); - break; - - case U8X8_MSG_GPIO_SPI_DATA: - //Function to define the logic level of the data line to the display - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],0); - break; - - case U8X8_MSG_GPIO_CS: - // Function to define the logic level of the CS line - if(arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_CS],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_CS],0); - break; - - case U8X8_MSG_GPIO_DC: - //Function to define the logic level of the Data/ Command line - if(arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_DC],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_DC],0); - break; - - case U8X8_MSG_GPIO_RESET: - //Function to define the logic level of the RESET line - if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_RESET],1); - else rt_pin_write(u8x8->pins[U8X8_PIN_RESET],0); - break; - - default: - //A message was received which is not implemented, return 0 to indicate an error - return 0; - } - return 1; + switch(msg) + { + case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second + __asm__ volatile("nop"); + break; + + case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds + __asm__ volatile("nop"); + break; + + case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds + for (uint16_t n = 0; n < 320; n++) + { + __asm__ volatile("nop"); + } + break; + + case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second + rt_thread_delay(arg_int); + break; + + case U8X8_MSG_GPIO_AND_DELAY_INIT: + // Function which implements a delay, arg_int contains the amount of ms + + // set spi pin mode + rt_pin_mode(u8x8->pins[U8X8_PIN_SPI_CLOCK],PIN_MODE_OUTPUT);//d0 a5 15 d1 a7 17 res b0 18 dc b1 19 cs a4 14 + rt_pin_mode(u8x8->pins[U8X8_PIN_SPI_DATA],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_RESET],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_DC],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_CS],PIN_MODE_OUTPUT); + + // set i2c pin mode + rt_pin_mode(u8x8->pins[U8X8_PIN_I2C_DATA],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],PIN_MODE_OUTPUT); + + // set 8080 pin mode + rt_pin_mode(u8x8->pins[U8X8_PIN_D0],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D1],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D2],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D3],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D4],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D5],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D6],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_D7],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_E],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_DC],PIN_MODE_OUTPUT); + rt_pin_mode(u8x8->pins[U8X8_PIN_RESET],PIN_MODE_OUTPUT); + + // set value + rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],1); + rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],1); + rt_pin_write(u8x8->pins[U8X8_PIN_RESET],1); + rt_pin_write(u8x8->pins[U8X8_PIN_DC],1); + rt_pin_write(u8x8->pins[U8X8_PIN_CS],1); + 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 + for (uint16_t n = 0; n < (arg_int<=2?160:40); n++) + { + __asm__ volatile("nop"); + } + break; + + //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int + //case U8X8_MSG_GPIO_SPI_CLOCK: + + //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int + //case U8X8_MSG_GPIO_SPI_DATA: + + case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D2],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D2],0); + break; + + case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D3],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D3],0); + break; + + case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D4],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D4],0); + break; + + case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D5],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D5],0); + break; + + case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D6],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D6],0); + break; + + case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_D7],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_D7],0); + break; + + case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_E],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_E],0); + break; + + case U8X8_MSG_GPIO_I2C_CLOCK: + // arg_int=0: Output low at I2C clock pin + // arg_int=1: Input dir with pullup high for I2C clock pin + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_I2C_CLOCK],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_I2C_CLOCK],0); + break; + + case U8X8_MSG_GPIO_I2C_DATA: + // arg_int=0: Output low at I2C data pin + // arg_int=1: Input dir with pullup high for I2C data pin + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_I2C_DATA],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_I2C_DATA],0); + break; + + case U8X8_MSG_GPIO_SPI_CLOCK: + //Function to define the logic level of the clockline + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_SPI_CLOCK],0); + break; + + case U8X8_MSG_GPIO_SPI_DATA: + //Function to define the logic level of the data line to the display + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_SPI_DATA],0); + break; + + case U8X8_MSG_GPIO_CS: + // Function to define the logic level of the CS line + if(arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_CS],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_CS],0); + break; + + case U8X8_MSG_GPIO_DC: + //Function to define the logic level of the Data/ Command line + if(arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_DC],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_DC],0); + break; + + case U8X8_MSG_GPIO_RESET: + //Function to define the logic level of the RESET line + if (arg_int) rt_pin_write(u8x8->pins[U8X8_PIN_RESET],1); + else rt_pin_write(u8x8->pins[U8X8_PIN_RESET],0); + break; + + default: + //A message was received which is not implemented, return 0 to indicate an error + return 0; + } + return 1; } +#if defined U8G2_USE_HW_I2C uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { - /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ - struct rt_i2c_msg msgs; - static uint8_t buffer[32]; - static uint8_t buf_idx; - uint8_t *data; - - rt_uint8_t t = 0; - switch(msg) - { - case U8X8_MSG_BYTE_SEND: - data = (uint8_t *)arg_ptr; - while( arg_int > 0 ) - { - buffer[buf_idx++] = *data; - data++; - arg_int--; - } - break; - case U8X8_MSG_BYTE_INIT: - i2c_bus = rt_i2c_bus_device_find(I2C_DEVICE_NAME); - if (i2c_bus == RT_NULL) - { - // rt_kprintf("Failed to find bus\n"); - return 0; - } - break; - case U8X8_MSG_BYTE_SET_DC: - break; - case U8X8_MSG_BYTE_START_TRANSFER: - buf_idx = 0; - break; - case U8X8_MSG_BYTE_END_TRANSFER: - if (i2c_bus == RT_NULL) - { - rt_kprintf("Failed to find bus\n"); - return 0; - } - // I2C Data Transfer - msgs.addr = u8x8_GetI2CAddress(u8x8)>>1; - msgs.flags = RT_I2C_WR; - msgs.buf = buffer; - msgs.len = buf_idx; - while(rt_i2c_transfer(i2c_bus, &msgs, 1) != 1 && t < MAX_RETRY) - { - t++; - }; - if(t >= MAX_RETRY) - { - return 0; - } - break; - default: - return 0; - } - return 1; + /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ + struct rt_i2c_msg msgs; + static uint8_t buffer[32]; + static uint8_t buf_idx; + uint8_t *data; + + rt_uint8_t t = 0; + switch(msg) + { + case U8X8_MSG_BYTE_SEND: + data = (uint8_t *)arg_ptr; + while( arg_int > 0 ) + { + buffer[buf_idx++] = *data; + data++; + arg_int--; + } + break; + + case U8X8_MSG_BYTE_INIT: + i2c_bus = rt_i2c_bus_device_find(U8G2_I2C_DEVICE_NAME); + if (i2c_bus == RT_NULL) + { + rt_kprintf("[u8g2] Failed to find bus %s\n", U8G2_I2C_DEVICE_NAME); + return 0; + } + break; + + case U8X8_MSG_BYTE_SET_DC: + break; + + case U8X8_MSG_BYTE_START_TRANSFER: + buf_idx = 0; + break; + + case U8X8_MSG_BYTE_END_TRANSFER: + if (i2c_bus == RT_NULL) + { + rt_kprintf("[u8g2] Failed to find bus %s\n", U8G2_I2C_DEVICE_NAME); + return 0; + } + // I2C Data Transfer + msgs.addr = u8x8_GetI2CAddress(u8x8)>>1; + msgs.flags = RT_I2C_WR; + msgs.buf = buffer; + msgs.len = buf_idx; + while(rt_i2c_transfer(i2c_bus, &msgs, 1) != 1 && t < MAX_RETRY) + { + t++; + }; + if(t >= MAX_RETRY) + { + return 0; + } + break; + + default: + return 0; + } + return 1; } +#endif /* U8G2_USE_HW_I2C */ +#if defined U8G2_USE_HW_SPI uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { - + uint8_t i; uint8_t *data; @@ -266,14 +287,14 @@ 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; - + while( arg_int > 0) { buffer_tx[buf_idx++] = (uint8_t)*data; - rt_spi_send(&spi_dev_ssd1306, (uint8_t*)data, 1); + rt_spi_send(&u8g2_spi_dev, (uint8_t*)data, 1); data++; arg_int--; - } + } break; case U8X8_MSG_BYTE_INIT: @@ -282,9 +303,8 @@ uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, vo /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */ /* 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, u8x8->pins[U8X8_PIN_CS]); - + u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level); + rt_hw_spi_config(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]); break; case U8X8_MSG_BYTE_SET_DC: @@ -292,27 +312,27 @@ uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, vo break; case U8X8_MSG_BYTE_START_TRANSFER: - u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level); + u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level); u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL); break; - case U8X8_MSG_BYTE_END_TRANSFER: + case U8X8_MSG_BYTE_END_TRANSFER: memset( tx, 0, ARRAY_SIZE(tx)*sizeof(uint8_t) ); memset( rx, 0, ARRAY_SIZE(rx)*sizeof(uint8_t) ); - + for (i = 0; i < buf_idx; ++i) { tx[i] = buffer_tx[i]; } - - u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL); + + 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); - buf_idx = 0; - + buf_idx = 0; break; - + default: return 0; - } + } return 1; } +#endif /* U8G2_USE_HW_SPI */ diff --git a/port/u8g2_port.h b/port/u8g2_port.h index b6b0c53f..c82bf5df 100644 --- a/port/u8g2_port.h +++ b/port/u8g2_port.h @@ -8,12 +8,30 @@ #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) -#define I2C_DEVICE_NAME "i2c2" -#define SPI_BUS_NAME "spi1" -#define SPI_SSD1306_DEVICE_NAME "spi10" +#if defined U8G2_USE_HW_SPI -uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); -uint8_t u8x8_rt_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); -uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); + #ifndef U8G2_SPI_BUS_NAME + #define U8G2_SPI_BUS_NAME "spi1" + #endif + + #ifndef U8G2_SPI_DEVICE_NAME + #define U8G2_SPI_DEVICE_NAME "spi10" + #endif + + uint8_t u8x8_byte_rt_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); #endif + +#if defined U8G2_USE_HW_I2C + + #ifndef U8G2_I2C_DEVICE_NAME + #define U8G2_I2C_DEVICE_NAME "i2c2" + #endif + + uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); + +#endif + +uint8_t u8x8_rt_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); + +#endif /* __U8G_PORT_H__ */