forked from espressif/arduino-esp32
Initial IDF-4.0 port
SmartConfig and ETH need some work to adapt to the new API
This commit is contained in:
@ -1,103 +0,0 @@
|
||||
/*
|
||||
This sketch shows how to configure different external or internal clock sources for the Ethernet PHY
|
||||
*/
|
||||
|
||||
#include <ETH.h>
|
||||
|
||||
/*
|
||||
* ETH_CLOCK_GPIO0_IN - default: external clock from crystal oscillator
|
||||
* ETH_CLOCK_GPIO0_OUT - 50MHz clock from internal APLL output on GPIO0 - possibly an inverter is needed for LAN8720
|
||||
* ETH_CLOCK_GPIO16_OUT - 50MHz clock from internal APLL output on GPIO16 - possibly an inverter is needed for LAN8720
|
||||
* ETH_CLOCK_GPIO17_OUT - 50MHz clock from internal APLL inverted output on GPIO17 - tested with LAN8720
|
||||
*/
|
||||
#ifdef ETH_CLK_MODE
|
||||
#undef ETH_CLK_MODE
|
||||
#endif
|
||||
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
|
||||
|
||||
// Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source)
|
||||
#define ETH_POWER_PIN -1
|
||||
|
||||
// Type of the Ethernet PHY (LAN8720 or TLK110)
|
||||
#define ETH_TYPE ETH_PHY_LAN8720
|
||||
|
||||
// I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
|
||||
#define ETH_ADDR 0
|
||||
|
||||
// Pin# of the I²C clock signal for the Ethernet PHY
|
||||
#define ETH_MDC_PIN 15
|
||||
|
||||
// Pin# of the I²C IO signal for the Ethernet PHY
|
||||
#define ETH_MDIO_PIN 2
|
||||
|
||||
|
||||
static bool eth_connected = false;
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event) {
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_ETH_START:
|
||||
Serial.println("ETH Started");
|
||||
//set eth hostname here
|
||||
ETH.setHostname("esp32-ethernet");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_CONNECTED:
|
||||
Serial.println("ETH Connected");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_GOT_IP:
|
||||
Serial.print("ETH MAC: ");
|
||||
Serial.print(ETH.macAddress());
|
||||
Serial.print(", IPv4: ");
|
||||
Serial.print(ETH.localIP());
|
||||
if (ETH.fullDuplex()) {
|
||||
Serial.print(", FULL_DUPLEX");
|
||||
}
|
||||
Serial.print(", ");
|
||||
Serial.print(ETH.linkSpeed());
|
||||
Serial.println("Mbps");
|
||||
eth_connected = true;
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_DISCONNECTED:
|
||||
Serial.println("ETH Disconnected");
|
||||
eth_connected = false;
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_STOP:
|
||||
Serial.println("ETH Stopped");
|
||||
eth_connected = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void testClient(const char * host, uint16_t port) {
|
||||
Serial.print("\nconnecting to ");
|
||||
Serial.println(host);
|
||||
|
||||
WiFiClient client;
|
||||
if (!client.connect(host, port)) {
|
||||
Serial.println("connection failed");
|
||||
return;
|
||||
}
|
||||
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
|
||||
while (client.connected() && !client.available());
|
||||
while (client.available()) {
|
||||
Serial.write(client.read());
|
||||
}
|
||||
|
||||
Serial.println("closing connection\n");
|
||||
client.stop();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
if (eth_connected) {
|
||||
testClient("google.com", 80);
|
||||
}
|
||||
delay(10000);
|
||||
}
|
@ -31,7 +31,6 @@ WPS
|
||||
static esp_wps_config_t config;
|
||||
|
||||
void wpsInitConfig(){
|
||||
config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
|
||||
config.wps_type = ESP_WPS_MODE;
|
||||
strcpy(config.factory_info.manufacturer, ESP_MANUFACTURER);
|
||||
strcpy(config.factory_info.model_number, ESP_MODEL_NUMBER);
|
||||
|
@ -16,8 +16,7 @@ void setup() {
|
||||
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
|
||||
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
|
||||
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
|
||||
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
|
||||
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
|
||||
esp_wifi_sta_wpa2_ent_enable();
|
||||
WiFi.begin(ssid); //connect to wifi
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
|
@ -19,14 +19,21 @@
|
||||
*/
|
||||
|
||||
#include "ETH.h"
|
||||
#include "eth_phy/phy.h"
|
||||
#include "eth_phy/phy_tlk110.h"
|
||||
#include "eth_phy/phy_lan8720.h"
|
||||
#include "esp_system.h"
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
#include "esp_eth.h"
|
||||
#include "esp_eth_phy.h"
|
||||
#else
|
||||
#include "eth_phy/phy.h"
|
||||
#include "eth_phy/phy_tlk110.h"
|
||||
#include "eth_phy/phy_lan8720.h"
|
||||
#endif
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/dns.h"
|
||||
|
||||
extern void tcpipInit();
|
||||
|
||||
#ifndef ESP_IDF_VERSION_MAJOR
|
||||
static int _eth_phy_mdc_pin = -1;
|
||||
static int _eth_phy_mdio_pin = -1;
|
||||
static int _eth_phy_power_pin = -1;
|
||||
@ -48,6 +55,7 @@ static void _eth_phy_power_enable(bool enable)
|
||||
digitalWrite(_eth_phy_power_pin, enable);
|
||||
delay(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
ETHClass::ETHClass():initialized(false),started(false),staticIP(false)
|
||||
{
|
||||
@ -56,6 +64,11 @@ ETHClass::ETHClass():initialized(false),started(false),staticIP(false)
|
||||
ETHClass::~ETHClass()
|
||||
{}
|
||||
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type){
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode)
|
||||
{
|
||||
esp_err_t err;
|
||||
@ -108,6 +121,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
|
||||
{
|
||||
@ -240,17 +254,29 @@ bool ETHClass::setHostname(const char * hostname)
|
||||
|
||||
bool ETHClass::fullDuplex()
|
||||
{
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
return true;//todo
|
||||
#else
|
||||
return eth_config.phy_get_duplex_mode();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ETHClass::linkUp()
|
||||
{
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
return true;//todo
|
||||
#else
|
||||
return eth_config.phy_check_link();
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t ETHClass::linkSpeed()
|
||||
{
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
return 100;//ToDo
|
||||
#else
|
||||
return eth_config.phy_get_speed_mode()?100:10;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ETHClass::enableIpV6()
|
||||
@ -272,15 +298,23 @@ uint8_t * macAddress(uint8_t* mac)
|
||||
if(!mac){
|
||||
return NULL;
|
||||
}
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
//ToDo
|
||||
#else
|
||||
esp_eth_get_mac(mac);
|
||||
#endif
|
||||
return mac;
|
||||
}
|
||||
|
||||
String ETHClass::macAddress(void)
|
||||
{
|
||||
uint8_t mac[6];
|
||||
uint8_t mac[6] = {0,0,0,0,0,0};//ToDo
|
||||
char macStr[18] = { 0 };
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
//ToDo
|
||||
#else
|
||||
esp_eth_get_mac(mac);
|
||||
#endif
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
#define _ETH_H_
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_eth.h"
|
||||
|
||||
#ifndef ETH_PHY_ADDR
|
||||
@ -45,8 +46,12 @@
|
||||
#endif
|
||||
|
||||
#ifndef ETH_CLK_MODE
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
#define ETH_CLK_MODE ESP_ETH_CLOCK_GPIO0_IN
|
||||
#else
|
||||
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX } eth_phy_type_t;
|
||||
|
||||
@ -55,13 +60,21 @@ class ETHClass {
|
||||
bool initialized;
|
||||
bool started;
|
||||
bool staticIP;
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
esp_eth_config_t eth_config;
|
||||
#else
|
||||
eth_config_t eth_config;
|
||||
#endif
|
||||
public:
|
||||
ETHClass();
|
||||
~ETHClass();
|
||||
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, eth_phy_type_t type=ETH_PHY_TYPE);
|
||||
#else
|
||||
bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
|
||||
|
||||
#endif
|
||||
|
||||
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
|
||||
|
||||
const char * getHostname();
|
||||
|
@ -46,7 +46,11 @@ private:
|
||||
return 0;
|
||||
}
|
||||
int count;
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
int res = lwip_ioctl(_fd, FIONREAD, &count);
|
||||
#else
|
||||
int res = lwip_ioctl_r(_fd, FIONREAD, &count);
|
||||
#endif
|
||||
if(res < 0) {
|
||||
_failed = true;
|
||||
return 0;
|
||||
@ -227,7 +231,11 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout * 1000;
|
||||
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
int res = lwip_connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
||||
#else
|
||||
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
||||
#endif
|
||||
if (res < 0 && errno != EINPROGRESS) {
|
||||
log_e("connect on fd %d, errno: %d, \"%s\"", sockfd, errno, strerror(errno));
|
||||
close(sockfd);
|
||||
|
@ -687,7 +687,12 @@ bool WiFiSTAClass::beginSmartConfig() {
|
||||
esp_wifi_disconnect();
|
||||
|
||||
esp_err_t err;
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
smartconfig_start_config_t conf = SMARTCONFIG_START_CONFIG_DEFAULT();
|
||||
err = esp_smartconfig_start(&conf);
|
||||
#else
|
||||
err = esp_smartconfig_start(reinterpret_cast<sc_callback_t>(&WiFiSTAClass::_smartConfigCallback), 1);
|
||||
#endif
|
||||
if (err == ESP_OK) {
|
||||
_smartConfigStarted = true;
|
||||
_smartConfigDone = false;
|
||||
@ -734,6 +739,7 @@ const char * sc_type_strings[] = {
|
||||
#endif
|
||||
|
||||
void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
|
||||
#ifndef ESP_IDF_VERSION_MAJOR //todo
|
||||
smartconfig_status_t status = (smartconfig_status_t) st;
|
||||
log_d("Status: %s", sc_status_strings[st % 5]);
|
||||
if (status == SC_STATUS_GETTING_SSID_PSWD) {
|
||||
@ -757,4 +763,5 @@ void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
|
||||
}
|
||||
WiFi.stopSmartConfig();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -49,7 +49,11 @@ WiFiClient WiFiServer::available(){
|
||||
else {
|
||||
struct sockaddr_in _client;
|
||||
int cs = sizeof(struct sockaddr_in);
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
client_sock = lwip_accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
#else
|
||||
client_sock = lwip_accept_r(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
#endif
|
||||
}
|
||||
if(client_sock >= 0){
|
||||
int val = 1;
|
||||
@ -99,7 +103,11 @@ bool WiFiServer::hasClient() {
|
||||
}
|
||||
struct sockaddr_in _client;
|
||||
int cs = sizeof(struct sockaddr_in);
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
_accepted_sockfd = lwip_accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
#else
|
||||
_accepted_sockfd = lwip_accept_r(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
#endif
|
||||
if (_accepted_sockfd >= 0) {
|
||||
return true;
|
||||
}
|
||||
@ -107,7 +115,11 @@ bool WiFiServer::hasClient() {
|
||||
}
|
||||
|
||||
void WiFiServer::end(){
|
||||
#ifdef ESP_IDF_VERSION_MAJOR
|
||||
lwip_close(sockfd);
|
||||
#else
|
||||
lwip_close_r(sockfd);
|
||||
#endif
|
||||
sockfd = -1;
|
||||
_listening = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user