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_webserver GMAC uIP Web Server Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This project implements a webserver example of the uIP TCP/IP stack.
00036  *  It enables the device to act as a web server, displaying network information
00037  *  through an HTML browser.
00038  *
00039  *  \section Requirements
00040  *
00041  * This package can be used with SAM V71 Xplained Ultra board.
00042  *
00043  *  \section Description
00044  *
00045  *  Please refer to the uIP documentation for more information
00046  *  about the TCP/IP stack, the webserver example.
00047  *
00048  * By default, the example does not use DHCP.
00049  * If you want to use DHCP, please:
00050  * - Open file uip-conf.h and don't comment the line "#define UIP_DHCP_on".
00051  * - Include uip/apps/dhcps to compile.
00052  *
00053  *  \section Usage
00054  *
00055  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00056  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00057  *  -# On the computer, open and configure a terminal application
00058  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00059  *    - 115200 bauds
00060  *    - 8 bits of data
00061  *    - No parity
00062  *    - 1 stop bit
00063  *    - No flow control
00064  *  -# Connect an Ethernet cable between the evaluation board and the network.
00065  *      The board may be connected directly to a computer; in this case,
00066  *      make sure to use a cross/twisted wired cable such as the one provided
00067  *      with the evaluation kit.
00068  *  -# Start the application. It will display the following message on the terminal:
00069  *    \code
00070  *    -- GMAC uIP Web Server Example xxx --
00071  *    -- xxxxxx-xx
00072  *    -- Compiled: xxx xx xxxx xx:xx:xx --
00073  *    - MAC 3a:1f:34:08:54:05
00074  *    - Host IP 10.217.12.223
00075  *    - Router IP 10.217.12.1
00076  *    - Net Mask 255.255.255.0
00077  *    \endcode
00078  * -# Type the IP address (Host IP in the debug log) of the device in a web 
00079  * browser:
00080  *    \code
00081  *    http://10.217.12.223
00082  *    \endcode
00083  *    The page generated by uIP will appear in the browser.
00084  *
00085  * \note
00086  * Make sure the IP address of the device(EK board) and the computer are in the 
00087  * same network.
00088  */
00089 
00090 /** \file
00091  *
00092  *  This file contains all the specific code for the gmac_uip_webserver example.
00093  *
00094  */
00095 
00096 /*----------------------------------------------------------------------------
00097  *        Headers
00098  *----------------------------------------------------------------------------*/
00099 
00100 #include "board.h"
00101 
00102 #include "uip.h"
00103 #include "uip_arp.h"
00104 #include "gmac_tapdev.h"
00105 #include "timer.h"
00106 
00107 /*---------------------------------------------------------------------------
00108  *         Variables
00109  *---------------------------------------------------------------------------*/
00110 
00111 /* uIP buffer : The ETH header */
00112 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00113 
00114 /* The MAC address used for demo */
00115 static const struct uip_eth_addr MacAddress = {{0x3a, 0x1f, 0x34, 0x09, 0x54, 0x54}};
00116 
00117 /* The IP address used for demo (ping ...) */
00118 static const uint8_t HostIpAddress[4] = {10, 217, 12, 223};
00119 
00120 /* Set the default router's IP address. */
00121 static const uint8_t RoutIpAddress[4] = {10, 217, 12, 1};
00122 
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: webserver application init\n\r");
00136     httpd_init();
00137     printf ("Type the IP address of the device in a web browser, \
00138         http://%d.%d.%d.%d \n\r", 
00139         HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00140 
00141 #ifdef __DHCPC_H__
00142     printf("P: DHCPC Init\n\r");
00143     dhcpc_init(MacAddress.addr, 6);
00144 #endif
00145 }
00146 
00147 /*----------------------------------------------------------------------------
00148  *        Exported functions
00149  *----------------------------------------------------------------------------*/
00150 
00151 /**
00152  * uip_log: Global function for uIP to use.
00153  * \param m Pointer to string that logged
00154  */
00155 void uip_log(char *m)
00156 {
00157     TRACE_INFO("-uIP log- %s\n\r", m);
00158 }
00159 
00160 #ifdef __DHCPC_H__
00161 /**
00162  * dhcpc_configured: Global function for uIP DHCPC to use,
00163  * notification of DHCP configuration.
00164  * \param s Pointer to DHCP state instance
00165  */
00166 void dhcpc_configured(const struct dhcpc_state *s)
00167 {
00168     u8_t * pAddr;
00169 
00170     printf("\n\r");
00171     printf("=== DHCP Configurations ===\n\r");
00172     pAddr = (u8_t *)s->ipaddr;
00173     printf("- IP   : %d.%d.%d.%d\n\r",
00174             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00175     pAddr = (u8_t *)s->netmask;
00176     printf("- Mask : %d.%d.%d.%d\n\r",
00177             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00178     pAddr = (u8_t *)s->default_router;
00179     printf("- GW   : %d.%d.%d.%d\n\r",
00180             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00181     pAddr = (u8_t *)s->dnsaddr;
00182     printf("- DNS  : %d.%d.%d.%d\n\r",
00183             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00184     printf("===========================\n\r\n");
00185     uip_sethostaddr(s->ipaddr);
00186     uip_setnetmask(s->netmask);
00187     uip_setdraddr(s->default_router);
00188 
00189 #ifdef __RESOLV_H__
00190     resolv_conf(s->dnsaddr);
00191 #else
00192     printf("DNS NOT enabled in the demo\n\r");
00193 #endif
00194 }
00195 #endif
00196 
00197 /**
00198  *  \brief gmac_uip_webserver example entry point.
00199  *
00200  *  \return Unused (ANSI-C compatibility).
00201  */
00202 int main(void)
00203 {
00204     uip_ipaddr_t ipaddr;
00205     struct timer periodic_timer, arp_timer;
00206     uint32_t i;
00207 
00208     /* Disable watchdog */
00209     WDT_Disable( WDT ) ;
00210 
00211     SCB_EnableICache();
00212     SCB_EnableDCache();
00213     TimeTick_Configure();
00214     printf("-- GMAC uIP Web Server Example %s --\n\r", SOFTPACK_VERSION);
00215     printf("-- %s\n\r", BOARD_NAME);
00216     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00217 
00218     /* Display MAC & IP settings */
00219     printf(" - MAC %x:%x:%x:%x:%x:%x\n\r",
00220             MacAddress.addr[0], MacAddress.addr[1], MacAddress.addr[2],
00221             MacAddress.addr[3], MacAddress.addr[4], MacAddress.addr[5]);
00222 #ifndef __DHCPC_H__
00223     printf(" - Host IP  %d.%d.%d.%d\n\r",
00224             HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00225     printf(" - Router IP  %d.%d.%d.%d\n\r",
00226             RoutIpAddress[0], RoutIpAddress[1], RoutIpAddress[2], RoutIpAddress[3]);
00227     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00228             NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00229 #endif
00230 
00231     /* System devices initialize */
00232     gmac_tapdev_setmac((uint8_t *)MacAddress.addr);
00233     gmac_tapdev_init();
00234     clock_init();    
00235     timer_set(&periodic_timer, CLOCK_SECOND / 2);
00236     timer_set(&arp_timer, CLOCK_SECOND * 10);
00237 
00238     /* Initialize uIP */
00239     uip_init();
00240 
00241 #ifdef __DHCPC_H__
00242     printf("P: DHCP Supported\n\r");
00243     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00244     uip_sethostaddr(ipaddr);
00245     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00246     uip_setdraddr(ipaddr);
00247     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00248     uip_setnetmask(ipaddr);
00249 #else
00250     /* Set the IP address of this host */
00251     uip_ipaddr(ipaddr, HostIpAddress[0], HostIpAddress[1],
00252             HostIpAddress[2], HostIpAddress[3]);
00253     uip_sethostaddr(ipaddr);
00254 
00255     uip_ipaddr(ipaddr, RoutIpAddress[0], RoutIpAddress[1],
00256             RoutIpAddress[2], RoutIpAddress[3]);
00257     uip_setdraddr(ipaddr);
00258 
00259     uip_ipaddr(ipaddr, NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00260     uip_setnetmask(ipaddr);
00261 #endif
00262 
00263     uip_setethaddr(MacAddress);
00264 
00265     _app_init();
00266 
00267     while(1) {
00268         uip_len = gmac_tapdev_read();
00269         if(uip_len > 0) {
00270             if(BUF->type == htons(UIP_ETHTYPE_IP)) {
00271                 uip_arp_ipin();
00272                 uip_input();
00273                 /* If the above function invocation resulted in data that
00274                    should be sent out on the network, the global variable
00275                    uip_len is set to a value > 0. */
00276                 if(uip_len > 0) {
00277                     uip_arp_out();
00278                     gmac_tapdev_send();
00279                 }
00280             } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
00281                 uip_arp_arpin();
00282                 /* If the above function invocation resulted in data that
00283                    should be sent out on the network, the global variable
00284                    uip_len is set to a value > 0. */
00285                 if(uip_len > 0) {
00286                     gmac_tapdev_send();
00287                 }
00288             }
00289         } else if(timer_expired(&periodic_timer)) {
00290             timer_reset(&periodic_timer);
00291             for(i = 0; i < UIP_CONNS; i++) {
00292                 uip_periodic(i);
00293                 SCB_CleanInvalidateDCache();
00294                 /* If the above function invocation resulted in data that
00295                    should be sent out on the network, the global variable
00296                    uip_len is set to a value > 0. */
00297                 if(uip_len > 0) {
00298                     uip_arp_out();
00299                     SCB_CleanInvalidateDCache();
00300                     gmac_tapdev_send();
00301                 }
00302             }
00303 #if UIP_UDP
00304             for(i = 0; i < UIP_UDP_CONNS; i++) {
00305                 uip_udp_periodic(i);
00306                 memory_sync();
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                     SCB_CleanInvalidateDCache();
00313                     gmac_tapdev_send();
00314                 }
00315             }
00316 #endif /* UIP_UDP */
00317 
00318             /* Call the ARP timer function every 10 seconds. */
00319             if(timer_expired(&arp_timer)) {
00320                 timer_reset(&arp_timer);
00321                 uip_arp_timer();
00322             }
00323         }
00324     }
00325 }
00326 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines