Skip to content

leds: Add support for setup/active mode GPIO outputs #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions components/config/enum.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <config.h>
#include <logging.h>

#include <string.h>

Expand All @@ -9,6 +10,12 @@ int config_enum_lookup(const struct config_enum *e, const char *name, const stru
*enump = e;
return 0;
}

if (e->alias && strcmp(e->alias, name) == 0) {
LOG_WARN("deprecated enum alias %s, renamed to %s", name, e->name);
*enump = e;
return 0;
}
}

return 1;
Expand Down Expand Up @@ -43,6 +50,12 @@ int config_enum_to_value(const struct config_enum *e, const char *name)
if (strcmp(e->name, name) == 0) {
return e->value;
}

if (e->alias && strcmp(e->alias, name) == 0) {
LOG_WARN("deprecated enum alias %s, renamed to %s", name, e->name);

return e->value;
}
}

return -1;
Expand Down
3 changes: 3 additions & 0 deletions components/config/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ enum config_type {
struct config_enum {
const char *name;

/* Migrate from old name */
const char *alias;

int value;
};

Expand Down
18 changes: 13 additions & 5 deletions components/i2s_out/esp32/pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ static void setup_gpio_pin(gpio_num_t gpio)
gpio_ll_pulldown_en(&GPIO, gpio);
}

static void disable_gpio_pin(gpio_num_t gpio)
static void clear_gpio_pin(gpio_num_t gpio)
{
gpio_ll_output_disable(&GPIO, gpio);
gpio_ll_set_level(&GPIO, gpio, 0);
}

