no-OS
Loading...
Searching...
No Matches
w5500.h
Go to the documentation of this file.
1/***************************************************************************/
33
34#ifndef __W5500_H__
35#define __W5500_H__
36
37#include "no_os_spi.h"
38#include "no_os_gpio.h"
39#include "no_os_util.h"
40
41#define W5500_CHIP_VERSION 0x04
42
43/* Byte manipulation helpers */
44#define W5500_BYTE_HIGH(value) (((value) >> 8) & 0xFF)
45#define W5500_BYTE_LOW(value) ((value) & 0xFF)
46
47/* SPI Frame Structure Masks */
48#define W5500_BSB_MASK NO_OS_GENMASK(7, 3) /* Block Select Bits */
49#define W5500_RWB_MASK NO_OS_BIT(2) /* Read/Write Bit */
50#define W5500_OM_MASK NO_OS_GENMASK(1, 0) /* Operation Mode Bits */
51
52/* Control Byte Construction Helpers */
53#define W5500_BSB(block) no_os_field_prep(W5500_BSB_MASK, block)
54#define W5500_RWB_READ 0x00
55#define W5500_RWB_WRITE NO_OS_BIT(2)
56#define W5500_OM_VDM 0x00 /* Variable Data Length Mode */
57#define W5500_OM_FDM_1 0x01 /* Fixed Data Length Mode 1 byte */
58#define W5500_OM_FDM_2 0x02 /* Fixed Data Length Mode 2 bytes */
59#define W5500_OM_FDM_4 0x03 /* Fixed Data Length Mode 4 bytes */
60
61/* Block Select Bit values */
62#define W5500_COMMON_REG 0x00 /* Common Register Block */
63#define W5500_SOCKET_REG_BLOCK(n) (0x01 + (n) * 4) /* Socket n Register Block */
64#define W5500_SOCKET_TX_BUF_BLOCK(n) (0x02 + (n) * 4) /* Socket n TX Buffer Block */
65#define W5500_SOCKET_RX_BUF_BLOCK(n) (0x03 + (n) * 4) /* Socket n RX Buffer Block */
66
67/* Common Register Offsets */
68#define W5500_MR 0x0000 /* Mode Register */
69#define W5500_GAR 0x0001 /* Gateway Address Register (4 bytes) */
70#define W5500_SUBR 0x0005 /* Subnet Mask Register (4 bytes) */
71#define W5500_SHAR 0x0009 /* Source Hardware Address Register (6 bytes) */
72#define W5500_SIPR 0x000F /* Source IP Address Register (4 bytes) */
73#define W5500_INTLEVEL 0x0013 /* Interrupt Low Level Timer Register (2 bytes) */
74#define W5500_IR 0x0015 /* Interrupt Register */
75#define W5500_IMR 0x0016 /* Interrupt Mask Register */
76#define W5500_SIR 0x0017 /* Socket Interrupt Register */
77#define W5500_SIMR 0x0018 /* Socket Interrupt Mask Register */
78#define W5500_RTR 0x0019 /* Retry Time Register (2 bytes) */
79#define W5500_RCR 0x001B /* Retry Count Register */
80#define W5500_PTIMER 0x001C /* PPP LCP Request Timer Register */
81#define W5500_PMAGIC 0x001D /* PPP LCP Magic Number Register */
82#define W5500_PHAR 0x001E /* PPP Destination MAC Register (6 bytes) */
83#define W5500_PSID 0x0024 /* PPP Session ID Register (2 bytes) */
84#define W5500_PMRU 0x0026 /* PPP Maximum Segment Size (2 bytes) */
85#define W5500_UIPR 0x0028 /* Unreachable IP Address Register (4 bytes) */
86#define W5500_UPORTR 0x002C /* Unreachable Port Register (2 bytes) */
87#define W5500_PHYCFGR 0x002E /* PHY Configuration Register */
88#define W5500_VERSIONR 0x0039 /* Chip Version Register */
89
90/* Mode Register (MR) bit values */
91#define W5500_MR_RST NO_OS_BIT(7) /* Reset */
92#define W5500_MR_WOL NO_OS_BIT(5) /* Wake on LAN */
93#define W5500_MR_PB NO_OS_BIT(4) /* Ping Block */
94#define W5500_MR_PPPOE NO_OS_BIT(3) /* PPPoE Mode */
95#define W5500_MR_FARP NO_OS_BIT(1) /* Force ARP */
96
97/* Socket Register Offsets */
98#define W5500_Sn_MR 0x0000 /* Socket n Mode Register */
99#define W5500_Sn_CR 0x0001 /* Socket n Command Register */
100#define W5500_Sn_IR 0x0002 /* Socket n Interrupt Register */
101#define W5500_Sn_SR 0x0003 /* Socket n Status Register */
102#define W5500_Sn_PORT 0x0004 /* Socket n Source Port Register (2 bytes) */
103#define W5500_Sn_DHAR 0x0006 /* Socket n Destination Hardware Address Register (6 bytes) */
104#define W5500_Sn_DIPR 0x000C /* Socket n Destination IP Address Register (4 bytes) */
105#define W5500_Sn_DPORT 0x0010 /* Socket n Destination Port Register (2 bytes) */
106#define W5500_Sn_MSSR 0x0012 /* Socket n Maximum Segment Size Register (2 bytes) */
107#define W5500_Sn_TOS 0x0015 /* Socket n Type of Service Register */
108#define W5500_Sn_TTL 0x0016 /* Socket n Time to Live Register */
109#define W5500_Sn_RXBUF_SIZE 0x001E /* Socket n RX Buffer Size Register */
110#define W5500_Sn_TXBUF_SIZE 0x001F /* Socket n TX Buffer Size Register */
111#define W5500_Sn_TX_FSR 0x0020 /* Socket n TX Free Size Register (2 bytes) */
112#define W5500_Sn_TX_RD 0x0022 /* Socket n TX Read Pointer Register (2 bytes) */
113#define W5500_Sn_TX_WR 0x0024 /* Socket n TX Write Pointer Register (2 bytes) */
114#define W5500_Sn_RX_RSR 0x0026 /* Socket n RX Received Size Register (2 bytes) */
115#define W5500_Sn_RX_RD 0x0028 /* Socket n RX Read Pointer Register (2 bytes) */
116#define W5500_Sn_RX_WR 0x002A /* Socket n RX Write Pointer Register (2 bytes) */
117#define W5500_Sn_IMR 0x002C /* Socket n Interrupt Mask Register */
118#define W5500_Sn_FRAG 0x002D /* Socket n Fragment Offset in IP Header (2 bytes) */
119#define W5500_Sn_KPALVTR 0x002F /* Socket n Keep Alive Timer Register */
120
121/* Socket Mode Register (Sn_MR) values */
122#define W5500_Sn_MR_CLOSE 0x00 /* Closed */
123#define W5500_Sn_MR_TCP 0x01 /* TCP */
124#define W5500_Sn_MR_UDP 0x02 /* UDP */
125#define W5500_Sn_MR_MACRAW 0x04 /* MAC Raw mode */
126#define W5500_Sn_MR_NDMC NO_OS_BIT(5) /* No Delayed ACK */
127#define W5500_Sn_MR_MULTI NO_OS_BIT(7) /* Multicasting */
128#define W5500_Sn_MR_BCASTB NO_OS_BIT(6) /* Broadcast Blocking */
129#define W5500_Sn_MR_UCASTB NO_OS_BIT(4) /* Unicast Blocking (UDP mode) */
130#define W5500_Sn_MR_MIP6B NO_OS_BIT(4) /* IPv6 Blocking (MACRAW mode) */
131
132/* Socket Command Register (Sn_CR) values */
133#define W5500_Sn_CR_OPEN 0x01 /* Initialize and open socket */
134#define W5500_Sn_CR_LISTEN 0x02 /* Wait connection request in TCP mode (Server mode) */
135#define W5500_Sn_CR_CONNECT 0x04 /* Send connection request in TCP mode (Client mode) */
136#define W5500_Sn_CR_DISCON 0x08 /* Send closing request in TCP mode */
137#define W5500_Sn_CR_CLOSE 0x10 /* Close socket */
138#define W5500_Sn_CR_SEND 0x20 /* Send data */
139#define W5500_Sn_CR_SEND_MAC 0x21 /* Send data with MAC address (UDP mode) */
140#define W5500_Sn_CR_SEND_KEEP 0x22 /* Send keep-alive packet (TCP mode) */
141#define W5500_Sn_CR_RECV 0x40 /* Receive data */
142
143/* Socket Status Register (Sn_SR) values */
144#define W5500_Sn_SR_CLOSED 0x00 /* Socket is closed */
145#define W5500_Sn_SR_INIT 0x13 /* Socket is initialized */
146#define W5500_Sn_SR_LISTEN 0x14 /* Socket is in listen state */
147#define W5500_Sn_SR_ESTABLISHED 0x17 /* Connection is established */
148#define W5500_Sn_SR_CLOSE_WAIT 0x1C /* Closing state */
149#define W5500_Sn_SR_UDP 0x22 /* Socket is in UDP mode */
150#define W5500_Sn_SR_MACRAW 0x42 /* Socket is in MACRAW mode */
151#define W5500_Sn_SR_SYNSENT 0x15 /* SYN packet sent (TCP client) */
152#define W5500_Sn_SR_SYNRECV 0x16 /* SYN packet received (TCP server) */
153#define W5500_Sn_SR_FIN_WAIT 0x18 /* Socket closing (FIN sent) */
154#define W5500_Sn_SR_CLOSING 0x1A /* Socket closing (FIN exchanged) */
155#define W5500_Sn_SR_TIME_WAIT 0x1B /* Socket closing (2MSL timer active) */
156#define W5500_Sn_SR_LAST_ACK 0x1D /* Socket closing (Last ACK) */
157
158/* Socket Interrupt Register (Sn_IR) values */
159#define W5500_Sn_IR_SEND_OK NO_OS_BIT(4) /* Send operation completed */
160#define W5500_Sn_IR_TIMEOUT NO_OS_BIT(3) /* Timeout occurred */
161#define W5500_Sn_IR_RECV NO_OS_BIT(2) /* Data received */
162#define W5500_Sn_IR_DISCON NO_OS_BIT(1) /* Connection termination requested/completed */
163#define W5500_Sn_IR_CON NO_OS_BIT(0) /* Connection established */
164
165/* Interrupt Register (IR) values */
166#define W5500_IR_CONFLICT NO_OS_BIT(7) /* IP address conflict */
167#define W5500_IR_UNREACH NO_OS_BIT(6) /* Destination unreachable */
168#define W5500_IR_PPPOE NO_OS_BIT(5) /* PPPoE connection closed */
169#define W5500_IR_MP NO_OS_BIT(4) /* Magic packet received */
170
171/* PHY Configuration Register (PHYCFGR) values */
172#define W5500_PHYCFGR_RST NO_OS_BIT(7) /* PHY Reset */
173#define W5500_PHYCFGR_OPMD NO_OS_BIT(6) /* Operation Mode Select */
174#define W5500_PHYCFGR_OPMDC NO_OS_GENMASK(5, 3) /* Operation Mode Configuration */
175#define W5500_PHYCFGR_DPX NO_OS_BIT(2) /* Duplex Status (1=Full, 0=Half) */
176#define W5500_PHYCFGR_SPD NO_OS_BIT(1) /* Speed Status (1=100Mbps, 0=10Mbps) */
177#define W5500_PHYCFGR_LNK NO_OS_BIT(0) /* Link Status (1=Up, 0=Down) */
178
179/* PHYCFGR Operation Mode Configuration values */
180#define W5500_PHYCFGR_OPMDC_10BT_HD no_os_field_prep(W5500_PHYCFGR_OPMDC, 0)
181/* 10BT Half-duplex, no auto-nego */
182#define W5500_PHYCFGR_OPMDC_10BT_FD no_os_field_prep(W5500_PHYCFGR_OPMDC, 1)
183/* 10BT Full-duplex, no auto-nego */
184#define W5500_PHYCFGR_OPMDC_100BT_HD no_os_field_prep(W5500_PHYCFGR_OPMDC, 2)
185/* 100BT Half-duplex, no auto-nego */
186#define W5500_PHYCFGR_OPMDC_100BT_FD no_os_field_prep(W5500_PHYCFGR_OPMDC, 3)
187/* 100BT Full-duplex, no auto-nego */
188#define W5500_PHYCFGR_OPMDC_100BT_HD_AN no_os_field_prep(W5500_PHYCFGR_OPMDC, 4)
189/* 100BT Half-duplex, with auto-nego */
190#define W5500_PHYCFGR_OPMDC_POWER_DOWN no_os_field_prep(W5500_PHYCFGR_OPMDC, 6)
191/* Power down mode */
192#define W5500_PHYCFGR_OPMDC_ALL_AN no_os_field_prep(W5500_PHYCFGR_OPMDC, 7)
193/* All capable, with auto-nego */
194
195/* Socket Buffer Size values (2^n KB) */
196#define W5500_SOCK_BUF_SIZE_0K 0x00 /* 0KB buffer size */
197#define W5500_SOCK_BUF_SIZE_1K 0x01 /* 1KB buffer size */
198#define W5500_SOCK_BUF_SIZE_2K 0x02 /* 2KB buffer size (default) */
199#define W5500_SOCK_BUF_SIZE_4K 0x04 /* 4KB buffer size */
200#define W5500_SOCK_BUF_SIZE_8K 0x08 /* 8KB buffer size */
201#define W5500_SOCK_BUF_SIZE_16K 0x10 /* 16KB buffer size */
202
203/* Maximum socket number */
204#define W5500_MAX_SOCK_NUMBER 7 /* 8 sockets (0-7) */
205
207 uint8_t ip[4];
208 uint8_t port[2];
209};
210
212 uint8_t protocol;
213 uint16_t mss;
214 uint8_t tx_buf_size;
215 uint8_t rx_buf_size;
216};
217
227
236
238int w5500_reg_write(struct w5500_dev *dev, uint8_t block,
239 uint16_t addr, const uint8_t *data, uint16_t len);
240
242int w5500_reg_read(struct w5500_dev *dev, uint8_t block,
243 uint16_t addr, uint8_t *data, uint16_t len);
244
246int w5500_read_16bit_reg(struct w5500_dev *dev, uint8_t block,
247 uint16_t addr, uint16_t *value);
248
250int w5500_set_mac(struct w5500_dev *dev, const uint8_t mac[6]);
251
253int w5500_set_ip(struct w5500_dev *dev, const uint8_t ip[4]);
254
256int w5500_set_subnet(struct w5500_dev *dev, const uint8_t subnet[4]);
257
259int w5500_set_gateway(struct w5500_dev *dev, const uint8_t gateway[4]);
260
262int w5500_get_mac(struct w5500_dev *dev, uint8_t mac[6]);
263
265int w5500_get_ip(struct w5500_dev *dev, uint8_t ip[4]);
266
268int w5500_get_subnet(struct w5500_dev *dev, uint8_t subnet[4]);
269
271int w5500_get_gateway(struct w5500_dev *dev, uint8_t gateway[4]);
272
274int w5500_check_link_status(struct w5500_dev *dev);
275
277int w5500_tcp_config(struct w5500_dev *dev, uint16_t retry_time,
278 uint8_t retry_count);
279
281int w5500_socket_read_status(struct w5500_dev *dev, uint8_t sock_id,
282 uint8_t *status);
283
285int w5500_socket_command_write(struct w5500_dev *dev, uint8_t sock_id,
286 uint8_t val);
287
289int w5500_socket_clear_interrupt(struct w5500_dev *dev, uint8_t sock_id,
290 uint8_t flags);
291
293int w5500_socket_init(struct w5500_dev *dev, uint8_t sock_id);
294
296int w5500_socket_open(struct w5500_dev *dev, uint8_t sock_id, uint8_t protocol,
297 uint8_t buff_size);
298
300int w5500_socket_close(struct w5500_dev *dev, uint8_t sock_id);
301
303int w5500_socket_connect(struct w5500_dev *dev, uint8_t sock_id,
304 struct w5500_socket_address *addr);
305
307int w5500_socket_disconnect(struct w5500_dev *dev, uint8_t sock_id);
308
310int w5500_socket_send(struct w5500_dev *dev, uint8_t sock_id, const void *buf,
311 uint16_t len);
312
314int w5500_socket_recv(struct w5500_dev *dev, uint8_t sock_id, void *buf,
315 uint16_t len);
316
318int w5500_socket_sendto(struct w5500_dev *dev, uint8_t sock_id, const void *buf,
319 uint16_t len, struct w5500_socket_address *to);
320
322int w5500_socket_recvfrom(struct w5500_dev *dev, uint8_t sock_id,
323 void *buf, uint16_t len,
324 struct w5500_socket_address *from);
325
327int w5500_socket_bind(struct w5500_dev *dev, uint8_t sock_id, uint16_t port);
328
330int w5500_socket_listen(struct w5500_dev *dev, uint8_t sock_id);
331
333int w5500_reset(struct w5500_dev *dev);
334
336int w5500_setup(struct w5500_dev *dev);
337
339int w5500_init(struct w5500_dev **device,
341
343int w5500_remove(struct w5500_dev *dev);
344
345#endif
struct ad7616_init_param init_param
Definition ad7616_sdz.c:107
Header file of GPIO Interface.
Header file of SPI Interface.
Header file of utility functions.
Definition ad9361_util.h:63
Structure holding the GPIO descriptor.
Definition no_os_gpio.h:84
Structure holding the parameters for GPIO initialization.
Definition no_os_gpio.h:67
Structure holding SPI descriptor.
Definition no_os_spi.h:180
Structure holding the parameters for SPI initialization.
Definition no_os_spi.h:128
Definition w5500.h:218
struct w5500_socket sockets[W5500_MAX_SOCK_NUMBER+1]
Definition w5500.h:225
struct no_os_spi_desc * spi
Definition w5500.h:221
uint8_t retry_count
Definition w5500.h:224
uint8_t mac_addr[6]
Definition w5500.h:222
struct no_os_gpio_desc * gpio_reset
Definition w5500.h:219
uint16_t retry_time
Definition w5500.h:223
struct no_os_gpio_desc * gpio_int
Definition w5500.h:220
Definition w5500.h:228
const struct no_os_spi_init_param * spi_init
Definition w5500.h:231
uint16_t retry_time
Definition w5500.h:233
uint8_t mac_addr[6]
Definition w5500.h:232
uint8_t retry_count
Definition w5500.h:234
const struct no_os_gpio_init_param * gpio_reset
Definition w5500.h:229
const struct no_os_gpio_init_param * gpio_int
Definition w5500.h:230
Definition w5500.h:206
uint8_t port[2]
Definition w5500.h:208
uint8_t ip[4]
Definition w5500.h:207
Definition w5500.h:211
uint8_t rx_buf_size
Definition w5500.h:215
uint8_t tx_buf_size
Definition w5500.h:214
uint8_t protocol
Definition w5500.h:212
uint16_t mss
Definition w5500.h:213
int w5500_set_gateway(struct w5500_dev *dev, const uint8_t gateway[4])
Set gateway address.
Definition w5500.c:197
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.
Definition w5500.c:374
int w5500_get_ip(struct w5500_dev *dev, uint8_t ip[4])
Get IP address.
Definition w5500.c:223
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.
Definition w5500.c:84
int w5500_tcp_config(struct w5500_dev *dev, uint16_t retry_time, uint8_t retry_count)
Configure W5500 TCP parameters (RTR and RCR)
Definition w5500.c:285
int w5500_set_ip(struct w5500_dev *dev, const uint8_t ip[4])
Set IP address.
Definition w5500.c:171
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)
Definition w5500.c:759
int w5500_get_mac(struct w5500_dev *dev, uint8_t mac[6])
Get MAC address.
Definition w5500.c:210
int w5500_reset(struct w5500_dev *dev)
Reset the W5500 chip using hardware or software method.
Definition w5500.c:970
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.
Definition w5500.c:52
int w5500_socket_listen(struct w5500_dev *dev, uint8_t sock_id)
Set socket to listen mode (TCP server)
Definition w5500.c:945
int w5500_socket_bind(struct w5500_dev *dev, uint8_t sock_id, uint16_t port)
Bind a socket to a specific port (server mode)
Definition w5500.c:924
int w5500_set_mac(struct w5500_dev *dev, const uint8_t mac[6])
Set MAC address.
Definition w5500.c:158
int w5500_get_gateway(struct w5500_dev *dev, uint8_t gateway[4])
Get gateway address.
Definition w5500.c:249
int w5500_socket_read_status(struct w5500_dev *dev, uint8_t sock_id, uint8_t *status)
Read status of a socket.
Definition w5500.c:359
int w5500_get_subnet(struct w5500_dev *dev, uint8_t subnet[4])
Get subnet mask.
Definition w5500.c:236
int w5500_socket_send(struct w5500_dev *dev, uint8_t sock_id, const void *buf, uint16_t len)
Send data through a socket.
Definition w5500.c:587
int w5500_socket_recv(struct w5500_dev *dev, uint8_t sock_id, void *buf, uint16_t len)
Receive data from a socket.
Definition w5500.c:673
int w5500_socket_init(struct w5500_dev *dev, uint8_t sock_id)
Initialize a socket.
Definition w5500.c:422
int w5500_init(struct w5500_dev **device, struct w5500_init_param *init_param)
Initialize the device.
Definition w5500.c:1048
int w5500_socket_clear_interrupt(struct w5500_dev *dev, uint8_t sock_id, uint8_t flags)
Clear specific socket interrupt flags.
Definition w5500.c:391
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)
Definition w5500.c:520
#define W5500_MAX_SOCK_NUMBER
Definition w5500.h:204
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.
Definition w5500.c:467
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)
Definition w5500.c:839
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.
Definition w5500.c:120
int w5500_remove(struct w5500_dev *dev)
Free a device descriptor and release resources.
Definition w5500.c:1103
int w5500_socket_close(struct w5500_dev *dev, uint8_t sock_id)
Close a socket.
Definition w5500.c:503
int w5500_setup(struct w5500_dev *dev)
Setup the W5500 chip with basic configuration.
Definition w5500.c:1021
int w5500_set_subnet(struct w5500_dev *dev, const uint8_t subnet[4])
Set subnet mask.
Definition w5500.c:184
int w5500_check_link_status(struct w5500_dev *dev)
Check the Ethernet link status.
Definition w5500.c:261
int w5500_socket_disconnect(struct w5500_dev *dev, uint8_t sock_id)
Disconnect a TCP connection.
Definition w5500.c:558