idf4: part 2.2 - update wifi & eth modules to new APIs

Only compile-tested so far.

Of note is that the WiFi auto-connect (flag) functionality has been removed
from the IDF, and as a follow-on so has the "auto" field in the wifi config.

On the Ethernet side, support for the TLK110 PHY seems to have been removed,
but on the other hand there is now new support for several others.
This commit is contained in:
Johny Mattsson 2021-07-20 18:18:21 +10:00
parent 62b3d06020
commit d2f8121e22
6 changed files with 178 additions and 228 deletions

View File

@ -1,6 +1,3 @@
#if 0
// FIXME - IDFv3 API no longer available; this needs a major rewrite :(
#include <string.h> #include <string.h>
#include "module.h" #include "module.h"
@ -10,64 +7,37 @@
#include "nodemcu_esp_event.h" #include "nodemcu_esp_event.h"
#include "ip_fmt.h" #include "ip_fmt.h"
#include "common.h" #include "common.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_eth_phy.h"
#include "driver/gpio.h" #include "driver/gpio.h"
// phy includes
#include "eth_phy/phy_lan8720.h"
#include "eth_phy/phy_tlk110.h"
#include "eth_phy/phy_ip101.h"
typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config);
typedef enum { typedef enum {
ETH_PHY_LAN8720 = 0, ETH_PHY_DP83848,
ETH_PHY_TLK110,
ETH_PHY_IP101, ETH_PHY_IP101,
ETH_PHY_KSZ8041,
ETH_PHY_KSZ8081,
ETH_PHY_LAN8720,
ETH_PHY_RTL8201,
ETH_PHY_MAX ETH_PHY_MAX
} eth_phy_t; } eth_phy_t;
static struct { static const new_eth_phy_fn new_eth_phy[] = {
const eth_config_t *eth_config; [ETH_PHY_DP83848] = esp_eth_phy_new_dp83848,
int pin_power; [ETH_PHY_IP101] = esp_eth_phy_new_ip101,
gpio_num_t pin_mdc, pin_mdio; [ETH_PHY_KSZ8041] = esp_eth_phy_new_ksz8041,
} module_config; [ETH_PHY_KSZ8081] = esp_eth_phy_new_ksz8081,
[ETH_PHY_LAN8720] = esp_eth_phy_new_lan8720,
[ETH_PHY_RTL8201] = esp_eth_phy_new_rtl8201,
};
static void phy_device_power_enable_via_gpio( bool enable ) _Static_assert(sizeof(new_eth_phy) == (sizeof(new_eth_phy[0]) * ETH_PHY_MAX),
{ "missing phy create func");
if (!enable)
module_config.eth_config->phy_power_enable( false );
gpio_pad_select_gpio( module_config.pin_power );
gpio_set_level( module_config.pin_power, enable ? 1 : 0 );
gpio_set_direction( module_config.pin_power, GPIO_MODE_OUTPUT );
// Allow the power up/down to take effect, min 300us
//vTaskDelay(1);
if (enable)
module_config.eth_config->phy_power_enable(true);
}
/**
* @brief gpio specific init
*
* @note RMII data pins are fixed in esp32:
* TXD0 <=> GPIO19
* TXD1 <=> GPIO22
* TX_EN <=> GPIO21
* RXD0 <=> GPIO25
* RXD1 <=> GPIO26
* CLK <=> GPIO0
*
*/
static void eth_gpio_config_rmii( void )
{
phy_rmii_configure_data_interface_pins();
phy_rmii_smi_configure_pins( module_config.pin_mdc, module_config.pin_mdio );
}
static esp_netif_t *eth = NULL;
esp_eth_handle_t eth_handle = NULL;
// --- Event handling ----------------------------------------------------- // --- Event handling -----------------------------------------------------
@ -76,7 +46,7 @@ typedef void (*fill_cb_arg_fn) (lua_State *L, const void *data);
typedef struct typedef struct
{ {
const char *name; const char *name;
esp_event_base_t event_base_ptr; esp_event_base_t *event_base_ptr;
int32_t event_id; int32_t event_id;
fill_cb_arg_fn fill_cb_arg; fill_cb_arg_fn fill_cb_arg;
} event_desc_t; } event_desc_t;
@ -88,11 +58,11 @@ static void empty_arg (lua_State *L, const void *data) {}
static const event_desc_t events[] = static const event_desc_t events[] =
{ {
{ "start", &ETHERNET_EVENT, ETHERNET_EVENT_START, empty_arg }, { "start", &ETH_EVENT, ETHERNET_EVENT_START, empty_arg },
{ "stop", &ETHERNET_EVENT, ETHERNET_EVENT_STOP, empty_arg }, { "stop", &ETH_EVENT, ETHERNET_EVENT_STOP, empty_arg },
{ "connected", &ETHERNET_EVENT, ETHERNET_EVENT_CONNECTED, empty_arg }, { "connected", &ETH_EVENT, ETHERNET_EVENT_CONNECTED, empty_arg },
{ "disconnected", &ETHERNET_EVENT, ETHERNET_EVENT_DISCONNECTED, empty_arg }, { "disconnected", &ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, empty_arg },
{ "got_ip", &IP_EVENT, IP_EVENT_GOT_IP, eth_got_ip }, { "got_ip", &IP_EVENT, IP_EVENT_ETH_GOT_IP, eth_got_ip },
}; };
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
@ -117,26 +87,22 @@ static int eth_event_idx_by_name( const char *name )
static void eth_got_ip( lua_State *L, const void *data ) static void eth_got_ip( lua_State *L, const void *data )
{ {
(void)data; const ip_event_got_ip_t *got_ip_info =
tcpip_adapter_ip_info_t ip_info; (const ip_event_got_ip_t *)data;
const esp_netif_ip_info_t *ip_info = &got_ip_info->ip_info;
memset(&ip_info, 0, sizeof(tcpip_adapter_ip_info_t));
if (tcpip_adapter_get_ip_info( ESP_IF_ETH, &ip_info ) != ESP_OK) {
luaL_error( L, "error from tcpip_adapter_get_ip_info!" );
}
// on_event() has prepared a table on top of stack. fill it with cb-specific fields: // on_event() has prepared a table on top of stack. fill it with cb-specific fields:
// ip, netmask, gw // ip, netmask, gw
char ipstr[IP_STR_SZ] = { 0 }; char ipstr[IP_STR_SZ] = { 0 };
ip4str( ipstr, &ip_info.ip ); ip4str_esp( ipstr, &ip_info->ip );
lua_pushstring( L, ipstr ); lua_pushstring( L, ipstr );
lua_setfield( L, -2, "ip" ); lua_setfield( L, -2, "ip" );
ip4str( ipstr, &ip_info.netmask ); ip4str_esp( ipstr, &ip_info->netmask );
lua_pushstring( L, ipstr ); lua_pushstring( L, ipstr );
lua_setfield( L, -2, "netmask" ); lua_setfield( L, -2, "netmask" );
ip4str(ipstr, &ip_info.gw ); ip4str_esp(ipstr, &ip_info->gw );
lua_pushstring( L, ipstr ); lua_pushstring( L, ipstr );
lua_setfield( L, -2, "gw" ); lua_setfield( L, -2, "gw" );
} }
@ -158,11 +124,11 @@ static void on_event(esp_event_base_t base, int32_t id, const void *data)
lua_settop( L, top ); lua_settop( L, top );
} }
NODEMCU_ESP_EVENT(ETHERNET_EVENT, ETHERNET_EVENT_START, on_event); NODEMCU_ESP_EVENT(ETH_EVENT, ETHERNET_EVENT_START, on_event);
NODEMCU_ESP_EVENT(ETHERNET_EVENT, ETHERNET_EVENT_STOP, on_event); NODEMCU_ESP_EVENT(ETH_EVENT, ETHERNET_EVENT_STOP, on_event);
NODEMCU_ESP_EVENT(ETHERNET_EVENT, ETHERNET_EVENT_CONNECTED, on_event); NODEMCU_ESP_EVENT(ETH_EVENT, ETHERNET_EVENT_CONNECTED, on_event);
NODEMCU_ESP_EVENT(ETHERNET_EVENT, ETHERNET_EVENT_DISCONNECTED, on_event); NODEMCU_ESP_EVENT(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, on_event);
NODEMCU_ESP_EVENT(IP_EVENT, IP_EVENT_ETH_GOT_IP, on_event); NODEMCU_ESP_EVENT(IP_EVENT, IP_EVENT_ETH_GOT_IP, on_event);
// Lua API // Lua API
@ -177,7 +143,7 @@ static int leth_set_mac( lua_State *L )
return luaL_error( L, "invalid mac string" ); return luaL_error( L, "invalid mac string" );
} }
if (ESP_OK != esp_eth_set_mac( mac )) { if (ESP_OK != esp_netif_set_mac( eth, mac )) {
return luaL_error( L, "error setting mac" ); return luaL_error( L, "error setting mac" );
} }
@ -187,9 +153,9 @@ static int leth_set_mac( lua_State *L )
static int leth_get_mac( lua_State *L ) static int leth_get_mac( lua_State *L )
{ {
char temp[64]; char temp[64];
uint8_t mac[6]; uint8_t mac[6] = { 0, };
esp_eth_get_mac( mac ); esp_netif_get_mac( eth, mac );
snprintf( temp, 63, MACSTR, MAC2STR(mac) ); snprintf( temp, 63, MACSTR, MAC2STR(mac) );
lua_pushstring( L, temp ); lua_pushstring( L, temp );
return 1; return 1;
@ -197,17 +163,19 @@ static int leth_get_mac( lua_State *L )
static int leth_get_speed( lua_State *L ) static int leth_get_speed( lua_State *L )
{ {
eth_speed_mode_t speed = esp_eth_get_speed(); eth_speed_t speed;
if (esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed) != ESP_OK)
return luaL_error(L, "failed to get eth speed");
switch (speed) { switch (speed) {
case ETH_SPEED_MODE_10M: case ETH_SPEED_10M:
lua_pushnumber( L, 10 ); lua_pushnumber( L, 10 );
break; break;
case ETH_SPEED_MODE_100M: case ETH_SPEED_100M:
lua_pushnumber( L, 100 ); lua_pushnumber( L, 100 );
break; break;
default: default:
return luaL_error( L, "invalid speed" ); return luaL_error( L, "invalid speed" );
break;
} }
return 1; return 1;
} }
@ -233,6 +201,9 @@ static int leth_on( lua_State *L )
static int leth_init( lua_State *L ) static int leth_init( lua_State *L )
{ {
if (eth != NULL)
return luaL_error(L, "ethernet interface already set up");
int stack = 0; int stack = 0;
int top = lua_gettop( L ); int top = lua_gettop( L );
@ -240,55 +211,69 @@ static int leth_init( lua_State *L )
// temporarily copy option table to top of stack for opt_ functions // temporarily copy option table to top of stack for opt_ functions
lua_pushvalue( L, stack ); lua_pushvalue( L, stack );
eth_phy_t phy = opt_checkint_range( L, "phy", -1, 0, ETH_PHY_MAX ); eth_mac_config_t mac_cfg = ETH_MAC_DEFAULT_CONFIG();
eth_phy_base_t phy_addr = opt_checkint_range( L, "addr", -1, 0, PHY31 );
eth_clock_mode_t clock_mode = opt_checkint_range( L, "clock_mode", -1, 0, ETH_CLOCK_GPIO17_OUT ); mac_cfg.smi_mdc_gpio_num =
module_config.pin_power = opt_checkint_range( L, "power", -1, -1, GPIO_NUM_MAX-1 ); // optional opt_checkint_range( L, "mdc", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
module_config.pin_mdc = opt_checkint_range( L, "mdc", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 ); mac_cfg.smi_mdio_gpio_num =
module_config.pin_mdio = opt_checkint_range( L, "mdio", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 ); opt_checkint_range( L, "mdio", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
eth_phy_config_t phy_cfg = ETH_PHY_DEFAULT_CONFIG();
phy_cfg.phy_addr = opt_checkint_range( L, "addr", -1, 0, 31 );
phy_cfg.reset_gpio_num =
opt_checkint_range( L, "power", -1, -1, GPIO_NUM_MAX-1 ); // optional
eth_phy_t phy_type = opt_checkint_range( L, "phy", -1, 0, ETH_PHY_MAX );
lua_settop( L, top ); lua_settop( L, top );
eth_config_t config; esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_cfg);
esp_eth_phy_t *phy = new_eth_phy[phy_type](&phy_cfg);
switch (phy) { esp_eth_config_t eth_cfg = ETH_DEFAULT_CONFIG(mac, phy);
case ETH_PHY_LAN8720:
config = phy_lan8720_default_ethernet_config;
module_config.eth_config = &phy_lan8720_default_ethernet_config;
break;
case ETH_PHY_TLK110:
config = phy_tlk110_default_ethernet_config;
module_config.eth_config = &phy_tlk110_default_ethernet_config;
break;
case ETH_PHY_IP101:
config = phy_ip101_default_ethernet_config;
module_config.eth_config = &phy_ip101_default_ethernet_config;
break;
default:
// prevented by opt_checkint_range
break;
};
config.phy_addr = phy_addr; esp_err_t err = esp_eth_driver_install(&eth_cfg, &eth_handle);
config.gpio_config = eth_gpio_config_rmii; if (err != ESP_OK)
config.tcpip_input = tcpip_adapter_eth_input; goto cleanup_mac_phy;
config.clock_mode = clock_mode;
if (module_config.pin_power >= GPIO_NUM_0) { esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
// power pin is optional esp_netif_t *new_eth = esp_netif_new(&netif_cfg);
config.phy_power_enable = phy_device_power_enable_via_gpio;
}
if (esp_eth_init( &config ) != ESP_OK) { err = esp_eth_set_default_handlers(eth);
luaL_error( L, "esp_eth_init failed" ); if (err != ESP_OK)
} goto cleanup_netif;
if (esp_eth_enable() != ESP_OK) {
luaL_error( L, "esp_eth_enable failed" );
}
void *glue = esp_eth_new_netif_glue(eth_handle);
err = esp_netif_attach(new_eth, glue);
if (err != ESP_OK)
goto cleanup_glue;
err = esp_eth_start(eth_handle);
if (err != ESP_OK)
goto cleanup_glue;
eth = new_eth;
return 0; return 0;
cleanup_glue:
esp_eth_del_netif_glue(glue);
cleanup_netif:
esp_netif_destroy(new_eth);
cleanup_mac_phy:
if (mac)
mac->del(mac);
if (phy)
phy->del(phy);
eth_handle = NULL;
return luaL_error(L, "failed to init ethernet, code %d", err);
} }
LROT_BEGIN(eth) LROT_BEGIN(eth)
LROT_FUNCENTRY( init, leth_init ) LROT_FUNCENTRY( init, leth_init )
LROT_FUNCENTRY( on, leth_on ) LROT_FUNCENTRY( on, leth_on )
@ -296,15 +281,12 @@ LROT_BEGIN(eth)
LROT_FUNCENTRY( get_mac, leth_get_mac ) LROT_FUNCENTRY( get_mac, leth_get_mac )
LROT_FUNCENTRY( set_mac, leth_set_mac ) LROT_FUNCENTRY( set_mac, leth_set_mac )
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 ) LROT_NUMENTRY( PHY_DP83848, ETH_PHY_DP83848 )
LROT_NUMENTRY( PHY_TLK110, ETH_PHY_TLK110 )
LROT_NUMENTRY( PHY_IP101, ETH_PHY_IP101 ) LROT_NUMENTRY( PHY_IP101, ETH_PHY_IP101 )
LROT_NUMENTRY( PHY_KSZ8041, ETH_PHY_KSZ8041 )
LROT_NUMENTRY( CLOCK_GPIO0_IN, ETH_CLOCK_GPIO0_IN ) LROT_NUMENTRY( PHY_KSZ8081, ETH_PHY_KSZ8081 )
LROT_NUMENTRY( CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO0_OUT ) LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 )
LROT_NUMENTRY( CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO16_OUT ) LROT_NUMENTRY( PHY_RTL8201, ETH_PHY_RTL8201 )
LROT_NUMENTRY( CLOCK_GPIO17_OUT, ETH_CLOCK_GPIO17_OUT )
LROT_END(eth, NULL, 0) LROT_END(eth, NULL, 0)
NODEMCU_MODULE(ETH, "eth", eth, NULL); NODEMCU_MODULE(ETH, "eth", eth, NULL);
#endif

