no-OS
Loading...
Searching...
No Matches
w5500.c File Reference

Implementation of W5500 Driver. More...

#include "w5500.h"
#include <errno.h>
#include <string.h>
#include "no_os_alloc.h"
#include "no_os_delay.h"
Include dependency graph for w5500.c:

Functions

int w5500_reg_write (struct w5500_dev *dev, uint8_t block, uint16_t addr, const uint8_t *data, uint16_t len)
 Write data to a W5500 register.
 
int w5500_reg_read (struct w5500_dev *dev, uint8_t block, uint16_t addr, uint8_t *data, uint16_t len)
 Read data from a W5500 register.
 
int w5500_read_16bit_reg (struct w5500_dev *dev, uint8_t block, uint16_t addr, uint16_t *value)
 Read a 16-bit register reliably by reading until consecutive values match.
 
int w5500_set_mac (struct w5500_dev *dev, const uint8_t mac[6])
 Set MAC address.
 
int w5500_set_ip (struct w5500_dev *dev, const uint8_t ip[4])
 Set IP address.
 
int w5500_set_subnet (struct w5500_dev *dev, const uint8_t subnet[4])
 Set subnet mask.
 
int w5500_set_gateway (struct w5500_dev *dev, const uint8_t gateway[4])
 Set gateway address.
 
int w5500_get_mac (struct w5500_dev *dev, uint8_t mac[6])
 Get MAC address.
 
int w5500_get_ip (struct w5500_dev *dev, uint8_t ip[4])
 Get IP address.
 
int w5500_get_subnet (struct w5500_dev *dev, uint8_t subnet[4])
 Get subnet mask.
 
int w5500_get_gateway (struct w5500_dev *dev, uint8_t gateway[4])
 Get gateway address.
 
int w5500_check_link_status (struct w5500_dev *dev)
 Check the Ethernet link status.
 
int w5500_tcp_config (struct w5500_dev *dev, uint16_t retry_time, uint8_t retry_count)
 Configure W5500 TCP parameters (RTR and RCR)
 
int w5500_socket_read_status (struct w5500_dev *dev, uint8_t sock_id, uint8_t *status)
 Read status of a socket.
 
int w5500_socket_command_write (struct w5500_dev *dev, uint8_t sock_id, uint8_t val)
 Write a command to a socket's command register.
 
int w5500_socket_clear_interrupt (struct w5500_dev *dev, uint8_t sock_id, uint8_t flags)
 Clear specific socket interrupt flags.
 
int w5500_socket_init (struct w5500_dev *dev, uint8_t sock_id)
 Initialize a socket.
 
int w5500_socket_open (struct w5500_dev *dev, uint8_t sock_id, uint8_t protocol, uint8_t buff_size)
 Open a socket with specified protocol.
 
int w5500_socket_close (struct w5500_dev *dev, uint8_t sock_id)
 Close a socket.
 
int w5500_socket_connect (struct w5500_dev *dev, uint8_t sock_id, struct w5500_socket_address *addr)
 Connect to a remote host (TCP client mode)
 
int w5500_socket_disconnect (struct w5500_dev *dev, uint8_t sock_id)
 Disconnect a TCP connection.
 
int w5500_socket_send (struct w5500_dev *dev, uint8_t sock_id, const void *buf, uint16_t len)
 Send data through a socket.
 
int w5500_socket_recv (struct w5500_dev *dev, uint8_t sock_id, void *buf, uint16_t len)
 Receive data from a socket.
 
int w5500_socket_sendto (struct w5500_dev *dev, uint8_t sock_id, const void *buf, uint16_t len, struct w5500_socket_address *to)
 Send data to a specific destination (UDP)
 
int w5500_socket_recvfrom (struct w5500_dev *dev, uint8_t sock_id, void *buf, uint16_t len, struct w5500_socket_address *from)
 Receive data from a socket with source address information (UDP)
 
int w5500_socket_bind (struct w5500_dev *dev, uint8_t sock_id, uint16_t port)
 Bind a socket to a specific port (server mode)
 
int w5500_socket_listen (struct w5500_dev *dev, uint8_t sock_id)
 Set socket to listen mode (TCP server)
 
int w5500_reset (struct w5500_dev *dev)
 Reset the W5500 chip using hardware or software method.
 
int w5500_setup (struct w5500_dev *dev)
 Setup the W5500 chip with basic configuration.
 
int w5500_init (struct w5500_dev **device, struct w5500_init_param *init_param)
 Initialize the device.
 
int w5500_remove (struct w5500_dev *dev)
 Free a device descriptor and release resources.
 

Detailed Description

Implementation of W5500 Driver.

Author
Alisa-Dariana Roman (alisa.nosp@m..rom.nosp@m.an@an.nosp@m.alog.nosp@m..com)

