-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathETH.h
225 lines (193 loc) · 6.81 KB
/
ETH.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
ETH.h - espre ETH PHY support.
Based on WiFi.h from Ardiono WiFi shield library.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _ETH_H_
#define _ETH_H_
//
// Example configurations for pins_arduino.h to allow starting with ETH.begin();
//
// // Example RMII LAN8720 (Olimex, etc.)
// #define ETH_PHY_TYPE ETH_PHY_LAN8720
// #define ETH_PHY_ADDR 0
// #define ETH_PHY_MDC 23
// #define ETH_PHY_MDIO 18
// #define ETH_PHY_POWER -1
// #define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
// // Example RMII ESP32_Ethernet_V4
// #define ETH_PHY_TYPE ETH_PHY_TLK110
// #define ETH_PHY_ADDR 1
// #define ETH_PHY_MDC 23
// #define ETH_PHY_MDIO 18
// #define ETH_PHY_POWER -1
// #define ETH_CLK_MODE ETH_CLOCK_GPIO0_OUT
// // Example SPI using ESP-IDF's driver
// #define ETH_PHY_TYPE ETH_PHY_W5500
// #define ETH_PHY_ADDR 1
// #define ETH_PHY_CS 15
// #define ETH_PHY_IRQ 4
// #define ETH_PHY_RST 5
// #define ETH_PHY_SPI_HOST SPI2_HOST
// #define ETH_PHY_SPI_SCK 14
// #define ETH_PHY_SPI_MISO 12
// #define ETH_PHY_SPI_MOSI 13
// // Example SPI using Arduino's driver
// #define ETH_PHY_TYPE ETH_PHY_W5500
// #define ETH_PHY_ADDR 1
// #define ETH_PHY_CS 15
// #define ETH_PHY_IRQ 4
// #define ETH_PHY_RST 5
// #define ETH_PHY_SPI SPI
// This will be uncommented once custom SPI support is available in ESP-IDF
#define ETH_SPI_SUPPORTS_CUSTOM 1
#define ETH_SPI_SUPPORTS_NO_IRQ 1
#include "Network.h"
#if ETH_SPI_SUPPORTS_CUSTOM
#include "SPI.h"
#endif
#include "esp_system.h"
#include "esp_eth.h"
#include "esp_netif.h"
#if CONFIG_ETH_USE_ESP32_EMAC
#define ETH_PHY_IP101 ETH_PHY_TLK110
typedef enum {
ETH_CLOCK_GPIO0_IN,
ETH_CLOCK_GPIO0_OUT,
ETH_CLOCK_GPIO16_OUT,
ETH_CLOCK_GPIO17_OUT
} eth_clock_mode_t;
//Dedicated GPIOs for RMII
#define ETH_RMII_TX_EN 21
#define ETH_RMII_TX0 19
#define ETH_RMII_TX1 22
#define ETH_RMII_RX0 25
#define ETH_RMII_RX1_EN 26
#define ETH_RMII_CRS_DV 27
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
#ifndef ETH_PHY_SPI_FREQ_MHZ
#define ETH_PHY_SPI_FREQ_MHZ 20
#endif /* ETH_PHY_SPI_FREQ_MHZ */
#define ETH_PHY_ADDR_AUTO ESP_ETH_PHY_ADDR_AUTO
typedef enum {
#if CONFIG_ETH_USE_ESP32_EMAC
ETH_PHY_LAN8720,
ETH_PHY_TLK110,
ETH_PHY_RTL8201,
ETH_PHY_DP83848,
ETH_PHY_KSZ8041,
ETH_PHY_KSZ8081,
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
#if CONFIG_ETH_SPI_ETHERNET_DM9051
ETH_PHY_DM9051,
#endif
#if CONFIG_ETH_SPI_ETHERNET_W5500
ETH_PHY_W5500,
#endif
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
ETH_PHY_KSZ8851,
#endif
ETH_PHY_MAX
} eth_phy_type_t;
class ETHClass : public NetworkInterface {
public:
ETHClass(uint8_t eth_index = 0);
~ETHClass();
#if CONFIG_ETH_USE_ESP32_EMAC
bool begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clk_mode);
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
#if ETH_SPI_SUPPORTS_CUSTOM
bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz = ETH_PHY_SPI_FREQ_MHZ);
#endif
bool begin(
eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck = -1, int miso = -1, int mosi = -1,
uint8_t spi_freq_mhz = ETH_PHY_SPI_FREQ_MHZ
);
bool begin() {
#if defined(ETH_PHY_TYPE) && defined(ETH_PHY_ADDR)
#if defined(CONFIG_ETH_USE_ESP32_EMAC) && defined(ETH_PHY_POWER) && defined(ETH_PHY_MDC) && defined(ETH_PHY_MDIO) && defined(ETH_CLK_MODE)
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_POWER, ETH_CLK_MODE);
#elif defined(ETH_PHY_CS) && defined(ETH_PHY_IRQ) && defined(ETH_PHY_RST)
#if ETH_SPI_SUPPORTS_CUSTOM && defined(ETH_PHY_SPI)
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI, ETH_PHY_SPI_FREQ_MHZ);
#elif defined(ETH_PHY_SPI_HOST) && defined(ETH_PHY_SPI_SCK) && defined(ETH_PHY_SPI_MISO) && defined(ETH_PHY_SPI_MOSI)
return begin(
ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI_HOST, ETH_PHY_SPI_SCK, ETH_PHY_SPI_MISO, ETH_PHY_SPI_MOSI,
ETH_PHY_SPI_FREQ_MHZ
);
#endif
#endif
#endif
return false;
}
void end();
// This function must be called before `begin()`
void setTaskStackSize(size_t size);
// ETH Handle APIs
bool fullDuplex() const;
uint8_t linkSpeed() const;
bool autoNegotiation() const;
uint32_t phyAddr() const;
esp_eth_handle_t handle() const;
#if ETH_SPI_SUPPORTS_CUSTOM
static esp_err_t _eth_spi_read(void *ctx, uint32_t cmd, uint32_t addr, void *data, uint32_t data_len);
static esp_err_t _eth_spi_write(void *ctx, uint32_t cmd, uint32_t addr, const void *data, uint32_t data_len);
#endif
protected:
#if ETH_SPI_SUPPORTS_CUSTOM
esp_err_t eth_spi_read(uint32_t cmd, uint32_t addr, void *data, uint32_t data_len);
esp_err_t eth_spi_write(uint32_t cmd, uint32_t addr, const void *data, uint32_t data_len);
#endif
// void getMac(uint8_t* mac);
size_t printDriverInfo(Print &out) const;
// static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
public:
void _onEthEvent(int32_t event_id, void *event_data);
private:
esp_eth_handle_t _eth_handle;
uint8_t _eth_index;
eth_phy_type_t _phy_type;
esp_eth_netif_glue_handle_t _glue_handle;
esp_eth_mac_t *_mac;
esp_eth_phy_t *_phy;
#if ETH_SPI_SUPPORTS_CUSTOM
SPIClass *_spi;
char _cs_str[10];
#endif
uint8_t _spi_freq_mhz;
int8_t _pin_cs;
int8_t _pin_irq;
int8_t _pin_rst;
int8_t _pin_sck;
int8_t _pin_miso;
int8_t _pin_mosi;
#if CONFIG_ETH_USE_ESP32_EMAC
int8_t _pin_mcd;
int8_t _pin_mdio;
int8_t _pin_power;
int8_t _pin_rmii_clock;
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
size_t _task_stack_size;
static bool ethDetachBus(void *bus_pointer);
bool beginSPI(
eth_phy_type_t type, int32_t phy_addr, uint8_t *mac_addr, int cs, int irq, int rst,
#if ETH_SPI_SUPPORTS_CUSTOM
SPIClass *spi,
#endif
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz
);
friend class EthernetClass; // to access beginSPI
};
extern ETHClass ETH;
#endif /* _ETH_H_ */