SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 SAMV71 Xplained Ultra board or SAME70 Xplained 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 board.
00054  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00055  *  -# On the computer, open and configure a terminal application
00056  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00057  *    - 115200 baud rate
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  *  \section References
00092  *  - gmac_lwip/main.c
00093  *  - gmacb.h
00094  *  - gmacd.h
00095  *  - gmac.h
00096  *  - liblwip.h
00097  */
00098 
00099 /** \file
00100  *
00101  *  This file contains all the specific code for the gmac_lwip example.
00102  *
00103  */
00104 
00105 /*----------------------------------------------------------------------------
00106  *        Headers
00107  *----------------------------------------------------------------------------*/
00108 
00109 
00110 #include <board.h>
00111 #include <liblwip.h>
00112 #include "httpd.h"
00113 
00114 /*---------------------------------------------------------------------------
00115  *         Local Define
00116  *---------------------------------------------------------------------------*/
00117 
00118 /** TWI clock frequency in Hz. */
00119 #define TWCK            400000
00120 /** Slave address of twi_eeprom AT24MAC.*/
00121 #define AT24MAC_SERIAL_NUM_ADD  0x5F
00122 /** Page size of an AT24MAC402 chip (in bytes)*/
00123 #define PAGE_SIZE       16
00124 /** Page numbers of an AT24MAC402 chip */
00125 #define EEPROM_PAGES    16
00126 /** EEPROM Pins definition */
00127 #define BOARD_PINS_TWI_EEPROM PINS_TWI0
00128 /** TWI0 peripheral ID for EEPROM device*/
00129 #define BOARD_ID_TWI_EEPROM   ID_TWIHS0
00130 /** TWI0 base address for EEPROM device */
00131 #define BOARD_BASE_TWI_EEPROM TWIHS0
00132 /*----------------------------------------------------------------------------
00133  *        Types
00134  *----------------------------------------------------------------------------*/
00135 
00136 /* Timer for calling lwIP tmr functions without system */
00137 typedef struct _timers_info {
00138     uint32_t timer;
00139     uint32_t timer_interval;
00140     void (*timer_func)(void);
00141 } timers_info;
00142 
00143 /*---------------------------------------------------------------------------
00144  *         Variables
00145  *---------------------------------------------------------------------------*/
00146 /** TWI driver instance.*/
00147 static Twid twid;
00148 
00149 /* lwIP tmr functions list */
00150 static timers_info timers_table[] = {
00151     /* LWIP_TCP */
00152     { 0, TCP_FAST_INTERVAL,     tcp_fasttmr},
00153     { 0, TCP_SLOW_INTERVAL,     tcp_slowtmr},
00154     /* LWIP_ARP */
00155     { 0, ARP_TMR_INTERVAL,      etharp_tmr},
00156     /* LWIP_DHCP */
00157 #if LWIP_DHCP
00158     { 0, DHCP_COARSE_TIMER_SECS, dhcp_coarse_tmr},
00159     { 0, DHCP_FINE_TIMER_MSECS,  dhcp_fine_tmr},
00160 #endif
00161 };
00162 
00163 /** The PINs for TWI*/
00164 static const Pin twiPins[]      = BOARD_PINS_TWI_EEPROM;
00165 
00166 /** The MAC address used for demo */
00167 static uint8_t GMacAddress[6] = {0x3a, 0x1f, 0x34, 0x08, 0x54, 0x54};
00168 
00169 /* The IP address used for demo (ping ...) */
00170 static uint8_t gIpAddress[4] = {192, 168, 1, 3};
00171 
00172 /* Set the default router's IP address. */
00173 static const uint8_t gGateWay[4] = {192, 168, 1, 2};
00174 /* The NetMask address */
00175 static const uint8_t gNetMask[4] = {255, 255, 255, 0};
00176 
00177 /*----------------------------------------------------------------------------
00178  *        Local functions
00179  *----------------------------------------------------------------------------*/
00180 /**
00181  * Process timing functions
00182  */
00183 static void timers_update(void)
00184 {
00185     static uint32_t last_time;
00186     uint32_t cur_time, time_diff, idxtimer;
00187     timers_info *ptmr_inf;
00188 
00189     cur_time = sys_get_ms();
00190 
00191     if (cur_time >= last_time)
00192         time_diff = cur_time - last_time;
00193     else
00194         time_diff = 0xFFFFFFFF - last_time + cur_time;
00195 
00196     if (time_diff) {
00197         last_time = cur_time;
00198 
00199         for (idxtimer = 0;
00200             idxtimer < (sizeof(timers_table) / sizeof(timers_info));
00201             idxtimer ++) {
00202             ptmr_inf = &timers_table[idxtimer];
00203             ptmr_inf->timer += time_diff;
00204 
00205             if (ptmr_inf->timer > ptmr_inf->timer_interval) {
00206                 if (ptmr_inf->timer_func)
00207                     ptmr_inf->timer_func();
00208 
00209                 ptmr_inf->timer -= ptmr_inf->timer_interval;
00210             }
00211         }
00212     }
00213 }
00214 
00215 /*----------------------------------------------------------------------------
00216  *        Exported functions
00217  *----------------------------------------------------------------------------*/
00218 
00219 /**
00220  * Gmac interrupt handler
00221  */
00222 void GMAC_Handler(void)
00223 {
00224     GMACD_Handler(&gGmacd, GMAC_QUE_0);
00225 }
00226 
00227 /**
00228  *  \brief gmac_lwip example entry point.
00229  *
00230  *  \return Unused (ANSI-C compatibility).
00231  */
00232 int main(void)
00233 {
00234     struct ip_addr ipaddr, netmask, gw;
00235     struct netif NetIf, *netif;
00236 
00237 #if LWIP_DHCP
00238     u8_t dhcp_state = DHCP_INIT;
00239 #endif
00240     uint8_t  OrigiGMacAddr[16];
00241     uint32_t i;
00242 
00243     /* Disable watchdog */
00244     WDT_Disable(WDT);
00245 
00246     printf("-- GMAC lwIP Example %s --\n\r", SOFTPACK_VERSION);
00247     printf("-- %s\n\r", BOARD_NAME);
00248     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00249             COMPILER_NAME);
00250 
00251     SCB_EnableICache();
00252     SCB_EnableDCache();
00253 
00254     /* Configure systick for 1 ms. */
00255     TimeTick_Configure();
00256 
00257     /* Configure TWI pins. */
00258     PIO_Configure(twiPins, PIO_LISTSIZE(twiPins));
00259     /* Enable TWI */
00260     PMC_EnablePeripheral(BOARD_ID_TWI_EEPROM);
00261     TWI_ConfigureMaster(BOARD_BASE_TWI_EEPROM, TWCK, BOARD_MCK);
00262     TWID_Initialize(&twid, BOARD_BASE_TWI_EEPROM);
00263     /* Display MAC & IP settings */
00264     TWID_Read(&twid, AT24MAC_SERIAL_NUM_ADD, 0x9A, 1, OrigiGMacAddr, PAGE_SIZE, 0);
00265 
00266     if ((OrigiGMacAddr[0] == 0xFC) && (OrigiGMacAddr[1] == 0xC2)
00267         && (OrigiGMacAddr[2] == 0x3D)) {
00268         for (i = 0; i < 6; i++)
00269             GMacAddress[i] = OrigiGMacAddr[i];
00270     }
00271 
00272     printf("-- MAC %x:%x:%x:%x:%x:%x\n\r",
00273             GMacAddress[0], GMacAddress[1], GMacAddress[2],
00274             GMacAddress[3], GMacAddress[4], GMacAddress[5]);
00275 
00276 #if !LWIP_DHCP
00277     printf(" - Host IP  %d.%d.%d.%d\n\r",
00278             gIpAddress[0], gIpAddress[1],
00279             gIpAddress[2], gIpAddress[3]);
00280     printf(" - GateWay IP  %d.%d.%d.%d\n\r",
00281             gGateWay[0], gGateWay[1], gGateWay[2], gGateWay[3]);
00282     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00283             gNetMask[0], gNetMask[1], gNetMask[2], gNetMask[3]);
00284 #else
00285     printf(" - DHCP Enabled\n\r");
00286 #endif
00287 
00288     /* Initialize system timing */
00289     sys_init_timing();
00290 
00291     /* Initialize lwIP modules */
00292     lwip_init();
00293 
00294     /* Initialize net interface for lwIP */
00295     gmacif_setmac((u8_t *)GMacAddress);
00296 
00297 #if !LWIP_DHCP
00298     IP4_ADDR(&gw, gGateWay[0], gGateWay[1], gGateWay[2], gGateWay[3]);
00299     IP4_ADDR(&ipaddr, gIpAddress[0], gIpAddress[1], gIpAddress[2], gIpAddress[3]);
00300     IP4_ADDR(&netmask, gNetMask[0], gNetMask[1], gNetMask[2], gNetMask[3]);
00301 #else
00302     IP4_ADDR(&gw, 0, 0, 0, 0);
00303     IP4_ADDR(&ipaddr, 0, 0, 0, 0);
00304     IP4_ADDR(&netmask, 0, 0, 0, 0);
00305 #endif
00306     netif = netif_add(&NetIf, &ipaddr, &netmask, &gw, NULL, gmacif_init, ip_input);
00307     netif_set_default(netif);
00308     netif_set_up(netif);
00309 
00310     /* Initialize http server application */
00311     if (ERR_OK != httpd_init()) {
00312         printf("httpd_init ERR_OK!");
00313         return -1;
00314     }
00315 
00316     printf("Type the IP address of the device in a web browser,\
00317         http://%d.%d.%d.%d \n\r",
00318             gIpAddress[0], gIpAddress[1], gIpAddress[2], gIpAddress[3]);
00319 
00320     while (1) {
00321         /* Run periodic tasks */
00322         timers_update();
00323         /* Run polling tasks */
00324         gmacif_poll(netif);
00325     }
00326 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines