23/08/03 Add Netdev to xidatong-arm32

This commit is contained in:
涂煜洋
2023-08-03 14:15:26 +08:00
parent 06b9d67f2d
commit 219575898a
17 changed files with 225 additions and 80 deletions
@@ -190,44 +190,6 @@ void Time_Update_LwIP(void)
// no need to do
}
/**
* @brief Should be called at the beginning of the program to set up the network interface.
* @param netif The network interface structure for this ethernetif.
* @retval err_t:
* - LL_OK: The IF is initialized
* - LL_ERR: The IF is uninitialized
*/
err_t ethernetif_init(struct netif* netif)
{
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#ifndef ETHERNET_LOOPBACK_TEST
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
#endif
/* initialize the hardware */
if (LL_OK != low_level_init(netif)) {
return LL_ERR;
}
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 LL_OK;
}
/**
* @brief This function should be called when a packet is ready to be read from the interface.
* @param netif The network interface structure for this ethernetif.
@@ -267,6 +229,45 @@ void ethernetif_input(void* netif_arg)
}
}
/**
* @brief Should be called at the beginning of the program to set up the network interface.
* @param netif The network interface structure for this ethernetif.
* @retval err_t:
* - LL_OK: The IF is initialized
* - LL_ERR: The IF is uninitialized
*/
err_t ethernetif_init(struct netif* netif)
{
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#ifndef ETHERNET_LOOPBACK_TEST
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
// netif->linkoutput = ethernetif_linkoutput;
#endif
/* initialize the hardware */
if (LL_OK != low_level_init(netif)) {
return LL_ERR;
}
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 LL_OK;
}
/**
* @brief Check the netif link status.
* @param netif the network interface
@@ -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
@@ -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;
}
@@ -0,0 +1,163 @@
#include <connect_ethernet.h>
#include <inet.h>
#include <lwip/dhcp.h>
#include <lwip/netif.h>
#include <netdev.h>
#include <xs_kdbg.h>
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);
}
@@ -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
}