Copyright 2025(c) Analog Devices, Inc.

SPDX-License-Identifier: BSD-3-Clause

Function Documentation

◆ w5500_check_link_status()

int w5500_check_link_status ( struct w5500_dev * dev)

Check the Ethernet link status.

Parameters
dev- The device descriptor
Returns
0 if link is up, -ENETDOWN if link is down, other negative codes on error

◆ w5500_get_gateway()

int w5500_get_gateway ( struct w5500_dev * dev,
uint8_t gateway[4] )

Get gateway address.

Parameters
dev- The device descriptor
gateway- Buffer to store the 4-byte gateway address
Returns
0 in case of success, negative error code otherwise

◆ w5500_get_ip()

int w5500_get_ip ( struct w5500_dev * dev,
uint8_t ip[4] )

Get IP address.

Parameters
dev- The device descriptor
ip- Buffer to store the 4-byte IP address
Returns
0 in case of success, negative error code otherwise

◆ w5500_get_mac()

int w5500_get_mac ( struct w5500_dev * dev,
uint8_t mac[6] )

Get MAC address.

Parameters
dev- The device descriptor
mac- Buffer to store the 6-byte MAC address
Returns
0 in case of success, negative error code otherwise

◆ w5500_get_subnet()

int w5500_get_subnet ( struct w5500_dev * dev,
uint8_t subnet[4] )

Get subnet mask.

Parameters
dev- The device descriptor
subnet- Buffer to store the 4-byte subnet mask
Returns
0 in case of success, negative error code otherwise

◆ w5500_init()

int w5500_init ( struct w5500_dev ** device,
struct w5500_init_param * init_param )

Initialize the device.

Parameters
device- Pointer to the device descriptor to be initialized
init_param- The device's initialization parameters
Returns
0 in case of success, negative error code otherwise

◆ w5500_read_16bit_reg()

int w5500_read_16bit_reg ( struct w5500_dev * dev,
uint8_t block,
uint16_t addr,
uint16_t * value )

Read a 16-bit register reliably by reading until consecutive values match.

Parameters
dev- The device descriptor
block- The block select bits (common registers, socket registers, etc.)
addr- The register address
value- Pointer to store the 16-bit value
Returns
0 in case of success, negative error code otherwise

◆ w5500_reg_read()

int w5500_reg_read ( struct w5500_dev * dev,
uint8_t block,
uint16_t addr,
uint8_t * data,
uint16_t len )

Read data from a W5500 register.

Parameters
dev- The device descriptor
block- The block select bits (common registers, socket registers, etc.)
addr- The register address
data- The buffer to store read data
len- The data length to read
Returns
0 in case of success, negative error code otherwise

◆ w5500_reg_write()

int w5500_reg_write ( struct w5500_dev * dev,
uint8_t block,
uint16_t addr,
const uint8_t * data,
uint16_t len )

Write data to a W5500 register.

Parameters
dev- The device descriptor
block- The block select bits (common registers, socket registers, etc.)
addr- The register address
data- The data buffer to write
len- The data length
Returns
0 in case of success, negative error code otherwise

◆ w5500_remove()

int w5500_remove ( struct w5500_dev * dev)

Free a device descriptor and release resources.

Parameters
dev- The device descriptor to be removed
Returns
0 in case of success, negative error code otherwise

◆ w5500_reset()

int w5500_reset ( struct w5500_dev * dev)

Reset the W5500 chip using hardware or software method.

Parameters
dev- The device descriptor
Returns
0 in case of success, negative error code otherwise

◆ w5500_set_gateway()

int w5500_set_gateway ( struct w5500_dev * dev,
const uint8_t gateway[4] )

Set gateway address.

Parameters
dev- The device descriptor
gateway- 4-byte gateway address array
Returns
0 in case of success, negative error code otherwise

◆ w5500_set_ip()

int w5500_set_ip ( struct w5500_dev * dev,
const uint8_t ip[4] )

Set IP address.

Parameters
dev- The device descriptor
ip- 4-byte IP address array
Returns
0 in case of success, negative error code otherwise

◆ w5500_set_mac()

int w5500_set_mac ( struct w5500_dev * dev,
const uint8_t mac[6] )

Set MAC address.

Parameters
dev- The device descriptor
mac- 6-byte MAC address array
Returns
0 in case of success, negative error code otherwise

◆ w5500_set_subnet()

int w5500_set_subnet ( struct w5500_dev * dev,
const uint8_t subnet[4] )

Set subnet mask.

Parameters
dev- The device descriptor
subnet- 4-byte subnet mask array
Returns
0 in case of success, negative error code otherwise

◆ w5500_setup()

int w5500_setup ( struct w5500_dev * dev)

Setup the W5500 chip with basic configuration.

Parameters
dev- The device descriptor
Returns
0 in case of success, negative error code otherwise

◆ w5500_socket_bind()

int w5500_socket_bind ( struct w5500_dev * dev,
uint8_t sock_id,
uint16_t port )

Bind a socket to a specific port (server mode)

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
port- Local port number
Returns
0 on success, negative error code otherwise

◆ w5500_socket_clear_interrupt()

int w5500_socket_clear_interrupt ( struct w5500_dev * dev,
uint8_t sock_id,
uint8_t flags )

Clear specific socket interrupt flags.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
flags- Interrupt flags to clear
Returns
0 on success, negative error code otherwise

◆ w5500_socket_close()

int w5500_socket_close ( struct w5500_dev * dev,
uint8_t sock_id )

Close a socket.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
Returns
0 on success, negative error code otherwise

◆ w5500_socket_command_write()

int w5500_socket_command_write ( struct w5500_dev * dev,
uint8_t sock_id,
uint8_t val )

Write a command to a socket's command register.

Parameters
dev- The device descriptor
sock_id- Socket number (0-7)
val- Command value to write
Returns
0 on success, negative error code otherwise

◆ w5500_socket_connect()

int w5500_socket_connect ( struct w5500_dev * dev,
uint8_t sock_id,
struct w5500_socket_address * addr )

Connect to a remote host (TCP client mode)

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
addr- Socket address structure containing remote IP and port
Returns
0 on success, negative error code otherwise

◆ w5500_socket_disconnect()

int w5500_socket_disconnect ( struct w5500_dev * dev,
uint8_t sock_id )

Disconnect a TCP connection.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
Returns
0 on success, -ENOTCONN if not connected, other negative error codes

◆ w5500_socket_init()

int w5500_socket_init ( struct w5500_dev * dev,
uint8_t sock_id )

Initialize a socket.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
Returns
0 on success, negative error code otherwise

◆ w5500_socket_listen()

int w5500_socket_listen ( struct w5500_dev * dev,
uint8_t sock_id )

Set socket to listen mode (TCP server)

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
Returns
0 on success, negative error code otherwise

◆ w5500_socket_open()

int w5500_socket_open ( struct w5500_dev * dev,
uint8_t sock_id,
uint8_t protocol,
uint8_t buff_size )

Open a socket with specified protocol.

Parameters
dev- The device descriptor
sock_id- Socket ID to use (0-7)
protocol- W5500 mode register protocol
buff_size- Buffer size (1, 2, 4, 8, 16 KB)
Returns
0 on success, negative error code otherwise

◆ w5500_socket_read_status()

int w5500_socket_read_status ( struct w5500_dev * dev,
uint8_t sock_id,
uint8_t * status )

Read status of a socket.

Parameters
dev- The device descriptor
sock_id- Socket number (0-7)
status- Variable to store status
Returns
0 on success, negative error code otherwise

◆ w5500_socket_recv()

int w5500_socket_recv ( struct w5500_dev * dev,
uint8_t sock_id,
void * buf,
uint16_t len )

Receive data from a socket.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
buf- Buffer to store received data
len- Maximum length to receive
Returns
Number of bytes received on success, negative error code otherwise

◆ w5500_socket_recvfrom()

int w5500_socket_recvfrom ( struct w5500_dev * dev,
uint8_t sock_id,
void * buf,
uint16_t len,
struct w5500_socket_address * from )

Receive data from a socket with source address information (UDP)

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
buf- Buffer to store received data
len- Maximum length to receive
from- Structure to store source address information
Returns
Number of bytes received on success, negative error code otherwise

◆ w5500_socket_send()

int w5500_socket_send ( struct w5500_dev * dev,
uint8_t sock_id,
const void * buf,
uint16_t len )

Send data through a socket.

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
buf- Data buffer to send
len- Data length
Returns
Number of bytes sent on success, negative error code otherwise

◆ w5500_socket_sendto()

int w5500_socket_sendto ( struct w5500_dev * dev,
uint8_t sock_id,
const void * buf,
uint16_t len,
struct w5500_socket_address * to )

Send data to a specific destination (UDP)

Parameters
dev- Device descriptor
sock_id- Socket number (0-7)
buf- Data buffer to send
len- Data length
to- Destination address (IP and port)
Returns
0 on success, negative error code otherwise

◆ w5500_tcp_config()

int w5500_tcp_config ( struct w5500_dev * dev,
uint16_t retry_time,
uint8_t retry_count )

Configure W5500 TCP parameters (RTR and RCR)

Parameters
dev- The device descriptor
retry_time- Retry timeout value in 100μs units (default: 2000 = 200ms)
retry_count- Maximum retry count (default: 8)
Returns
0 on success, negative error code otherwise