diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/battery/battery.c | 31 | ||||
-rw-r--r-- | drivers/battery/battery.h | 34 | ||||
-rw-r--r-- | drivers/battery/battery_adc.c | 55 | ||||
-rw-r--r-- | drivers/battery/battery_driver.h | 29 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_transient.h | 2 | ||||
-rw-r--r-- | drivers/painter/gc9xxx/qp_gc9a01_opcodes.h | 2 | ||||
-rw-r--r-- | drivers/painter/ld7032/qp_ld7032.h | 2 | ||||
-rw-r--r-- | drivers/painter/ld7032/qp_ld7032_opcodes.h | 2 | ||||
-rw-r--r-- | drivers/usbpd.h | 2 |
9 files changed, 154 insertions, 5 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/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/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/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); |