Skip to content

Commit

Permalink
Merge pull request #10929 from gschorcht/esp_now_wifi_cleanup
Browse files Browse the repository at this point in the history
cpu/esp*: esp_now and esp_wifi cleanup
  • Loading branch information
MrKevinWeiss authored Aug 1, 2019
2 parents 8c4cc54 + 97bb337 commit 2be1323
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 78 deletions.
2 changes: 2 additions & 0 deletions cpu/esp32/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ ifneq (,$(filter esp_gdbstub,$(USEMODULE)))
endif

ifneq (,$(filter esp_now,$(USEMODULE)))
$(eval GNRC_NETIF_NUMOF=$(shell echo $$(($(GNRC_NETIF_NUMOF)+1))))
USEMODULE += esp_wifi_any
endif

ifneq (,$(filter esp_wifi,$(USEMODULE)))
$(eval GNRC_NETIF_NUMOF=$(shell echo $$(($(GNRC_NETIF_NUMOF)+1))))
USEMODULE += esp_wifi_any
endif

Expand Down
58 changes: 43 additions & 15 deletions cpu/esp32/esp-wifi/esp_wifi_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ static wifi_config_t wifi_config_sta = {
.sta = {
.ssid = ESP_WIFI_SSID,
.password = ESP_WIFI_PASS,
.bssid_set = 0,
.channel = 0,
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
Expand All @@ -205,53 +206,75 @@ static void esp_wifi_setup (esp_wifi_netdev_t* dev)
{
DEBUG("%s: %p\n", __func__, dev);

/* initialize buffer */
dev->rx_len = 0;

/* set the event handler */
esp_system_event_add_handler(_esp_system_event_handler, NULL);

/*
* Init the WiFi driver. TODO It is not only required before ESP_WIFI is
* initialized but also before other WiFi functions are used. Once other
* WiFi functions are realized it has to be moved to a more common place.
*/
esp_err_t result;

#ifndef MODULE_ESP_NOW
/* if esp_now is used, the following part is already done */
extern portMUX_TYPE g_intr_lock_mux;
mutex_init(&g_intr_lock_mux);

esp_system_event_add_handler (_esp_system_event_handler, NULL);

esp_err_t result;
#if CONFIG_ESP32_WIFI_NVS_ENABLED
#if CONFIG_ESP32_WIFI_NVS_ENABLED
result = nvs_flash_init();
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_wifi", "nfs_flash_init failed with return value %d\n", result);
LOG_TAG_ERROR("esp_wifi", "nfs_flash_init failed "
"with return value %d\n", result);
return;
}
#endif
#endif /* CONFIG_ESP32_WIFI_NVS_ENABLED */

/* initialize the WiFi driver with default configuration */
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
result = esp_wifi_init(&cfg);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_wifi", "esp_wifi_init failed with return value %d\n", result);
LOG_TAG_ERROR("esp_wifi", "esp_wifi_init failed "
"with return value %d\n", result);
return;
}

#ifdef CONFIG_WIFI_COUNTRY
/* set configuration storage type */
result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_now", "esp_wifi_set_storage failed "
"with return value %d\n", result);
return NULL;
}

#ifdef CONFIG_WIFI_COUNTRY
/* TODO */
#endif
#endif /* CONFIG_WIFI_COUNTRY */

result = esp_wifi_set_mode(WIFI_MODE_STA);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_wifi", "esp_wifi_set_mode failed with return value %d\n", result);
LOG_TAG_ERROR("esp_wifi", "esp_wifi_set_mode failed "
"with return value %d\n", result);
return;
}
#endif /* MODULE_ESP_NOW */

/* set the Station and SoftAP configuration */
/* set the Station configuration */
result = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config_sta);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_wifi", "esp_wifi_set_config station failed with return value %d\n", result);
LOG_TAG_ERROR("esp_wifi", "esp_wifi_set_config station failed "
"with return value %d\n", result);
return;
}

/* start the WiFi driver */
result = esp_wifi_start();
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_wifi", "esp_wifi_start failed with return value %d\n", result);
LOG_TAG_ERROR("esp_wifi", "esp_wifi_start failed "
"with return value %d\n", result);
return;
}

Expand Down Expand Up @@ -460,8 +483,13 @@ void auto_init_esp_wifi (void)
esp_wifi_setup(&_esp_wifi_dev);
_esp_wifi_dev.event = SYSTEM_EVENT_MAX; /* no event */
_esp_wifi_dev.netif = gnrc_netif_ethernet_create(_esp_wifi_stack,
ESP_WIFI_STACKSIZE, ESP_WIFI_PRIO,
"netdev-esp-wifi",
ESP_WIFI_STACKSIZE,
#ifdef MODULE_ESP_NOW
ESP_WIFI_PRIO - 1,
#else
ESP_WIFI_PRIO,
#endif
"esp-wifi",
(netdev_t *)&_esp_wifi_dev);
}

Expand Down
4 changes: 2 additions & 2 deletions cpu/esp32/include/sdk_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ extern "C" {
#define CONFIG_ESP32_WIFI_TX_BUFFER_TYPE 1
#define CONFIG_ESP32_WIFI_STATIC_TX_BUFFER 0
#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER 1
#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM 48
#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM 20
#define CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM 10
#define CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM 64
#define CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM 20
#define CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED 1
#define CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED 1
#define CONFIG_ESP32_WIFI_TX_BA_WIN 6
Expand Down
8 changes: 7 additions & 1 deletion cpu/esp8266/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ ifneq (, $(filter esp_sw_timer, $(USEMODULE)))
USEMODULE += esp_sdk
endif

ifneq (, $(filter esp_now esp_wifi, $(USEMODULE)))
ifneq (, $(filter esp_wifi, $(USEMODULE)))
$(eval GNRC_NETIF_NUMOF=$(shell echo $$(($(GNRC_NETIF_NUMOF)+1))))
USEMODULE += esp_sdk
USEMODULE += netopt
endif

ifneq (, $(filter esp_now, $(USEMODULE)))
$(eval GNRC_NETIF_NUMOF=$(shell echo $$(($(GNRC_NETIF_NUMOF)+1))))
USEMODULE += esp_sdk
USEMODULE += netopt
Expand Down
9 changes: 8 additions & 1 deletion cpu/esp8266/esp-wifi/esp_wifi_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,14 @@ static int IRAM _send(netdev_t *netdev, const iolist_t *iolist)
return -EIO;
}

#ifndef MODULE_ESP_NOW
if (wifi_get_opmode() != ESP_WIFI_MODE) {
ESP_WIFI_DEBUG("WiFi is not in correct mode, cannot send");
_in_send = false;
critical_exit();
return -EIO;
}
#endif

const iolist_t *iol = iolist;
size_t iol_len = 0;
Expand Down Expand Up @@ -688,7 +690,12 @@ void auto_init_esp_wifi(void)

/* create netif */
gnrc_netif_ethernet_create(_esp_wifi_stack, ESP_WIFI_STACKSIZE,
ESP_WIFI_PRIO, "esp_wifi",
#ifdef MODULE_ESP_NOW
ESP_WIFI_PRIO - 1,
#else
ESP_WIFI_PRIO,
#endif
"esp-wifi",
(netdev_t *)&_esp_wifi_dev);
}

Expand Down
2 changes: 1 addition & 1 deletion cpu/esp_common/esp-now/esp_now_gnrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void auto_init_esp_now(void)
} else {
gnrc_netif_esp_now_create(_esp_now_stack, sizeof(_esp_now_stack),
ESP_NOW_PRIO,
"net-esp-now",
"esp-now",
&esp_now_dev->netdev);
}
}
Expand Down
90 changes: 39 additions & 51 deletions cpu/esp_common/esp-now/esp_now_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@

#endif /* MCU_ESP8266 */

#if MODULE_ESP_WIFI && !ESP_NOW_UNICAST
#error If module esp_wifi is used, module esp_now has to be used in unicast mode
#endif

/**
* There is only one ESP-NOW device. We define it as static device variable
Expand Down Expand Up @@ -383,31 +386,6 @@ static esp_err_t IRAM_ATTR _esp_system_event_handler(void *ctx, system_event_t *
return ESP_OK;
}

/**
* Default WiFi configuration, overwrite them with your configs
*/
#ifndef CONFIG_WIFI_STA_SSID
#define CONFIG_WIFI_STA_SSID "RIOT_AP"
#endif
#ifndef CONFIG_WIFI_STA_PASSWORD
#define CONFIG_WIFI_STA_PASSWORD "ThisistheRIOTporttoESP"
#endif
#ifndef CONFIG_WIFI_STA_CHANNEL
#define CONFIG_WIFI_STA_CHANNEL 0
#endif

#define CONFIG_WIFI_STA_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
#define CONFIG_WIFI_STA_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#define CONFIG_WIFI_STA_RSSI -127
#define CONFIG_WIFI_STA_AUTHMODE WIFI_AUTH_WPA_WPA2_PSK

#define CONFIG_WIFI_AP_PASSWORD ESP_NOW_SOFT_AP_PASSPHRASE
#define CONFIG_WIFI_AP_CHANNEL ESP_NOW_CHANNEL
#define CONFIG_WIFI_AP_AUTH WIFI_AUTH_WPA_WPA2_PSK
#define CONFIG_WIFI_AP_HIDDEN false
#define CONFIG_WIFI_AP_BEACON 100
#define CONFIG_WIFI_AP_MAX_CONN 4

extern esp_err_t esp_system_event_add_handler(system_event_cb_t handler,
void *arg);

Expand All @@ -429,20 +407,19 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
}

