New kernel drivers and device migrations (#563)
This commit is contained in:
committed by
GitHub
parent
955416dac8
commit
8af6204ba1
@@ -6,17 +6,20 @@
|
||||
extern "C" {
|
||||
|
||||
extern Driver xpt2046_driver;
|
||||
extern Driver xpt2046_power_supply_driver;
|
||||
|
||||
static error_t start() {
|
||||
/* We crash when construct fails, because if a single driver fails to construct,
|
||||
* there is no guarantee that the previously constructed drivers can be destroyed */
|
||||
check(driver_construct_add(&xpt2046_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&xpt2046_power_supply_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
/* We crash when destruct fails, because if a single driver fails to destruct,
|
||||
* there is no guarantee that the previously destroyed drivers can be recovered */
|
||||
check(driver_remove_destruct(&xpt2046_power_supply_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&xpt2046_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/esp32_spi.h>
|
||||
#include <tactility/drivers/pointer.h>
|
||||
#include <tactility/drivers/power_supply.h>
|
||||
#include <tactility/drivers/spi_controller.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/log.h>
|
||||
@@ -17,15 +18,149 @@
|
||||
#include <esp_lcd_touch_xpt2046.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <new>
|
||||
|
||||
#define TAG "XPT2046"
|
||||
#define GET_CONFIG(device) (static_cast<const Xpt2046Config*>((device)->config))
|
||||
|
||||
// Rough LiPo discharge curve floor, used together with the configured reference voltage
|
||||
// (the 100% point) to estimate a charge percentage from the sensed v-bat voltage.
|
||||
#define POWER_SUPPLY_MIN_MV 3200
|
||||
|
||||
struct Xpt2046Internal {
|
||||
esp_lcd_panel_io_handle_t io_handle;
|
||||
esp_lcd_touch_handle_t touch_handle;
|
||||
Device* power_supply_device;
|
||||
};
|
||||
|
||||
// region Power supply
|
||||
|
||||
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
|
||||
return property == POWER_SUPPLY_PROP_VOLTAGE || property == POWER_SUPPLY_PROP_CAPACITY;
|
||||
}
|
||||
|
||||
static int estimate_capacity_from_mv(int battery_mv, uint32_t reference_mv) {
|
||||
if (battery_mv <= POWER_SUPPLY_MIN_MV) return 0;
|
||||
if ((uint32_t)battery_mv >= reference_mv) return 100;
|
||||
return (battery_mv - POWER_SUPPLY_MIN_MV) * 100 / ((int)reference_mv - POWER_SUPPLY_MIN_MV);
|
||||
}
|
||||
|
||||
// The v-bat reading is noisy (shared ADC, no dedicated sample/hold), so it's smoothed by
|
||||
// averaging over several samples rather than trusting a single conversion.
|
||||
#define POWER_SUPPLY_SAMPLE_COUNT 20
|
||||
|
||||
static error_t read_battery_mv(esp_lcd_touch_handle_t touch_handle, int* out_mv) {
|
||||
float volts_sum = 0.0f;
|
||||
|
||||
for (int i = 0; i < POWER_SUPPLY_SAMPLE_COUNT; i++) {
|
||||
float volts;
|
||||
if (esp_lcd_touch_xpt2046_read_battery_level(touch_handle, &volts) != ESP_OK) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
volts_sum += volts;
|
||||
}
|
||||
|
||||
*out_mv = (int)((volts_sum / POWER_SUPPLY_SAMPLE_COUNT) * 1000.0f);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
|
||||
if (property != POWER_SUPPLY_PROP_VOLTAGE && property != POWER_SUPPLY_PROP_CAPACITY) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
auto* parent = device_get_parent(device);
|
||||
const auto* parent_config = GET_CONFIG(parent);
|
||||
auto* parent_internal = static_cast<Xpt2046Internal*>(device_get_driver_data(parent));
|
||||
|
||||
int battery_mv;
|
||||
error_t error = read_battery_mv(parent_internal->touch_handle, &battery_mv);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
out_value->int_value = (property == POWER_SUPPLY_PROP_VOLTAGE) ? battery_mv : estimate_capacity_from_mv(battery_mv, parent_config->power_supply_reference_voltage_mv);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static bool ps_supports_charge_control(Device*) { return false; }
|
||||
static bool ps_is_allowed_to_charge(Device*) { return false; }
|
||||
static error_t ps_set_allowed_to_charge(Device*, bool) { return ERROR_NOT_SUPPORTED; }
|
||||
static bool ps_supports_quick_charge(Device*) { return false; }
|
||||
static bool ps_is_quick_charge_enabled(Device*) { return false; }
|
||||
static error_t ps_set_quick_charge_enabled(Device*, bool) { return ERROR_NOT_SUPPORTED; }
|
||||
static bool ps_supports_power_off(Device*) { return false; }
|
||||
static error_t ps_power_off(Device*) { return ERROR_NOT_SUPPORTED; }
|
||||
|
||||
static constexpr PowerSupplyApi XPT2046_POWER_SUPPLY_API = {
|
||||
.supports_property = ps_supports_property,
|
||||
.get_property = ps_get_property,
|
||||
.supports_charge_control = ps_supports_charge_control,
|
||||
.is_allowed_to_charge = ps_is_allowed_to_charge,
|
||||
.set_allowed_to_charge = ps_set_allowed_to_charge,
|
||||
.supports_quick_charge = ps_supports_quick_charge,
|
||||
.is_quick_charge_enabled = ps_is_quick_charge_enabled,
|
||||
.set_quick_charge_enabled = ps_set_quick_charge_enabled,
|
||||
.supports_power_off = ps_supports_power_off,
|
||||
.power_off = ps_power_off,
|
||||
};
|
||||
|
||||
// Registered (driver_construct_add() in module.cpp) so driver_bind() has a valid ->internal,
|
||||
// but never matched against a devicetree node: xpt2046 wires it up directly by pointer.
|
||||
Driver xpt2046_power_supply_driver = {
|
||||
.name = "xpt2046-power-supply",
|
||||
.compatible = (const char*[]) { "xpt2046-power-supply", nullptr },
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = &XPT2046_POWER_SUPPLY_API,
|
||||
.device_type = &POWER_SUPPLY_TYPE,
|
||||
.owner = &xpt2046_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
static error_t create_power_supply_child(Device* parent, Device*& out_child) {
|
||||
auto* child = new(std::nothrow) Device { .address = 0, .name = "xpt2046-power-supply", .config = nullptr, .parent = nullptr, .internal = nullptr };
|
||||
if (child == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
error_t error = device_construct(child);
|
||||
if (error != ERROR_NONE) {
|
||||
delete child;
|
||||
return error;
|
||||
}
|
||||
|
||||
device_set_parent(child, parent);
|
||||
device_set_driver(child, &xpt2046_power_supply_driver);
|
||||
|
||||
error = device_add(child);
|
||||
if (error != ERROR_NONE) {
|
||||
device_destruct(child);
|
||||
delete child;
|
||||
return error;
|
||||
}
|
||||
|
||||
error = device_start(child);
|
||||
if (error != ERROR_NONE) {
|
||||
device_remove(child);
|
||||
device_destruct(child);
|
||||
delete child;
|
||||
return error;
|
||||
}
|
||||
|
||||
out_child = child;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static void destroy_power_supply_child(Device* child) {
|
||||
check(device_stop(child) == ERROR_NONE);
|
||||
check(device_remove(child) == ERROR_NONE);
|
||||
check(device_destruct(child) == ERROR_NONE);
|
||||
delete child;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
@@ -82,13 +217,30 @@ static error_t start(Device* device) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
internal->power_supply_device = nullptr;
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
if (config->power_supply) {
|
||||
error_t error = create_power_supply_child(device, internal->power_supply_device);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to create power-supply device");
|
||||
esp_lcd_touch_del(internal->touch_handle);
|
||||
esp_lcd_panel_io_del(internal->io_handle);
|
||||
free(internal);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = static_cast<Xpt2046Internal*>(device_get_driver_data(device));
|
||||
|
||||
if (internal->power_supply_device != nullptr) {
|
||||
destroy_power_supply_child(internal->power_supply_device);
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
// esp_lcd_touch_del() only releases the touch-side resources; the panel IO handle is owned
|
||||
|
||||
Reference in New Issue
Block a user