ProjetoBB
Dependencies: F7_Ethernet WebSocketClient mbed mcp3008
Fork of Nucleo_F746ZG_Ethernet by
F7_Ethernet/EthernetInterface.cpp@0:f9b6112278fe, 2016-06-18 (annotated)
- Committer:
- DieterGraef
- Date:
- Sat Jun 18 10:49:12 2016 +0000
- Revision:
- 0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:f9b6112278fe | 1 | /* EthernetInterface.cpp */ |
DieterGraef | 0:f9b6112278fe | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
DieterGraef | 0:f9b6112278fe | 3 | * |
DieterGraef | 0:f9b6112278fe | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
DieterGraef | 0:f9b6112278fe | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
DieterGraef | 0:f9b6112278fe | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
DieterGraef | 0:f9b6112278fe | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
DieterGraef | 0:f9b6112278fe | 8 | * furnished to do so, subject to the following conditions: |
DieterGraef | 0:f9b6112278fe | 9 | * |
DieterGraef | 0:f9b6112278fe | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
DieterGraef | 0:f9b6112278fe | 11 | * substantial portions of the Software. |
DieterGraef | 0:f9b6112278fe | 12 | * |
DieterGraef | 0:f9b6112278fe | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
DieterGraef | 0:f9b6112278fe | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
DieterGraef | 0:f9b6112278fe | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
DieterGraef | 0:f9b6112278fe | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DieterGraef | 0:f9b6112278fe | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
DieterGraef | 0:f9b6112278fe | 18 | */ |
DieterGraef | 0:f9b6112278fe | 19 | #include "EthernetInterface.h" |
DieterGraef | 0:f9b6112278fe | 20 | |
DieterGraef | 0:f9b6112278fe | 21 | #include "lwip/inet.h" |
DieterGraef | 0:f9b6112278fe | 22 | #include "lwip/netif.h" |
DieterGraef | 0:f9b6112278fe | 23 | #include "netif/etharp.h" |
DieterGraef | 0:f9b6112278fe | 24 | #include "lwip/dhcp.h" |
DieterGraef | 0:f9b6112278fe | 25 | #include "eth_arch.h" |
DieterGraef | 0:f9b6112278fe | 26 | #include "lwip/tcpip.h" |
DieterGraef | 0:f9b6112278fe | 27 | |
DieterGraef | 0:f9b6112278fe | 28 | #include "mbed.h" |
DieterGraef | 0:f9b6112278fe | 29 | |
DieterGraef | 0:f9b6112278fe | 30 | /* TCP/IP and Network Interface Initialisation */ |
DieterGraef | 0:f9b6112278fe | 31 | static struct netif netif; |
DieterGraef | 0:f9b6112278fe | 32 | |
DieterGraef | 0:f9b6112278fe | 33 | static char mac_addr[19]; |
DieterGraef | 0:f9b6112278fe | 34 | static char ip_addr[17] = "\0"; |
DieterGraef | 0:f9b6112278fe | 35 | static char gateway[17] = "\0"; |
DieterGraef | 0:f9b6112278fe | 36 | static char networkmask[17] = "\0"; |
DieterGraef | 0:f9b6112278fe | 37 | static bool use_dhcp = false; |
DieterGraef | 0:f9b6112278fe | 38 | |
DieterGraef | 0:f9b6112278fe | 39 | static Semaphore tcpip_inited(0); |
DieterGraef | 0:f9b6112278fe | 40 | static Semaphore netif_linked(0); |
DieterGraef | 0:f9b6112278fe | 41 | static Semaphore netif_up(0); |
DieterGraef | 0:f9b6112278fe | 42 | |
DieterGraef | 0:f9b6112278fe | 43 | static void tcpip_init_done(void *arg) { |
DieterGraef | 0:f9b6112278fe | 44 | tcpip_inited.release(); |
DieterGraef | 0:f9b6112278fe | 45 | } |
DieterGraef | 0:f9b6112278fe | 46 | |
DieterGraef | 0:f9b6112278fe | 47 | static void netif_link_callback(struct netif *netif) { |
DieterGraef | 0:f9b6112278fe | 48 | if (netif_is_link_up(netif)) { |
DieterGraef | 0:f9b6112278fe | 49 | netif_linked.release(); |
DieterGraef | 0:f9b6112278fe | 50 | } |
DieterGraef | 0:f9b6112278fe | 51 | } |
DieterGraef | 0:f9b6112278fe | 52 | |
DieterGraef | 0:f9b6112278fe | 53 | static void netif_status_callback(struct netif *netif) { |
DieterGraef | 0:f9b6112278fe | 54 | if (netif_is_up(netif)) { |
DieterGraef | 0:f9b6112278fe | 55 | strcpy(ip_addr, inet_ntoa(netif->ip_addr)); |
DieterGraef | 0:f9b6112278fe | 56 | strcpy(gateway, inet_ntoa(netif->gw)); |
DieterGraef | 0:f9b6112278fe | 57 | strcpy(networkmask, inet_ntoa(netif->netmask)); |
DieterGraef | 0:f9b6112278fe | 58 | netif_up.release(); |
DieterGraef | 0:f9b6112278fe | 59 | } |
DieterGraef | 0:f9b6112278fe | 60 | } |
DieterGraef | 0:f9b6112278fe | 61 | |
DieterGraef | 0:f9b6112278fe | 62 | static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { |
DieterGraef | 0:f9b6112278fe | 63 | tcpip_init(tcpip_init_done, NULL); |
DieterGraef | 0:f9b6112278fe | 64 | tcpip_inited.wait(); |
DieterGraef | 0:f9b6112278fe | 65 | |
DieterGraef | 0:f9b6112278fe | 66 | memset((void*) &netif, 0, sizeof(netif)); |
DieterGraef | 0:f9b6112278fe | 67 | netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input); |
DieterGraef | 0:f9b6112278fe | 68 | netif_set_default(&netif); |
DieterGraef | 0:f9b6112278fe | 69 | |
DieterGraef | 0:f9b6112278fe | 70 | netif_set_link_callback (&netif, netif_link_callback); |
DieterGraef | 0:f9b6112278fe | 71 | netif_set_status_callback(&netif, netif_status_callback); |
DieterGraef | 0:f9b6112278fe | 72 | } |
DieterGraef | 0:f9b6112278fe | 73 | |
DieterGraef | 0:f9b6112278fe | 74 | static void set_mac_address(void) { |
DieterGraef | 0:f9b6112278fe | 75 | #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) |
DieterGraef | 0:f9b6112278fe | 76 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2, |
DieterGraef | 0:f9b6112278fe | 77 | MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5); |
DieterGraef | 0:f9b6112278fe | 78 | #else |
DieterGraef | 0:f9b6112278fe | 79 | char mac[6]; |
DieterGraef | 0:f9b6112278fe | 80 | mbed_mac_address(mac); |
DieterGraef | 0:f9b6112278fe | 81 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
DieterGraef | 0:f9b6112278fe | 82 | #endif |
DieterGraef | 0:f9b6112278fe | 83 | } |
DieterGraef | 0:f9b6112278fe | 84 | |
DieterGraef | 0:f9b6112278fe | 85 | int EthernetInterface::init() { |
DieterGraef | 0:f9b6112278fe | 86 | use_dhcp = true; |
DieterGraef | 0:f9b6112278fe | 87 | set_mac_address(); |
DieterGraef | 0:f9b6112278fe | 88 | init_netif(NULL, NULL, NULL); |
DieterGraef | 0:f9b6112278fe | 89 | return 0; |
DieterGraef | 0:f9b6112278fe | 90 | } |
DieterGraef | 0:f9b6112278fe | 91 | |
DieterGraef | 0:f9b6112278fe | 92 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { |
DieterGraef | 0:f9b6112278fe | 93 | use_dhcp = false; |
DieterGraef | 0:f9b6112278fe | 94 | |
DieterGraef | 0:f9b6112278fe | 95 | set_mac_address(); |
DieterGraef | 0:f9b6112278fe | 96 | strcpy(ip_addr, ip); |
DieterGraef | 0:f9b6112278fe | 97 | |
DieterGraef | 0:f9b6112278fe | 98 | ip_addr_t ip_n, mask_n, gateway_n; |
DieterGraef | 0:f9b6112278fe | 99 | inet_aton(ip, &ip_n); |
DieterGraef | 0:f9b6112278fe | 100 | inet_aton(mask, &mask_n); |
DieterGraef | 0:f9b6112278fe | 101 | inet_aton(gateway, &gateway_n); |
DieterGraef | 0:f9b6112278fe | 102 | init_netif(&ip_n, &mask_n, &gateway_n); |
DieterGraef | 0:f9b6112278fe | 103 | |
DieterGraef | 0:f9b6112278fe | 104 | return 0; |
DieterGraef | 0:f9b6112278fe | 105 | } |
DieterGraef | 0:f9b6112278fe | 106 | |
DieterGraef | 0:f9b6112278fe | 107 | int EthernetInterface::connect(unsigned int timeout_ms) { |
DieterGraef | 0:f9b6112278fe | 108 | eth_arch_enable_interrupts(); |
DieterGraef | 0:f9b6112278fe | 109 | |
DieterGraef | 0:f9b6112278fe | 110 | int inited; |
DieterGraef | 0:f9b6112278fe | 111 | if (use_dhcp) { |
DieterGraef | 0:f9b6112278fe | 112 | dhcp_start(&netif); |
DieterGraef | 0:f9b6112278fe | 113 | |
DieterGraef | 0:f9b6112278fe | 114 | // Wait for an IP Address |
DieterGraef | 0:f9b6112278fe | 115 | // -1: error, 0: timeout |
DieterGraef | 0:f9b6112278fe | 116 | inited = netif_up.wait(timeout_ms); |
DieterGraef | 0:f9b6112278fe | 117 | } else { |
DieterGraef | 0:f9b6112278fe | 118 | netif_set_up(&netif); |
DieterGraef | 0:f9b6112278fe | 119 | |
DieterGraef | 0:f9b6112278fe | 120 | // Wait for the link up |
DieterGraef | 0:f9b6112278fe | 121 | inited = netif_linked.wait(timeout_ms); |
DieterGraef | 0:f9b6112278fe | 122 | } |
DieterGraef | 0:f9b6112278fe | 123 | |
DieterGraef | 0:f9b6112278fe | 124 | return (inited > 0) ? (0) : (-1); |
DieterGraef | 0:f9b6112278fe | 125 | } |
DieterGraef | 0:f9b6112278fe | 126 | |
DieterGraef | 0:f9b6112278fe | 127 | int EthernetInterface::disconnect() { |
DieterGraef | 0:f9b6112278fe | 128 | if (use_dhcp) { |
DieterGraef | 0:f9b6112278fe | 129 | dhcp_release(&netif); |
DieterGraef | 0:f9b6112278fe | 130 | dhcp_stop(&netif); |
DieterGraef | 0:f9b6112278fe | 131 | } else { |
DieterGraef | 0:f9b6112278fe | 132 | netif_set_down(&netif); |
DieterGraef | 0:f9b6112278fe | 133 | } |
DieterGraef | 0:f9b6112278fe | 134 | |
DieterGraef | 0:f9b6112278fe | 135 | eth_arch_disable_interrupts(); |
DieterGraef | 0:f9b6112278fe | 136 | |
DieterGraef | 0:f9b6112278fe | 137 | return 0; |
DieterGraef | 0:f9b6112278fe | 138 | } |
DieterGraef | 0:f9b6112278fe | 139 | |
DieterGraef | 0:f9b6112278fe | 140 | char* EthernetInterface::getMACAddress() { |
DieterGraef | 0:f9b6112278fe | 141 | return mac_addr; |
DieterGraef | 0:f9b6112278fe | 142 | } |
DieterGraef | 0:f9b6112278fe | 143 | |
DieterGraef | 0:f9b6112278fe | 144 | char* EthernetInterface::getIPAddress() { |
DieterGraef | 0:f9b6112278fe | 145 | return ip_addr; |
DieterGraef | 0:f9b6112278fe | 146 | } |
DieterGraef | 0:f9b6112278fe | 147 | |
DieterGraef | 0:f9b6112278fe | 148 | char* EthernetInterface::getGateway() { |
DieterGraef | 0:f9b6112278fe | 149 | return gateway; |
DieterGraef | 0:f9b6112278fe | 150 | } |
DieterGraef | 0:f9b6112278fe | 151 | |
DieterGraef | 0:f9b6112278fe | 152 | char* EthernetInterface::getNetworkMask() { |
DieterGraef | 0:f9b6112278fe | 153 | return networkmask; |
DieterGraef | 0:f9b6112278fe | 154 | } |
DieterGraef | 0:f9b6112278fe | 155 | |
DieterGraef | 0:f9b6112278fe | 156 |