#ifdef MCU_ESP32
/*
* Init the WiFi driver. TODO It is not only required before ESP_NOW is
* initialized but also before other WiFi functions are used. Once other
* WiFi functions are realized it has to be moved to a more common place.
*/
extern portMUX_TYPE g_intr_lock_mux;
mutex_init(&g_intr_lock_mux);

/* initialize buffer */
dev->rx_len = 0;

/* set the event handler */
esp_system_event_add_handler(_esp_system_event_handler, NULL);

/* init the WiFi driver */
extern portMUX_TYPE g_intr_lock_mux;
mutex_init(&g_intr_lock_mux);

esp_err_t result;

#if CONFIG_ESP32_WIFI_NVS_ENABLED
result = nvs_flash_init();
if (result != ESP_OK) {
Expand All @@ -452,6 +429,7 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
}
#endif /* CONFIG_ESP32_WIFI_NVS_ENABLED */

/* initialize the WiFi driver with default configuration */
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
result = esp_wifi_init(&cfg);
if (result != ESP_OK) {
Expand All @@ -460,20 +438,27 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
return NULL;
}

/* set configuration storage type */
result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_now",
"esp_wifi_set_storage failed with return value %d\n",
result);
return NULL;
}

#ifdef CONFIG_WIFI_COUNTRY
/* TODO */
#endif /* CONFIG_WIFI_COUNTRY */

/* we use predefined station configuration */
/* we use predefined station configuration since it has not to be changed */
wifi_config_t wifi_config_sta = {
.sta = {
.ssid = CONFIG_WIFI_STA_SSID,
.password = CONFIG_WIFI_STA_PASSWORD,
.channel = CONFIG_WIFI_STA_CHANNEL,
.scan_method = CONFIG_WIFI_STA_SCAN_METHOD,
.sort_method = CONFIG_WIFI_STA_SORT_METHOD,
.threshold.rssi = CONFIG_WIFI_STA_RSSI,
.threshold.authmode = CONFIG_WIFI_STA_AUTHMODE
.channel = 0,
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.threshold.rssi = -127,
.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK
}
};

Expand All @@ -491,12 +476,12 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
wifi_config_ap.ap.ssid_len = strlen((char*)wifi_config_ap.ap.ssid);

wifi_config_ap.ap.channel = esp_now_params.channel;
wifi_config_ap.ap.authmode = CONFIG_WIFI_AP_AUTH;
wifi_config_ap.ap.ssid_hidden = CONFIG_WIFI_AP_HIDDEN;
wifi_config_ap.ap.max_connection = CONFIG_WIFI_AP_MAX_CONN;
wifi_config_ap.ap.beacon_interval = CONFIG_WIFI_AP_BEACON;
wifi_config_ap.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
wifi_config_ap.ap.ssid_hidden = false;
wifi_config_ap.ap.max_connection = 4;
wifi_config_ap.ap.beacon_interval = 100;

/* set the WiFi interface to Station + SoftAP mode without DHCP */
/* set the WiFi interface to Station + SoftAP */
result = esp_wifi_set_mode(WIFI_MODE_STA | WIFI_MODE_AP);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_now",
Expand All @@ -515,12 +500,13 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
result = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config_ap);
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_now",
"esp_wifi_set_mode softap failed with return value %d\n",
"esp_wifi_set_config softap failed with return value %d\n",
result);
return NULL;
}

/* start the WiFi driver */
#ifndef MODULE_ESP_WIFI
/* start WiFi if esp_wifi is not used, otherwise it is done by esp_wifi */
result = esp_wifi_start();
if (result != ESP_OK) {
LOG_TAG_ERROR("esp_now",
Expand All @@ -532,6 +518,8 @@ esp_now_netdev_t *netdev_esp_now_setup(void)
esp_wifi_set_mac(ESP_IF_WIFI_STA, (uint8_t*)_esp_now_mac);
#endif

#endif /* MODULE_ESP_WIFI */

#else /* MCU_ESP32 */

int result;
Expand All @@ -553,10 +541,10 @@ esp_now_netdev_t *netdev_esp_now_setup(void)

ap_conf.ssid_len = strlen((char*)ap_conf.ssid);
ap_conf.channel = esp_now_params.channel; /* support 1 ~ 13 */
ap_conf.authmode = AUTH_WPA2_PSK; /* don't support AUTH_WEP in softAP mode. */
ap_conf.ssid_hidden = 0; /* default 0 */
ap_conf.max_connection = 4; /* default 4, max 4 */
ap_conf.beacon_interval = 100; /* support 100 ~ 60000 ms, default 100 */
ap_conf.authmode = AUTH_WPA2_PSK; /* don't support AUTH_WEP in softAP mode. */
ap_conf.ssid_hidden = 0; /* default 0 */
ap_conf.max_connection = 4; /* default 4, max 4 */
ap_conf.beacon_interval = 100; /* support 100 ~ 60000 ms, default 100 */

wifi_softap_set_config_current(&ap_conf);

Expand Down
Loading

0 comments on commit 2be1323

Please sign in to comment.