Skip to content

Port ethernet to EMAC #4918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doxyfile_options
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@ PREDEFINED = DOXYGEN_ONLY \
DEVICE_CAN \
DEVICE_ETHERNET \
DEVICE_EMAC \
DEVICE_ETH \
DEVICE_FLASH \
DEVICE_I2C \
DEVICE_I2CSLAVE \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
4 changes: 2 additions & 2 deletions features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include <algorithm>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif
#ifndef MBED_EXTENDED_TESTS
#error [NOT_SUPPORTED] Pressure tests are not supported by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif
#ifndef MBED_EXTENDED_TESTS
#error [NOT_SUPPORTED] Parallel pressure tests are not supported by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
4 changes: 2 additions & 2 deletions features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif

#include "mbed.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif
#ifndef MBED_EXTENDED_TESTS
#error [NOT_SUPPORTED] Pressure tests are not supported by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#if !DEVICE_ETH
#error [NOT_SUPPORTED] Ethernet not supported for this targets
#endif
#ifndef MBED_EXTENDED_TESTS
#error [NOT_SUPPORTED] Parallel pressure tests are not supported by default
Expand Down
144 changes: 121 additions & 23 deletions features/FEATURE_LWIP/lwip-interface/emac_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@
* limitations under the License.
*/

#if DEVICE_EMAC

#include "emac_api.h"
#if !NSAPI_PPP_AVAILABLE
#include "mbed_ipstack.h"
#include "emac_stack_mem.h"
#include "lwip/tcpip.h"
#include "lwip/tcp.h"
#include "lwip/ip.h"
#include "netif/etharp.h"
#include "lwip/ethip6.h"
#include "netsocket/nsapi_types.h"

#include "mbed_ipstack_lwip.h"

static err_t emac_lwip_low_level_output(struct netif *netif, struct pbuf *p)
{
emac_interface_t *mac = (emac_interface_t *)netif->state;
bool ret = mac->ops.link_out(mac, (emac_stack_mem_t *)p);

mbed_ipstack_interface_t *mac = netif->state;
bool ret = mac->ops->link_out(mac->hw, p);
return ret ? ERR_OK : ERR_IF;
}

static void emac_lwip_input(void *data, emac_stack_t *buf)
static void emac_lwip_input(void *data, emac_stack_mem_t *buf)
{
struct pbuf *p = (struct pbuf *)buf;
struct netif *netif = (struct netif *)data;
struct pbuf *p = buf;
struct netif *netif = data;

/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK) {
Expand All @@ -46,7 +48,7 @@ static void emac_lwip_input(void *data, emac_stack_t *buf)

static void emac_lwip_state_change(void *data, bool up)
{
struct netif *netif = (struct netif *)data;
struct netif *netif = data;

if (up) {
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, netif, 1);
Expand All @@ -55,34 +57,130 @@ static void emac_lwip_state_change(void *data, bool up)
}
}

err_t emac_lwip_if_init(struct netif *netif)
#if LWIP_IGMP

#include "lwip/igmp.h"
/**
* IPv4 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv4 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
static err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, u8_t action)
{
int err = ERR_OK;
emac_interface_t *mac = (emac_interface_t *)netif->state;
mbed_ipstack_interface_t *emac = netif->state;
if (emac->ops->add_multicast_group == NULL) {
return ERR_OK; /* This function is not mandatory */
}

mac->ops.set_link_input_cb(mac, emac_lwip_input, netif);
mac->ops.set_link_state_cb(mac, emac_lwip_state_change, netif);
switch (action) {
case NETIF_ADD_MAC_FILTER:
{
uint32_t group23 = ntohl(group->addr) & 0x007FFFFF;
uint8_t addr[6];
addr[0] = LL_IP4_MULTICAST_ADDR_0;
addr[1] = LL_IP4_MULTICAST_ADDR_1;
addr[2] = LL_IP4_MULTICAST_ADDR_2;
addr[3] = group23 >> 16;
addr[4] = group23 >> 8;
addr[5] = group23;
emac->ops->add_multicast_group(emac, addr);
return ERR_OK;
}
case NETIF_DEL_MAC_FILTER:
/* As we don't reference count, silently ignore delete requests */
return ERR_OK;
default:
return ERR_ARG;
}
}
#endif

