24#include "wizchip_conf.h"
25#include "wizchip_qspi_pio.h"
26#include "wizchip_spi.h"
39#define UDP_MAX_PAYLOAD_BYTES 1472U
40#define UDP_W5500_OVERHEAD_BYTES 8U
41#define UDP_RX_REQUIRED_BYTES (UDP_MAX_PAYLOAD_BYTES + UDP_W5500_OVERHEAD_BYTES)
44 uint64_t* tx_full_enter_events)
46 static uint64_t rx_events = 0;
47 static uint64_t tx_events = 0;
48 static bool rx_was_full =
false;
49 static bool tx_was_full =
false;
51 uint16_t rx_used_bytes = getSn_RX_RSR(
UDP_SOCKET);
52 uint16_t rx_cap_bytes = (uint16_t)getSn_RXBUF_SIZE(
UDP_SOCKET) * 1024U;
54 uint16_t tx_free_bytes = getSn_TX_FSR(
UDP_SOCKET);
55 uint16_t tx_cap_bytes = (uint16_t)getSn_TXBUF_SIZE(
UDP_SOCKET) * 1024U;
60 uint16_t rx_free_bytes =
61 (rx_used_bytes < rx_cap_bytes) ? (uint16_t)(rx_cap_bytes - rx_used_bytes) : (uint16_t)0;
63 bool tx_is_full = (tx_cap_bytes > 0U) && (tx_free_bytes == 0U);
65 if (rx_is_full && !rx_was_full)
69 if (tx_is_full && !tx_was_full)
74 rx_was_full = rx_is_full;
75 tx_was_full = tx_is_full;
77 if (rx_full_enter_events)
79 *rx_full_enter_events = rx_events;
81 if (tx_full_enter_events)
83 *tx_full_enter_events = tx_events;
90 for (
int i = 0; i < 4; i++)
92 bcast[i] = (uint8_t)((ip_addr[i] & mask[i]) | (uint8_t)(~mask[i]));
98 uint8_t phy_cfg = getPHYCFGR();
99 LOG_DEBUG(
"PHY CFG: 0x%02X (Link %s)\r\n", phy_cfg, (phy_cfg & 0x01) ?
"UP" :
"DOWN");
103 LOG_DEBUG(
"Socket Mode: 0x%02X, Status: 0x%02X\r\n", mode, status);
115 LOG_DEBUG(
"sendto() error %ld\r\n", (
long)sent_len);
128 &(send_config->
port));
131 frame->
length = (size_t)recv_len;
132 LOG_DEBUG(
"RX %ld bytes from %u.%u.%u.%u:%u\r\n", (
long)recv_len,
138 int print_len = (recv_len < 16) ? recv_len : 16;
139 for (
int i = 0; i < print_len; i++)
151 int8_t ret = socket(
IP_SOCKET, Sn_MR_IPRAW, 0, 0);
154 LOG_ERROR(
"W5500: socket() failed, ret=%d\r\n", (
int)ret);
157 LOG_DEBUG(
"W5500: IPRAW Socket opened successfully in blocking mode\r\n");
163 LOG_DEBUG(
"Starting UDP echo on port %u...\r\n", config->
port);
168 int8_t ret = socket(
UDP_SOCKET, Sn_MR_UDP, config->
port, SF_IO_NONBLOCK);
171 LOG_ERROR(
"W5500: socket() failed, ret=%d\r\n", (
int)ret);
174 LOG_DEBUG(
"W5500: Socket opened successfully in non blocking mode\r\n");
190 network_initialize(config->
net_info);
191 print_network_information(config->
net_info);
197 uint8_t memsize[2][8] = {
198 {1, 8, 1, 1, 1, 1, 1, 2},
199 {1, 8, 1, 1, 1, 1, 1, 2},
202 if (ctlwizchip(CW_INIT_WIZCHIP, (
void*)memsize) == -1)
204 LOG_ERROR(
"W5500: failed to apply socket memory map\r\n");
221 .dhcp = NETINFO_STATIC};
229 wizchip_spi_initialize();
231 LOG_DEBUG(
"W5500: Init critical section\r\n");
232 wizchip_cris_initialize();
238 wizchip_initialize();
240 LOG_DEBUG(
"W5500: Apply socket memory map\r\n");
void fatal_panic(e2s_error_t reason)
Print error message and panic.
e2s_error_t
Common error codes returned by ether2ser modules.
@ E2S_ERR_W5500_INIT_FAILED
@ E2S_ERR_W5500_SOCKET_OPEN_FAILED
Network configuration wrapper used by the W5500 driver.
uint8_t broadcast_address[4]
UDP endpoint configuration.
static e2s_error_t w5500_apply_socket_mem_map(void)
void w5500_set_network_defaults(NETWORK_CONFIG_T *config)
Fill network config with compile-time defaults.
void w5500_poll_udp_buffer_full_events(uint64_t *rx_full_enter_events, uint64_t *tx_full_enter_events)
void w5500_udp_tx(UDP_CONFIG_T *send_config, const UDP_FRAME_T *frame)
Send one UDP frame through W5500.
void w5500_set_network(NETWORK_CONFIG_T *config)
Apply network configuration to W5500 hardware.
e2s_error_t w5500_open_ipraw_socket(void)
Open IPRAW socket mode.
void w5500_driver_init(void)
Initialize W5500 driver and low-level interface.
e2s_error_t w5500_open_udp_socket(UDP_CONFIG_T *config)
Open configured UDP socket.
bool w5500_poll_rx(UDP_CONFIG_T *send_config, UDP_FRAME_T *frame)
Poll W5500 for received UDP data.
e2s_error_t w5500_reconfigure_udp_socket(UDP_CONFIG_T *config)
Reconfigure UDP socket with new endpoint settings.
#define UDP_RX_REQUIRED_BYTES
void w5500_debug_status(void)
Print W5500 socket/PHY debug status.
static void ipv4_calc_broadcast_u8(const uint8_t ip_addr[4], const uint8_t mask[4], uint8_t bcast[4])
#define DEFAULT_SUBNET_MASK
#define DEFAULT_GATEWAY_ADDR