aboutsummaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/battery/battery.c31
-rw-r--r--drivers/battery/battery.h34
-rw-r--r--drivers/battery/battery_adc.c55
-rw-r--r--drivers/battery/battery_driver.h29
-rw-r--r--drivers/bluetooth/bluefruit_le.cpp21
-rw-r--r--drivers/bluetooth/bluefruit_le.h4
-rw-r--r--drivers/eeprom/eeprom_transient.h2
-rw-r--r--drivers/painter/gc9xxx/qp_gc9a01_opcodes.h2
-rw-r--r--drivers/painter/ili9xxx/qp_ili9486.c16
-rw-r--r--drivers/painter/ld7032/qp_ld7032.h2
-rw-r--r--drivers/painter/ld7032/qp_ld7032_opcodes.h2
-rw-r--r--drivers/sensors/pimoroni_trackball.c12
-rw-r--r--drivers/usbpd.h2
13 files changed, 168 insertions, 44 deletions
diff --git a/drivers/battery/battery.c b/drivers/battery/battery.c
new file mode 100644
index 0000000000..65497399e8
--- /dev/null
+++ b/drivers/battery/battery.c
@@ -0,0 +1,31 @@
+// Copyright 2025 QMK
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "battery_driver.h"
+#include "battery.h"
+#include "timer.h"
+
+#ifndef BATTERY_SAMPLE_INTERVAL
+# define BATTERY_SAMPLE_INTERVAL 30000
+#endif
+
+static uint8_t last_bat_level = 100;
+
+void battery_init(void) {
+ battery_driver_init();
+
+ last_bat_level = battery_driver_sample_percent();
+}
+
+void battery_task(void) {
+ static uint32_t bat_timer = 0;
+ if (timer_elapsed32(bat_timer) > BATTERY_SAMPLE_INTERVAL) {
+ last_bat_level = battery_driver_sample_percent();
+
+ bat_timer = timer_read32();
+ }
+}
+
+uint8_t battery_get_percent(void) {
+ return last_bat_level;
+}
diff --git a/drivers/battery/battery.h b/drivers/battery/battery.h
new file mode 100644
index 0000000000..831b1f07e5
--- /dev/null
+++ b/drivers/battery/battery.h
@@ -0,0 +1,34 @@
+// Copyright 2025 QMK
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <stdint.h>
+
+/**
+ * \file
+ *
+ * \defgroup battery Battery API
+ *
+ * \brief API to query battery status.
+ * \{
+ */
+
+/**
+ * \brief Initialize the battery driver.
+ */
+void battery_init(void);
+
+/**
+ * \brief Perform housekeeping tasks.
+ */
+void battery_task(void);
+
+/**
+ * \brief Sample battery level.
+ *
+ * \return The battery percentage, in the range 0-100.
+ */
+uint8_t battery_get_percent(void);
+
+/** \} */
diff --git a/drivers/battery/battery_adc.c b/drivers/battery/battery_adc.c
new file mode 100644
index 0000000000..cf0e69cb48
--- /dev/null
+++ b/drivers/battery/battery_adc.c
@@ -0,0 +1,55 @@
+// Copyright 2025 QMK
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "analog.h"
+#include "gpio.h"
+
+#ifndef BATTERY_PIN
+# error("BATTERY_PIN not configured!")
+#endif
+
+#ifndef BATTERY_REF_VOLTAGE_MV
+# define BATTERY_REF_VOLTAGE_MV 3300
+#endif
+
+#ifndef BATTERY_VOLTAGE_DIVIDER_R1
+# define BATTERY_VOLTAGE_DIVIDER_R1 100
+#endif
+
+#ifndef BATTERY_VOLTAGE_DIVIDER_R2
+# define BATTERY_VOLTAGE_DIVIDER_R2 100
+#endif
+
+// TODO: infer from adc config?
+#ifndef BATTERY_ADC_RESOLUTION
+# define BATTERY_ADC_RESOLUTION 10
+#endif
+
+void battery_driver_init(void) {
+ gpio_set_pin_input(BATTERY_PIN);
+}
+
+uint16_t battery_driver_get_mv(void) {
+ uint32_t raw = analogReadPin(BATTERY_PIN);
+
+ uint32_t bat_mv = raw * BATTERY_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION);
+
+#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_VOLTAGE_DIVIDER_R2 > 0
+ bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_VOLTAGE_DIVIDER_R2) / BATTERY_VOLTAGE_DIVIDER_R2;
+#endif
+
+ return bat_mv;
+}
+
+uint8_t battery_driver_sample_percent(void) {
+ uint16_t bat_mv = battery_driver_get_mv();
+
+ // https://github.com/zmkfirmware/zmk/blob/3f7c9d7cc4f46617faad288421025ea2a6b0bd28/app/module/drivers/sensor/battery/battery_common.c#L33
+ if (bat_mv >= 4200) {
+ return 100;
+ } else if (bat_mv <= 3450) {
+ return 0;
+ }
+
+ return bat_mv * 2 / 15 - 459;
+}
diff --git a/drivers/battery/battery_driver.h b/drivers/battery/battery_driver.h
new file mode 100644
index 0000000000..c2ee75e966
--- /dev/null
+++ b/drivers/battery/battery_driver.h
@@ -0,0 +1,29 @@
+// Copyright 2025 QMK
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <stdint.h>
+
+/**
+ * \file
+ *
+ * \defgroup battery Battery Driver API
+ *
+ * \brief API to query battery status.
+ * \{
+ */
+
+/**
+ * \brief Initialize the battery driver. This function must be called only once, before any of the below functions can be called.
+ */
+void battery_driver_init(void);
+
+/**
+ * \brief Sample battery level.
+ *
+ * \return The battery percentage, in the range 0-100.
+ */
+uint8_t battery_driver_sample_percent(void);
+
+/** \} */
diff --git a/drivers/bluetooth/bluefruit_le.cpp b/drivers/bluetooth/bluefruit_le.cpp
index 218eca2195..5fdd104dcf 100644
--- a/drivers/bluetooth/bluefruit_le.cpp
+++ b/drivers/bluetooth/bluefruit_le.cpp
@@ -32,13 +32,8 @@
# define BLUEFRUIT_LE_SCK_DIVISOR 2 // 4MHz SCK/8MHz CPU, calculated for Feather 32U4 BLE
#endif
-#define SAMPLE_BATTERY
#define ConnectionUpdateInterval 1000 /* milliseconds */
-#ifndef BATTERY_LEVEL_PIN
-# define BATTERY_LEVEL_PIN B5
-#endif
-
static struct {
bool is_connected;
bool initialized;
@@ -48,10 +43,6 @@ static struct {
#define UsingEvents 2
bool event_flags;
-#ifdef SAMPLE_BATTERY
- uint16_t last_battery_update;
- uint32_t vbat;
-#endif
uint16_t last_connection_update;
} state;
@@ -549,14 +540,6 @@ void bluefruit_le_task(void) {
set_connected(atoi(resbuf));
}
}
-
-#ifdef SAMPLE_BATTERY
- if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) {
- state.last_battery_update = timer_read();
-
- state.vbat = analogReadPin(BATTERY_LEVEL_PIN);
- }
-#endif
}
static bool process_queue_item(struct queue_item *item, uint16_t timeout) {
@@ -655,10 +638,6 @@ void bluefruit_le_send_mouse(report_mouse_t *report) {
}
}
-uint32_t bluefruit_le_read_battery_voltage(void) {
- return state.vbat;
-}
-
bool bluefruit_le_set_mode_leds(bool on) {
if (!state.configured) {
return false;
diff --git a/drivers/bluetooth/bluefruit_le.h b/drivers/bluetooth/bluefruit_le.h
index a3de03c35c..5efe92b6e0 100644
--- a/drivers/bluetooth/bluefruit_le.h
+++ b/drivers/bluetooth/bluefruit_le.h
@@ -45,10 +45,6 @@ extern void bluefruit_le_send_consumer(uint16_t usage);
* change. */
extern void bluefruit_le_send_mouse(report_mouse_t *report);
-/* Compute battery voltage by reading an analog pin.
- * Returns the integer number of millivolts */
-extern uint32_t bluefruit_le_read_battery_voltage(void);
-
extern bool bluefruit_le_set_mode_leds(bool on);
extern bool bluefruit_le_set_power_level(int8_t level);
diff --git a/drivers/eeprom/eeprom_transient.h b/drivers/eeprom/eeprom_transient.h
index 687b8619fe..0483fd58b9 100644
--- a/drivers/eeprom/eeprom_transient.h
+++ b/drivers/eeprom/eeprom_transient.h
@@ -20,6 +20,6 @@
The size of the transient EEPROM buffer size.
*/
#ifndef TRANSIENT_EEPROM_SIZE
-# include "eeconfig.h"
+# include "nvm_eeprom_eeconfig_internal.h"
# define TRANSIENT_EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
#endif
diff --git a/drivers/painter/gc9xxx/qp_gc9a01_opcodes.h b/drivers/painter/gc9xxx/qp_gc9a01_opcodes.h
index 5853902e68..31de5ee9e3 100644
--- a/drivers/painter/gc9xxx/qp_gc9a01_opcodes.h
+++ b/drivers/painter/gc9xxx/qp_gc9a01_opcodes.h
@@ -101,4 +101,4 @@
#define GC9A01_GRAM_INTERFACE_RGB 0b00000010
#define GC9A01_RGB_INTERFACE_MODE_1_TRANSFER 0b00000000
-#define GC9A01_RGB_INTERFACE_MODE_3_TRANSFER 0b00000001 \ No newline at end of file
+#define GC9A01_RGB_INTERFACE_MODE_3_TRANSFER 0b00000001
diff --git a/drivers/painter/ili9xxx/qp_ili9486.c b/drivers/painter/ili9xxx/qp_ili9486.c
index c4f3c15cec..48db779c04 100644
--- a/drivers/painter/ili9xxx/qp_ili9486.c
+++ b/drivers/painter/ili9xxx/qp_ili9486.c
@@ -75,9 +75,9 @@ static void qp_comms_spi_dc_reset_send_command_odd_cs_pulse(painter_device_t dev
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
- writePinLow(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_low(comms_config->spi_config.chip_select_pin);
qp_comms_spi_dc_reset_send_command(device, cmd);
- writePinHigh(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_high(comms_config->spi_config.chip_select_pin);
}
static uint32_t qp_comms_spi_send_data_odd_cs_pulse(painter_device_t device, const void *data, uint32_t byte_count) {
@@ -88,20 +88,20 @@ static uint32_t qp_comms_spi_send_data_odd_cs_pulse(painter_device_t device, con
const uint8_t *p = (const uint8_t *)data;
uint32_t max_msg_length = 1024;
- writePinHigh(comms_config->dc_pin);
+ gpio_write_pin_high(comms_config->dc_pin);
while (bytes_remaining > 0) {
uint32_t bytes_this_loop = QP_MIN(bytes_remaining, max_msg_length);
bool odd_bytes = bytes_this_loop & 1;
// send data
- writePinLow(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_low(comms_config->spi_config.chip_select_pin);
spi_transmit(p, bytes_this_loop);
p += bytes_this_loop;
// extra CS toggle, for alignment
if (odd_bytes) {
- writePinHigh(comms_config->spi_config.chip_select_pin);
- writePinLow(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_high(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_low(comms_config->spi_config.chip_select_pin);
}
bytes_remaining -= bytes_this_loop;
@@ -116,9 +116,9 @@ static uint32_t qp_ili9486_send_data_toggling(painter_device_t device, const uin
uint32_t ret;
for (uint8_t j = 0; j < byte_count; ++j) {
- writePinLow(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_low(comms_config->spi_config.chip_select_pin);
ret = qp_comms_spi_dc_reset_send_data(device, &data[j], 1);
- writePinHigh(comms_config->spi_config.chip_select_pin);
+ gpio_write_pin_high(comms_config->spi_config.chip_select_pin);
}
return ret;
diff --git a/drivers/painter/ld7032/qp_ld7032.h b/drivers/painter/ld7032/qp_ld7032.h
index 967eb7999c..9af80dd468 100644
--- a/drivers/painter/ld7032/qp_ld7032.h
+++ b/drivers/painter/ld7032/qp_ld7032.h
@@ -63,4 +63,4 @@ painter_device_t qp_ld7032_make_spi_device(uint16_t panel_width, uint16_t panel_
*/
painter_device_t qp_ld7032_make_i2c_device(uint16_t panel_width, uint16_t panel_height, uint8_t i2c_address);
-#endif // QUANTUM_PAINTER_LD7032_I2C_ENABLE \ No newline at end of file
+#endif // QUANTUM_PAINTER_LD7032_I2C_ENABLE
diff --git a/drivers/painter/ld7032/qp_ld7032_opcodes.h b/drivers/painter/ld7032/qp_ld7032_opcodes.h
index 08ab77d6f8..ed8b397c67 100644
--- a/drivers/painter/ld7032/qp_ld7032_opcodes.h
+++ b/drivers/painter/ld7032/qp_ld7032_opcodes.h
@@ -42,4 +42,4 @@ typedef enum {
LD7032_S_START_STOP = 0xCD,
LD7032_S_SELECT = 0xCE,
LD7032_TESTCNT1 = 0xF0, //-0xFF
-} ld7032_opcodes; \ No newline at end of file
+} ld7032_opcodes;
diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c
index afbe3d5b77..06e5042953 100644
--- a/drivers/sensors/pimoroni_trackball.c
+++ b/drivers/sensors/pimoroni_trackball.c
@@ -101,12 +101,12 @@ int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_di
}
mouse_xy_report_t pimoroni_trackball_adapt_values(xy_clamp_range_t *offset) {
- if (*offset > XY_REPORT_MAX) {
- *offset -= XY_REPORT_MAX;
- return (mouse_xy_report_t)XY_REPORT_MAX;
- } else if (*offset < XY_REPORT_MIN) {
- *offset += XY_REPORT_MAX;
- return (mouse_xy_report_t)XY_REPORT_MIN;
+ if (*offset > MOUSE_REPORT_XY_MAX) {
+ *offset -= MOUSE_REPORT_XY_MAX;
+ return (mouse_xy_report_t)MOUSE_REPORT_XY_MAX;
+ } else if (*offset < MOUSE_REPORT_XY_MIN) {
+ *offset += MOUSE_REPORT_XY_MAX;
+ return (mouse_xy_report_t)MOUSE_REPORT_XY_MIN;
} else {
mouse_xy_report_t temp_return = *offset;
*offset = 0;
diff --git a/drivers/usbpd.h b/drivers/usbpd.h
index df4f29bb9d..e9dca67f66 100644
--- a/drivers/usbpd.h
+++ b/drivers/usbpd.h
@@ -26,4 +26,4 @@ typedef enum {
void usbpd_init(void);
// Gets the current state of the USBPD allowance
-usbpd_allowance_t usbpd_get_allowance(void); \ No newline at end of file
+usbpd_allowance_t usbpd_get_allowance(void);