drfhaust/eth-w5500

1.0.0

Latest
uploaded 3 hours ago
W5500 Ethernet driver for ESP-IDF with simple API (pure ESP-IDF, no Arduino)

readme

# W5500 Ethernet Component for ESP-IDF

Simple W5500 Ethernet driver for ESP-IDF. Pure C, no Arduino dependencies.

## Features

- **Simple API**: Easy-to-use functions for initialization and status
- **Struct-based config**: No Kconfig needed
- **External SPI bus support**: Share SPI with other devices
- **DHCP and static IP**: Flexible network configuration
- **Status callbacks**: Get notified on link/IP changes
- **Polling mode**: Works without interrupt pin

## Installation

```bash
idf.py add-dependency "drfhaust/eth-w5500"
```

## Quick Start

### Basic Usage (DHCP)

```c
#include "eth_w5500.h"

void app_main(void)
{
    eth_w5500_config_t cfg = ETH_W5500_DEFAULT_CONFIG();
    cfg.spi_mosi = 23;
    cfg.spi_miso = 19;
    cfg.spi_clk = 18;
    cfg.spi_cs = 5;

    eth_w5500_init(&cfg);
    eth_w5500_start();

    // Wait for IP (10 second timeout)
    if (eth_w5500_wait_for_ip(10000)) {
        printf("Connected! IP: %s\n", eth_w5500_ip_to_str(eth_w5500_get_ip()));
    }
}
```

### External SPI Bus

```c
#include "eth_w5500.h"

void app_main(void)
{
    // Your existing SPI bus
    spi_bus_config_t bus_cfg = { /* ... */ };
    spi_bus_initialize(SPI2_HOST, &bus_cfg, SPI_DMA_CH_AUTO);

    // Use external bus
    eth_w5500_config_t cfg = ETH_W5500_EXTERNAL_SPI(SPI2_HOST, GPIO_NUM_5);
    eth_w5500_init(&cfg);
    eth_w5500_start();
}
```

### Static IP

```c
#include "eth_w5500.h"

void app_main(void)
{
    eth_w5500_config_t cfg = ETH_W5500_DEFAULT_CONFIG();
    cfg.use_dhcp = false;
    cfg.static_ip = ETH_W5500_IP4(192, 168, 1, 100);
    cfg.static_gateway = ETH_W5500_IP4(192, 168, 1, 1);
    cfg.static_netmask = ETH_W5500_IP4(255, 255, 255, 0);
    cfg.static_dns = ETH_W5500_IP4(8, 8, 8, 8);

    eth_w5500_init(&cfg);
    eth_w5500_start();
}
```

### Status Callbacks

```c
#include "eth_w5500.h"

void on_eth_status(eth_w5500_status_t status, void *ctx)
{
    switch (status) {
        case ETH_W5500_STATUS_GOT_IP:
            printf("Connected!\n");
            break;
        case ETH_W5500_STATUS_LINK_UP:
            printf("Link up, waiting for IP...\n");
            break;
        case ETH_W5500_STATUS_STARTED:
            printf("No link\n");
            break;
    }
}

void app_main(void)
{
    eth_w5500_on_status_change(on_eth_status, NULL);

    eth_w5500_config_t cfg = ETH_W5500_DEFAULT_CONFIG();
    eth_w5500_init(&cfg);
    eth_w5500_start();
}
```

## Configuration

| Field | Description | Default |
|-------|-------------|---------|
| `spi_mosi` | MOSI pin (-1 for external bus) | 23 |
| `spi_miso` | MISO pin (-1 for external bus) | 19 |
| `spi_clk` | CLK pin (-1 for external bus) | 18 |
| `spi_cs` | CS pin (required) | 5 |
| `spi_host` | SPI host (-1 = SPI2_HOST) | -1 |
| `spi_clock_mhz` | SPI clock (8-25 MHz) | 25 |
| `int_gpio` | Interrupt pin (-1 = polling) | -1 |
| `reset_gpio` | Reset pin (-1 = none) | -1 |
| `mac_addr` | Custom MAC (NULL = auto) | NULL |
| `hostname` | Hostname | "esp32-w5500" |
| `use_dhcp` | Use DHCP | true |

## API Reference

### Initialization

```c
esp_err_t eth_w5500_init(const eth_w5500_config_t *config);
void eth_w5500_deinit(void);
esp_err_t eth_w5500_start(void);
esp_err_t eth_w5500_stop(void);
```

### Status

```c
eth_w5500_status_t eth_w5500_get_status(void);
bool eth_w5500_link_up(void);
bool eth_w5500_has_ip(void);
bool eth_w5500_is_connected(void);  // link + IP
void eth_w5500_get_info(eth_w5500_info_t *info);
uint32_t eth_w5500_get_ip(void);
esp_netif_t *eth_w5500_get_netif(void);
```

### Network Control

```c
esp_err_t eth_w5500_dhcp_start(void);
esp_err_t eth_w5500_dhcp_stop(void);
esp_err_t eth_w5500_set_static_ip(uint32_t ip, uint32_t gw, uint32_t mask, uint32_t dns);
```

### Callbacks

```c
void eth_w5500_on_status_change(eth_w5500_callback_t callback, void *ctx);
```

### Utility

```c
bool eth_w5500_wait_for_ip(uint32_t timeout_ms);
const char *eth_w5500_ip_to_str(uint32_t ip);
#define ETH_W5500_IP4(a, b, c, d)  // Make IP from octets
```

## Status Codes

| Status | Description |
|--------|-------------|
| `ETH_W5500_STATUS_STOPPED` | Driver not running |
| `ETH_W5500_STATUS_STARTED` | Started, no link |
| `ETH_W5500_STATUS_LINK_UP` | Link up, waiting for IP |
| `ETH_W5500_STATUS_GOT_IP` | Connected with IP |
| `ETH_W5500_STATUS_ERROR` | Error state |

## Wiring

| W5500 Pin | ESP32 Pin (Default) |
|-----------|---------------------|
| MOSI | GPIO 23 |
| MISO | GPIO 19 |
| SCLK | GPIO 18 |
| CS | GPIO 5 |
| INT | Not required (polling) |
| RST | Not required |

## License

MIT License - Olaifa Oluwadara Daniel

Links

Maintainer

  • Olaifa Oluwadara Daniel <akolaifa4@gmail.com>

License: MIT

To add this component to your project, run:

idf.py add-dependency "drfhaust/eth-w5500^1.0.0"

download archive

Stats

  • Archive size
    Archive size ~ 40.47 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

drfhaust/eth-w5500 version: 1.0.0
|