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_lwip GMAC lwIP Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This project implements webserver example by using lwIP stack, It enables
00036  *  the device to act as a web server, sending a very short page when accessed
00037  *  through a 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 lwIP documentation for more information about
00046  *  the TCP/IP stack and the webserver example.
00047  *
00048  *  By default, the example does not use DHCP. If you want to use DHCP,
00049  *  please open file lwipopts.h and define "LWIP_DHCP" and "LWIP_UDP" to 1.
00050  *
00051  *  \section Usage
00052  *
00053  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00054  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00055  *  -# On the computer, open and configure a terminal application
00056  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00057  *    - 115200 bauds
00058  *    - 8 bits of data
00059  *    - No parity
00060  *    - 1 stop bit
00061  *    - No flow control
00062  *  -# Connect an Ethernet cable between the evaluation board and the network.
00063  *      The board may be connected directly to a computer; in this case,
00064  *      make sure to use a cross/twisted wired cable such as the one provided
00065  *      with the evaluation kit.
00066  *  -# Start the application. It will display the following message on the 
00067  *  terminal:
00068  *    \code
00069  *    -- GMAC lwIP Example xxx --
00070  *    -- xxxxxx-xx
00071  *    -- Compiled: xxx xx xxxx xx:xx:xx --
00072  *      MAC 3a:1f:34:08:54:54
00073  *    - Host IP  192.168.1.3
00074  *    - Gateway IP 192.168.1.2
00075  *    - Net Mask 255.255.255.0
00076  *    \endcode
00077  * -# Type the IP address (Host IP in the debug log) of the device in a web
00078  *    browser, like this:
00079  *    \code
00080  *    http://192.168.1.3
00081  *    \endcode
00082  *    The page generated by lwIP will appear in the web browser, like below:
00083  *    \code
00084  *    Small test page.#
00085  *    \endcode
00086  *
00087  *  \note
00088  *  Make sure the IP address of the device and the computer are in the same 
00089  *  network.
00090  */
00091 
00092 /** \file
00093  *
00094  *  This file contains all the specific code for the gmac_lwip example.
00095  *
00096  */
00097 
00098 /*----------------------------------------------------------------------------
00099  *        Headers
00100  *----------------------------------------------------------------------------*/
00101 
00102 
00103 #include <board.h>
00104 #include <liblwip.h>
00105 #include "httpd.h"
00106 
00107 /*----------------------------------------------------------------------------
00108  *        Types
00109  *----------------------------------------------------------------------------*/
00110 
00111 /* Timer for calling lwIP tmr functions without system */
00112 typedef struct _timers_info {
00113     uint32_t timer;
00114     uint32_t timer_interval;
00115     void (*timer_func)(void);
00116 } timers_info;
00117 
00118 /*---------------------------------------------------------------------------
00119  *         Variables
00120  *---------------------------------------------------------------------------*/
00121 
00122 #if 1
00123 /* lwIP tmr functions list */
00124 static timers_info timers_table[] = {
00125     /* LWIP_TCP */
00126     { 0, TCP_FAST_INTERVAL,     tcp_fasttmr},
00127     { 0, TCP_SLOW_INTERVAL,     tcp_slowtmr},
00128     /* LWIP_ARP */
00129     { 0, ARP_TMR_INTERVAL,      etharp_tmr},
00130     /* LWIP_DHCP */
00131 #if LWIP_DHCP
00132     { 0, DHCP_COARSE_TIMER_SECS, dhcp_coarse_tmr},
00133     { 0, DHCP_FINE_TIMER_MSECS,  dhcp_fine_tmr},
00134 #endif
00135 };
00136 #endif
00137 
00138 /* The MAC address used for demo */
00139 static uint8_t gMacAddress[6] = {0x3a, 0x1f, 0x34, 0x08, 0x54, 0x54};
00140 
00141 /* The IP address used for demo (ping ...) */
00142 static uint8_t gIpAddress[4] = {192, 168, 1, 3};
00143 
00144 /* Set the default router's IP address. */
00145 static const uint8_t gGateWay[4] = {192, 168, 1, 2};
00146 /* The NetMask address */
00147 static const uint8_t gNetMask[4] = {255, 255, 255, 0};
00148 
00149 /*----------------------------------------------------------------------------
00150  *        Local functions
00151  *----------------------------------------------------------------------------*/
00152 /**
00153  * Process timing functions
00154  */
00155 static void timers_update(void)
00156 {
00157     static uint32_t last_time;
00158     uint32_t cur_time, time_diff, idxtimer;
00159     timers_info * ptmr_inf;
00160 
00161     cur_time = sys_get_ms();
00162     if (cur_time >= last_time) {
00163         time_diff = cur_time - last_time;
00164     } else {
00165         time_diff = 0xFFFFFFFF - last_time + cur_time;
00166     }
00167     if (time_diff) {
00168         last_time = cur_time;
00169         for(idxtimer = 0;
00170             idxtimer < (sizeof(timers_table)/sizeof(timers_info));
00171             idxtimer ++) {
00172             ptmr_inf = &timers_table[idxtimer];
00173             ptmr_inf->timer += time_diff;
00174             if (ptmr_inf->timer > ptmr_inf->timer_interval) {
00175                 if (ptmr_inf->timer_func)
00176                     ptmr_inf->timer_func();
00177                 ptmr_inf->timer -= ptmr_inf->timer_interval;
00178             }
00179         }
00180     }
00181 }
00182 
00183 /*----------------------------------------------------------------------------
00184  *        Exported functions
00185  *----------------------------------------------------------------------------*/
00186 
00187 /**
00188  * Gmac interrupt handler
00189  */
00190 void GMAC_Handler(void)
00191 {
00192     GMACD_Handler(&gGmacd, GMAC_QUE_0);
00193 }
00194 
00195 /**
00196  *  \brief gmac_lwip example entry point.
00197  *
00198  *  \return Unused (ANSI-C compatibility).
00199  */
00200 int main(void)
00201 {
00202     struct ip_addr ipaddr, netmask, gw;
00203     struct netif NetIf, *netif;
00204 
00205 #if LWIP_DHCP
00206     u8_t dhcp_state = DHCP_INIT;
00207 #endif
00208 
00209     /* Disable watchdog */
00210     WDT_Disable( WDT) ;
00211  
00212     printf("-- GMAC lwIP Example %s --\n\r", SOFTPACK_VERSION);
00213     printf("-- %s\n\r", BOARD_NAME);
00214     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00215 
00216     SCB_EnableICache();
00217     SCB_EnableDCache();
00218 
00219     /* Configure systick for 1 ms. */
00220     TimeTick_Configure ();
00221 
00222     /* Display MAC & IP settings */
00223     printf(" - MAC %x:%x:%x:%x:%x:%x\n\r",
00224             gMacAddress[0], gMacAddress[1], gMacAddress[2],
00225             gMacAddress[3], gMacAddress[4], gMacAddress[5]);
00226 
00227 #if !LWIP_DHCP
00228     printf(" - Host IP  %d.%d.%d.%d\n\r",
00229             gIpAddress[0], gIpAddress[1],
00230             gIpAddress[2], gIpAddress[3]);
00231     printf(" - GateWay IP  %d.%d.%d.%d\n\r",
00232             gGateWay[0], gGateWay[1], gGateWay[2], gGateWay[3]);
00233     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00234             gNetMask[0], gNetMask[1], gNetMask[2], gNetMask[3]);
00235 #else
00236     printf(" - DHCP Enabled\n\r");
00237 #endif
00238 
00239     /* Initialize system timing */
00240     sys_init_timing();
00241 
00242     /* Initialize lwIP modules */
00243     lwip_init();
00244 
00245     /* Initialize net interface for lwIP */
00246     gmacif_setmac((u8_t*)gMacAddress);
00247 
00248 #if !LWIP_DHCP
00249     IP4_ADDR(&gw, gGateWay[0], gGateWay[1], gGateWay[2], gGateWay[3]);
00250     IP4_ADDR(&ipaddr, gIpAddress[0], gIpAddress[1], gIpAddress[2], gIpAddress[3]);
00251     IP4_ADDR(&netmask, gNetMask[0], gNetMask[1], gNetMask[2], gNetMask[3]);
00252 #else
00253     IP4_ADDR(&gw, 0, 0, 0, 0);
00254     IP4_ADDR(&ipaddr, 0, 0, 0, 0);
00255     IP4_ADDR(&netmask, 0, 0, 0, 0);
00256 #endif
00257     netif = netif_add(&NetIf, &ipaddr, &netmask, &gw, NULL, gmacif_init, ip_input);
00258     netif_set_default(netif);
00259     netif_set_up(netif);
00260     /* Initialize http server application */
00261     if (ERR_OK != httpd_init())
00262     {
00263         printf("httpd_init ERR_OK!");
00264         return -1;
00265     }
00266     printf ("Type the IP address of the device in a web browser,\
00267         http://%d.%d.%d.%d \n\r", 
00268         gIpAddress[0], gIpAddress[1], gIpAddress[2], gIpAddress[3]);
00269     while(1) {
00270         /* Run periodic tasks */
00271         timers_update();
00272         /* Run polling tasks */
00273         gmacif_poll(netif);
00274     }
00275 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines