- Main Page
- Data Structures
- Files

# ethernet.c

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00015 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00016  *
    00017  * Redistribution and use in source and binary forms, with or without
    00018  * modification, are permitted provided that the following conditions are met:
    00019  *
    00020  * 1. Redistributions of source code must retain the above copyright notice, this
    00021  * list of conditions and the following disclaimer.
    00022  *
    00023  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00024  * this list of conditions and the following disclaimer in the documentation
    00025  * and/or other materials provided with the distribution.
    00026  *
    00027  * 3. The name of Atmel may not be used to endorse or promote products derived
    00028  * from this software without specific prior written permission.
    00029  *
    00030  * 4. This software may only be redistributed and used in connection with an Atmel
    00031  * AVR product.
    00032  *
    00033  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00034  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00035  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00036  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
    00037  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00038  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00039  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00040  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00041  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00042  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
    00043  *
    00044  */
    00045
    00046
    00047 #include <string.h>
    00048
    00049 #include "gpio.h" // Have to include gpio.h before FreeRTOS.h as long as FreeRTOS
    00050                   // redefines the inline keyword to empty.
    00051
    00052 /* Scheduler include files. */
    00053 #include "FreeRTOS.h"
    00054 #include "task.h"
    00055
    00056 /* Demo program include files. */
    00057 #include "partest.h"
    00058 #include "serial.h"
    00059 #include "conf_lwip_threads.h"
    00060
    00061 /* ethernet includes */
    00062 #include "ethernet.h"
    00063 #include "conf_eth.h"
    00064 #include "macb.h"
    00065
    00066 #if (HTTP_USED == 1)
    00067   #include "BasicWEB.h"
    00068 #endif
    00069
    00070 #if (SSL_USED == 1)
    00071   #include "BasicSSL.h"
    00072 #endif
    00073
    00074 /* lwIP includes */
    00075 #include "lwip/sys.h"
    00076 #include "lwip/api.h"
    00077 #include "lwip/tcpip.h"
    00078 #include <lwip/dhcp.h>
    00079 #include <lwip/dns.h>
    00080 #include "lwip/memp.h"
    00081 #include "lwip/stats.h"
    00082 #include "netif/loopif.h"
    00083
    00084 #include "print_funcs.h"
    00085
    00086 //_____ M A C R O S ________________________________________________________
    00087
    00088
    00089 //_____ D E F I N I T I O N S ______________________________________________
    00090
    00091 /* global variable containing MAC Config (hw addr, IP, GW, ...) */
    00092 struct netif MACB_if;
    00093
    00094 //_____ D E C L A R A T I O N S ____________________________________________
    00095
    00096 /* Initialisation required by lwIP. */
    00097 static void prvlwIPInit( void );
    00098
    00099 /* Initialisation of ethernet interfaces by reading config file */
    00100 static void prvEthernetConfigureInterface(void * param);
    00101 static void prvInitDNS( void );
    00102 void dns_found(const char *name, struct ip_addr *addr, void *arg);
    00103 void status_callback(struct netif *netif);
    00104
    00105 void vStartEthernetTaskLauncher( unsigned portBASE_TYPE uxPriority )
    00106 {
    00107   /* Spawn the Sentinel task. */
    00108   xTaskCreate( vStartEthernetTask, ( const signed portCHAR * )"ETHLAUNCH",
    00109                configMINIMAL_STACK_SIZE, NULL, uxPriority, ( xTaskHandle * )NULL );
    00110 }
    00111
    00117 portTASK_FUNCTION( vStartEthernetTask, pvParameters )
    00118 {
    00119    static const gpio_map_t MACB_GPIO_MAP =
    00120    {
    00121       {AVR32_MACB_MDC_0_PIN,    AVR32_MACB_MDC_0_FUNCTION   },
    00122       {AVR32_MACB_MDIO_0_PIN,   AVR32_MACB_MDIO_0_FUNCTION  },
    00123       {AVR32_MACB_RXD_0_PIN,    AVR32_MACB_RXD_0_FUNCTION   },
    00124       {AVR32_MACB_TXD_0_PIN,    AVR32_MACB_TXD_0_FUNCTION   },
    00125       {AVR32_MACB_RXD_1_PIN,    AVR32_MACB_RXD_1_FUNCTION   },
    00126       {AVR32_MACB_TXD_1_PIN,    AVR32_MACB_TXD_1_FUNCTION   },
    00127       {AVR32_MACB_TX_EN_0_PIN,  AVR32_MACB_TX_EN_0_FUNCTION },
    00128       {AVR32_MACB_RX_ER_0_PIN,  AVR32_MACB_RX_ER_0_FUNCTION },
    00129       {AVR32_MACB_RX_DV_0_PIN,  AVR32_MACB_RX_DV_0_FUNCTION },
    00130       {AVR32_MACB_TX_CLK_0_PIN, AVR32_MACB_TX_CLK_0_FUNCTION}
    00131    };
    00132
    00133    // Assign GPIO to MACB
    00134    gpio_enable_module(MACB_GPIO_MAP, sizeof(MACB_GPIO_MAP) / sizeof(MACB_GPIO_MAP[0]));
    00135
    00136    /* Setup lwIP. */
    00137    prvlwIPInit();
    00138
    00139 #if (HTTP_USED == 1)
    00140    /* Create the WEB server task.  This uses the lwIP RTOS abstraction layer.*/
    00141    sys_thread_new( "WEB", vBasicWEBServer, ( void * ) NULL,
    00142                    lwipBASIC_WEB_SERVER_STACK_SIZE,
    00143                    lwipBASIC_WEB_SERVER_PRIORITY );
    00144 #endif
    00145
    00146 #if (SSL_USED == 1)
    00147    /* Create the SSL Client task.  This uses the lwIP RTOS abstraction layer.*/
    00148    sys_thread_new( "SSL", vBasicSSLClient, ( void * ) NULL,
    00149                    lwipBASIC_SSL_CLIENT_STACK_SIZE,
    00150                    lwipBASIC_SSL_CLIENT_PRIORITY );
    00151 #endif
    00152
    00153   // Kill this task.
    00154   vTaskDelete(NULL);
    00155 }
    00156
    00157
    00159 static void tcpip_init_done(void *arg)
    00160 {
    00161   sys_sem_t *sem;
    00162   sem = (sys_sem_t *)arg;
    00163   sys_sem_signal(*sem); // Signal the waiting thread that the TCP/IP init is done.
    00164 }
    00165
    00169 void status_callback(struct netif *netif)
    00170 {
    00171   if (netif_is_up(netif)) {
    00172     print_dbg("LwIP: status_callback==UP, local interface IP is ");
    00173     print_dbg(inet_ntoa(*(struct in_addr*)&(netif->ip_addr)));
    00174     print_dbg("\n");
    00175   } else {
    00176     print_dbg("status_callback==DOWN\n");
    00177   }
    00178 #if ( LWIP_NETIF_HOSTNAME == 1)
    00179   print_dbg("LwIP: Host Name Is : ");
    00180   print_dbg(MACB_if.hostname);
    00181   print_dbg("\n");
    00182 #endif
    00183 }
    00184
    00188 void dns_found(const char *name, struct ip_addr *addr, void *arg)
    00189 {
    00190   LWIP_UNUSED_ARG(arg);
    00191   print_dbg(name);
    00192   print_dbg(":");
    00193   print_dbg(addr?inet_ntoa(*(struct in_addr*)addr):"<not found>\n");
    00194 }
    00195
    00199 static void prvlwIPInit( void )
    00200 {
    00201   sys_sem_t sem;
    00202
    00203
    00204   sem = sys_sem_new(0); // Create a new semaphore.
    00205   tcpip_init(tcpip_init_done, &sem);
    00206   sys_sem_wait(sem);    // Block until the lwIP stack is initialized.
    00207   sys_sem_free(sem);    // Free the semaphore.
    00208
    00209   /* Set hw and IP parameters, initialize MACB too */
    00210   prvEthernetConfigureInterface(NULL);
    00211
    00212 }
    00213
    00217 #if LWIP_DNS
    00218 static void prvInitDNS( void ) {
    00219     char* dnsname=DNS_SERVER_NAME;
    00220     struct ip_addr dnsresp;
    00221     if (dns_gethostbyname(dnsname, &dnsresp, dns_found, 0) == ERR_OK) {
    00222         dns_found(dnsname, &dnsresp, 0);
    00223     }
    00224 }
    00225 #endif
    00226
    00230 static void prvEthernetConfigureInterface(void * param)
    00231 {
    00232    struct ip_addr    xIpAddr, xNetMask, xGateway;
    00233    extern err_t      ethernetif_init( struct netif *netif );
    00234    unsigned portCHAR MacAddress[6];
    00235
    00236    /* Default MAC addr. */
    00237    MacAddress[0] = ETHERNET_CONF_ETHADDR0;
    00238    MacAddress[1] = ETHERNET_CONF_ETHADDR1;
    00239    MacAddress[2] = ETHERNET_CONF_ETHADDR2;
    00240    MacAddress[3] = ETHERNET_CONF_ETHADDR3;
    00241    MacAddress[4] = ETHERNET_CONF_ETHADDR4;
    00242    MacAddress[5] = ETHERNET_CONF_ETHADDR5;
    00243
    00244    /* pass the MAC address to MACB module */
    00245    vMACBSetMACAddress( MacAddress );
    00246
    00247 #if LWIP_DHCP
    00248
    00249   xIpAddr.addr  = 0;
    00250   xNetMask.addr = 0;
    00251   xNetMask.addr = 0;
    00252
    00253   /* add data to netif */
    00254   netif_add( &MACB_if, &xIpAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input );
    00255
    00256   /* make it the default interface */
    00257   netif_set_default( &MACB_if );
    00258
    00259   /* Setup callback function for netif status change */
    00260   netif_set_status_callback(&MACB_if, status_callback);
    00261
    00262   /* bring it up */
    00263   dhcp_start( &MACB_if );
    00264   print_dbg("LwIP: DHCP Started ...\n");
    00265
    00266 #if LWIP_DNS
    00267   /* Init DNS */
    00268   prvInitDNS();
    00269   print_dbg("LwIP: DNS Started ...\n");
    00270 #endif
    00271
    00272 #else
    00273    /* Default ip addr. */
    00274    IP4_ADDR( &xIpAddr,ETHERNET_CONF_IPADDR0,ETHERNET_CONF_IPADDR1,ETHERNET_CONF_IPADDR2,ETHERNET_CONF_IPADDR3 );
    00275
    00276    /* Default Subnet mask. */
    00277    IP4_ADDR( &xNetMask,ETHERNET_CONF_NET_MASK0,ETHERNET_CONF_NET_MASK1,ETHERNET_CONF_NET_MASK2,ETHERNET_CONF_NET_MASK3 );
    00278
    00279    /* Default Gw addr. */
    00280    IP4_ADDR( &xGateway,ETHERNET_CONF_GATEWAY_ADDR0,ETHERNET_CONF_GATEWAY_ADDR1,ETHERNET_CONF_GATEWAY_ADDR2,ETHERNET_CONF_GATEWAY_ADDR3 );
    00281
    00282    /* add data to netif */
    00283    netif_add( &MACB_if, &xIpAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input );
    00284
    00285    /* make it the default interface */
    00286    netif_set_default( &MACB_if );
    00287
    00288    /* bring it up */
    00289    netif_set_up( &MACB_if );
    00290
    00291
    00292 #endif
    00293
    00294
    00295 }