SAMV71 Xplained Ultra Software Package 1.4

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_helloworld GMAC uIP Hello World Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This project implements a telnet hello-world example of the uIP stack.
00036  *  It makes the device to respond to telnet connections on port 1000.
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 hello-world 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. 
00055  *     Please 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 Hello World 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 1000:
00078  *    \code
00079  *    telnet 10.217.12.223 1000
00080  *    \endcode
00081  *    A greeting message will appear:
00082  *    \code
00083  *    Hello. What is your name?
00084  *    \endcode
00085  *
00086  * \note
00087  * Make sure the IP address of the device(EK board) and the computer are in the 
00088  * same network.
00089  */
00090 
00091 /** \file
00092  *
00093  *  This file contains all the specific code for the gmac_uip_helloworld 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] ={192, 168, 1, 3 };
00120 
00121 /* Set the default router's IP address. */
00122 static const uint8_t RoutIpAddress[4] = {192, 168, 1, 2 };
00123 /* The NetMask address */
00124 static const uint8_t NetMask[4] = {255, 255, 255, 0};
00125 
00126 /*----------------------------------------------------------------------------
00127  *        Local functions
00128  *----------------------------------------------------------------------------*/
00129 
00130 /**
00131  * Initialize demo application
00132  */
00133 static void _app_init(void)
00134 {
00135     printf("P: hello-world application init\n\r");
00136     hello_world_init();
00137 
00138 #ifdef __DHCPC_H__
00139     printf("P: DHCPC Init\n\r");
00140     dhcpc_init(MacAddress.addr, 6);
00141 #endif
00142 }
00143 
00144 /*----------------------------------------------------------------------------
00145  *        Exported functions
00146  *----------------------------------------------------------------------------*/
00147 
00148 /**
00149  * uip_log: Global function for uIP to use.
00150  * \param m Pointer to string that logged
00151  */
00152 void uip_log(char *m)
00153 {
00154     TRACE_INFO("-uIP log- %s\n\r", m);
00155 }
00156 
00157 #ifdef __DHCPC_H__
00158 /**
00159  * dhcpc_configured: Global function for uIP DHCPC to use,
00160  * notification of DHCP configuration.
00161  * \param s Pointer to DHCP state instance
00162  */
00163 void dhcpc_configured(const struct dhcpc_state *s)
00164 {
00165     u8_t * pAddr;
00166 
00167     printf("\n\r");
00168     printf("=== DHCP Configurations ===\n\r");
00169     pAddr = (u8_t *)s->ipaddr;
00170     printf("- IP   : %d.%d.%d.%d\n\r",
00171             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00172     pAddr = (u8_t *)s->netmask;
00173     printf("- Mask : %d.%d.%d.%d\n\r",
00174             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00175     pAddr = (u8_t *)s->default_router;
00176     printf("- GW   : %d.%d.%d.%d\n\r",
00177             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00178     pAddr = (u8_t *)s->dnsaddr;
00179     printf("- DNS  : %d.%d.%d.%d\n\r",
00180             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00181     printf("===========================\n\r\n");
00182     uip_sethostaddr(s->ipaddr);
00183     uip_setnetmask(s->netmask);
00184     uip_setdraddr(s->default_router);
00185 
00186 #ifdef __RESOLV_H__
00187     resolv_conf(s->dnsaddr);
00188 #else
00189     printf("DNS NOT enabled in the demo\n\r");
00190 #endif
00191 }
00192 #endif
00193 
00194 /**
00195  *  \brief gmac_uip_helloworld example entry point.
00196  *
00197  *  \return Unused (ANSI-C compatibility).
00198  */
00199 int main(void)
00200 {
00201     uip_ipaddr_t ipaddr;
00202     struct timer periodic_timer, arp_timer;
00203     uint32_t i;
00204 
00205     /* Disable watchdog */
00206     WDT_Disable( WDT ) ;
00207 
00208     SCB_EnableICache();
00209     SCB_EnableDCache();
00210 
00211     printf("-- GMAC uIP Hello World Example %s --\n\r", SOFTPACK_VERSION);
00212     printf("-- %s\n\r", BOARD_NAME);
00213     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00214 
00215 
00216     /* Configure systick for 1 ms. */
00217     TimeTick_Configure ();
00218 
00219     /* Display MAC & IP settings */
00220     printf(" - MAC %x:%x:%x:%x:%x:%x\n\r",
00221             MacAddress.addr[0], MacAddress.addr[1], MacAddress.addr[2],
00222             MacAddress.addr[3], MacAddress.addr[4], MacAddress.addr[5]);
00223 #ifndef __DHCPC_H__
00224     printf(" - Host IP  %d.%d.%d.%d\n\r",
00225             HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00226     printf(" - Router IP  %d.%d.%d.%d\n\r",
00227             RoutIpAddress[0], RoutIpAddress[1], RoutIpAddress[2], RoutIpAddress[3]);
00228     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00229             NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00230 #endif
00231 
00232     /* System devices initialize */
00233     gmac_tapdev_setmac((uint8_t *)MacAddress.addr);
00234     gmac_tapdev_init();
00235     clock_init();
00236     timer_set(&periodic_timer, CLOCK_SECOND / 2);
00237     timer_set(&arp_timer, CLOCK_SECOND * 10);
00238 
00239     /* Init uIP */
00240     uip_init();
00241 
00242 #ifdef __DHCPC_H__
00243     printf("P: DHCP Supported\n\r");
00244     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00245     uip_sethostaddr(ipaddr);
00246     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00247     uip_setdraddr(ipaddr);
00248     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00249     uip_setnetmask(ipaddr);
00250 #else
00251     /* Set the IP address of this host */
00252     uip_ipaddr(ipaddr, HostIpAddress[0], HostIpAddress[1],
00253             HostIpAddress[2], HostIpAddress[3]);
00254     uip_sethostaddr(ipaddr);
00255 
00256     uip_ipaddr(ipaddr, RoutIpAddress[0], RoutIpAddress[1],
00257             RoutIpAddress[2], RoutIpAddress[3]);
00258     uip_setdraddr(ipaddr);
00259 
00260     uip_ipaddr(ipaddr, NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00261     uip_setnetmask(ipaddr);
00262 #endif
00263 
00264     uip_setethaddr(MacAddress);
00265 
00266     _app_init();
00267 
00268     while(1) {
00269         uip_len = gmac_tapdev_read();
00270         if(uip_len > 0) {
00271             if(BUF->type == htons(UIP_ETHTYPE_IP)) {
00272                 uip_arp_ipin();
00273                 uip_input();
00274                 /* If the above function invocation resulted in data that
00275                    should be sent out on the network, the global variable
00276                    uip_len is set to a value > 0. */
00277                 if(uip_len > 0) {
00278                     uip_arp_out();
00279                     gmac_tapdev_send();
00280                 }
00281             } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
00282               
00283                 uip_arp_arpin();
00284                 /* If the above function invocation resulted in data that
00285                    should be sent out on the network, the global variable
00286                    uip_len is set to a value > 0. */
00287                 if(uip_len > 0) {
00288                     gmac_tapdev_send();
00289                 }
00290             }
00291         } else if(timer_expired(&periodic_timer)) {
00292             timer_reset(&periodic_timer);
00293             for(i = 0; i < UIP_CONNS; i++) {
00294                 uip_periodic(i);
00295                 /* If the above function invocation resulted in data that
00296                    should be sent out on the network, the global variable
00297                    uip_len is set to a value > 0. */
00298                 if(uip_len > 0) {
00299                     
00300                     uip_arp_out();
00301                     gmac_tapdev_send();
00302                 }
00303             }
00304 #if UIP_UDP
00305             for(i = 0; i < UIP_UDP_CONNS; i++) {
00306                 uip_udp_periodic(i);
00307                 /* If the above function invocation resulted in data that
00308                    should be sent out on the network, the global variable
00309                    uip_len is set to a value > 0. */
00310                 if(uip_len > 0) {
00311                     uip_arp_out();
00312                     gmac_tapdev_send();
00313                 }
00314             }
00315 #endif /* UIP_UDP */
00316 
00317             /* Call the ARP timer function every 10 seconds. */
00318             if(timer_expired(&arp_timer)) {
00319                 timer_reset(&arp_timer);
00320                 uip_arp_timer();
00321             }
00322         }
00323     }
00324 }
00325 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines