Template for LPC1768

Dependencies:   Gimbal MLX90620 Socket lwip-eth lwip-sys lwip mbed-rtos mbed

Fork of EkkoEye by EkkoSense

Committer:
emilmont
Date:
Fri Feb 15 17:01:20 2013 +0000
Revision:
26:dd9794ce1d64
Parent:
22:2a797ba9babe
Child:
27:2124eae946f3
Correct Ethernet initialization with static IP

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 26:dd9794ce1d64 34 static char ip_addr[16] = "\0";
emilmont 26:dd9794ce1d64 35 static bool use_dhcp = false;
donatien 3:f5776537f27f 36
emilmont 26:dd9794ce1d64 37 static Semaphore tcpip_inited(0);
emilmont 26:dd9794ce1d64 38 static Semaphore netif_linked(0);
emilmont 26:dd9794ce1d64 39 static Semaphore netif_up(0);
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 26:dd9794ce1d64 44
emilmont 26:dd9794ce1d64 45 static void netif_link_callback(struct netif *netif) {
emilmont 26:dd9794ce1d64 46 if (netif_is_link_up(netif)) {
emilmont 26:dd9794ce1d64 47 netif_linked.release();
emilmont 26:dd9794ce1d64 48 }
emilmont 26:dd9794ce1d64 49 }
emilmont 26:dd9794ce1d64 50
emilmont 14:cec293071eed 51 static void netif_status_callback(struct netif *netif) {
emilmont 26:dd9794ce1d64 52 if (netif_is_up(netif)) {
emilmont 26:dd9794ce1d64 53 strcpy(ip_addr, inet_ntoa(netif->ip_addr));
emilmont 26:dd9794ce1d64 54 netif_up.release();
emilmont 26:dd9794ce1d64 55 }
emilmont 14:cec293071eed 56 }
emilmont 26:dd9794ce1d64 57
emilmont 14:cec293071eed 58 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
emilmont 14:cec293071eed 59 tcpip_init(tcpip_init_done, NULL);
emilmont 14:cec293071eed 60 tcpip_inited.wait();
emilmont 14:cec293071eed 61
emilmont 14:cec293071eed 62 memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
emilmont 22:2a797ba9babe 63 netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input);
emilmont 14:cec293071eed 64 netif_set_default(&lpcNetif);
emilmont 26:dd9794ce1d64 65
emilmont 26:dd9794ce1d64 66 netif_set_link_callback (&lpcNetif, netif_link_callback);
emilmont 14:cec293071eed 67 netif_set_status_callback(&lpcNetif, netif_status_callback);
donatien 3:f5776537f27f 68 }
donatien 3:f5776537f27f 69
emilmont 14:cec293071eed 70 int EthernetInterface::init() {
emilmont 14:cec293071eed 71 use_dhcp = true;
emilmont 14:cec293071eed 72 init_netif(NULL, NULL, NULL);
emilmont 15:fd9597f1b81b 73 return 0;
donatien 3:f5776537f27f 74 }
donatien 3:f5776537f27f 75
emilmont 14:cec293071eed 76 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
emilmont 14:cec293071eed 77 use_dhcp = false;
emilmont 26:dd9794ce1d64 78 strcpy(ip_addr, ip);
emilmont 26:dd9794ce1d64 79
emilmont 14:cec293071eed 80 ip_addr_t ip_n, mask_n, gateway_n;
emilmont 14:cec293071eed 81 inet_aton(ip, &ip_n);
emilmont 14:cec293071eed 82 inet_aton(mask, &mask_n);
emilmont 14:cec293071eed 83 inet_aton(gateway, &gateway_n);
emilmont 14:cec293071eed 84 init_netif(&ip_n, &mask_n, &gateway_n);
emilmont 26:dd9794ce1d64 85
emilmont 15:fd9597f1b81b 86 return 0;
donatien 3:f5776537f27f 87 }
donatien 3:f5776537f27f 88
emilmont 21:8cd2de462559 89 int EthernetInterface::connect(unsigned int timeout_ms) {
emilmont 14:cec293071eed 90 NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
emilmont 14:cec293071eed 91 NVIC_EnableIRQ(ENET_IRQn);
emilmont 14:cec293071eed 92
emilmont 26:dd9794ce1d64 93 int inited;
emilmont 14:cec293071eed 94 if (use_dhcp) {
emilmont 14:cec293071eed 95 dhcp_start(&lpcNetif);
emilmont 26:dd9794ce1d64 96
emilmont 26:dd9794ce1d64 97 // Wait for an IP
emilmont 26:dd9794ce1d64 98 // -1: error, 0: timeout
emilmont 26:dd9794ce1d64 99 inited = netif_up.wait(timeout_ms);
emilmont 14:cec293071eed 100 } else {
emilmont 14:cec293071eed 101 netif_set_up(&lpcNetif);
emilmont 26:dd9794ce1d64 102
emilmont 26:dd9794ce1d64 103 // Wait for the link up
emilmont 26:dd9794ce1d64 104 inited = netif_linked.wait(timeout_ms);
emilmont 14:cec293071eed 105 }
emilmont 14:cec293071eed 106
emilmont 21:8cd2de462559 107 return (inited > 0) ? (0) : (-1);
donatien 3:f5776537f27f 108 }
donatien 3:f5776537f27f 109
emilmont 14:cec293071eed 110 int EthernetInterface::disconnect() {
emilmont 14:cec293071eed 111 if (use_dhcp) {
emilmont 14:cec293071eed 112 dhcp_release(&lpcNetif);
emilmont 14:cec293071eed 113 dhcp_stop(&lpcNetif);
emilmont 14:cec293071eed 114 } else {
emilmont 14:cec293071eed 115 netif_set_down(&lpcNetif);
emilmont 14:cec293071eed 116 }
emilmont 14:cec293071eed 117
emilmont 14:cec293071eed 118 NVIC_DisableIRQ(ENET_IRQn);
emilmont 14:cec293071eed 119
emilmont 15:fd9597f1b81b 120 return 0;
donatien 3:f5776537f27f 121 }
donatien 3:f5776537f27f 122
emilmont 14:cec293071eed 123 char* EthernetInterface::getIPAddress() {
emilmont 26:dd9794ce1d64 124 return ip_addr;
emilmont 14:cec293071eed 125 }