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_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 SAMV71 Xplained Ultra board or SAME70 Xplained 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 board.
00056  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00057  *  -# On the computer, open and configure a terminal application
00058  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00059  *    - 115200 baud rate
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  *  \section References
00090  *  - gmac_uip_webserver/main.c
00091  *  - gmacb.h
00092  *  - gmacd.h
00093  *  - gmac.h
00094  *  - uip.h
00095  */
00096 
00097 /** \file
00098  *
00099  *  This file contains all the specific code for the gmac_uip_webserver example.
00100  *
00101  */
00102 
00103 /*----------------------------------------------------------------------------
00104  *        Headers
00105  *----------------------------------------------------------------------------*/
00106 
00107 #include "board.h"
00108 
00109 #include "uip.h"
00110 #include "uip_arp.h"
00111 #include "gmac_tapdev.h"
00112 #include "timer.h"
00113 
00114 /*---------------------------------------------------------------------------
00115  *         Variables
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 /* uIP buffer : The ETH header */
00134 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00135 /** TWI driver instance.*/
00136 static Twid twid;
00137 
00138 /** The PINs for TWI*/
00139 static const Pin twiPins[]      = BOARD_PINS_TWI_EEPROM;
00140 /* The MAC address used for demo */
00141 static struct uip_eth_addr GMacAddress = {{0x3a, 0x1f, 0x34, 0x09, 0x54, 0x54}};
00142 
00143 /* The IP address used for demo (ping ...) */
00144 static const uint8_t HostIpAddress[4] = {192, 168, 1, 3};
00145 
00146 /* Set the default router's IP address. */
00147 static const uint8_t RoutIpAddress[4] = {192, 168, 1, 2 };
00148 
00149 /* The NetMask address */
00150 static const uint8_t NetMask[4] = {255, 255, 255, 0};
00151 
00152 /*----------------------------------------------------------------------------
00153  *        Local functions
00154  *----------------------------------------------------------------------------*/
00155 
00156 /**
00157  * Initialize demo application
00158  */
00159 static void _app_init(void)
00160 {
00161     printf("P: webserver application init\n\r");
00162     httpd_init();
00163     printf("Type the IP address of the device in a web browser, \
00164         http://%d.%d.%d.%d \n\r",
00165             HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00166 
00167 #ifdef __DHCPC_H__
00168     printf("P: DHCPC Init\n\r");
00169     dhcpc_init(MacAddress.addr, 6);
00170 #endif
00171 }
00172 
00173 /*----------------------------------------------------------------------------
00174  *        Exported functions
00175  *----------------------------------------------------------------------------*/
00176 
00177 /**
00178  * uip_log: Global function for uIP to use.
00179  * \param m Pointer to string that logged
00180  */
00181 void uip_log(char *m)
00182 {
00183     TRACE_INFO("-uIP log- %s\n\r", m);
00184 }
00185 
00186 #ifdef __DHCPC_H__
00187 /**
00188  * dhcpc_configured: Global function for uIP DHCPC to use,
00189  * notification of DHCP configuration.
00190  * \param s Pointer to DHCP state instance
00191  */
00192 void dhcpc_configured(const struct dhcpc_state *s)
00193 {
00194     u8_t *pAddr;
00195 
00196     printf("\n\r");
00197     printf("=== DHCP Configurations ===\n\r");
00198     pAddr = (u8_t *)s->ipaddr;
00199     printf("- IP   : %d.%d.%d.%d\n\r",
00200             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00201     pAddr = (u8_t *)s->netmask;
00202     printf("- Mask : %d.%d.%d.%d\n\r",
00203             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00204     pAddr = (u8_t *)s->default_router;
00205     printf("- GW   : %d.%d.%d.%d\n\r",
00206             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00207     pAddr = (u8_t *)s->dnsaddr;
00208     printf("- DNS  : %d.%d.%d.%d\n\r",
00209             pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
00210     printf("===========================\n\r\n");
00211     uip_sethostaddr(s->ipaddr);
00212     uip_setnetmask(s->netmask);
00213     uip_setdraddr(s->default_router);
00214 
00215 #ifdef __RESOLV_H__
00216     resolv_conf(s->dnsaddr);
00217 #else
00218     printf("DNS NOT enabled in the demo\n\r");
00219 #endif
00220 }
00221 #endif
00222 
00223 /**
00224  *  \brief gmac_uip_webserver example entry point.
00225  *
00226  *  \return Unused (ANSI-C compatibility).
00227  */
00228 int main(void)
00229 {
00230     uip_ipaddr_t ipaddr;
00231     struct timer periodic_timer, arp_timer;
00232     uint32_t i;
00233     struct uip_eth_addr OrigiGMacAddr;
00234 
00235     /* Disable watchdog */
00236     WDT_Disable(WDT);
00237 
00238     /* Enable I and D cache */
00239     SCB_EnableICache();
00240     SCB_EnableDCache();
00241     TimeTick_Configure();
00242     printf("-- GMAC uIP Web Server Example %s --\n\r", SOFTPACK_VERSION);
00243     printf("-- %s\n\r", BOARD_NAME);
00244     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00245             COMPILER_NAME);
00246 
00247     /* Configure TWI pins. */
00248     PIO_Configure(twiPins, PIO_LISTSIZE(twiPins));
00249     /* Enable TWI */
00250     PMC_EnablePeripheral(BOARD_ID_TWI_EEPROM);
00251     TWI_ConfigureMaster(BOARD_BASE_TWI_EEPROM, TWCK, BOARD_MCK);
00252     TWID_Initialize(&twid, BOARD_BASE_TWI_EEPROM);
00253     /* Display MAC & IP settings */
00254     TWID_Read(&twid, AT24MAC_SERIAL_NUM_ADD, 0x9A, 1, OrigiGMacAddr.addr, PAGE_SIZE,
00255               0);
00256 
00257     if ((OrigiGMacAddr.addr[0] == 0xFC) && (OrigiGMacAddr.addr[1] == 0xC2)
00258         && (OrigiGMacAddr.addr[2] == 0x3D)) {
00259         for (i = 0; i < 6; i++)
00260             GMacAddress.addr[i] = OrigiGMacAddr.addr[i];
00261     }
00262 
00263     printf("-- MAC %x:%x:%x:%x:%x:%x\n\r",
00264             GMacAddress.addr[0], GMacAddress.addr[1], GMacAddress.addr[2],
00265             GMacAddress.addr[3], GMacAddress.addr[4], GMacAddress.addr[5]);
00266 
00267 #ifndef __DHCPC_H__
00268     printf(" - Host IP  %d.%d.%d.%d\n\r",
00269             HostIpAddress[0], HostIpAddress[1], HostIpAddress[2], HostIpAddress[3]);
00270     printf(" - Router IP  %d.%d.%d.%d\n\r",
00271             RoutIpAddress[0], RoutIpAddress[1], RoutIpAddress[2], RoutIpAddress[3]);
00272     printf(" - Net Mask  %d.%d.%d.%d\n\r",
00273             NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00274 #endif
00275 
00276     /* System devices initialize */
00277     gmac_tapdev_setmac((uint8_t *)GMacAddress.addr);
00278     gmac_tapdev_init();
00279     clock_init();
00280     timer_set(&periodic_timer, CLOCK_SECOND / 2);
00281     timer_set(&arp_timer, CLOCK_SECOND * 10);
00282 
00283     /* Initialize uIP */
00284     uip_init();
00285 
00286 #ifdef __DHCPC_H__
00287     printf("P: DHCP Supported\n\r");
00288     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00289     uip_sethostaddr(ipaddr);
00290     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00291     uip_setdraddr(ipaddr);
00292     uip_ipaddr(ipaddr, 0, 0, 0, 0);
00293     uip_setnetmask(ipaddr);
00294 #else
00295     /* Set the IP address of this host */
00296     uip_ipaddr(ipaddr, HostIpAddress[0], HostIpAddress[1],
00297                 HostIpAddress[2], HostIpAddress[3]);
00298     uip_sethostaddr(ipaddr);
00299 
00300     uip_ipaddr(ipaddr, RoutIpAddress[0], RoutIpAddress[1],
00301                 RoutIpAddress[2], RoutIpAddress[3]);
00302     uip_setdraddr(ipaddr);
00303 
00304     uip_ipaddr(ipaddr, NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
00305     uip_setnetmask(ipaddr);
00306 #endif
00307     uip_setethaddr(GMacAddress);
00308     _app_init();
00309 
00310     while (1) {
00311         uip_len = gmac_tapdev_read();
00312 
00313         if (uip_len > 0) {
00314             if (BUF->type == htons(UIP_ETHTYPE_IP)) {
00315                 uip_arp_ipin();
00316                 uip_input();
00317 
00318                 /* If the above function invocation resulted in data that
00319                     should be sent out on the network, the global variable
00320                     uip_len is set to a value > 0. */
00321                 if (uip_len > 0) {
00322                     uip_arp_out();
00323                     gmac_tapdev_send();
00324                 }
00325             } else if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
00326                     uip_arp_arpin();
00327 
00328                 /* If the above function invocation resulted in data that
00329                     should be sent out on the network, the global variable
00330                     uip_len is set to a value > 0. */
00331                 if (uip_len > 0)
00332                     gmac_tapdev_send();
00333             }
00334         } else if (timer_expired(&periodic_timer)) {
00335             timer_reset(&periodic_timer);
00336 
00337             for (i = 0; i < UIP_CONNS; i++) {
00338                 uip_periodic(i);
00339 
00340                 /* If the above function invocation resulted in data that
00341                     should be sent out on the network, the global variable
00342                     uip_len is set to a value > 0. */
00343                 if (uip_len > 0) {
00344                     uip_arp_out();
00345                     gmac_tapdev_send();
00346                 }
00347             }
00348 
00349 #if UIP_UDP
00350 
00351             for (i = 0; i < UIP_UDP_CONNS; i++) {
00352                 uip_udp_periodic(i);
00353                 memory_sync();
00354 
00355                 /* If the above function invocation resulted in data that
00356                     should be sent out on the network, the global variable
00357                     uip_len is set to a value > 0. */
00358                 if (uip_len > 0) {
00359                     uip_arp_out();
00360                     gmac_tapdev_send();
00361                 }
00362             }
00363 
00364 #endif /* UIP_UDP */
00365 
00366             /* Call the ARP timer function every 10 seconds. */
00367             if (timer_expired(&arp_timer)) {
00368                 timer_reset(&arp_timer);
00369                 uip_arp_timer();
00370             }
00371         }
00372     }
00373 }
00374 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines