uio

Dependencies:   Socket_youcef2 lwip-eth lwip-sys lwip

Fork of EthernetInterface by mbed official

Committer:
emilmont
Date:
Mon Jul 23 11:53:01 2012 +0000
Revision:
14:cec293071eed
Parent:
10:8d1c7a87c17b
Child:
15:fd9597f1b81b
tidyup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 4:9a52c802be61 1 /* EthernetInterface.cpp */
donatien 3:f5776537f27f 2 /* Copyright (C) 2012 mbed.org, MIT License
donatien 3:f5776537f27f 3 *
donatien 3:f5776537f27f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien 3:f5776537f27f 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
donatien 3:f5776537f27f 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
donatien 3:f5776537f27f 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
donatien 3:f5776537f27f 8 * furnished to do so, subject to the following conditions:
donatien 3:f5776537f27f 9 *
donatien 3:f5776537f27f 10 * The above copyright notice and this permission notice shall be included in all copies or
donatien 3:f5776537f27f 11 * substantial portions of the Software.
donatien 3:f5776537f27f 12 *
donatien 3:f5776537f27f 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien 3:f5776537f27f 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien 3:f5776537f27f 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien 3:f5776537f27f 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 3:f5776537f27f 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
donatien 3:f5776537f27f 18 */
donatien 3:f5776537f27f 19 #include "EthernetInterface.h"
donatien 3:f5776537f27f 20
donatien 3:f5776537f27f 21 #include "lwip/inet.h"
donatien 3:f5776537f27f 22 #include "lwip/netif.h"
donatien 3:f5776537f27f 23 #include "netif/etharp.h"
donatien 3:f5776537f27f 24 #include "lwip/dhcp.h"
donatien 3:f5776537f27f 25 #include "arch/lpc17_emac.h"
emilmont 14:cec293071eed 26 #include "lpc_phy.h"
donatien 3:f5776537f27f 27 #include "lwip/tcpip.h"
donatien 3:f5776537f27f 28
donatien 3:f5776537f27f 29 #include "mbed.h"
donatien 3:f5776537f27f 30
emilmont 14:cec293071eed 31 /* TCP/IP and Network Interface Initialisation */
emilmont 14:cec293071eed 32 static struct netif lpcNetif;
donatien 3:f5776537f27f 33
emilmont 14:cec293071eed 34 static Semaphore tcpip_inited(0);
emilmont 14:cec293071eed 35 static Semaphore netif_inited(0);
donatien 3:f5776537f27f 36
emilmont 14:cec293071eed 37 static char ip_addr[16];
emilmont 14:cec293071eed 38 static bool connected;
emilmont 14:cec293071eed 39 static bool use_dhcp = false;
donatien 3:f5776537f27f 40
emilmont 14:cec293071eed 41 static void tcpip_init_done(void *arg) {
emilmont 14:cec293071eed 42 tcpip_inited.release();
emilmont 14:cec293071eed 43 }
emilmont 14:cec293071eed 44 static void netif_status_callback(struct netif *netif) {
emilmont 14:cec293071eed 45 strcpy(ip_addr, inet_ntoa(netif->ip_addr));
emilmont 14:cec293071eed 46 connected = netif_is_up(netif) ? true : false;
emilmont 14:cec293071eed 47 netif_inited.release();
emilmont 14:cec293071eed 48 }
emilmont 14:cec293071eed 49 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
emilmont 14:cec293071eed 50 tcpip_init(tcpip_init_done, NULL);
emilmont 14:cec293071eed 51 tcpip_inited.wait();
emilmont 14:cec293071eed 52
emilmont 14:cec293071eed 53 memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
emilmont 14:cec293071eed 54 netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, ethernet_input);
emilmont 14:cec293071eed 55 netif_set_default(&lpcNetif);
emilmont 14:cec293071eed 56 netif_set_status_callback(&lpcNetif, netif_status_callback);
donatien 3:f5776537f27f 57 }
donatien 3:f5776537f27f 58
emilmont 14:cec293071eed 59 int EthernetInterface::init() {
emilmont 14:cec293071eed 60 use_dhcp = true;
emilmont 14:cec293071eed 61 init_netif(NULL, NULL, NULL);
emilmont 14:cec293071eed 62 return OK;
donatien 3:f5776537f27f 63 }
donatien 3:f5776537f27f 64
emilmont 14:cec293071eed 65 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
emilmont 14:cec293071eed 66 use_dhcp = false;
emilmont 14:cec293071eed 67 ip_addr_t ip_n, mask_n, gateway_n;
emilmont 14:cec293071eed 68 inet_aton(ip, &ip_n);
emilmont 14:cec293071eed 69 inet_aton(mask, &mask_n);
emilmont 14:cec293071eed 70 inet_aton(gateway, &gateway_n);
emilmont 14:cec293071eed 71 init_netif(&ip_n, &mask_n, &gateway_n);
emilmont 14:cec293071eed 72 return OK;
donatien 3:f5776537f27f 73 }
donatien 3:f5776537f27f 74
emilmont 14:cec293071eed 75 int EthernetInterface::connect() {
emilmont 14:cec293071eed 76 NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
emilmont 14:cec293071eed 77 NVIC_EnableIRQ(ENET_IRQn);
emilmont 14:cec293071eed 78
emilmont 14:cec293071eed 79 if (use_dhcp) {
emilmont 14:cec293071eed 80 dhcp_start(&lpcNetif);
emilmont 14:cec293071eed 81 } else {
emilmont 14:cec293071eed 82 netif_set_up(&lpcNetif);
emilmont 14:cec293071eed 83 }
emilmont 14:cec293071eed 84
emilmont 14:cec293071eed 85 netif_inited.wait();
emilmont 14:cec293071eed 86
emilmont 14:cec293071eed 87 return OK;
donatien 3:f5776537f27f 88 }
donatien 3:f5776537f27f 89
emilmont 14:cec293071eed 90 int EthernetInterface::disconnect() {
emilmont 14:cec293071eed 91 if (use_dhcp) {
emilmont 14:cec293071eed 92 dhcp_release(&lpcNetif);
emilmont 14:cec293071eed 93 dhcp_stop(&lpcNetif);
emilmont 14:cec293071eed 94 } else {
emilmont 14:cec293071eed 95 netif_set_down(&lpcNetif);
emilmont 14:cec293071eed 96 }
emilmont 14:cec293071eed 97
emilmont 14:cec293071eed 98 NVIC_DisableIRQ(ENET_IRQn);
emilmont 14:cec293071eed 99
emilmont 14:cec293071eed 100 return OK;
donatien 3:f5776537f27f 101 }
donatien 3:f5776537f27f 102
emilmont 14:cec293071eed 103 char* EthernetInterface::getIPAddress() {
emilmont 14:cec293071eed 104 return (connected) ? (ip_addr) : (NULL);
emilmont 14:cec293071eed 105 }