Marcelo Rebonatto / Mbed 2 deprecated PM_COPIA

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetIf.cpp Source File

EthernetIf.cpp

00001 #include "Rede/EthernetIf.h"
00002 
00003 #include "lwip/inet.h"
00004 #include "lwip/netif.h"
00005 #include "netif/etharp.h"
00006 #include "lwip/dhcp.h"
00007 #include "arch/lpc17_emac.h"
00008 #include "lpc_phy.h"
00009 #include "lwip/tcpip.h"
00010 
00011 #include "mbed.h"
00012 
00013 struct netif EthernetIf::lpcNetif;
00014 bool EthernetIf::use_dhcp;
00015 ip_addr_t EthernetIf::ip_n;
00016 ip_addr_t EthernetIf::netmask_n;
00017 ip_addr_t EthernetIf::gw_n;
00018 Semaphore EthernetIf::tcpip_initialized(0);
00019 Semaphore EthernetIf::link_up(0);
00020 Semaphore EthernetIf::netif_up(0);
00021 bool EthernetIf::link_is_up(false);
00022 
00023 uint32_t EthernetIf::os_timer_cb_led[5]; 
00024 osTimerDef_t EthernetIf::os_timer_def_led = { (led_refresh), (os_timer_cb_led) };
00025 
00026 void EthernetIf::tcpip_init_done(void *arg)
00027 {
00028     //This function is called when the TCPIP stack finishes to initialize
00029     tcpip_initialized.release();
00030 }
00031 
00032 void EthernetIf::link_status_callback(struct netif *netif)
00033 {
00034     //Called when the interface link goes up or down
00035     if(netif_is_link_up(netif))
00036     {
00037         printf("Link up!!!\n");
00038         if(!link_is_up)
00039         {
00040             if(use_dhcp)
00041             {
00042                 dhcp_start(&lpcNetif);
00043             }
00044             else
00045             {
00046                 netif_set_addr(&lpcNetif,&ip_n,&netmask_n,&gw_n);
00047                 netif_set_up(&lpcNetif);
00048             }
00049         }
00050         link_up.release();
00051         link_is_up = true;
00052     }
00053     else
00054     {
00055         printf("Link down!!!\n");
00056         netif_set_down(&lpcNetif);
00057         link_is_up = false;
00058     }
00059 }
00060 
00061 void EthernetIf::interface_status_callback(struct netif *netif)
00062 {
00063     //called when the status of the interface is up or down
00064     if(netif_is_up(netif))
00065     {
00066         netif_up.release();
00067         printf("Interface Ethernet is up\n");
00068         printf("IP Address: %s\n",get_IpAddress());
00069         printf("Netmask: %s\n",get_Netmask());
00070         printf("Gateway: %s\n\n",get_Gateway());
00071     }
00072 }
00073 
00074 void EthernetIf::led_refresh(void const* arg)
00075 {
00076     if(LPC_GPIO1->FIOPIN & (1<<25))
00077         LPC_GPIO0->FIOSET |= (1<<4);
00078     else
00079         LPC_GPIO0->FIOCLR |= (1<<4);
00080 
00081     if(LPC_GPIO1->FIOPIN & (1<<26))
00082         LPC_GPIO0->FIOSET |= (1<<5);
00083     else
00084         LPC_GPIO0->FIOCLR |= (1<<5);
00085 }
00086 
00087 int EthernetIf::_init()
00088 {
00089 
00090     LPC_PINCON->PINSEL3 &= ~(3<<18);    //GPIO P1.25 - MBED LED LINK
00091     LPC_PINCON->PINSEL3 &= ~(3<<20);    //GPIO P1.26 - MBED LED SPEED
00092     LPC_PINCON->PINSEL0 &= ~(3<<8);     //GPIO P0.4 - ETHERNET GREEN LED
00093     LPC_PINCON->PINSEL0 &= ~(3<<10);    //GPIO P0.5 - ETHERNET YELLOW LED
00094     LPC_GPIO1->FIODIR &= ~((1<<25)|(1<<26));
00095     LPC_GPIO0->FIODIR |= ((1<<4)|(1<<5));
00096     
00097     tcpip_init(tcpip_init_done, NULL);
00098     tcpip_initialized.wait();
00099     
00100     memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
00101     netif_add(&lpcNetif, &ip_n, &netmask_n, &gw_n, NULL, lpc_enetif_init, tcpip_input);
00102     netif_set_default(&lpcNetif);
00103     
00104     netif_set_link_callback  (&lpcNetif, link_status_callback);
00105     netif_set_status_callback(&lpcNetif, interface_status_callback);
00106     
00107     return 0;
00108 }
00109 
00110 int EthernetIf::Initialize()
00111 {
00112     use_dhcp = true;
00113     
00114     return _init();
00115 }
00116 
00117 int EthernetIf::Initialize(char *ip,char *netmask,char *gw)
00118 {
00119     use_dhcp = false;
00120     inet_aton(ip,&ip_n);
00121     inet_aton(netmask,&netmask_n);
00122     inet_aton(gw,&gw_n);
00123     
00124     return _init();
00125 }
00126 
00127 int EthernetIf::Connect(unsigned int timeout /*= 1500*/)
00128 {
00129     NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
00130     NVIC_EnableIRQ(ENET_IRQn);
00131     
00132     netif_set_link_up(&lpcNetif);
00133     int inited;
00134     inited = netif_up.wait(timeout);
00135     
00136     osTimerId led_timer = osTimerCreate(osTimer(led), osTimerPeriodic, NULL);
00137     osTimerStart(led_timer, 100);
00138     
00139     return (inited > 0) ? (0) : (-1);
00140 }
00141 
00142 int EthernetIf::Disconnect()
00143 {
00144     if (use_dhcp)
00145     {
00146         dhcp_release(&lpcNetif);
00147         dhcp_stop(&lpcNetif);
00148     }
00149     else
00150     {
00151         netif_set_down(&lpcNetif);
00152     }
00153     
00154     NVIC_DisableIRQ(ENET_IRQn);
00155     
00156     return 0;
00157 }
00158 
00159 bool EthernetIf::is_LinkUp()
00160 {
00161     if(netif_is_link_up(&lpcNetif))
00162         return true;
00163     else
00164         return false;
00165 }
00166 
00167 bool EthernetIf::is_InterfaceUp()
00168 {
00169     if(netif_is_up(&lpcNetif))
00170         return true;
00171     else
00172         return false;
00173 }
00174 
00175 char* EthernetIf::get_IpAddress()
00176 {
00177     return ipaddr_ntoa(&lpcNetif.ip_addr);
00178 }
00179 
00180 char* EthernetIf::get_Netmask()
00181 {
00182     return ipaddr_ntoa(&lpcNetif.netmask);
00183 }
00184 
00185 char* EthernetIf::get_Gateway()
00186 {
00187     return ipaddr_ntoa(&lpcNetif.gw);
00188 }