Ethernet Network Library

Dependencies:   NetworkingCoreLib

Dependents:   EthernetHTTPClientTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetNetwork.cpp Source File

EthernetNetwork.cpp

00001 /* Ethernetnetwork.cpp */
00002 /*
00003  Copyright (C) 2012 ARM Limited.
00004 
00005  Permission is hereby granted, free of charge, to any person obtaining a copy of
00006  this software and associated documentation files (the "Software"), to deal in
00007  the Software without restriction, including without limitation the rights to
00008  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
00009  of the Software, and to permit persons to whom the Software is furnished to do
00010  so, subject to the following conditions:
00011 
00012  The above copyright notice and this permission notice shall be included in all
00013  copies or substantial portions of the Software.
00014 
00015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021  SOFTWARE.
00022  */
00023 
00024 #define __DEBUG__ 4
00025 #ifndef __MODULE__
00026 #define __MODULE__ "EthernetNetwork.cpp"
00027 #endif
00028 
00029 #include "core/fwk.h"
00030 
00031 #include "EthernetNetwork.h"
00032 
00033 #include "lwip/inet.h"
00034 #include "lwip/netif.h"
00035 #include "netif/etharp.h"
00036 #include "lwip/dhcp.h"
00037 #include "lwip/arch/lpc17_emac.h"
00038 #include "lpc_phy.h" /* For the PHY monitor support */
00039 #include "lwip/tcpip.h"
00040 
00041 #include "mbed.h"
00042 
00043 #if NET_ETHERNET
00044 
00045 EthernetNetwork::EthernetNetwork() : LwIPInterface(), m_supervisor(&EthernetNetwork::phySupervisorCb, osTimerPeriodic, this), m_lpcNetif(), m_useDHCP(false)
00046 {
00047 
00048 }
00049 
00050 int EthernetNetwork::init() //With DHCP
00051 {
00052   ip_addr_t ip_n, mask_n, gateway_n;
00053 
00054   m_useDHCP = true;
00055 
00056   DBG("Initializing LwIP");
00057   LwIPInterface::init(); //Init LwIP, NOT including PPP
00058 
00059   DBG("DHCP IP assignment");
00060 
00061   memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif));
00062   netif_add(&m_lpcNetif, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/);
00063   netif_set_default(&m_lpcNetif);
00064 
00065   return OK;
00066 }
00067 
00068 int EthernetNetwork::init(const char* ip, const char* mask, const char* gateway,
00069     const char* dns1, const char* dns2) //No DHCP
00070 {
00071   ip_addr_t ip_n, mask_n, gateway_n, dns1_n, dns2_n;
00072 
00073   m_useDHCP = false;
00074 
00075   DBG("Initializing LwIP");
00076   LwIPInterface::init(); //Init LwIP, NOT including PPP
00077 
00078   DBG("Static IP assignment");
00079   inet_aton(ip, &ip_n);
00080   inet_aton(mask, &mask_n);
00081   inet_aton(gateway, &gateway_n);
00082   inet_aton(dns1, &dns1_n);
00083   inet_aton(dns2, &dns2_n);
00084 
00085   memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif));
00086   netif_add(&m_lpcNetif, &ip_n, &mask_n, &gateway_n, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/);
00087   netif_set_default(&m_lpcNetif);
00088 
00089   return OK;
00090 }
00091 
00092 /*virtual*/int EthernetNetwork::connect()
00093 {
00094   m_supervisor.start(250);
00095   netif_set_up(&m_lpcNetif);
00096 
00097   DBG("Enable MAC interrupts");
00098   NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
00099   NVIC_EnableIRQ(ENET_IRQn);
00100 
00101   if(m_useDHCP)
00102   {
00103     dhcp_start(&m_lpcNetif);
00104   }
00105 
00106   return OK;
00107 }
00108 
00109 /*virtual*/int EthernetNetwork::disconnect()
00110 {
00111   if(m_useDHCP)
00112   {
00113     dhcp_stop(&m_lpcNetif);
00114   }
00115 
00116   netif_set_down(&m_lpcNetif);
00117 
00118   DBG("Disable MAC interrupts");
00119   NVIC_DisableIRQ(ENET_IRQn);
00120 
00121   m_supervisor.stop();
00122 
00123   return OK;
00124 }
00125 
00126 /*static*/ void EthernetNetwork::phySupervisorCb(void const* ctx)
00127 {
00128   EthernetNetwork* pIf = (EthernetNetwork*) ctx;
00129   /* Call the PHY status update state machine once in a while
00130    to keep the link status up-to-date */
00131   if (lpc_phy_sts_sm(&pIf->m_lpcNetif) != 0)
00132   {
00133     /* Set the state of the LED to on if the ethernet link is
00134      active or off is disconnected. */
00135     if (pIf->m_lpcNetif.flags & NETIF_FLAG_LINK_UP)
00136     {
00137       pIf->m_linkUp = true;
00138     }
00139     else
00140     {
00141       pIf->m_linkUp = false;
00142     }
00143   }
00144 }
00145 
00146 #endif
00147