SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2013, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  *  \page gmac_uip_telnetd GMAC Telnetd Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This project implements a telnet server example of the uIP TCP/IP stack.
00036  *  It enables the device to act as a simple telnetd server.
00037  *
00038  *  \section Requirements
00039  *
00040  * This package can be used with SAM V71 Xplained Ultra board.
00041  *
00042  *  \section Description
00043  *
00044  * Please refer to the uIP documentation for more information
00045  * about the TCP/IP stack, the telnetd example.
00046  *
00047  * By default, the example does not use DHCP.
00048  * If you want to use DHCP, please:
00049  * - Open file uip-conf.h and don't comment the line "#define UIP_DHCP_on".
00050  * - Include uip/apps/dhcps to compile.
00051  *
00052  *  \section Usage
00053  *
00054  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. Please
00055  *     refer to the Getting Started with SAM V71 Microcontrollers.pdf
00056  *  -# On the computer, open and configure a terminal application
00057  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00058  *    - 115200 bauds
00059  *    - 8 bits of data
00060  *    - No parity
00061  *    - 1 stop bit
00062  *    - No flow control
00063  *  -# Connect an Ethernet cable between the evaluation board and the network.
00064  *      The board may be connected directly to a computer; in this case,
00065  *      make sure to use a cross/twisted wired cable such as the one provided
00066  *      with the evaluation kit.
00067  *  -# Start the application. It will display the following message on the terminal:
00068  *    \code
00069  *    -- GMAC uIP Telnetd Example xxx --
00070  *    -- xxxxxx-xx
00071  *    -- Compiled: xxx xx xxxx xx:xx:xx --
00072  *    - MAC 3a:1f:34:08:54:05
00073  *    - Host IP 10.217.12.223
00074  *    - Router IP 10.217.12.1
00075  *    - Net Mask 255.255.255.0
00076  *    \endcode
00077  *  -# Connect to the %device IP address using telnet on port 23:
00078  *    \code
00079  *    telnet 10.217.12.223 23
00080  *    \endcode
00081  *    A telnet terminal will appear:
00082  *    \code
00083  *    uIP command shell
00084  *    Type '?' and return for help
00085  *    uIP 1.0>
00086  *    \endcode
00087  * \note
00088  * Make sure the IP address of the device(EK board) and the computer are in the same network.
00089  */
00090 
00091 /** \file
00092  *
00093  *  This file contains all the specific code for the gmac_uip_telnetd example.
00094  *
00095  */
00096 
00097 /*----------------------------------------------------------------------------
00098  *        Headers
00099  *----------------------------------------------------------------------------*/
00100 
00101 #include "board.h"
00102 
00103 #include "uip.h"
00104 #include "uip_arp.h"
00105 #include "gmac_tapdev.h"
00106 #include "timer.h"
00107 
00108 /*---------------------------------------------------------------------------
00109  *         Variables
00110  *---------------------------------------------------------------------------*/
00111 
00112 /* uIP buffer : The ETH header */
00113 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00114 
00115 /* The MAC address used for demo */
00116 static const struct uip_eth_addr MacAddress = {{0x3a, 0x1f, 0x34, 0x08, 0x54, 0x54}};
00117 
00118 /* The IP address used for demo (ping ...) */
00119 static uint8_t HostIpAddress[4] = {10,217,12,223};
00120 
00121 /* Set the default router's IP address. */
00122 static const uint8_t RoutIpAddress[4] = {10,217,12,1};
00123 
00124 /* The NetMask address */
00125 static const uint8_t NetMask[4] = {255, 255, 248, 0};
00126 
00127 /*----------------------------------------------------------------------------
00128  *        Local functions
00129  *----------------------------------------------------------------------------*/
00130 
00131 /**
00132  * Initialize demo application
00133  */
00134 static void _app_init(void)
00135 {
00136     printf("P: telnetd application init\n\r");
00137     telnetd_init();
00138 
00139 #ifdef __DHCPC_H__
00140     printf("P: DHCPC Init\n\r");
00141     dhcpc_init(MacAddress.addr, 6);
00142 #endif
00143 }
00144 
00145 /*----------------------------------------------------------------------------
00146  *        Exported functions
00147  *----------------------------------------------------------------------------*/
00148 
00149 /**
00150  * uip_log: Global function for uIP to use.
00151  * \param m Pointer to string that logged
00152  */
00153 void uip_log(char *m)
00154 {
00155     TRACE_INFO("-uIP log- %s\n\r", m);
00156 }
00157 
00158 #ifdef __DHCPC_H__
00159 /**
00160  * dhcpc_configured: Global function for uIP DHCPC to use,
00161  * notification of DHCP configuration.
00162  * \param s Pointer to DHCP state instance
00163  */
00164 void dhcpc_configured(const struct dhcpc_state *s)
00165 {
00166     u8_t * pAddr;
00167 
00168     printf("\n\r");
00169     printf("=== DHCP Configurations ===\n\r");
00170     pAddr = (u8_t *)s->ipaddr;
00171     printf("- IP   : %d.%d.%d.%d\n\r",
00172             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00173     pAddr = (u8_t *)s->netmask;
00174     printf("- Mask : %d.%d.%d.%d\n\r",
00175             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00176     pAddr = (u8_t *)s->default_router;
00177     printf("- GW   : %d.%d.%d.%d\n\r",
00178             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00179     pAddr = (u8_t *)s->dnsaddr;
00180     printf("- DNS  : %d.%d.%d.%d\n\r",
00181             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00182     printf("===========================\n\r\n");
00183     uip_sethostaddr(s->ipaddr);
00184     uip_setnetmask(s->netmask);
00185     uip_setdraddr(s->default_router);
00186 
00187 #ifdef __RESOLV_H__
00188     resolv_conf(s->dnsaddr);
00189 #else
00190     printf("DNS NOT enabled in the demo\n\r");
00191 #endif
00192 }
00193 #endif
00194 
00195 /**
00196  *  \brief gmac_uip_telnetd example entry point.
00197  *
00198  *  \return Unused (ANSI-C compatibility).
00199  */
00200 int main(void)
00201 {
00202     uip_ipaddr_t ipaddr;
00203     struct timer periodic_timer, arp_timer;
00204     uint32_t i;
00205 
00206     /* Disable watchdog */
00207     WDT_Disable( WDT ) ;
00208    
00209     SCB_EnableICache();
00210     SCB_EnableDCache();
00211 
00212     TimeTick_Configure();
00213     printf("-- GMAC uIP Telnetd Example %s --\n\r", SOFTPACK_VERSION);
00214     printf("-- %s\n\r", BOARD_NAME);
00215     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00216 
00217     /* Display MAC & IP settings */
00218     printf(" - MAC %x:%x:%x:%x:%x:%x\n\r",
00219             MacAddress.addr[0], MacAddress.addr[1], MacAddress.addr[2],
00220             MacAddress.addr[3], MacAddress.addr[4], MacAddress.addr[5]);
00221 #ifndef __DHCPC_H__
00222     printf(" - Host IP  %d.%d.%d.%d\n\r",
00223             HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00224     printf(" - Router IP  %d.%d.%d.%d\n\r",
00225             RoutIpAddress[0], RoutIpAddress[1], RoutIpAddress[2], RoutIpAddress[3]);
00226     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00227             NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00228 #endif
00229 
00230     /* System devices initialize */
00231     gmac_tapdev_setmac((uint8_t *)MacAddress.addr);
00232     gmac_tapdev_init();
00233     clock_init();
00234     timer_set(&periodic_timer, CLOCK_SECOND / 2);
00235     timer_set(&arp_timer, CLOCK_SECOND * 10);
00236 
00237     /* Init uIP */
00238     uip_init();
00239 
00240 #ifdef __DHCPC_H__
00241     printf("P: DHCP Supported\n\r");
00242     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00243     uip_sethostaddr(ipaddr);
00244     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00245     uip_setdraddr(ipaddr);
00246     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00247     uip_setnetmask(ipaddr);
00248 #else
00249     /* Set the IP address of this host */
00250     uip_ipaddr(ipaddr, HostIpAddress[0], HostIpAddress[1],
00251             HostIpAddress[2], HostIpAddress[3]);
00252     uip_sethostaddr(ipaddr);
00253 
00254     uip_ipaddr(ipaddr, RoutIpAddress[0], RoutIpAddress[1],
00255             RoutIpAddress[2], RoutIpAddress[3]);
00256     uip_setdraddr(ipaddr);
00257 
00258     uip_ipaddr(ipaddr, NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00259     uip_setnetmask(ipaddr);
00260 #endif
00261 
00262     uip_setethaddr(MacAddress);
00263 
00264     _app_init();
00265 
00266     while(1) {
00267         uip_len = gmac_tapdev_read();
00268         if(uip_len > 0) {
00269             if(BUF->type == htons(UIP_ETHTYPE_IP)) {
00270                 uip_arp_ipin();
00271                 uip_input();
00272                 /* If the above function invocation resulted in data that
00273                    should be sent out on the network, the global variable
00274                    uip_len is set to a value > 0. */
00275                 if(uip_len > 0) {
00276                     uip_arp_out();
00277                     gmac_tapdev_send();
00278                 }
00279             } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
00280                 uip_arp_arpin();
00281                 /* If the above function invocation resulted in data that
00282                    should be sent out on the network, the global variable
00283                    uip_len is set to a value > 0. */
00284                 if(uip_len > 0) {
00285                     gmac_tapdev_send();
00286                 }
00287             }
00288         } else if(timer_expired(&periodic_timer)) {
00289             timer_reset(&periodic_timer);
00290             for(i = 0; i < UIP_CONNS; i++) {
00291                 uip_periodic(i);
00292                 SCB_CleanInvalidateDCache();
00293                 /* If the above function invocation resulted in data that
00294                    should be sent out on the network, the global variable
00295                    uip_len is set to a value > 0. */
00296                 if(uip_len > 0) {
00297                     uip_arp_out();
00298                     gmac_tapdev_send();
00299                 }
00300             }
00301 #if UIP_UDP
00302             for(i = 0; i < UIP_UDP_CONNS; i++) {
00303                 uip_udp_periodic(i);
00304                 /* If the above function invocation resulted in data that
00305                    should be sent out on the network, the global variable
00306                    uip_len is set to a value > 0. */
00307                 if(uip_len > 0) {
00308                     uip_arp_out();
00309                     gmac_tapdev_send();
00310                 }
00311             }
00312 #endif /* UIP_UDP */
00313 
00314             /* Call the ARP timer function every 10 seconds. */
00315             if(timer_expired(&arp_timer)) {
00316                 timer_reset(&arp_timer);
00317                 uip_arp_timer();
00318             }
00319         }
00320     }
00321 }
00322 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines