mbed-os
Fork of mbed-os by
features/FEATURE_LWIP/lwip-interface/emac_lwip.c@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2016 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | #if DEVICE_EMAC |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "emac_api.h" |
elessair | 0:f269e3021894 | 20 | #include "emac_stack_mem.h" |
elessair | 0:f269e3021894 | 21 | #include "lwip/tcpip.h" |
elessair | 0:f269e3021894 | 22 | #include "lwip/tcp.h" |
elessair | 0:f269e3021894 | 23 | #include "lwip/ip.h" |
elessair | 0:f269e3021894 | 24 | #include "netif/etharp.h" |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p) |
elessair | 0:f269e3021894 | 27 | { |
elessair | 0:f269e3021894 | 28 | emac_interface_t *mac = (emac_interface_t *)netif->state; |
elessair | 0:f269e3021894 | 29 | bool ret = mac->ops.link_out(mac, (emac_stack_mem_t *)p); |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | return ret ? ERR_OK : ERR_IF; |
elessair | 0:f269e3021894 | 32 | } |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | static void emac_lwip_input(void *data, emac_stack_t *buf) |
elessair | 0:f269e3021894 | 35 | { |
elessair | 0:f269e3021894 | 36 | struct pbuf *p = (struct pbuf *)buf; |
elessair | 0:f269e3021894 | 37 | struct netif *netif = (struct netif *)data; |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | /* pass all packets to ethernet_input, which decides what packets it supports */ |
elessair | 0:f269e3021894 | 40 | if (netif->input(p, netif) != ERR_OK) { |
elessair | 0:f269e3021894 | 41 | LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n")); |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | pbuf_free(p); |
elessair | 0:f269e3021894 | 44 | } |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | static void emac_lwip_state_change(void *data, bool up) |
elessair | 0:f269e3021894 | 48 | { |
elessair | 0:f269e3021894 | 49 | struct netif *netif = (struct netif *)data; |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | if (up) { |
elessair | 0:f269e3021894 | 52 | tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, netif, 1); |
elessair | 0:f269e3021894 | 53 | } else { |
elessair | 0:f269e3021894 | 54 | tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, netif, 1); |
elessair | 0:f269e3021894 | 55 | } |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | err_t emac_lwip_if_init(struct netif *netif) |
elessair | 0:f269e3021894 | 59 | { |
elessair | 0:f269e3021894 | 60 | int err = ERR_OK; |
elessair | 0:f269e3021894 | 61 | emac_interface_t *mac = (emac_interface_t *)netif->state; |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | mac->ops.set_link_input_cb(mac, emac_lwip_input, netif); |
elessair | 0:f269e3021894 | 64 | mac->ops.set_link_state_cb(mac, emac_lwip_state_change, netif); |
elessair | 0:f269e3021894 | 65 | |
elessair | 0:f269e3021894 | 66 | netif->hwaddr_len = mac->ops.get_hwaddr_size(mac); |
elessair | 0:f269e3021894 | 67 | mac->ops.get_hwaddr(mac, netif->hwaddr); |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | netif->mtu = mac->ops.get_mtu_size(mac); |
elessair | 0:f269e3021894 | 70 | |
elessair | 0:f269e3021894 | 71 | /* Interface capabilities */ |
elessair | 0:f269e3021894 | 72 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP; |
elessair | 0:f269e3021894 | 73 | |
elessair | 0:f269e3021894 | 74 | mac->ops.get_ifname(mac, netif->name, 2); |
elessair | 0:f269e3021894 | 75 | |
elessair | 0:f269e3021894 | 76 | #if LWIP_IPV4 |
elessair | 0:f269e3021894 | 77 | netif->output = etharp_output; |
elessair | 0:f269e3021894 | 78 | #endif /* LWIP_IPV4 */ |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | netif->linkoutput = emac_lwip_low_level_output; |
elessair | 0:f269e3021894 | 81 | |
elessair | 0:f269e3021894 | 82 | if (!mac->ops.power_up(mac)) { |
elessair | 0:f269e3021894 | 83 | err = ERR_IF; |
elessair | 0:f269e3021894 | 84 | } |
elessair | 0:f269e3021894 | 85 | |
elessair | 0:f269e3021894 | 86 | return err; |
elessair | 0:f269e3021894 | 87 | } |
elessair | 0:f269e3021894 | 88 | |
elessair | 0:f269e3021894 | 89 | #endif /* DEVICE_EMAC */ |