summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/eeprom/eeprom_i2c.h5
-rw-r--r--drivers/eeprom/eeprom_spi.c42
-rw-r--r--drivers/eeprom/eeprom_wear_leveling.c23
-rw-r--r--drivers/gpio/pca9505.c166
-rw-r--r--drivers/gpio/pca9505.h67
-rw-r--r--drivers/led/aw20216.c6
-rw-r--r--drivers/led/issi/is31fl3733-simple.c6
-rw-r--r--drivers/led/issi/is31fl3733.c6
-rw-r--r--drivers/led/issi/is31fl3736.c6
-rw-r--r--drivers/led/issi/is31fl3737.c6
-rw-r--r--drivers/led/issi/is31fl3741.c6
-rw-r--r--drivers/painter/gc9a01/qp_gc9a01.c9
-rw-r--r--drivers/painter/ili9xxx/qp_ili9163.c9
-rw-r--r--drivers/painter/ili9xxx/qp_ili9341.c9
-rw-r--r--drivers/painter/ili9xxx/qp_ili9488.c120
-rw-r--r--drivers/painter/ili9xxx/qp_ili9488.h37
-rw-r--r--drivers/painter/ili9xxx/qp_ili9xxx_opcodes.h1
-rw-r--r--drivers/painter/ssd1351/qp_ssd1351.c9
-rw-r--r--drivers/painter/st77xx/qp_st7789.c9
-rw-r--r--drivers/painter/tft_panel/qp_tft_panel.c60
-rw-r--r--drivers/painter/tft_panel/qp_tft_panel.h16
-rw-r--r--drivers/ps2/ps2.h1
-rw-r--r--drivers/ps2/ps2_interrupt.c4
-rw-r--r--drivers/ps2/ps2_mouse.c19
-rw-r--r--drivers/sensors/cirque_pinnacle.c232
-rw-r--r--drivers/sensors/cirque_pinnacle.h62
-rw-r--r--drivers/sensors/cirque_pinnacle_gestures.c227
-rw-r--r--drivers/sensors/cirque_pinnacle_gestures.h107
-rw-r--r--drivers/sensors/cirque_pinnacle_i2c.c4
-rw-r--r--drivers/sensors/cirque_pinnacle_regdefs.h405
-rw-r--r--drivers/sensors/cirque_pinnacle_spi.c4
-rw-r--r--drivers/sensors/pimoroni_trackball.c13
-rw-r--r--drivers/sensors/pimoroni_trackball.h1
-rw-r--r--drivers/serial.h10
-rw-r--r--drivers/wear_leveling/wear_leveling_flash_spi.c101
-rw-r--r--drivers/wear_leveling/wear_leveling_flash_spi_config.h34
36 files changed, 1611 insertions, 231 deletions
diff --git a/drivers/eeprom/eeprom_i2c.h b/drivers/eeprom/eeprom_i2c.h
index 77eea66d63..85317c9ea5 100644
--- a/drivers/eeprom/eeprom_i2c.h
+++ b/drivers/eeprom/eeprom_i2c.h
@@ -54,6 +54,11 @@
# define EXTERNAL_EEPROM_PAGE_SIZE 32
# define EXTERNAL_EEPROM_ADDRESS_SIZE 2
# define EXTERNAL_EEPROM_WRITE_TIME 5
+#elif defined(EEPROM_I2C_24LC32A)
+# define EXTERNAL_EEPROM_BYTE_COUNT 4096
+# define EXTERNAL_EEPROM_PAGE_SIZE 32
+# define EXTERNAL_EEPROM_ADDRESS_SIZE 2
+# define EXTERNAL_EEPROM_WRITE_TIME 5
#elif defined(EEPROM_I2C_MB85RC256V)
# define EXTERNAL_EEPROM_BYTE_COUNT 32768
# define EXTERNAL_EEPROM_PAGE_SIZE 128
diff --git a/drivers/eeprom/eeprom_spi.c b/drivers/eeprom/eeprom_spi.c
index 25955498c4..51ba25dece 100644
--- a/drivers/eeprom/eeprom_spi.c
+++ b/drivers/eeprom/eeprom_spi.c
@@ -58,14 +58,20 @@ static bool spi_eeprom_start(void) {
static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
uint32_t deadline = timer_read32() + timeout;
- spi_status_t response;
- do {
+ spi_status_t response = SR_WIP;
+ while (response & SR_WIP) {
+ if (!spi_eeprom_start()) {
+ return SPI_STATUS_ERROR;
+ }
+
spi_write(CMD_RDSR);
response = spi_read();
+ spi_stop();
+
if (timer_read32() >= deadline) {
return SPI_STATUS_TIMEOUT;
}
- } while (response & SR_WIP);
+ }
return SPI_STATUS_SUCCESS;
}
@@ -105,27 +111,21 @@ void eeprom_driver_erase(void) {
void eeprom_read_block(void *buf, const void *addr, size_t len) {
//-------------------------------------------------
// Wait for the write-in-progress bit to be cleared
- bool res = spi_eeprom_start();
- if (!res) {
- dprint("failed to start SPI for WIP check\n");
- memset(buf, 0, len);
- return;
- }
-
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
- spi_stop();
- if (response == SPI_STATUS_TIMEOUT) {
- dprint("SPI timeout for WIP check\n");
+ if (response != SPI_STATUS_SUCCESS) {
+ spi_stop();
memset(buf, 0, len);
+ dprint("SPI timeout for WIP check\n");
return;
}
//-------------------------------------------------
// Perform read
- res = spi_eeprom_start();
+ bool res = spi_eeprom_start();
if (!res) {
- dprint("failed to start SPI for read\n");
+ spi_stop();
memset(buf, 0, len);
+ dprint("failed to start SPI for read\n");
return;
}
@@ -158,15 +158,9 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
//-------------------------------------------------
// Wait for the write-in-progress bit to be cleared
- res = spi_eeprom_start();
- if (!res) {
- dprint("failed to start SPI for WIP check\n");
- return;
- }
-
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
- spi_stop();
- if (response == SPI_STATUS_TIMEOUT) {
+ if (response != SPI_STATUS_SUCCESS) {
+ spi_stop();
dprint("SPI timeout for WIP check\n");
return;
}
@@ -175,6 +169,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Enable writes
res = spi_eeprom_start();
if (!res) {
+ spi_stop();
dprint("failed to start SPI for write-enable\n");
return;
}
@@ -186,6 +181,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Perform the write
res = spi_eeprom_start();
if (!res) {
+ spi_stop();
dprint("failed to start SPI for write\n");
return;
}
diff --git a/drivers/eeprom/eeprom_wear_leveling.c b/drivers/eeprom/eeprom_wear_leveling.c
new file mode 100644
index 0000000000..bd77eef35c
--- /dev/null
+++ b/drivers/eeprom/eeprom_wear_leveling.c
@@ -0,0 +1,23 @@
+// Copyright 2022 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <stdint.h>
+#include <string.h>
+
+#include "eeprom_driver.h"
+#include "wear_leveling.h"
+
+void eeprom_driver_init(void) {
+ wear_leveling_init();
+}
+
+void eeprom_driver_erase(void) {
+ wear_leveling_erase();
+}
+
+void eeprom_read_block(void *buf, const void *addr, size_t len) {
+ wear_leveling_read((uint32_t)addr, buf, len);
+}
+
+void eeprom_write_block(const void *buf, void *addr, size_t len) {
+ wear_leveling_write((uint32_t)addr, buf, len);
+}
diff --git a/drivers/gpio/pca9505.c b/drivers/gpio/pca9505.c
new file mode 100644
index 0000000000..5803746c96
--- /dev/null
+++ b/drivers/gpio/pca9505.c
@@ -0,0 +1,166 @@
+// Copyright 2022 nirim000
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "i2c_master.h"
+#include "pca9505.h"
+
+#include "debug.h"
+
+#define SLAVE_TO_ADDR(n) (n << 1)
+#define TIMEOUT 100
+
+enum {
+ CMD_INPUT_0 = 0,
+ CMD_INPUT_1,
+ CMD_INPUT_2,
+ CMD_INPUT_3,
+ CMD_INPUT_4,
+ CMD_OUTPUT_0 = 8,
+ CMD_OUTPUT_1,
+ CMD_OUTPUT_2,
+ CMD_OUTPUT_3,
+ CMD_OUTPUT_4,
+ CMD_INVERSION_0 = 16,
+ CMD_INVERSION_1,
+ CMD_INVERSION_2,
+ CMD_INVERSION_3,
+ CMD_INVERSION_4,
+ CMD_CONFIG_0 = 24,
+ CMD_CONFIG_1,
+ CMD_CONFIG_2,
+ CMD_CONFIG_3,
+ CMD_CONFIG_4,
+};
+
+void pca9505_init(uint8_t slave_addr) {
+ static uint8_t s_init = 0;
+ if (!s_init) {
+ i2c_init();
+
+ s_init = 1;
+ }
+
+ // TODO: could check device connected
+ // i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
+ // i2c_stop();
+}
+
+bool pca9505_set_config(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
+ uint8_t addr = SLAVE_TO_ADDR(slave_addr);
+ uint8_t cmd = 0;
+ switch (port) {
+ case 0:
+ cmd = CMD_CONFIG_0;
+ break;
+ case 1:
+ cmd = CMD_CONFIG_1;
+ break;
+ case 2:
+ cmd = CMD_CONFIG_2;
+ break;
+ case 3:
+ cmd = CMD_CONFIG_3;
+ break;
+ case 4:
+ cmd = CMD_CONFIG_4;
+ break;
+ }
+
+ i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
+ if (ret != I2C_STATUS_SUCCESS) {
+ print("pca9505_set_config::FAILED\n");
+ return false;
+ }
+
+ return true;
+}
+
+bool pca9505_set_polarity(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
+ uint8_t addr = SLAVE_TO_ADDR(slave_addr);
+ uint8_t cmd = 0;
+ switch (port) {
+ case 0:
+ cmd = CMD_INVERSION_0;
+ break;
+ case 1:
+ cmd = CMD_INVERSION_1;
+ break;
+ case 2:
+ cmd = CMD_INVERSION_2;
+ break;
+ case 3:
+ cmd = CMD_INVERSION_3;
+ break;
+ case 4:
+ cmd = CMD_INVERSION_4;
+ break;
+ }
+
+ i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
+ if (ret != I2C_STATUS_SUCCESS) {
+ print("pca9505_set_polarity::FAILED\n");
+ return false;
+ }
+
+ return true;
+}
+
+bool pca9505_set_output(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
+ uint8_t addr = SLAVE_TO_ADDR(slave_addr);
+ uint8_t cmd = 0;
+ switch (port) {
+ case 0:
+ cmd = CMD_OUTPUT_0;
+ break;
+ case 1:
+ cmd = CMD_OUTPUT_1;
+ break;
+ case 2:
+ cmd = CMD_OUTPUT_2;
+ break;
+ case 3:
+ cmd = CMD_OUTPUT_3;
+ break;
+ case 4:
+ cmd = CMD_OUTPUT_4;
+ break;
+ }
+
+ i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
+ if (ret != I2C_STATUS_SUCCESS) {
+ print("pca9505_set_output::FAILED\n");
+ return false;
+ }
+
+ return true;
+}
+
+bool pca9505_readPins(uint8_t slave_addr, pca9505_port_t port, uint8_t* out) {
+ uint8_t addr = SLAVE_TO_ADDR(slave_addr);
+ uint8_t cmd = 0;
+ switch (port) {
+ case 0:
+ cmd = CMD_INPUT_0;
+ break;
+ case 1:
+ cmd = CMD_INPUT_1;
+ break;
+ case 2:
+ cmd = CMD_INPUT_2;
+ break;
+ case 3:
+ cmd = CMD_INPUT_3;
+ break;
+ case 4:
+ cmd = CMD_INPUT_4;
+ break;
+ }
+
+ i2c_status_t ret = i2c_readReg(addr, cmd, out, sizeof(uint8_t), TIMEOUT);
+ if (ret != I2C_STATUS_SUCCESS) {
+ print("pca9505_readPins::FAILED\n");
+ return false;
+ }
+
+ return true;
+}
diff --git a/drivers/gpio/pca9505.h b/drivers/gpio/pca9505.h
new file mode 100644
index 0000000000..732ddb88ea
--- /dev/null
+++ b/drivers/gpio/pca9505.h
@@ -0,0 +1,67 @@
+// Copyright 2022 nirim000
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/**
+ * Port ID
+ */
+typedef enum {
+ PCA9505_PORT0,
+ PCA9505_PORT1,
+ PCA9505_PORT2,
+ PCA9505_PORT3,
+ PCA9505_PORT4,
+} pca9505_port_t;
+
+/**
+ * Helpers for set_config
+ */
+enum {
+ ALL_NORMAL = 0,
+ ALL_INVERTED = 0xFF,
+};
+
+/**
+ * Helpers for set_config
+ */
+enum {
+ ALL_OUTPUT = 0,
+ ALL_INPUT = 0xFF,
+};
+
+/**
+ * Helpers for set_output
+ */
+enum {
+ ALL_LOW = 0,
+ ALL_HIGH = 0xFF,
+};
+
+/**
+ * Init expander and any other dependent drivers
+ */
+void pca9505_init(uint8_t slave_addr);
+
+/**
+ * Configure input/output to a given port
+ */
+bool pca9505_set_config(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);
+
+/**
+ * Configure polarity to a given port
+ */
+bool pca9505_set_polarity(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);
+
+/**
+ * Write high/low to a given port
+ */
+bool pca9505_set_output(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);
+
+/**
+ * Read state of a given port
+ */
+bool pca9505_readPins(uint8_t slave_addr, pca9505_port_t port, uint8_t* ret);
diff --git a/drivers/led/aw20216.c b/drivers/led/aw20216.c
index 448accdcd3..55083936ef 100644
--- a/drivers/led/aw20216.c
+++ b/drivers/led/aw20216.c
@@ -53,6 +53,10 @@
# define AW_GLOBAL_CURRENT_MAX 150
#endif
+#ifndef AW_SPI_MODE
+# define AW_SPI_MODE 0
+#endif
+
#ifndef AW_SPI_DIVISOR
# define AW_SPI_DIVISOR 4
#endif
@@ -63,7 +67,7 @@ bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false};
bool AW20216_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8_t len) {
static uint8_t s_spi_transfer_buffer[2] = {0};
- if (!spi_start(cs_pin, false, 3, AW_SPI_DIVISOR)) {
+ if (!spi_start(cs_pin, false, AW_SPI_MODE, AW_SPI_DIVISOR)) {
spi_stop();
return false;
}
diff --git a/drivers/led/issi/is31fl3733-simple.c b/drivers/led/issi/is31fl3733-simple.c
index af006f756d..2f41a7b1a9 100644
--- a/drivers/led/issi/is31fl3733-simple.c
+++ b/drivers/led/issi/is31fl3733-simple.c
@@ -70,6 +70,10 @@
# define ISSI_CSPULLUP PUR_0R
#endif
+#ifndef ISSI_GLOBALCURRENT
+# define ISSI_GLOBALCURRENT 0xFF
+#endif
+
// Transfer buffer for TWITransmitData()
uint8_t g_twi_transfer_buffer[20];
@@ -182,7 +186,7 @@ void IS31FL3733_init(uint8_t addr, uint8_t sync) {
// Set de-ghost pull-down resistors (CSx)
IS31FL3733_write_register(addr, ISSI_REG_CSPULLUP, ISSI_CSPULLUP);
// Set global current to maximum.
- IS31FL3733_write_register(addr, ISSI_REG_GLOBALCURRENT, 0xFF);
+ IS31FL3733_write_register(addr, ISSI_REG_GLOBALCURRENT, ISSI_GLOBALCURRENT);
// Disable software shutdown.
IS31FL3733_write_register(addr, ISSI_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((ISSI_PWM_FREQUENCY & 0b111) << 3) | 0x01);
diff --git a/drivers/led/issi/is31fl3733.c b/drivers/led/issi/is31fl3733.c
index a2fdaa90fa..add998f256 100644
--- a/drivers/led/issi/is31fl3733.c
+++ b/drivers/led/issi/is31fl3733.c
@@ -69,6 +69,10 @@
# define ISSI_CSPULLUP PUR_0R
#endif
+#ifndef ISSI_GLOBALCURRENT
+# define ISSI_GLOBALCURRENT 0xFF
+#endif
+
// Transfer buffer for TWITransmitData()
uint8_t g_twi_transfer_buffer[20];
@@ -172,7 +176,7 @@ void IS31FL3733_init(uint8_t addr, uint8_t sync) {
// Set de-ghost pull-down resistors (CSx)
IS31FL3733_write_register(addr, ISSI_REG_CSPULLUP, ISSI_CSPULLUP);
// Set global current to maximum.
- IS31FL3733_write_register(addr, ISSI_REG_GLOBALCURRENT, 0xFF);
+ IS31FL3733_write_register(addr, ISSI_REG_GLOBALCURRENT, ISSI_GLOBALCURRENT);
// Disable software shutdown.
IS31FL3733_write_register(addr, ISSI_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((ISSI_PWM_FREQUENCY & 0b111) << 3) | 0x01);
diff --git a/drivers/led/issi/is31fl3736.c b/drivers/led/issi/is31fl3736.c
index 7752a3f6cb..e9943614d2 100644
--- a/drivers/led/issi/is31fl3736.c
+++ b/drivers/led/issi/is31fl3736.c
@@ -63,6 +63,10 @@
# define ISSI_CSPULLUP PUR_0R
#endif
+#ifndef ISSI_GLOBALCURRENT
+# define ISSI_GLOBALCURRENT 0xFF
+#endif
+
// Transfer buffer for TWITransmitData()
uint8_t g_twi_transfer_buffer[20];
@@ -154,7 +158,7 @@ void IS31FL3736_init(uint8_t addr) {
// Set de-ghost pull-down resistors (CSx)
IS31FL3736_write_register(addr, ISSI_REG_CSPULLUP, ISSI_CSPULLUP);
// Set global current to maximum.
- IS31FL3736_write_register(addr, ISSI_REG_GLOBALCURRENT, 0xFF);
+ IS31FL3736_write_register(addr, ISSI_REG_GLOBALCURRENT, ISSI_GLOBALCURRENT);
// Disable software shutdown.
IS31FL3736_write_register(addr, ISSI_REG_CONFIGURATION, 0x01);
diff --git a/drivers/led/issi/is31fl3737.c b/drivers/led/issi/is31fl3737.c
index bce0c34b2c..932530ac0a 100644
--- a/drivers/led/issi/is31fl3737.c
+++ b/drivers/led/issi/is31fl3737.c
@@ -69,6 +69,10 @@
# define ISSI_CSPULLUP PUR_0R
#endif
+#ifndef ISSI_GLOBALCURRENT
+# define ISSI_GLOBALCURRENT 0xFF
+#endif
+
// Transfer buffer for TWITransmitData()
uint8_t g_twi_transfer_buffer[20];
@@ -161,7 +165,7 @@ void IS31FL3737_init(uint8_t addr) {
// Set de-ghost pull-down resistors (CSx)
IS31FL3737_write_register(addr, ISSI_REG_CSPULLUP, ISSI_CSPULLUP);
// Set global current to maximum.
- IS31FL3737_write_register(addr, ISSI_REG_GLOBALCURRENT, 0xFF);
+ IS31FL3737_write_register(addr, ISSI_REG_GLOBALCURRENT, ISSI_GLOBALCURRENT);
// Disable software shutdown.
IS31FL3737_write_register(addr, ISSI_REG_CONFIGURATION, ((ISSI_PWM_FREQUENCY & 0b111) << 3) | 0x01);
diff --git a/drivers/led/issi/is31fl3741.c b/drivers/led/issi/is31fl3741.c
index 393b0179b5..ba6b6761a3 100644
--- a/drivers/led/issi/is31fl3741.c
+++ b/drivers/led/issi/is31fl3741.c
@@ -69,6 +69,10 @@
# define ISSI_CSPULLUP PUR_32KR
#endif
+#ifndef ISSI_GLOBALCURRENT
+# define ISSI_GLOBALCURRENT 0xFF
+#endif
+
#define ISSI_MAX_LEDS 351
// Transfer buffer for TWITransmitData()
@@ -163,7 +167,7 @@ void IS31FL3741_init(uint8_t addr) {
IS31FL3741_write_register(addr, ISSI_REG_CONFIGURATION, 0x01);
// Set Golbal Current Control Register
- IS31FL3741_write_register(addr, ISSI_REG_GLOBALCURRENT, 0xFF);
+ IS31FL3741_write_register(addr, ISSI_REG_GLOBALCURRENT, ISSI_GLOBALCURRENT);
// Set Pull up & Down for SWx CSy
IS31FL3741_write_register(addr, ISSI_REG_PULLDOWNUP, ((ISSI_CSPULLUP << 4) | ISSI_SWPULLUP));
diff --git a/drivers/painter/gc9a01/qp_gc9a01.c b/drivers/painter/gc9a01/qp_gc9a01.c
index ad76d58b07..37700a28a4 100644
--- a/drivers/painter/gc9a01/qp_gc9a01.c
+++ b/drivers/painter/gc9a01/qp_gc9a01.c
@@ -102,12 +102,11 @@ const struct tft_panel_dc_reset_painter_driver_vtable_t gc9a01_driver_vtable = {
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
- .palette_convert = qp_tft_panel_palette_convert,
- .append_pixels = qp_tft_panel_append_pixels,
+ .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
+ .append_pixels = qp_tft_panel_append_pixels_rgb565,
},
- .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
- .num_window_bytes = 2,
- .swap_window_coords = false,
+ .num_window_bytes = 2,
+ .swap_window_coords = false,
.opcodes =
{
.display_on = GC9A01_CMD_DISPLAY_ON,
diff --git a/drivers/painter/ili9xxx/qp_ili9163.c b/drivers/painter/ili9xxx/qp_ili9163.c
index beaac0fbb5..14363c7d04 100644
--- a/drivers/painter/ili9xxx/qp_ili9163.c
+++ b/drivers/painter/ili9xxx/qp_ili9163.c
@@ -67,12 +67,11 @@ const struct tft_panel_dc_reset_painter_driver_vtable_t ili9163_driver_vtable =
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
- .palette_convert = qp_tft_panel_palette_convert,
- .append_pixels = qp_tft_panel_append_pixels,
+ .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
+ .append_pixels = qp_tft_panel_append_pixels_rgb565,
},
- .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
- .num_window_bytes = 2,
- .swap_window_coords = false,
+ .num_window_bytes = 2,
+ .swap_window_coords = false,
.opcodes =
{
.display_on = ILI9XXX_CMD_DISPLAY_ON,
diff --git a/drivers/painter/ili9xxx/qp_ili9341.c b/drivers/painter/ili9xxx/qp_ili9341.c
index 1f41dcfc0b..9608f109bd 100644
--- a/drivers/painter/ili9xxx/qp_ili9341.c
+++ b/drivers/painter/ili9xxx/qp_ili9341.c
@@ -74,12 +74,11 @@ const struct tft_panel_dc_reset_painter_driver_vtable_t ili9341_driver_vtable =
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
- .palette_convert = qp_tft_panel_palette_convert,
- .append_pixels = qp_tft_panel_append_pixels,
+ .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
+ .append_pixels = qp_tft_panel_append_pixels_rgb565,
},
- .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
- .num_window_bytes = 2,
- .swap_window_coords = false,
+ .num_window_bytes = 2,
+ .swap_window_coords = false,
.opcodes =
{
.display_on = ILI9XXX_CMD_DISPLAY_ON,
diff --git a/drivers/painter/ili9xxx/qp_ili9488.c b/drivers/painter/ili9xxx/qp_ili9488.c
new file mode 100644
index 0000000000..55cf9f896f
--- /dev/null
+++ b/drivers/painter/ili9xxx/qp_ili9488.c
@@ -0,0 +1,120 @@
+// Copyright 2021 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "qp_internal.h"
+#include "qp_comms.h"
+#include "qp_ili9488.h"
+#include "qp_ili9xxx_opcodes.h"
+#include "qp_tft_panel.h"
+
+#ifdef QUANTUM_PAINTER_ILI9488_SPI_ENABLE
+# include <qp_comms_spi.h>
+#endif // QUANTUM_PAINTER_ILI9488_SPI_ENABLE
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Common
+
+// Driver storage
+tft_panel_dc_reset_painter_device_t ili9488_drivers[ILI9488_NUM_DEVICES] = {0};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Initialization
+
+bool qp_ili9488_init(painter_device_t device, painter_rotation_t rotation) {
+ // clang-format off
+ const uint8_t ili9488_init_sequence[] = {
+ // Command, Delay, N, Data[N]
+ ILI9XXX_CMD_RESET, 120, 0,
+ ILI9XXX_SET_PGAMMA, 0, 15, 0x00, 0x03, 0x09, 0x08, 0x16, 0x0A, 0x3F, 0x78, 0x4C, 0x09, 0x0A, 0x08, 0x16, 0x1A, 0x0F,
+ ILI9XXX_SET_NGAMMA, 0, 15, 0x00, 0x16, 0x19, 0x03, 0x0F, 0x05, 0x32, 0x45, 0x46, 0x04, 0x0E, 0x0D, 0x35, 0x37, 0x0F,
+ ILI9XXX_SET_POWER_CTL_1, 0, 2, 0x17, 0x15,
+ ILI9XXX_SET_POWER_CTL_2, 0, 1, 0x41,
+ ILI9XXX_SET_VCOM_CTL_1, 0, 3, 0x00, 0x12, 0x80,
+ ILI9XXX_SET_PIX_FMT, 0, 1, 0x66,
+ ILI9XXX_SET_RGB_IF_SIG_CTL, 0, 1, 0x80,
+ ILI9XXX_SET_FRAME_CTL_NORMAL, 0, 1, 0xA0,
+ ILI9XXX_SET_INVERSION_CTL, 0, 1, 0x02,
+ ILI9XXX_SET_FUNCTION_CTL, 0, 2, 0x02, 0x02,
+ ILI9XXX_SET_IMAGE_FUNCTION, 0, 1, 0x00,
+ ILI9XXX_SET_PUMP_RATIO_CTL, 0, 4, 0xA9, 0x51, 0x2C, 0x82,
+ ILI9XXX_CMD_SLEEP_OFF, 5, 0,
+ ILI9XXX_CMD_DISPLAY_ON, 20, 0
+ };
+ // clang-format on
+ qp_comms_bulk_command_sequence(device, ili9488_init_sequence, sizeof(ili9488_init_sequence));
+
+ // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
+ const uint8_t madctl[] = {
+ [QP_ROTATION_0] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MY,
+ [QP_ROTATION_90] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX | ILI9XXX_MADCTL_MV | ILI9XXX_MADCTL_MY,
+ [QP_ROTATION_180] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX,
+ [QP_ROTATION_270] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MV,
+ };
+ qp_comms_command_databyte(device, ILI9XXX_SET_MEM_ACS_CTL, madctl[rotation]);
+
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Driver vtable
+
+const struct tft_panel_dc_reset_painter_driver_vtable_t ili9488_driver_vtable = {
+ .base =
+ {
+ .init = qp_ili9488_init,
+ .power = qp_tft_panel_power,
+ .clear = qp_tft_panel_clear,
+ .flush = qp_tft_panel_flush,
+ .pixdata = qp_tft_panel_pixdata,
+ .viewport = qp_tft_panel_viewport,
+ .palette_convert = qp_tft_panel_palette_convert_rgb888,
+ .append_pixels = qp_tft_panel_append_pixels_rgb888,
+ },
+ .num_window_bytes = 2,
+ .swap_window_coords = false,
+ .opcodes =
+ {
+ .display_on = ILI9XXX_CMD_DISPLAY_ON,
+ .display_off = ILI9XXX_CMD_DISPLAY_OFF,
+ .set_column_address = ILI9XXX_SET_COL_ADDR,
+ .set_row_address = ILI9XXX_SET_PAGE_ADDR,
+ .enable_writes = ILI9XXX_SET_MEM,
+ },
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// SPI
+
+#ifdef QUANTUM_PAINTER_ILI9488_SPI_ENABLE
+
+// Factory function for creating a handle to the ILI9488 device
+painter_device_t qp_ili9488_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
+ for (uint32_t i = 0; i < ILI9488_NUM_DEVICES; ++i) {
+ tft_panel_dc_reset_painter_device_t *driver = &ili9488_drivers[i];
+ if (!driver->base.driver_vtable) {
+ driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&ili9488_driver_vtable;
+ driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
+ driver->base.native_bits_per_pixel = 24; // RGB888
+ driver->base.panel_width = panel_width;
+ driver->base.panel_height = panel_height;
+ driver->base.rotation = QP_ROTATION_0;
+ driver->base.offset_x = 0;
+ driver->base.offset_y = 0;
+
+ // SPI and other pin configuration
+ driver->base.comms_config = &driver->spi_dc_reset_config;
+ driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
+ driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
+ driver->spi_dc_reset_config.spi_config.lsb_first = false;
+ driver->spi_dc_reset_config.spi_config.mode = spi_mode;
+ driver->spi_dc_reset_config.dc_pin = dc_pin;
+ driver->spi_dc_reset_config.reset_pin = reset_pin;
+ return (painter_device_t)driver;
+ }
+ }
+ return NULL;
+}
+
+#endif // QUANTUM_PAINTER_ILI9488_SPI_ENABLE
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/drivers/painter/ili9xxx/qp_ili9488.h b/drivers/painter/ili9xxx/qp_ili9488.h
new file mode 100644
index 0000000000..21b8f03322
--- /dev/null
+++ b/drivers/painter/ili9xxx/qp_ili9488.h
@@ -0,0 +1,37 @@
+// Copyright 2021 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "gpio.h"
+#include "qp_internal.h"
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Quantum Painter ILI9488 configurables (add to your keyboard's config.h)
+
+#ifndef ILI9488_NUM_DEVICES
+/**
+ * @def This controls the maximum number of ILI9488 devices that Quantum Painter can communicate with at any one time.
+ * Increasing this number allows for multiple displays to be used.
+ */
+# define ILI9488_NUM_DEVICES 1
+#endif
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Quantum Painter ILI9488 device factories
+
+#ifdef QUANTUM_PAINTER_ILI9488_SPI_ENABLE
+/**
+ * Factory method for an ILI9488 SPI LCD device.
+ *
+ * @param panel_width[in] the width of the display panel
+ * @param panel_height[in] the height of the display panel
+ * @param chip_select_pin[in] the GPIO pin used for SPI chip select
+ * @param dc_pin[in] the GPIO pin used for D/C control
+ * @param reset_pin[in] the GPIO pin used for RST
+ * @param spi_divisor[in] the SPI divisor to use when communicating with the display
+ * @param spi_mode[in] the SPI mode to use when communicating with the display
+ * @return the device handle used with all drawing routines in Quantum Painter
+ */
+painter_device_t qp_ili9488_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
+#endif // QUANTUM_PAINTER_ILI9488_SPI_ENABLE
diff --git a/drivers/painter/ili9xxx/qp_ili9xxx_opcodes.h b/drivers/painter/ili9xxx/qp_ili9xxx_opcodes.h
index 1fa395cb89..47bb703648 100644
--- a/drivers/painter/ili9xxx/qp_ili9xxx_opcodes.h
+++ b/drivers/painter/ili9xxx/qp_ili9xxx_opcodes.h
@@ -85,6 +85,7 @@
#define ILI9XXX_SET_NGAMMA 0xE1 // Set negative gamma
#define ILI9XXX_SET_DGAMMA_CTL_1 0xE2 // Set digital gamma ctl 1
#define ILI9XXX_SET_DGAMMA_CTL_2 0xE3 // Set digital gamma ctl 2
+#define ILI9XXX_SET_IMAGE_FUNCTION 0xE9 // Set image function
#define ILI9XXX_ENABLE_3_GAMMA 0xF2 // Enable 3 gamma
#define ILI9XXX_SET_IF_CTL 0xF6 // Set interface control
#define ILI9XXX_SET_PUMP_RATIO_CTL 0xF7 // Set pump ratio control
diff --git a/drivers/painter/ssd1351/qp_ssd1351.c b/drivers/painter/ssd1351/qp_ssd1351.c
index 970e7e67f3..7ce76bab6d 100644
--- a/drivers/painter/ssd1351/qp_ssd1351.c
+++ b/drivers/painter/ssd1351/qp_ssd1351.c
@@ -71,12 +71,11 @@ const struct tft_panel_dc_reset_painter_driver_vtable_t ssd1351_driver_vtable =
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
- .palette_convert = qp_tft_panel_palette_convert,
- .append_pixels = qp_tft_panel_append_pixels,
+ .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
+ .append_pixels = qp_tft_panel_append_pixels_rgb565,
},
- .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
- .num_window_bytes = 1,
- .swap_window_coords = true,
+ .num_window_bytes = 1,
+ .swap_window_coords = true,
.opcodes =
{
.display_on = SSD1351_DISPLAYON,
diff --git a/drivers/painter/st77xx/qp_st7789.c b/drivers/painter/st77xx/qp_st7789.c
index d005ece050..49e8436c29 100644
--- a/drivers/painter/st77xx/qp_st7789.c
+++ b/drivers/painter/st77xx/qp_st7789.c
@@ -90,12 +90,11 @@ const struct tft_panel_dc_reset_painter_driver_vtable_t st7789_driver_vtable = {
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
- .palette_convert = qp_tft_panel_palette_convert,
- .append_pixels = qp_tft_panel_append_pixels,
+ .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
+ .append_pixels = qp_tft_panel_append_pixels_rgb565,
},
- .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
- .num_window_bytes = 2,
- .swap_window_coords = false,
+ .num_window_bytes = 2,
+ .swap_window_coords = false,
.opcodes =
{
.display_on = ST77XX_CMD_DISPLAY_ON,
diff --git a/drivers/painter/tft_panel/qp_tft_panel.c b/drivers/painter/tft_panel/qp_tft_panel.c
index 4d636c9509..ad83b6c792 100644
--- a/drivers/painter/tft_panel/qp_tft_panel.c
+++ b/drivers/painter/tft_panel/qp_tft_panel.c
@@ -10,29 +10,6 @@
#define BYTE_SWAP(x) (((((uint16_t)(x)) >> 8) & 0x00FF) | ((((uint16_t)(x)) << 8) & 0xFF00))
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// Native pixel format conversion
-
-uint16_t qp_rgb888_to_rgb565(uint8_t r, uint8_t g, uint8_t b) {
- uint16_t rgb565 = (((uint16_t)r) >> 3) << 11 | (((uint16_t)g) >> 2) << 5 | (((uint16_t)b) >> 3);
- return rgb565;
-}
-
-uint16_t qp_rgb888_to_rgb565_swapped(uint8_t r, uint8_t g, uint8_t b) {
- uint16_t rgb565 = (((uint16_t)r) >> 3) << 11 | (((uint16_t)g) >> 2) << 5 | (((uint16_t)b) >> 3);
- return BYTE_SWAP(rgb565);
-}
-
-uint16_t qp_rgb888_to_bgr565(uint8_t r, uint8_t g, uint8_t b) {
- uint16_t bgr565 = (((uint16_t)b) >> 3) << 11 | (((uint16_t)g) >> 2) << 5 | (((uint16_t)r) >> 3);
- return bgr565;
-}
-
-uint16_t qp_rgb888_to_bgr565_swapped(uint8_t r, uint8_t g, uint8_t b) {
- uint16_t bgr565 = (((uint16_t)b) >> 3) << 11 | (((uint16_t)g) >> 2) << 5 | (((uint16_t)r) >> 3);
- return BYTE_SWAP(bgr565);
-}
-
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Quantum Painter API implementations
// Power control
@@ -105,26 +82,49 @@ bool qp_tft_panel_viewport(painter_device_t device, uint16_t left, uint16_t top,
// Stream pixel data to the current write position in GRAM
bool qp_tft_panel_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
- qp_comms_send(device, pixel_data, native_pixel_count * sizeof(uint16_t));
+ struct painter_driver_t *driver = (struct painter_driver_t *)device;
+ qp_comms_send(device, pixel_data, native_pixel_count * driver->native_bits_per_pixel / 8);
return true;
}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Convert supplied palette entries into their native equivalents
-bool qp_tft_panel_palette_convert(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
- struct painter_driver_t * driver = (struct painter_driver_t *)device;
- struct tft_panel_dc_reset_painter_driver_vtable_t *vtable = (struct tft_panel_dc_reset_painter_driver_vtable_t *)driver->driver_vtable;
+
+bool qp_tft_panel_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
for (int16_t i = 0; i < palette_size; ++i) {
- RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
- palette[i].rgb565 = vtable->rgb888_to_native16bit(rgb.r, rgb.g, rgb.b);
+ RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
+ uint16_t rgb565 = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
+ palette[i].rgb565 = BYTE_SWAP(rgb565);
}
return true;
}
+bool qp_tft_panel_palette_convert_rgb888(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
+ for (int16_t i = 0; i < palette_size; ++i) {
+ RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
+ palette[i].rgb888.r = rgb.r;
+ palette[i].rgb888.g = rgb.g;
+ palette[i].rgb888.b = rgb.b;
+ }
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Append pixels to the target location, keyed by the pixel index
-bool qp_tft_panel_append_pixels(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
+
+bool qp_tft_panel_append_pixels_rgb565(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
uint16_t *buf = (uint16_t *)target_buffer;
for (uint32_t i = 0; i < pixel_count; ++i) {
buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
}
return true;
}
+
+bool qp_tft_panel_append_pixels_rgb888(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
+ for (uint32_t i = 0; i < pixel_count; ++i) {
+ target_buffer[(pixel_offset + i) * 3 + 0] = palette[palette_indices[i]].rgb888.r;
+ target_buffer[(pixel_offset + i) * 3 + 1] = palette[palette_indices[i]].rgb888.g;
+ target_buffer[(pixel_offset + i) * 3 + 2] = palette[palette_indices[i]].rgb888.b;
+ }
+ return true;
+}
diff --git a/drivers/painter/tft_panel/qp_tft_panel.h b/drivers/painter/tft_panel/qp_tft_panel.h
index 6eddfc503d..3cb015891b 100644
--- a/drivers/painter/tft_panel/qp_tft_panel.h
+++ b/drivers/painter/tft_panel/qp_tft_panel.h
@@ -11,15 +11,10 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common TFT panel implementation using D/C, and RST pins.
-typedef uint16_t (*rgb888_to_native_uint16_t)(uint8_t r, uint8_t g, uint8_t b);
-
// Driver vtable with extras
struct tft_panel_dc_reset_painter_driver_vtable_t {
struct painter_driver_vtable_t base; // must be first, so it can be cast to/from the painter_driver_vtable_t* type
- // Conversion function for palette entries
- rgb888_to_native_uint16_t rgb888_to_native16bit;
-
// Number of bytes for transmitting x/y coordinates
uint8_t num_window_bytes;
@@ -58,10 +53,9 @@ bool qp_tft_panel_clear(painter_device_t device);
bool qp_tft_panel_flush(painter_device_t device);
bool qp_tft_panel_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom);
bool qp_tft_panel_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count);
-bool qp_tft_panel_palette_convert(painter_device_t device, int16_t palette_size, qp_pixel_t *palette);
-bool qp_tft_panel_append_pixels(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices);
-uint16_t qp_rgb888_to_rgb565(uint8_t r, uint8_t g, uint8_t b);
-uint16_t qp_rgb888_to_rgb565_swapped(uint8_t r, uint8_t g, uint8_t b);
-uint16_t qp_rgb888_to_bgr565(uint8_t r, uint8_t g, uint8_t b);
-uint16_t qp_rgb888_to_bgr565_swapped(uint8_t r, uint8_t g, uint8_t b);
+bool qp_tft_panel_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette);
+bool qp_tft_panel_palette_convert_rgb888(painter_device_t device, int16_t palette_size, qp_pixel_t *palette);
+
+bool qp_tft_panel_append_pixels_rgb565(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices);
+bool qp_tft_panel_append_pixels_rgb888(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices);
diff --git a/drivers/ps2/ps2.h b/drivers/ps2/ps2.h
index f123192852..2465e16235 100644
--- a/drivers/ps2/ps2.h
+++ b/drivers/ps2/ps2.h
@@ -89,6 +89,7 @@ uint8_t ps2_host_send(uint8_t data);
uint8_t ps2_host_recv_response(void);
uint8_t ps2_host_recv(void);
void ps2_host_set_led(uint8_t usb_led);
+bool pbuf_has_data(void);
/*--------------------------------------------------------------------
* static functions
diff --git a/drivers/ps2/ps2_interrupt.c b/drivers/ps2/ps2_interrupt.c
index c49b4f8b75..c9a9f1e1ec 100644
--- a/drivers/ps2/ps2_interrupt.c
+++ b/drivers/ps2/ps2_interrupt.c
@@ -66,8 +66,8 @@ uint8_t ps2_error = PS2_ERR_NONE;
static inline uint8_t pbuf_dequeue(void);
static inline void pbuf_enqueue(uint8_t data);
-static inline bool pbuf_has_data(void);
static inline void pbuf_clear(void);
+bool pbuf_has_data(void);
#if defined(PROTOCOL_CHIBIOS)
void ps2_interrupt_service_routine(void);
@@ -309,7 +309,7 @@ static inline uint8_t pbuf_dequeue(void) {
return val;
}
-static inline bool pbuf_has_data(void) {
+bool pbuf_has_data(void) {
#if defined(__AVR__)
uint8_t sreg = SREG;
cli();
diff --git a/drivers/ps2/ps2_mouse.c b/drivers/ps2/ps2_mouse.c
index ccb0a929ae..66b48bb3c3 100644
--- a/drivers/ps2/ps2_mouse.c
+++ b/drivers/ps2/ps2_mouse.c
@@ -53,6 +53,7 @@ void ps2_mouse_init(void) {
ps2_mouse_set_remote_mode();
#else
ps2_mouse_enable_data_reporting();
+ ps2_mouse_set_stream_mode();
#endif
#ifdef PS2_MOUSE_ENABLE_SCROLLING
@@ -75,19 +76,33 @@ void ps2_mouse_task(void) {
extern int tp_buttons;
/* receives packet from mouse */
+#ifdef PS2_MOUSE_USE_REMOTE_MODE
uint8_t rcv;
rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
if (rcv == PS2_ACK) {
mouse_report.buttons = ps2_host_recv_response() | tp_buttons;
mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
-#ifdef PS2_MOUSE_ENABLE_SCROLLING
+# ifdef PS2_MOUSE_ENABLE_SCROLLING
mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
-#endif
+# endif
+ } else {
+ if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
+ return;
+ }
+#else
+ if (pbuf_has_data()) {
+ mouse_report.buttons = ps2_host_recv_response() | tp_buttons;
+ mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
+ mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
+# ifdef PS2_MOUSE_ENABLE_SCROLLING
+ mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
+# endif
} else {
if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
return;
}
+#endif
/* if mouse moves or buttons state changes */
if (mouse_report.x || mouse_report.y || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
diff --git a/drivers/sensors/cirque_pinnacle.c b/drivers/sensors/cirque_pinnacle.c
index 2db7f916fe..c50d5a2525 100644
--- a/drivers/sensors/cirque_pinnacle.c
+++ b/drivers/sensors/cirque_pinnacle.c
@@ -1,52 +1,24 @@
// Copyright (c) 2018 Cirque Corp. Restrictions apply. See: www.cirque.com/sw-license
+// based on https://github.com/cirque-corp/Cirque_Pinnacle_1CA027/tree/master/Circular_Trackpad
+// with modifications and changes for QMK
+// refer to documentation: Gen2 and Gen3 (Pinnacle ASIC) at https://www.cirque.com/documentation
+
#include "cirque_pinnacle.h"
#include "print.h"
#include "debug.h"
#include "wait.h"
-
-// Registers for RAP
-// clang-format off
-#define FIRMWARE_ID 0x00
-#define FIRMWARE_VERSION_C 0x01
-#define STATUS_1 0x02
-#define SYSCONFIG_1 0x03
-#define FEEDCONFIG_1 0x04
-#define FEEDCONFIG_2 0x05
-#define CALIBRATION_CONFIG_1 0x07
-#define PS2_AU_CONTROL 0x08
-#define SAMPLE_RATE 0x09
-#define Z_IDLE_COUNT 0x0A
-#define Z_SCALER 0x0B
-#define SLEEP_INTERVAL 0x0C
-#define SLEEP_TIMER 0x0D
-#define PACKET_BYTE_0 0x12
-#define PACKET_BYTE_1 0x13
-#define PACKET_BYTE_2 0x14
-#define PACKET_BYTE_3 0x15
-#define PACKET_BYTE_4 0x16
-#define PACKET_BYTE_5 0x17
-
-#define ERA_VALUE 0x1B
-#define ERA_HIGH_BYTE 0x1C
-#define ERA_LOW_BYTE 0x1D
-#define ERA_CONTROL 0x1E
-
-// ADC-attenuation settings (held in BIT_7 and BIT_6)
-// 1X = most sensitive, 4X = least sensitive
-#define ADC_ATTENUATE_1X 0x00
-#define ADC_ATTENUATE_2X 0x40
-#define ADC_ATTENUATE_3X 0x80
-#define ADC_ATTENUATE_4X 0xC0
-
-// Register config values for this demo
-#define SYSCONFIG_1_VALUE 0x00
-#define FEEDCONFIG_1_VALUE 0x03 // 0x03 for absolute mode 0x01 for relative mode
-#define FEEDCONFIG_2_VALUE 0x1C // 0x1F for normal functionality 0x1E for intellimouse disabled
-#define Z_IDLE_COUNT_VALUE 0x05
-// clang-format on
+#include "timer.h"
+
+#ifndef CIRQUE_PINNACLE_ATTENUATION
+# ifdef CIRQUE_PINNACLE_CURVED_OVERLAY
+# define CIRQUE_PINNACLE_ATTENUATION EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_2X
+# else
+# define CIRQUE_PINNACLE_ATTENUATION EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_4X
+# endif
+#endif
bool touchpad_init;
-uint16_t scale_data = 1024;
+uint16_t scale_data = CIRQUE_PINNACLE_DEFAULT_SCALE;
void cirque_pinnacle_clear_flags(void);
void cirque_pinnacle_enable_feed(bool feedEnable);
@@ -103,45 +75,45 @@ void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResoluti
// Clears Status1 register flags (SW_CC and SW_DR)
void cirque_pinnacle_clear_flags() {
- RAP_Write(STATUS_1, 0x00);
+ RAP_Write(HOSTREG__STATUS1, HOSTREG__STATUS1_DEFVAL & ~(HOSTREG__STATUS1__COMMAND_COMPLETE | HOSTREG__STATUS1__DATA_READY));
wait_us(50);
}
// Enables/Disables the feed
void cirque_pinnacle_enable_feed(bool feedEnable) {
- uint8_t temp;
-
- RAP_ReadBytes(FEEDCONFIG_1, &temp, 1); // Store contents of FeedConfig1 register
+ uint8_t feedconfig1;
+ RAP_ReadBytes(HOSTREG__FEEDCONFIG1, &feedconfig1, 1);
if (feedEnable) {
- temp |= 0x01; // Set Feed Enable bit
- RAP_Write(0x04, temp);
+ feedconfig1 |= HOSTREG__FEEDCONFIG1__FEED_ENABLE;
} else {
- temp &= ~0x01; // Clear Feed Enable bit
- RAP_Write(0x04, temp);
+ feedconfig1 &= ~HOSTREG__FEEDCONFIG1__FEED_ENABLE;
}
+ RAP_Write(HOSTREG__FEEDCONFIG1, feedconfig1);
}
/* ERA (Extended Register Access) Functions */
// Reads <count> bytes from an extended register at <address> (16-bit address),
// stores values in <*data>
void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) {
- uint8_t ERAControlValue = 0xFF;
+ uint8_t ERAControlValue = 0xFF;
+ uint16_t timeout_timer;
cirque_pinnacle_enable_feed(false); // Disable feed
- RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Send upper byte of ERA address
- RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address
+ RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_HIGH, (uint8_t)(address >> 8)); // Send upper byte of ERA address
+ RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_LOW, (uint8_t)(address & 0x00FF)); // Send lower byte of ERA address
for (uint16_t i = 0; i < count; i++) {
- RAP_Write(ERA_CONTROL, 0x05); // Signal ERA-read (auto-increment) to Pinnacle
+ RAP_Write(HOSTREG__EXT_REG_AXS_CTRL, HOSTREG__EREG_AXS__INC_ADDR_READ | HOSTREG__EREG_AXS__READ); // Signal ERA-read (auto-increment) to Pinnacle
// Wait for status register 0x1E to clear
+ timeout_timer = timer_read();
do {
- RAP_ReadBytes(ERA_CONTROL, &ERAControlValue, 1);
- } while (ERAControlValue != 0x00);
+ RAP_ReadBytes(HOSTREG__EXT_REG_AXS_CTRL, &ERAControlValue, 1);
+ } while ((ERAControlValue != 0x00) && (timer_elapsed(timeout_timer) <= CIRQUE_PINNACLE_TIMEOUT));
- RAP_ReadBytes(ERA_VALUE, data + i, 1);
+ RAP_ReadBytes(HOSTREG__EXT_REG_AXS_VALUE, data + i, 1);
cirque_pinnacle_clear_flags();
}
@@ -149,49 +121,83 @@ void ERA_ReadBytes(uint16_t address, uint8_t* data, uint16_t count) {
// Writes a byte, <data>, to an extended register at <address> (16-bit address)
void ERA_WriteByte(uint16_t address, uint8_t data) {
- uint8_t ERAControlValue = 0xFF;
+ uint8_t ERAControlValue = 0xFF;
+ uint16_t timeout_timer;
cirque_pinnacle_enable_feed(false); // Disable feed
- RAP_Write(ERA_VALUE, data); // Send data byte to be written
+ RAP_Write(HOSTREG__EXT_REG_AXS_VALUE, data); // Send data byte to be written
- RAP_Write(ERA_HIGH_BYTE, (uint8_t)(address >> 8)); // Upper byte of ERA address
- RAP_Write(ERA_LOW_BYTE, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address
+ RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_HIGH, (uint8_t)(address >> 8)); // Upper byte of ERA address
+ RAP_Write(HOSTREG__EXT_REG_AXS_ADDR_LOW, (uint8_t)(address & 0x00FF)); // Lower byte of ERA address
- RAP_Write(ERA_CONTROL, 0x02); // Signal an ERA-write to Pinnacle
+ RAP_Write(HOSTREG__EXT_REG_AXS_CTRL, HOSTREG__EREG_AXS__WRITE); // Signal an ERA-write to Pinnacle
// Wait for status register 0x1E to clear
+ timeout_timer = timer_read();
do {
- RAP_ReadBytes(ERA_CONTROL, &ERAControlValue, 1);
- } while (ERAControlValue != 0x00);
+ RAP_ReadBytes(HOSTREG__EXT_REG_AXS_CTRL, &ERAControlValue, 1);
+ } while ((ERAControlValue != 0x00) && (timer_elapsed(timeout_timer) <= CIRQUE_PINNACLE_TIMEOUT));
cirque_pinnacle_clear_flags();
}
void cirque_pinnacle_set_adc_attenuation(uint8_t adcGain) {
- uint8_t temp = 0x00;
+ uint8_t adcconfig = 0x00;
- ERA_ReadBytes(0x0187, &temp, 1);
- temp &= 0x3F; // clear top two bits
- temp |= adcGain;
- ERA_WriteByte(0x0187, temp);
- ERA_ReadBytes(0x0187, &temp, 1);
+ ERA_ReadBytes(EXTREG__TRACK_ADCCONFIG, &adcconfig, 1);
+ adcconfig &= EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_MASK;
+ adcconfig |= adcGain;
+ ERA_WriteByte(EXTREG__TRACK_ADCCONFIG, adcconfig);
+ ERA_ReadBytes(EXTREG__TRACK_ADCCONFIG, &adcconfig, 1);
}
// Changes thresholds to improve detection of fingers
+// Not needed for flat overlay?
void cirque_pinnacle_tune_edge_sensitivity(void) {
- uint8_t temp = 0x00;
+ uint8_t widezmin = 0x00;
- ERA_ReadBytes(0x0149, &temp, 1);
- ERA_WriteByte(0x0149, 0x04);
- ERA_ReadBytes(0x0149, &temp, 1);
+ ERA_ReadBytes(EXTREG__XAXIS_WIDEZMIN, &widezmin, 1);
+ ERA_WriteByte(EXTREG__XAXIS_WIDEZMIN, 0x04); // magic number from Cirque sample code
+ ERA_ReadBytes(EXTREG__XAXIS_WIDEZMIN, &widezmin, 1);
- ERA_ReadBytes(0x0168, &temp, 1);
- ERA_WriteByte(0x0168, 0x03);
- ERA_ReadBytes(0x0168, &temp, 1);
+ ERA_ReadBytes(EXTREG__YAXIS_WIDEZMIN, &widezmin, 1);
+ ERA_WriteByte(EXTREG__YAXIS_WIDEZMIN, 0x03); // magic number from Cirque sample code
+ ERA_ReadBytes(EXTREG__YAXIS_WIDEZMIN, &widezmin, 1);
}
-/* Pinnacle-based TM040040 Functions */
+// Perform calibration
+void cirque_pinnacle_calibrate(void) {
+ uint8_t calconfig;
+ uint16_t timeout_timer;
+
+ RAP_ReadBytes(HOSTREG__CALCONFIG1, &calconfig, 1);
+ calconfig |= HOSTREG__CALCONFIG1__CALIBRATE;
+ RAP_Write(HOSTREG__CALCONFIG1, calconfig);
+
+ // Calibration takes ~100ms according to GT-AN-090624, doubling the timeout just to be safe
+ timeout_timer = timer_read();
+ do {
+ RAP_ReadBytes(HOSTREG__CALCONFIG1, &calconfig, 1);
+ } while ((calconfig & HOSTREG__CALCONFIG1__CALIBRATE) && (timer_elapsed(timeout_timer) <= 200));
+
+ cirque_pinnacle_clear_flags();
+}
+
+// Enable/disable cursor smoothing, smoothing is enabled by default
+void cirque_pinnacle_cursor_smoothing(bool enable) {
+ uint8_t feedconfig3;
+
+ RAP_ReadBytes(HOSTREG__FEEDCONFIG3, &feedconfig3, 1);
+ if (enable) {
+ feedconfig3 &= ~HOSTREG__FEEDCONFIG3__DISABLE_CROSS_RATE_SMOOTHING;
+ } else {
+ feedconfig3 |= HOSTREG__FEEDCONFIG3__DISABLE_CROSS_RATE_SMOOTHING;
+ }
+ RAP_Write(HOSTREG__FEEDCONFIG3, feedconfig3);
+}
+
+/* Pinnacle-based TM040040/TM035035/TM023023 Functions */
void cirque_pinnacle_init(void) {
#if defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
spi_init();
@@ -200,39 +206,71 @@ void cirque_pinnacle_init(void) {
#endif
touchpad_init = true;
+
// Host clears SW_CC flag
cirque_pinnacle_clear_flags();
- // Host configures bits of registers 0x03 and 0x05
- RAP_Write(SYSCONFIG_1, SYSCONFIG_1_VALUE);
- RAP_Write(FEEDCONFIG_2, FEEDCONFIG_2_VALUE);
+ // send a RESET command now, in case QMK had a soft-reset without a power cycle
+ RAP_Write(HOSTREG__SYSCONFIG1, HOSTREG__SYSCONFIG1__RESET);
+ wait_ms(30); // Pinnacle needs 10-15ms to boot, so wait long enough before configuring
+ RAP_Write(HOSTREG__SYSCONFIG1, HOSTREG__SYSCONFIG1_DEFVAL);
+ wait_us(50);
+
+ // FeedConfig2 (Feature flags for Relative Mode Only)
+ RAP_Write(HOSTREG__FEEDCONFIG2, HOSTREG__FEEDCONFIG2_DEFVAL);
- // Host enables preferred output mode (absolute)
- RAP_Write(FEEDCONFIG_1, FEEDCONFIG_1_VALUE);
+ // FeedConfig1 (Data Output Flags)
+ RAP_Write(HOSTREG__FEEDCONFIG1, CIRQUE_PINNACLE_POSITION_MODE ? HOSTREG__FEEDCONFIG1__DATA_TYPE__REL0_ABS1 : HOSTREG__FEEDCONFIG1_DEFVAL);
- // Host sets z-idle packet count to 5 (default is 30)
- RAP_Write(Z_IDLE_COUNT, Z_IDLE_COUNT_VALUE);
+ // Host sets z-idle packet count to 5 (default is 0x1E/30)
+ RAP_Write(HOSTREG__ZIDLE, 5);
- cirque_pinnacle_set_adc_attenuation(0xFF);
+ cirque_pinnacle_set_adc_attenuation(CIRQUE_PINNACLE_ATTENUATION);
+#ifdef CIRQUE_PINNACLE_CURVED_OVERLAY
cirque_pinnacle_tune_edge_sensitivity();
+#endif
+ // Force a calibration after setting ADC attenuation
+ cirque_pinnacle_calibrate();
+
cirque_pinnacle_enable_feed(true);
}
-// Reads XYZ data from Pinnacle registers 0x14 through 0x17
-// Stores result in pinnacle_data_t struct with xValue, yValue, and zValue members
pinnacle_data_t cirque_pinnacle_read_data(void) {
- uint8_t data[6] = {0};
- pinnacle_data_t result = {0};
- RAP_ReadBytes(PACKET_BYTE_0, data, 6);
+ uint8_t data_ready = 0;
+ uint8_t data[6] = {0};
+ pinnacle_data_t result = {0};
+
+ // Check if there is valid data available
+ RAP_ReadBytes(HOSTREG__STATUS1, &data_ready, 1);
+ if ((data_ready & HOSTREG__STATUS1__DATA_READY) == 0) {
+ // no data available yet
+ result.valid = false; // be explicit
+ return result;
+ }
- cirque_pinnacle_clear_flags();
+ // Read all data bytes
+ RAP_ReadBytes(HOSTREG__PACKETBYTE_0, data, 6);
- result.buttonFlags = data[0] & 0x3F;
- result.xValue = data[2] | ((data[4] & 0x0F) << 8);
- result.yValue = data[3] | ((data[4] & 0xF0) << 4);
- result.zValue = data[5] & 0x3F;
+ // Get ready for the next data sample
+ cirque_pinnacle_clear_flags();
- result.touchDown = (result.xValue != 0 || result.yValue != 0);
+#if CIRQUE_PINNACLE_POSITION_MODE
+ // Decode data for absolute mode
+ // Register 0x13 is unused in this mode (palm detection area)
+ result.buttonFlags = data[0] & 0x3F; // bit0 to bit5 are switch 0-5, only hardware button presses (from input pin on the Pinnacle chip)
+ result.xValue = data[2] | ((data[4] & 0x0F) << 8); // merge high and low bits for X
+ result.yValue = data[3] | ((data[4] & 0xF0) << 4); // merge high and low bits for Y
+ result.zValue = data[5] & 0x3F; // Z is only lower 6 bits, upper 2 bits are reserved/unused
+ result.touchDown = (result.xValue != 0 || result.yValue != 0); // (0,0) is a "magic coordinate" to indicate "finger touched down"
+#else
+ // Decode data for relative mode
+ // Registers 0x16 and 0x17 are unused in this mode
+ result.buttons = data[0] & 0x07; // bit0 = primary button, bit1 = secondary button, bit2 = auxilary button, if Taps enabled then also software-recognized taps are reported
+ result.xDelta = data[1];
+ result.yDelta = data[2];
+ result.wheelCount = data[3];
+#endif
+ result.valid = true;
return result;
}
diff --git a/drivers/sensors/cirque_pinnacle.h b/drivers/sensors/cirque_pinnacle.h
index c8cb360e03..1c9bf06fd3 100644
--- a/drivers/sensors/cirque_pinnacle.h
+++ b/drivers/sensors/cirque_pinnacle.h
@@ -2,26 +2,23 @@
#pragma once
+#include "cirque_pinnacle_regdefs.h"
#include <stdint.h>
#include <stdbool.h>
-// Convenient way to store and access measurements
-typedef struct {
- uint16_t xValue;
- uint16_t yValue;
- uint16_t zValue;
- uint8_t buttonFlags;
- bool touchDown;
-} pinnacle_data_t;
+#ifndef CIRQUE_PINNACLE_TIMEOUT
+# define CIRQUE_PINNACLE_TIMEOUT 20 // I2C timeout in milliseconds
+#endif
-void cirque_pinnacle_init(void);
-pinnacle_data_t cirque_pinnacle_read_data(void);
-void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution);
-uint16_t cirque_pinnacle_get_scale(void);
-void cirque_pinnacle_set_scale(uint16_t scale);
+#define CIRQUE_PINNACLE_ABSOLUTE_MODE 1
+#define CIRQUE_PINNACLE_RELATIVE_MODE 0
+#ifndef CIRQUE_PINNACLE_POSITION_MODE
+# define CIRQUE_PINNACLE_POSITION_MODE CIRQUE_PINNACLE_ABSOLUTE_MODE
+#endif
-#ifndef CIRQUE_PINNACLE_TIMEOUT
-# define CIRQUE_PINNACLE_TIMEOUT 20
+#define CIRQUE_PINNACLE_DEFAULT_SCALE 1024
+#ifndef CIRQUE_PINNACLE_DIAMETER_MM
+# define CIRQUE_PINNACLE_DIAMETER_MM 40
#endif
// Coordinate scaling values
@@ -43,12 +40,14 @@ void cirque_pinnacle_set_scale(uint16_t scale);
#ifndef CIRQUE_PINNACLE_Y_RANGE
# define CIRQUE_PINNACLE_Y_RANGE (CIRQUE_PINNACLE_Y_UPPER - CIRQUE_PINNACLE_Y_LOWER)
#endif
-
+#if !defined(POINTING_DEVICE_TASK_THROTTLE_MS)
+# define POINTING_DEVICE_TASK_THROTTLE_MS 10 // Cirque Pinnacle in normal operation produces data every 10ms. Advanced configuration for pen/stylus usage might require lower values.
+#endif
#if defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c)
# include "i2c_master.h"
// Cirque's 7-bit I2C Slave Address
# ifndef CIRQUE_PINNACLE_ADDR
-# define CIRQUE_PINNACLE_ADDR 0x2A
+# define CIRQUE_PINNACLE_ADDR I2C_ADDRESS_DEFAULT
# endif
#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
# include "spi_master.h"
@@ -72,3 +71,32 @@ void cirque_pinnacle_set_scale(uint16_t scale);
# endif
# endif
#endif
+
+#define DIVIDE_UNSIGNED_ROUND(numerator, denominator) (((numerator) + ((denominator) / 2)) / (denominator))
+#define CIRQUE_PINNACLE_INCH_TO_PX(inch) (DIVIDE_UNSIGNED_ROUND((inch) * (uint32_t)CIRQUE_PINNACLE_DIAMETER_MM * 10, 254))
+#define CIRQUE_PINNACLE_PX_TO_INCH(px) (DIVIDE_UNSIGNED_ROUND((px) * (uint32_t)254, CIRQUE_PINNACLE_DIAMETER_MM * 10))
+
+// Convenient way to store and access measurements
+typedef struct {
+ bool valid; // true if valid data was read, false if no data was ready
+#if CIRQUE_PINNACLE_POSITION_MODE
+ uint16_t xValue;
+ uint16_t yValue;
+ uint16_t zValue;
+ uint8_t buttonFlags;
+ bool touchDown;
+#else
+ uint8_t xDelta;
+ uint8_t yDelta;
+ uint8_t wheelCount;
+ uint8_t buttons;
+#endif
+} pinnacle_data_t;
+
+void cirque_pinnacle_init(void);
+void cirque_pinnacle_calibrate(void);
+void cirque_pinnacle_cursor_smoothing(bool enable);
+pinnacle_data_t cirque_pinnacle_read_data(void);
+void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution);
+uint16_t cirque_pinnacle_get_scale(void);
+void cirque_pinnacle_set_scale(uint16_t scale);
diff --git a/drivers/sensors/cirque_pinnacle_gestures.c b/drivers/sensors/cirque_pinnacle_gestures.c
new file mode 100644
index 0000000000..70f16527ba
--- /dev/null
+++ b/drivers/sensors/cirque_pinnacle_gestures.c
@@ -0,0 +1,227 @@
+/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
+ * Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
+ *
+ * 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 <stdlib.h>
+#include <lib/lib8tion/lib8tion.h>
+#include "cirque_pinnacle_gestures.h"
+#include "pointing_device.h"
+#include "timer.h"
+#include "wait.h"
+
+#if defined(CIRQUE_PINNACLE_TAP_ENABLE) || defined(CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE)
+static cirque_pinnacle_features_t features = {.tap_enable = true, .circular_scroll_enable = true};
+#endif
+
+#ifdef CIRQUE_PINNACLE_TAP_ENABLE
+static trackpad_tap_context_t tap;
+
+static report_mouse_t trackpad_tap(report_mouse_t mouse_report, pinnacle_data_t touchData) {
+ if (touchData.touchDown != tap.touchDown) {
+ tap.touchDown = touchData.touchDown;
+ if (!touchData.zValue) {
+ if (timer_elapsed(tap.timer) < CIRQUE_PINNACLE_TAPPING_TERM && tap.timer != 0) {
+ mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
+ pointing_device_set_report(mouse_report);
+ pointing_device_send();
+# if TAP_CODE_DELAY > 0
+ wait_ms(TAP_CODE_DELAY);
+# endif
+ mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
+ pointing_device_set_report(mouse_report);
+ pointing_device_send();
+ }
+ }
+ tap.timer = timer_read();
+ }
+ if (timer_elapsed(tap.timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
+ tap.timer = 0;
+ }
+
+ return mouse_report;
+}
+
+void cirque_pinnacle_enable_tap(bool enable) {
+ features.tap_enable = enable;
+}
+#endif
+
+#ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
+/* To set a trackpad exclusively as scroll wheel: outer_ring_pct = 100, trigger_px = 0, trigger_ang = 0 */
+static circular_scroll_context_t scroll = {.config = {.outer_ring_pct = 33,
+ .trigger_px = 16,
+ .trigger_ang = 9102, /* 50 degrees */
+ .wheel_clicks = 18}};
+
+static inline uint16_t atan2_16(int32_t dy, int32_t dx) {
+ if (dy == 0) {
+ if (dx >= 0) {
+ return 0;
+ } else {
+ return 32768;
+ }
+ }
+
+ int32_t abs_y = dy > 0 ? dy : -dy;
+ int16_t a;
+
+ if (dx >= 0) {
+ a = 8192 - (8192 * (dx - abs_y) / (dx + abs_y));
+ } else {
+ a = 24576 - (8192 * (dx + abs_y) / (abs_y - dx));
+ }
+
+ if (dy < 0) {
+ return -a; // negate if in quad III or IV
+ }
+ return a;
+}
+
+static circular_scroll_t circular_scroll(pinnacle_data_t touchData) {
+ circular_scroll_t report = {0, 0, false};
+ int8_t x, y, wheel_clicks;
+ uint8_t center = 256 / 2, mag;
+ int16_t ang, dot, det, opposite_side, adjacent_side;
+ uint16_t scale = cirque_pinnacle_get_scale();
+
+ if (touchData.zValue) {
+ /*
+ * Place origin at center of trackpad, treat coordinates as vectors.
+ * Scale to fixed int8_t size; angles are independent of resolution.
+ */
+ if (scale) {
+ x = (int8_t)((int32_t)touchData.xValue * 256 / scale - center);
+ y = (int8_t)((int32_t)touchData.yValue * 256 / scale - center);
+ } else {
+ x = 0;
+ y = 0;
+ }
+
+ /* Check if first touch */
+ if (!scroll.z) {
+ report.suppress_touch = false;
+ /* Check if touch falls within outer ring */
+ mag = sqrt16(x * x + y * y);
+ if (mag * 100 / center >= 100 - scroll.config.outer_ring_pct) {
+ scroll.state = SCROLL_DETECTING;
+ scroll.x = x;
+ scroll.y = y;
+ scroll.mag = mag;
+ /*
+ * Decide scroll axis:
+ * Vertical if started from righ half
+ * Horizontal if started from left half
+ * Flipped for left-handed
+ */
+# if defined(POINTING_DEVICE_ROTATION_90)
+ scroll.axis = y < 0;
+# elif defined(POINTING_DEVICE_ROTATION_180)
+ scroll.axis = x > 0;
+# elif defined(POINTING_DEVICE_ROTATION_270)
+ scroll.axis = y > 0;
+# else
+ scroll.axis = x < 0;
+# endif
+ }
+ } else if (scroll.state == SCROLL_DETECTING) {
+ report.suppress_touch = true;
+ /* Already detecting scroll, check movement from touchdown location */
+ mag = sqrt16((x - scroll.x) * (x - scroll.x) + (y - scroll.y) * (y - scroll.y));
+ if (mag >= scroll.config.trigger_px) {
+ /*
+ * Find angle of movement.
+ * 0 degrees here means movement towards center of circle
+ */
+ dot = scroll.x * x + scroll.y * y;
+ det = scroll.x * y - scroll.y * x;
+ opposite_side = abs(det); /* Based on scalar rejection */
+ adjacent_side = abs(scroll.mag * scroll.mag - abs(dot)); /* Based on scalar projection */
+ ang = (int16_t)atan2_16(opposite_side, adjacent_side);
+ if (ang < scroll.config.trigger_ang) {
+ /* Not a scroll, release coordinates */
+ report.suppress_touch = false;
+ scroll.state = NOT_SCROLL;
+ } else {
+ /* Scroll detected */
+ scroll.state = SCROLL_VALID;
+ }
+ }
+ }
+ if (scroll.state == SCROLL_VALID) {
+ report.suppress_touch = true;
+ dot = scroll.x * x + scroll.y * y;
+ det = scroll.x * y - scroll.y * x;
+ ang = (int16_t)atan2_16(det, dot);
+ wheel_clicks = ((int32_t)ang * scroll.config.wheel_clicks) / 65536;
+ if (wheel_clicks >= 1 || wheel_clicks <= -1) {
+ if (scroll.config.left_handed) {
+ if (scroll.axis == 0) {
+ report.h = -wheel_clicks;
+ } else {
+ report.v = wheel_clicks;
+ }
+ } else {
+ if (scroll.axis == 0) {
+ report.v = -wheel_clicks;
+ } else {
+ report.h = wheel_clicks;
+ }
+ }
+ scroll.x = x;
+ scroll.y = y;
+ }
+ }
+ }
+
+ scroll.z = touchData.zValue;
+ if (!scroll.z) scroll.state = SCROLL_UNINITIALIZED;
+
+ return report;
+}
+
+void cirque_pinnacle_enable_circular_scroll(bool enable) {
+ features.circular_scroll_enable = enable;
+}
+
+void cirque_pinnacle_configure_circular_scroll(uint8_t outer_ring_pct, uint8_t trigger_px, uint16_t trigger_ang, uint8_t wheel_clicks, bool left_handed) {
+ scroll.config.outer_ring_pct = outer_ring_pct;
+ scroll.config.trigger_px = trigger_px;
+ scroll.config.trigger_ang = trigger_ang;
+ scroll.config.wheel_clicks = wheel_clicks;
+ scroll.config.left_handed = left_handed;
+}
+#endif
+
+bool cirque_pinnacle_gestures(report_mouse_t* mouse_report, pinnacle_data_t touchData) {
+ bool suppress_mouse_update = false;
+
+#ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
+ circular_scroll_t scroll_report;
+ if (features.circular_scroll_enable) {
+ scroll_report = circular_scroll(touchData);
+ mouse_report->v = scroll_report.v;
+ mouse_report->h = scroll_report.h;
+ suppress_mouse_update = scroll_report.suppress_touch;
+ }
+#endif
+
+#ifdef CIRQUE_PINNACLE_TAP_ENABLE
+ if (features.tap_enable) {
+ *mouse_report = trackpad_tap(*mouse_report, touchData);
+ }
+#endif
+
+ return suppress_mouse_update;
+}
diff --git a/drivers/sensors/cirque_pinnacle_gestures.h b/drivers/sensors/cirque_pinnacle_gestures.h
new file mode 100644
index 0000000000..f39782b467
--- /dev/null
+++ b/drivers/sensors/cirque_pinnacle_gestures.h
@@ -0,0 +1,107 @@
+/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
+ * Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
+ *
+ * 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 "cirque_pinnacle.h"
+#include "report.h"
+
+typedef struct {
+ bool tap_enable;
+ bool circular_scroll_enable;
+} cirque_pinnacle_features_t;
+
+#ifdef CIRQUE_PINNACLE_TAP_ENABLE
+# ifndef CIRQUE_PINNACLE_TAPPING_TERM
+# include "action.h"
+# include "action_tapping.h"
+# define CIRQUE_PINNACLE_TAPPING_TERM GET_TAPPING_TERM(KC_BTN1, &(keyrecord_t){})
+# endif
+# ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE
+# define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8)
+# endif
+
+typedef struct {
+ uint16_t timer;
+ bool touchDown;
+} trackpad_tap_context_t;
+
+/* Enable/disable tap gesture */
+void cirque_pinnacle_enable_tap(bool enable);
+#endif
+
+#ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
+typedef enum {
+ SCROLL_UNINITIALIZED,
+ SCROLL_DETECTING,
+ SCROLL_VALID,
+ NOT_SCROLL,
+} circular_scroll_status_t;
+
+typedef struct {
+ int8_t v;
+ int8_t h;
+ bool suppress_touch;
+} circular_scroll_t;
+
+typedef struct {
+ uint8_t outer_ring_pct; /* Width of outer ring, given as a percentage of the radius */
+ uint8_t trigger_px; /* Amount of movement before triggering scroll validation, in pixels 0~127 */
+ uint16_t trigger_ang; /* Angle required to validate scroll, in radians where pi = 32768 */
+ uint8_t wheel_clicks; /* How many clicks to report in a circle */
+ bool left_handed; /* Whether scrolling should be flipped for left handed use */
+} circular_scroll_config_t;
+
+typedef struct {
+ circular_scroll_config_t config;
+ circular_scroll_status_t state;
+ uint8_t mag;
+ int8_t x;
+ int8_t y;
+ uint16_t z;
+ bool axis;
+} circular_scroll_context_t;
+
+/* Enable/disable circular scroll gesture */
+void cirque_pinnacle_enable_circular_scroll(bool enable);
+
+/*
+ * Configure circular scroll gesture.
+ * Trackpad can be configured to act exclusively as a scroll wheel with outer_ring_pct = 0, trigger_px = 0, trigger_ang = 0.
+ * @param outer_ring_pct Width of outer ring from which to begin scroll validation, given as a percentage of the radius.
+ * @param trigger_px Amount of movement before triggering scroll validation. Expressed in pixels, trackpad coordinates are scaled to radius of 128 pixels for circular scroll.
+ * @param triger_ang Angle required to validate scroll, angle smaller than this will invalidate scroll. In radians where pi = 32768, 0 means movement towards center of trackpad, 16384 means movement perpendicular to center.
+ * @param wheel_clicks Number of scroll wheel clicks to report in a full rotation.
+ * @param left_handed Whether scrolling should be flipped for left-handed use.
+ */
+void cirque_pinnacle_configure_circular_scroll(uint8_t outer_ring_pct, uint8_t trigger_px, uint16_t trigger_ang, uint8_t wheel_clicks, bool left_handed);
+#endif
+
+#ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
+/* Implementation in pointing_device_drivers.c */
+
+/* Enable/disable inertial cursor */
+void cirque_pinnacle_enable_cursor_glide(bool enable);
+
+/*
+ * Configure inertial cursor.
+ * @param trigger_px Movement required to trigger cursor glide, set this to non-zero if you have some amount of hover.
+ */
+void cirque_pinnacle_configure_cursor_glide(float trigger_px);
+#endif
+
+/* Process available gestures */
+bool cirque_pinnacle_gestures(report_mouse_t* mouse_report, pinnacle_data_t touchData);
diff --git a/drivers/sensors/cirque_pinnacle_i2c.c b/drivers/sensors/cirque_pinnacle_i2c.c
index 8a38f1dcea..b328dd9a7a 100644
--- a/drivers/sensors/cirque_pinnacle_i2c.c
+++ b/drivers/sensors/cirque_pinnacle_i2c.c
@@ -19,7 +19,7 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) {
i2c_writeReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, NULL, 0, CIRQUE_PINNACLE_TIMEOUT);
if (i2c_readReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, data, count, CIRQUE_PINNACLE_TIMEOUT) != I2C_STATUS_SUCCESS) {
#ifdef CONSOLE_ENABLE
- dprintf("error right touchpad\n");
+ dprintf("error cirque_pinnacle i2c_readReg\n");
#endif
touchpad_init = false;
}
@@ -34,7 +34,7 @@ void RAP_Write(uint8_t address, uint8_t data) {
if (touchpad_init) {
if (i2c_writeReg(CIRQUE_PINNACLE_ADDR << 1, cmdByte, &data, sizeof(data), CIRQUE_PINNACLE_TIMEOUT) != I2C_STATUS_SUCCESS) {
#ifdef CONSOLE_ENABLE
- dprintf("error right touchpad\n");
+ dprintf("error cirque_pinnacle i2c_writeReg\n");
#endif
touchpad_init = false;
}
diff --git a/drivers/sensors/cirque_pinnacle_regdefs.h b/drivers/sensors/cirque_pinnacle_regdefs.h
new file mode 100644
index 0000000000..993da1e757
--- /dev/null
+++ b/drivers/sensors/cirque_pinnacle_regdefs.h
@@ -0,0 +1,405 @@
+// Copyright (c) 2018 Cirque Corp. Restrictions apply. See: www.cirque.com/sw-license
+// based on https://github.com/cirque-corp/Cirque_Pinnacle_1CA027/tree/master/Additional_Examples
+// with modifications and changes for QMK
+// refer to documentation: Gen2 and Gen3 (Pinnacle ASIC) at https://www.cirque.com/gen2gen3-asic-details
+
+#pragma once
+
+// clang-format off
+
+#define HostReg__0 (0x00)
+#define HostReg__1 (0x01)
+#define HostReg__2 (0x02)
+#define HostReg__3 (0x03)
+#define HostReg__4 (0x04)
+#define HostReg__5 (0x05)
+#define HostReg__6 (0x06)
+#define HostReg__7 (0x07)
+#define HostReg__8 (0x08)
+#define HostReg__9 (0x09)
+#define HostReg__10 (0x0A)
+#define HostReg__11 (0x0B)
+#define HostReg__12 (0x0C)
+#define HostReg__13 (0x0D)
+#define HostReg__14 (0x0E)
+#define HostReg__15 (0x0F)
+#define HostReg__16 (0x10)
+#define HostReg__17 (0x11)
+#define HostReg__18 (0x12)
+#define HostReg__19 (0x13)
+#define HostReg__20 (0x14)
+#define HostReg__21 (0x15)
+#define HostReg__22 (0x16)
+#define HostReg__23 (0x17)
+#define HostReg__24 (0x18)
+#define HostReg__25 (0x19)
+#define HostReg__26 (0x1A)
+#define HostReg__27 (0x1B)
+#define HostReg__28 (0x1C)
+#define HostReg__29 (0x1D)
+#define HostReg__30 (0x1E)
+#define HostReg__31 (0x1F)
+
+// ---------------- Register Assignments -------------------------------------
+
+/*--------------------------------------------------------------------------*\
+ Chip ID / Version
+\*--------------------------------------------------------------------------*/
+// Chip ID Register
+#define HOSTREG__CHIPID HostReg__0
+
+// Chip Version Register
+#define HOSTREG__VERSION HostReg__1
+
+/*--------------------------------------------------------------------------*\
+ Status Register
+\*--------------------------------------------------------------------------*/
+// Status 1 Register -- MUST BE HOSTREG__2
+#define HOSTREG__STATUS1 HostReg__2
+# define HOSTREG__STATUS1__DATA_READY 0x04
+# define HOSTREG__STATUS1__COMMAND_COMPLETE 0x08
+#define HOSTREG__STATUS1_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ System Config Register
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__SYSCONFIG1 HostReg__3
+# define HOSTREG__SYSCONFIG1__RESET 0x01
+# define HOSTREG__SYSCONFIG1__STANDBY 0x02
+# define HOSTREG__SYSCONFIG1__AUTO_SLEEP 0x04
+# define HOSTREG__SYSCONFIG1__TRACK_DISABLE 0x08
+# define HOSTREG__SYSCONFIG1__ANYMEAS_ENABLE 0x10
+# define HOSTREG__SYSCONFIG1__GPIO_CTRL_ENABLE 0x20
+# define HOSTREG__SYSCONFIG1__WAKEUP_TOGGLE 0x40
+# define HOSTREG__SYSCONFIG1__FORCE_WAKEUP 0x80
+#define HOSTREG__SYSCONFIG1_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ Feed Config Registers
+\*--------------------------------------------------------------------------*/
+// Feed Config Register1
+#define HOSTREG__FEEDCONFIG1 HostReg__4
+# define HOSTREG__FEEDCONFIG1__FEED_ENABLE 0x01
+# define HOSTREG__FEEDCONFIG1__DATA_TYPE__REL0_ABS1 0x02
+# define HOSTREG__FEEDCONFIG1__FILTER_DISABLE 0x04
+# define HOSTREG__FEEDCONFIG1__X_AXIS_DISABLE 0x08
+# define HOSTREG__FEEDCONFIG1__Y_AXIS_DISABLE 0x10
+# define HOSTREG__FEEDCONFIG1__AXIS_FOR_Z__Y0_X1 0x20
+# define HOSTREG__FEEDCONFIG1__X_DATA_INVERT 0x40
+# define HOSTREG__FEEDCONFIG1__Y_DATA_INVERT 0x80
+#define HOSTREG__FEEDCONFIG1_DEFVAL 0x00
+
+// Feed Config Register2
+#define HOSTREG__FEEDCONFIG2 HostReg__5
+# define HOSTREG__FEEDCONFIG2__INTELLIMOUSE_MODE 0x01
+# define HOSTREG__FEEDCONFIG2__ALL_TAP_DISABLE 0x02
+# define HOSTREG__FEEDCONFIG2__SECONDARY_TAP_DISABLE 0x04
+# define HOSTREG__FEEDCONFIG2__SCROLL_DISABLE 0x08
+# define HOSTREG__FEEDCONFIG2__GLIDE_EXTEND_DISABLE 0x10
+# define HOSTREG__FEEDCONFIG2__PALM_BEFORE_Z_ENABLE 0x20
+# define HOSTREG__FEEDCONFIG2__BUTNS_46_SCROLL_5_MIDDLE 0x40
+# define HOSTREG__FEEDCONFIG2__SWAP_XY_RELATIVE 0x80
+#define HOSTREG__FEEDCONFIG2_DEFVAL 0x00
+
+// Feed Config Register3
+#define HOSTREG__FEEDCONFIG3 HostReg__6
+# define HOSTREG__FEEDCONFIG3__BTNS_456_TO_123_IN_REL 0x01
+# define HOSTREG__FEEDCONFIG3__DISABLE_CROSS_RATE_SMOOTHING 0x02
+# define HOSTREG__FEEDCONFIG3__DISABLE_PALM_NERD_MEAS 0x04
+# define HOSTREG__FEEDCONFIG3__DISABLE_NOISE_AVOIDANCE 0x08
+# define HOSTREG__FEEDCONFIG3__DISABLE_WRAP_LOCKOUT 0x10
+# define HOSTREG__FEEDCONFIG3__DISABLE_DYNAMIC_EMI_ADJUST 0x20
+# define HOSTREG__FEEDCONFIG3__DISABLE_HW_EMI_DETECT 0x40
+# define HOSTREG__FEEDCONFIG3__DISABLE_SW_EMI_DETECT 0x80
+#define HOSTREG__FEEDCONFIG3_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ Calibration Config
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__CALCONFIG1 HostReg__7
+# define HOSTREG__CALCONFIG1__CALIBRATE 0x01
+# define HOSTREG__CALCONFIG1__BACKGROUND_COMP_ENABLE 0x02
+# define HOSTREG__CALCONFIG1__NERD_COMP_ENABLE 0x04
+# define HOSTREG__CALCONFIG1__TRACK_ERROR_COMP_ENABLE 0x08
+# define HOSTREG__CALCONFIG1__TAP_COMP_ENABLE 0x10
+# define HOSTREG__CALCONFIG1__PALM_ERROR_COMP_ENABLE 0x20
+# define HOSTREG__CALCONFIG1__CALIBRATION_MATRIX_DISABLE 0x40
+# define HOSTREG__CALCONFIG1__FORCE_PRECALIBRATION_NOISE_CHECK 0x80
+#define HOSTREG__CALCONFIG1_DEFVAL (HOSTREG__CALCONFIG1__BACKGROUND_COMP_ENABLE | HOSTREG__CALCONFIG1__NERD_COMP_ENABLE | HOSTREG__CALCONFIG1__TRACK_ERROR_COMP_ENABLE | HOSTREG__CALCONFIG1__TAP_COMP_ENABLE | HOSTREG__CALCONFIG1__PALM_ERROR_COMP_ENABLE)
+
+/*--------------------------------------------------------------------------*\
+ PS2 Aux Control Register
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__PS2AUX_CTRL HostReg__8
+# define HOSTREG__PS2AUX_CTRL__CMD_PASSTHRU_ENABLE 0x01
+# define HOSTREG__PS2AUX_CTRL__SP_EXTENDED_MODE 0x02
+# define HOSTREG__PS2AUX_CTRL__GS_DISABLE 0x04
+# define HOSTREG__PS2AUX_CTRL__SP_DISABLE 0x08
+# define HOSTREG__PS2AUX_CTRL__GS_COORDINATE_DISABLE 0x10
+# define HOSTREG__PS2AUX_CTRL__SP_COORDINATE_DISABLE 0x20
+# define HOSTREG__PS2AUX_CTRL__DISABLE_AA00_DETECT 0x40
+# define HOSTREG__PS2AUX_CTRL__AUX_PRESENT 0x80
+#define HOSTREG__PR2AUX_CTRL_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ Sample Rate Value
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__SAMPLERATE HostReg__9
+# define HOSTREG__SAMPLERATE__10_SPS 0x0A
+# define HOSTREG__SAMPLERATE__20_SPS 0x14
+# define HOSTREG__SAMPLERATE__40_SPS 0x28
+# define HOSTREG__SAMPLERATE__60_SPS 0x3C
+# define HOSTREG__SAMPLERATE__80_SPS 0x50
+# define HOSTREG__SAMPLERATE__100_SPS 0x64
+# define HOSTREG__SAMPLERATE__200_SPS 0xC8 // 200sps not supported
+ // only for ps2 compatibility
+ // rate set to 100sps
+#define HOSTREG__SAMPLERATE_DEFVAL HOSTREG__SAMPLERATE__100_SPS
+
+/*--------------------------------------------------------------------------*\
+ Z Idle Value
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__ZIDLE HostReg__10
+#define HOSTREG__ZIDLE_DEFVAL 30 // 0x1E
+
+/*--------------------------------------------------------------------------*\
+ Z Scaler Value
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__ZSCALER HostReg__11
+#define HOSTREG__ZSCALER_DEFVAL 8 // 0x08
+
+/*--------------------------------------------------------------------------*\
+ Sleep Interval Value
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__SLEEP_INTERVAL HostReg__12
+#define HOSTREG__SLEEP_INTERVAL_DEFVAL 73 // 0x49
+
+/*--------------------------------------------------------------------------*\
+ Sleep Delay Value
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__SLEEP_DELAY HostReg__13
+#define HOSTREG__SLEEP_DELAY_DEFVAL 39 // 0x27
+
+/*--------------------------------------------------------------------------*\
+ Dynamic EMI Bad Channel Count Thresholds
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__DYNAMIC_EMI_ADJUST_THRESHOLD HostReg__14
+#define HOSTREG__DYNAMIC_EMI_ADJUST_THRESHOLD_DEFVAL 66 // 0x42
+
+/*--------------------------------------------------------------------------*\
+ Packet Registers
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__PACKETBYTE_0 HostReg__18
+#define HOSTREG__PACKETBYTE_1 HostReg__19
+#define HOSTREG__PACKETBYTE_2 HostReg__20
+#define HOSTREG__PACKETBYTE_3 HostReg__21
+#define HOSTREG__PACKETBYTE_4 HostReg__22
+#define HOSTREG__PACKETBYTE_5 HostReg__23
+
+/*--------------------------------------------------------------------------*\
+ Port A GPIO Control
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__PORTA_GPIO_CTRL HostReg__24
+#define HOSTREG__PORTA_GPIO_CTRL_DEFVAL 0xFF
+
+/*--------------------------------------------------------------------------*\
+ Port A GPIO Data
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__PORTA_GPIO_DATA HostReg__25
+#define HOSTREG__PORTA_GPIO_DATA_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ Port B GPIO Control And Data
+\*--------------------------------------------------------------------------*/
+
+#define HOSTREG__PORTB_GPIO_CTRL_DATA HostReg__26
+# define HOSTREG__PORTB_GPIO_DATA__PB0 0x01
+# define HOSTREG__PORTB_GPIO_DATA__PB1 0x02
+# define HOSTREG__PORTB_GPIO_DATA__PB2 0x04
+# define HOSTREG__PORTB_GPIO_CTRL__PB0 0x08
+# define HOSTREG__PORTB_GPIO_CTRL__PB1 0x10
+# define HOSTREG__PORTB_GPIO_CTRL__PB2 0x20
+# define HOSTREG__PORTB_GPIO_RSVD_0 0x40
+# define HOSTREG__PORTB_GPIO_READ1_WRITE0 0x80
+#define HOSTREG__PORTB_GPIO_CTRL_DATA_DEFVAL (HOSTREG__PORTB_GPIO_CTRL__PB0 | HOSTREG__PORTB_GPIO_CTRL__PB1 | HOSTREG__PORTB_GPIO_CTRL__PB2)
+
+/*--------------------------------------------------------------------------*\
+ Extended Register Access
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__EXT_REG_AXS_VALUE HostReg__27
+
+#define HOSTREG__EXT_REG_AXS_ADDR_HIGH HostReg__28
+#define HOSTREG__EXT_REG_AXS_ADDR_LOW HostReg__29
+
+#define HOSTREG__EXT_REG_AXS_CTRL HostReg__30
+# define HOSTREG__EREG_AXS__READ 0x01
+# define HOSTREG__EREG_AXS__WRITE 0x02
+# define HOSTREG__EREG_AXS__INC_ADDR_READ 0x04
+# define HOSTREG__EREG_AXS__INC_ADDR_WRITE 0x08
+# define HOSTREG__EREG_AXS__RSVD_3 0x10
+# define HOSTREG__EREG_AXS__RSVD_2 0x20
+# define HOSTREG__EREG_AXS__RSVD_1 0x40
+# define HOSTREG__EREG_AXS__RSVD_0 0x80
+
+#define HOSTREG__EXT_REG_AXS_VALUE_DEFVAL 0x00
+#define HOSTREG__EXT_REG_AXS_ADDR_HIGH_DEFVAL 0x00
+#define HOSTREG__EXT_REG_AXS_ADDR_LOW_DEFVAL 0x00
+#define HOSTREG__EXT_REG_AXS_CTRL_DEFVAL 0x00
+
+/*--------------------------------------------------------------------------*\
+ Product ID
+\*--------------------------------------------------------------------------*/
+#define HOSTREG__PRODUCT_ID HostReg__31
+
+
+
+//Some useful values
+#define I2C_ADDRESS_DEFAULT 0x2A
+#define FIRMWARE_ID 0x07
+#define FIRMWARE_VERSION 0x9D
+
+//Anymeas config options
+//First setting is HostReg 5. This sets toggle frequency (EF) and gain.
+//Gain is upper two bits (0xC0), frequency is lower 6 bits (0x3F)
+#define AnyMeas_AccumBits_ElecFreq HostReg__5
+# define ADCCNFG_ELEC_FREQ 0x3F /* Bit 4, 3, 2, 1, 0 */
+# define ADCCNFG_EF_0 0x02 // 500,000Hz
+# define ADCCNFG_EF_1 0x03 // 444,444Hz
+# define ADCCNFG_EF_2 0x04 // 400,000Hz
+# define ADCCNFG_EF_3 0x05 // 363,636Hz
+# define ADCCNFG_EF_4 0x06 // 333,333Hz
+# define ADCCNFG_EF_5 0x07 // 307,692Hz
+# define ADCCNFG_EF_6 0x09 // 267,000Hz
+# define ADCCNFG_EF_7 0x0B // 235,000Hz
+# define ADCCNFG_ACCUMBITSSELECT 0xC0 /* Bit 7, 6 */
+# define ADCCNFG_ACCBITS_17_14_0 0x00 //This is about 2x gain
+# define ADCCNFG_ACCBITS_17_15_1 0x40 //This is about 1.6x gain
+# define ADCCNFG_ACCBITS_17_2__80 0x80 //This is about 1.3x gain
+# define ADCCNFG_ACCBITS_17_2__C0 0xC0 //This is lowest gain
+//Note, all frequencies above are based on default 500ns aperture. If aperture is shorter the frequencies will be faster and if aperture is longer the frequencies will be slower.
+
+//Next is HostReg 6. This sets the sample length. There are four possible settings to bit length. All other settings are not normally used and should be a 0.
+#define AnyMeas_BitLength HostReg__6
+# define ADCCTRL_BIT_LENGTH 0x03 /* Bit 1, 0 */
+# define ADCCTRL_SAMPLES_32 0x00 //Note: this does not work.
+# define ADCCTRL_SAMPLES_128 0x01
+# define ADCCTRL_SAMPLES_256 0x02
+# define ADCCTRL_SAMPLES_512 0x03
+# define ADCCTRL_ENABLE 0x20 /* Bit 5 */
+# define ADCCTRL_INT_FLAG 0x40 /* Bit 6 */
+# define ADCCTRL_START_BUSY 0x80 /* Bit 7 */
+//The smaller the sample length the faster the measurement but the lower the SNR. For high SNR requirements 512 sample length is recommended. Alternatively, multiple 128 or 256 length measurements could be averaged.
+
+//Next is HostReg 7. This sets the sense mux. Pinnacle has 2 sense lines, Sense N and Sense P1. There is also a Sense P2 but it is not bonded out, it is only internal.
+//Signal on Sense N will be inverted from signal on Sense P1. Other than sign inversion, signal strength should be the same.
+#define AnyMeas_ADC_MuxControl HostReg__7
+# define ADCMUXCTRL_SENSEP1GATE 0x01 //Enables Sense P1. Can be combined with Sense N input or exclusivly Sense P1 alone.
+# define ADCMUXCTRL_SENSEP2GATE 0x02 //Not used.
+# define ADCMUXCTRL_SENSENGATE 0x04 //Enables Sense N. Can be combined with Sense P inputs or exclusivly Sense N alone.
+# define ADCMUXCTRL_REF0GATE 0x08 //This enables the RefCap0. This is a capacitor inside the chip that is roughly 0.25pF. It is also controlled with the toggle and polarity bits so those bits must be set properly as well in order to use it.
+# define ADCMUXCTRL_REF1GATE 0x10 //This enables the RefCap1. This is a capacitor inside the chip that is roughly 0.5pF. It is also controlled with the toggle and polarity bits so those bits must be set properly as well in order to use it.
+# define ADCMUXCTRL_OSCMEASEN 0x80 //this is a test mode for measuring the internal oscillator. It is for IC test only.
+
+//Next is HostReg 8. This contains various ADC config settings that are not likely to be used.
+#define AnyMeas_ADC_Config2 HostReg__8
+# define ADCCNFG2_ADC_CLK_SELECT 0x01 /* Bit 0 */ //If 0 use the standard 8Mhz clock. If 1 use a divide by 2, 4Mhz clock. Only used if extra slow toggle frequencies are required.
+# define ADCCNFG2_EMI_FLAG 0x02 /* Bit 1 */ //EMI flag threshold only used with internal FW. Not valid in anymeas mode.
+# define ADCCNFG2_EMI_FLAG_THRESHOLD_0 0x04 /* Bit 2 */ //EMI flag threshold only used with internal FW. Not valid in anymeas mode.
+# define ADCCNFG2_EMI_FLAG_THRESHOLD_1 0x08 /* Bit 3 */ //EMI flag threshold only used with internal FW. Not valid in anymeas mode.
+# define ADCCNFG2_DSX2_EXTEND 0x10 /* Bit 4 */ //extend one signal on the receive. Could also be helpful in situations where sensor cap is extremely high.
+# define ADCCNFG2_ETOGGLE_DELAY 0x20 /* Bit 5 */ //delay a bit before toggling electrodes. Could be helpful in situations where sensor cap is extremely high.
+
+//Next is HostReg 9. This sets the aperture length. Bottom 4 bits set the aperture width
+#define AnyMeas_ADC_AWidth HostReg__9
+# define ADCAWIDTH_AWIDTHMASK 0x0F
+# define ADCAWIDTH_APERTURE_OPEN 0x00 //does not work
+# define ADCAWIDTH_APERTURE_125NS 0x01 //does not work
+# define ADCAWIDTH_APERTURE_250NS 0x02
+# define ADCAWIDTH_APERTURE_375NS 0x03
+# define ADCAWIDTH_APERTURE_500NS 0x04
+# define ADCAWIDTH_APERTURE_625NS 0x05
+# define ADCAWIDTH_APERTURE_750NS 0x06
+# define ADCAWIDTH_APERTURE_875NS 0x07
+# define ADCAWIDTH_APERTURE_1000NS 0x08
+# define ADCAWIDTH_APERTURE_1125NS 0x09
+# define ADCAWIDTH_APERTURE_1250NS 0x0A
+# define ADCAWIDTH_APERTURE_1375NS 0x0B
+# define ADCAWIDTH_APERTURE_1500NS 0x0C
+# define ADCAWIDTH_APERTURE_1625NS 0x0D
+# define ADCAWIDTH_APERTURE_1750NS 0x0E
+# define ADCAWIDTH_APERTURE_1875NS 0x0F
+# define ADCAWIDTH_AWIDTHPLUSHALF 0x10
+# define ADCAWIDTH_AOPEN 0x20
+# define ADCAWIDTH_W2WAIT 0x40
+
+//next two registers give the high and low bytes to the 16 bit address where Pinnacle will pull the measurement data. Normally these addresses are within the base 32 registers.
+#define AnyMeas_pADCMeasInfoStart_High_Byte HostReg__10
+#define AnyMeas_pADCMeasInfoStart_Low_Byte HostReg__11
+
+//Next is the measurement index, this sets the measurement state machine to the start and should be a 0 at start.
+#define AnyMeas_MeasIndex HostReg__12
+# define ANYMEASSTATE_RESET_START 0x00
+# define ANYMEASSTATE_START_MEASUREMENT 0x01
+# define ANYMEASSTATE_WAIT_FOR_MEASUREMENT_AND_HOST 0x02
+
+//next is the state itself of the measurement, should always be 0.
+#define AnyMeas_State HostReg__13
+
+//next is the number of measurements. Use 0x80 to repeat the single measurement or repeat a number of measurements.
+//0x40 will turn the ADC off after measurements. This will result in longer startup time for a subsequent measurement, but lower idle power draw.
+#define AnyMeas_Control_NumMeas HostReg__14
+# define ANYMEAS_CONTROL__NUM_MEAS_MASK 0x3F
+# define ANYMEAS_CONTROL__ADC_POST_MEAS_PWR 0x40
+# define ANYMEAS_CONTROL__REPEAT 0x80
+
+//These are not used
+#define AnyMeas_pADCMeasInfo_High_Byte HostReg__15
+#define AnyMeas_pADCMeasInfo_Low_Byte HostReg__16
+
+//16 bit result of measurement will be found in these two registers.
+#define AnyMeas_Result_High_Byte HostReg__17
+#define AnyMeas_Result_Low_Byte HostReg__18
+
+// ---------------- Extended Register Assignments ----------------------------
+/*--------------------------------------------------------------------------*\
+ ADC Mux Control
+\*--------------------------------------------------------------------------*/
+#define EXTREG__ADCMUX_CTRL 0x00EB
+# define EXTREG__ADCMUX_CTRL__SNSP_ENABLE 0x01
+# define EXTREG__ADCMUX_CTRL__SNSN_ENABLE 0x04
+
+/*--------------------------------------------------------------------------*\
+ Timer Reload Registers
+\*--------------------------------------------------------------------------*/
+#define EXTREG__PACKET_TIMER_RELOAD 0x019F
+#define EXTREG__TRACK_TIMER_RELOAD 0x019E
+// These two registers should have matching content.
+# define EXTREG__TIMER_RELOAD__300_SPS 0x06
+# define EXTREG__TIMER_RELOAD__200_SPS 0x09
+# define EXTREG__TIMER_RELOAD__100_SPS 0x13
+
+/*--------------------------------------------------------------------------*\
+ Track ADC Config
+\*--------------------------------------------------------------------------*/
+#define EXTREG__TRACK_ADCCONFIG 0x0187
+// ADC-attenuation settings (held in BIT_7 and BIT_6)
+// 1X = most sensitive, 4X = least sensitive
+# define EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_MASK 0x3F
+# define EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_1X 0x00
+# define EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_2X 0x40
+# define EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_3X 0x80
+# define EXTREG__TRACK_ADCCONFIG__ADC_ATTENUATE_4X 0xC0
+#define EXTREG__TRACK_ADCCONFIG_DEFVAL 0x4E
+
+
+/*--------------------------------------------------------------------------*\
+ Tune Edge Sensitivity
+\*--------------------------------------------------------------------------*/
+// These registers are not detailed in any publically available documentation
+// Names inferred from debug prints in https://github.com/cirque-corp/Cirque_Pinnacle_1CA027/blob/master/Circular_Trackpad
+#define EXTREG__XAXIS_WIDEZMIN 0x0149
+#define EXTREG__YAXIS_WIDEZMIN 0x0168
+#define EXTREG__XAXIS_WIDEZMIN_DEFVAL 0x06
+#define EXTREG__YAXIS_WIDEZMIN_DEFVAL 0x05
+
+// clang-format on
diff --git a/drivers/sensors/cirque_pinnacle_spi.c b/drivers/sensors/cirque_pinnacle_spi.c
index 34c77df07b..bd980fc863 100644
--- a/drivers/sensors/cirque_pinnacle_spi.c
+++ b/drivers/sensors/cirque_pinnacle_spi.c
@@ -25,7 +25,7 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) {
}
} else {
#ifdef CONSOLE_ENABLE
- dprintf("error right touchpad\n");
+ dprintf("error cirque_pinnacle spi_start read\n");
#endif
touchpad_init = false;
}
@@ -43,7 +43,7 @@ void RAP_Write(uint8_t address, uint8_t data) {
spi_write(data);
} else {
#ifdef CONSOLE_ENABLE
- dprintf("error right touchpad\n");
+ dprintf("error cirque_pinnacle spi_start write\n");
#endif
touchpad_init = false;
}
diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c
index 333e017a06..88a351316b 100644
--- a/drivers/sensors/pimoroni_trackball.c
+++ b/drivers/sensors/pimoroni_trackball.c
@@ -95,16 +95,3 @@ int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_di
uint16_t magnitude = (scale * offset * offset * precision) >> 7;
return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude);
}
-
-void pimoroni_trackball_adapt_values(int8_t* mouse, int16_t* offset) {
- if (*offset > 127) {
- *mouse = 127;
- *offset -= 127;
- } else if (*offset < -127) {
- *mouse = -127;
- *offset += 127;
- } else {
- *mouse = *offset;
- *offset = 0;
- }
-}
diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h
index e20ee748a7..749f381bbd 100644
--- a/drivers/sensors/pimoroni_trackball.h
+++ b/drivers/sensors/pimoroni_trackball.h
@@ -52,7 +52,6 @@ typedef struct {
void pimoroni_trackball_device_init(void);
void pimoroni_trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white);
int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale);
-void pimoroni_trackball_adapt_values(int8_t* mouse, int16_t* offset);
uint16_t pimoroni_trackball_get_cpi(void);
void pimoroni_trackball_set_cpi(uint16_t cpi);
i2c_status_t read_pimoroni_trackball(pimoroni_data_t* data);
diff --git a/drivers/serial.h b/drivers/serial.h
index 0cfdbd9959..fb91b136e7 100644
--- a/drivers/serial.h
+++ b/drivers/serial.h
@@ -27,3 +27,13 @@ void soft_serial_initiator_init(void);
void soft_serial_target_init(void);
bool soft_serial_transaction(int sstd_index);
+
+#ifdef SERIAL_DEBUG
+# include <debug.h>
+# include <print.h>
+# define serial_dprintf(...) dprintf(__VA_ARGS__)
+#else
+# define serial_dprintf(...) \
+ do { \
+ } while (0)
+#endif
diff --git a/drivers/wear_leveling/wear_leveling_flash_spi.c b/drivers/wear_leveling/wear_leveling_flash_spi.c
new file mode 100644
index 0000000000..6191f8bf09
--- /dev/null
+++ b/drivers/wear_leveling/wear_leveling_flash_spi.c
@@ -0,0 +1,101 @@
+// Copyright 2022 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <stdbool.h>
+#include <hal.h>
+#include "util.h"
+#include "timer.h"
+#include "wear_leveling.h"
+#include "wear_leveling_internal.h"
+
+#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT
+# define WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT 32
+#endif // WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT
+
+bool backing_store_init(void) {
+ bs_dprintf("Init\n");
+ flash_init();
+ return true;
+}
+
+bool backing_store_unlock(void) {
+ bs_dprintf("Unlock\n");
+ // No-op -- handled by the flash driver as it is.
+ return true;
+}
+
+bool backing_store_erase(void) {
+#ifdef WEAR_LEVELING_DEBUG_OUTPUT
+ uint32_t start = timer_read32();
+#endif
+
+ bool ret = true;
+ for (int i = 0; i < (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT); ++i) {
+ flash_status_t status = flash_erase_block(((WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) + i) * (EXTERNAL_FLASH_BLOCK_SIZE));
+ if (status != FLASH_STATUS_SUCCESS) {
+ ret = false;
+ break;
+ }
+ }
+
+ bs_dprintf("Backing store erase took %ldms to complete\n", ((long)(timer_read32() - start)));
+ return ret;
+}
+
+bool backing_store_write(uint32_t address, backing_store_int_t value) {
+ return backing_store_write_bulk(address, &value, 1);
+}
+
+bool backing_store_lock(void) {
+ bs_dprintf("Lock \n");
+ // No-op -- handled by the flash driver as it is.
+ return true;
+}
+
+bool backing_store_read(uint32_t address, backing_store_int_t *value) {
+ return backing_store_read_bulk(address, value, 1);
+}
+
+bool backing_store_read_bulk(uint32_t address, backing_store_int_t *values, size_t item_count) {
+ bs_dprintf("Read ");
+ uint32_t offset = (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) * (EXTERNAL_FLASH_BLOCK_SIZE) + address;
+ flash_status_t status = flash_read_block(offset, values, sizeof(backing_store_int_t) * item_count);
+ if (status == FLASH_STATUS_SUCCESS) {
+ for (size_t i = 0; i < item_count; ++i) {
+ values[i] = ~values[i];
+ }
+ wl_dump(offset, values, sizeof(backing_store_int_t) * item_count);
+ }
+ return status == FLASH_STATUS_SUCCESS;
+}
+
+bool backing_store_write_bulk(uint32_t address, backing_store_int_t *values, size_t item_count) {
+ uint32_t offset = (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) * (EXTERNAL_FLASH_BLOCK_SIZE) + address;
+ size_t index = 0;
+ backing_store_int_t temp[WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT];
+ do {
+ // Copy out the block of data we want to transmit first
+ size_t this_loop = MIN(item_count, WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT);
+ for (size_t i = 0; i < this_loop; ++i) {
+ temp[i] = values[index + i];
+ }
+
+ bs_dprintf("Write ");
+ wl_dump(offset, temp, sizeof(backing_store_int_t) * this_loop);
+
+ // Take the complement instead
+ for (size_t i = 0; i < this_loop; ++i) {
+ temp[i] = ~temp[i];
+ }
+
+ // Write out the block
+ if (flash_write_block(offset, temp, sizeof(backing_store_int_t) * this_loop) != FLASH_STATUS_SUCCESS) {
+ return false;
+ }
+
+ offset += this_loop * sizeof(backing_store_int_t);
+ index += this_loop;
+ item_count -= this_loop;
+ } while (item_count > 0);
+
+ return true;
+}
diff --git a/drivers/wear_leveling/wear_leveling_flash_spi_config.h b/drivers/wear_leveling/wear_leveling_flash_spi_config.h
new file mode 100644
index 0000000000..394370daa3
--- /dev/null
+++ b/drivers/wear_leveling/wear_leveling_flash_spi_config.h
@@ -0,0 +1,34 @@
+// Copyright 2022 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+#pragma once
+
+#ifndef __ASSEMBLER__
+# include <stdlib.h>
+# include <stdint.h>
+# include "flash_spi.h"
+#endif
+
+// Use 1 block -- check the config for the SPI flash to determine how big it is
+#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT
+# define WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT 1
+#endif // WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT
+
+// Start at the first block of the external flash
+#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET
+# define WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET 0
+#endif // WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET
+
+// 8-byte writes by default
+#ifndef BACKING_STORE_WRITE_SIZE
+# define BACKING_STORE_WRITE_SIZE 8
+#endif
+
+// The space allocated by the block
+#ifndef WEAR_LEVELING_BACKING_SIZE
+# define WEAR_LEVELING_BACKING_SIZE ((EXTERNAL_FLASH_BLOCK_SIZE) * (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT))
+#endif // WEAR_LEVELING_BACKING_SIZE
+
+// Use half of the backing size for logical EEPROM
+#ifndef WEAR_LEVELING_LOGICAL_SIZE
+# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2)
+#endif // WEAR_LEVELING_LOGICAL_SIZE