View File

@ -99,7 +99,7 @@ extern void wifi_sta_init (void);
static int wifi_init (lua_State *L) static int wifi_init (lua_State *L)
{ {
wifi_ap_init (); wifi_ap_init ();
// FIXME wifi_sta_init (); wifi_sta_init ();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_wifi_init (&cfg); esp_err_t err = esp_wifi_init (&cfg);
@ -108,7 +108,7 @@ static int wifi_init (lua_State *L)
} }
//LROT_EXTERN(wifi_sta); FIXME LROT_EXTERN(wifi_sta);
LROT_EXTERN(wifi_ap); LROT_EXTERN(wifi_ap);
LROT_BEGIN(wifi) LROT_BEGIN(wifi)
@ -118,7 +118,7 @@ LROT_BEGIN(wifi)
LROT_FUNCENTRY( start, wifi_start ) LROT_FUNCENTRY( start, wifi_start )
LROT_FUNCENTRY( stop, wifi_stop ) LROT_FUNCENTRY( stop, wifi_stop )
// LROT_TABENTRY ( sta, wifi_sta ) FIXME LROT_TABENTRY ( sta, wifi_sta )
LROT_TABENTRY ( ap, wifi_ap ) LROT_TABENTRY ( ap, wifi_ap )

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 Dius Computing Pty Ltd. All rights reserved. * Copyright 2016-2021 Dius Computing Pty Ltd. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -38,13 +38,15 @@
#include "nodemcu_esp_event.h" #include "nodemcu_esp_event.h"
#include <string.h> #include <string.h>
#include "dhcpserver/dhcpserver_options.h" #include "dhcpserver/dhcpserver_options.h"
#include "esp_netif.h"
// Note: these are documented in wifi.md, update there too if changed here! // Note: these are documented in wifi.md, update there too if changed here!
#define DEFAULT_AP_CHANNEL 11 #define DEFAULT_AP_CHANNEL 11
#define DEFAULT_AP_MAXCONNS 4 #define DEFAULT_AP_MAXCONNS 4
#define DEFAULT_AP_BEACON 100 #define DEFAULT_AP_BEACON 100
static esp_netif_t *wifi_ap = NULL;
// --- Event handling ---------------------------------------------------- // --- Event handling ----------------------------------------------------
static void ap_staconn (lua_State *L, const void *data); static void ap_staconn (lua_State *L, const void *data);
static void ap_stadisconn (lua_State *L, const void *data); static void ap_stadisconn (lua_State *L, const void *data);
@ -124,6 +126,8 @@ NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_AP_PROBEREQRECVED, on_event);
void wifi_ap_init (void) void wifi_ap_init (void)
{ {
wifi_ap = esp_netif_create_default_wifi_ap();
for (unsigned i = 0; i < ARRAY_LEN(event_cb); ++i) for (unsigned i = 0; i < ARRAY_LEN(event_cb); ++i)
event_cb[i] = LUA_NOREF; event_cb[i] = LUA_NOREF;
} }
@ -132,57 +136,50 @@ void wifi_ap_init (void)
static int wifi_ap_setip(lua_State *L) static int wifi_ap_setip(lua_State *L)
{ {
tcpip_adapter_ip_info_t ipInfo;
ip_addr_t dns;
uint8_t opt;
size_t len;
const char *str;
ip_addr_t ipAddr;
ipAddr.type = IPADDR_TYPE_V4;
luaL_checkanytable (L, 1); luaL_checkanytable (L, 1);
//memset(&ipInfo, 0, sizeof(tcpip_adapter_ip_info_t)); size_t len = 0;
const char *str = NULL;
esp_netif_ip_info_t ip_info = { 0, };
lua_getfield (L, 1, "ip"); lua_getfield (L, 1, "ip");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.ip) != ESP_OK)
{ {
return luaL_error(L, "Could not parse IP address, aborting"); return luaL_error(L, "Could not parse IP address, aborting");
} }
ipInfo.ip = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "gateway"); lua_getfield (L, 1, "gateway");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.gw) != ESP_OK)
{ {
return luaL_error(L, "Could not parse Gateway address, aborting"); return luaL_error(L, "Could not parse Gateway address, aborting");
} }
ipInfo.gw = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "netmask"); lua_getfield (L, 1, "netmask");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.netmask) != ESP_OK)
{ {
return luaL_error(L, "Could not parse Netmask, aborting"); return luaL_error(L, "Could not parse Netmask, aborting");
} }
ipInfo.netmask = ipAddr.u_addr.ip4;
ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); ESP_ERROR_CHECK(esp_netif_dhcps_stop(wifi_ap));
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo); esp_netif_set_ip_info(wifi_ap, &ip_info);
esp_netif_dns_info_t dns = { .ip = { .type = ESP_IPADDR_TYPE_V4 } };
lua_getfield (L, 1, "dns"); lua_getfield (L, 1, "dns");
str = luaL_optlstring(L, -1, "", &len); str = luaL_optlstring(L, -1, "", &len);
if(ipaddr_aton(str, &dns)) if (*str)
{ {
opt = 1; if (esp_netif_str_to_ip4(str, &dns.ip.u_addr.ip4) != ESP_OK)
dhcps_dns_setserver(&dns); {
tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET, DOMAIN_NAME_SERVER, &opt, sizeof(opt)); return luaL_error(L, "Could not parse Dns, aborting");
}
esp_netif_set_dns_info(wifi_ap, ESP_NETIF_DNS_MAIN, &dns);
} }
ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); ESP_ERROR_CHECK(esp_netif_dhcps_start(wifi_ap));
return 0; return 0;
} }

