summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common_features.mk8
-rw-r--r--docs/_summary.md1
-rw-r--r--docs/feature_st7565.md270
-rw-r--r--drivers/lcd/st7565.c479
-rw-r--r--drivers/lcd/st7565.h215
-rw-r--r--quantum/quantum.h4
-rw-r--r--tmk_core/common/keyboard.c18
7 files changed, 995 insertions, 0 deletions
diff --git a/common_features.mk b/common_features.mk
index 37ce928e2a..b259af46c0 100644
--- a/common_features.mk
+++ b/common_features.mk
@@ -587,6 +587,14 @@ ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes)
SRC += oled_driver.c
endif
+ifeq ($(strip $(ST7565_ENABLE)), yes)
+ OPT_DEFS += -DST7565_ENABLE
+ COMMON_VPATH += $(DRIVER_PATH)/oled # For glcdfont.h
+ COMMON_VPATH += $(DRIVER_PATH)/lcd
+ QUANTUM_LIB_SRC += spi_master.c
+ SRC += st7565.c
+endif
+
include $(DRIVER_PATH)/qwiic/qwiic.mk
ifeq ($(strip $(UCIS_ENABLE)), yes)
diff --git a/docs/_summary.md b/docs/_summary.md
index 9798ef5127..4141e01e77 100644
--- a/docs/_summary.md
+++ b/docs/_summary.md
@@ -93,6 +93,7 @@
* Hardware Features
* Displays
* [HD44780 LCD Controller](feature_hd44780.md)
+ * [ST7565 LCD Driver](feature_st7565.md)
* [OLED Driver](feature_oled_driver.md)
* Lighting
* [Backlight](feature_backlight.md)
diff --git a/docs/feature_st7565.md b/docs/feature_st7565.md
new file mode 100644
index 0000000000..7db0f9ac4a
--- /dev/null
+++ b/docs/feature_st7565.md
@@ -0,0 +1,270 @@
+# ST7565 LCD Driver
+
+## Supported Hardware
+
+LCD modules using ST7565 driver IC, communicating over SPI.
+
+|Module |IC |Size |Notes |
+|------------------------------|-------|------|----------------------------------------------------------|
+|Newhaven Display NHD-C12832A1Z|ST7565R|128x32|Used by Ergodox Infinity; primary consumer of this feature|
+|Zolentech ZLE12864B |ST7565P|128x64|Requires contrast adjustment |
+
+## Usage
+
+To enable the feature, there are three steps. First, when compiling your keyboard, you'll need to add the following to your `rules.mk`:
+
+```make
+ST7565_ENABLE = yes
+```
+
+Then in your `keymap.c` file, implement the ST7565 task call. This example assumes your keymap has three layers named `_QWERTY`, `_FN` and `_ADJ`:
+
+```c
+#ifdef ST7565_ENABLE
+void st7565_task_user(void) {
+ // Host Keyboard Layer Status
+ st7565_write_P(PSTR("Layer: "), false);
+
+ switch (get_highest_layer(layer_state)) {
+ case _QWERTY:
+ st7565_write_P(PSTR("Default\n"), false);
+ break;
+ case _FN:
+ st7565_write_P(PSTR("FN\n"), false);
+ break;
+ case _ADJ:
+ st7565_write_P(PSTR("ADJ\n"), false);
+ break;
+ default:
+ // Or use the write_ln shortcut over adding '\n' to the end of your string
+ st7565_write_ln_P(PSTR("Undefined"), false);
+ }
+
+ // Host Keyboard LED Status
+ led_t led_state = host_keyboard_led_state();
+ st7565_write_P(led_state.num_lock ? PSTR("NUM ") : PSTR(" "), false);
+ st7565_write_P(led_state.caps_lock ? PSTR("CAP ") : PSTR(" "), false);
+ st7565_write_P(led_state.scroll_lock ? PSTR("SCR ") : PSTR(" "), false);
+}
+#endif
+```
+
+## Logo Example
+
+In the default font, certain ranges of characters are reserved for a QMK logo. To render this logo to the screen, use the following code example:
+
+```c
+static void render_logo(void) {
+ static const char PROGMEM qmk_logo[] = {
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94,
+ 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4,
+ 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00
+ };
+
+ st7565_write_P(qmk_logo, false);
+}
+```
+
+## Buffer Read Example
+For some purposes, you may need to read the current state of the display buffer. The `st7565_read_raw` function can be used to safely read bytes from the buffer.
+
+In this example, calling `fade_display` in the `st7565_task_user` function will slowly fade away whatever is on the screen by turning random pixels off over time.
+```c
+//Setup some mask which can be or'd with bytes to turn off pixels
+const uint8_t single_bit_masks[8] = {127, 191, 223, 239, 247, 251, 253, 254};
+
+static void fade_display(void) {
+ //Define the reader structure
+ display_buffer_reader_t reader;
+ uint8_t buff_char;
+ if (random() % 30 == 0) {
+ srand(timer_read());
+ // Fetch a pointer for the buffer byte at index 0. The return structure
+ // will have the pointer and the number of bytes remaining from this
+ // index position if we want to perform a sequential read by
+ // incrementing the buffer pointer
+ reader = st7565_read_raw(0);
+ //Loop over the remaining buffer and erase pixels as we go
+ for (uint16_t i = 0; i < reader.remaining_element_count; i++) {
+ //Get the actual byte in the buffer by dereferencing the pointer
+ buff_char = *reader.current_element;
+ if (buff_char != 0) {
+ st7565_write_raw_byte(buff_char & single_bit_masks[rand() % 8], i);
+ }
+ //increment the pointer to fetch a new byte during the next loop
+ reader.current_element++;
+ }
+ }
+}
+```
+
+## Other Examples
+
+In split keyboards, it is very common to have two displays that each render different content and are oriented or flipped differently. You can do this by switching which content to render by using the return value from `is_keyboard_master()` or `is_keyboard_left()` found in `split_util.h`, e.g:
+
+```c
+#ifdef ST7565_ENABLE
+display_rotation_t st7565_init_user(display_rotation_t rotation) {
+ if (!is_keyboard_master()) {
+ return DISPLAY_ROTATION_180; // flips the display 180 degrees if offhand
+ }
+
+ return rotation;
+}
+
+void st7565_task_user(void) {
+ if (is_keyboard_master()) {
+ render_status(); // Renders the current keyboard state (layer, lock, caps, scroll, etc)
+ } else {
+ render_logo(); // Renders a static logo
+ }
+}
+#endif
+```
+
+## Basic Configuration
+
+|Define |Default |Description |
+|------------------------|--------------|-----------------------------------------------------------------------------------------------------|
+|`ST7565_A0_PIN` |*Not defined* |(Required) The GPIO connected to the display's A0 (data/command) pin |
+|`ST7565_RST_PIN` |*Not defined* |(Required) The GPIO connected to the display's reset pin |
+|`ST7565_SS_PIN` |*Not defined* |(Required) The GPIO connected to the display's slave select pin |
+|`ST7565_SPI_CLK_DIVISOR`|`4` |The SPI clock divisor to use |
+|`ST7565_FONT_H` |`"glcdfont.c"`|The font code file to use for custom fonts |
+|`ST7565_FONT_START` |`0` |The starting character index for custom fonts |
+|`ST7565_FONT_END` |`223` |The ending character index for custom fonts |
+|`ST7565_FONT_WIDTH` |`6` |The font width |
+|`ST7565_FONT_HEIGHT` |`8` |The font height (untested) |
+|`ST7565_TIMEOUT` |`60000` |Turns off the screen after 60000ms of keyboard inactivity. Helps reduce burn-in. Set to 0 to disable.|
+|`ST7565_COLUMN_OFFSET` |`0` |Shift output to the right this many pixels. |
+|`ST7565_CONTRAST` |`32` |The default contrast level of the display, from 0 to 255. |
+|`ST7565_UPDATE_INTERVAL`|`0` |Set the time interval for updating the display in ms. This will improve the matrix scan rate. |
+
+## Custom sized displays
+
+The default display size for this feature is 128x32 and all necessary defines are precalculated with that in mind.
+
+|Define |Default |Description |
+|-----------------------|----------|-----------------------------------------------------------------------------------------------------------|
+|`ST7565_DISPLAY_WIDTH` |`128` |The width of the display. |
+|`ST7565_DISPLAY_HEIGHT`|`32` |The height of the display. |
+|`ST7565_MATRIX_SIZE` |`512` |The local buffer size to allocate.<br>`(ST7565_DISPLAY_HEIGHT / 8 * ST7565_DISPLAY_WIDTH)`. |
+|`ST7565_BLOCK_TYPE` |`uint16_t`|The unsigned integer type to use for dirty rendering. |
+|`ST7565_BLOCK_COUNT` |`16` |The number of blocks the display is divided into for dirty rendering.<br>`(sizeof(ST7565_BLOCK_TYPE) * 8)`.|
+|`ST7565_BLOCK_SIZE` |`32` |The size of each block for dirty rendering<br>`(ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)`. |
+
+## API
+
+```c
+// Rotation enum values are flags
+typedef enum {
+ DISPLAY_ROTATION_0,
+ DISPLAY_ROTATION_180
+} display_rotation_t;
+
+// Initialize the display, rotating the rendered output based on the define passed in.
+// Returns true if the was initialized successfully
+bool st7565_init(display_rotation_t rotation);
+
+// Called at the start of st7565_init, weak function overridable by the user
+// rotation - the value passed into st7565_init
+// Return new display_rotation_t if you want to override default rotation
+display_rotation_t st7565_init_user(display_rotation_t rotation);
+
+// Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
+void st7565_clear(void);
+
+// Renders the dirty chunks of the buffer to display
+void st7565_render(void);
+
+// Moves cursor to character position indicated by column and line, wraps if out of bounds
+// Max column denoted by 'st7565_max_chars()' and max lines by 'st7565_max_lines()' functions
+void st7565_set_cursor(uint8_t col, uint8_t line);
+
+// Advances the cursor to the next page, writing ' ' if true
+// Wraps to the begining when out of bounds
+void st7565_advance_page(bool clearPageRemainder);
+
+// Moves the cursor forward 1 character length
+// Advance page if there is not enough room for the next character
+// Wraps to the begining when out of bounds
+void st7565_advance_char(void);
+
+// Writes a single character to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Main handler that writes character data to the display buffer
+void st7565_write_char(const char data, bool invert);
+
+// Writes a string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+void st7565_write(const char *data, bool invert);
+
+// Writes a string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
+void st7565_write_ln(const char *data, bool invert);
+
+// Pans the buffer to the right (or left by passing true) by moving contents of the buffer
+// Useful for moving the screen in preparation for new drawing
+void st7565_pan(bool left);
+
+// Returns a pointer to the requested start index in the buffer plus remaining
+// buffer length as struct
+display_buffer_reader_t st7565_read_raw(uint16_t start_index);
+
+// Writes a string to the buffer at current cursor position
+void st7565_write_raw(const char *data, uint16_t size);
+
+// Writes a single byte into the buffer at the specified index
+void st7565_write_raw_byte(const char data, uint16_t index);
+
+// Sets a specific pixel on or off
+// Coordinates start at top-left and go right and down for positive x and y
+void st7565_write_pixel(uint8_t x, uint8_t y, bool on);
+
+// Writes a PROGMEM string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Remapped to call 'void st7565_write(const char *data, bool invert);' on ARM
+void st7565_write_P(const char *data, bool invert);
+
+// Writes a PROGMEM string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
+// Remapped to call 'void st7565_write_ln(const char *data, bool invert);' on ARM
+void st7565_write_ln_P(const char *data, bool invert);
+
+// Writes a PROGMEM string to the buffer at current cursor position
+void st7565_write_raw_P(const char *data, uint16_t size);
+
+// Can be used to manually turn on the screen if it is off
+// Returns true if the screen was on or turns on
+bool st7565_on(void);
+
+// Called when st7565_on() turns on the screen, weak function overridable by the user
+// Not called if the screen is already on
+void st7565_on_user(void);
+
+// Can be used to manually turn off the screen if it is on
+// Returns true if the screen was off or turns off
+bool st7565_off(void);
+
+// Called when st7565_off() turns off the screen, weak function overridable by the user
+// Not called if the screen is already off
+void st7565_off_user(void);
+
+// Returns true if the screen is currently on, false if it is
+// not
+bool st7565_is_on(void);
+
+// Basically it's st7565_render, but with timeout management and st7565_task_user calling!
+void st7565_task(void);
+
+// Called at the start of st7565_task, weak function overridable by the user
+void st7565_task_user(void);
+
+// Returns the maximum number of characters that will fit on a line
+uint8_t st7565_max_chars(void);
+
+// Returns the maximum number of lines that will fit on the display
+uint8_t st7565_max_lines(void);
+```
diff --git a/drivers/lcd/st7565.c b/drivers/lcd/st7565.c
new file mode 100644
index 0000000000..4b4891ce71
--- /dev/null
+++ b/drivers/lcd/st7565.c
@@ -0,0 +1,479 @@
+/*
+Copyright 2021
+
+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
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+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 "st7565.h"
+
+#include <string.h>
+
+#include "keyboard.h"
+#include "progmem.h"
+#include "timer.h"
+#include "wait.h"
+
+#include ST7565_FONT_H
+
+// Fundamental Commands
+#define CONTRAST 0x81
+#define DISPLAY_ALL_ON 0xA5
+#define DISPLAY_ALL_ON_RESUME 0xA4
+#define NORMAL_DISPLAY 0xA6
+#define DISPLAY_ON 0xAF
+#define DISPLAY_OFF 0xAE
+#define NOP 0xE3
+
+// Addressing Setting Commands
+#define PAM_SETCOLUMN_LSB 0x00
+#define PAM_SETCOLUMN_MSB 0x10
+#define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
+
+// Hardware Configuration Commands
+#define DISPLAY_START_LINE 0x40
+#define SEGMENT_REMAP 0xA0
+#define SEGMENT_REMAP_INV 0xA1
+#define COM_SCAN_INC 0xC0
+#define COM_SCAN_DEC 0xC8
+#define LCD_BIAS_7 0xA3
+#define LCD_BIAS_9 0xA2
+#define RESISTOR_RATIO 0x20
+#define POWER_CONTROL 0x28
+
+// Misc defines
+#ifndef ST7565_BLOCK_COUNT
+# define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
+#endif
+#ifndef ST7565_BLOCK_SIZE
+# define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
+#endif
+
+#define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
+
+#define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
+
+// Display buffer's is the same as the display memory layout
+// this is so we don't end up with rounding errors with
+// parts of the display unusable or don't get cleared correctly
+// and also allows for drawing & inverting
+uint8_t st7565_buffer[ST7565_MATRIX_SIZE];
+uint8_t * st7565_cursor;
+ST7565_BLOCK_TYPE st7565_dirty = 0;
+bool st7565_initialized = false;
+bool st7565_active = false;
+display_rotation_t st7565_rotation = DISPLAY_ROTATION_0;
+#if ST7565_TIMEOUT > 0
+uint32_t st7565_timeout;
+#endif
+#if ST7565_UPDATE_INTERVAL > 0
+uint16_t st7565_update_timeout;
+#endif
+
+// Flips the rendering bits for a character at the current cursor position
+static void InvertCharacter(uint8_t *cursor) {
+ const uint8_t *end = cursor + ST7565_FONT_WIDTH;
+ while (cursor < end) {
+ *cursor = ~(*cursor);
+ cursor++;
+ }
+}
+
+bool st7565_init(display_rotation_t rotation) {
+ setPinOutput(ST7565_A0_PIN);
+ writePinHigh(ST7565_A0_PIN);
+ setPinOutput(ST7565_RST_PIN);
+ writePinHigh(ST7565_RST_PIN);
+
+ st7565_rotation = st7565_init_user(rotation);
+
+ spi_init();
+ spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
+
+ st7565_reset();
+
+ st7565_send_cmd(LCD_BIAS_7);
+ if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
+ st7565_send_cmd(SEGMENT_REMAP);
+ st7565_send_cmd(COM_SCAN_DEC);
+ } else {
+ st7565_send_cmd(SEGMENT_REMAP_INV);
+ st7565_send_cmd(COM_SCAN_INC);
+ }
+ st7565_send_cmd(DISPLAY_START_LINE | 0x00);
+ st7565_send_cmd(CONTRAST);
+ st7565_send_cmd(ST7565_CONTRAST);
+ st7565_send_cmd(RESISTOR_RATIO | 0x01);
+ st7565_send_cmd(POWER_CONTROL | 0x04);
+ wait_ms(50);
+ st7565_send_cmd(POWER_CONTROL | 0x06);
+ wait_ms(50);
+ st7565_send_cmd(POWER_CONTROL | 0x07);
+ wait_ms(10);
+ st7565_send_cmd(DISPLAY_ON);
+ st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
+ st7565_send_cmd(NORMAL_DISPLAY);
+
+ spi_stop();
+
+#if ST7565_TIMEOUT > 0
+ st7565_timeout = timer_read32() + ST7565_TIMEOUT;
+#endif
+
+ st7565_clear();
+ st7565_initialized = true;
+ st7565_active = true;
+ return true;
+}
+
+__attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) { return rotation; }
+
+void st7565_clear(void) {
+ memset(st7565_buffer, 0, sizeof(st7565_buffer));
+ st7565_cursor = &st7565_buffer[0];
+ st7565_dirty = ST7565_ALL_BLOCKS_MASK;
+}
+
+uint8_t crot(uint8_t a, int8_t n) {
+ const uint8_t mask = 0x7;
+ n &= mask;
+ return a << n | a >> (-n & mask);
+}
+
+void st7565_render(void) {
+ if (!st7565_initialized) {
+ return;
+ }
+
+ // Do we have work to do?
+ st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
+ if (!st7565_dirty) {
+ return;
+ }
+
+ // Find first dirty block
+ uint8_t update_start = 0;
+ while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
+ ++update_start;
+ }
+
+ // Calculate commands to set memory addressing bounds.
+ uint8_t start_page = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
+ uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
+ // IC has 132 segment drivers, for panels with less width we need to offset the starting column
+ if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
+ start_column += (132 - ST7565_DISPLAY_WIDTH);
+ }
+
+ spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
+
+ st7565_send_cmd(PAM_PAGE_ADDR | start_page);
+ st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
+ st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
+
+ st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
+
+ // Turn on display if it is off
+ st7565_on();
+
+ // Clear dirty flag
+ st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
+}
+
+void st7565_set_cursor(uint8_t col, uint8_t line) {
+ uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
+
+ // Out of bounds?
+ if (index >= ST7565_MATRIX_SIZE) {
+ index = 0;
+ }
+
+ st7565_cursor = &st7565_buffer[index];
+}
+
+void st7565_advance_page(bool clearPageRemainder) {
+ uint16_t index = st7565_cursor - &st7565_buffer[0];
+ uint8_t remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
+
+ if (clearPageRemainder) {
+ // Remaining Char count
+ remaining = remaining / ST7565_FONT_WIDTH;
+
+ // Write empty character until next line
+ while (remaining--) st7565_write_char(' ', false);
+ } else {
+ // Next page index out of bounds?
+ if (index + remaining >= ST7565_MATRIX_SIZE) {
+ index = 0;
+ remaining = 0;
+ }
+
+ st7565_cursor = &st7565_buffer[index + remaining];
+ }
+}
+
+void st7565_advance_char(void) {
+ uint16_t nextIndex = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
+ uint8_t remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
+
+ // Do we have enough space on the current line for the next character
+ if (remainingSpace < ST7565_FONT_WIDTH) {
+ nextIndex += remainingSpace;
+ }
+
+ // Did we go out of bounds
+ if (nextIndex >= ST7565_MATRIX_SIZE) {
+ nextIndex = 0;
+ }
+
+ // Update cursor position
+ st7565_cursor = &st7565_buffer[nextIndex];
+}
+
+// Main handler that writes character data to the display buffer
+void st7565_write_char(const char data, bool invert) {
+ // Advance to the next line if newline
+ if (data == '\n') {
+ // Old source wrote ' ' until end of line...
+ st7565_advance_page(true);
+ return;
+ }
+
+ if (data == '\r') {
+ st7565_advance_page(false);
+ return;
+ }
+
+ // copy the current render buffer to check for dirty after
+ static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
+ memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
+
+ _Static_assert(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
+
+ // set the reder buffer data
+ uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
+ if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
+ memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
+ } else {
+ const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
+ memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
+ }
+
+ // Invert if needed
+ if (invert) {
+ InvertCharacter(st7565_cursor);
+ }
+
+ // Dirty check
+ if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
+ uint16_t index = st7565_cursor - &st7565_buffer[0];
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
+ // Edgecase check if the written data spans the 2 chunks
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
+ }
+
+ // Finally move to the next char
+ st7565_advance_char();
+}
+
+void st7565_write(const char *data, bool invert) {
+ const char *end = data + strlen(data);
+ while (data < end) {
+ st7565_write_char(*data, invert);
+ data++;
+ }
+}
+
+void st7565_write_ln(const char *data, bool invert) {
+ st7565_write(data, invert);
+ st7565_advance_page(true);
+}
+
+void st7565_pan(bool left) {
+ uint16_t i = 0;
+ for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
+ if (left) {
+ for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
+ i = y * ST7565_DISPLAY_WIDTH + x;
+ st7565_buffer[i] = st7565_buffer[i + 1];
+ }
+ } else {
+ for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
+ i = y * ST7565_DISPLAY_WIDTH + x;
+ st7565_buffer[i] = st7565_buffer[i - 1];
+ }
+ }
+ }
+ st7565_dirty = ST7565_ALL_BLOCKS_MASK;
+}
+
+display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
+ if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
+ display_buffer_reader_t ret_reader;
+ ret_reader.current_element = &st7565_buffer[start_index];
+ ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
+ return ret_reader;
+}
+
+void st7565_write_raw_byte(const char data, uint16_t index) {
+ if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
+ if (st7565_buffer[index] == data) return;
+ st7565_buffer[index] = data;
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
+}
+
+void st7565_write_raw(const char *data, uint16_t size) {
+ uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
+ if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
+ for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
+ if (st7565_buffer[i] == data[i]) continue;
+ st7565_buffer[i] = data[i];
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
+ }
+}
+
+void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
+ if (x >= ST7565_DISPLAY_WIDTH) {
+ return;
+ }
+ uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
+ if (index >= ST7565_MATRIX_SIZE) {
+ return;
+ }
+ uint8_t data = st7565_buffer[index];
+ if (on) {
+ data |= (1 << (y % 8));
+ } else {
+ data &= ~(1 << (y % 8));
+ }
+ if (st7565_buffer[index] != data) {
+ st7565_buffer[index] = data;
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
+ }
+}
+
+#if defined(__AVR__)
+void st7565_write_P(const char *data, bool invert) {
+ uint8_t c = pgm_read_byte(data);
+ while (c != 0) {
+ st7565_write_char(c, invert);
+ c = pgm_read_byte(++data);
+ }
+}
+
+void st7565_write_ln_P(const char *data, bool invert) {
+ st7565_write_P(data, invert);
+ st7565_advance_page(true);
+}
+
+void st7565_write_raw_P(const char *data, uint16_t size) {
+ uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
+ if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
+ for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
+ uint8_t c = pgm_read_byte(data++);
+ if (st7565_buffer[i] == c) continue;
+ st7565_buffer[i] = c;
+ st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
+ }
+}
+#endif // defined(__AVR__)
+
+bool st7565_on(void) {
+ if (!st7565_initialized) {
+ return st7565_active;
+ }
+
+#if ST7565_TIMEOUT > 0
+ st7565_timeout = timer_read32() + ST7565_TIMEOUT;
+#endif
+
+ if (!st7565_active) {
+ spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
+ st7565_send_cmd(DISPLAY_ON);
+ spi_stop();
+ st7565_active = true;
+ st7565_on_user();
+ }
+ return st7565_active;
+}
+
+__attribute__((weak)) void st7565_on_user(void) {}
+
+bool st7565_off(void) {
+ if (!st7565_initialized) {
+ return !st7565_active;
+ }
+
+ if (st7565_active) {
+ spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
+ st7565_send_cmd(DISPLAY_OFF);
+ spi_stop();
+ st7565_active = false;
+ st7565_off_user();
+ }
+ return !st7565_active;
+}
+
+__attribute__((weak)) void st7565_off_user(void) {}
+
+bool st7565_is_on(void) { return st7565_active; }
+
+uint8_t st7565_max_chars(void) { return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH; }
+
+uint8_t st7565_max_lines(void) { return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT; }
+
+void st7565_task(void) {
+ if (!st7565_initialized) {
+ return;
+ }
+
+#if ST7565_UPDATE_INTERVAL > 0
+ if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
+ st7565_update_timeout = timer_read();
+ st7565_set_cursor(0, 0);
+ st7565_task_user();
+ }
+#else
+ st7565_set_cursor(0, 0);
+ st7565_task_user();
+#endif
+
+ // Smart render system, no need to check for dirty
+ st7565_render();
+
+ // Display timeout check
+#if ST7565_TIMEOUT > 0
+ if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
+ st7565_off();
+ }
+#endif
+}
+
+__attribute__((weak)) void st7565_task_user(void) {}
+
+void st7565_reset(void) {
+ writePinLow(ST7565_RST_PIN);
+ wait_ms(20);
+ writePinHigh(ST7565_RST_PIN);
+ wait_ms(20);
+}
+
+spi_status_t st7565_send_cmd(uint8_t cmd) {
+ writePinLow(ST7565_A0_PIN);
+ return spi_write(cmd);
+}
+
+spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
+ writePinHigh(ST7565_A0_PIN);
+ return spi_transmit(data, length);
+}
diff --git a/drivers/lcd/st7565.h b/drivers/lcd/st7565.h
new file mode 100644
index 0000000000..53cfc9a810
--- /dev/null
+++ b/drivers/lcd/st7565.h
@@ -0,0 +1,215 @@
+/*
+Copyright 2021
+
+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
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+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/>.
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "spi_master.h"
+
+#ifndef ST7565_DISPLAY_WIDTH
+# define ST7565_DISPLAY_WIDTH 128
+#endif
+#ifndef ST7565_DISPLAY_HEIGHT
+# define ST7565_DISPLAY_HEIGHT 32
+#endif
+#ifndef ST7565_MATRIX_SIZE
+# define ST7565_MATRIX_SIZE (ST7565_DISPLAY_HEIGHT / 8 * ST7565_DISPLAY_WIDTH) // 1024 (compile time mathed)
+#endif
+#ifndef ST7565_BLOCK_TYPE
+# define ST7565_BLOCK_TYPE uint16_t
+#endif
+#ifndef ST7565_BLOCK_COUNT
+# define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8) // 32 (compile time mathed)
+#endif
+#ifndef ST7565_BLOCK_SIZE
+# define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT) // 32 (compile time mathed)
+#endif
+
+// the column address corresponding to the first column in the display hardware
+#if !defined(ST7565_COLUMN_OFFSET)
+# define ST7565_COLUMN_OFFSET 0
+#endif
+
+// spi clock divisor
+#if !defined(ST7565_SPI_CLK_DIVISOR)
+# define ST7565_SPI_CLK_DIVISOR 4
+#endif
+
+// Custom font file to use
+#if !defined(ST7565_FONT_H)
+# define ST7565_FONT_H "glcdfont.c"
+#endif
+// unsigned char value of the first character in the font file
+#if !defined(ST7565_FONT_START)
+# define ST7565_FONT_START 0
+#endif
+// unsigned char value of the last character in the font file
+#if !defined(ST7565_FONT_END)
+# define ST7565_FONT_END 223
+#endif
+// Font render width
+#if !defined(ST7565_FONT_WIDTH)
+# define ST7565_FONT_WIDTH 6
+#endif
+// Font render height
+#if !defined(ST7565_FONT_HEIGHT)
+# define ST7565_FONT_HEIGHT 8
+#endif
+// Default contrast level
+#if !defined(ST7565_CONTRAST)
+# define ST7565_CONTRAST 32
+#endif
+
+#if !defined(ST7565_TIMEOUT)
+# if defined(ST7565_DISABLE_TIMEOUT)
+# define ST7565_TIMEOUT 0
+# else
+# define ST7565_TIMEOUT 60000
+# endif
+#endif
+
+#if !defined(ST7565_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD)
+# define ST7565_UPDATE_INTERVAL 50
+#endif
+
+typedef struct __attribute__((__packed__)) {
+ uint8_t *current_element;
+ uint16_t remaining_element_count;
+} display_buffer_reader_t;
+
+// Rotation enum values are flags
+typedef enum { DISPLAY_ROTATION_0, DISPLAY_ROTATION_180 } display_rotation_t;
+
+// Initialize the display, rotating the rendered output based on the define passed in.
+// Returns true if the display was initialized successfully
+bool st7565_init(display_rotation_t rotation);
+
+// Called at the start of st7565_init, weak function overridable by the user
+// rotation - the value passed into st7565_init
+// Return new display_rotation_t if you want to override default rotation
+display_rotation_t st7565_init_user(display_rotation_t rotation);
+
+// Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
+void st7565_clear(void);
+
+// Renders the dirty chunks of the buffer to display
+void st7565_render(void);
+
+// Moves cursor to character position indicated by column and line, wraps if out of bounds
+// Max column denoted by 'st7565_max_chars()' and max lines by 'st7565_max_lines()' functions
+void st7565_set_cursor(uint8_t col, uint8_t line);
+
+// Advances the cursor to the next page, writing ' ' if true
+// Wraps to the begining when out of bounds
+void st7565_advance_page(bool clearPageRemainder);
+
+// Moves the cursor forward 1 character length
+// Advance page if there is not enough room for the next character
+// Wraps to the begining when out of bounds
+void st7565_advance_char(void);
+
+// Writes a single character to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Main handler that writes character data to the display buffer
+void st7565_write_char(const char data, bool invert);
+
+// Writes a string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+void st7565_write(const char *data, bool invert);
+
+// Writes a string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
+void st7565_write_ln(const char *data, bool invert);
+
+// Pans the buffer to the right (or left by passing true) by moving contents of the buffer
+// Useful for moving the screen in preparation for new drawing
+void st7565_pan(bool left);
+
+// Returns a pointer to the requested start index in the buffer plus remaining
+// buffer length as struct
+display_buffer_reader_t st7565_read_raw(uint16_t start_index);
+
+// Writes a string to the buffer at current cursor position
+void st7565_write_raw(const char *data, uint16_t size);
+
+// Writes a single byte into the buffer at the specified index
+void st7565_write_raw_byte(const char data, uint16_t index);
+
+// Sets a specific pixel on or off
+// Coordinates start at top-left and go right and down for positive x and y
+void st7565_write_pixel(uint8_t x, uint8_t y, bool on);
+
+#if defined(__AVR__)
+// Writes a PROGMEM string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Remapped to call 'void st7565_write(const char *data, bool invert);' on ARM
+void st7565_write_P(const char *data, bool invert);
+
+// Writes a PROGMEM string to the buffer at current cursor position
+// Advances the cursor while writing, inverts the pixels if true
+// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
+// Remapped to call 'void st7565_write_ln(const char *data, bool invert);' on ARM
+void st7565_write_ln_P(const char *data, bool invert);
+
+// Writes a PROGMEM string to the buffer at current cursor position
+void st7565_write_raw_P(const char *data, uint16_t size);
+#else
+# define st7565_write_P(data, invert) st7565_write(data, invert)
+# define st7565_write_ln_P(data, invert) st7565_write_ln(data, invert)
+# define st7565_write_raw_P(data, size) st7565_write_raw(data, size)
+#endif // defined(__AVR__)
+
+// Can be used to manually turn on the screen if it is off
+// Returns true if the screen was on or turns on
+bool st7565_on(void);
+
+// Called wh