Enc28j60 Ethernet uIP protocol

Source: Internet
Author: User
Tags define null htons

The smart home gateway and peripheral single-chip microcomputer communication data prepared for the Guangdong Electronic Design Competition need to use TCP/IP communication, so the enc28j60 module is used.

In embedded systems, Ethernet controllers are usually one of the hot topics of research. The enc28j60 of microchip has a high application value in embedded systems. The chip integrates Mac controller and PHY and uses the SPI interface, the Ethernet connection function is suitable for Embedded Systems with tight pin resources. This article mainly introduces the initialization of the enc28j60 controller of microchip and the precautions and key points related to programming.

As a widely used lightweight embedded TCP/IP protocol stack, uip udp protocol implementation is not complete. Currently, only UDP clients are available in the latest version 1.0, UDP server is not yet implemented. To this end, we have made the following improvements: Initialize the UDP Service port, judge and match the port number after receiving the UDP client data packet, and release the destination port after the UDP server sends the message.

Hardware Platform: stc12c60s2 + enc28j60

 

Main function: In the function, you can understand the UIP process for users who process application data. The UIP process contains all the processed results.
All are called back to the uip_appcall () function for unified processing, so all the work to be completed is in the uip_appcall () function.

/** Copyright (c) 2001-2003, Adam dunkels. * All Rights Reserved. ** redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclawing. * 2. redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclawing in the * Documentation and/or other materials provided with the distribution. * 3. the name of the author may not be used to endorse or promote * products derived from this software without specific prior * written permission. ** this software is provided by the author ''as is'' and any express * or implied warranties, including, but not limited, the implied * warranties of merchantability and fitness for a special purpose * are disclaimed. in no event shall the author be liable for any * direct, indirect, incidental, special, exemplary, or consequential * damages (including, but not limited, procurement of substitute * goods or services; loss of use, data, or profits; or business * interruption) however caused and on any theory of liability, * Whether in contract, strict liability, or tort (including * negligence or otherwise) arising in any way out of the use of this * software, even if advised of the possibility of such damage. ** this file is part of the uIP TCP/IP stack. ** $ ID: Main. c, V 1.10.2.1 2003/10/04 22:54:17 Adam exp $ **/# include "UIP. H "# include" uip_arp.h "# include" httpd. H "// # include" Telnet. H "# include" mcu_uart.h "# include" enc28j60. H "# include" tcp_server.h "# define Buf (struct uip_eth_hdr *) & uip_buf [0]) # ifndef null # define null (void *) 0 # endif/* null * // * optional */intmain (void) {idata u8_t I, arptimer; idata u16_t J; init_uart (); // initialize printu ("Starting ...... \ r \ n ");/* initialize the device driver. */dev_init (); // Nic initialization uip_arp_init (); // ARP initialization/* initialize the uIP TCP/IP stack. */uip_init (); // The uIP protocol initializes printu ("UIP \ r \ n");/* initialize the HTTP server. * // httpd_init (); tcp_server_init (); // The arptimer that implements the function is set to 0; // ARP cache table update timer printu ("OK \ r \ n"); While (1) {/* Let the tapdev network device driver read an entire IP packet into the uip_buf. if it must wait for more than 0.5 seconds, it will return with the return value 0. if so, we know that it is time to call upon the uip_periodic (). otherwise, the tapdev has already ed an IP packet that is to be processed by UIP. */uip_len = dev_poll (); // query whether a packet has been received by the NIC (j = 0; j <500; j ++); // delay to avoid slow server response, cause unnecessary resend packets/* If (uip_len> 0) {printuf ("--------------- uip_len = 0x % x", uip_len); printuf ("% x ---------- \ r \ n ", uip_len); for (I = 0; I <uip_len; I ++) {printuf ("% x", uip_buf [I]); If (I + 1) % 16 = 0) printu ("\ r \ n");} printu ("\ r \ n");} */If (uip_len = 0) {for (I = 0; I <uip_conns; I ++) {// protocol stack resolution, group package, status monitoring, and other uip_periodic (I ); /* If the above function invocation resulted in data that shocould be sent out on the network, the global variable uip_len is set to a value> 0. */If (uip_len> 0) {uip_arp_out (); // dev_send () ;}# if uip_udp for (I = 0; I <uip_udp_conns; I ++) {uip_udp_periodic (I);/* If the above function invocation resulted in data that shocould be sent out on the network, the global variable uip_len is set to a value> 0. */If (uip_len> 0) {uip_arp_out (); dev_send () ;}# endif/* uip_udp * // * call the ARP timer function every 10 seconds. */If (++ arptimer = 20) {uip_arp_timer (); // update ARP cache arptimer = 0 ;}} else {// If (BUF-> type = htons (uip_ethtype_ip) {uip_arp_ipin (); uip_input (); /* If the above function invocation resulted in data that shocould be sent out on the network, the global variable uip_len is set to a value> 0. */If (uip_len> 0) {uip_arp_out (); dev_send () ;}} else if (BUF-> type = htons (uip_ethtype_arp )) {// process arpuip_arp_arpin ();/* If the above function invocation resulted in data that shoshould be sent out on the network, the global variable uip_len is set to a value> 0. */If (uip_len> 0) {dev_send () ;}}} return 0;}/* Consumer */voiduip_log (char * m) {// printf ("UIP Log message: % s \ n", m);}/* optional );}/*-----------------------------------------------------------------------------------*/

