From 0eb38e31bacbf3c5e3b188c59672649cf817c351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=82=E7=85=9C=E6=B4=8B?= <1163589503@qq.com> Date: Thu, 3 Aug 2023 14:15:26 +0800 Subject: [PATCH] 23/08/03 Add Netdev to xidatong-arm32 --- .../Applications/webnet/WebNet_XiUOS | 2 +- .../third_party_driver/ethernet/ethernetif.c | 8 - .../third_party_driver/ethernet/Makefile | 2 +- .../ethernet/enet_ethernetif.c | 7 + .../third_party_driver/ethernet/eth_netdev.c | 163 ++++++++++++++++++ .../include/connect_ethernet.h | 2 + Ubiquitous/XiZi_IIoT/path_kernel.mk | 1 + .../resources/ethernet/LwIP/arch/lwipopts.h | 3 +- .../resources/ethernet/LwIP/arch/sys_arch.c | 6 +- 9 files changed, 178 insertions(+), 16 deletions(-) create mode 100644 Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c diff --git a/APP_Framework/Applications/webnet/WebNet_XiUOS b/APP_Framework/Applications/webnet/WebNet_XiUOS index 2056a6d58..60a8a500b 160000 --- a/APP_Framework/Applications/webnet/WebNet_XiUOS +++ b/APP_Framework/Applications/webnet/WebNet_XiUOS @@ -1 +1 @@ -Subproject commit 2056a6d5876ac607052522b82134ee25071419f8 +Subproject commit 60a8a500b93b47876c6eaff600e4cf2b8bf7b283 diff --git a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/ethernetif.c b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/ethernetif.c index c8fa10c40..6ea4d6ba0 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/ethernetif.c +++ b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/ethernetif.c @@ -238,17 +238,10 @@ err_t ethernetif_linkoutput(struct netif* netif, struct pbuf* p) struct eth_tx_msg msg; msg.netif = netif; msg.buf = p; - // ret = sys_sem_new(&(msg.completion_sem), 0); - // if (EOK != ret) { - // return ret; - // } if (sys_mbox_trypost(get_eth_tx_mbox(), (void*)&msg) == EOK) { - // sys_arch_sem_wait(&(msg.completion_sem), WAITING_FOREVER); } - // sys_sem_free(&(msg.completion_sem)); - // return msg.ret_val; return LL_OK; } @@ -260,7 +253,6 @@ void ethernetif_output() if (sys_arch_mbox_fetch(get_eth_tx_mbox(), ¶m_ptr, WAITING_FOREVER) != SYS_ARCH_TIMEOUT) { struct eth_tx_msg* msg = (struct eth_tx_msg*)param_ptr; msg->ret_val = low_level_output(msg->netif, msg->buf); - // sys_sem_signal(&(msg->completion_sem)); } } } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/Makefile b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/Makefile index 65f5adc78..6cd8a10fa 100755 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c +SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c eth_netdev.c SRC_DIR := lan8720 include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/enet_ethernetif.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/enet_ethernetif.c index 9b8ca475a..114f1ef61 100755 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/enet_ethernetif.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/enet_ethernetif.c @@ -68,6 +68,7 @@ #include "fsl_gpio.h" #include "fsl_iomuxc.h" +#include "netdev.h" #include "sys_arch.h" /******************************************************************************* @@ -319,6 +320,12 @@ err_t ethernetif_init(struct netif *netif, struct ethernetif *ethernetif, } #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Adding netdev.\n", __func__)); + if (EOK != lwip_netdev_add(netif)) { + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] LWIP add netdev failed.\n", __func__)); + } else { + printf("[%s] Add Netdev successful\n", __func__); + } return ERR_OK; } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c new file mode 100644 index 000000000..2dfe75795 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c @@ -0,0 +1,163 @@ + +#include +#include +#include +#include +#include +#include + +static const uint32_t NETIF_NAME_LEN = 2; + +static int lwip_netdev_set_up(struct netdev* netdev) +{ + netif_set_up((struct netif*)netdev->user_data); + return ERR_OK; +} + +static int lwip_netdev_set_down(struct netdev* netif) +{ + netif_set_down((struct netif*)netif->user_data); + return ERR_OK; +} + +#ifndef ip_2_ip4 +#define ip_2_ip4(ipaddr) (ipaddr) +#endif +static int lwip_netdev_set_addr_info(struct netdev* netdev, ip_addr_t* ip_addr, ip_addr_t* netmask, ip_addr_t* gw) +{ + if (ip_addr && netmask && gw) { + netif_set_addr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw)); + } else { + if (ip_addr) { + netif_set_ipaddr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr)); + } + if (netmask) { + netif_set_netmask((struct netif*)netdev->user_data, ip_2_ip4(netmask)); + } + if (gw) { + netif_set_gw((struct netif*)netdev->user_data, ip_2_ip4(gw)); + } + } +} + +#ifdef LWIP_DNS +static int lwip_netdev_set_dns_server(struct netdev* netdev, uint8_t dns_num, ip_addr_t* dns_server) +{ +#if LWIP_VERSION_MAJOR == 1U /* v1.x */ + extern void dns_setserver(u8_t numdns, ip_addr_t * dnsserver); +#else /* >=2.x */ + extern void dns_setserver(uint8_t dns_num, const ip_addr_t* dns_server); +#endif /* LWIP_VERSION_MAJOR == 1U */ + + dns_setserver(dns_num, dns_server); + return ERR_OK; +} +#endif + +#ifdef LWIP_DHCP +static int lwip_netdev_set_dhcp(struct netdev* netdev, bool is_enabled) +{ + netdev_low_level_set_dhcp_status(netdev, is_enabled); + + if (true == is_enabled) { + dhcp_start((struct netif*)netdev->user_data); + } else { + dhcp_stop((struct netif*)netdev->user_data); + } + + return ERR_OK; +} +#endif + +static int lwip_netdev_set_default(struct netdev* netdev) +{ + netif_set_default((struct netif*)netdev->user_data); + return ERR_OK; +} + +static const struct netdev_ops lwip_netdev_ops = { + .set_up = lwip_netdev_set_up, + .set_down = lwip_netdev_set_down, + .set_addr_info = lwip_netdev_set_addr_info, +#ifdef LWIP_DNS + .set_dns_server = lwip_netdev_set_dns_server, +#endif +#ifdef LWIP_DHCP + .set_dhcp = lwip_netdev_set_dhcp, +#endif + .set_default = lwip_netdev_set_default, +}; + +static inline int netdev_set_flags(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + struct netdev* netdev = netdev_get_by_name(lwip_netif->name); + if (netdev == NULL) { + return -ERR_IF; + } + + netdev->mtu = lwip_netif->mtu; + // set flags + if (lwip_netif->flags | NETIF_FLAG_BROADCAST) { + netdev->flags |= NETDEV_FLAG_BROADCAST; + } + if (lwip_netif->flags | NETIF_FLAG_ETHARP) { + netdev->flags |= NETDEV_FLAG_ETHARP; + } + if (lwip_netif->flags | NETIF_FLAG_IGMP) { + netdev->flags |= NETDEV_FLAG_IGMP; + } +#if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */ + if (lwip_netif->flags & NETIF_FLAG_MLD6) { + netdev->flags |= NETDEV_FLAG_MLD6; + } +#endif /* LWIP_VERSION_MAJOR >= 2U */ + +#if LWIP_DHCP + netdev_low_level_set_dhcp_status(netdev, true); +#else + netdev_low_level_set_dhcp_status(netdev, false); +#endif + + return ERR_OK; +} + +int lwip_netdev_add(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + + struct netdev* netdev = calloc(1, sizeof(struct netdev)); + if (netdev == NULL) { + return -ERR_IF; + } + + // init netdev + char netif_name[NETIF_NAME_LEN + 1]; + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Lwip netif name: %s\n", __func__, lwip_netif->name)); + strncpy(netif_name, lwip_netif->name, NETIF_NAME_LEN); + // register netdev + int result = netdev_register(netdev, netif_name, (void*)lwip_netif); + // set values of netdev + netdev_set_flags(lwip_netif); + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + + return result; +} + +void lwip_netdev_del(struct netif* lwip_netif) +{ + char name[NETIF_NAME_LEN + 1]; + struct netdev* netdev; + + CHECK(lwip_netif); + + strncpy(name, lwip_netif->name, NETIF_NAME_LEN); + netdev = netdev_get_by_name(name); + netdev_unregister(netdev); + free(netdev); +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/connect_ethernet.h b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/connect_ethernet.h index 12f65358b..0933eecd5 100755 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/connect_ethernet.h +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/connect_ethernet.h @@ -33,6 +33,8 @@ #define sourceClock CLOCK_GetFreq(kCLOCK_CoreSysClk) #endif +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); #ifdef __cplusplus } diff --git a/Ubiquitous/XiZi_IIoT/path_kernel.mk b/Ubiquitous/XiZi_IIoT/path_kernel.mk index 69319dc59..c43284a6e 100755 --- a/Ubiquitous/XiZi_IIoT/path_kernel.mk +++ b/Ubiquitous/XiZi_IIoT/path_kernel.mk @@ -53,6 +53,7 @@ KERNELPATHS += \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/prot \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/arch +KERNELPATHS += -I$(KERNEL_ROOT)/resources/include/netdev endif endif diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/lwipopts.h b/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/lwipopts.h index 12fa886a0..f8bd4ad3c 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/lwipopts.h +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/lwipopts.h @@ -566,8 +566,7 @@ typedef unsigned int nfds_t; #define MEMP_LIB_MALLOC 1 #define MEMP_MEM_MALLOC 1 -// #define lw_print KPrintf -#define lw_print +#define lw_print KPrintf #define lw_error KPrintf #define lw_notice KPrintf diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/sys_arch.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/sys_arch.c index 847e92559..aff096677 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/sys_arch.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/LwIP/arch/sys_arch.c @@ -360,14 +360,10 @@ void lwip_config_input(struct netif* net) void lwip_config_tcp(uint8_t enet_port, char* ip, char* mask, char* gw) { - static uint32_t g_eth_mbox_size = 16; if (*get_eth_tcpip_sem() == -1) { sys_sem_new(get_eth_tcpip_sem(), 1); } sys_sem_new(get_eth_recv_sem(), 0); - // sys_mbox_new(get_eth_tx_mbox(), g_eth_mbox_size); - - lwip_config_input(&gnetif); ip4_addr_t net_ipaddr, net_netmask, net_gw; char* eth_cfg; @@ -415,6 +411,8 @@ void lwip_config_tcp(uint8_t enet_port, char* ip, char* mask, char* gw) lw_print(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t*)&net_gw)[0], ((u8_t*)&net_gw)[1], ((u8_t*)&net_gw)[2], ((u8_t*)&net_gw)[3]); lw_print("************************************************\r\n"); + + lwip_config_input(&gnetif); } void lwip_config_net(uint8_t enet_port, char* ip, char* mask, char* gw)