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:
committed by
GitHub
parent
6fb2bb736c
commit
3b5a401594
@@ -1,16 +0,0 @@
|
||||
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: "espressif,esp32-gpio-backlight"
|
||||
|
||||
properties:
|
||||
pin-backlight:
|
||||
type: phandles
|
||||
required: true
|
||||
description: Backlight enable output pin
|
||||
default-on:
|
||||
type: boolean
|
||||
default: true
|
||||
description: Whether the backlight is turned on by set_brightness_default()
|
||||
@@ -1,29 +0,0 @@
|
||||
description: ESP32 LEDC-backed PWM backlight
|
||||
|
||||
compatible: "espressif,esp32-ledc-backlight"
|
||||
|
||||
properties:
|
||||
pin-backlight:
|
||||
type: phandles
|
||||
required: true
|
||||
description: Backlight PWM output pin
|
||||
frequency-hz:
|
||||
type: int
|
||||
default: 30000
|
||||
description: PWM frequency in Hz
|
||||
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.
|
||||
ledc-timer:
|
||||
type: int
|
||||
default: 0
|
||||
description: LEDC timer index, defined by ledc_timer_t
|
||||
ledc-channel:
|
||||
type: int
|
||||
default: 0
|
||||
description: LEDC channel index, defined by ledc_channel_t
|
||||
@@ -0,0 +1,33 @@
|
||||
description: ESP32 LEDC-backed generic PWM output
|
||||
|
||||
compatible: "espressif,esp32-pwm-ledc"
|
||||
|
||||
properties:
|
||||
pin:
|
||||
type: phandles
|
||||
required: true
|
||||
description: PWM output pin
|
||||
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
|
||||
duty-resolution:
|
||||
type: int
|
||||
default: 10
|
||||
description: LEDC duty resolution in bits, defined by ledc_timer_bit_t
|
||||
ledc-timer:
|
||||
type: int
|
||||
required: true
|
||||
description: LEDC timer index, defined by ledc_timer_t
|
||||
ledc-channel:
|
||||
type: int
|
||||
required: true
|
||||
description: LEDC channel index, defined by ledc_channel_t
|
||||
@@ -1,15 +0,0 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <tactility/drivers/esp32_ledc_backlight.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(esp32_ledc_backlight, struct Esp32LedcBacklightConfig)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
+2
-2
@@ -2,13 +2,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <tactility/drivers/esp32_gpio_backlight.h>
|
||||
#include <tactility/drivers/esp32_pwm_ledc.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(esp32_gpio_backlight, struct Esp32GpioBacklightConfig)
|
||||
DEFINE_DEVICETREE(esp32_pwm_ledc, struct Esp32PwmLedcConfig)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/drivers/backlight.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Esp32GpioBacklightConfig {
|
||||
struct GpioPinSpec pin_backlight;
|
||||
bool default_on;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
+7
-6
@@ -2,18 +2,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <driver/ledc.h>
|
||||
#include <tactility/drivers/backlight.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
#include <tactility/drivers/pwm.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Esp32LedcBacklightConfig {
|
||||
struct GpioPinSpec pin_backlight;
|
||||
uint32_t frequency_hz;
|
||||
struct BrightnessLevelRange brightness_range;
|
||||
uint8_t brightness_default;
|
||||
struct Esp32PwmLedcConfig {
|
||||
struct GpioPinSpec pin;
|
||||
uint32_t period_ns;
|
||||
uint32_t duty_ns;
|
||||
bool inverted;
|
||||
ledc_timer_bit_t duty_resolution;
|
||||
ledc_timer_t ledc_timer;
|
||||
ledc_channel_t ledc_channel;
|
||||
};
|
||||
@@ -1,119 +0,0 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/esp32_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 <cstdlib>
|
||||
|
||||
#define TAG "Esp32GpioBacklight"
|
||||
#define GET_CONFIG(device) (static_cast<const Esp32GpioBacklightConfig*>((device)->config))
|
||||
|
||||
struct Esp32GpioBacklightInternal {
|
||||
GpioDescriptor* descriptor;
|
||||
uint8_t brightness;
|
||||
};
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
auto* descriptor = gpio_descriptor_acquire(config->pin_backlight.gpio_controller, config->pin_backlight.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_backlight.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 = static_cast<Esp32GpioBacklightInternal*>(malloc(sizeof(Esp32GpioBacklightInternal)));
|
||||
if (internal == nullptr) {
|
||||
gpio_descriptor_release(descriptor);
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->descriptor = descriptor;
|
||||
internal->brightness = 0;
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
backlight_set_brightness(device, 0); // Allowed to fail, we don't care about the result
|
||||
|
||||
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
|
||||
gpio_descriptor_release(internal->descriptor);
|
||||
free(internal);
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region BacklightApi
|
||||
|
||||
static error_t esp32_gpio_backlight_set_brightness(Device* device, uint8_t brightness) {
|
||||
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
|
||||
|
||||
error_t error = gpio_descriptor_set_level(internal->descriptor, brightness > 0);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to set backlight level");
|
||||
return error;
|
||||
}
|
||||
|
||||
internal->brightness = brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_gpio_backlight_set_brightness_default(Device* device) {
|
||||
return esp32_gpio_backlight_set_brightness(device, GET_CONFIG(device)->default_on ? 1 : 0);
|
||||
}
|
||||
|
||||
static error_t esp32_gpio_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
|
||||
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
|
||||
*out_brightness = internal->brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static uint8_t esp32_gpio_backlight_get_min_brightness(Device*) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t esp32_gpio_backlight_get_max_brightness(Device*) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static const BacklightApi esp32_gpio_backlight_api = {
|
||||
.set_brightness = esp32_gpio_backlight_set_brightness,
|
||||
.set_brightness_default = esp32_gpio_backlight_set_brightness_default,
|
||||
.get_brightness = esp32_gpio_backlight_get_brightness,
|
||||
.get_min_brightness = esp32_gpio_backlight_get_min_brightness,
|
||||
.get_max_brightness = esp32_gpio_backlight_get_max_brightness,
|
||||
};
|
||||
|
||||
extern Module platform_esp32_module;
|
||||
|
||||
Driver esp32_gpio_backlight_driver = {
|
||||
.name = "esp32_gpio_backlight",
|
||||
.compatible = (const char*[]) { "espressif,esp32-gpio-backlight", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &esp32_gpio_backlight_api,
|
||||
.device_type = &BACKLIGHT_TYPE,
|
||||
.owner = &platform_esp32_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
@@ -1,139 +0,0 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/esp32_ledc_backlight.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/backlight.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <driver/ledc.h>
|
||||
#include <esp_err.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#define TAG "Esp32LedcBacklight"
|
||||
#define GET_CONFIG(device) (static_cast<const Esp32LedcBacklightConfig*>((device)->config))
|
||||
|
||||
struct Esp32LedcBacklightInternal {
|
||||
uint8_t brightness;
|
||||
};
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
ledc_timer_config_t timer_config = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = LEDC_TIMER_8_BIT,
|
||||
.timer_num = config->ledc_timer,
|
||||
.freq_hz = config->frequency_hz,
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false,
|
||||
};
|
||||
if (ledc_timer_config(&timer_config) != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to configure LEDC timer");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
ledc_channel_config_t channel_config = {
|
||||
.gpio_num = (int)config->pin_backlight.pin,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.channel = config->ledc_channel,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = config->ledc_timer,
|
||||
.duty = config->brightness_range.min,
|
||||
.hpoint = 0,
|
||||
.sleep_mode = LEDC_SLEEP_MODE_NO_ALIVE_NO_PD,
|
||||
.flags = {
|
||||
.output_invert = 0,
|
||||
},
|
||||
};
|
||||
if (ledc_channel_config(&channel_config) != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to configure LEDC channel");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* internal = static_cast<Esp32LedcBacklightInternal*>(malloc(sizeof(Esp32LedcBacklightInternal)));
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->brightness = config->brightness_range.min;
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
backlight_set_brightness(device, 0); // Allowed to fail, we don't care about the result
|
||||
|
||||
auto* internal = static_cast<Esp32LedcBacklightInternal*>(device_get_driver_data(device));
|
||||
free(internal);
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region BacklightApi
|
||||
|
||||
static error_t esp32_ledc_backlight_set_brightness(Device* device, uint8_t brightness) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
auto* internal = static_cast<Esp32LedcBacklightInternal*>(device_get_driver_data(device));
|
||||
|
||||
esp_err_t ret = ledc_set_duty(LEDC_LOW_SPEED_MODE, config->ledc_channel, brightness);
|
||||
if (ret == ESP_OK) {
|
||||
ret = ledc_update_duty(LEDC_LOW_SPEED_MODE, config->ledc_channel);
|
||||
}
|
||||
if (ret != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to set brightness: %s", esp_err_to_name(ret));
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
internal->brightness = brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_ledc_backlight_set_brightness_default(Device* device) {
|
||||
return esp32_ledc_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
|
||||
}
|
||||
|
||||
static error_t esp32_ledc_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
|
||||
auto* internal = static_cast<Esp32LedcBacklightInternal*>(device_get_driver_data(device));
|
||||
*out_brightness = internal->brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static uint8_t esp32_ledc_backlight_get_min_brightness(Device* device) {
|
||||
return GET_CONFIG(device)->brightness_range.min;
|
||||
}
|
||||
|
||||
static uint8_t esp32_ledc_backlight_get_max_brightness(Device* device) {
|
||||
return GET_CONFIG(device)->brightness_range.max;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static const BacklightApi esp32_ledc_backlight_api = {
|
||||
.set_brightness = esp32_ledc_backlight_set_brightness,
|
||||
.set_brightness_default = esp32_ledc_backlight_set_brightness_default,
|
||||
.get_brightness = esp32_ledc_backlight_get_brightness,
|
||||
.get_min_brightness = esp32_ledc_backlight_get_min_brightness,
|
||||
.get_max_brightness = esp32_ledc_backlight_get_max_brightness,
|
||||
};
|
||||
|
||||
extern Module platform_esp32_module;
|
||||
|
||||
Driver esp32_ledc_backlight_driver = {
|
||||
.name = "esp32_ledc_backlight",
|
||||
.compatible = (const char*[]) { "espressif,esp32-ledc-backlight", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &esp32_ledc_backlight_api,
|
||||
.device_type = &BACKLIGHT_TYPE,
|
||||
.owner = &platform_esp32_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
@@ -0,0 +1,253 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/esp32_pwm_ledc.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/pwm.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <driver/ledc.h>
|
||||
#include <esp_err.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#define TAG "Esp32PwmLedc"
|
||||
#define GET_CONFIG(device) (static_cast<const Esp32PwmLedcConfig*>((device)->config))
|
||||
#define GET_INTERNAL(device) (static_cast<Esp32PwmLedcInternal*>(device_get_driver_data(device)))
|
||||
|
||||
struct Esp32PwmLedcInternal {
|
||||
uint32_t period_ns;
|
||||
uint32_t duty_ns;
|
||||
bool inverted;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
// region Helpers
|
||||
|
||||
static uint32_t compute_freq_hz(uint32_t period_ns) {
|
||||
return period_ns > 0 ? (uint32_t)(1000000000ULL / period_ns) : 0;
|
||||
}
|
||||
|
||||
static uint32_t compute_raw_duty(uint32_t duty_ns, uint32_t period_ns, ledc_timer_bit_t duty_resolution) {
|
||||
if (period_ns == 0) return 0;
|
||||
uint64_t max_duty = 1ULL << duty_resolution;
|
||||
uint64_t raw_duty = ((uint64_t)duty_ns * max_duty) / period_ns;
|
||||
return (uint32_t)(raw_duty > max_duty ? max_duty : raw_duty);
|
||||
}
|
||||
|
||||
// Reprograms the LEDC timer's frequency/resolution. Independent of the enabled state: it doesn't
|
||||
// touch the channel's signal-output-enable bit, so it's safe to call while output is stopped.
|
||||
static error_t apply_period(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
const auto* internal = GET_INTERNAL(device);
|
||||
|
||||
ledc_timer_config_t timer_config = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = config->duty_resolution,
|
||||
.timer_num = config->ledc_timer,
|
||||
.freq_hz = compute_freq_hz(internal->period_ns),
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false,
|
||||
};
|
||||
if (ledc_timer_config(&timer_config) != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to configure LEDC timer");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// ledc_update_duty() unconditionally re-enables the channel's signal output, so this only
|
||||
// touches hardware while the device is enabled; a pending duty/period change made while disabled
|
||||
// is picked up from internal state the next time enable() is called.
|
||||
static error_t apply_duty(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
const auto* internal = GET_INTERNAL(device);
|
||||
if (!internal->enabled) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
uint32_t raw_duty = compute_raw_duty(internal->duty_ns, internal->period_ns, config->duty_resolution);
|
||||
esp_err_t ret = ledc_set_duty(LEDC_LOW_SPEED_MODE, config->ledc_channel, raw_duty);
|
||||
if (ret == ESP_OK) {
|
||||
ret = ledc_update_duty(LEDC_LOW_SPEED_MODE, config->ledc_channel);
|
||||
}
|
||||
if (ret != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to set duty: %s", esp_err_to_name(ret));
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// Rebuilds the LEDC channel (duty, output polarity, timer/pin binding) from current internal
|
||||
// state. Like ledc_update_duty(), this unconditionally re-enables the channel's signal output,
|
||||
// so callers must only invoke this while the device is meant to be enabled.
|
||||
static error_t apply_channel(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
const auto* internal = GET_INTERNAL(device);
|
||||
|
||||
ledc_channel_config_t channel_config = {
|
||||
.gpio_num = (int)config->pin.pin,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.channel = config->ledc_channel,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = config->ledc_timer,
|
||||
.duty = compute_raw_duty(internal->duty_ns, internal->period_ns, config->duty_resolution),
|
||||
.hpoint = 0,
|
||||
.sleep_mode = LEDC_SLEEP_MODE_NO_ALIVE_NO_PD,
|
||||
.flags = {
|
||||
.output_invert = internal->inverted ? 1u : 0u,
|
||||
},
|
||||
};
|
||||
if (ledc_channel_config(&channel_config) != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to configure LEDC channel");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
// Nothing here touches LEDC hardware: period/duty/inverted may be overridden via the PwmApi
|
||||
// before the first enable() call, so construction only needs to seed tracked state from config.
|
||||
// enable() is what actually programs the timer and channel from that tracked state.
|
||||
static error_t start(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
auto* internal = static_cast<Esp32PwmLedcInternal*>(malloc(sizeof(Esp32PwmLedcInternal)));
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->period_ns = config->period_ns;
|
||||
internal->duty_ns = config->duty_ns;
|
||||
internal->inverted = config->inverted;
|
||||
internal->enabled = false;
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = GET_INTERNAL(device);
|
||||
if (internal->enabled) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
ledc_stop(LEDC_LOW_SPEED_MODE, config->ledc_channel, 0); // Allowed to fail, we don't care about the result
|
||||
}
|
||||
|
||||
device_set_driver_data(device, nullptr);
|
||||
free(internal);
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region PwmApi
|
||||
|
||||
static error_t esp32_pwm_ledc_set_period(Device* device, uint32_t period_ns) {
|
||||
GET_INTERNAL(device)->period_ns = period_ns;
|
||||
error_t error = apply_period(device);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
return apply_duty(device);
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_get_period(Device* device, uint32_t* period_ns) {
|
||||
*period_ns = GET_INTERNAL(device)->period_ns;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_set_duty(Device* device, uint32_t duty_ns) {
|
||||
GET_INTERNAL(device)->duty_ns = duty_ns;
|
||||
return apply_duty(device);
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_get_duty(Device* device, uint32_t* duty_ns) {
|
||||
*duty_ns = GET_INTERNAL(device)->duty_ns;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_set_inverted(Device* device, bool inverted) {
|
||||
auto* internal = GET_INTERNAL(device);
|
||||
internal->inverted = inverted;
|
||||
|
||||
// While disabled, just track the override; apply_channel() rebuilds the channel with it
|
||||
// (and every other tracked setting) the next time enable() runs.
|
||||
if (!internal->enabled) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
return apply_channel(device);
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_is_inverted(Device* device, bool* inverted) {
|
||||
*inverted = GET_INTERNAL(device)->inverted;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// Applies the tracked period, duty and inverted settings (whether they came from the config
|
||||
// defaults or were overridden beforehand) and turns the output on.
|
||||
static error_t esp32_pwm_ledc_enable(Device* device) {
|
||||
error_t error = apply_period(device);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
error = apply_channel(device);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
GET_INTERNAL(device)->enabled = true;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_disable(Device* device) {
|
||||
auto* internal = GET_INTERNAL(device);
|
||||
if (!internal->enabled) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
const auto* config = GET_CONFIG(device);
|
||||
internal->enabled = false;
|
||||
|
||||
if (ledc_stop(LEDC_LOW_SPEED_MODE, config->ledc_channel, 0) != ESP_OK) {
|
||||
LOG_E(TAG, "Failed to stop LEDC channel");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t esp32_pwm_ledc_is_enabled(Device* device, bool* enabled) {
|
||||
*enabled = GET_INTERNAL(device)->enabled;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static const PwmApi esp32_pwm_ledc_api = {
|
||||
.set_period = esp32_pwm_ledc_set_period,
|
||||
.get_period = esp32_pwm_ledc_get_period,
|
||||
.set_duty = esp32_pwm_ledc_set_duty,
|
||||
.get_duty = esp32_pwm_ledc_get_duty,
|
||||
.set_inverted = esp32_pwm_ledc_set_inverted,
|
||||
.is_inverted = esp32_pwm_ledc_is_inverted,
|
||||
.enable = esp32_pwm_ledc_enable,
|
||||
.disable = esp32_pwm_ledc_disable,
|
||||
.is_enabled = esp32_pwm_ledc_is_enabled,
|
||||
};
|
||||
|
||||
extern Module platform_esp32_module;
|
||||
|
||||
Driver esp32_pwm_ledc_driver = {
|
||||
.name = "esp32_pwm_ledc",
|
||||
.compatible = (const char*[]) { "espressif,esp32-pwm-ledc", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &esp32_pwm_ledc_api,
|
||||
.device_type = &PWM_TYPE,
|
||||
.owner = &platform_esp32_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
@@ -19,8 +19,7 @@ extern Driver esp32_i2s_driver;
|
||||
#if SOC_LCD_I80_SUPPORTED
|
||||
extern Driver esp32_i8080_driver;
|
||||
#endif
|
||||
extern Driver esp32_gpio_backlight_driver;
|
||||
extern Driver esp32_ledc_backlight_driver;
|
||||
extern Driver esp32_pwm_ledc_driver;
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
extern Driver esp32_sdmmc_driver;
|
||||
#endif
|
||||
@@ -56,8 +55,7 @@ static error_t start() {
|
||||
#if SOC_LCD_I80_SUPPORTED
|
||||
check(driver_construct_add(&esp32_i8080_driver) == ERROR_NONE);
|
||||
#endif
|
||||
check(driver_construct_add(&esp32_gpio_backlight_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&esp32_ledc_backlight_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&esp32_pwm_ledc_driver) == ERROR_NONE);
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
check(driver_construct_add(&esp32_sdmmc_driver) == ERROR_NONE);
|
||||
#endif
|
||||
@@ -111,8 +109,7 @@ static error_t stop() {
|
||||
#if SOC_LCD_I80_SUPPORTED
|
||||
check(driver_remove_destruct(&esp32_i8080_driver) == ERROR_NONE);
|
||||
#endif
|
||||
check(driver_remove_destruct(&esp32_ledc_backlight_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&esp32_gpio_backlight_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&esp32_pwm_ledc_driver) == ERROR_NONE);
|
||||
#if SOC_SDMMC_HOST_SUPPORTED
|
||||
check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user