int i2s_out_pin_init(struct i2s_out *i2s_out)
Expand Down Expand Up @@ -126,6 +126,7 @@ int i2s_out_pin_setup(struct i2s_out *i2s_out, const struct i2s_out_options *opt
case I2S_OUT_MODE_16BIT_SERIAL:
case I2S_OUT_MODE_32BIT_SERIAL:
if (options->bck_gpio > 0) {
i2s_out->bck_gpio_inv = options->bck_inv;
i2s_out->bck_gpios[0] = options->bck_gpio;

setup_gpio_pin(options->bck_gpio);
Expand Down Expand Up @@ -158,6 +159,7 @@ int i2s_out_pin_setup(struct i2s_out *i2s_out, const struct i2s_out_options *opt
case I2S_OUT_MODE_8BIT_PARALLEL:
for (int i = 0; i < I2S_OUT_PARALLEL_SIZE; i++) {
if (options->bck_gpios[i] > 0) {
i2s_out->bck_gpio_inv = options->bck_inv;
i2s_out->bck_gpios[i] = options->bck_gpios[i];

setup_gpio_pin(options->bck_gpios[i]);
Expand Down Expand Up @@ -203,19 +205,25 @@ void i2s_out_pin_teardown(struct i2s_out *i2s_out)

for (int i = 0; i < I2S_OUT_PARALLEL_SIZE; i++) {
if (i2s_out->bck_gpios[i] >= 0) {
disable_gpio_pin(i2s_out->bck_gpios[i]);
clear_gpio_pin(i2s_out->bck_gpios[i]);

esp_rom_gpio_connect_out_signal(i2s_out->bck_gpios[i], SIG_GPIO_OUT_IDX, i2s_out->bck_gpio_inv, false);

i2s_out->bck_gpios[i] = -1;
}

if (i2s_out->data_gpios[i] >= 0) {
disable_gpio_pin(i2s_out->data_gpios[i]);
clear_gpio_pin(i2s_out->data_gpios[i]);

esp_rom_gpio_connect_out_signal(i2s_out->data_gpios[i], SIG_GPIO_OUT_IDX, false, false);

i2s_out->data_gpios[i] = -1;
}

if (i2s_out->inv_data_gpios[i] >= 0) {
disable_gpio_pin(i2s_out->inv_data_gpios[i]);
clear_gpio_pin(i2s_out->inv_data_gpios[i]);

esp_rom_gpio_connect_out_signal(i2s_out->inv_data_gpios[i], SIG_GPIO_OUT_IDX, true, false);

i2s_out->inv_data_gpios[i] = -1;
}
Expand Down
1 change: 1 addition & 0 deletions components/i2s_out/i2s_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct i2s_out {
/* pin */
SemaphoreHandle_t pin_mutex;
#if CONFIG_IDF_TARGET_ESP32
bool bck_gpio_inv;
gpio_num_t bck_gpios[I2S_OUT_PARALLEL_SIZE];
gpio_num_t data_gpios[I2S_OUT_PARALLEL_SIZE];
gpio_num_t inv_data_gpios[I2S_OUT_PARALLEL_SIZE];
Expand Down
40 changes: 40 additions & 0 deletions components/leds/gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "gpio.h"

#if CONFIG_LEDS_GPIO_ENABLED
void leds_gpio_setup (const struct leds_interface_options_gpio *options)
{
if (!options->gpio_options) {
return;
}

switch (options->mode) {
case LEDS_GPIO_MODE_NONE:
break;

case LEDS_GPIO_MODE_SETUP:
case LEDS_GPIO_MODE_ACTIVE:
gpio_out_set(options->gpio_options, options->pins);
break;
}
}

void leds_gpio_close (const struct leds_interface_options_gpio *options)
{
if (!options->gpio_options) {
return;
}

switch (options->mode) {
case LEDS_GPIO_MODE_NONE:
break;

case LEDS_GPIO_MODE_SETUP:
break;

case LEDS_GPIO_MODE_ACTIVE:
gpio_out_clear(options->gpio_options);
break;
}

}
#endif
8 changes: 8 additions & 0 deletions components/leds/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <leds.h>

#if CONFIG_LEDS_GPIO_ENABLED
void leds_gpio_setup (const struct leds_interface_options_gpio *options);
void leds_gpio_close (const struct leds_interface_options_gpio *options);
#endif
14 changes: 12 additions & 2 deletions components/leds/include/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,24 @@ enum leds_interface leds_interface_for_protocol(enum leds_protocol protocol);

#if CONFIG_LEDS_GPIO_ENABLED
struct leds_interface_options_gpio {
enum leds_interface_gpio_mode {
LEDS_GPIO_MODE_NONE,

/* Set pins after interface setup, keep set after TX end */
LEDS_GPIO_MODE_SETUP,

/* Set pins after interface setup, clear at TX end */
LEDS_GPIO_MODE_ACTIVE,
} mode;

/**
* GPIO used for output multiplexing.
* The `gpio_out_pins` will be set before TX start, and cleared after TX end.
* The `pins` will be set.
* Any other `gpio_options->pins` will be cleared.
*/
struct gpio_options *gpio_options;

gpio_pins_t gpio_out_pins;
gpio_pins_t pins;
};
#endif

Expand Down
5 changes: 2 additions & 3 deletions components/leds/interfaces/i2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ struct leds_interface_i2s {

struct i2s_out *i2s_out;
struct i2s_out_options i2s_out_options;

struct gpio_options *gpio_options;
gpio_pins_t gpio_out_pins;

struct leds_interface_options_gpio gpio;
};

size_t leds_interface_i2s_buffer_size(enum leds_interface_i2s_mode mode, unsigned led_count, unsigned pin_count);
Expand Down
12 changes: 4 additions & 8 deletions components/leds/interfaces/i2s/tx.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../i2s.h"
#include "../../leds.h"
#include "../../stats.h"
#include "../../gpio.h"

#include <logging.h>

Expand Down Expand Up @@ -288,8 +289,7 @@ int leds_interface_i2s_init(struct leds_interface_i2s *interface, const struct l
interface->i2s_out_options.inv_data_gpio = options->inv_data_pin;
#endif

interface->gpio_options = options->gpio.gpio_options;
interface->gpio_out_pins = options->gpio.gpio_out_pins;
interface->gpio = options->gpio;

return 0;
}
Expand Down Expand Up @@ -332,9 +332,7 @@ int leds_interface_i2s_tx(struct leds_interface_i2s *interface, const struct led
}

#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_set(interface->gpio_options, interface->gpio_out_pins);
}
leds_gpio_setup(&interface->gpio);
#endif

WITH_STATS_TIMER(&stats->write) {
Expand All @@ -352,9 +350,7 @@ int leds_interface_i2s_tx(struct leds_interface_i2s *interface, const struct led

error:
#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_clear(interface->gpio_options);
}
leds_gpio_close(&interface->gpio);
#endif

if ((err = i2s_out_close(interface->i2s_out))) {
Expand Down
3 changes: 1 addition & 2 deletions components/leds/interfaces/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
spi_device_handle_t device;
#endif

struct gpio_options *gpio_options;
gpio_pins_t gpio_out_pins;
struct leds_interface_options_gpio gpio;
};

size_t leds_interface_spi_buffer_size(enum leds_interface_spi_mode mode, unsigned count);
Expand Down
12 changes: 4 additions & 8 deletions components/leds/interfaces/spi/interface.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../spi.h"
#include "../../leds.h"
#include "../../stats.h"
#include "../../gpio.h"

#include <logging.h>

Expand Down Expand Up @@ -189,8 +190,7 @@ int leds_interface_spi_init(struct leds_interface_spi *interface, const struct l
return err;
}

interface->gpio_options = options->gpio.gpio_options;
interface->gpio_out_pins = options->gpio.gpio_out_pins;
interface->gpio = options->gpio;

return 0;
}
Expand Down Expand Up @@ -302,9 +302,7 @@ int leds_interface_spi_tx(struct leds_interface_spi *interface, const struct led
}

#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_set(interface->gpio_options, interface->gpio_out_pins);
}
leds_gpio_setup(&interface->gpio);
#endif

WITH_STATS_TIMER(&stats->tx) {
Expand All @@ -327,9 +325,7 @@ int leds_interface_spi_tx(struct leds_interface_spi *interface, const struct led

error:
#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_clear(interface->gpio_options);
}
leds_gpio_close(&interface->gpio);
#endif

if ((err = leds_interface_spi_master_close(interface))) {
Expand Down
3 changes: 1 addition & 2 deletions components/leds/interfaces/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
struct uart *uart;
struct uart_options uart_options;

struct gpio_options *gpio_options;
gpio_pins_t gpio_out_pins;
struct leds_interface_options_gpio gpio;
};

int leds_interface_uart_init(struct leds_interface_uart *interface, const struct leds_interface_uart_options *options, enum leds_interface_uart_mode mode, union leds_interface_uart_func func);
Expand Down
12 changes: 4 additions & 8 deletions components/leds/interfaces/uart/tx.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../uart.h"
#include "../../leds.h"
#include "../../stats.h"
#include "../../gpio.h"

#include <logging.h>

Expand Down Expand Up @@ -56,8 +57,7 @@ int leds_interface_uart_init(struct leds_interface_uart *interface, const struct

interface->uart_options.pin_mutex = options->pin_mutex;

interface->gpio_options = options->gpio.gpio_options;
interface->gpio_out_pins = options->gpio.gpio_out_pins;
interface->gpio = options->gpio;

return 0;
}
Expand Down Expand Up @@ -116,9 +116,7 @@ int leds_interface_uart_tx(struct leds_interface_uart *interface, const struct l
}

#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_set(interface->gpio_options, interface->gpio_out_pins);
}
leds_gpio_setup(&interface->gpio);
#endif

WITH_STATS_TIMER(&stats->tx) {
Expand All @@ -143,9 +141,7 @@ int leds_interface_uart_tx(struct leds_interface_uart *interface, const struct l

error:
#if CONFIG_LEDS_GPIO_ENABLED
if (interface->gpio_options) {
gpio_out_clear(interface->gpio_options);
}
leds_gpio_close(&interface->gpio);
#endif

if ((err = uart_close(interface->uart))) {
Expand Down
6 changes: 3 additions & 3 deletions main/atx_psu_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
struct atx_psu_config atx_psu_config = {};

const struct config_enum atx_psu_gpio_mode_enum[] = {
{ "", ATX_PSU_GPIO_MODE_DISABLED },
{ "LOW", ATX_PSU_GPIO_MODE_LOW },
{ "HIGH", ATX_PSU_GPIO_MODE_HIGH },
{ "", .value = ATX_PSU_GPIO_MODE_DISABLED },
{ "LOW", .value = ATX_PSU_GPIO_MODE_LOW },
{ "HIGH", .value = ATX_PSU_GPIO_MODE_HIGH },
{}
};

Expand Down
16 changes: 8 additions & 8 deletions main/dmx_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ struct dmx_input_config dmx_input_config = {};
struct dmx_output_config dmx_output_configs[DMX_OUTPUT_COUNT] = {};

const struct config_enum dmx_uart_enum[] = {
{ "", -1 },
{ "", .value = -1 },
#if defined(UART_0) && CONFIG_ESP_CONSOLE_UART_NUM != 0
{ "UART0", UART_0 },
{ "UART0", .value = UART_0 },
#endif
#if defined(UART_0_SWAP) && CONFIG_ESP_CONSOLE_UART_NUM != 0
{ "UART0_SWAP", UART_0_SWAP }, // ESP8266 specialty
{ "UART0_SWAP", .value = UART_0_SWAP }, // ESP8266 specialty
#endif
#if defined(UART_1) && CONFIG_ESP_CONSOLE_UART_NUM != 1
{ "UART1", UART_1 },
{ "UART1", .value = UART_1 },
#endif
#if defined(UART_2) && CONFIG_ESP_CONSOLE_UART_NUM != 2
{ "UART2", UART_2 },
{ "UART2", .value = UART_2 },
#endif
{}
};
Expand All @@ -40,9 +40,9 @@ const struct config_enum dmx_uart_enum[] = {
#endif

const struct config_enum dmx_gpio_mode_enum[] = {
{ "", DMX_GPIO_MODE_DISABLED },
{ "LOW", DMX_GPIO_MODE_LOW },
{ "HIGH", DMX_GPIO_MODE_HIGH },
{ "", .value = DMX_GPIO_MODE_DISABLED },
{ "LOW", .value = DMX_GPIO_MODE_LOW },
{ "HIGH", .value = DMX_GPIO_MODE_HIGH },
{}
};

Expand Down
Loading