if (!mac->ops.power_up(mac)) {
err = ERR_IF;
#if LWIP_IPV6_MLD

#include "lwip/mld6.h"
/**
* IPv6 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv6 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
static err_t mld_mac_filter(struct netif *netif, const ip6_addr_t *group, u8_t action)
{
mbed_ipstack_interface_t *emac = netif->state;
if (emac->ops->add_multicast_group == NULL) {
return ERR_OK; /* This function is not mandatory */
}

netif->mtu = mac->ops.get_mtu_size(mac);
netif->hwaddr_len = mac->ops.get_hwaddr_size(mac);
mac->ops.get_hwaddr(mac, netif->hwaddr);
switch (action) {
case NETIF_ADD_MAC_FILTER:
{
uint32_t group32 = ntohl(group->addr[3]);
uint8_t addr[6];
addr[0] = LL_IP6_MULTICAST_ADDR_0;
addr[1] = LL_IP6_MULTICAST_ADDR_1;
addr[2] = group32 >> 24;
addr[3] = group32 >> 16;
addr[4] = group32 >> 8;
addr[5] = group32;
emac->ops->add_multicast_group(emac, addr);
return ERR_OK;
}
case NETIF_DEL_MAC_FILTER:
/* As we don't reference count, silently ignore delete requests */
return ERR_OK;
default:
return ERR_ARG;
}
}
#endif

err_t emac_lwip_if_init(struct netif *netif)
{
int err = ERR_OK;
mbed_ipstack_interface_t *emac = netif->state;

emac->ops->set_link_input_cb(emac->hw, emac_lwip_input, netif);
emac->ops->set_link_state_cb(emac->hw, emac_lwip_state_change, netif);

netif->hwaddr_len = emac->ops->get_hwaddr_size(emac);
emac->ops->get_hwaddr(emac->hw, netif->hwaddr);
netif->mtu = emac->ops->get_mtu_size(emac->hw);

/* Interface capabilities */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;

mac->ops.get_ifname(mac, netif->name, 2);
emac->ops->get_ifname(emac->hw, netif->name, 2);

#if LWIP_IPV4
netif->output = etharp_output;
#if LWIP_IGMP
netif->igmp_mac_filter = igmp_mac_filter;
netif->flags |= NETIF_FLAG_IGMP;
#endif /* LWIP_IGMP */
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#if LWIP_IPV6_MLD
netif->mld_mac_filter = mld_mac_filter;
netif->flags |= NETIF_FLAG_MLD6;
#else
// Would need to enable all multicasts here - no API in fsl_enet to do that
#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system"
#endif
#endif

netif->linkoutput = emac_lwip_low_level_output;

if (!emac->ops->power_up(emac->hw)) {
err = ERR_IF;
}

return err;
}

#endif /* DEVICE_EMAC */
#endif //!NSAPI_PPP_AVAILABLE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* EthernetInterface.h */
/* emac_lwip.h */
/* Copyright (C) 2012 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Expand All @@ -17,29 +17,19 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Architecture specific Ethernet interface
// Must be implemented by each target

#ifndef ETHARCH_H_
#define ETHARCH_H_
#ifndef EMAC_LWIP_H_
#define EMAC_LWIP_H_

#include "lwip/netif.h"

#ifdef __cplusplus
extern "C" {
#endif

#if DEVICE_EMAC
err_t emac_lwip_if_init(struct netif *netif);

#else /* DEVICE_EMAC */
void eth_arch_enable_interrupts(void);
void eth_arch_disable_interrupts(void);
err_t eth_arch_enetif_init(struct netif *netif);
#endif

#ifdef __cplusplus
}
#endif

#endif // #ifndef ETHARCHINTERFACE_H_
#endif // #ifndef EMAC_LWIP_H_
Loading