mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
238 lines
6.7 KiB
Diff
238 lines
6.7 KiB
Diff
From 4f00ec2e28975b6fddbf82e5bfe9a86647aa6c58 Mon Sep 17 00:00:00 2001
|
|
From: Bas Stottelaar <basstottelaar@gmail.com>
|
|
Date: Sat, 11 Jun 2016 15:54:41 +0200
|
|
Subject: [PATCH 2/2] u8g2: add riot-os interface.
|
|
|
|
---
|
|
csrc/u8g2_riotos.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
csrc/u8x8.h | 17 +++++-
|
|
2 files changed, 175 insertions(+), 3 deletions(-)
|
|
create mode 100644 csrc/u8g2_riotos.c
|
|
|
|
diff --git a/csrc/u8g2_riotos.c b/csrc/u8g2_riotos.c
|
|
new file mode 100644
|
|
index 0000000..cf171f9
|
|
--- /dev/null
|
|
+++ b/csrc/u8g2_riotos.c
|
|
@@ -0,0 +1,161 @@
|
|
+#include "u8g2.h"
|
|
+
|
|
+#include "xtimer.h"
|
|
+
|
|
+#include "periph/spi.h"
|
|
+#include "periph/i2c.h"
|
|
+#include "periph/gpio.h"
|
|
+
|
|
+#include <stdio.h>
|
|
+
|
|
+#if SPI_NUMOF
|
|
+static spi_speed_t u8x8_pulse_width_to_spi_speed(uint32_t pulse_width)
|
|
+{
|
|
+ uint32_t cycle_time = 2 * pulse_width;
|
|
+
|
|
+ if (cycle_time < 100) {
|
|
+ return SPI_SPEED_10MHZ;
|
|
+ } else if (cycle_time < 200) {
|
|
+ return SPI_SPEED_5MHZ;
|
|
+ } else if (cycle_time < 1000) {
|
|
+ return SPI_SPEED_1MHZ;
|
|
+ } else if (cycle_time < 2500) {
|
|
+ return SPI_SPEED_400KHZ;
|
|
+ }
|
|
+
|
|
+ return SPI_SPEED_100KHZ;
|
|
+}
|
|
+#endif /* SPI_NUMOF */
|
|
+
|
|
+#if SPI_NUMOF
|
|
+static spi_speed_t u8x8_takeover_edge_to_spi_conf(uint32_t takeover_edge)
|
|
+{
|
|
+ if (takeover_edge == 2) {
|
|
+ return SPI_CONF_SECOND_RISING;
|
|
+ }
|
|
+
|
|
+ return SPI_CONF_FIRST_FALLING;
|
|
+}
|
|
+#endif /* SPI_NUMOF */
|
|
+
|
|
+static void u8x8_enable_pins(gpio_t* pins, uint32_t pins_enabled)
|
|
+{
|
|
+ uint8_t i;
|
|
+
|
|
+ for (i = 0; i < 32; i++) {
|
|
+ if (pins_enabled & (1 << i)) {
|
|
+ if (pins[i] != GPIO_UNDEF) {
|
|
+ if (i < U8X8_PIN_OUTPUT_CNT) {
|
|
+ gpio_init(pins[i], GPIO_OUT);
|
|
+ } else {
|
|
+ gpio_init(pins[i], GPIO_IN);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+}
|
|
+
|
|
+uint8_t u8x8_gpio_and_delay_riotos(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
|
+{
|
|
+ (void) arg_ptr;
|
|
+
|
|
+ switch (msg) {
|
|
+ case U8X8_MSG_GPIO_AND_DELAY_INIT:
|
|
+ u8x8_enable_pins(u8g2->pins, u8g2->pins_enabled);
|
|
+ break;
|
|
+ case U8X8_MSG_DELAY_MILLI:
|
|
+ xtimer_usleep(arg_int * 1000);
|
|
+ break;
|
|
+ case U8X8_MSG_DELAY_10MICRO:
|
|
+ xtimer_usleep(10);
|
|
+ break;
|
|
+ case U8X8_MSG_DELAY_100NANO:
|
|
+ xtimer_nanosleep(100);
|
|
+ break;
|
|
+ case U8X8_MSG_GPIO_CS:
|
|
+ gpio_write(u8g2->pins[U8X8_PIN_CS], arg_int);
|
|
+ break;
|
|
+ case U8X8_MSG_GPIO_DC:
|
|
+ gpio_write(u8g2->pins[U8X8_PIN_DC], arg_int);
|
|
+ break;
|
|
+ case U8X8_MSG_GPIO_RESET:
|
|
+ gpio_write(u8g2->pins[U8X8_PIN_RESET], arg_int);
|
|
+ break;
|
|
+ default:
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ return 1;
|
|
+}
|
|
+
|
|
+#if SPI_NUMOF
|
|
+uint8_t u8x8_byte_riotos_hw_spi(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
|
+{
|
|
+ spi_t dev = (spi_t) u8g2->dev;
|
|
+
|
|
+ switch (msg) {
|
|
+ case U8X8_MSG_BYTE_SEND:
|
|
+ spi_transfer_bytes(dev, (char *) arg_ptr, NULL, arg_int);
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_INIT:
|
|
+ spi_init_master(dev,
|
|
+ u8x8_takeover_edge_to_spi_conf(u8g2->display_info->sck_takeover_edge),
|
|
+ u8x8_pulse_width_to_spi_speed(u8g2->display_info->sck_pulse_width_ns));
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_SET_DC:
|
|
+ u8x8_gpio_SetDC(u8g2, arg_int);
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_START_TRANSFER:
|
|
+ spi_acquire(dev);
|
|
+
|
|
+ u8x8_gpio_SetCS(u8g2, u8g2->display_info->chip_enable_level);
|
|
+ u8g2->gpio_and_delay_cb(u8g2, U8X8_MSG_DELAY_NANO, u8g2->display_info->post_chip_enable_wait_ns, NULL);
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_END_TRANSFER:
|
|
+ u8g2->gpio_and_delay_cb(u8g2, U8X8_MSG_DELAY_NANO, u8g2->display_info->pre_chip_disable_wait_ns, NULL);
|
|
+ u8x8_gpio_SetCS(u8g2, u8g2->display_info->chip_disable_level);
|
|
+
|
|
+ spi_release(dev);
|
|
+ break;
|
|
+ default:
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ return 1;
|
|
+}
|
|
+#endif /* SPI_NUMOF */
|
|
+
|
|
+#if I2C_NUMOF
|
|
+uint8_t u8x8_byte_riotos_hw_i2c(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
|
+{
|
|
+ static uint8_t buffer[255];
|
|
+ static uint8_t index;
|
|
+
|
|
+ i2c_t dev = (i2c_t) u8g2->dev;
|
|
+
|
|
+ switch (msg) {
|
|
+ case U8X8_MSG_BYTE_SEND:
|
|
+ while (arg_int--) {
|
|
+ buffer[index++] = *((uint8_t *)arg_ptr++);
|
|
+ }
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_INIT:
|
|
+ i2c_init_master(dev, I2C_SPEED_FAST);
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_SET_DC:
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_START_TRANSFER:
|
|
+ i2c_acquire(dev);
|
|
+ index = 0;
|
|
+ break;
|
|
+ case U8X8_MSG_BYTE_END_TRANSFER:
|
|
+ i2c_write_bytes(dev, u8x8_GetI2CAddress(u8g2), (char *) buffer, index);
|
|
+ i2c_release(dev);
|
|
+ break;
|
|
+ default:
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ return 1;
|
|
+}
|
|
+#endif /* I2C_NUMOF */
|
|
diff --git a/csrc/u8x8.h b/csrc/u8x8.h
|
|
index 1a8fff9..d3c4022 100644
|
|
--- a/csrc/u8x8.h
|
|
+++ b/csrc/u8x8.h
|
|
@@ -107,6 +107,8 @@
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
+#include "periph/gpio.h"
|
|
+
|
|
#if defined(__GNUC__) && defined(__AVR__)
|
|
#include <avr/pgmspace.h>
|
|
#endif
|
|
@@ -154,9 +156,9 @@ extern "C" {
|
|
# define u8x8_pgm_read(adr) (*(const uint8_t *)(adr))
|
|
#endif
|
|
|
|
-#ifdef ARDUINO
|
|
-#define U8X8_USE_PINS
|
|
-#endif
|
|
+//#ifdef ARDUINO
|
|
+//#define U8X8_USE_PINS
|
|
+//#endif
|
|
|
|
/*==========================================*/
|
|
/* U8X8 typedefs and data structures */
|
|
@@ -308,6 +310,10 @@ struct u8x8_struct
|
|
#ifdef U8X8_USE_PINS
|
|
uint8_t pins[U8X8_PIN_CNT]; /* defines a pinlist: Mainly a list of pins for the Arduino Envionment, use U8X8_PIN_xxx to access */
|
|
#endif
|
|
+
|
|
+ gpio_t* pins;
|
|
+ uint32_t pins_enabled;
|
|
+ uint32_t dev;
|
|
};
|
|
|
|
#define u8x8_GetCols(u8x8) ((u8x8)->display_info->tile_width)
|
|
@@ -325,6 +331,8 @@ struct u8x8_struct
|
|
#define u8x8_SetMenuHomePin(u8x8, val) u8x8_SetPin((u8x8),U8X8_PIN_MENU_HOME,(val))
|
|
#endif
|
|
|
|
+#define u8x8_SetPins(u8x8,pins,pins_enabled) {(u8x8)->pins = (pins); (u8x8)->pins_enabled = (pins_enabled);}
|
|
+#define u8x8_SetDevice(u8x8,device) ((u8x8)->dev = device)
|
|
|
|
/*==========================================*/
|
|
|
|
@@ -777,6 +785,9 @@ extern const uint8_t u8x8_font_pcsenior_u[] U8X8_FONT_SECTION("u8x8_font_pcsenio
|
|
|
|
/* end font list */
|
|
|
|
+extern uint8_t u8x8_byte_riotos_hw_spi(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
|
+extern uint8_t u8x8_gpio_and_delay_riotos(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
|
+extern uint8_t u8x8_byte_riotos_hw_i2c(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
--
|
|
2.8.1
|
|
|