Device migrations, new drivers, cleanup (#567)

* **New Features**
  * Added native display support for ST7796, ILI9341, and ST7789 panels across supported boards.
  * Added FT5x06 and FT6x36 touchscreen support.
  * Generic PWM driver
  * ESP32 PWM driver
  * Generic RGB LED driver
  * RGB PWM LED driver
  * RGB GPIO LED driver
  * Implementation of RGB LED for various boards

* **Improvements**
  * Updated board hardware descriptions to use explicit display/touch/backlight device-tree bindings and disabled deprecated HAL usage.
  * Improved display and touch-driver cleanup to prevent stale resources and improve shutdown reliability.
  * Pinned esp-hosted library to a fixed version
 
* **Deletions**
  * Obsolete placeholder display
  * Legacy ILI9488 support.
  * ESP32-specific LEDC PWM implementation
This commit is contained in:
Ken Van Hoeylandt
2026-07-16 22:47:26 +02:00
committed by GitHub
parent 6fb2bb736c
commit 3b5a401594
175 changed files with 4218 additions and 1462 deletions
@@ -0,0 +1,16 @@
description: >
Simple GPIO-driven on/off backlight, for panels whose backlight is a single digital
enable pin rather than a PWM-dimmable one. Brightness is treated as boolean: any
value greater than 0 turns the backlight on, 0 turns it off.
compatible: "gpio-backlight"
properties:
pin:
type: phandles
required: true
description: Backlight enable output pin
enabled:
type: boolean
default: false
description: Whether the backlight is turned on by set_brightness_default()
@@ -6,7 +6,7 @@ description: >
HAL's initBoot() hook. Declare a gpio-hog node before the dependent device(s) in the .dts
source so it runs first.
compatible: "tactility,gpio-hog"
compatible: "gpio-hog"
properties:
pin:
@@ -0,0 +1,19 @@
description: >
PWM-driven display backlight. Wraps any PWM_TYPE device (e.g. espressif,esp32-pwm-ledc or
pwm-generic) and maps the brightness-level-range onto its duty cycle.
compatible: "pwm-backlight"
properties:
pwm:
type: phandles
required: true
description: The PWM device driving the backlight
brightness-level-range:
type: values
default: [0, 255]
description: Inclusive [min,max] brightness range. The minimum value turns the backlight off.
brightness-default:
type: int
default: 200
description: Default brightness level, applied by set_brightness_default(). Should fall within brightness-level-range.
+20
View File
@@ -0,0 +1,20 @@
description: >
Generic PWM device that tracks period, duty cycle, polarity and enabled state in memory
without driving real hardware. Useful as a placeholder on boards without a real PWM
peripheral wired up yet, or in the POSIX simulator.
compatible: "pwm-generic"
properties:
period-ns:
type: int
required: true
description: The PWM period, in nanoseconds
duty-ns:
type: int
default: 0
description: The PWM duty cycle (active time within one period), in nanoseconds
inverted:
type: boolean
default: false
description: Whether the output polarity is inverted
@@ -0,0 +1,27 @@
description: >
RGB LED driven by 3 plain digital GPIO pins. Each channel is on/off only (no dimming):
a color component greater than 0 turns that channel's pin on.
compatible: "rgb-led-gpio"
properties:
pin-red:
type: phandles
required: true
description: Red channel output pin
pin-green:
type: phandles
required: true
description: Green channel output pin
pin-blue:
type: phandles
required: true
description: Blue channel output pin
enabled:
type: boolean
default: false
description: Whether the LED is turned on by default
default-color:
type: values
default: [255, 255, 255]
description: Color applied when the LED is turned on by default
+27
View File
@@ -0,0 +1,27 @@
description: >
RGB LED driven by 3 PWM devices (e.g. espressif,esp32-pwm-ledc or pwm-generic),
one per channel, giving each channel dimming via the wrapped device's duty cycle.
compatible: "rgb-led-pwm"
properties:
pwm-red:
type: phandle
required: true
description: PWM device driving the red channel
pwm-green:
type: phandle
required: true
description: PWM device driving the green channel
pwm-blue:
type: phandle
required: true
description: PWM device driving the blue channel
enabled:
type: boolean
default: false
description: Whether the LED is turned on by default
default-color:
type: values
default: [255, 255, 255]
description: Color applied when the LED is turned on by default
@@ -1,5 +0,0 @@
description: SPI peripheral
compatible: "spi-peripheral"
properties: {}
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/gpio_backlight.h>
DEFINE_DEVICETREE(gpio_backlight, struct GpioBacklightConfig)
@@ -1,15 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/pointer_placeholder.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(pointer_placeholder, struct PointerPlaceholderConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/pwm_backlight.h>
DEFINE_DEVICETREE(pwm_backlight, struct PwmBacklightConfig)
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/rgb_led_gpio.h>
DEFINE_DEVICETREE(rgb_led_gpio, struct RgbLedGpioConfig)
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/rgb_led_pwm.h>
DEFINE_DEVICETREE(rgb_led_pwm, struct RgbLedPwmConfig)
@@ -1,15 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/spi_peripheral.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(spi_peripheral, struct SpiPeripheralConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <tactility/drivers/gpio.h>
/**
* @brief Devicetree configuration for a GPIO-driven on/off backlight.
*/
struct GpioBacklightConfig {
/** Backlight enable output pin */
struct GpioPinSpec pin;
/** Whether the backlight is turned on by set_brightness_default() */
bool enabled;
};
#ifdef __cplusplus
}
#endif
@@ -1,16 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct PointerPlaceholderConfig {
uint8_t _unused;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,150 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <tactility/device.h>
#include <tactility/error.h>
/**
* @brief Devicetree configuration for a PWM device.
*/
struct PwmConfig {
/** The PWM period, in nanoseconds */
uint32_t period_ns;
/** The PWM duty cycle (active time within one period), in nanoseconds */
uint32_t duty_ns;
/** Whether the output polarity is inverted */
bool inverted;
};
/**
* @brief API for PWM drivers.
*/
struct PwmApi {
/**
* @brief Sets the PWM period.
* @param[in] device the PWM device
* @param[in] period_ns the period, in nanoseconds
* @retval ERROR_NONE when the operation was successful
*/
error_t (*set_period)(struct Device* device, uint32_t period_ns);
/**
* @brief Gets the PWM period.
* @param[in] device the PWM device
* @param[out] period_ns the period, in nanoseconds
* @retval ERROR_NONE when the operation was successful
*/
error_t (*get_period)(struct Device* device, uint32_t* period_ns);
/**
* @brief Sets the PWM duty cycle.
* @param[in] device the PWM device
* @param[in] duty_ns the active time within one period, in nanoseconds
* @retval ERROR_NONE when the operation was successful
*/
error_t (*set_duty)(struct Device* device, uint32_t duty_ns);
/**
* @brief Gets the PWM duty cycle.
* @param[in] device the PWM device
* @param[out] duty_ns the active time within one period, in nanoseconds
* @retval ERROR_NONE when the operation was successful
*/
error_t (*get_duty)(struct Device* device, uint32_t* duty_ns);
/**
* @brief Sets whether the output polarity is inverted.
* @param[in] device the PWM device
* @param[in] inverted true to invert the output polarity
* @retval ERROR_NONE when the operation was successful
*/
error_t (*set_inverted)(struct Device* device, bool inverted);
/**
* @brief Gets whether the output polarity is inverted.
* @param[in] device the PWM device
* @param[out] inverted true when the output polarity is inverted
* @retval ERROR_NONE when the operation was successful
*/
error_t (*is_inverted)(struct Device* device, bool* inverted);
/**
* @brief Enables the PWM output.
* @param[in] device the PWM device
* @retval ERROR_NONE when the operation was successful
*/
error_t (*enable)(struct Device* device);
/**
* @brief Disables the PWM output.
* @param[in] device the PWM device
* @retval ERROR_NONE when the operation was successful
*/
error_t (*disable)(struct Device* device);
/**
* @brief Gets whether the PWM output is enabled.
* @param[in] device the PWM device
* @param[out] enabled true when the output is enabled
* @retval ERROR_NONE when the operation was successful
*/
error_t (*is_enabled)(struct Device* device, bool* enabled);
};
/**
* @brief Sets the PWM period using the specified PWM device.
*/
error_t pwm_set_period(struct Device* device, uint32_t period_ns);
/**
* @brief Gets the PWM period using the specified PWM device.
*/
error_t pwm_get_period(struct Device* device, uint32_t* period_ns);
/**
* @brief Sets the PWM duty cycle using the specified PWM device.
*/
error_t pwm_set_duty(struct Device* device, uint32_t duty_ns);
/**
* @brief Gets the PWM duty cycle using the specified PWM device.
*/
error_t pwm_get_duty(struct Device* device, uint32_t* duty_ns);
/**
* @brief Sets whether the output polarity is inverted using the specified PWM device.
*/
error_t pwm_set_inverted(struct Device* device, bool inverted);
/**
* @brief Gets whether the output polarity is inverted using the specified PWM device.
*/
error_t pwm_is_inverted(struct Device* device, bool* inverted);
/**
* @brief Enables the PWM output using the specified PWM device.
*/
error_t pwm_enable(struct Device* device);
/**
* @brief Disables the PWM output using the specified PWM device.
*/
error_t pwm_disable(struct Device* device);
/**
* @brief Gets whether the PWM output is enabled using the specified PWM device.
*/
error_t pwm_is_enabled(struct Device* device, bool* enabled);
extern const struct DeviceType PWM_TYPE;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <tactility/device.h>
#include <tactility/drivers/backlight.h>
/**
* @brief Devicetree configuration for a PWM-driven backlight.
*/
struct PwmBacklightConfig {
/** The PWM device driving the backlight */
struct Device* pwm;
/** Inclusive [min,max] brightness range. The minimum value turns the backlight off. */
struct BrightnessLevelRange brightness_range;
/** Default brightness level, applied by set_brightness_default() */
uint8_t brightness_default;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <tactility/device.h>
#include <tactility/error.h>
/**
* @brief An RGB color value.
*/
struct RgbLedColor {
uint8_t r;
uint8_t g;
uint8_t b;
};
/**
* @brief API for RGB LED drivers.
*/
struct RgbLedApi {
/**
* @brief Sets the LED color.
* @param[in] device the RGB LED device
* @param[in] color the color to set
* @retval ERROR_NONE when the operation was successful
*/
error_t (*set_color)(struct Device* device, const struct RgbLedColor color);
/**
* @brief Gets the LED color.
* @param[in] device the RGB LED device
* @param[out] out_color the current color
* @retval ERROR_NONE when the operation was successful
*/
error_t (*get_color)(struct Device* device, struct RgbLedColor* out_color);
/**
* @brief Enables the LED output.
* @param[in] device the RGB LED device
* @retval ERROR_NONE when the operation was successful
*/
error_t (*enable)(struct Device* device);
/**
* @brief Disables the LED output.
* @param[in] device the RGB LED device
*/
void (*disable)(struct Device* device);
};
/**
* @brief Sets the LED color using the specified RGB LED device.
*/
error_t rgb_led_set_color(struct Device* device, struct RgbLedColor color);
/**
* @brief Gets the LED color using the specified RGB LED device.
*/
error_t rgb_led_get_color(struct Device* device, struct RgbLedColor* out_color);
/**
* @brief Enables the LED output using the specified RGB LED device.
*/
error_t rgb_led_enable(struct Device* device);
/**
* @brief Disables the LED output using the specified RGB LED device.
*/
void rgb_led_disable(struct Device* device);
extern const struct DeviceType RGB_LED_TYPE;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <tactility/drivers/gpio.h>
#include <tactility/drivers/rgb_led.h>
/**
* @brief Devicetree configuration for the GPIO-driven RGB LED.
*/
struct RgbLedGpioConfig {
/** Red channel output pin */
struct GpioPinSpec pin_red;
/** Green channel output pin */
struct GpioPinSpec pin_green;
/** Blue channel output pin */
struct GpioPinSpec pin_blue;
/** Whether the LED is turned on by default */
bool enabled;
/** Color applied when the LED is turned on by default */
struct RgbLedColor default_color;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <tactility/device.h>
#include <tactility/drivers/rgb_led.h>
/**
* @brief Devicetree configuration for the PWM-driven RGB LED.
*/
struct RgbLedPwmConfig {
/** PWM device driving the red channel */
struct Device* pwm_red;
/** PWM device driving the green channel */
struct Device* pwm_green;
/** PWM device driving the blue channel */
struct Device* pwm_blue;
/** Whether the LED is turned on by default */
bool enabled;
/** Color applied when the LED is turned on by default */
struct RgbLedColor default_color;
};
#ifdef __cplusplus
}
#endif
@@ -1,19 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdint.h>
#include <tactility/device.h>
#ifdef __cplusplus
extern "C" {
#endif
struct SpiPeripheralConfig {
uint8_t _unused;
};
extern const struct DeviceType SPI_PERIPHERAL_TYPE;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/gpio_backlight.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/backlight.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
#define TAG "GpioBacklight"
#define GET_CONFIG(device) (static_cast<const GpioBacklightConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<GpioBacklightInternal*>(device_get_driver_data(device)))
extern "C" {
struct GpioBacklightInternal {
GpioDescriptor* descriptor;
bool enabled;
};
// region GpioBacklightApi
static error_t enable(Device* device) {
auto* internal = GET_INTERNAL(device);
error_t error = gpio_descriptor_set_level(internal->descriptor, true);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to enable backlight");
return error;
}
internal->enabled = true;
return ERROR_NONE;
}
static error_t disable(Device* device) {
auto* internal = GET_INTERNAL(device);
error_t error = gpio_descriptor_set_level(internal->descriptor, false);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to disable backlight");
return error;
}
internal->enabled = false;
return ERROR_NONE;
}
// endregion
// region BacklightApi
static error_t gpio_backlight_set_brightness(Device* device, uint8_t brightness) {
return brightness > 0 ? enable(device) : disable(device);
}
static error_t gpio_backlight_set_brightness_default(Device* device) {
return GET_CONFIG(device)->enabled ? enable(device) : disable(device);
}
static error_t gpio_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
*out_brightness = GET_INTERNAL(device)->enabled ? 1 : 0;
return ERROR_NONE;
}
static uint8_t gpio_backlight_get_min_brightness(Device*) {
return 0;
}
static uint8_t gpio_backlight_get_max_brightness(Device*) {
return 1;
}
// endregion
static constexpr BacklightApi GPIO_BACKLIGHT_API = {
.set_brightness = gpio_backlight_set_brightness,
.set_brightness_default = gpio_backlight_set_brightness_default,
.get_brightness = gpio_backlight_get_brightness,
.get_min_brightness = gpio_backlight_get_min_brightness,
.get_max_brightness = gpio_backlight_get_max_brightness,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
auto* descriptor = gpio_descriptor_acquire(config->pin.gpio_controller, config->pin.pin, GPIO_OWNER_GPIO);
if (descriptor == nullptr) {
LOG_E(TAG, "Failed to acquire GPIO descriptor");
return ERROR_RESOURCE;
}
if (gpio_descriptor_set_flags(descriptor, config->pin.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) {
LOG_E(TAG, "Failed to configure backlight pin as output");
gpio_descriptor_release(descriptor);
return ERROR_RESOURCE;
}
auto* internal = new(std::nothrow) GpioBacklightInternal { .descriptor = descriptor, .enabled = false };
if (internal == nullptr) {
gpio_descriptor_release(descriptor);
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
if (gpio_backlight_set_brightness_default(device) != ERROR_NONE) {
// Allowed to fail, we don't care about the result
LOG_W(TAG, "gpio_backlight_set_brightness_default(%s) failed", device->name);
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
disable(device); // Allowed to fail, we don't care about the result
auto* internal = GET_INTERNAL(device);
gpio_descriptor_release(internal->descriptor);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
extern Module root_module;
Driver gpio_backlight_driver = {
.name = "gpio_backlight",
.compatible = (const char*[]) { "gpio-backlight", nullptr },
.start_device = start,
.stop_device = stop,
.api = &GPIO_BACKLIGHT_API,
.device_type = &BACKLIGHT_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
+1 -1
View File
@@ -62,7 +62,7 @@ extern Module root_module;
Driver gpio_hog_driver = {
.name = "gpio_hog",
.compatible = (const char*[]) { "tactility,gpio-hog", nullptr },
.compatible = (const char*[]) { "gpio-hog", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
@@ -1,26 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/pointer_placeholder.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/pointer.h>
#include <tactility/module.h>
extern "C" {
static error_t start(Device*) { return ERROR_NONE; }
static error_t stop(Device*) { return ERROR_NONE; }
extern Module root_module;
Driver pointer_placeholder_driver = {
.name = "pointer_placeholder",
.compatible = (const char*[]) { "pointer-placeholder", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = &POINTER_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
+58
View File
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/pwm.h>
#include <tactility/device.h>
#define PWM_DRIVER_API(driver) ((struct PwmApi*)driver->api)
extern "C" {
error_t pwm_set_period(Device* device, uint32_t period_ns) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->set_period(device, period_ns);
}
error_t pwm_get_period(Device* device, uint32_t* period_ns) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->get_period(device, period_ns);
}
error_t pwm_set_duty(Device* device, uint32_t duty_ns) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->set_duty(device, duty_ns);
}
error_t pwm_get_duty(Device* device, uint32_t* duty_ns) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->get_duty(device, duty_ns);
}
error_t pwm_set_inverted(Device* device, bool inverted) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->set_inverted(device, inverted);
}
error_t pwm_is_inverted(Device* device, bool* inverted) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->is_inverted(device, inverted);
}
error_t pwm_enable(Device* device) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->enable(device);
}
error_t pwm_disable(Device* device) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->disable(device);
}
error_t pwm_is_enabled(Device* device, bool* enabled) {
const auto* driver = device_get_driver(device);
return PWM_DRIVER_API(driver)->is_enabled(device, enabled);
}
const DeviceType PWM_TYPE {
.name = "pwm"
};
}
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/pwm_backlight.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/backlight.h>
#include <tactility/drivers/pwm.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
#define TAG "PwmBacklight"
#define GET_CONFIG(device) (static_cast<const PwmBacklightConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<PwmBacklightInternal*>(device_get_driver_data(device)))
extern "C" {
struct PwmBacklightInternal {
uint8_t brightness;
};
// region BacklightApi
static error_t apply_brightness(Device* device, uint8_t brightness) {
const auto* config = GET_CONFIG(device);
if (brightness <= config->brightness_range.min) {
error_t error = pwm_disable(config->pwm);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to disable PWM");
return error;
}
return ERROR_NONE;
}
if (brightness > config->brightness_range.max) {
brightness = config->brightness_range.max;
}
uint32_t period_ns;
error_t error = pwm_get_period(config->pwm, &period_ns);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to get PWM period");
return error;
}
uint8_t level = brightness - config->brightness_range.min;
uint8_t range = config->brightness_range.max - config->brightness_range.min;
uint32_t duty_ns = (uint32_t)(((uint64_t)level * period_ns) / range);
error = pwm_set_duty(config->pwm, duty_ns);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to set PWM duty");
return error;
}
return pwm_enable(config->pwm);
}
static error_t pwm_backlight_set_brightness(Device* device, uint8_t brightness) {
error_t error = apply_brightness(device, brightness);
if (error != ERROR_NONE) {
return error;
}
GET_INTERNAL(device)->brightness = brightness;
return ERROR_NONE;
}
static error_t pwm_backlight_set_brightness_default(Device* device) {
return pwm_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
}
static error_t pwm_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
*out_brightness = GET_INTERNAL(device)->brightness;
return ERROR_NONE;
}
static uint8_t pwm_backlight_get_min_brightness(Device* device) {
return GET_CONFIG(device)->brightness_range.min;
}
static uint8_t pwm_backlight_get_max_brightness(Device* device) {
return GET_CONFIG(device)->brightness_range.max;
}
// endregion
static constexpr BacklightApi PWM_BACKLIGHT_API = {
.set_brightness = pwm_backlight_set_brightness,
.set_brightness_default = pwm_backlight_set_brightness_default,
.get_brightness = pwm_backlight_get_brightness,
.get_min_brightness = pwm_backlight_get_min_brightness,
.get_max_brightness = pwm_backlight_get_max_brightness,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
if (config->brightness_range.max <= config->brightness_range.min) {
return ERROR_INVALID_ARGUMENT;
}
auto* internal = new(std::nothrow) PwmBacklightInternal { .brightness = GET_CONFIG(device)->brightness_range.min };
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
pwm_backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result
return ERROR_NONE;
}
static error_t stop(Device* device) {
pwm_backlight_set_brightness(device, GET_CONFIG(device)->brightness_range.min); // Allowed to fail, we don't care about the result
auto* internal = GET_INTERNAL(device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
extern Module root_module;
Driver pwm_backlight_driver = {
.name = "pwm_backlight",
.compatible = (const char*[]) { "pwm-backlight", nullptr },
.start_device = start,
.stop_device = stop,
.api = &PWM_BACKLIGHT_API,
.device_type = &BACKLIGHT_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/rgb_led.h>
#include <tactility/device.h>
#define RGB_LED_DRIVER_API(driver) ((struct RgbLedApi*)driver->api)
extern "C" {
error_t rgb_led_set_color(Device* device, RgbLedColor color) {
const auto* driver = device_get_driver(device);
return RGB_LED_DRIVER_API(driver)->set_color(device, color);
}
error_t rgb_led_get_color(Device* device, RgbLedColor* out_color) {
const auto* driver = device_get_driver(device);
return RGB_LED_DRIVER_API(driver)->get_color(device, out_color);
}
error_t rgb_led_enable(Device* device) {
const auto* driver = device_get_driver(device);
return RGB_LED_DRIVER_API(driver)->enable(device);
}
void rgb_led_disable(Device* device) {
const auto* driver = device_get_driver(device);
RGB_LED_DRIVER_API(driver)->disable(device);
}
const DeviceType RGB_LED_TYPE {
.name = "rgb_led"
};
}
@@ -0,0 +1,171 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/rgb_led_gpio.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
#define TAG "RgbLedGpio"
#define GET_CONFIG(device) (static_cast<const RgbLedGpioConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<RgbLedGpioInternal*>(device_get_driver_data(device)))
extern "C" {
struct RgbLedGpioInternal {
GpioDescriptor* descriptor_red;
GpioDescriptor* descriptor_green;
GpioDescriptor* descriptor_blue;
RgbLedColor color;
bool enabled;
};
// region RgbLedApi
static error_t apply_levels(Device* device) {
auto* internal = GET_INTERNAL(device);
bool on = internal->enabled;
error_t error = gpio_descriptor_set_level(internal->descriptor_red, on && internal->color.r > 0);
if (error == ERROR_NONE) {
error = gpio_descriptor_set_level(internal->descriptor_green, on && internal->color.g > 0);
}
if (error == ERROR_NONE) {
error = gpio_descriptor_set_level(internal->descriptor_blue, on && internal->color.b > 0);
}
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to apply LED levels");
}
return error;
}
static error_t rgb_led_gpio_set_color(Device* device, RgbLedColor color) {
GET_INTERNAL(device)->color = color;
return apply_levels(device);
}
static error_t rgb_led_gpio_get_color(Device* device, RgbLedColor* out_color) {
*out_color = GET_INTERNAL(device)->color;
return ERROR_NONE;
}
static error_t rgb_led_gpio_enable(Device* device) {
GET_INTERNAL(device)->enabled = true;
return apply_levels(device);
}
static void rgb_led_gpio_disable(Device* device) {
GET_INTERNAL(device)->enabled = false;
apply_levels(device); // Allowed to fail, we don't care about the result
}
// endregion
static constexpr RgbLedApi RGB_LED_GPIO_API = {
.set_color = rgb_led_gpio_set_color,
.get_color = rgb_led_gpio_get_color,
.enable = rgb_led_gpio_enable,
.disable = rgb_led_gpio_disable,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
auto* descriptor_red = gpio_descriptor_acquire(config->pin_red.gpio_controller, config->pin_red.pin, GPIO_OWNER_GPIO);
if (descriptor_red == nullptr) {
LOG_E(TAG, "Failed to acquire red GPIO descriptor");
return ERROR_RESOURCE;
}
auto* descriptor_green = gpio_descriptor_acquire(config->pin_green.gpio_controller, config->pin_green.pin, GPIO_OWNER_GPIO);
if (descriptor_green == nullptr) {
LOG_E(TAG, "Failed to acquire green GPIO descriptor");
gpio_descriptor_release(descriptor_red);
return ERROR_RESOURCE;
}
auto* descriptor_blue = gpio_descriptor_acquire(config->pin_blue.gpio_controller, config->pin_blue.pin, GPIO_OWNER_GPIO);
if (descriptor_blue == nullptr) {
LOG_E(TAG, "Failed to acquire blue GPIO descriptor");
gpio_descriptor_release(descriptor_red);
gpio_descriptor_release(descriptor_green);
return ERROR_RESOURCE;
}
bool ok = gpio_descriptor_set_flags(descriptor_red, config->pin_red.flags | GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE &&
gpio_descriptor_set_flags(descriptor_green, config->pin_green.flags | GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE &&
gpio_descriptor_set_flags(descriptor_blue, config->pin_blue.flags | GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE;
if (!ok) {
LOG_E(TAG, "Failed to configure LED pins as outputs");
gpio_descriptor_release(descriptor_red);
gpio_descriptor_release(descriptor_green);
gpio_descriptor_release(descriptor_blue);
return ERROR_RESOURCE;
}
auto* internal = new(std::nothrow) RgbLedGpioInternal {
.descriptor_red = descriptor_red,
.descriptor_green = descriptor_green,
.descriptor_blue = descriptor_blue,
.color = config->default_color,
.enabled = false
};
if (internal == nullptr) {
gpio_descriptor_release(descriptor_red);
gpio_descriptor_release(descriptor_green);
gpio_descriptor_release(descriptor_blue);
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
if (config->enabled) {
if (rgb_led_gpio_enable(device) != ERROR_NONE) {
// Allowed to fail, we don't care about the result
LOG_W(TAG, "led_gpio_enable(%s) failed", device->name);
}
} else {
if (apply_levels(device) != ERROR_NONE) {
// Allowed to fail, we don't care about the result
LOG_W(TAG, "led_gpio_apply_levels(%s) failed", device->name);
}
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
rgb_led_gpio_disable(device);
auto* internal = GET_INTERNAL(device);
gpio_descriptor_release(internal->descriptor_red);
gpio_descriptor_release(internal->descriptor_green);
gpio_descriptor_release(internal->descriptor_blue);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
extern Module root_module;
Driver rgb_led_gpio_driver = {
.name = "rgb_led_gpio",
.compatible = (const char*[]) { "rgb-led-gpio", nullptr },
.start_device = start,
.stop_device = stop,
.api = &RGB_LED_GPIO_API,
.device_type = &RGB_LED_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
@@ -0,0 +1,157 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/rgb_led_pwm.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/pwm.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
#define TAG "RgbLedPwm"
#define GET_CONFIG(device) (static_cast<const RgbLedPwmConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<RgbLedPwmInternal*>(device_get_driver_data(device)))
extern "C" {
struct RgbLedPwmInternal {
RgbLedColor color;
bool enabled;
};
// region RgbLedApi
static error_t apply_channel(Device* pwm_device, uint8_t component) {
uint32_t period_ns;
error_t error = pwm_get_period(pwm_device, &period_ns);
if (error != ERROR_NONE) {
return error;
}
uint32_t duty_ns = (uint32_t)(((uint64_t)component * period_ns) / 255);
return pwm_set_duty(pwm_device, duty_ns);
}
static error_t apply_color(Device* device) {
const auto* config = GET_CONFIG(device);
const auto* internal = GET_INTERNAL(device);
error_t error = apply_channel(config->pwm_red, internal->color.r);
if (error == ERROR_NONE) {
error = apply_channel(config->pwm_green, internal->color.g);
}
if (error == ERROR_NONE) {
error = apply_channel(config->pwm_blue, internal->color.b);
}
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to apply LED color");
}
return error;
}
static error_t rgb_led_pwm_set_color(Device* device, RgbLedColor color) {
GET_INTERNAL(device)->color = color;
return apply_color(device);
}
static error_t rgb_led_pwm_get_color(Device* device, RgbLedColor* out_color) {
*out_color = GET_INTERNAL(device)->color;
return ERROR_NONE;
}
static error_t rgb_led_pwm_enable(Device* device) {
const auto* config = GET_CONFIG(device);
error_t error = pwm_enable(config->pwm_red);
if (error != ERROR_NONE) {
return error;
}
error = pwm_enable(config->pwm_green);
if (error != ERROR_NONE) {
pwm_disable(config->pwm_red);
return error;
}
error = pwm_enable(config->pwm_blue);
if (error != ERROR_NONE) {
pwm_disable(config->pwm_green);
pwm_disable(config->pwm_red);
LOG_E(TAG, "Failed to enable LED");
return error;
}
GET_INTERNAL(device)->enabled = true;
return ERROR_NONE;
}
static void rgb_led_pwm_disable(Device* device) {
const auto* config = GET_CONFIG(device);
GET_INTERNAL(device)->enabled = false;
pwm_disable(config->pwm_red);
pwm_disable(config->pwm_green);
pwm_disable(config->pwm_blue);
}
// endregion
static constexpr RgbLedApi RGB_LED_PWM_API = {
.set_color = rgb_led_pwm_set_color,
.get_color = rgb_led_pwm_get_color,
.enable = rgb_led_pwm_enable,
.disable = rgb_led_pwm_disable,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
auto* internal = new(std::nothrow) RgbLedPwmInternal {
.color = config->default_color,
.enabled = false
};
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
error_t error = apply_color(device);
if (error != ERROR_NONE) {
device_set_driver_data(device, nullptr);
delete internal;
return error;
}
if (config->enabled) {
rgb_led_pwm_enable(device); // Allowed to fail, we don't care about the result
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
rgb_led_pwm_disable(device);
auto* internal = GET_INTERNAL(device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
extern Module root_module;
Driver rgb_led_pwm_driver = {
.name = "rgb_led_pwm",
.compatible = (const char*[]) { "rgb-led-pwm", nullptr },
.start_device = start,
.stop_device = stop,
.api = &RGB_LED_PWM_API,
.device_type = &RGB_LED_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
@@ -1,28 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/spi_peripheral.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
static error_t start(Device*) { return ERROR_NONE; }
static error_t stop(Device*) { return ERROR_NONE; }
const DeviceType SPI_PERIPHERAL_TYPE = {
.name = "spi_peripheral"
};
extern Module root_module;
Driver spi_peripheral_driver = {
.name = "spi_peripheral",
.compatible = (const char*[]) { "spi-peripheral", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = &SPI_PERIPHERAL_TYPE,
.owner = &root_module,
.internal = nullptr
};
}
+8 -4
View File
@@ -16,16 +16,20 @@ static error_t start() {
if (driver_construct_add(&root_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver display_placeholder_driver;
if (driver_construct_add(&display_placeholder_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver pointer_placeholder_driver;
if (driver_construct_add(&pointer_placeholder_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver spi_peripheral_driver;
if (driver_construct_add(&spi_peripheral_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver battery_sense_driver;
if (driver_construct_add(&battery_sense_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver battery_sense_power_supply_driver;
if (driver_construct_add(&battery_sense_power_supply_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver gpio_hog_driver;
if (driver_construct_add(&gpio_hog_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver pwm_backlight_driver;
if (driver_construct_add(&pwm_backlight_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver gpio_backlight_driver;
if (driver_construct_add(&gpio_backlight_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver rgb_led_gpio_driver;
if (driver_construct_add(&rgb_led_gpio_driver) != ERROR_NONE) return ERROR_RESOURCE;
extern Driver rgb_led_pwm_driver;
if (driver_construct_add(&rgb_led_pwm_driver) != ERROR_NONE) return ERROR_RESOURCE;
return ERROR_NONE;
}
-3
View File
@@ -29,7 +29,6 @@
#include <tactility/drivers/rtc.h>
#include <tactility/drivers/sdcard.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/drivers/spi_peripheral.h>
#include <tactility/drivers/uart_controller.h>
#include <tactility/drivers/wifi.h>
#include <tactility/error.h>
@@ -243,8 +242,6 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(spi_controller_try_lock),
DEFINE_MODULE_SYMBOL(spi_controller_unlock),
DEFINE_MODULE_SYMBOL(SPI_CONTROLLER_TYPE),
// drivers/spi_peripheral
DEFINE_MODULE_SYMBOL(SPI_PERIPHERAL_TYPE),
// drivers/uart_controller
DEFINE_MODULE_SYMBOL(uart_controller_open),
DEFINE_MODULE_SYMBOL(uart_controller_close),