diff --git a/components/services/gpio_exp.c b/components/services/gpio_exp.c index cada03b2..cf7bf62c 100644 --- a/components/services/gpio_exp.c +++ b/components/services/gpio_exp.c @@ -102,8 +102,8 @@ struct gpio_exp_s *gpio_exp_get_expander(int gpio) { struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config) { struct gpio_exp_s *expander = expanders + n_expanders; - if (config->base < GPIO_EXP_BASE_MIN || n_expanders == sizeof(expanders)/sizeof(struct gpio_exp_s)) { - ESP_LOGE(TAG, "Base %d GPIO must be > %d for %s or too many expanders %d", config->base, GPIO_EXP_BASE_MIN, config->model, n_expanders); + if (config->base < GPIO_NUM_MAX || n_expanders == sizeof(expanders)/sizeof(struct gpio_exp_s)) { + ESP_LOGE(TAG, "Base %d GPIO must be at least %d for %s or too many expanders %d", config->base, GPIO_NUM_MAX, config->model, n_expanders); return NULL; } @@ -305,22 +305,22 @@ void gpio_exp_enumerate(gpio_exp_enumerator enumerator, struct gpio_exp_s *expan * Wrapper function */ esp_err_t gpio_set_pull_mode_u(int gpio, gpio_pull_mode_t mode) { - if (gpio < GPIO_EXP_BASE_MIN) return gpio_set_pull_mode(gpio, mode); + if (gpio < GPIO_NUM_MAX) return gpio_set_pull_mode(gpio, mode); return gpio_exp_set_pull_mode(gpio, mode, NULL); } esp_err_t gpio_set_direction_u(int gpio, gpio_mode_t mode) { - if (gpio < GPIO_EXP_BASE_MIN) return gpio_set_direction(gpio, mode); + if (gpio < GPIO_NUM_MAX) return gpio_set_direction(gpio, mode); return gpio_exp_set_direction(gpio, mode, NULL); } int gpio_get_level_u(int gpio) { - if (gpio < GPIO_EXP_BASE_MIN) return gpio_get_level(gpio); + if (gpio < GPIO_NUM_MAX) return gpio_get_level(gpio); return gpio_exp_get_level(gpio, 50, NULL); } esp_err_t gpio_set_level_u(int gpio, int level) { - if (gpio < GPIO_EXP_BASE_MIN) return gpio_set_level(gpio, level); + if (gpio < GPIO_NUM_MAX) return gpio_set_level(gpio, level); return gpio_exp_set_level(gpio, level, false, NULL); } diff --git a/components/services/gpio_exp.h b/components/services/gpio_exp.h index 04404bbf..433dac66 100644 --- a/components/services/gpio_exp.h +++ b/components/services/gpio_exp.h @@ -12,8 +12,6 @@ #include "freertos/FreeRTOS.h" #include "driver/gpio.h" -#define GPIO_EXP_BASE_MIN 100 - struct gpio_exp_s; typedef struct { diff --git a/components/services/led.c b/components/services/led.c index 70abc69f..a56f2797 100644 --- a/components/services/led.c +++ b/components/services/led.c @@ -19,6 +19,7 @@ #include "driver/gpio.h" #include "driver/ledc.h" #include "platform_config.h" +#include "gpio_exp.h" #include "led.h" #include "globdefs.h" #include "accessors.h" @@ -54,7 +55,7 @@ static int led_max = 2; * */ static void set_level(struct led_s *led, bool on) { - if (led->pwm < 0) gpio_set_level(led->gpio, on ? led->onstate : !led->onstate); + if (led->pwm < 0 || led->gpio >= GPIO_NUM_MAX) gpio_set_level_u(led->gpio, on ? led->onstate : !led->onstate); else { ledc_set_duty(LEDC_HIGH_SPEED_MODE, led->channel, on ? led->pwm : (led->onstate ? 0 : pwm_system.max)); ledc_update_duty(LEDC_HIGH_SPEED_MODE, led->channel); diff --git a/components/services/services.c b/components/services/services.c index 799dcec8..ea2b9b3c 100644 --- a/components/services/services.c +++ b/components/services/services.c @@ -12,6 +12,7 @@ #include "driver/ledc.h" #include "driver/i2c.h" #include "platform_config.h" +#include "gpio_exp.h" #include "battery.h" #include "led.h" #include "monitor.h" @@ -44,12 +45,12 @@ void set_power_gpio(int gpio, char *value) { if (!strcasecmp(value, "vcc") ) { gpio_pad_select_gpio(gpio); gpio_set_direction(gpio, GPIO_MODE_OUTPUT); - gpio_set_level(gpio, 1); + gpio_set_level_u(gpio, 1); } else if (!strcasecmp(value, "gnd")) { gpio_pad_select_gpio(gpio); gpio_set_direction(gpio, GPIO_MODE_OUTPUT); - gpio_set_level(gpio, 0); - } else parsed = false ; + gpio_set_level_u(gpio, 0); + } else parsed = false; if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value); } @@ -69,7 +70,11 @@ void services_init(void) { } #endif - // set potential power GPIO + // create GPIO expander + const gpio_exp_config_t * gpio_exp_config = config_gpio_exp_get(); + if (gpio_exp_config) gpio_exp_create(gpio_exp_config); + + // set potential power GPIO (a GPIO-powered expander might be an issue) parse_set_GPIO(set_power_gpio); // shared I2C bus @@ -100,10 +105,6 @@ void services_init(void) { ESP_LOGW(TAG, "no SPI configured"); } - // create GPIO expander - const gpio_exp_config_t * gpio_exp_config = config_gpio_exp_get(); - if (gpio_exp_config) gpio_exp_create(gpio_exp_config); - // system-wide PWM timer configuration ledc_timer_config_t pwm_timer = { .duty_resolution = LEDC_TIMER_13_BIT, diff --git a/components/squeezelite/output_i2s.c b/components/squeezelite/output_i2s.c index bdc54b09..db83a60b 100644 --- a/components/squeezelite/output_i2s.c +++ b/components/squeezelite/output_i2s.c @@ -43,6 +43,7 @@ sure that using rate_delay would fix that #include "led.h" #include "monitor.h" #include "platform_config.h" +#include "gpio_exp.h" #include "accessors.h" #include "equalizer.h" #include "globdefs.h" @@ -131,10 +132,10 @@ static bool handler(u8_t *data, int len){ if (jack_mutes_amp && jack_inserted_svc()) { adac->speaker(false); - if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, !amp_control.active); + if (amp_control.gpio != -1) gpio_set_level_u(amp_control.gpio, !amp_control.active); } else { adac->speaker(true); - if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, amp_control.active); + if (amp_control.gpio != -1) gpio_set_level_u(amp_control.gpio, amp_control.active); } } @@ -157,7 +158,7 @@ static void jack_handler(bool inserted) { if (jack_mutes_amp) { LOG_INFO("switching amplifier %s", inserted ? "OFF" : "ON"); adac->speaker(!inserted); - if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, inserted ? !amp_control.active : amp_control.active); + if (amp_control.gpio != -1) gpio_set_level_u(amp_control.gpio, inserted ? !amp_control.active : amp_control.active); } // activate headset @@ -179,7 +180,7 @@ static void set_amp_gpio(int gpio, char *value) { gpio_pad_select_gpio(amp_control.gpio); gpio_set_direction(amp_control.gpio, GPIO_MODE_OUTPUT); - gpio_set_level(amp_control.gpio, !amp_control.active); + gpio_set_level_u(amp_control.gpio, !amp_control.active); LOG_INFO("setting amplifier GPIO %d (active:%d)", amp_control.gpio, amp_control.active); } @@ -455,14 +456,14 @@ static void output_thread_i2s(void *arg) { LOG_INFO("Output state is %d", output.state); if (output.state == OUTPUT_OFF) { led_blink(LED_GREEN, 100, 2500); - if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, !amp_control.active); + if (amp_control.gpio != -1) gpio_set_level_u(amp_control.gpio, !amp_control.active); LOG_INFO("switching off amp GPIO %d", amp_control.gpio); } else if (output.state == OUTPUT_STOPPED) { adac->speaker(false); led_blink(LED_GREEN, 200, 1000); } else if (output.state == OUTPUT_RUNNING) { if (!jack_mutes_amp || !jack_inserted_svc()) { - if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, amp_control.active); + if (amp_control.gpio != -1) gpio_set_level_u(amp_control.gpio, amp_control.active); adac->speaker(true); } led_on(LED_GREEN);