Add mongoose

This commit is contained in:
TXuian
2023-11-09 17:25:03 +08:00
parent 71e3c8a654
commit 0f2e05aed4
247 changed files with 0 additions and 127943 deletions
@@ -1,3 +0,0 @@
config BSP_USING_ETH
bool
default y
@@ -1,3 +0,0 @@
SRC_FILES := ethernetif.c eth_driver.c eth_netdev.c
include $(KERNEL_ROOT)/compiler.mk
@@ -1,287 +0,0 @@
/**
* @file ethernetif.c
* @brief support hc32f4a0-board ethernetif function and register to Lwip
* @version 3.0
* @author AIIT XUOS Lab
* @date 2022-12-05
*/
#include <connect_ethernet.h>
#include <hc32_ll_fcg.h>
#include <hc32_ll_gpio.h>
#include <hc32_ll_utility.h>
#include <lwip/timeouts.h>
#include <netif/etharp.h>
#include <sys_arch.h>
void eth_irq_handler(void)
{
static x_base eth_irq_lock;
eth_irq_lock = DISABLE_INTERRUPT();
// handle irq
if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RIS)) {
sys_sem_signal(get_eth_recv_sem());
ETH_DMA_ClearStatus(ETH_DMA_FLAG_RIS | ETH_DMA_FLAG_NIS);
}
ENABLE_INTERRUPT(eth_irq_lock);
}
/**
* @brief In this function, the hardware should be initialized.
* @param netif The already initialized network interface structure for this ethernetif.
* @retval int32_t:
* - LL_OK: Initialize success
* - LL_ERR: Initialize failed
*/
int32_t low_level_init(struct netif* netif)
{
int32_t i32Ret = LL_ERR;
stc_eth_init_t stcEthInit;
uint16_t u16RegVal;
/* Enable ETH clock */
FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_ETHMAC, ENABLE);
/* Init Ethernet GPIO */
Ethernet_GpioInit();
/* Reset ETHERNET */
(void)ETH_DeInit();
/* Configure structure initialization */
(void)ETH_CommStructInit(&EthHandle.stcCommInit);
(void)ETH_StructInit(&stcEthInit);
#ifdef ETH_INTERFACE_RMII
EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_RMII;
#else
EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_MII;
#endif
// stcEthInit.stcMacInit.u32ReceiveAll = ETH_MAC_RX_ALL_ENABLE;
EthHandle.stcCommInit.u32ReceiveMode = ETH_RX_MD_INT;
// EthHandle.stcCommInit.u32ChecksumMode = ETH_MAC_CHECKSUM_MD_SW;
// install irq
sys_sem_new(get_eth_recv_sem(), 0);
hc32_install_irq_handler(&eth_irq_config, eth_irq_handler, ENABLE);
/* Configure ethernet peripheral */
if (LL_OK == ETH_Init(&EthHandle, &stcEthInit)) {
u8EthInitStatus = 1U;
i32Ret = LL_OK;
}
#ifdef ETHERNET_LOOPBACK_TEST
/* Enable PHY loopback */
(void)ETH_PHY_LoopBackCmd(&EthHandle, ENABLE);
#endif
/* Initialize Tx Descriptors list: Chain Mode */
(void)ETH_DMA_TxDescListInit(&EthHandle, EthDmaTxDscrTab, &EthTxBuff[0][0], ETH_TX_BUF_NUM);
/* Initialize Rx Descriptors list: Chain Mode */
(void)ETH_DMA_RxDescListInit(&EthHandle, EthDmaRxDscrTab, &EthRxBuff[0][0], ETH_RX_BUF_NUM);
/* set MAC hardware address length */
netif->hwaddr_len = 6U;
/* set MAC hardware address */
EthHandle.stcCommInit.u16AutoNego = ETH_AUTO_NEGO_DISABLE;
netif->hwaddr[0] = (EthHandle.stcCommInit).au8MacAddr[0];
netif->hwaddr[1] = (EthHandle.stcCommInit).au8MacAddr[1];
netif->hwaddr[2] = (EthHandle.stcCommInit).au8MacAddr[2];
netif->hwaddr[3] = (EthHandle.stcCommInit).au8MacAddr[3];
netif->hwaddr[4] = (EthHandle.stcCommInit).au8MacAddr[4];
netif->hwaddr[5] = (EthHandle.stcCommInit).au8MacAddr[5];
/* maximum transfer unit */
netif->mtu = 1500U;
/* device capabilities */
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
/* Enable MAC and DMA transmission and reception */
(void)ETH_Start();
/* Configure PHY LED mode */
u16RegVal = PHY_PAGE_ADDR_7;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
(void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
MODIFY_REG16(u16RegVal, PHY_LED_SELECT, PHY_LED_SELECT_10);
(void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
u16RegVal = PHY_PAGE_ADDR_0;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
#ifdef ETH_INTERFACE_RMII
/* Disable Power Saving Mode */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_PSMR, &u16RegVal);
CLR_REG16_BIT(u16RegVal, PHY_EN_PWR_SAVE);
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSMR, u16RegVal);
/* Configure PHY to generate an interrupt when Eth Link state changes */
u16RegVal = PHY_PAGE_ADDR_7;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
/* Enable Interrupt on change of link status */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
SET_REG16_BIT(u16RegVal, PHY_INT_LINK_CHANGE);
(void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
u16RegVal = PHY_PAGE_ADDR_0;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
#endif
return i32Ret;
}
/**
* @brief This function should do the actual transmission of the packet.
* @param netif The network interface structure for this ethernetif.
* @param p The MAC packet to send.
* @retval int32_t:
* - LL_OK: The packet could be sent
* - LL_ERR: The packet couldn't be sent
*/
err_t low_level_output(struct netif* netif, struct pbuf* p)
{
err_t i32Ret;
struct pbuf* q;
uint8_t* txBuffer;
__IO stc_eth_dma_desc_t* DmaTxDesc;
uint32_t byteCnt;
uint32_t frameLength = 0UL;
uint32_t bufferOffset;
uint32_t payloadOffset;
DmaTxDesc = EthHandle.stcTxDesc;
txBuffer = (uint8_t*)((EthHandle.stcTxDesc)->u32Buf1Addr);
bufferOffset = 0UL;
/* Copy frame from pbufs to driver buffers */
for (q = p; q != NULL; q = q->next) {
/* If this buffer isn't available, goto error */
if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN)) {
i32Ret = (err_t)ERR_USE;
goto error;
}
/* Get bytes in current buffer */
byteCnt = q->len;
payloadOffset = 0UL;
/* Check if the length of data to copy is bigger than Tx buffer size */
while ((byteCnt + bufferOffset) > ETH_TX_BUF_SIZE) {
/* Copy data to Tx buffer*/
(void)memcpy((uint8_t*)&(txBuffer[bufferOffset]), (uint8_t*)&(((uint8_t*)q->payload)[payloadOffset]), (ETH_TX_BUF_SIZE - bufferOffset));
/* Point to next descriptor */
DmaTxDesc = (stc_eth_dma_desc_t*)(DmaTxDesc->u32Buf2NextDescAddr);
/* Check if the buffer is available */
if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN)) {
i32Ret = (err_t)ERR_USE;
goto error;
}
txBuffer = (uint8_t*)(DmaTxDesc->u32Buf1Addr);
byteCnt = byteCnt - (ETH_TX_BUF_SIZE - bufferOffset);
payloadOffset = payloadOffset + (ETH_TX_BUF_SIZE - bufferOffset);
frameLength = frameLength + (ETH_TX_BUF_SIZE - bufferOffset);
bufferOffset = 0UL;
}
/* Copy the remaining bytes */
(void)memcpy((uint8_t*)&(txBuffer[bufferOffset]), (uint8_t*)&(((uint8_t*)q->payload)[payloadOffset]), byteCnt);
bufferOffset = bufferOffset + byteCnt;
frameLength = frameLength + byteCnt;
}
/* Prepare transmit descriptors to give to DMA */
if (LL_OK != ETH_DMA_SetTransFrame(&EthHandle, frameLength)) {
KPrintf("[%s] Error sending eth DMA frame\n", __func__);
}
i32Ret = (err_t)ERR_OK;
error:
/* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_UNS)) {
/* Clear DMA UNS flag */
ETH_DMA_ClearStatus(ETH_DMA_FLAG_UNS);
/* Resume DMA transmission */
WRITE_REG32(CM_ETH->DMA_TXPOLLR, 0UL);
}
return i32Ret;
}
/**
* @brief Should allocate a pbuf and transfer the bytes of the incoming packet from the interface into the pbuf.
* @param netif The network interface structure for this ethernetif.
* @retval A pbuf filled with the received packet (including MAC header) or NULL on memory error.
*/
struct pbuf* low_level_input(struct netif* netif)
{
struct pbuf* p = NULL;
struct pbuf* q;
uint32_t len;
uint8_t* rxBuffer;
__IO stc_eth_dma_desc_t* DmaRxDesc;
uint32_t byteCnt;
uint32_t bufferOffset;
uint32_t payloadOffset;
uint32_t i;
/* Get received frame */
if (LL_OK != ETH_DMA_GetReceiveFrame(&EthHandle)) {
return NULL;
}
/* Obtain the size of the packet */
len = (EthHandle.stcRxFrame).u32Len;
rxBuffer = (uint8_t*)(EthHandle.stcRxFrame).u32Buf;
if (len > 0UL) {
/* Allocate a pbuf chain of pbufs from the buffer */
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
// p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
}
if (p != NULL) {
DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
bufferOffset = 0UL;
for (q = p; q != NULL; q = q->next) {
byteCnt = q->len;
payloadOffset = 0UL;
/* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size */
while ((byteCnt + bufferOffset) > ETH_RX_BUF_SIZE) {
/* Copy data to pbuf */
(void)memcpy((uint8_t*)&(((uint8_t*)q->payload)[payloadOffset]), (uint8_t*)&(rxBuffer[bufferOffset]), (ETH_RX_BUF_SIZE - bufferOffset));
/* Point to next descriptor */
DmaRxDesc = (stc_eth_dma_desc_t*)(DmaRxDesc->u32Buf2NextDescAddr);
rxBuffer = (uint8_t*)(DmaRxDesc->u32Buf1Addr);
byteCnt = byteCnt - (ETH_RX_BUF_SIZE - bufferOffset);
payloadOffset = payloadOffset + (ETH_RX_BUF_SIZE - bufferOffset);
bufferOffset = 0UL;
}
/* Copy remaining data in pbuf */
(void)memcpy((uint8_t*)&(((uint8_t*)q->payload)[payloadOffset]), (uint8_t*)&(rxBuffer[bufferOffset]), byteCnt);
bufferOffset = bufferOffset + byteCnt;
}
}
/* Release descriptors to DMA */
DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
for (i = 0UL; i < (EthHandle.stcRxFrame).u32SegCount; i++) {
DmaRxDesc->u32ControlStatus |= ETH_DMA_RXDESC_OWN;
DmaRxDesc = (stc_eth_dma_desc_t*)(DmaRxDesc->u32Buf2NextDescAddr);
}
/* Clear Segment_Count */
(EthHandle.stcRxFrame).u32SegCount = 0UL;
/* When Rx Buffer unavailable flag is set, clear it and resume reception */
if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RUS)) {
/* Clear DMA RUS flag */
ETH_DMA_ClearStatus(ETH_DMA_FLAG_RUS);
/* Resume DMA reception */
WRITE_REG32(CM_ETH->DMA_RXPOLLR, 0UL);
}
return p;
}
extern void LwipSetIPTest(int argc, char* argv[]);
int HwEthInit(void)
{
// lwip_config_tcp(0, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
LwipSetIPTest(1, NULL);
return EOK;
}
@@ -1,200 +0,0 @@
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*/
/**
* @file eth_netdev.c
* @brief register net dev function for lwip
* @version 3.0
* @author AIIT XUOS Lab
* @date 2023-08-07
*/
#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];
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);
}
@@ -1,552 +0,0 @@
/**
*******************************************************************************
* @file eth/eth_loopback/source/ethernetif.c
* @brief This file implements Ethernet network interface drivers.
@verbatim
Change Logs:
Date Author Notes
2022-03-31 CDT First version
@endverbatim
*******************************************************************************
* Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by XHSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/**
* @file ethernetif.c
* @brief support hc32f4a0-board ethernetif function and register to Lwip
* @version 3.0
* @author AIIT XUOS Lab
* @date 2022-12-05
*/
/*************************************************
File name: ethernetif.c
Description: support edu-arm32-board ethernetif configure and register to Lwip
Others: take projects\ev_hc32f4a0_lqfp176\examples\eth\eth_loopback\source\ethernetif.c for references
History:
1. Date: 2022-12-05
Author: AIIT XUOS Lab
Modification:
1、include harware_ethernetif.h、hc32_ll_eth.h、hc32_ll_gpio.h、hc32_ll_utility.h、hc32_ll_fcg.h and lwip H files;
2、modify ethernetif_init as err_t;
3、add ETH_RST_PORT and ETH_RST_PIN;
4、add ETH_LINK_LED_PORT and ETH_LINK_LED_PIN;
5、add ethernetif_config_enet_set;
6、add ETHERNET_LOOPBACK_TEST with testnetif and txPbuf;
7、modify ethernetif_init() and ethernetif_input() to support LwIP.
*************************************************/
/*******************************************************************************
* Include files
******************************************************************************/
#include <connect_ethernet.h>
#include <hc32_ll_fcg.h>
#include <hc32_ll_gpio.h>
#include <hc32_ll_utility.h>
#include <lwip/timeouts.h>
#include <netif/etharp.h>
#include <netdev.h>
#include <xs_isr.h>
#include <xs_kdbg.h>
/**
* @addtogroup HC32F4A0_DDL_Examples
* @{
*/
/**
* @addtogroup ETH_Loopback
* @{
*/
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
* @defgroup ETH_IF_Global_Functions Ethernet Interface Global Functions
* @{
*/
/**
* @brief Initializes the Ethernet GPIO.
* @param None
* @retval None
*/
void Ethernet_GpioInit(void)
{
/* ETH_RST */
stc_gpio_init_t stcGpioInit;
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinState = PIN_STAT_RST;
stcGpioInit.u16PinDir = PIN_DIR_OUT;
(void)GPIO_Init(ETH_RST_PORT, ETH_RST_PIN, &stcGpioInit);
GPIO_ResetPins(ETH_RST_PORT, ETH_RST_PIN);
SysTick_Delay(PHY_HW_RST_DELAY);
GPIO_SetPins(ETH_RST_PORT, ETH_RST_PIN);
SysTick_Delay(PHY_HW_RST_DELAY);
/* ETH_LINK_LED LED2 */
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinState = PIN_STAT_RST;
stcGpioInit.u16PinDir = PIN_DIR_OUT;
(void)GPIO_Init(ETH_LINK_LED_PORT, ETH_LINK_LED_PIN, &stcGpioInit);
GPIO_ResetPins(ETH_LINK_LED_PORT, ETH_LINK_LED_PIN);
/* Configure MII/RMII selection IO for ETH */
#ifdef ETH_INTERFACE_RMII
/* Ethernet RMII pins configuration */
/*
ETH_SMI_MDIO ----------------> PA2
ETH_SMI_MDC -----------------> PC1
ETH_RMII_TX_EN --------------> PG11
ETH_RMII_TXD0 ---------------> PG13
ETH_RMII_TXD1 ---------------> PG14
ETH_RMII_REF_CLK ------------> PA1
ETH_RMII_CRS_DV -------------> PA7
ETH_RMII_RXD0 ---------------> PC4
ETH_RMII_RXD1 ---------------> PC5
ETH_RMII_RX_ER --------------> PI10
*/
/* Configure PA1, PA2 and PA7 */
GPIO_SetFunc(GPIO_PORT_A, (GPIO_PIN_01 | GPIO_PIN_02 | GPIO_PIN_07), GPIO_FUNC_11);
/* Configure PC1, PC4 and PC5 */
GPIO_SetFunc(GPIO_PORT_C, (GPIO_PIN_01 | GPIO_PIN_04 | GPIO_PIN_05), GPIO_FUNC_11);
/* Configure PG11, PG13 and PG14 */
GPIO_SetFunc(GPIO_PORT_G, (GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14), GPIO_FUNC_11);
/* Configure PI10 */
GPIO_SetFunc(GPIO_PORT_I, GPIO_PIN_10, GPIO_FUNC_11);
#else
/* Ethernet MII pins configuration */
/*
ETH_SMI_MDIO ----------------> PA2
ETH_SMI_MDC -----------------> PC1
ETH_MII_TX_CLK --------------> PB6
ETH_MII_TX_EN ---------------> PG11
ETH_MII_TXD0 ----------------> PG13
ETH_MII_TXD1 ----------------> PG14
ETH_MII_TXD2 ----------------> PB9
ETH_MII_TXD3 ----------------> PB8
ETH_MII_RX_CLK --------------> PA1
ETH_MII_RX_DV ---------------> PA7
ETH_MII_RXD0 ----------------> PC4
ETH_MII_RXD1 ----------------> PC5
ETH_MII_RXD2 ----------------> PB0
ETH_MII_RXD3 ----------------> PB1
ETH_MII_RX_ER ---------------> PI10
ETH_MII_CRS -----------------> PH2
ETH_MII_COL -----------------> PH3
*/
/* Configure PA1, PA2 and PA7 */
GPIO_SetFunc(GPIO_PORT_A, (GPIO_PIN_01 | GPIO_PIN_02 | GPIO_PIN_07), GPIO_FUNC_11);
/* Configure PB0, PB1, PB6, PB8 and PB9 */
GPIO_SetFunc(GPIO_PORT_B, (GPIO_PIN_00 | GPIO_PIN_01 | GPIO_PIN_06 | GPIO_PIN_08 | GPIO_PIN_09), GPIO_FUNC_11);
/* Configure PC1, PC4 and PC5 */
GPIO_SetFunc(GPIO_PORT_C, (GPIO_PIN_01 | GPIO_PIN_04 | GPIO_PIN_05), GPIO_FUNC_11);
/* Configure PG11, PG13 and PG14 */
GPIO_SetFunc(GPIO_PORT_G, (GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14), GPIO_FUNC_11);
/* Configure PH2, PH3 */
GPIO_SetFunc(GPIO_PORT_H, (GPIO_PIN_02 | GPIO_PIN_03), GPIO_FUNC_11);
/* Configure PI10 */
GPIO_SetFunc(GPIO_PORT_I, GPIO_PIN_10, GPIO_FUNC_11);
#endif
}
void* ethernetif_config_enet_set(uint8_t enet_port)
{
return NONE;
}
void Time_Update_LwIP(void)
{
// no need to do
}
/**
* @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.
* @retval None
*/
void ethernetif_input(void* netif_arg)
{
struct pbuf* p;
struct netif* netif = (struct netif*)netif_arg;
x_base critical_lock;
/* Move received packet into a new pbuf */
while (1) {
sys_arch_sem_wait(get_eth_recv_sem(), WAITING_FOREVER);
while (1) {
p = low_level_input(netif);
#ifndef ETHERNET_LOOPBACK_TEST
/* Entry point to the LwIP stack */
if (p != NULL) {
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
KPrintf("ethernetif_input: IP input error\n");
pbuf_free(p);
p = NULL;
}
} else {
break;
}
#else
/* No packet could be read, silently ignore this */
if (p != NULL) {
EthernetIF_InputCallback(netif, p);
free(p);
}
#endif
}
}
}
/**
* @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
* @retval None
*/
void EthernetIF_CheckLink(struct netif* netif)
{
uint16_t u16RegVal = 0U;
static uint8_t u8PreStatus = 0U;
if (1U == u8EthInitStatus) {
u8EthInitStatus = 0U;
u8PhyLinkStatus = ETH_LINK_UP;
u8PreStatus = 1U;
/* Notify link status change */
EthernetIF_NotifyLinkChange(netif);
} else {
/* Read PHY_BSR */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BSR, &u16RegVal);
/* Check whether the link is up or down*/
if ((0x0000U != u16RegVal) && (0xFFFFU != u16RegVal)) {
if ((0U != (u16RegVal & PHY_LINK_STATUS)) && (0U == u8PreStatus)) {
u8PhyLinkStatus = ETH_LINK_UP;
u8PreStatus = 1U;
EthernetIF_LinkCallback(netif);
}
if ((0U == (u16RegVal & PHY_LINK_STATUS)) && (1U == u8PreStatus)) {
u8PhyLinkStatus = ETH_LINK_DOWN;
u8PreStatus = 0U;
EthernetIF_LinkCallback(netif);
}
}
}
}
/**
* @brief Update the netif link status.
* @param netif The network interface.
* @retval None
*/
void EthernetIF_UpdateLink(struct netif* netif)
{
uint16_t u16RegVal;
if (1U == u8EthInitStatus) {
u8EthInitStatus = 0U;
u8PhyLinkStatus = ETH_LINK_UP;
/* Notify link status change */
EthernetIF_NotifyLinkChange(netif);
} else {
u16RegVal = PHY_PAGE_ADDR_0;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
/* Read PHY_IISDR */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_IISDR, &u16RegVal);
/* Check whether the link interrupt has occurred or not */
if (0U != (u16RegVal & PHY_FLAG_LINK_STATUS_CHANGE)) {
/* Read PHY_BSR */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BSR, &u16RegVal);
if ((0x0000U != u16RegVal) && (0xFFFFU != u16RegVal)) {
if (ETH_LINK_UP != u8PhyLinkStatus) {
/* Wait until the auto-negotiation will be completed */
SysTick_Delay(2U);
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BSR, &u16RegVal);
}
/* Check whether the link is up or down*/
if (0U != (u16RegVal & PHY_LINK_STATUS)) {
u8PhyLinkStatus = ETH_LINK_UP;
} else {
u8PhyLinkStatus = ETH_LINK_DOWN;
}
EthernetIF_LinkCallback(netif);
}
}
}
}
/**
* @brief Ethernet interface periodic handle
* @param netif The network interface
* @retval None
*/
void EthernetIF_PeriodicHandle(struct netif* netif)
{
#ifndef ETH_INTERFACE_RMII
uint32_t curTick;
static uint32_t u32LinkTimer = 0UL;
curTick = SysTick_GetTick();
/* Check link status periodically */
if ((curTick - u32LinkTimer) >= LINK_TIMER_INTERVAL) {
u32LinkTimer = curTick;
EthernetIF_CheckLink(netif);
}
#endif /* ETH_INTERFACE_RMII */
}
/**
* @brief Link callback function
* @note This function is called on change of link status to update low level driver configuration.
* @param netif The network interface
* @retval None
*/
void EthernetIF_LinkCallback(struct netif* netif)
{
__IO uint32_t tickStart = 0UL;
uint16_t u16RegVal = 0U;
int32_t i32negoResult = LL_ERR;
if (ETH_LINK_UP == u8PhyLinkStatus) {
/* Restart the auto-negotiation */
if (ETH_AUTO_NEGO_DISABLE != (EthHandle.stcCommInit).u16AutoNego) {
/* Enable Auto-Negotiation */
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BCR, &u16RegVal);
u16RegVal |= PHY_AUTONEGOTIATION;
(void)ETH_PHY_WriteReg(&EthHandle, PHY_BCR, u16RegVal);
/* Wait until the auto-negotiation will be completed */
tickStart = SysTick_GetTick();
do {
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BSR, &u16RegVal);
if (PHY_AUTONEGO_COMPLETE == (u16RegVal & PHY_AUTONEGO_COMPLETE)) {
break;
}
/* Check for the Timeout (3s) */
} while ((SysTick_GetTick() - tickStart) <= 3000U);
if (PHY_AUTONEGO_COMPLETE == (u16RegVal & PHY_AUTONEGO_COMPLETE)) {
i32negoResult = LL_OK;
/* Configure ETH duplex mode according to the result of automatic negotiation */
if (0U != (u16RegVal & (PHY_100BASE_TX_FD | PHY_10BASE_T_FD))) {
(EthHandle.stcCommInit).u32DuplexMode = ETH_MAC_DUPLEX_MD_FULL;
} else {
(EthHandle.stcCommInit).u32DuplexMode = ETH_MAC_DUPLEX_MD_HALF;
}
/* Configure ETH speed according to the result of automatic negotiation */
if (0U != (u16RegVal & (PHY_100BASE_TX_FD | PHY_100BASE_TX_HD))) {
(EthHandle.stcCommInit).u32Speed = ETH_MAC_SPEED_100M;
} else {
(EthHandle.stcCommInit).u32Speed = ETH_MAC_SPEED_10M;
}
}
}
/* AutoNegotiation disable or failed*/
if (LL_ERR == i32negoResult) {
(void)ETH_PHY_ReadReg(&EthHandle, PHY_BCR, &u16RegVal);
CLR_REG16_BIT(u16RegVal, PHY_FULLDUPLEX_100M);
/* Set MAC Speed and Duplex Mode to PHY */
(void)ETH_PHY_WriteReg(&EthHandle, PHY_BCR,
((uint16_t)((EthHandle.stcCommInit).u32DuplexMode >> 3U) | (uint16_t)((EthHandle.stcCommInit).u32Speed >> 1U) | u16RegVal));
}
/* ETH MAC Re-Configuration */
ETH_MAC_SetDuplexSpeed((EthHandle.stcCommInit).u32DuplexMode, (EthHandle.stcCommInit).u32Speed);
/* Restart MAC interface */
(void)ETH_Start();
} else {
/* Stop MAC interface */
(void)ETH_Stop();
}
/* Notify link status change */
EthernetIF_NotifyLinkChange(netif);
}
/**
* @brief Ethernet interface periodic handle
* @param netif The network interface
* @retval int32_t:
* - LL_OK: The IF is link up
* - LL_ERR: The IF is link down
*/
int32_t EthernetIF_IsLinkUp(struct netif* netif)
{
return (0U != u8PhyLinkStatus) ? LL_OK : LL_ERR;
}
/**
* @brief Notify link status change.
* @param netif The network interface
* @retval None
*/
__WEAKDEF void EthernetIF_NotifyLinkChange(struct netif* netif)
{
/* This is function could be implemented in user file when the callback is needed */
if (LL_OK == EthernetIF_IsLinkUp(netif)) {
GPIO_SetPins(ETH_LINK_LED_PORT, ETH_LINK_LED_PIN);
} else {
GPIO_ResetPins(ETH_LINK_LED_PORT, ETH_LINK_LED_PIN);
}
}
/**
* @brief Input data handle callback.
* @param netif The network interface structure for this ethernetif
* @param p The MAC packet to receive
* @retval None
*/
__WEAKDEF void EthernetIF_InputCallback(struct netif* netif, struct pbuf* p)
{
/* This is function could be implemented in user file when the callback is needed */
#ifdef ETHERNET_LOOPBACK_TEST
if ((0 == (memcmp(p->payload, txPbuf.payload, p->len))) && (p->len == txPbuf.len)) {
KPrintf("eth receive data OK! \r\n");
KPrintf("receive data %d %s\n", p->len, p->payload);
} else {
KPrintf("eth receive data error! \r\n");
}
#endif
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef ETHERNET_LOOPBACK_TEST
static void EthLoopBackTask(void* parameter)
{
while (1) {
if (RESET == GPIO_ReadInputPins(USER_KEY_PORT, USER_KEY_PIN)) {
KPrintf("ready to send eth data\n");
if (LL_OK != low_level_output(&testnetif, &txPbuf)) {
KPrintf("eth send data error! \r\n");
}
}
// KPrintf("ready to receive eth loop back data\n");
/* Read a received packet */
ethernetif_input(&testnetif);
/* Handle periodic timers */
EthernetIF_PeriodicHandle(&testnetif);
}
}
static void EthLoopBackTest(void)
{
x_err_t ret = EOK;
stc_gpio_init_t stcGpioInit;
/* KEY initialize */
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinState = PIN_STAT_RST;
stcGpioInit.u16PinDir = PIN_DIR_IN;
(void)GPIO_Init(USER_KEY_PORT, USER_KEY_PIN, &stcGpioInit);
GPIO_ResetPins(USER_KEY_PORT, USER_KEY_PIN);
/* Configure the Ethernet */
(void)ethernetif_init(&testnetif);
/* fill data to txPbuf */
txPbuf.next = NULL;
txPbuf.payload = txBuf;
txPbuf.len = strlen(txBuf);
int eth_loopback_task = 0;
eth_loopback_task = KTaskCreate("eth_loopback", EthLoopBackTask, NONE,
2048, 8);
if (eth_loopback_task < 0) {
KPrintf("eth_loopback_task create failed ...%s %d.\n", __FUNCTION__, __LINE__);
return;
}
StartupKTask(eth_loopback_task);
return;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
EthLoopBackTest, EthLoopBackTest, EthLoopBackTest);
#endif
/******************************************************************************
* EOF (not truncated)
*****************************************************************************/