Skip to content

Commit ec8343f

Browse files
committed
feat(eth): Ethernet - enc28j60 driver copied from IDF examples
1 parent d164df8 commit ec8343f

File tree

5 files changed

+1780
-1
lines changed

5 files changed

+1780
-1
lines changed

CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ set(ARDUINO_LIBRARY_ESP_SR_SRCS
140140

141141
set(ARDUINO_LIBRARY_ESPmDNS_SRCS libraries/ESPmDNS/src/ESPmDNS.cpp)
142142

143-
set(ARDUINO_LIBRARY_Ethernet_SRCS libraries/Ethernet/src/ETH.cpp)
143+
set(ARDUINO_LIBRARY_Ethernet_SRCS
144+
libraries/Ethernet/src/ETH.cpp
145+
libraries/Ethernet/src/utility/enc28j60/esp_eth_mac_enc28j60.c
146+
libraries/Ethernet/src/utility/enc28j60/esp_eth_phy_enc28j60.c)
144147

145148
set(ARDUINO_LIBRARY_FFat_SRCS libraries/FFat/src/FFat.cpp)
146149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/**
22+
* @brief SPI Instruction Set
23+
*
24+
*/
25+
#define ENC28J60_SPI_CMD_RCR (0x00) // Read Control Register
26+
#define ENC28J60_SPI_CMD_RBM (0x01) // Read Buffer Memory
27+
#define ENC28J60_SPI_CMD_WCR (0x02) // Write Control Register
28+
#define ENC28J60_SPI_CMD_WBM (0x03) // Write Buffer Memory
29+
#define ENC28J60_SPI_CMD_BFS (0x04) // Bit Field Set
30+
#define ENC28J60_SPI_CMD_BFC (0x05) // Bit Field Clear
31+
#define ENC28J60_SPI_CMD_SRC (0x07) // Soft Reset
32+
33+
/**
34+
* @brief Shared Registers in ENC28J60 (accessible on each bank)
35+
*
36+
*/
37+
#define ENC28J60_EIE (0x1B) // Ethernet Interrupt Enable
38+
#define ENC28J60_EIR (0x1C) // Ethernet Interrupt flags
39+
#define ENC28J60_ESTAT (0x1D) // Ethernet Status
40+
#define ENC28J60_ECON2 (0x1E) // Ethernet Control Register2
41+
#define ENC28J60_ECON1 (0x1F) // Ethernet Control Register1
42+
43+
/**
44+
* @brief Per-bank Registers in ENC28J60
45+
* @note Address[15:12]: Register Type, 0 -> ETH, 1 -> MII/MAC
46+
* Address[11:8] : Bank address
47+
* Address[7:0] : Register Index
48+
*/
49+
// Bank 0 Registers
50+
#define ENC28J60_ERDPTL (0x0000) // Read Pointer Low Byte ERDPT<7:0>)
51+
#define ENC28J60_ERDPTH (0x0001) // Read Pointer High Byte (ERDPT<12:8>)
52+
#define ENC28J60_EWRPTL (0x0002) // Write Pointer Low Byte (EWRPT<7:0>)
53+
#define ENC28J60_EWRPTH (0x0003) // Write Pointer High Byte (EWRPT<12:8>)
54+
#define ENC28J60_ETXSTL (0x0004) // TX Start Low Byte (ETXST<7:0>)
55+
#define ENC28J60_ETXSTH (0x0005) // TX Start High Byte (ETXST<12:8>)
56+
#define ENC28J60_ETXNDL (0x0006) // TX End Low Byte (ETXND<7:0>)
57+
#define ENC28J60_ETXNDH (0x0007) // TX End High Byte (ETXND<12:8>)
58+
#define ENC28J60_ERXSTL (0x0008) // RX Start Low Byte (ERXST<7:0>)
59+
#define ENC28J60_ERXSTH (0x0009) // RX Start High Byte (ERXST<12:8>)
60+
#define ENC28J60_ERXNDL (0x000A) // RX End Low Byte (ERXND<7:0>)
61+
#define ENC28J60_ERXNDH (0x000B) // RX End High Byte (ERXND<12:8>)
62+
#define ENC28J60_ERXRDPTL (0x000C) // RX RD Pointer Low Byte (ERXRDPT<7:0>)
63+
#define ENC28J60_ERXRDPTH (0x000D) // RX RD Pointer High Byte (ERXRDPT<12:8>)
64+
#define ENC28J60_ERXWRPTL (0x000E) // RX WR Pointer Low Byte (ERXWRPT<7:0>)
65+
#define ENC28J60_ERXWRPTH (0x000F) // RX WR Pointer High Byte (ERXWRPT<12:8>)
66+
#define ENC28J60_EDMASTL (0x0010) // DMA Start Low Byte (EDMAST<7:0>)
67+
#define ENC28J60_EDMASTH (0x0011) // DMA Start High Byte (EDMAST<12:8>)
68+
#define ENC28J60_EDMANDL (0x0012) // DMA End Low Byte (EDMAND<7:0>)
69+
#define ENC28J60_EDMANDH (0x0013) // DMA End High Byte (EDMAND<12:8>)
70+
#define ENC28J60_EDMADSTL (0x0014) // DMA Destination Low Byte (EDMADST<7:0>)
71+
#define ENC28J60_EDMADSTH (0x0015) // DMA Destination High Byte (EDMADST<12:8>)
72+
#define ENC28J60_EDMACSL (0x0016) // DMA Checksum Low Byte (EDMACS<7:0>)
73+
#define ENC28J60_EDMACSH (0x0017) // DMA Checksum High Byte (EDMACS<15:8>)
74+
75+
// Bank 1 Registers
76+
#define ENC28J60_EHT0 (0x0100) // Hash Table Byte 0 (EHT<7:0>)
77+
#define ENC28J60_EHT1 (0x0101) // Hash Table Byte 1 (EHT<15:8>)
78+
#define ENC28J60_EHT2 (0x0102) // Hash Table Byte 2 (EHT<23:16>)
79+
#define ENC28J60_EHT3 (0x0103) // Hash Table Byte 3 (EHT<31:24>)
80+
#define ENC28J60_EHT4 (0x0104) // Hash Table Byte 4 (EHT<39:32>)
81+
#define ENC28J60_EHT5 (0x0105) // Hash Table Byte 5 (EHT<47:40>)
82+
#define ENC28J60_EHT6 (0x0106) // Hash Table Byte 6 (EHT<55:48>)
83+
#define ENC28J60_EHT7 (0x0107) // Hash Table Byte 7 (EHT<63:56>)
84+
#define ENC28J60_EPMM0 (0x0108) // Pattern Match Mask Byte 0 (EPMM<7:0>)
85+
#define ENC28J60_EPMM1 (0x0109) // Pattern Match Mask Byte 1 (EPMM<15:8>)
86+
#define ENC28J60_EPMM2 (0x010A) // Pattern Match Mask Byte 2 (EPMM<23:16>)
87+
#define ENC28J60_EPMM3 (0x010B) // Pattern Match Mask Byte 3 (EPMM<31:24>)
88+
#define ENC28J60_EPMM4 (0x010C) // Pattern Match Mask Byte 4 (EPMM<39:32>)
89+
#define ENC28J60_EPMM5 (0x010D) // Pattern Match Mask Byte 5 (EPMM<47:40>)
90+
#define ENC28J60_EPMM6 (0x010E) // Pattern Match Mask Byte 6 (EPMM<55:48>)
91+
#define ENC28J60_EPMM7 (0x010F) // Pattern Match Mask Byte 7 (EPMM<63:56>)
92+
#define ENC28J60_EPMCSL (0x0110) // Pattern Match Checksum Low Byte (EPMCS<7:0>)
93+
#define ENC28J60_EPMCSH (0x0111) // Pattern Match Checksum High Byte (EPMCS<15:0>)
94+
#define ENC28J60_EPMOL (0x0114) // Pattern Match Offset Low Byte (EPMO<7:0>)
95+
#define ENC28J60_EPMOH (0x0115) // Pattern Match Offset High Byte (EPMO<12:8>)
96+
#define ENC28J60_ERXFCON (0x0118) // Receive Fileter Control
97+
#define ENC28J60_EPKTCNT (0x0119) // Ethernet Packet Count
98+
99+
// Bank 2 Register
100+
#define ENC28J60_MACON1 (0x1200) // MAC Control Register 1
101+
#define ENC28J60_MACON2 (0x1201) // MAC Control Register 2
102+
#define ENC28J60_MACON3 (0x1202) // MAC Control Register 3
103+
#define ENC28J60_MACON4 (0x1203) // MAC Control Register 4
104+
#define ENC28J60_MABBIPG (0x1204) // Back-to-Back Inter-Packet Gap (BBIPG<6:0>)
105+
#define ENC28J60_MAIPGL (0x1206) // Non-Back-to-Back Inter-Packet Gap Low Byte (MAIPGL<6:0>)
106+
#define ENC28J60_MAIPGH (0x1207) // Non-Back-to-Back Inter-Packet Gap High Byte (MAIPGH<6:0>)
107+
#define ENC28J60_MACLCON1 (0x1208) // Retransmission Maximum (RETMAX<3:0>)
108+
#define ENC28J60_MACLCON2 (0x1209) // Collision Window (COLWIN<5:0>)
109+
#define ENC28J60_MAMXFLL (0x120A) // Maximum Frame Length Low Byte (MAMXFL<7:0>)
110+
#define ENC28J60_MAMXFLH (0x120B) // Maximum Frame Length High Byte (MAMXFL<15:8>)
111+
#define ENC28J60_MICMD (0x1212) // MII Command Register
112+
#define ENC28J60_MIREGADR (0x1214) // MII Register Address (MIREGADR<4:0>)
113+
#define ENC28J60_MIWRL (0x1216) // MII Write Data Low Byte (MIWR<7:0>)
114+
#define ENC28J60_MIWRH (0x1217) // MII Write Data High Byte (MIWR<15:8>)
115+
#define ENC28J60_MIRDL (0x1218) // MII Read Data Low Byte (MIRD<7:0>)
116+
#define ENC28J60_MIRDH (0x1219) // MII Read Data High Byte(MIRD<15:8>)
117+
118+
// Bank 3 Registers
119+
#define ENC28J60_MAADR5 (0x1300) // MAC Address Byte 5 (MAADR<15:8>)
120+
#define ENC28J60_MAADR6 (0x1301) // MAC Address Byte 6 (MAADR<7:0>)
121+
#define ENC28J60_MAADR3 (0x1302) // MAC Address Byte 3 (MAADR<31:24>), OUI Byte 3
122+
#define ENC28J60_MAADR4 (0x1303) // MAC Address Byte 4 (MAADR<23:16>)
123+
#define ENC28J60_MAADR1 (0x1304) // MAC Address Byte 1 (MAADR<47:40>), OUI Byte 1
124+
#define ENC28J60_MAADR2 (0x1305) // MAC Address Byte 2 (MAADR<39:32>), OUI Byte 2
125+
#define ENC28J60_EBSTSD (0x0306) // Built-in Self-Test Fill Seed (EBSTSD<7:0>)
126+
#define ENC28J60_EBSTCON (0x0307) // Built-in Self-Test Control
127+
#define ENC28J60_EBSTCSL (0x0308) // Built-in Self-Test Checksum Low Byte (EBSTCS<7:0>)
128+
#define ENC28J60_EBSTCSH (0x0309) // Built-in Self-Test Checksum High Byte (EBSTCS<15:8>)
129+
#define ENC28J60_MISTAT (0x130A) // MII Status Register
130+
#define ENC28J60_EREVID (0x0312) // Ethernet Revision ID (EREVID<4:0>)
131+
#define ENC28J60_ECOCON (0x0315) // Clock Output Control Register
132+
#define ENC28J60_EFLOCON (0x0317) // Ethernet Flow Control
133+
#define ENC28J60_EPAUSL (0x0318) // Pause Timer Value Low Byte (EPAUS<7:0>)
134+
#define ENC28J60_EPAUSH (0x0319) // Pause Timer Value High Byte (EPAUS<15:8>)
135+
136+
/**
137+
* @brief status and flag of ENC28J60 specific registers
138+
*
139+
*/
140+
// EIE bit definitions
141+
#define EIE_INTIE (1<<7) // Global INT Interrupt Enable
142+
#define EIE_PKTIE (1<<6) // Receive Packet Pending Interrupt Enable
143+
#define EIE_DMAIE (1<<5) // DMA Interrupt Enable
144+
#define EIE_LINKIE (1<<4) // Link Status Change Interrupt Enable
145+
#define EIE_TXIE (1<<3) // Transmit Enable
146+
#define EIE_TXERIE (1<<1) // Transmit Error Interrupt Enable
147+
#define EIE_RXERIE (1<<0) // Receive Error Interrupt Enable
148+
149+
// EIR bit definitions
150+
#define EIR_PKTIF (1<<6) // Receive Packet Pending Interrupt Flag
151+
#define EIR_DMAIF (1<<5) // DMA Interrupt Flag
152+
#define EIR_LINKIF (1<<4) // Link Change Interrupt Flag
153+
#define EIR_TXIF (1<<3) // Transmit Interrupt Flag
154+
#define EIR_TXERIF (1<<1) // Transmit Error Interrupt Flag
155+
#define EIR_RXERIF (1<<0) // Receive Error Interrupt Flag
156+
157+
// ESTAT bit definitions
158+
#define ESTAT_INT (1<<7) // INT Interrupt Flag
159+
#define ESTAT_BUFER (1<<6) // Buffer Error Status
160+
#define ESTAT_LATECOL (1<<4) // Late Collision Error
161+
#define ESTAT_RXBUSY (1<<2) // Receive Busy
162+
#define ESTAT_TXABRT (1<<1) // Transmit Abort Error
163+
#define ESTAT_CLKRDY (1<<0) // Clock Ready
164+
165+
// ECON2 bit definitions
166+
#define ECON2_AUTOINC (1<<7) // Automatic Buffer Pointer Increment Enable
167+
#define ECON2_PKTDEC (1<<6) // Packet Decrement
168+
#define ECON2_PWRSV (1<<5) // Power Save Enable
169+
#define ECON2_VRPS (1<<3) // Voltage Regulator Power Save Enable
170+
171+
// ECON1 bit definitions
172+
#define ECON1_TXRST (1<<7) // Transmit Logic Reset
173+
#define ECON1_RXRST (1<<6) // Receive Logic Reset
174+
#define ECON1_DMAST (1<<5) // DMA Start and Busy Status
175+
#define ECON1_CSUMEN (1<<4) // DMA Checksum Enable
176+
#define ECON1_TXRTS (1<<3) // Transmit Request to Send
177+
#define ECON1_RXEN (1<<2) // Receive Enable
178+
#define ECON1_BSEL1 (1<<1) // Bank Select1
179+
#define ECON1_BSEL0 (1<<0) // Bank Select0
180+
181+
// ERXFCON bit definitions
182+
#define ERXFCON_UCEN (1<<7) // Unicast Filter Enable
183+
#define ERXFCON_ANDOR (1<<6) // AND/OR Filter Select
184+
#define ERXFCON_CRCEN (1<<5) // Post-Filter CRC Check Enable
185+
#define ERXFCON_PMEN (1<<4) // Pattern Match Filter Enable
186+
#define ERXFCON_MPEN (1<<3) // Magic Packet Filter Enable
187+
#define ERXFCON_HTEN (1<<2) // Hash Table Filter Enable
188+
#define ERXFCON_MCEN (1<<1) // Multicast Filter Enable
189+
#define ERXFCON_BCEN (1<<0) // Broadcast Filter Enable
190+
191+
// MACON1 bit definitions
192+
#define MACON1_TXPAUS (1<<3) // Pause Control Frame Transmission Enable
193+
#define MACON1_RXPAUS (1<<2) // Pause Control Frame Reception Enable
194+
#define MACON1_PASSALL (1<<1) // Pass All Received Frames Enable
195+
#define MACON1_MARXEN (1<<0) // MAC Receive Enable
196+
197+
// MACON3 bit definitions
198+
#define MACON3_PADCFG2 (1<<7) // Automatic Pad and CRC Configuration bit 2
199+
#define MACON3_PADCFG1 (1<<6) // Automatic Pad and CRC Configuration bit 1
200+
#define MACON3_PADCFG0 (1<<5) // Automatic Pad and CRC Configuration bit 0
201+
#define MACON3_TXCRCEN (1<<4) // Transmit CRC Enable
202+
#define MACON3_PHDRLEN (1<<3) // Proprietary Header Enable
203+
#define MACON3_HFRMLEN (1<<2) // Huge Frame Enable
204+
#define MACON3_FRMLNEN (1<<1) // Frame Length Checking Enable
205+
#define MACON3_FULDPX (1<<0) // MAC Full-Duplex Enable
206+
207+
// MACON4 bit definitions
208+
#define MACON4_DEFER (1<<6) // Defer Transmission Enable
209+
#define MACON4_BPEN (1<<5) // No Backoff During Backpressure Enable
210+
#define MACON4_NOBKFF (1<<4) // No Backoff Enable
211+
212+
// MICMD bit definitions
213+
#define MICMD_MIISCAN (1<<1) // MII Scan Enable
214+
#define MICMD_MIIRD (1<<0) // MII Read Enable
215+
216+
// EBSTCON bit definitions
217+
#define EBSTCON_PSV2 (1<<7) // Pattern Shift Value 2
218+
#define EBSTCON_PSV1 (1<<6) // Pattern Shift Value 1
219+
#define EBSTCON_PSV0 (1<<5) // Pattern Shift Value 0
220+
#define EBSTCON_PSEL (1<<4) // Port Select
221+
#define EBSTCON_TMSEL1 (1<<3) // Test Mode Select 1
222+
#define EBSTCON_TMSEL0 (1<<2) // Test Mode Select 0
223+
#define EBSTCON_TME (1<<1) // Test Mode Enable
224+
#define EBSTCON_BISTST (1<<0) // Built-in Self-Test Start/Busy
225+
226+
// MISTAT bit definitions
227+
#define MISTAT_NVALID (1<<2) // MII Management Read Data Not Valid
228+
#define MISTAT_SCAN (1<<1) // MII Management Scan Operation in Progress
229+
#define MISTAT_BUSY (1<<0) // MII Management Busy
230+
231+
// EFLOCON bit definitions
232+
#define EFLOCON_FULDPXS (1<<2) // Full-Duplex Shadown
233+
#define EFLOCON_FCEN1 (1<<1) // Flow Control Enable 1
234+
#define EFLOCON_FCEN0 (1<<0) // Flow Control Enable 0
235+
236+
#ifdef __cplusplus
237+
}
238+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include "esp_eth_phy.h"
14+
#include "esp_eth_mac.h"
15+
#include "driver/spi_master.h"
16+
17+
#define CS_HOLD_TIME_MIN_NS 210
18+
19+
/**
20+
* @brief ENC28J60 specific configuration
21+
*
22+
*/
23+
typedef struct {
24+
spi_host_device_t spi_host_id; /*!< SPI peripheral */
25+
spi_device_interface_config_t *spi_devcfg; /*!< SPI device configuration */
26+
int int_gpio_num; /*!< Interrupt GPIO number */
27+
} eth_enc28j60_config_t;
28+
29+
/**
30+
* @brief ENC28J60 Supported Revisions
31+
*
32+
*/
33+
typedef enum {
34+
ENC28J60_REV_B1 = 0b00000010,
35+
ENC28J60_REV_B4 = 0b00000100,
36+
ENC28J60_REV_B5 = 0b00000101,
37+
ENC28J60_REV_B7 = 0b00000110
38+
} eth_enc28j60_rev_t;
39+
40+
/**
41+
* @brief Default ENC28J60 specific configuration
42+
*
43+
*/
44+
#define ETH_ENC28J60_DEFAULT_CONFIG(spi_host, spi_devcfg_p) \
45+
{ \
46+
.spi_host_id = spi_host, \
47+
.spi_devcfg = spi_devcfg_p, \
48+
.int_gpio_num = 4, \
49+
}
50+
51+
/**
52+
* @brief Compute amount of SPI bit-cycles the CS should stay active after the transmission
53+
* to meet ENC28J60 CS Hold Time specification.
54+
*
55+
* @param clock_speed_mhz SPI Clock frequency in MHz (valid range is <1, 20>)
56+
* @return uint8_t
57+
*/
58+
static inline uint8_t enc28j60_cal_spi_cs_hold_time(int clock_speed_mhz) {
59+
if (clock_speed_mhz <= 0 || clock_speed_mhz > 20) {
60+
return 0;
61+
}
62+
int temp = clock_speed_mhz * CS_HOLD_TIME_MIN_NS;
63+
uint8_t cs_posttrans = temp / 1000;
64+
if (temp % 1000) {
65+
cs_posttrans += 1;
66+
}
67+
68+
return cs_posttrans;
69+
}
70+
71+
/**
72+
* @brief Create ENC28J60 Ethernet MAC instance
73+
*
74+
* @param[in] enc28j60_config: ENC28J60 specific configuration
75+
* @param[in] mac_config: Ethernet MAC configuration
76+
*
77+
* @return
78+
* - instance: create MAC instance successfully
79+
* - NULL: create MAC instance failed because some error occurred
80+
*/
81+
esp_eth_mac_t *esp_eth_mac_new_enc28j60(const eth_enc28j60_config_t *enc28j60_config, const eth_mac_config_t *mac_config);
82+
83+
/**
84+
* @brief Create a PHY instance of ENC28J60
85+
*
86+
* @param[in] config: configuration of PHY
87+
*
88+
* @return
89+
* - instance: create PHY instance successfully
90+
* - NULL: create PHY instance failed because some error occurred
91+
*/
92+
esp_eth_phy_t *esp_eth_phy_new_enc28j60(const eth_phy_config_t *config);
93+
94+
/**
95+
* @brief Get ENC28J60 silicon revision ID
96+
*
97+
* @param mac ENC28J60 MAC Handle
98+
* @return eth_enc28j60_rev_t
99+
* - returns silicon revision ID read during initialization
100+
*/
101+
eth_enc28j60_rev_t emac_enc28j60_get_chip_info(esp_eth_mac_t *mac);
102+
103+
#ifdef __cplusplus
104+
}
105+
#endif

0 commit comments

Comments
 (0)