This is a sample code for using W5500 EVB as UDP. Differed from TCP, UDP does not require handshake or any confirmation of connection or disconnection. In addition, UDP does not check each datagram packet. Although there is no assurance of no missing data, UDP allows rapid data transmission in return. Livestream of video, radio and music are good example of UDP.
Besides, the definition of UDP Client and UDP Server are unclear normally due to the fact of no handshake required, unless special functions of UDP are used such as Broadcasting and Multicasting, which would be discussed in other pages.
Before burning the bin file into your EVB:
-
-
- Connect W5500 EVB to the router/NAT that you are using, or any other device that allows you to connect to internet
- Manually change the Network configuration:
- Go to the definition of function set_default() (Should be inside device.c), make sure the subnet mask, local IP address, gateway and DNS are set according to the internet setting you are using with your router/NAT
- Change the remote_ip, remote_port and local_port according to your terminal’s network setting. The remote_ip should be exactly the same as your terminal’s.
- Rebuild the sample code and burn it into W5500EVB
- Turn off the firewall if necessary
-
Code Explanation
Basically the main loop is divided into two states using Switch Statement.
int main(void) {
After boot or reboot W5500EVB, all necessary configurations are initialized.
uint8 remote_ip[4]={192,168,1,151}; uint16 remote_port=6000; uint16 local_port=5000; uint16 len=0; Systick_Init(72); GPIO_Configuration(); WIZ_SPI_Init(); USART1_Init(); at24c16_init(); Reset_W5500(); set_default(); set_network(); printf("UDP Local Port: %d\r\n",local_port); printf("UDP Remote IP: %d.%d.%d.%d \r\n",remote_ip[0],remote_ip[1],remote_ip[2],remote_ip[3]); printf("UDP Remote Port: %d \r\n",remote_port); printf("W5500 Init Complete!\r\n"); printf("Start UDP Test!\r\n");
/****************** + Remote IP is the IP of your terminal + Remote port is the port of terminal's socket + Local port is the port for W5500EVB These are all the necessary initialization These are messages shown in serial window for debugging ********************/
Socket UDP
Socket is opened for UDP communication. The flow of communication of W5500EVB would be:
while(1) { switch(getSn_SR(0)) {
case SOCK_UDP: Delay_ms(100); if(getSn_IR(0) & Sn_IR_RECV) { setSn_IR(0, Sn_IR_RECV); } if((len=getSn_RX_RSR(0))>0) { memset(buffer,0,len+1); recvfrom(0,buffer, len, remote_ip,&remote_port); printf("%s\r\n",buffer); sendto(0,buffer,len, remote_ip, remote_port); } break;
/**************** 1. Get the interrupt register status to make sure data is received 2. Check if the register for received data is empty. If it is, the data in the register of W5500 would be copied and be stored in buffer of STM32 MCU(the variable “buffer” is set as a global variable. 3. It sends the same datagram packet back to terminal as feedback 4. Clear the buffer ****************/
Then one transmission ends and waits for the next one
Socket Closed
Socket was closed and open again for next connection
case SOCK_CLOSED: socket(0,Sn_MR_UDP,local_port,0); break; } } }
/**************** Socket was closed and open again for next connection *****************/
Remarks
For multiple sockets, the main loop is structured in the same way as one socket. The only difference is that pulling method is used to function all sockets, therefore delay for sending message is significantly noticeable. The sample code are only for demonstration, users are free to use any other method to do open multiple sockets(interrupt for example). For the sample code of multiple sockets, only two sockets are allowed by default. Users are free to change it if necessary.