GPIO descriptor fixes (#578)
This commit is contained in:
committed by
GitHub
parent
a3fda9ad8f
commit
8b92aa8e5a
@@ -90,18 +90,12 @@ static constexpr BacklightApi GPIO_BACKLIGHT_API = {
|
||||
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);
|
||||
auto* descriptor = gpio_descriptor_acquire(config->pin.gpio_controller, config->pin.pin, config->pin.flags | GPIO_FLAG_DIRECTION_OUTPUT, 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);
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/gpio_descriptor.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <new>
|
||||
|
||||
constexpr auto* TAG = "gpio";
|
||||
#define GPIO_INTERNAL_API(driver) ((struct GpioControllerApi*)(driver)->api)
|
||||
|
||||
extern "C" {
|
||||
@@ -31,6 +32,7 @@ struct GpioControllerData {
|
||||
for (uint32_t i = 0; i < pin_count; ++i) {
|
||||
descriptors[i].controller = device;
|
||||
descriptors[i].pin = static_cast<gpio_pin_t>(i);
|
||||
descriptors[i].flags = 0;
|
||||
descriptors[i].owner_type = GPIO_OWNER_NONE;
|
||||
descriptors[i].controller_context = this->controller_context;
|
||||
}
|
||||
@@ -48,6 +50,7 @@ struct GpioControllerData {
|
||||
GpioDescriptor* gpio_descriptor_acquire(
|
||||
Device* controller,
|
||||
gpio_pin_t pin_number,
|
||||
gpio_flags_t flags,
|
||||
GpioOwnerType owner
|
||||
) {
|
||||
check(owner != GPIO_OWNER_NONE);
|
||||
@@ -56,22 +59,48 @@ GpioDescriptor* gpio_descriptor_acquire(
|
||||
|
||||
mutex_lock(&data->mutex);
|
||||
if (pin_number >= data->pin_count) {
|
||||
LOG_E(TAG, "%s: pin %lu is out of range (> %lu)", controller->name, pin_number, data->pin_count);
|
||||
mutex_unlock(&data->mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GpioDescriptor* desc = &data->descriptors[pin_number];
|
||||
if (desc->owner_type != GPIO_OWNER_NONE) {
|
||||
LOG_E(TAG, "%s: pin %lu is in use", controller->name, pin_number);
|
||||
mutex_unlock(&data->mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
desc->owner_type = owner;
|
||||
desc->flags = flags;
|
||||
mutex_unlock(&data->mutex);
|
||||
|
||||
// Init flags by implementation
|
||||
auto init_result = gpio_descriptor_set_flags(desc, flags);
|
||||
if (init_result != ERROR_NONE) {
|
||||
LOG_E(TAG, "%s: pin %d failed to set flags to %08lx", controller->name, pin_number, flags);
|
||||
mutex_lock(&data->mutex);
|
||||
desc->owner_type = GPIO_OWNER_NONE;
|
||||
desc->flags = 0;
|
||||
mutex_unlock(&data->mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
GpioDescriptor* gpio_descriptor_acquire_pin_spec(
|
||||
GpioPinSpec const* pin_spec,
|
||||
GpioOwnerType owner
|
||||
) {
|
||||
return gpio_descriptor_acquire(
|
||||
pin_spec->gpio_controller,
|
||||
pin_spec->pin,
|
||||
pin_spec->flags,
|
||||
owner
|
||||
);
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_release(GpioDescriptor* descriptor) {
|
||||
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(descriptor->controller));
|
||||
mutex_lock(&data->mutex);
|
||||
@@ -133,6 +162,17 @@ error_t gpio_descriptor_get_flags(GpioDescriptor* descriptor, gpio_flags_t* flag
|
||||
return GPIO_INTERNAL_API(driver)->get_flags(descriptor, flags);
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_has_flags(GpioDescriptor* descriptor, gpio_flags_t flags, bool* has_flags) {
|
||||
const auto* driver = device_get_driver(descriptor->controller);
|
||||
gpio_flags_t available_flags = 0;
|
||||
error_t result = GPIO_INTERNAL_API(driver)->get_flags(descriptor, &available_flags);
|
||||
if (result != ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
*has_flags = (available_flags & flags) == flags;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_get_pin_number(GpioDescriptor* descriptor, gpio_pin_t* pin) {
|
||||
*pin = descriptor->pin;
|
||||
return ERROR_NONE;
|
||||
|
||||
@@ -18,32 +18,31 @@ const DeviceType GPIO_HOG_TYPE { .name = "gpio_hog" };
|
||||
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_HOG);
|
||||
gpio_flags_t flags = 0;
|
||||
bool initial_high = false;
|
||||
switch (config->mode) {
|
||||
case GPIO_HOG_MODE_OUTPUT_HIGH:
|
||||
flags = GPIO_FLAG_DIRECTION_OUTPUT;
|
||||
initial_high = true;
|
||||
break;
|
||||
case GPIO_HOG_MODE_OUTPUT_LOW:
|
||||
flags = GPIO_FLAG_DIRECTION_OUTPUT;
|
||||
break;
|
||||
case GPIO_HOG_MODE_INPUT:
|
||||
flags = GPIO_FLAG_DIRECTION_INPUT;
|
||||
break;
|
||||
default:
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* descriptor = gpio_descriptor_acquire(config->pin.gpio_controller, config->pin.pin, config->pin.flags | flags, GPIO_OWNER_HOG);
|
||||
if (descriptor == nullptr) {
|
||||
LOG_E(TAG, "Failed to acquire GPIO descriptor");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
switch (config->mode) {
|
||||
case GPIO_HOG_MODE_OUTPUT_HIGH:
|
||||
ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE &&
|
||||
gpio_descriptor_set_level(descriptor, true) == ERROR_NONE;
|
||||
break;
|
||||
case GPIO_HOG_MODE_OUTPUT_LOW:
|
||||
ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE &&
|
||||
gpio_descriptor_set_level(descriptor, false) == ERROR_NONE;
|
||||
break;
|
||||
case GPIO_HOG_MODE_INPUT:
|
||||
ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_INPUT) == ERROR_NONE;
|
||||
break;
|
||||
default:
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
LOG_E(TAG, "Failed to configure hogged pin %u", config->pin.pin);
|
||||
if (config->mode != GPIO_HOG_MODE_INPUT && gpio_descriptor_set_level(descriptor, initial_high) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to set initial level to %d", (int)initial_high);
|
||||
gpio_descriptor_release(descriptor);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/rgb_led.h>
|
||||
#include <tactility/drivers/rgb_led_gpio.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/gpio_descriptor.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
#include <new>
|
||||
|
||||
#define TAG "RgbLedGpio"
|
||||
constexpr auto* TAG = "RgbLedGpio";
|
||||
#define GET_CONFIG(device) (static_cast<const RgbLedGpioConfig*>((device)->config))
|
||||
#define GET_INTERNAL(device) (static_cast<RgbLedGpioInternal*>(device_get_driver_data(device)))
|
||||
|
||||
@@ -77,20 +79,20 @@ static constexpr RgbLedApi RGB_LED_GPIO_API = {
|
||||
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);
|
||||
auto* descriptor_red = gpio_descriptor_acquire(config->pin_red.gpio_controller, config->pin_red.pin, config->pin_red.flags | GPIO_FLAG_DIRECTION_OUTPUT, 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);
|
||||
auto* descriptor_green = gpio_descriptor_acquire(config->pin_green.gpio_controller, config->pin_green.pin, config->pin_green.flags | GPIO_FLAG_DIRECTION_OUTPUT, 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);
|
||||
auto* descriptor_blue = gpio_descriptor_acquire(config->pin_blue.gpio_controller, config->pin_blue.pin, config->pin_blue.flags | GPIO_FLAG_DIRECTION_OUTPUT, GPIO_OWNER_GPIO);
|
||||
if (descriptor_blue == nullptr) {
|
||||
LOG_E(TAG, "Failed to acquire blue GPIO descriptor");
|
||||
gpio_descriptor_release(descriptor_red);
|
||||
@@ -98,17 +100,6 @@ static error_t start(Device* device) {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user