1# ENC28J60 Example
2(See the README.md file in the upper level 'examples' directory for more information about examples.)
3
4## Overview
5
6ENC28J60 is a standalone Ethernet controller with a standard SPI interface. This example demonstrates how to drive this controller as an SPI device and then attach to TCP/IP stack.
7
8This is also an example of how to integrate a new Ethernet MAC driver into the `esp_eth` component, without needing to modify the ESP-IDF component.
9
10If you have a more complicated application to go (for example, connect to some IoT cloud via MQTT), you can always reuse the initialization codes in this example.
11
12## How to use example
13
14### Hardware Required
15
16To run this example, you need to prepare following hardwares:
17* [ESP32 board](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html) (e.g. ESP32-PICO, ESP32 DevKitC, etc)
18* ENC28J60 module (the latest revision should be 6)
19* **!! IMPORTANT !!** Proper input power source since ENC28J60 is quite power consuming device (it consumes more than 200 mA in peaks when transmitting). If improper power source is used, input voltage may drop and ENC28J60 may either provide nonsense response to host controller via SPI (fail to read registers properly) or it may enter to some strange state in the worst case. There are several options how to resolve it:
20  * Power ESP32 board from `USB 3.0`, if board is used as source of power to ENC board.
21  * Power ESP32 board from external 5V power supply with current limit at least 1 A, if board is used as source of power to ENC board.
22  * Power ENC28J60 from external 3.3V power supply with common GND to ESP32 board. Note that there might be some ENC28J60 boards with integrated voltage regulator on market and so powered by 5 V. Please consult documentation of your board for details.
23
24  If a ESP32 board is used as source of power to ENC board, ensure that that particular board is assembled with voltage regulator capable to deliver current up to 1 A. This is a case of ESP32 DevKitC or ESP-WROVER-KIT, for example. Such setup was tested and works as expected. Other boards may use different voltage regulators and may perform differently.
25  **WARNING:** Always consult documentation/schematics associated with particular ENC28J60 and ESP32 boards used in your use-case first.
26
27#### Pin Assignment
28
29* ENC28J60 Ethernet module consumes one SPI interface plus an interrupt GPIO. By default they're connected as follows:
30
31| GPIO   | ENC28J60    |
32| ------ | ----------- |
33| GPIO19 | SPI_CLK     |
34| GPIO23 | SPI_MOSI    |
35| GPIO25 | SPI_MISO    |
36| GPIO22 | SPI_CS      |
37| GPIO4  | Interrupt   |
38
39### Configure the project
40
41```
42idf.py menuconfig
43```
44
45In the `Example Configuration` menu, set SPI specific configuration, such as SPI host number, GPIO used for MISO/MOSI/CS signal, GPIO for interrupt event and the SPI clock rate, duplex mode.
46
47**Note:** According to ENC28J60 data sheet and our internal testing, SPI clock could reach up to 20MHz, but in practice, the clock speed may depend on your PCB layout/wiring/power source. In this example, the default clock rate is set to 8 MHz since some ENC28J60 silicon revisions may not properly work at frequencies less than 8 MHz.
48
49### Build, Flash, and Run
50
51Build the project and flash it to the board, then run monitor tool to view serial output:
52
53```
54idf.py -p PORT build flash monitor
55```
56
57(Replace PORT with the name of the serial port to use.)
58
59(To exit the serial monitor, type ``Ctrl-]``.)
60
61See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
62
63## Example Output
64
65```bash
66I (0) cpu_start: Starting scheduler on APP CPU.
67I (401) enc28j60: revision: 6
68I (411) esp_eth.netif.glue: 00:04:a3:12:34:56
69I (411) esp_eth.netif.glue: ethernet attached to netif
70I (421) eth_example: Ethernet Started
71I (2421) enc28j60: working in 10Mbps
72I (2421) enc28j60: working in half duplex
73I (2421) eth_example: Ethernet Link Up
74I (2421) eth_example: Ethernet HW Addr 00:04:a3:12:34:56
75I (4391) esp_netif_handlers: eth ip: 192.168.2.34, mask: 255.255.255.0, gw: 192.168.2.2
76I (4391) eth_example: Ethernet Got IP Address
77I (4391) eth_example: ~~~~~~~~~~~
78I (4391) eth_example: ETHIP:192.168.2.34
79I (4401) eth_example: ETHMASK:255.255.255.0
80I (4401) eth_example: ETHGW:192.168.2.2
81I (4411) eth_example: ~~~~~~~~~~~
82```
83
84Now you can ping your ESP32 in the terminal by entering `ping 192.168.2.34` (it depends on the actual IP address you get).
85
86**Notes:**
871. ENC28J60 hasn't burned any valid MAC address in the chip, you need to write an unique MAC address into its internal MAC address register before any traffic happened on TX and RX line.
882. It is recommended to operate the ENC28J60 in full-duplex mode since various errata exist to the half-duplex mode (even though addressed in the example) and due to its poor performance in the half-duplex mode (especially in TCP connections). However, ENC28J60 does not support automatic duplex negotiation. If it is connected to an automatic duplex negotiation enabled network switch or Ethernet controller, then ENC28J60 will be detected as a half-duplex device. To communicate in Full-Duplex mode, ENC28J60 and the remote node (switch, router or Ethernet controller) **must be manually configured for full-duplex operation**:
89   * The ENC28J60 can be set to full-duplex in the `Example Configuration` menu.
90   * On Ubuntu/Debian Linux distribution use:
91    ```
92    sudo ethtool -s YOUR_INTERFACE_NAME speed 10 duplex full autoneg off
93    ```
94   * On Windows, go to `Network Connections` -> `Change adapter options` -> open `Properties` of selected network card -> `Configure` -> `Advanced` -> `Link Speed & Duplex` -> select `10 Mbps Full Duplex in dropdown menu`.
953. Ensure that your wiring between ESP32 board and the ENC28J60 board is realized by short wires with the same length and no wire crossings.
964. CS Hold Time needs to be configured to be at least 210 ns to properly read MAC and MII registers as defined by ENC28J60 Data Sheet. This is automatically configured in the example based on selected SPI clock frequency by computing amount of SPI bit-cycles the CS should stay active after the transmission. However, if your PCB design/wiring requires different value, please update `cs_ena_posttrans` member of `devcfg` structure per your actual needs.
97
98
99## Troubleshooting
100
101(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
102