View File

@ -1,8 +1,5 @@
#if 0
// FIXME - tcpip_adapter deprecated/removed - need to rewrite for esp_netif
/* /*
* Copyright 2016 Dius Computing Pty Ltd. All rights reserved. * Copyright 2016-2021 Dius Computing Pty Ltd. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -42,7 +39,9 @@
#include "ip_fmt.h" #include "ip_fmt.h"
#include "nodemcu_esp_event.h" #include "nodemcu_esp_event.h"
#include <string.h> #include <string.h>
#include "esp_netif.h"
static esp_netif_t *wifi_sta = NULL;
static int scan_cb_ref = LUA_NOREF; static int scan_cb_ref = LUA_NOREF;
// --- Event handling ----------------------------------------------------- // --- Event handling -----------------------------------------------------
@ -154,6 +153,8 @@ NODEMCU_ESP_EVENT(IP_EVENT, IP_EVENT_STA_GOT_IP, on_event);
void wifi_sta_init (void) void wifi_sta_init (void)
{ {
wifi_sta = esp_netif_create_default_wifi_sta();
for (unsigned i = 0; i < ARRAY_LEN(event_cb); ++i) for (unsigned i = 0; i < ARRAY_LEN(event_cb); ++i)
event_cb[i] = LUA_NOREF; event_cb[i] = LUA_NOREF;
} }
@ -172,52 +173,45 @@ static void do_connect (esp_event_base_t base, int32_t id, const void *data)
// --- Lua API functions ---------------------------------------------------- // --- Lua API functions ----------------------------------------------------
static int wifi_sta_setip(lua_State *L) static int wifi_sta_setip(lua_State *L)
{ {
tcpip_adapter_ip_info_t ipInfo;
tcpip_adapter_dns_info_t dnsinfo;
size_t len;
const char *str;
ip_addr_t ipAddr;
ipAddr.type = IPADDR_TYPE_V4;
luaL_checkanytable (L, 1); luaL_checkanytable (L, 1);
//memset(&ipInfo, 0, sizeof(tcpip_adapter_ip_info_t)); size_t len = 0;
const char *str = NULL;
esp_netif_ip_info_t ip_info = { 0, };
lua_getfield (L, 1, "ip"); lua_getfield (L, 1, "ip");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.ip) != ESP_OK)
{ {
return luaL_error(L, "Could not parse IP address, aborting"); return luaL_error(L, "Could not parse IP address, aborting");
} }
ipInfo.ip = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "netmask"); lua_getfield (L, 1, "netmask");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.netmask) != ESP_OK)
{ {
return luaL_error(L, "Could not parse Netmask, aborting"); return luaL_error(L, "Could not parse Netmask, aborting");
} }
ipInfo.netmask = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "gateway"); lua_getfield (L, 1, "gateway");
str = luaL_checklstring (L, -1, &len); str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr)) if (esp_netif_str_to_ip4(str, &ip_info.gw) != ESP_OK)
{ {
return luaL_error(L, "Could not parse Gateway address, aborting"); return luaL_error(L, "Could not parse Gateway address, aborting");
} }
ipInfo.gw = ipAddr.u_addr.ip4;
esp_netif_dns_info_t dns_info = { .ip = { .type = ESP_IPADDR_TYPE_V4 } };
lua_getfield (L, 1, "dns"); lua_getfield (L, 1, "dns");
str = luaL_optlstring(L, -1, str, &len); str = luaL_optlstring(L, -1, str, &len); // default to gateway
if(!ipaddr_aton(str, &dnsinfo.ip)) if (esp_netif_str_to_ip4(str, &dns_info.ip.u_addr.ip4) != ESP_OK)
{ {
return luaL_error(L, "Could not parse DNS address, aborting"); return luaL_error(L, "Could not parse DNS address, aborting");
} }
ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)); ESP_ERROR_CHECK(esp_netif_dhcpc_stop(wifi_sta));
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_STA, TCPIP_ADAPTER_DNS_MAIN, &dnsinfo); esp_netif_set_ip_info(wifi_sta, &ip_info);
esp_netif_set_dns_info(wifi_sta, ESP_NETIF_DNS_MAIN, &dns_info);
return 0; return 0;
} }
@ -225,10 +219,9 @@ static int wifi_sta_setip(lua_State *L)
static int wifi_sta_sethostname(lua_State *L) static int wifi_sta_sethostname(lua_State *L)
{ {
size_t l; size_t l;
esp_err_t err;
const char *hostname = luaL_checklstring(L, 1, &l); const char *hostname = luaL_checklstring(L, 1, &l);
err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname); esp_err_t err = esp_netif_set_hostname(wifi_sta, hostname);
if (err != ESP_OK) if (err != ESP_OK)
return luaL_error (L, "failed to set hostname, code %d", err); return luaL_error (L, "failed to set hostname, code %d", err);
@ -286,22 +279,12 @@ static int wifi_sta_config (lua_State *L)
return luaL_error (L, "invalid BSSID: %s", bssid); return luaL_error (L, "invalid BSSID: %s", bssid);
} }
lua_getfield (L, 1, "auto");
bool auto_conn = luaL_optbool (L, -1, true);
SET_SAVE_MODE(save); SET_SAVE_MODE(save);
esp_err_t err = esp_wifi_set_auto_connect (auto_conn); esp_err_t err = esp_wifi_set_config (WIFI_IF_STA, &cfg);
if (err != ESP_OK)
return luaL_error (L, "failed to set wifi auto-connect, code %d", err);
err = esp_wifi_set_config (WIFI_IF_STA, &cfg);
if (err != ESP_OK) if (err != ESP_OK)
return luaL_error (L, "failed to set wifi config, code %d", err); return luaL_error (L, "failed to set wifi config, code %d", err);
if (auto_conn) return 0;
err = esp_wifi_connect ();
return (err == ESP_OK) ?
0 : luaL_error (L, "failed to begin connect, code %d", err);
} }
@ -343,14 +326,6 @@ static int wifi_sta_getconfig (lua_State *L)
lua_setfield (L, -2, "bssid"); lua_setfield (L, -2, "bssid");
} }
bool auto_conn;
err = esp_wifi_get_auto_connect (&auto_conn);
if (err != ESP_OK)
return luaL_error (L, "failed to get auto-connect, code %d", err);
lua_pushboolean (L, auto_conn);
lua_setfield (L, -2, "auto");
return 1; return 1;
} }
@ -477,4 +452,3 @@ LROT_END(wifi_sta, NULL, 0)
NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_STA_START, do_connect); NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_STA_START, do_connect);
NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, do_connect); NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, do_connect);
NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, on_scan_done); NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, on_scan_done);
#endif

View File

@ -44,7 +44,14 @@ Connection speed in Mbit/s, or error if not connected.
## eth.init() ## eth.init()
Initialize the PHY chip and set up its tcpip adapter. Initialize the internal ethernet interface by configuring the MAC and PHY
and starting the interface.
Note that while the PHY model and some GPIO pins are configured at runtime,
the clock configuration has been moved into the menuconfig and is no
longer available at runtime. Please refer to the settings available
under `Component config -> Ethernet -> Support ESP32 internal EMAC controller`.
#### Syntax #### Syntax
```lua ```lua
@ -52,32 +59,30 @@ eth.init(cfg)
``` ```
#### Parameters #### Parameters
- `cfg` table containing configuration data. All entries are mandatory: - `cfg` table containing configuration data. Unless otherwise noted,
- `addr` PHY address, 0 to 31 the entries are mandatory:
- `clock_mode` external/internal clock mode selection, one of - `addr` PHY address, 0 to 31, optional (default will attempt auto-detect)
- `eth.CLOCK_GPIO0_IN`
- `eth.CLOCK_GPIO0_OUT`
- `eth.CLOCK_GPIO16_OUT`
- `eth.CLOCK_GPIO17_OUT`
- `mdc` MDC pin number - `mdc` MDC pin number
- `mdio` MDIO pin number - `mdio` MDIO pin number
- `phy` PHY chip model, one of - `phy` PHY chip model, one of
- `eth.PHY_IP101` - `PHY_DP83848`
- `eth.PHY_LAN8720` - `PHY_IP101`
- `eth.PHY_TLK110` - `PHY_KSZ8041`
- `PHY_KSZ8081`
- `PHY_LAN8720`
- `PHY_RTL8201`
- `power` power enable pin, optional - `power` power enable pin, optional
#### Returns #### Returns
`nil` `nil`
An error is thrown in case of invalid parameters or if the ethernet driver failed. An error is thrown in case of invalid parameters, MAC/PHY setup errors, or if the ethernet interface has already been successfully configured.
#### Example #### Example
```lua ```lua
-- Initialize ESP32-GATEWAY -- Initialize ESP32-GATEWAY
eth.init({phy = eth.PHY_LAN8720, eth.init({phy = eth.PHY_LAN8720,
addr = 0, addr = 0,
clock_mode = eth.CLOCK_GPIO17_OUT,
power = 5, power = 5,
mdc = 23, mdc = 23,
mdio = 18}) mdio = 18})
@ -85,7 +90,6 @@ eth.init({phy = eth.PHY_LAN8720,
-- Initialize wESP32 -- Initialize wESP32
eth.init({phy = eth.PHY_LAN8720, eth.init({phy = eth.PHY_LAN8720,
addr = 0, addr = 0,
clock_mode = eth.CLOCK_GPIO0_IN,
mdc = 16, mdc = 16,
mdio = 17}) mdio = 17})
``` ```

View File

@ -89,9 +89,6 @@ not running. This is to enable users to choose whether to expend the power
necessary for radio comms. A sensor device running on battery might only necessary for radio comms. A sensor device running on battery might only
want to enable WiFi every 10th boot for example. want to enable WiFi every 10th boot for example.
If station mode is configured with `auto` set to true, the chip will attempt
to join the configured network at this point.
#### Syntax #### Syntax
`wifi.start()` `wifi.start()`
@ -131,6 +128,10 @@ Sets the WiFi station configuration.
The WiFi mode must be set to `wifi.STATION` or `wifi.STATIONAP` before this The WiFi mode must be set to `wifi.STATION` or `wifi.STATIONAP` before this
function can be used. function can be used.
Note that the earlier auto-connect feature is no longer available due to
being removed in the SDK/IDF. After start-up it is necessary to call
[`wifi.stat.connect()`](#wifistaconnect) manually.
#### Syntax #### Syntax
`wifi.sta.config(station_config, save)` `wifi.sta.config(station_config, save)`
@ -138,9 +139,6 @@ function can be used.
- `station_config` table containing configuration data for station - `station_config` table containing configuration data for station
- `ssid` string which is less than 32 bytes. - `ssid` string which is less than 32 bytes.
- `pwd` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point. - `pwd` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point.
- `auto` defaults to true
- `true` to enable auto connect and connect to access point, hence with `auto=true` there's no need to call [`wifi.sta.connect()`](#wifistaconnect)
- `false` to disable auto connect and remain disconnected from access point
- `bssid` string that contains the MAC address of the access point (optional) - `bssid` string that contains the MAC address of the access point (optional)
- You can set BSSID if you have multiple access points with the same SSID. - You can set BSSID if you have multiple access points with the same SSID.
- Note: if you set BSSID for a specific SSID and would like to configure station to connect to the same SSID only without the BSSID requirement, you MUST first configure to station to a different SSID first, then connect to the desired SSID - Note: if you set BSSID for a specific SSID and would like to configure station to connect to the same SSID only without the BSSID requirement, you MUST first configure to station to a different SSID first, then connect to the desired SSID
@ -179,13 +177,6 @@ station_cfg.pwd="password"
station_cfg.bssid="AA:BB:CC:DD:EE:FF" station_cfg.bssid="AA:BB:CC:DD:EE:FF"
wifi.sta.config(station_cfg) wifi.sta.config(station_cfg)
--configure station but don't connect to Access point
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.auto=false
wifi.sta.config(station_cfg)
``` ```
#### See also #### See also
@ -194,7 +185,9 @@ wifi.sta.config(station_cfg)
## wifi.sta.connect() ## wifi.sta.connect()
Connects to the configured AP in station mode. You only ever need to call this if auto-connect was disabled in [`wifi.sta.config()`](#wifistaconfig). Connects to the configured AP in station mode. You will want to call this
on start-up after [`wifi.start()`](#wifistart), and probably also in
response to getting `disconnected` events in order to reconnect.
#### Syntax #### Syntax
`wifi.sta.connect()` `wifi.sta.connect()`