Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver/w5500: driver for the W5500 ethernet chip #20301

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions drivers/include/net/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ typedef enum {
NETDEV_ESP_WIFI,
NETDEV_CDC_ECM,
NETDEV_TINYUSB,
NETDEV_W5500,
/* add more if needed */
} netdev_type_t;
/** @} */
Expand Down Expand Up @@ -360,11 +361,11 @@ typedef enum {
* be used by upper layers to store reference information.
*/
struct netdev {
const struct netdev_driver *driver; /**< ptr to that driver's interface. */
netdev_event_cb_t event_callback; /**< callback for device events */
void *context; /**< ptr to network stack context */
const struct netdev_driver *driver; /**< ptr to that driver's interface. */
netdev_event_cb_t event_callback; /**< callback for device events */
void *context; /**< ptr to network stack context */
#ifdef MODULE_NETDEV_LAYER
netdev_t *lower; /**< ptr to the lower netdev layer */
netdev_t *lower; /**< ptr to the lower netdev layer */
#endif
#ifdef MODULE_L2FILTER
l2filter_t filter[CONFIG_L2FILTER_LISTSIZE]; /**< link layer address filters */
Expand Down Expand Up @@ -401,12 +402,12 @@ void netdev_register_signal(struct netdev *dev, netdev_type_t type, uint8_t inde
static inline void netdev_register(struct netdev *dev, netdev_type_t type, uint8_t index)
{
#ifdef MODULE_NETDEV_REGISTER
dev->type = type;
dev->type = type;
dev->index = index;
#else
(void) dev;
(void) type;
(void) index;
(void)dev;
(void)type;
(void)index;
#endif

if (IS_ACTIVE(CONFIG_NETDEV_REGISTER_SIGNAL)) {
Expand Down
90 changes: 90 additions & 0 deletions drivers/include/w5500.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2023 Stefan Schmidt
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @defgroup drivers_w5500 W5500 ethernet driver
* @ingroup drivers_netdev
* @brief Driver for W5500 ethernet devices
*
* This device driver only exposes the MACRAW mode of W5500 devices, so it does
* not offer any support for the on-chip IPv4, UDP, and TCP capabilities of
* these chips. In connection with RIOT we are only interested in the RAW
* Ethernet packets, which we can use through netdev with any software network
* stack provided by RIOT (e.g. GNRC). This enables W5500 devices to communicate
* via IPv6, enables unlimited connections, and more...
*
* @note This driver can be used in polling or interrupt mode.
* On some shields the interrupt line is not enabled by default,
* you have to close the corresponding solder bridge to make the
* interrupt mode work...
*
* @{
*
* @file
* @brief Interface definition for the W5500 device driver
*
* @author Stefan Schmidt <stemschmidt@gmail.com>
*/

#ifndef W5500_H
#define W5500_H

#include <stdint.h>

#include "periph/spi.h"
#include "periph/gpio.h"
#include "net/netdev.h"
#include "kernel_defines.h"
#include "ztimer.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief W5500 device descriptor
*/
typedef struct {
spi_t spi; /**< SPI bus used */
spi_clk_t clk; /**< clock speed used on the selected SPI bus */
gpio_t cs; /**< pin connected to the chip select line */
gpio_t irq; /**< pin connected to the INT line */
uint32_t polling_interval_ms; /**< interval for polling, 0 if interrupt mode */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered doing a compile-time check, so we don't even need to compile the polling code in if we have a IRQ pin connected?

Suggested change
uint32_t polling_interval_ms; /**< interval for polling, 0 if interrupt mode */
#if W5500_PARAM_EVT == GPIO_UNDEF
uint32_t polling_interval_ms; /**< interval for polling, 0 if interrupt mode */
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The w5500.h header is independent of the configuration, it does not include w5500_params.h. So I do not have the information W5500_PARAM_IRQ == GPIO_UNDEF available in the header

} w5500_params_t;

/**
* @brief Device descriptor for W5500 devices
*/
typedef struct w5500 {
netdev_t netdev; /**< extends the netdev structure */
w5500_params_t p; /**< device configuration parameters */
uint16_t frame_size; /**< size of the frame which has been send */
bool link_up; /**< used to prevent sending the same LINK event twice */
bool frame_sent; /**< indicates that the frame has been transmitted */
ztimer_t timerInstance; /**< stores the polling interval timer in polling mode */
} w5500_t;

/**
* @brief So the initial device setup
*
* This function pre-initializes the netdev structure, saves the configuration
* parameters and finally initializes the SPI bus and the used GPIO pins.
*
* @param [out] dev the handle of the device to initialize
* @param [in] params parameters for device initialization
* @param [in] index Index of @p params in a global parameter struct array.
* If initialized manually, pass a unique identifier instead.
*/
void w5500_setup(w5500_t *dev, const w5500_params_t *params, uint8_t index);

#ifdef __cplusplus
}
#endif

#endif /* W5500_H */
/** @} */
52 changes: 52 additions & 0 deletions drivers/w5500/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2023 Stefan Schmidt
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#

menuconfig MODULE_W5500
bool "W5500 Ethernet Adapter"
depends on HAS_PERIPH_GPIO
depends on HAS_PERIPH_GPIO_IRQ
depends on HAS_PERIPH_SPI
depends on TEST_KCONFIG
select MODULE_LUID
select MODULE_NETDEV_ETH
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
select MODULE_PERIPH_SPI

config HAVE_W5500
bool
select MODULE_W5500 if MODULE_NETDEV_DEFAULT
help
Indicates that a w5500 ethernet adapter is present.

if MODULE_W5500

config W5500_USE_POLLING
bool "Use driver in polling mode"
default y

if W5500_USE_POLLING

config W5500_POLLING_INTERVAL
int "Polling interval in ms"
default 100

#endif # W5500_USE_POLLING

config W5500_MAC_FILTER
bool "Enable hardware MAC filter"
default n

config W5500_BROADCAST_FILTER
bool "Enable hardware broadcast filter"
default n

config W5500_MULTICAST_FILTER
bool "Enable hardware multicast filter"
default n

endif # MODULE_W5500
1 change: 1 addition & 0 deletions drivers/w5500/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
6 changes: 6 additions & 0 deletions drivers/w5500/Makefile.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FEATURES_REQUIRED += periph_gpio_irq
FEATURES_REQUIRED += periph_spi
USEMODULE += luid
USEMODULE += iolist
USEMODULE += netdev_eth
USEMODULE += ztimer_msec
2 changes: 2 additions & 0 deletions drivers/w5500/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
USEMODULE_INCLUDES_w5500 := $(LAST_MAKEFILEDIR)/include
USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_w5500)
73 changes: 73 additions & 0 deletions drivers/w5500/include/w5500_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2023 Stefan Schmidt
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup drivers_w5500
* @{
*
* @file
* @brief Default parameters for W5500 Ethernet devices
*
* @author Stefan Schmidt <stemschmidt@gmail.com>
*/

#ifndef W5500_PARAMS_H
#define W5500_PARAMS_H

#include "board.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Default configuration parameters for the W5500 driver
* @{
*/
#ifndef W5500_PARAM_SPI
#define W5500_PARAM_SPI (SPI_DEV(0)) /**< Default SPI device */
#endif
#ifndef W5500_PARAM_SPI_CLK
#define W5500_PARAM_SPI_CLK (SPI_CLK_10MHZ) /**< Default SPI speed */
#endif
#ifndef W5500_PARAM_CS
#define W5500_PARAM_CS (GPIO_PIN(0, 27)) /**< Default SPI chip select pin */
#endif
#ifndef W5500_PARAM_INT
#define W5500_PARAM_INT GPIO_UNDEF /**< set to invalid */
#endif
#ifndef CONFIG_W5500_POLLING_INTERVAL
#define CONFIG_W5500_POLLING_INTERVAL 100u /**< default polling interval 100 ms */
#endif

#ifndef W5500_PARAMS
/**
* @brief W5500 initialization parameters
*/

#define W5500_PARAMS { .spi = W5500_PARAM_SPI, \
.clk = W5500_PARAM_SPI_CLK, \
.cs = W5500_PARAM_CS, \
.irq = W5500_PARAM_INT, \
.polling_interval_ms = CONFIG_W5500_POLLING_INTERVAL }
#endif
/** @} */

/**
* @brief W5500 configuration
*/
static const w5500_params_t w5500_params[] = {
W5500_PARAMS
};

#ifdef __cplusplus
}
#endif

#endif /* W5500_PARAMS_H */
/** @} */
Loading
Loading