Skip to content

Commit 9b066ea

Browse files
authored
Added dual antenna for WiFi (based on the ESP32-WROOM-DA module) (espressif#6226)
* Added dual antenna for WiFi (based on the ESP32-WROOM-DA module) * Fixed build error * Fixed indentation and renamed function to setDualAntennaConfig * Added the RX and TX selection modes as configuration * Mode code optimization
1 parent bb4d902 commit 9b066ea

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

libraries/WiFi/src/WiFiGeneric.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,94 @@ bool WiFiGenericClass::initiateFTM(uint8_t frm_count, uint16_t burst_period, uin
11921192
return true;
11931193
}
11941194

1195+
/**
1196+
* Configure Dual antenna.
1197+
* @param gpio_ant1 Configure the GPIO number for the antenna 1 connected to the RF switch (default GPIO2 on ESP32-WROOM-DA)
1198+
* @param gpio_ant2 Configure the GPIO number for the antenna 2 connected to the RF switch (default GPIO25 on ESP32-WROOM-DA)
1199+
* @param rx_mode Set the RX antenna mode. See wifi_rx_ant_t for the options.
1200+
* @param tx_mode Set the TX antenna mode. See wifi_tx_ant_t for the options.
1201+
* @return true on success
1202+
*/
1203+
bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode) {
1204+
1205+
wifi_ant_gpio_config_t wifi_ant_io;
1206+
1207+
if (ESP_OK != esp_wifi_get_ant_gpio(&wifi_ant_io)) {
1208+
log_e("Failed to get antenna configuration");
1209+
return false;
1210+
}
1211+
1212+
wifi_ant_io.gpio_cfg[0].gpio_num = gpio_ant1;
1213+
wifi_ant_io.gpio_cfg[0].gpio_select = 1;
1214+
wifi_ant_io.gpio_cfg[1].gpio_num = gpio_ant2;
1215+
wifi_ant_io.gpio_cfg[1].gpio_select = 1;
1216+
1217+
if (ESP_OK != esp_wifi_set_ant_gpio(&wifi_ant_io)) {
1218+
log_e("Failed to set antenna GPIO configuration");
1219+
return false;
1220+
}
1221+
1222+
// Set antenna default configuration
1223+
wifi_ant_config_t ant_config = {
1224+
.rx_ant_mode = WIFI_ANT_MODE_AUTO,
1225+
.tx_ant_mode = WIFI_ANT_MODE_AUTO,
1226+
.enabled_ant0 = 0,
1227+
.enabled_ant1 = 1,
1228+
};
1229+
1230+
switch (rx_mode)
1231+
{
1232+
case WIFI_RX_ANT0:
1233+
ant_config.rx_ant_mode = WIFI_ANT_MODE_ANT0;
1234+
break;
1235+
case WIFI_RX_ANT1:
1236+
ant_config.rx_ant_mode = WIFI_ANT_MODE_ANT1;
1237+
break;
1238+
case WIFI_RX_ANT_AUTO:
1239+
log_i("TX Antenna will be automatically selected");
1240+
ant_config.rx_ant_default = WIFI_ANT_ANT0;
1241+
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
1242+
// Force TX for AUTO if RX is AUTO
1243+
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
1244+
goto set_ant;
1245+
break;
1246+
default:
1247+
log_e("Invalid default antenna! Falling back to AUTO");
1248+
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
1249+
break;
1250+
}
1251+
1252+
switch (tx_mode)
1253+
{
1254+
case WIFI_TX_ANT0:
1255+
ant_config.tx_ant_mode = WIFI_ANT_MODE_ANT0;
1256+
break;
1257+
case WIFI_TX_ANT1:
1258+
ant_config.tx_ant_mode = WIFI_ANT_MODE_ANT1;
1259+
break;
1260+
case WIFI_TX_ANT_AUTO:
1261+
log_i("RX Antenna will be automatically selected");
1262+
ant_config.rx_ant_default = WIFI_ANT_ANT0;
1263+
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
1264+
// Force RX for AUTO if RX is AUTO
1265+
ant_config.rx_ant_mode = WIFI_ANT_MODE_AUTO;
1266+
break;
1267+
default:
1268+
log_e("Invalid default antenna! Falling back to AUTO");
1269+
ant_config.rx_ant_default = WIFI_ANT_ANT0;
1270+
ant_config.tx_ant_mode = WIFI_ANT_MODE_AUTO;
1271+
break;
1272+
}
1273+
1274+
set_ant:
1275+
if (ESP_OK != esp_wifi_set_ant(&ant_config)) {
1276+
log_e("Failed to set antenna configuration");
1277+
return false;
1278+
}
1279+
1280+
return true;
1281+
}
1282+
11951283
// -----------------------------------------------------------------------------------------------------------------------
11961284
// ------------------------------------------------ Generic Network function ---------------------------------------------
11971285
// -----------------------------------------------------------------------------------------------------------------------

libraries/WiFi/src/WiFiGeneric.h

+14
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ static const int WIFI_SCAN_DONE_BIT= BIT12;
139139
static const int WIFI_DNS_IDLE_BIT = BIT13;
140140
static const int WIFI_DNS_DONE_BIT = BIT14;
141141

142+
typedef enum {
143+
WIFI_RX_ANT0 = 0,
144+
WIFI_RX_ANT1,
145+
WIFI_RX_ANT_AUTO
146+
} wifi_rx_ant_t;
147+
148+
typedef enum {
149+
WIFI_TX_ANT0 = 0,
150+
WIFI_TX_ANT1,
151+
WIFI_TX_ANT_AUTO
152+
} wifi_tx_ant_t;
153+
142154
class WiFiGenericClass
143155
{
144156
public:
@@ -174,6 +186,8 @@ class WiFiGenericClass
174186

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

189+
bool setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode);
190+
177191
static const char * getHostname();
178192
static bool setHostname(const char * hostname);
179193
static bool hostname(const String& aHostname) { return setHostname(aHostname.c_str()); }

0 commit comments

Comments
 (0)