Skip to content

Added dual antenna for WiFi (based on the ESP32-WROOM-DA module) #6226

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 5 commits into from
Feb 3, 2022
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
88 changes: 88 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,94 @@ bool WiFiGenericClass::initiateFTM(uint8_t frm_count, uint16_t burst_period, uin
return true;
}

/**
* Configure Dual antenna.
* @param gpio_ant1 Configure the GPIO number for the antenna 1 connected to the RF switch (default GPIO2 on ESP32-WROOM-DA)
* @param gpio_ant2 Configure the GPIO number for the antenna 2 connected to the RF switch (default GPIO25 on ESP32-WROOM-DA)
* @param rx_mode Set the RX antenna mode. See wifi_rx_ant_t for the options.
* @param tx_mode Set the TX antenna mode. See wifi_tx_ant_t for the options.
* @return true on success
*/
bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode) {

wifi_ant_gpio_config_t wifi_ant_io;

if (ESP_OK != esp_wifi_get_ant_gpio(&wifi_ant_io)) {
log_e("Failed to get antenna configuration");
return false;
}

wifi_ant_io.gpio_cfg[0].gpio_num = gpio_ant1;
wifi_ant_io.gpio_cfg[0].gpio_select = 1;
wifi_ant_io.gpio_cfg[1].gpio_num = gpio_ant2;
wifi_ant_io.gpio_cfg[1].gpio_select = 1;

if (ESP_OK != esp_wifi_set_ant_gpio(&wifi_ant_io)) {
log_e("Failed to set antenna GPIO configuration");
return false;
}

// Set antenna default configuration
wifi_ant_config_t ant_config = {
.rx_ant_mode = WIFI_ANT_MODE_AUTO,
.tx_ant_mode = WIFI_ANT_MODE_AUTO,
.enabled_ant0 = 0,
.enabled_ant1 = 1,
};

switch (rx_mode)
{
case WIFI_RX_ANT0:
ant_config.rx_ant_mode = WIFI_ANT_MODE_ANT0;
break;
case WIFI_RX_ANT1:
ant_config.rx_ant_mode = WIFI_ANT_MODE_ANT1;
break;
case WIFI_RX_ANT_AUTO:
log_i("TX Antenna will be automatically selected");
ant_config.rx_ant_default = WIFI_ANT_ANT0;
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
// Force TX for AUTO if RX is AUTO
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
goto set_ant;
break;
default:
log_e("Invalid default antenna! Falling back to AUTO");
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
break;
}

switch (tx_mode)
{
case WIFI_TX_ANT0:
ant_config.tx_ant_mode = WIFI_ANT_MODE_ANT0;
break;
case WIFI_TX_ANT1:
ant_config.tx_ant_mode = WIFI_ANT_MODE_ANT1;
break;
case WIFI_TX_ANT_AUTO:
log_i("RX Antenna will be automatically selected");
ant_config.rx_ant_default = WIFI_ANT_ANT0;
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
// Force RX for AUTO if RX is AUTO
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
break;
default:
log_e("Invalid default antenna! Falling back to AUTO");
ant_config.rx_ant_default = WIFI_ANT_ANT0;
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
break;
}

set_ant:
if (ESP_OK != esp_wifi_set_ant(&ant_config)) {
log_e("Failed to set antenna configuration");
return false;
}

return true;
}

// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------ Generic Network function ---------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ static const int WIFI_SCAN_DONE_BIT= BIT12;
static const int WIFI_DNS_IDLE_BIT = BIT13;
static const int WIFI_DNS_DONE_BIT = BIT14;

typedef enum {
WIFI_RX_ANT0 = 0,
WIFI_RX_ANT1,
WIFI_RX_ANT_AUTO
} wifi_rx_ant_t;

typedef enum {
WIFI_TX_ANT0 = 0,
WIFI_TX_ANT1,
WIFI_TX_ANT_AUTO
} wifi_tx_ant_t;

class WiFiGenericClass
{
public:
Expand Down Expand Up @@ -174,6 +186,8 @@ class WiFiGenericClass

bool initiateFTM(uint8_t frm_count=16, uint16_t burst_period=2, uint8_t channel=1, const uint8_t * mac=NULL);

bool setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode);

static const char * getHostname();
static bool setHostname(const char * hostname);
static bool hostname(const String& aHostname) { return setHostname(aHostname.c_str()); }
Expand Down