# ethernet.c File Reference

## Detailed Description

ethernet management for AVR32 UC3.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices can be used.
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file ethernet.c.

`#include <string.h>`
`#include "gpio.h"`
`#include "FreeRTOS.h"`
`#include "task.h"`
`#include "partest.h"`
`#include "serial.h"`
`#include "conf_lwip_threads.h"`
`#include "ethernet.h"`
`#include "conf_eth.h"`
`#include "macb.h"`
`#include "lwip/sys.h"`
`#include "lwip/api.h"`
`#include "lwip/tcpip.h"`
`#include <lwip/dhcp.h>`
`#include <lwip/dns.h>`
`#include "lwip/memp.h"`
`#include "lwip/stats.h"`
`#include "netif/loopif.h"`
`#include "print_funcs.h"`

Go to the source code of this file.

## Functions
void | dns_found (const char *name, struct ip_addr *addr, void *arg)
| dns error message, tel if found or not
| portTASK_FUNCTION (vStartEthernetTask, pvParameters)
| create ethernet task, for ethernet management.
static void | prvEthernetConfigureInterface (void *param)
| function that look for DNS
static void | prvInitDNS (void)
static void | prvlwIPInit (void)
| start lwIP layer.
void | status_callback (struct netif *netif)
| status callback used to pront address givern by DHCP
static void | tcpip_init_done (void *arg)
| Callback executed when the TCP/IP init is done.
void | vStartEthernetTaskLauncher (unsigned portBASE_TYPE uxPriority)
| Create the vStartEthernetTask task.
## Variables
struct netif | MACB_if

## Function Documentation

void dns_found | ( | const char * | name,
|  | struct ip_addr * | addr,
|  | void * | arg |
| ) |  |  |

dns error message, tel if found or not

Definition at line 188 of file ethernet.c.

    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 }

portTASK_FUNCTION | ( | vStartEthernetTask | ,
|  | pvParameters |  |
| ) |  |  |

create ethernet task, for ethernet management.

Parameters:

  | uxPriority | Input. priority for the task, it should be low

Definition at line 117 of file ethernet.c.

References lwipBASIC_SSL_CLIENT_PRIORITY, lwipBASIC_SSL_CLIENT_STACK_SIZE, lwipBASIC_WEB_SERVER_PRIORITY, lwipBASIC_WEB_SERVER_STACK_SIZE, prvlwIPInit(), and sys_thread_new().

    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 }

static void prvEthernetConfigureInterface | ( | void * | param | ) | `[static]`

function that look for DNS

set ethernet config

Definition at line 230 of file ethernet.c.

References ETHERNET_CONF_ETHADDR0, ETHERNET_CONF_ETHADDR1, ETHERNET_CONF_ETHADDR2, ETHERNET_CONF_ETHADDR3, ETHERNET_CONF_ETHADDR4, ETHERNET_CONF_ETHADDR5, ETHERNET_CONF_GATEWAY_ADDR0, ETHERNET_CONF_GATEWAY_ADDR1, ETHERNET_CONF_GATEWAY_ADDR2, ETHERNET_CONF_GATEWAY_ADDR3, ETHERNET_CONF_IPADDR0, ETHERNET_CONF_IPADDR1, ETHERNET_CONF_IPADDR2, ETHERNET_CONF_IPADDR3, ETHERNET_CONF_NET_MASK0, ETHERNET_CONF_NET_MASK1, ETHERNET_CONF_NET_MASK2, ETHERNET_CONF_NET_MASK3, ethernetif_init(), MACB_if, prvInitDNS(), and status_callback().

Referenced by prvlwIPInit().

    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 }

static void prvInitDNS | ( | void |  | ) | `[static]`

Referenced by prvEthernetConfigureInterface().

static void prvlwIPInit | ( | void |  | ) | `[static]`

start lwIP layer.

Definition at line 199 of file ethernet.c.

References prvEthernetConfigureInterface(), sys_sem_free(), sys_sem_new(), and tcpip_init_done().

Referenced by portTASK_FUNCTION().

    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 }

void status_callback | ( | struct netif * | netif | ) |

status callback used to pront address givern by DHCP

Definition at line 169 of file ethernet.c.

References MACB_if.

Referenced by prvEthernetConfigureInterface().

    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 }

static void tcpip_init_done | ( | void * | arg | ) | `[static]`

Callback executed when the TCP/IP init is done.

Definition at line 159 of file ethernet.c.

References sys_sem_signal().

Referenced by prvlwIPInit().

    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 }

void vStartEthernetTaskLauncher | ( | unsigned portBASE_TYPE | uxPriority | ) |

Create the vStartEthernetTask task.

Parameters:

  | uxPriority | Input; priority of the task to create.

Definition at line 105 of file ethernet.c.

References configMINIMAL_STACK_SIZE.

Referenced by main().

    00106 {
    00107   /* Spawn the Sentinel task. */
    00108   xTaskCreate( vStartEthernetTask, ( const signed portCHAR * )"ETHLAUNCH",
    00109                configMINIMAL_STACK_SIZE, NULL, uxPriority, ( xTaskHandle * )NULL );
    00110 }

## Variable Documentation

struct netif MACB_if

Definition at line 92 of file ethernet.c.

Referenced by prvEthernetConfigureInterface(), and status_callback().