In tcp_server_init (); // user-defined, implement the function

# Include "example0.h" # include "example1.h" # include "tcp_server.h" // # include "httpd. H "# include" UIP. H "# include" mcu_uart.h "Void tcp_server_init (void) // In application functions, the UIP event detection function is used to determine the processing method, // you can determine the port number of the current connection to process different connections. {Httpd_init (); example0_init (); // User-Defined Function to implement some functions. It mainly listens to the port example1_init (); //} void tcp_server_appcall (void) {Switch (uip_conn-> lport) {Case htons (80): httpd_appcall (); break; Case htons (8000): example0_app (); break; Case htons (8001 ): exampleappsapp (); break ;}}

For example, in example0_init

void example0_init(void) {uip_listen(HTONS(8000));}

 

How can I call a self-compiled function? In uipopt. H, you can modify the IP address, MAC address, and default gateway. Implemented User-Defined Functions

 

#define UIP_PINGADDRCONF 0#define UIP_IPADDR0     192 /**< The first octet of the IP address of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_IPADDR1     168 /**< The second octet of the IP address of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_IPADDR2     0   /**< The third octet of the IP address of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_IPADDR3     44  /**< The fourth octet of the IP address of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_NETMASK0    255 /**< The first octet of the netmask of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_NETMASK1    255 /**< The second octet of the netmask of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_NETMASK2    255 /**< The third octet of the netmask of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_NETMASK3    0   /**< The fourth octet of the netmask of       this uIP node, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_DRIPADDR0   192 /**< The first octet of the IP address of       the default router, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_DRIPADDR1   168 /**< The second octet of the IP address of       the default router, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_DRIPADDR2   0   /**< The third octet of the IP address of       the default router, if UIP_FIXEDADDR is       1. \hideinitializer */#define UIP_DRIPADDR3   1   /**< The fourth octet of the IP address of       the default router, if UIP_FIXEDADDR is       1. \hideinitializer *//** * Specifies if the uIP ARP module should be compiled with a fixed * Ethernet MAC address or not. * * If this configuration option is 0, the macro uip_setethaddr() can * be used to specify the Ethernet address at run-time. * * \hideinitializer */#define UIP_FIXEDETHADDR 1#define UIP_ETHADDR0    0x12  /**< The first octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */#define UIP_ETHADDR1    0x34  /**< The second octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */#define UIP_ETHADDR2    0x56  /**< The third octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */#define UIP_ETHADDR3    0x78  /**< The fourth octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */#define UIP_ETHADDR4    0x90  /**< The fifth octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */#define UIP_ETHADDR5    0xAB  /**< The sixth octet of the Ethernet address if UIP_FIXEDETHADDR is 1. \hideinitializer */
/* To mount your application to a UIP, you must define macro uip_appcall () as the actual application function name. In this way, when a UIP event occurs, the kernel will call this application for processing */# define uip_appcall tcp_server_appcall // httpd_appcall /*

Finally, how can we conduct the test: Use the TCP/UDP debugging tool to download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.