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