diff options
Diffstat (limited to 'drivers')
33 files changed, 889 insertions, 182 deletions
diff --git a/drivers/lcd/hd44780.h b/drivers/lcd/hd44780.h index 9e43339344..402217a547 100644 --- a/drivers/lcd/hd44780.h +++ b/drivers/lcd/hd44780.h @@ -21,9 +21,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> /** - * \defgroup hd44780 + * \file * - * HD44780 Character LCD Driver + * \defgroup hd44780 HD44780 Character LCD Driver * \{ */ diff --git a/drivers/led/apa102.c b/drivers/led/apa102.c index f291948975..40fc68e4f1 100644 --- a/drivers/led/apa102.c +++ b/drivers/led/apa102.c @@ -27,7 +27,7 @@ # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX) || defined(GD32VF103) # define APA102_NOPS (100 / (1000000000L / (CPU_CLOCK / 4))) // This calculates how many loops of 4 nops to run to delay 100 ns # else -# error("APA102_NOPS configuration required") +# error APA102_NOPS configuration required # define APA102_NOPS 0 // this just pleases the compile so the above error is easier to spot # endif # endif @@ -43,14 +43,14 @@ } \ } while (0) -#define APA102_SEND_BIT(byte, bit) \ - do { \ - writePin(RGB_DI_PIN, (byte >> bit) & 1); \ - io_wait; \ - writePinHigh(RGB_CI_PIN); \ - io_wait; \ - writePinLow(RGB_CI_PIN); \ - io_wait; \ +#define APA102_SEND_BIT(byte, bit) \ + do { \ + writePin(APA102_DI_PIN, (byte >> bit) & 1); \ + io_wait; \ + writePinHigh(APA102_CI_PIN); \ + io_wait; \ + writePinLow(APA102_CI_PIN); \ + io_wait; \ } while (0) uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS; @@ -77,11 +77,11 @@ void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds) { } void static apa102_init(void) { - setPinOutput(RGB_DI_PIN); - setPinOutput(RGB_CI_PIN); + setPinOutput(APA102_DI_PIN); + setPinOutput(APA102_CI_PIN); - writePinLow(RGB_DI_PIN); - writePinLow(RGB_CI_PIN); + writePinLow(APA102_DI_PIN); + writePinLow(APA102_CI_PIN); } void apa102_set_brightness(uint8_t brightness) { diff --git a/drivers/led/aw20216.c b/drivers/led/aw20216.c index cbb0b60774..7895f1497b 100644 --- a/drivers/led/aw20216.c +++ b/drivers/led/aw20216.c @@ -1,4 +1,5 @@ /* Copyright 2021 Jasper Chan + * 2023 Huckies <https://github.com/Huckies> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,6 +16,7 @@ */ #include "aw20216.h" +#include "wait.h" #include "spi_master.h" /* The AW20216 appears to be somewhat similar to the IS31FL743, although quite @@ -34,6 +36,8 @@ #define AW_REG_CONFIGURATION 0x00 // PG0 #define AW_REG_GLOBALCURRENT 0x01 // PG0 +#define AW_REG_RESET 0x2F // PG0 +#define AW_REG_MIXFUNCTION 0x46 // PG0 // Default value of AW_REG_CONFIGURATION // D7:D4 = 1011, SWSEL (SW1~SW12 active) @@ -41,7 +45,10 @@ // D2:D1 = 00, OSDE (open/short detection enable) // D0 = 0, CHIPEN (write 1 to enable LEDs when hardware enable pulled high) #define AW_CONFIG_DEFAULT 0b10110000 +#define AW_MIXCR_DEFAULT 0b00000000 +#define AW_RESET_CMD 0xAE #define AW_CHIPEN 1 +#define AW_LPEN (0x01 << 1) #define AW_PWM_REGISTER_COUNT 216 @@ -94,6 +101,10 @@ static inline bool AW20216_write_register(pin_t cs_pin, uint8_t page, uint8_t re return AW20216_write(cs_pin, page, reg, &value, 1); } +void AW20216_soft_reset(pin_t cs_pin) { + AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_RESET, AW_RESET_CMD); +} + static void AW20216_init_scaling(pin_t cs_pin) { // Set constant current to the max, control brightness with PWM for (uint8_t i = 0; i < AW_PWM_REGISTER_COUNT; i++) { @@ -111,15 +122,23 @@ static inline void AW20216_soft_enable(pin_t cs_pin) { AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); } +static inline void AW20216_auto_lowpower(pin_t cs_pin) { + AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_MIXFUNCTION, AW_MIXCR_DEFAULT | AW_LPEN); +} + void AW20216_init(pin_t cs_pin, pin_t en_pin) { setPinOutput(en_pin); writePinHigh(en_pin); + AW20216_soft_reset(cs_pin); + wait_ms(2); + // Drivers should start with all scaling and PWM registers as off AW20216_init_current_limit(cs_pin); AW20216_init_scaling(cs_pin); AW20216_soft_enable(cs_pin); + AW20216_auto_lowpower(cs_pin); } void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { diff --git a/drivers/led/issi/is31fl3218.c b/drivers/led/issi/is31fl3218.c index d43863ac4b..c2300ebe89 100644 --- a/drivers/led/issi/is31fl3218.c +++ b/drivers/led/issi/is31fl3218.c @@ -45,9 +45,7 @@ void IS31FL3218_write_register(uint8_t reg, uint8_t data) { void IS31FL3218_write_pwm_buffer(uint8_t *pwm_buffer) { g_twi_transfer_buffer[0] = ISSI_REG_PWM; - for (int i = 0; i < 18; i++) { - g_twi_transfer_buffer[1 + i] = pwm_buffer[i]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18); i2c_transmit(ISSI_ADDRESS, g_twi_transfer_buffer, 19, ISSI_TIMEOUT); } diff --git a/drivers/led/issi/is31fl3218.h b/drivers/led/issi/is31fl3218.h index fa760da191..26bb01a014 100644 --- a/drivers/led/issi/is31fl3218.h +++ b/drivers/led/issi/is31fl3218.h @@ -18,6 +18,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> void IS31FL3218_init(void); void IS31FL3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3731-simple.c b/drivers/led/issi/is31fl3731-simple.c index 84060f9426..a62b21cc6b 100644 --- a/drivers/led/issi/is31fl3731-simple.c +++ b/drivers/led/issi/is31fl3731-simple.c @@ -123,9 +123,7 @@ void IS31FL3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer - for (int j = 0; j < 16; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { diff --git a/drivers/led/issi/is31fl3731-simple.h b/drivers/led/issi/is31fl3731-simple.h index 1ddadd5209..7834766b94 100644 --- a/drivers/led/issi/is31fl3731-simple.h +++ b/drivers/led/issi/is31fl3731-simple.h @@ -20,6 +20,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> #include "progmem.h" typedef struct is31_led { diff --git a/drivers/led/issi/is31fl3731.c b/drivers/led/issi/is31fl3731.c index fed5354145..80344ca721 100644 --- a/drivers/led/issi/is31fl3731.c +++ b/drivers/led/issi/is31fl3731.c @@ -111,9 +111,7 @@ void IS31FL3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer - for (int j = 0; j < 16; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { diff --git a/drivers/led/issi/is31fl3731.h b/drivers/led/issi/is31fl3731.h index 6791289c9e..4c79cb8146 100644 --- a/drivers/led/issi/is31fl3731.h +++ b/drivers/led/issi/is31fl3731.h @@ -19,6 +19,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> #include "progmem.h" typedef struct is31_led { diff --git a/drivers/led/issi/is31fl3733-simple.c b/drivers/led/issi/is31fl3733-simple.c index 1e0994d780..21138c6e05 100644 --- a/drivers/led/issi/is31fl3733-simple.c +++ b/drivers/led/issi/is31fl3733-simple.c @@ -129,9 +129,7 @@ bool IS31FL3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // Copy the data from i to i+15. // Device will auto-increment register for data after the first byte // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. - for (int j = 0; j < 16; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { diff --git a/drivers/led/issi/is31fl3733-simple.h b/drivers/led/issi/is31fl3733-simple.h index f0ea3adca0..1571fdd3d5 100644 --- a/drivers/led/issi/is31fl3733-simple.h +++ b/drivers/led/issi/is31fl3733-simple.h @@ -22,6 +22,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> #include "progmem.h" typedef struct is31_led { diff --git a/drivers/led/issi/is31fl3736.c b/drivers/led/issi/is31fl3736.c index 82e7ee3d18..d6b0881139 100644 --- a/drivers/led/issi/is31fl3736.c +++ b/drivers/led/issi/is31fl3736.c @@ -77,7 +77,7 @@ uint8_t g_twi_transfer_buffer[20]; // buffers and the transfers in IS31FL3736_write_pwm_buffer() but it's // probably not worth the extra complexity. uint8_t g_pwm_buffer[DRIVER_COUNT][192]; -bool g_pwm_buffer_update_required = false; +bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; uint8_t g_led_control_registers[DRIVER_COUNT][24] = {{0}, {0}}; bool g_led_control_registers_update_required = false; @@ -107,9 +107,7 @@ void IS31FL3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - for (int j = 0; j < 16; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { @@ -171,10 +169,10 @@ void IS31FL3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < RGB_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_is31_leds[index]), sizeof(led)); - g_pwm_buffer[led.driver][led.r] = red; - g_pwm_buffer[led.driver][led.g] = green; - g_pwm_buffer[led.driver][led.b] = blue; - g_pwm_buffer_update_required = true; + g_pwm_buffer[led.driver][led.r] = red; + g_pwm_buffer[led.driver][led.g] = green; + g_pwm_buffer[led.driver][led.b] = blue; + g_pwm_buffer_update_required[led.driver] = true; } } @@ -232,9 +230,9 @@ void IS31FL3736_mono_set_brightness(int index, uint8_t value) { if (index >= 0 && index < 96) { // Index in range 0..95 -> A1..A8, B1..B8, etc. // Map index 0..95 to registers 0x00..0xBE (interleaved) - uint8_t pwm_register = index * 2; - g_pwm_buffer[0][pwm_register] = value; - g_pwm_buffer_update_required = true; + uint8_t pwm_register = index * 2; + g_pwm_buffer[0][pwm_register] = value; + g_pwm_buffer_update_required[0] = true; } } @@ -262,16 +260,15 @@ void IS31FL3736_mono_set_led_control_register(uint8_t index, bool enabled) { g_led_control_registers_update_required = true; } -void IS31FL3736_update_pwm_buffers(uint8_t addr1, uint8_t addr2) { - if (g_pwm_buffer_update_required) { +void IS31FL3736_update_pwm_buffers(uint8_t addr, uint8_t index) { + if (g_pwm_buffer_update_required[index]) { // Firstly we need to unlock the command register and select PG1 - IS31FL3736_write_register(addr1, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5); - IS31FL3736_write_register(addr1, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM); + IS31FL3736_write_register(addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5); + IS31FL3736_write_register(addr, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM); - IS31FL3736_write_pwm_buffer(addr1, g_pwm_buffer[0]); - // IS31FL3736_write_pwm_buffer(addr2, g_pwm_buffer[1]); + IS31FL3736_write_pwm_buffer(addr, g_pwm_buffer[index]); } - g_pwm_buffer_update_required = false; + g_pwm_buffer_update_required[index] = false; } void IS31FL3736_update_led_control_registers(uint8_t addr1, uint8_t addr2) { diff --git a/drivers/led/issi/is31fl3736.h b/drivers/led/issi/is31fl3736.h index ccb19afbcc..332b2035f3 100644 --- a/drivers/led/issi/is31fl3736.h +++ b/drivers/led/issi/is31fl3736.h @@ -19,6 +19,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> #include "progmem.h" // Simple interface option. @@ -58,8 +59,8 @@ void IS31FL3736_mono_set_led_control_register(uint8_t index, bool enabled); // (eg. from a timer interrupt). // Call this while idle (in between matrix scans). // If the buffer is dirty, it will update the driver with the buffer. -void IS31FL3736_update_pwm_buffers(uint8_t addr1, uint8_t addr2); -void IS31FL3736_update_led_control_registers(uint8_t addr1, uint8_t addr2); +void IS31FL3736_update_pwm_buffers(uint8_t addr, uint8_t index); +void IS31FL3736_update_led_control_registers(uint8_t addr, uint8_t index); #define PUR_0R 0x00 // No PUR resistor #define PUR_05KR 0x01 // 0.5k Ohm resistor diff --git a/drivers/led/issi/is31fl3737.c b/drivers/led/issi/is31fl3737.c index 45a20018c5..b6ed6b2629 100644 --- a/drivers/led/issi/is31fl3737.c +++ b/drivers/led/issi/is31fl3737.c @@ -114,9 +114,7 @@ void IS31FL3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - for (int j = 0; j < 16; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; - } + memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { diff --git a/drivers/led/issi/is31fl3737.h b/drivers/led/issi/is31fl3737.h index fb0c33420c..ca9a917ee2 100644 --- a/drivers/led/issi/is31fl3737.h +++ b/drivers/led/issi/is31fl3737.h @@ -21,6 +21,7 @@ #include <stdint.h> #include <stdbool.h> +#include <string.h> #include "progmem.h" typedef struct is31_led { @@ -45,8 +46,8 @@ void IS31FL3737_set_led_control_register(uint8_t index, bool red, bool green, bo // (eg. from a timer interrupt). // Call this while idle (in between matrix scans). // If the buffer is dirty, it will update the driver with the buffer. -void IS31FL3737_update_pwm_buffers(uint8_t addr1, uint8_t addr2); -void IS31FL3737_update_led_control_registers(uint8_t addr1, uint8_t addr2); +void IS31FL3737_update_pwm_buffers(uint8_t addr, uint8_t index); +void IS31FL3737_update_led_control_registers(uint8_t addr, uint8_t index); #define PUR_0R 0x00 // No PUR resistor #define PUR_05KR 0x01 // 0.5k Ohm resistor in t_NOL diff --git a/drivers/led/issi/is31fl3741.c b/drivers/led/issi/is31fl3741.c index c2cdd4c46f..d5dfaf1de6 100644 --- a/drivers/led/issi/is31fl3741.c +++ b/drivers/led/issi/is31fl3741.c @@ -104,9 +104,7 @@ void IS31FL3741_write_register(uint8_t addr, uint8_t reg, uint8_t data) { } bool IS31FL3741_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // unlock the command register and select PG2 - IS31FL3741_write_register(addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5); - IS31FL3741_write_register(addr, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM0); + // Assume PG1 is already selected for (int i = 0; i < 342; i += 18) { if (i == 180) { @@ -222,6 +220,10 @@ void IS31FL3741_set_led_control_register(uint8_t index, bool red, bool green, bo void IS31FL3741_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { + // unlock the command register and select PG2 + IS31FL3741_write_register(addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5); + IS31FL3741_write_register(addr, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM0); + IS31FL3741_write_pwm_buffer(addr, g_pwm_buffer[index]); } diff --git a/drivers/oled/ssd1306_sh1106.c b/drivers/oled/oled_driver.c index 342920572e..8ff6e0426c 100644 --- a/drivers/oled/ssd1306_sh1106.c +++ b/drivers/oled/oled_driver.c @@ -14,20 +14,23 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "i2c_master.h" + +#if defined(OLED_TRANSPORT_SPI) +# include "spi_master.h" +#elif defined(OLED_TRANSPORT_I2C) +# include "i2c_master.h" +#endif #include "oled_driver.h" #include OLED_FONT_H #include "timer.h" #include "print.h" - #include <string.h> - #include "progmem.h" - -#include "keyboard.h" +#include "wait.h" // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf // for SH1106: https://www.velleman.eu/downloads/29/infosheets/sh1106_datasheet.pdf +// for SH1107: https://www.displayfuture.com/Display/datasheet/controller/SH1107.pdf // Fundamental Commands #define CONTRAST 0x81 @@ -82,6 +85,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. // Charge Pump Commands #define CHARGE_PUMP 0x8D +// Commands specific to the SH1107 chip +#define SH1107_DISPLAY_START_LINE 0xDC +#define SH1107_MEMORY_MODE_PAGE 0x20 +#define SH1107_MEMORY_MODE_VERTICAL 0x21 + // Misc defines #ifndef OLED_BLOCK_COUNT # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) @@ -89,19 +97,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef OLED_BLOCK_SIZE # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) #endif +// Default display clock +#if !defined(OLED_DISPLAY_CLOCK) +# define OLED_DISPLAY_CLOCK 0x80 +#endif +// Default VCOMH deselect value +#if !defined(OLED_VCOM_DETECT) +# define OLED_VCOM_DETECT 0x20 +#endif +#if !defined(OLED_PRE_CHARGE_PERIOD) +# define OLED_PRE_CHARGE_PERIOD 0xF1 +#endif #define OLED_ALL_BLOCKS_MASK (((((OLED_BLOCK_TYPE)1 << (OLED_BLOCK_COUNT - 1)) - 1) << 1) | 1) +#define OLED_IC_HAS_HORIZONTAL_MODE (OLED_IC == OLED_IC_SSD1306) +#define OLED_IC_COM_PINS_ARE_COLUMNS (OLED_IC == OLED_IC_SH1107) + +#ifndef OLED_COM_PIN_COUNT +# if OLED_IC == OLED_IC_SSD1306 +# define OLED_COM_PIN_COUNT 64 +# elif OLED_IC == OLED_IC_SH1106 +# define OLED_COM_PIN_COUNT 64 +# elif OLED_IC == OLED_IC_SH1107 +# define OLED_COM_PIN_COUNT 128 +# else +# error Invalid OLED_IC value +# endif +#endif + +#ifndef OLED_COM_PIN_OFFSET +# define OLED_COM_PIN_OFFSET 0 +#endif + // i2c defines #define I2C_CMD 0x00 #define I2C_DATA 0x40 -#if defined(__AVR__) -# define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) -#else // defined(__AVR__) -# define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) -#endif // defined(__AVR__) -#define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) -#define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, OLED_I2C_TIMEOUT) #define HAS_FLAGS(bits, flags) ((bits & flags) == flags) @@ -132,24 +163,118 @@ uint32_t oled_scroll_timeout; uint16_t oled_update_timeout; #endif -// Internal variables to reduce math instructions +#if defined(OLED_TRANSPORT_SPI) +# ifndef OLED_DC_PIN +# error "The OLED driver in SPI needs a D/C pin defined" +# endif +# ifndef OLED_CS_PIN +# error "The OLED driver in SPI needs a CS pin defined" +# endif +# ifndef OLED_SPI_MODE +# define OLED_SPI_MODE 3 +# endif +# ifndef OLED_SPI_DIVISOR +# define OLED_SPI_DIVISOR 2 +# endif +#elif defined(OLED_TRANSPORT_I2C) +# if !defined(OLED_DISPLAY_ADDRESS) +# define OLED_DISPLAY_ADDRESS 0x3C +# endif +#endif + +// Transmit/Write Funcs. +__attribute__((weak)) bool oled_send_cmd(const uint8_t *data, uint16_t size) { +#if defined(OLED_TRANSPORT_SPI) + if (!spi_start(OLED_CS_PIN, false, OLED_SPI_MODE, OLED_SPI_DIVISOR)) { + return false; + } + // Command Mode + writePinLow(OLED_DC_PIN); + // Send the commands + if (spi_transmit(&data[1], size - 1) != SPI_STATUS_SUCCESS) { + spi_stop(); + return false; + } + spi_stop(); + return true; +#elif defined(OLED_TRANSPORT_I2C) + i2c_status_t status = i2c_transmit((OLED_DISPLAY_ADDRESS << 1), data, size, OLED_I2C_TIMEOUT); + return (status == I2C_STATUS_SUCCESS); +#endif +} + +__attribute__((weak)) bool oled_send_cmd_P(const uint8_t *data, uint16_t size) { #if defined(__AVR__) -// identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently -// probably should move this into i2c_master... -static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout) { - i2c_status_t status = i2c_start(address | I2C_WRITE, timeout); +# if defined(OLED_TRANSPORT_SPI) + if (!spi_start(OLED_CS_PIN, false, OLED_SPI_MODE, OLED_SPI_DIVISOR)) { + return false; + } + spi_status_t status = SPI_STATUS_SUCCESS; + // Command Mode + writePinLow(OLED_DC_PIN); + // Send the commands + for (uint16_t i = 1; i < size && status >= 0; i++) { + status = spi_write(pgm_read_byte((const char *)&data[i])); + } + spi_stop(); + return (status >= 0); +# elif defined(OLED_TRANSPORT_I2C) + i2c_status_t status = i2c_start((OLED_DISPLAY_ADDRESS << 1) | I2C_WRITE, OLED_I2C_TIMEOUT); - for (uint16_t i = 0; i < length && status >= 0; i++) { - status = i2c_write(pgm_read_byte((const char *)data++), timeout); - if (status) break; + for (uint16_t i = 0; i < size && status >= 0; i++) { + status = i2c_write(pgm_read_byte((const char *)data++), OLED_I2C_TIMEOUT); } i2c_stop(); - return status; + return (status == I2C_STATUS_SUCCESS); +# endif +#else + return oled_send_cmd(data, size); +#endif } + +__attribute__((weak)) bool oled_send_data(const uint8_t *data, uint16_t size) { +#if defined(OLED_TRANSPORT_SPI) + if (!spi_start(OLED_CS_PIN, false, OLED_SPI_MODE, OLED_SPI_DIVISOR)) { + return false; + } + // Data Mode + writePinHigh(OLED_DC_PIN); + // Send the commands + if (spi_transmit(data, size) != SPI_STATUS_SUCCESS) { + spi_stop(); + return false; + } + spi_stop(); + return true; +#elif defined(OLED_TRANSPORT_I2C) + i2c_status_t status = i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), I2C_DATA, data, size, OLED_I2C_TIMEOUT); + return (status == I2C_STATUS_SUCCESS); #endif +} + +__attribute__((weak)) void oled_driver_init(void) { +#if defined(OLED_TRANSPORT_SPI) + spi_init(); + setPinOutput(OLED_CS_PIN); + writePinHigh(OLED_CS_PIN); + + setPinOutput(OLED_DC_PIN); + writePinLow(OLED_DC_PIN); +# ifdef OLED_RST_PIN + /* Reset device */ + setPinOutput(OLED_RST_PIN); + writePinLow(OLED_RST_PIN); + wait_ms(20); + writePinHigh(OLED_RST_PIN); + wait_ms(20); +# endif +#elif defined(OLED_TRANSPORT_I2C) + i2c_init(); +#endif +} // Flips the rendering bits for a character at the current cursor position static void InvertCharacter(uint8_t *cursor) { @@ -161,7 +286,7 @@ static void InvertCharacter(uint8_t *cursor) { } bool oled_init(oled_rotation_t rotation) { -#if defined(USE_I2C) && defined(SPLIT_KEYBOARD) +#if defined(USE_I2C) && defined(SPLIT_KEYBOARD) && defined(OLED_TRANSPORT_I2C) if (!is_keyboard_master()) { return true; } @@ -173,47 +298,61 @@ bool oled_init(oled_rotation_t rotation) { } else { oled_rotation_width = OLED_DISPLAY_HEIGHT; } - i2c_init(); + oled_driver_init(); static const uint8_t PROGMEM display_setup1[] = { I2C_CMD, DISPLAY_OFF, DISPLAY_CLOCK, - 0x80, + OLED_DISPLAY_CLOCK, MULTIPLEX_RATIO, +#if OLED_IC_COM_PINS_ARE_COLUMNS + OLED_DISPLAY_WIDTH - 1, +#else OLED_DISPLAY_HEIGHT - 1, - DISPLAY_OFFSET, +#endif +#if OLED_IC == OLED_IC_SH1107 + SH1107_DISPLAY_START_LINE, 0x00, +#else DISPLAY_START_LINE | 0x00, +#endif CHARGE_PUMP, 0x14, -#if (OLED_IC != OLED_IC_SH1106) +#if OLED_IC_HAS_HORIZONTAL_MODE // MEMORY_MODE is unsupported on SH1106 (Page Addressing only) MEMORY_MODE, 0x00, // Horizontal addressing mode +#elif OLED_IC == OLED_IC_SH1107 + // Page addressing mode + SH1107_MEMORY_MODE_PAGE, #endif }; - if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) { + if (!oled_send_cmd_P(display_setup1, ARRAY_SIZE(display_setup1))) { print("oled_init cmd set 1 failed\n"); return false; } if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) { - static const uint8_t PROGMEM display_normal[] = {I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC}; - if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) { + static const uint8_t PROGMEM display_normal[] = { + I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC, DISPLAY_OFFSET, OLED_COM_PIN_OFFSET, + }; + if (!oled_send_cmd_P(display_normal, ARRAY_SIZE(display_normal))) { print("oled_init cmd normal rotation failed\n"); return false; } } else { - static const uint8_t PROGMEM display_flipped[] = {I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC}; - if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) { + static const uint8_t PROGMEM display_flipped[] = { + I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC, DISPLAY_OFFSET, (OLED_COM_PIN_COUNT - |