Skip to content

Commit

Permalink
Esp32 fixes (#2528)
Browse files Browse the repository at this point in the history
* Fix GPIO18/19 for ESP32-C3

Use HAL to control IO MUX which includes additional steps to disable internal USB.

* Fix ESP32 hardware timer

* Configure esp32 WiFi station for full scan and pick one with best signal

If multiple APs exist for same SSID then picking first one found (fast scan) can cause reliability problems if signal strength is low
  • Loading branch information
mikee47 authored Jul 20, 2022
1 parent c126bb7 commit afa167e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Sming/Arch/Esp32/Components/driver/hw_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
****/

#include <driver/hw_timer.h>
#include <driver/periph_ctrl.h>
#include <hal/timer_hal.h>
#include <esp_intr_alloc.h>

Expand Down Expand Up @@ -36,11 +37,11 @@ void IRAM_ATTR timerIsr(void* arg)

timer_hal_clear_intr_status(&timer.hal);

if(timer.autoload) {
timer_hal_set_alarm_enable(&timer.hal, true);
} else {
if(!timer.autoload) {
timer_hal_set_counter_enable(&timer.hal, false);
}

timer_hal_set_alarm_enable(&timer.hal, true);
}

} // namespace
Expand Down Expand Up @@ -102,6 +103,7 @@ void hw_timer_init(void)
{
timer.group = HW_TIMER1_GROUP;
timer.index = HW_TIMER1_INDEX;
periph_module_enable(timer_group_periph_signals.groups[timer.group].module);
timer_hal_init(&timer.hal, timer.group, timer.index);
timer_hal_set_counter_enable(&timer.hal, false);
timer_hal_set_alarm_enable(&timer.hal, true);
Expand Down
5 changes: 3 additions & 2 deletions Sming/Arch/Esp32/Components/driver/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <BitManipulations.h>
#include <Data/Range.h>
#include <esp_systemapi.h>
#include <hal/gpio_ll.h>

namespace
{
Expand Down Expand Up @@ -757,14 +758,14 @@ bool smg_uart_set_pins(smg_uart_t* uart, int tx_pin, int rx_pin)
auto& conn = uart_periph_signal[uart->uart_nr];

if(tx_pin != UART_PIN_NO_CHANGE) {
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[tx_pin], PIN_FUNC_GPIO);
gpio_ll_iomux_func_sel(GPIO_PIN_MUX_REG[tx_pin], PIN_FUNC_GPIO);
gpio_set_level(gpio_num_t(tx_pin), true);
gpio_matrix_out(tx_pin, conn.tx_sig, false, false);
uart->tx_pin = tx_pin;
}

if(rx_pin != UART_PIN_NO_CHANGE) {
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[rx_pin], PIN_FUNC_GPIO);
gpio_ll_iomux_func_sel(GPIO_PIN_MUX_REG[rx_pin], PIN_FUNC_GPIO);
gpio_set_pull_mode(gpio_num_t(rx_pin), GPIO_PULLUP_ONLY);
gpio_set_direction(gpio_num_t(rx_pin), GPIO_MODE_INPUT);
gpio_matrix_in(rx_pin, conn.rx_sig, false);
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Esp32/Core/Digital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void pinMode(uint16_t pin, uint8_t mode)
gpio_ll_pullup_dis(&GPIO, gpio);
}

PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[pin], PIN_FUNC_GPIO);
gpio_ll_iomux_func_sel(GPIO_PIN_MUX_REG[pin], PIN_FUNC_GPIO);
}

//Detect if pin is input
Expand Down
4 changes: 4 additions & 0 deletions Sming/Components/Network/Arch/Esp32/Platform/StationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ bool StationImpl::config(const Config& cfg)
config.sta.bssid_set = false;
}

// Find *all* APs for the requested SSID and pick the best one
config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;

enable(true, cfg.save);

if(cfg.save) {
Expand Down

0 comments on commit afa167e

Please sign in to comment.