5.10 ETH 使用说明

本章说明 N32H49x 的 ETH 外设(配合 lwIP 2.2.0 协议栈)在开发套件中的目录组织、公开 API 与示例用法。

5.10.1 相关文件

下表列出该模块在开发套件中的头文件、驱动实现、示例根目录与一个示例的目录组成。

相对路径 作用
firmware/n32h49x_std_periph_driver/inc/n32h49x_eth.h ETH 外设的寄存器定义、数据类型与公开函数声明。
firmware/n32h49x_std_periph_driver/src/n32h49x_eth.c ETH 驱动实现:初始化、描述符与 DMA 收发、状态与中断查询。
middlewares/lwip-2.2.0/ lwIP 2.2.0 协议栈,为示例提供 IP、TCP、UDP 与应用层接口。
middlewares/FreeRTOSv202212.01/ 带操作系统示例使用的 FreeRTOS 内核。
projects/n32h49x_EVAL/examples/ETH/ 全部 12 个示例:HTTP_Client、HTTP_Server、iPerf_Netconn、iPerf_Socket、NetBIOS、PING、TCP_Client、TCP_Client_RAW、TCP_Server、TCP_Server_RAW、UDP、UDP_RAW。
projects/n32h49x_EVAL/examples/ETH/PING/ 无操作系统示例的目录组成:src/ 源码、inc/ 头文件、bsp/ lwIP 适配层,以及 MDK-ARM/ 与 EWARM/ 工程。

5.10.2 API 参考

搜索 API共 126 个函数

以下 126 个公开函数按 n32h49x_eth.h 的声明顺序分为 12 组;每个条目给出功能摘要与源码注释原文。

初始化与复位

ETH_DeInit、ETH_StructInit、ETH_ExternalPHYInit、ETH_Init、ETH_SoftwareReset、ETH_CSRRegisterWrite1ClearCmd

PHY 与 MDC 时钟

ETH_ConfigMDCNormalClock、ETH_ConfigMDCFastClock、ETH_ReadPHYRegister、ETH_WritePHYRegister

MAC 启停、地址与过滤

ETH_TxMACCmd、ETH_RxMACCmd、ETH_SetMACAddress、ETH_GetMACAddress、ETH_SetHashTable、ETH_MACAddressPerfectFilterCmd、ETH_SetMACAddressFilter、ETH_SetMACAddressFilterMaskBytes

VLAN 标签

ETH_RxVLANTagStructInit、ETH_RxVLANTagInit、ETH_SetVLANHashTable、ETH_SetRxVLANIdentifier

流控与低功耗唤醒

ETH_InitiatePauseControlPacket、ETH_ActivateBackpressure、ETH_PowerDownCmd、ETH_MagicPacketDetectionCmd、ETH_WakeUpPacketDetectionCmd、ETH_GlobalUnicastWakeUpCmd、ETH_ResetWakeUpPacketFilterRegPointer、ETH_SetWakeUpPacketFilterRegister、ETH_GetFlowControlBusyStatus、ETH_GetPMTFlagStatus

MMC 统计

ETH_MMCCountersReset、ETH_MMCCounterStopRolloverCmd、ETH_MMCResetOnReadCmd、ETH_MMCCounterFreezeCmd、ETH_MMCCounterHalfPreset、ETH_MMCCounterFullPreset、ETH_MMCCounterUpdateForDropBCPacketCmd、ETH_GetMMCCounterValue

时间戳与 PPS

ETH_MACTimeStampCmd、ETH_SetTimeStampUpdateMethod、ETH_MACTimeStampInit、ETH_MACTimeStampUpdate、ETH_EnableTimeStampIntTrigger、ETH_MACTimeStampAddendRegUpdate、ETH_MACTimeStampAllPacketsCmd、ETH_MACTimeStampDigBinRolloverCmd、ETH_PTPTypePacketProcesCmd、ETH_MACAddrPTPPacketFilterCmd、ETH_MACTxTimeStampStatusModeCmd、ETH_SetSubSecondIncrementValue、ETH_SetTimeStampUpdateValue、ETH_SetTimeStampAddendValue、ETH_GetTimeStampRegValue、ETH_ConfigPPSOutput、ETH_SetPPSTargetTimeValue

MTL 队列管理

ETH_MTLDropTxStatusCmd、ETH_MTLCounterPreset、ETH_MTLCounterReset、ETH_FlushTransmitQueue、ETH_GetMTLUnderflowPacketCounter、ETH_GetMTLOverflowPacketCounter、ETH_GetMTLMissedPacketCounter

DMA 描述符与收发

ETH_DMATxDescListInit、ETH_DMARxDescListInit、ETH_RxDescAssignMemory、ETH_BuildRxDescriptors、ETH_PrepareTxDescriptors、ETH_Transmit、ETH_TransmitIT、ETH_IsRxDataAvailable、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_GetRxPacketInfo、ETH_TxDMACmd、ETH_RxDMACmd、ETH_ResumeDMATransmission、ETH_ResumeDMAReception、ETH_Start、ETH_StartIT、ETH_Stop、ETH_StopIT、ETH_SetRxInterruptWatchdogTimer、ETH_GetTxDescFlagStatus

DMA 状态与查询

ETH_GetCurrentTxDescriptorAddress、ETH_GetCurrentRxDescriptorAddress、ETH_GetCurrentTxBufferAddress、ETH_GetCurrentRxBufferAddress、ETH_GetTxDMAErrorType、ETH_GetRxDMAErrorType、ETH_GetDMACh0DroppedPacketCounter、ETH_GetDMACh0ERICounter、ETH_GetDMACh0TxProcessStatus、ETH_GetDMACh0RxProcessStatus、ETH_GetDMACh0FlagStatus、ETH_GetDMAInterruptStatus、ETH_GetFlushTransmitQueueStatus

状态与中断标志查询

ETH_GetRxDescExtendedFlagStatus、ETH_GetRxDescFilterFlagStatus、ETH_GetRxDescFlagStatus、ETH_GetMACInterruptStatus、ETH_GetMACTxRxFlagStatus、ETH_GetMMCRxInterruptStatus、ETH_GetMMCTxInterruptStatus、ETH_GetMACTimeStampFlagStatus、ETH_GetMTLInterruptStatus、ETH_GetMTLQueueInterruptStatus、ETH_GetMTLCounterFlagStatus、ETH_GetSoftwareResetStatus、ETH_GetDMACh0InterruptStatus

中断标志清除与使能

ETH_ClearMACPHYInterruptFlag、ETH_ClearPMTFlag、ETH_ClearMMCRxInterruptFlag、ETH_ClearMMCTxInterruptFlag、ETH_ClearMACTimeStampFlag、ETH_ClearMACTxRxFlag、ETH_ClearMACMDIOInterruptFlag、ETH_ClearMTLQueueInterruptFlag、ETH_ClearDMACh0Flag、ETH_ClearDMACh0InterruptFlag、ETH_ConfigMACInterrupt、ETH_ConfigMMCRxInterrupt、ETH_ConfigMMCTxInterrupt、ETH_ConfigMTLQueueInterrupt、ETH_ConfigDMACh0Interrupt

void ETH_DeInit(ETH_Module* ETHx)将 ETH 外设寄存器复位为默认复位值。
源码注释
name    ETH_DeInit.
fun     DeInitializes the ETH peripheral.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_StructInit(ETH_InitType* ETH_InitParam)初始化用于配置 ETH 的 ETH_InitType 类型结构体参数;该函数通常在初始化 ETH_InitType 类型参数之前调用。
源码注释
name    ETH_StructInit.
fun     Initializes the structure parameter of type ETH_InitType used to
        initialize ETH. This function is usually called before initializing
        a parameter of type ETH_InitType.
param   ETH_InitParam :
          - Pointer to the ETH_InitType structure which will be initialized.
return  none
void ETH_DMATxDescListInit(ETH_Module* ETHx, ETH_InfoType* pInfo)初始化 ETH DMA 发送描述符。
源码注释
name    ETH_DMATxDescListInit.
fun     Initialize the ETH DMA transmit descriptor.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  none
void ETH_DMARxDescListInit(ETH_Module* ETHx, ETH_InfoType* pInfo)初始化 ETH DMA 接收描述符。
源码注释
name    ETH_DMARxDescListInit.
fun     Initialize the ETH DMA Receive descriptor.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  none
void ETH_ConfigMDCNormalClock(ETH_Module* ETHx)根据 CSR 时钟频率(HclkFreq)配置 MDC 正常时钟频率(1.0MHz ≤ MDC 时钟 ≤ 2.5MHz)。
源码注释
name    ETH_ConfigMDCNormalClock.
fun     Configure the normal clock frequency (1.0MHz ≤ MDC clock ≤ 2.5MHz) of the
        MDC based on the CSR clock frequency(HclkFreq),
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_ConfigMDCFastClock(ETH_Module* ETHx, uint32_t DivValue)根据 CSR 时钟频率(HclkFreq)配置 MDC 快速时钟频率(MDC 时钟大于 2.5MHz)。
源码注释
name    ETH_ConfigMDCFastClock.
fun     Configure the fast clock frequency (MDC clock > 2.5MHz) of the MDC based
        on the CSR clock frequency(HclkFreq),
param   ETHx (The input parameters must be the following values):
          - ETH
param   DivValue (The input parameters must be the following values):
          - ETH_MDCFCLK_CRSCLK_DIV4
          - ETH_MDCFCLK_CRSCLK_DIV6
          - ETH_MDCFCLK_CRSCLK_DIV8
          - ETH_MDCFCLK_CRSCLK_DIV10
          - ETH_MDCFCLK_CRSCLK_DIV12
          - ETH_MDCFCLK_CRSCLK_DIV14
          - ETH_MDCFCLK_CRSCLK_DIV16
          - ETH_MDCFCLK_CRSCLK_DIV18
return  none
note    Configure for fast clocks is only supported if the interface chip supports
        faster MDC clocks.
uint32_t ETH_ReadPHYRegister(ETH_Module* ETHx, uint32_t PHYAddr, uint32_t PHYReg, uint32_t* pRegValue)读取一个 PHY 寄存器。
源码注释
name    ETH_ReadPHYRegister.
fun     Read a PHY register.
param   ETHx (The input parameters must be the following values):
          - ETH
param   PHYAddr :
          - PHY port address, must be a value from 0 to 31.
param   PHYReg :
          - PHY register address, must be a value from 0 to 31.
param   pRegValue :
          - Points to the address where the read PHY register value is stored.
return  0 or 1.
uint32_t ETH_WritePHYRegister(ETH_Module* ETHx, uint32_t PHYAddr, uint32_t PHYReg, uint32_t PHYRegValue)读取一个 PHY 寄存器。
源码注释
name    ETH_WritePHYRegister.
fun     Read a PHY register.
param   ETHx (The input parameters must be the following values):
          - ETH
param   PHYAddr :
          - PHY port address, must be a value from 0 to 31.
param   PHYReg :
          - PHY register address, must be a value from 0 to 31.
param   PHYRegValue :
          - The value to write.
return  0 or 1.
EthFuncStatusType ETH_ExternalPHYInit(ETH_Module* ETHx, ETH_InfoType* pInfo, ETH_InitType* ETH_InitParam)初始化外部 PHY,包括与是否使能 PHY 自协商功能相关的设置。
源码注释
name    ETH_ExternalPHYInit.
fun     Initialize the external PHY, including settings related to whether or
        not to enable the PHY auto-negotiation function.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   ETH_InitParam :
          - Pointer to the ETH_InitType structure which will be initialized.
return  One of EthFuncStatusType.
EthFuncStatusType ETH_Init(ETH_Module* ETHx, ETH_InfoType* pInfo, ETH_InitType* ETH_InitParam)使用 ETH_InitType 类型的 ETH_InitParam 初始化 ETH 模块,包括 ETH MAC 初始化、MTL 初始化、ETH DMA 初始化等。
源码注释
name    ETH_Init.
fun     Initialize the ETH module with ETH_InitParam of type ETH_InitType, including
        ETH MAC initialization, MTL initialization, ETH DMA initialization, etc.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   ETH_InitParam :
          - Pointer to the ETH_InitType structure which will be initialized.
return  One of EthFuncStatusType.
void ETH_TxMACCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 发送。
源码注释
name    ETH_TxMACCmd.
fun     Enables or disables the MAC transmission.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_RxMACCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 接收。
源码注释
name    ETH_RxMACCmd.
fun     Enables or disables the MAC reception.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_SetHashTable(ETH_Module* ETHx, uint32_t *pHashTable)设置 ETH 哈希表值。
源码注释
name    ETH_SetHashTable.
fun     Set the ETH Hash Table Value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pHashTable :
          - Pointer to a table of two 32 bit values, that contains the 64
            bits of the hash table.
return  none
void ETH_RxVLANTagStructInit(ETH_RxVLANTagInitType* ETH_RxVTInitParam)初始化用于配置 VLAN 标签的 ETH_RxVLANTagInitType 类型结构体参数;该函数通常在初始化 ETH_RxVLANTagInitType 类型参数之前调用。
源码注释
name    ETH_RxVLANTagStructInit.
fun     Initializes the structure parameter of type ETH_RxVLANTagInitType used to
        initialize VLAN tag. This function is usually called before initializing
        a parameter of type ETH_RxVLANTagInitType.
param   ETH_InitParam :
          - Pointer to the ETH_RxVLANTagInitType structure which will be initialized.
return  none
void ETH_RxVLANTagInit(ETH_Module* ETHx, ETH_RxVLANTagInitType* ETH_RxVTInitParam)配置接收路径上的 VLAN 标签处理。
源码注释
name    ETH_RxVLANTagInit.
fun     Initialize VLAN tag processing on the receive path.
param   ETHx (The input parameters must be the following values):
          - ETH
param   ETH_RxVTInitParam :
          - Pointer to the ETH_RxVLANTagInitType structure which will be initialized.
return  none
void ETH_SetVLANHashTable(ETH_Module* ETHx, uint16_t HashTable)设置 ETH VLAN 哈希表值。
源码注释
name    ETH_SetVLANHashTable.
fun     Set the ETH VLAN Hash Table Value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   HashTable :
          - VLAN hash table 16 bit value.
return  none
void ETH_SetRxVLANIdentifier(ETH_Module* ETHx, uint32_t VLANIdentifier)设置接收数据包的 VLAN 标识符。
源码注释
name    ETH_SetRxVLANIdentifier.
fun     Set the VLAN Identifier for Rx packets.
param   ETHx (The input parameters must be the following values):
          - ETH
param   VLANIdentifier :
          - VLAN Identifier value.
return  none
void ETH_InitiatePauseControlPacket(ETH_Module* ETHx)在发送流控操作期间发起一次暂停控制包传输。
源码注释
name    ETH_InitiatePauseControlPacket.
fun     Initiates a pause control packet transmission during TX flow control
        operation.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    The pause packet function is only valid when the MAC is configured
        in full-duplex mode.
note    During control packet transmission, this bit remains set to 1 to
        indicate that packet transmission is in progress. When pause packet
        transmission is complete, the MAC resets this bit to 0. No write
        operations should be performed to this register until this bit is
        cleared.
void ETH_ActivateBackpressure(ETH_Module* ETHx)在发送流控操作期间激活背压功能。
源码注释
name    ETH_ActivateBackpressure.
fun     Activate backpressure function during TX flow control operation.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    Activate backpressure function is only valid when the MAC is configured
        in half-duplex mode.
note    during backpressure, when the MAC receives a new frame, the transmitter
        starts sending a JAM pattern resulting in a collision.
note    BPA is automatically disabled when the MAC is configured in full-duplex
        mode.
void ETH_PowerDownCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 ETH 掉电模式。
源码注释
name    ETH_PowerDownCmd.
fun     Enables or disables the ETH Power Down mode.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MagicPacketDetectionCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 魔术包检测。
源码注释
name    ETH_MagicPacketDetectionCmd.
fun     Enables or disables the MAC Magic Packet Detection.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_WakeUpPacketDetectionCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 远程唤醒包检测。
源码注释
name    ETH_WakeUpPacketDetectionCmd.
fun     Enables or disables the MAC Remote Wake-Up Packet Detection.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_GlobalUnicastWakeUpCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能将任意经 MAC 地址识别过滤的单播包作为唤醒包。
源码注释
name    ETH_GlobalUnicastWakeUpCmd.
fun     Enables or disables any unicast packet filtered by the MAC
        address recognition to be a wake-up packet.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ResetWakeUpPacketFilterRegPointer(ETH_Module* ETHx)复位唤醒包过滤器寄存器指针。
源码注释
name    ETH_ResetWakeUpPacketFilterRegPointer.
fun     Reset Wakeup packet filter register pointer.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_SetWakeUpPacketFilterRegister(ETH_Module* ETHx, uint32_t *pBuffer)填充远程唤醒包寄存器。
源码注释
name    ETH_SetWakeUpPacketFilterRegister.
fun     Populates the remote wakeup packet registers.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pBuffer :
          - Pointer on remote WakeUp Packet Filter Register buffer data (8 words).
return  none
note    When MAC RWK Packet Filter register is written, the content is transferred
        from CSR clock domain to PHY receive clock domain after the write operation,
        there should not be any further write to the MAC RWK Packet Filter register
        until the first write is updated in PHY receive clock domain. Otherwise, the
        second write operation does not get updated to the PHY receive clock domain.
        Therefore, the delay between two writes to the MAC RWK Packet Filter register
        should be at least 4 cycles of the PHY receive clock.
void ETH_CSRRegisterWrite1ClearCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能部分 CSR 寄存器的写 1 清零模式。
源码注释
name    ETH_CSRRegisterWrite1ClearCmd.
fun     Enables or disables Write 1 clear mode for some CSR registers.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_SetMACAddress(ETH_Module* ETHx, EthMacAddr0123Type Offset, uint8_t* pAddr)由应用层设置 MAC 地址,支持配置 MAC 地址寄存器 0~3。
源码注释
name    ETH_SetMACAddress.
fun     Set MAC address by application layer setting. Support configuring
        MAC address registers 0~3.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MAC_ADDR0_OFFSET
          - ETH_MAC_ADDR1_OFFSET
          - ETH_MAC_ADDR2_OFFSET
          - ETH_MAC_ADDR3_OFFSET
param   pAddr:
          - Pointer on MAC address buffer
return  none
void ETH_GetMACAddress(ETH_Module* ETHx, EthMacAddr0123Type Offset, uint8_t* pAddr)由应用层获取 MAC 地址,支持读取 MAC 地址寄存器 0~3。
源码注释
name    ETH_GetMACAddress.
fun     Get MAC address by application layer setting. Support get MAC address
        registers 0~3.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MAC_ADDR0_OFFSET
          - ETH_MAC_ADDR1_OFFSET
          - ETH_MAC_ADDR2_OFFSET
          - ETH_MAC_ADDR3_OFFSET
param   pAddr:
          - Pointer on MAC address buffer
return  none
void ETH_MACAddressPerfectFilterCmd(ETH_Module* ETHx, EthMacAddr123Type Offset, FunctionalStatus Cmd)使能或失能地址过滤模块使用指定的 ETH MAC 地址(MAC 地址 1~3)进行完美过滤。
源码注释
name    ETH_MACAddressPerfectFilterCmd.
fun     Enables or disables the Address filter module uses the specified ETH
        MAC address (MAC address 1 ~ MAC address 3) for perfect filtering.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MACADDR1_OFFSET
          - ETH_MACADDR2_OFFSET
          - ETH_MACADDR3_OFFSET
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_SetMACAddressFilter(ETH_Module* ETHx, EthMacAddr123Type Offset, EthMacAddrFilterType Filter)设置指定的 ETH MAC 地址(MAC 地址 1~3)的过滤器类型。
源码注释
name    ETH_SetMACAddressFilter.
fun     Set the filter type for the specified ETH MAC address (MAC address 1 ~
        MAC address 3).
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MACADDR1_OFFSET
          - ETH_MACADDR2_OFFSET
          - ETH_MACADDR3_OFFSET
param   Filter (The input parameters must be the following values):
          - ETH_MACADDR_FILTER_SA
          - ETH_MACADDR_FILTER_DA
return  none
void ETH_SetMACAddressFilterMaskBytes(ETH_Module* ETHx, EthMacAddr123Type Offset, uint32_t MaskByte)设置指定的 ETH MAC 地址(MAC 地址 1~3)的过滤掩码字节。
源码注释
name    ETH_SetMACAddressFilterMaskBytes.
fun     Set the filter mask bytes for the specified ETH MAC address (MAC address 1 ~
        MAC address 3).
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MACADDR1_OFFSET
          - ETH_MACADDR2_OFFSET
          - ETH_MACADDR3_OFFSET
param   MaskByte (The input parameters must be the following values):
          - ETH_MACADDR_MASK_BYTE6
          - ETH_MACADDR_MASK_BYTE5
          - ETH_MACADDR_MASK_BYTE4
          - ETH_MACADDR_MASK_BYTE3
          - ETH_MACADDR_MASK_BYTE2
          - ETH_MACADDR_MASK_BYTE1
return  none
void ETH_MMCCountersReset(ETH_Module* ETHx)复位 MMC 计数器。
源码注释
name    ETH_MMCCountersReset.
fun     Resets the MMC Counters.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MMCCounterStopRolloverCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MMC 计数器停止翻转。
源码注释
name    ETH_MMCCounterStopRolloverCmd.
fun     Enables or disables the MMC Counter Stop Rollover.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MMCResetOnReadCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MMC 读时复位。
源码注释
name    ETH_MMCResetOnReadCmd.
fun     Enables or disables the MMC Reset On Read.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MMCCounterFreezeCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MMC 计数器冻结。
源码注释
name    ETH_MMCCounterFreezeCmd.
fun     Enables or disables the MMC Counter Freeze.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MMCCounterHalfPreset(ETH_Module* ETHx)将 MMC 计数器预置并初始化为接近半量程值:0x7FFF_FFF0。
源码注释
name    ETH_MMCCounterHalfPreset.
fun     Preset and Initialize the MMC counters to almost-half value:
        0x7FFF_FFF0.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MMCCounterFullPreset(ETH_Module* ETHx)将 MMC 计数器预置并初始化为接近满量程值:0xFFFF_FFF0。
源码注释
name    ETH_MMCCounterFullPreset.
fun     Preset and Initialize the MMC counters to almost-full value:
        0xFFFF_FFF0.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MMCCounterUpdateForDropBCPacketCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能为被丢弃的广播包更新 MMC 计数器。
源码注释
name    ETH_MMCCounterUpdateForDropBCPacketCmd.
fun     Enables or disables the Update MMC Counters for Dropped Broadcast
        Packets.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
uint32_t ETH_GetMMCCounterValue(ETH_Module* ETHx, EthMmcAddrType Offset)获取指定的 ETH MMC 计数器值。
源码注释
name    ETH_GetMMCCounterValue.
fun     Get the specified ETH MMC counter value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MMC_SCGP_OFFSET
          - ETH_MMC_MCGP_OFFSET
          - ETH_MMC_PCG_OFFSET
          - ETH_MMC_BPG_OFFSET
          - ETH_MMC_MPG_OFFSET
          - ETH_MMC_CRCEP_OFFSET
          - ETH_MMC_AEP_OFFSET
          - ETH_MMC_UPG_OFFSET
return  Corresponding counter value.
void ETH_MACTimeStampCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 时间戳。
源码注释
name    ETH_MACTimeStampCmd.
fun     Enables or disables the MAC timestamp.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_SetTimeStampUpdateMethod(ETH_Module* ETHx, uint32_t Method)选择 MAC 时间戳更新方式。
源码注释
name    ETH_SetTimeStampUpdateMethod.
fun     Selects the MAC timestamp Update method.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Method (The input parameters must be the following values):
          - ETH_MACTS_FINE_UPDATE
          - ETH_MACTS_COARSE_UPDATE
return  none
void ETH_MACTimeStampInit(ETH_Module* ETHx)初始化 MAC 时间戳;用 MAC 系统时间秒更新寄存器与纳秒更新寄存器中的值初始化(覆盖)系统时间。
源码注释
name    ETH_MACTimeStampInit.
fun     Initialize the MAC timestamp. The system time is initialized
        (overwritten) with the values specified in the MAC System Time Seconds
        Update Register and the MAC System Time Nanoseconds Update Register.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MACTimeStampUpdate(ETH_Module* ETHx)更新 MAC 时间戳;用 MAC 系统时间秒更新寄存器与纳秒更新寄存器中的值更新(增加或减少)系统时间。
源码注释
name    ETH_MACTimeStampUpdate.
fun     Update the MAC timestamp. The system time is updated (added or
        subtracted) with the values specified in the MAC System Time Seconds
        Update Register and the MAC System Time Nanoseconds Update Register.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_EnableTimeStampIntTrigger(ETH_Module* ETHx)使能 MAC 时间戳中断触发;当系统时间大于目标时间寄存器中写入的值时产生时间戳中断。
源码注释
name    ETH_EnableTimeStampIntTrigger.
fun     Enable the MAC TimeStamp interrupt trigger. the timestamp interrupt is
        generated when the System Time becomes greater than the value written
        in the Target Time register.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MACTimeStampAddendRegUpdate(ETH_Module* ETHx)更新 MAC 时间戳加数寄存器;时间戳加数寄存器的内容在 PTP 模块中更新以进行精细校正。
源码注释
name    ETH_MACTimeStampAddendRegUpdate.
fun     Update the MAC timestamp addend register. the content of the Timestamp
        Addend register is updated in the PTP block for fine correction.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MACTimeStampAllPacketsCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能对所有数据包的 MAC 时间戳。
源码注释
name    ETH_MACTimeStampAllPacketsCmd.
fun     Enables or disables the MAC timestamp for All Packets.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MACTimeStampDigBinRolloverCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能数字或二进制形式的 MAC 时间戳翻转。
源码注释
name    ETH_MACTimeStampDigBinRolloverCmd.
fun     Enables or disables the MAC timestamp Rollover for Digital or Binary.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_PTPTypePacketProcesCmd(ETH_Module* ETHx, uint32_t PacketType, FunctionalStatus Cmd)使能或失能对所选 PTP 包类型的处理。
源码注释
name    ETH_PTPTypePacketProcesCmd.
fun     Enables or disables the processing for selected PTP packet type.
param   ETHx (The input parameters must be the following values):
          - ETH
param   PacketType (The input parameters must be the following values):
          - ETH_MACPTP_TYPE_VERSION2
          - ETH_MACPTP_TYPE_ETHERNET
          - ETH_MACPTP_TYPE_IPV6UDP
          - ETH_MACPTP_TYPE_IPV4UDP (default)
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MACAddrPTPPacketFilterCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能用于 PTP 包过滤的 MAC 地址。
源码注释
name    ETH_MACAddrPTPPacketFilterCmd.
fun     Enables or disables the MAC address for PTP packet filtering.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MACTxTimeStampStatusModeCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 MAC 发送时间戳状态模式。
源码注释
name    ETH_MACTxTimeStampStatusModeCmd.
fun     Enables or disables the MAC TX timestamp status mode.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_SetSubSecondIncrementValue(ETH_Module* ETHx, uint8_t IncValue)设置系统时间亚秒增量值。
源码注释
name    ETH_SetSubSecondIncrementValue.
fun     Sets the system time Sub-Second Increment value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IncValue :
          - 0~0xFF
return  none
void ETH_SetTimeStampUpdateValue(ETH_Module* ETHx, uint32_t CalMode, uint32_t SecondValue, uint32_t SubSecondValue)设置时间戳更新值。
源码注释
name    ETH_SetTimeStampUpdateValue.
fun     Sets the Time Stamp update value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   CalMode (The input parameters must be the following values):
          - ETH_MACTS_CALMODE_SUB
          - ETH_MACTS_CALMODE_ADD
param   SecondValue :
          - 0~0xFFFFFFFF
param   SubSecondValue :
          - 0~0x7FFFFFFF
return  none
note    When CalMode is set to ETH_MACTS_CALMODE_SUB, the seconds field
        and nanosecond field must be programmed with the complement of
        the corresponding part of the update value.
void ETH_SetTimeStampAddendValue(ETH_Module* ETHx, uint32_t AddValue)设置时间戳加数值。
源码注释
name    ETH_SetTimeStampAddendValue.
fun     Sets the Time Stamp addend value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   AddValue :
          - 0~0xFFFFFFFF
return  none
uint32_t ETH_GetTimeStampRegValue(ETH_Module* ETHx, EthTsAddrType Offset)获取指定的 ETH MAC 时间戳寄存器值。
源码注释
name    ETH_GetTimeStampRegValue.
fun     Gets the specified ETH MAC TimeStamp register value.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Offset (The input parameters must be the following values):
          - ETH_MACTS_SSIR_OFFSET
          - ETH_MACTS_STSR_OFFSET
          - ETH_MACTS_STNSR_OFFSET
          - ETH_MACTS_STSUR_OFFSET
          - ETH_MACTS_STNSUR_OFFSET
          - ETH_MACTS_TAR_OFFSET
          - ETH_MACTS_TTSNSR_OFFSET
          - ETH_MACTS_TTSSR_OFFSET
          - ETH_MACTS_TICNSR_OFFSET
          - ETH_MACTS_TECNSR_OFFSET
          - ETH_MACTS_TILR_OFFSET
          - ETH_MACTS_TELR_OFFSET
          - ETH_MACTS_PTTSR_OFFSET
          - ETH_MACTS_PTTNSR_OFFSET
return  Corresponding MAC TimeStamp value.
void ETH_ConfigPPSOutput(ETH_Module* ETHx, uint32_t OutputFreq)配置 PPS 输出,例如配置输出频率,默认为每秒一个脉冲。
源码注释
name    ETH_ConfigPPSOutput.
fun     Configure the PPS output, e.g. configure the output frequency,
        which defaults to one pulse per second.
param   ETHx (The input parameters must be the following values):
          - ETH
param   OutputFreq (The input parameters must be the following values):
          - ETH_MACPPS_FREQCTRL_B2D1
          - ETH_MACPPS_FREQCTRL_B4D2
          - ETH_MACPPS_FREQCTRL_B8D4
          - ETH_MACPPS_FREQCTRL_B16D8
          - ETH_MACPPS_FREQCTRL_B32D16
          - ETH_MACPPS_FREQCTRL_B64D32
          - ETH_MACPPS_FREQCTRL_B128D64
          - ETH_MACPPS_FREQCTRL_B256D128
          - ETH_MACPPS_FREQCTRL_B512D256
          - ETH_MACPPS_FREQCTRL_B1024D512
          - ETH_MACPPS_FREQCTRL_B2048D1024
          - ETH_MACPPS_FREQCTRL_B4096D2048
          - ETH_MACPPS_FREQCTRL_B8192D4096
          - ETH_MACPPS_FREQCTRL_B16384D8192
          - ETH_MACPPS_FREQCTRL_B32768D16384
return  none
void ETH_SetPPSTargetTimeValue(ETH_Module* ETHx, uint32_t SecondValue, uint32_t SubSecondValue)使用秒字段和纳秒字段设置 PPS 目标时间值。
源码注释
name    ETH_SetPPSTargetTimeValue.
fun     Sets the PPS target time value with seconds field and nanosecond field.
param   ETHx (The input parameters must be the following values):
          - ETH
param   SecondValue :
          - 0~0xFFFFFFFF
param   SubSecondValue :
          - 0~0x7FFFFFFF
return  none
void ETH_MTLDropTxStatusCmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能丢弃发送状态。
源码注释
name    ETH_MTLDropTxStatusCmd.
fun     Enables or disables the Drop Transmit Status.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_MTLCounterPreset(ETH_Module* ETHx)将 MTL 计数器预置并初始化为值:0x7F0。
源码注释
name    ETH_MTLCounterPreset.
fun     Preset and Initialize the MTL counters to value: 0x7F0.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_MTLCounterReset(ETH_Module* ETHx)复位 MTL 的所有计数器。
源码注释
name    ETH_MTLCounterReset.
fun     Reset all counters of MTL.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    The counter reset control bit is automatically cleared after 1 clock
        cycle.
void ETH_FlushTransmitQueue(ETH_Module* ETHx)清除 ETH 发送 FIFO。
源码注释
name    ETH_FlushTransmitQueue.
fun     Clears the ETH transmit FIFO.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    The MTL Transmit Queue Operation Mode Register (ETH_MTLTXQOPMOD)
        should not be written until the flush operation is complete.
        This function causes all data in the Tx FIFO to be lost or flushed.
        However, data that has been accepted by the MAC transmitter will not
        be flushed. It will be scheduled for transmission and will result in
        underflow and short packets being sent.
uint16_t ETH_GetMTLUnderflowPacketCounter(ETH_Module* ETHx)获取因发送队列下溢而被控制器中止的数据包数量。
源码注释
name    ETH_GetMTLUnderflowPacketCounter.
fun     Get the number of packets aborted by the controller due to Tx queue
        underflow.
param   ETHx (The input parameters must be the following values):
          - ETH
return  Counter value: 0~0x7FF.
uint16_t ETH_GetMTLOverflowPacketCounter(ETH_Module* ETHx)获取因接收队列溢出而被丢弃的数据包数量。
源码注释
name    ETH_GetMTLOverflowPacketCounter.
fun     Gets the number of packets discarded due to Rx queue overflow.
param   ETHx (The input parameters must be the following values):
          - ETH
return  Counter value: 0~0x7FF.
uint16_t ETH_GetMTLMissedPacketCounter(ETH_Module* ETHx)获取因应用对该队列执行包刷新请求而丢失的数据包数量。
源码注释
name    ETH_GetMTLMissedPacketCounter.
fun     Gets the number of packets lost due to the application performing
        packet refresh requests against this queue.
param   ETHx (The input parameters must be the following values):
          - ETH
return  Counter value: 0~0x7FF.
void ETH_SoftwareReset(ETH_Module* ETHx)ETH 软件复位,MAC 与 DMA 控制器复位 DMA、MTL、MAC 的逻辑及所有内部寄存器。
源码注释
name    ETH_SoftwareReset.
fun     ETH software reset, the MAC and the DMA controller reset the logic
        and all internal registers of the DMA, MTL, and MAC.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    1. The software reset status bit (SWR) is automatically cleared after
        the reset operation is completed.
        2. Before reprogramming any ETH register, a value of zero should be
        read in SWR bit.
        3. The SWR bit must be read at least 4 CSR clock cycles after it is
        written to 1.
void ETH_TxDMACmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 DMA 发送。
源码注释
name    ETH_TxDMACmd.
fun     Enables or disables the DMA transmission.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_RxDMACmd(ETH_Module* ETHx, FunctionalStatus Cmd)使能或失能 DMA 接收。
源码注释
name    ETH_RxDMACmd.
fun     Enables or disables the DMA reception.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ResumeDMATransmission(ETH_Module* ETHx)当发送 DMA 进入挂起模式时,通过向发送描述符尾指针寄存器写入任意值来恢复发送 DMA。
源码注释
name    ETH_ResumeDMATransmission.
fun     Resume TX DMA by writing any value to transmit descriptor tail pointer
        register when TX DMA enters suspend mode.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    This function is only applicable when the DMA enters suspend mode.
void ETH_ResumeDMAReception(ETH_Module* ETHx)当接收 DMA 进入挂起模式时,通过向接收描述符尾指针寄存器写入任意值来恢复接收 DMA。
源码注释
name    ETH_ResumeDMAReception.
fun     Resume RX DMA by writing any value to receive descriptor tail pointer
        register when RX DMA enters suspend mode.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
note    This function is only applicable when the DMA enters suspend mode.
void ETH_SetRxInterruptWatchdogTimer(ETH_Module* ETHx, uint32_t CntUnit, uint32_t CntValue)设置 DMA 接收中断的看门狗定时器。
源码注释
name    ETH_SetRxInterruptWatchdogTimer.
fun     Set the watchdog timer for the DMA receive interrupt.
param   ETHx (The input parameters must be the following values):
          - ETH
param   CntUnit (The input parameters must be the following values):
          - ETH_DMARXINT_WDGTIM_CNTUNIT_256
          - ETH_DMARXINT_WDGTIM_CNTUNIT_512
          - ETH_DMARXINT_WDGTIM_CNTUNIT_1024
          - ETH_DMARXINT_WDGTIM_CNTUNIT_2048
param   CntValue :
          - Count value: 0~0x000000FF.
return  none
note    Timer cycles = count unit * count value (CntUnit * CntValue) system
        clock cycles.
uint32_t ETH_GetCurrentTxDescriptorAddress(ETH_Module* ETHx)获取 DMA 当前读取的应用发送描述符地址。
源码注释
name    ETH_GetCurrentTxDescriptorAddress.
fun     Gets the address of the current application transmit descriptor read
        by the DMA.
param   ETHx (The input parameters must be the following values):
          - ETH
return  The value of the current Tx descriptor address.
uint32_t ETH_GetCurrentRxDescriptorAddress(ETH_Module* ETHx)获取 DMA 当前读取的应用接收描述符地址。
源码注释
name    ETH_GetCurrentRxDescriptorAddress.
fun     Gets the address of the current application receive descriptor read
        by the DMA.
param   ETHx (The input parameters must be the following values):
          - ETH
return  The value of the current Rx descriptor address.
uint32_t ETH_GetCurrentTxBufferAddress(ETH_Module* ETHx)获取 DMA 当前读取的应用发送缓冲区地址。
源码注释
name    ETH_GetCurrentTxBufferAddress.
fun     Gets the address of the current application transmit buffer read
        by the DMA.
param   ETHx (The input parameters must be the following values):
          - ETH
return  The value of the current Tx buffer address.
uint32_t ETH_GetCurrentRxBufferAddress(ETH_Module* ETHx)获取 DMA 当前读取的应用接收缓冲区地址。
源码注释
name    ETH_GetCurrentRxBufferAddress.
fun     Gets the address of the current application receive buffer read
        by the DMA.
param   ETHx (The input parameters must be the following values):
          - ETH
return  The value of the current Rx buffer address.
uint8_t ETH_GetTxDMAErrorType(ETH_Module* ETHx)获取导致致命总线错误的发送 DMA 错误类型。
源码注释
name    ETH_GetTxDMAErrorType.
fun     Get the type of TX DMA error that caused the fatal bus error.
param   ETHx (The input parameters must be the following values):
          - ETH
return  TX DMA Error Bits[2:0] Value:
          - bit[0]:
            - 1: Error during read transfer
            - 0: Error during write transfer
          - bit[1]:
            - 1: Error during descriptor access
            - 0: Error during data buffer access
          - bit[2]:
            - 1: Error during data transfer by Tx DMA
            - 0: No Error during data transfer by Tx DMA
        Invalid value:
          - 0xFF
note    This function returns a valid value only if a fatal bus error occurs
        (FBE set to 1). And these error types do not generate interrupts.
uint8_t ETH_GetRxDMAErrorType(ETH_Module* ETHx)获取导致致命总线错误的接收 DMA 错误类型。
源码注释
name    ETH_GetRxDMAErrorType.
fun     Get the type of RX DMA error that caused the fatal bus error.
param   ETHx (The input parameters must be the following values):
          - ETH
return  RX DMA Error Bits[2:0] Value:
          - bit[0]:
            - 1: Error during read transfer
            - 0: Error during write transfer
          - bit[1]:
            - 1: Error during descriptor access
            - 0: Error during data buffer access
          - bit[2]:
            - 1: Error during data transfer by Rx DMA
            - 0: No Error during data transfer by Rx DMA
        Invalid value:
          - 0xFF
note    This function returns a valid value only if a fatal bus error occurs
        (FBE set to 1). And these error types do not generate interrupts.
uint16_t ETH_GetDMACh0DroppedPacketCounter(ETH_Module* ETHx)获取 DMA 因总线错误或配置 DMA 通道 0 接收控制寄存器中的 RPF 字段而丢弃的包计数。
源码注释
name    ETH_GetDMACh0DroppedPacketCounter.
fun     Get the number of packet counters dropped by the DMA due to bus errors
        or programming the RPF field in the DMA Channel 0 Rx Control Register.
param   ETHx (The input parameters must be the following values):
          - ETH
return  Counter value: 0~0x7FF.
uint16_t ETH_GetDMACh0ERICounter(ETH_Module* ETHx)获取提前接收中断(ERI)有效次数的计数。
源码注释
name    ETH_GetDMACh0ERICounter.
fun     Get the number of counts for the number of times an ERI (Early Receive
        Interrupt) is valid.
param   ETHx (The input parameters must be the following values):
          - ETH
return  Counter value: 0~0xFFF.
EthFuncStatusType ETH_RxDescAssignMemory(ETH_InfoType* pInfo, uint32_t Index, uint8_t* pBuffer1, uint8_t* pBuffer2)为 DMA 接收描述符分配内存缓冲区。
源码注释
name    ETH_RxDescAssignMemory.
fun     Assign memory buffers to a DMA Rx descriptor.
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   Index :
          - Index of the DMA Rx descriptor.
param   pBuffer1 :
          - Points to Buffer1 address.
param   pBuffer2 :
          - Points to Buffer2 address, if available.
return  One of EthFuncStatusType.
EthFuncStatusType ETH_BuildRxDescriptors(ETH_Module* ETHx, ETH_InfoType* pInfo)将最后接收数据包的接收描述符归还给 DMA,使 ETH DMA 能够使用这些描述符接收后续数据包;应在处理完接收数据包后调用。
源码注释
name    ETH_BuildRxDescriptors.
fun     This function gives back Rx Desc of the last received Packet to the DMA,
        so ETH DMA will be able to use these descriptors to receive next Packets.
        It should be called after processing the received Packet.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  One of EthFuncStatusType.
EthFuncStatusType ETH_PrepareTxDescriptors(ETH_Module* ETHx, ETH_InfoType* pInfo, ETH_TxPacketType* pTxPacket, uint32_t ItMode)在发送之前准备发送 DMA 描述符。
源码注释
name    ETH_PrepareTxDescriptors.
fun     Prepare Tx DMA descriptor before transmission.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   pTxPacket :
          - Points to the TX packet, containing some configuration information
param   ItMode :
          - Mode Selection: 1 indicates to interrupt mode, 0 indicates to polling mode.
return  One of EthFuncStatusType.
EthFuncStatusType ETH_Transmit(ETH_Module* ETHx, ETH_InfoType* pInfo, ETH_TxPacketType* pTxPacket, uint32_t Timeout)通过轮询模式发送数据包。
源码注释
name    ETH_Transmit.
fun     Send packets via polling mode.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   pTxPacket :
          - Points to the TX packet, containing some configuration information
param   Timeout :
          - timeout value
return  One of EthFuncStatusType.
EthFuncStatusType ETH_TransmitIT(ETH_Module* ETHx, ETH_InfoType* pInfo, ETH_TxPacketType* pTxPacket)通过中断模式发送数据包。
源码注释
name    ETH_TransmitIT.
fun     Send packets via interrupt mode.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   pTxPacket :
          - Points to the TX packet, containing some configuration information
return  One of EthFuncStatusType.
uint8_t ETH_IsRxDataAvailable(ETH_InfoType* pInfo)检查接收数据包的数据。
源码注释
name    ETH_IsRxDataAvailable.
fun     Checks the data of received packets.
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  1 or 0.
EthFuncStatusType ETH_GetRxDataBuffer(ETH_InfoType* pInfo, ETH_BufferType* pRxBuffer)用于在处理接收数据包时获取数据缓冲区。
源码注释
name    ETH_GetRxDataBuffer.
fun     Used to fetch the data buffer when processing the received Packet.
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   pRxBuffer :
          - Points to the RX buffer, ETH_BufferType structure.
return  One of EthFuncStatusType.
uint16_t ETH_GetRxDataLength(ETH_InfoType* pInfo)用于在处理接收数据包时获取最后接收数据包的数据长度。
源码注释
name    ETH_GetRxDataLength.
fun     Used to fetch the data length of last received Packet when processing the
        received Packet.
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  0 or length of last received Packet.
EthFuncStatusType ETH_GetRxPacketInfo(ETH_InfoType* pInfo, ETH_RxPacketType* pRxPacket)获取接收数据包信息(包类型、VLAN 标签、过滤状态等)。
源码注释
name    ETH_GetRxPacketInfo.
fun     Get the Rx Packet info (Packet type, VLAN tag, Filters status, ...).
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
param   pRxPacket :
          - Points to the RX packet, containing some packet information.
return  One of EthFuncStatusType.
void ETH_Start(ETH_Module* ETHx)通过轮询模式打开 ETH,包括打开发送与接收 DMA 引擎、打开发送与接收 MAC 控制器等。
源码注释
name    ETH_Start.
fun     Turn on ETH via polling mode, including turning on TX and RX DMA engines,
        turning on TX and RX MAC controllers, etc.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none.
void ETH_StartIT(ETH_Module* ETHx, ETH_InfoType* pInfo)通过中断模式打开 ETH,包括打开发送与接收 DMA 引擎、打开发送与接收 MAC 控制器等。
源码注释
name    ETH_StartIT.
fun     Turn on ETH via interrupt mode, including turning on TX and RX DMA engines,
        turning on TX and RX MAC controllers, etc.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  none.
void ETH_Stop(ETH_Module* ETHx)通过轮询模式关闭 ETH,包括关闭发送与接收 DMA 引擎、关闭发送与接收 MAC 控制器等。
源码注释
name    ETH_Stop.
fun     Turn off ETH in polling mode, including turning off TX and RX DMA engines,
        turning off TX and RX MAC controllers, etc.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none.
void ETH_StopIT(ETH_Module* ETHx, ETH_InfoType* pInfo)通过中断模式关闭 ETH,包括关闭发送与接收 DMA 引擎、关闭发送与接收 MAC 控制器等。
源码注释
name    ETH_StopIT.
fun     Turn off ETH in interrupt mode, including turning off TX and RX DMA engines,
        turning off TX and RX MAC controllers, etc.
param   ETHx (The input parameters must be the following values):
          - ETH
param   pInfo :
          - Pointer to an ETH_InfoType structure parameter containing various
            information about the operation of the ETH module.
return  none.
FlagStatus ETH_GetTxDescFlagStatus(ETH_DMADescType* pTxDescriptor, uint32_t Flag)检查指定的 ETH DMA 发送描述符状态标志位是否置位。
源码注释
name    ETH_GetTxDescFlagStatus.
fun     Checks whether the specified ETH DMA Tx Descriptor status flag
        bits is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMATXDESC_FLAG_IHE
          - ETH_DMATXDESC_FLAG_DB
          - ETH_DMATXDESC_FLAG_UF
          - ETH_DMATXDESC_FLAG_ED
          - ETH_DMATXDESC_FLAG_CC
          - ETH_DMATXDESC_FLAG_EC
          - ETH_DMATXDESC_FLAG_LC
          - ETH_DMATXDESC_FLAG_NC
          - ETH_DMATXDESC_FLAG_LOC
          - ETH_DMATXDESC_FLAG_PCE
          - ETH_DMATXDESC_FLAG_PF
          - ETH_DMATXDESC_FLAG_JT
          - ETH_DMATXDESC_FLAG_ES
          - ETH_DMATXDESC_FLAG_TTSS
          - ETH_DMATXDESC_FLAG_DE
          - ETH_DMATXDESC_FLAG_LD
          - ETH_DMATXDESC_FLAG_FD
return  SET or RESET
FlagStatus ETH_GetRxDescExtendedFlagStatus(ETH_DMADescType* pRxDescriptor, uint32_t Flag)检查指定的 ETH DMA 接收描述符扩展状态标志位是否置位。
源码注释
name    ETH_GetRxDescExtendedFlagStatus.
fun     Checks whether the specified ETH DMA Rx Descriptor Extended status
        flag bits is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMARXDESC_EXTFLAG_PT_NONE
          - ETH_DMARXDESC_EXTFLAG_PT_NUDP
          - ETH_DMARXDESC_EXTFLAG_PT_TCP
          - ETH_DMARXDESC_EXTFLAG_PT_ICNP
          - ETH_DMARXDESC_EXTFLAG_IPHE
          - ETH_DMARXDESC_EXTFLAG_IPV4
          - ETH_DMARXDESC_EXTFLAG_IPV6
          - ETH_DMARXDESC_EXTFLAG_IPCB
          - ETH_DMARXDESC_EXTFLAG_IPCE
          - ETH_DMARXDESC_EXTFLAG_PMT_NONE
          - ETH_DMARXDESC_EXTFLAG_PMT_SYNC
          - ETH_DMARXDESC_EXTFLAG_PMT_FU
          - ETH_DMARXDESC_EXTFLAG_PMT_DREQ
          - ETH_DMARXDESC_EXTFLAG_PMT_DRESP
          - ETH_DMARXDESC_EXTFLAG_PMT_PDREQ
          - ETH_DMARXDESC_EXTFLAG_PMT_PDRESP
          - ETH_DMARXDESC_EXTFLAG_PMT_PRFU
          - ETH_DMARXDESC_EXTFLAG_PMT_ANN
          - ETH_DMARXDESC_EXTFLAG_PMT_MAN
          - ETH_DMARXDESC_EXTFLAG_PMT_SIG
          - ETH_DMARXDESC_EXTFLAG_PMT_PRT
          - ETH_DMARXDESC_EXTFLAG_PFT
          - ETH_DMARXDESC_EXTFLAG_PV
          - ETH_DMARXDESC_EXTFLAG_TSA
          - ETH_DMARXDESC_EXTFLAG_TD
return  SET or RESET
FlagStatus ETH_GetRxDescFilterFlagStatus(ETH_DMADescType* pRxDescriptor, uint32_t Flag)检查指定的 ETH DMA 接收描述符过滤状态标志位是否置位。
源码注释
name    ETH_GetRxDescFilterFlagStatus.
fun     Checks whether the specified ETH DMA Rx Descriptor Filter status flag
        bits is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMARXDESC_FLTFLAG_VFS
          - ETH_DMARXDESC_FLTFLAG_SAF
          - ETH_DMARXDESC_FLTFLAG_DAF
          - ETH_DMARXDESC_FLTFLAG_HF
return  SET or RESET
FlagStatus ETH_GetRxDescFlagStatus(ETH_DMADescType* pRxDescriptor, uint32_t Flag)检查指定的 ETH DMA 接收描述符状态标志位是否置位。
源码注释
name    ETH_GetRxDescFlagStatus.
fun     Checks whether the specified ETH DMA Rx Descriptor status flag bits
        is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMARXDESC_FLAG_ES
          - ETH_DMARXDESC_FLAG_LT_LP
          - ETH_DMARXDESC_FLAG_LT_TP
          - ETH_DMARXDESC_FLAG_LT_ARP
          - ETH_DMARXDESC_FLAG_LT_VLAN
          - ETH_DMARXDESC_FLAG_LT_DVLAN
          - ETH_DMARXDESC_FLAG_LT_MAC
          - ETH_DMARXDESC_FLAG_LT_OAM
          - ETH_DMARXDESC_FLAG_DE
          - ETH_DMARXDESC_FLAG_RE
          - ETH_DMARXDESC_FLAG_OE
          - ETH_DMARXDESC_FLAG_RWT
          - ETH_DMARXDESC_FLAG_GP
          - ETH_DMARXDESC_FLAG_CE
          - ETH_DMARXDESC_FLAG_RS0V
          - ETH_DMARXDESC_FLAG_RS1V
          - ETH_DMARXDESC_FLAG_RS2V
          - ETH_DMARXDESC_FLAG_LD
          - ETH_DMARXDESC_FLAG_FD
return  SET or RESET
FlagStatus ETH_GetFlowControlBusyStatus(ETH_Module* ETHx)检查 ETH 流控忙位是否置位。
源码注释
name    ETH_GetFlowControlBusyStatus.
fun     Checks whether the ETH flow control busy bit is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
return  SET or RESET
note    The Flow Control register should not be written to until this bit
        is cleared.
INTStatus ETH_GetMACInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH MAC 中断标志是否置位。
源码注释
name    ETH_GetMACInterruptStatus.
fun     Checks whether the specified ETH MAC Interrupt flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MAC_INT_FLAG_PHY
          - ETH_MAC_INT_FLAG_PMT
          - ETH_MAC_INT_FLAG_MMCRX
          - ETH_MAC_INT_FLAG_MMCTX
          - ETH_MAC_INT_FLAG_TS
          - ETH_MAC_INT_FLAG_TX
          - ETH_MAC_INT_FLAG_RX
          - ETH_MAC_INT_FLAG_MDIO
return  SET or RESET
FlagStatus ETH_GetMACTxRxFlagStatus(ETH_Module* ETHx, uint32_t Flag)检查指定的 ETH MAC 收发状态标志位是否置位。
源码注释
name    ETH_GetMACTxRxFlagStatus.
fun     Checks whether the specified ETH MAC transmit-receive status
        flag bits is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_MACTX_FLAG_TJT
          - ETH_MACTX_FLAG_NCARR
          - ETH_MACTX_FLAG_LCARR
          - ETH_MACTX_FLAG_EXDEF
          - ETH_MACTX_FLAG_LCOL
          - ETH_MACTX_FLAG_EXCOL
          - ETH_MACRX_FLAG_RWT
return  SET or RESET
FlagStatus ETH_GetPMTFlagStatus(ETH_Module* ETHx, uint32_t Flag)检查指定的 ETH PMT 状态标志位是否置位。
源码注释
name    ETH_GetPMTFlagStatus.
fun     Checks whether the specified ETH PMT status flag bits is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_PMT_FLAG_WUPFRPR
          - ETH_PMT_FLAG_WUPR
          - ETH_PMT_FLAG_MPR
return  SET or RESET
INTStatus ETH_GetMMCRxInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH MMC 接收中断标志是否置位。
源码注释
name    ETH_GetMMCRxInterruptStatus.
fun     Checks whether the specified ETH MMC RX Interrupt flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MMCRX_INT_FLAG_BCGPIS
          - ETH_MMCRX_INT_FLAG_MCGPIS
          - ETH_MMCRX_INT_FLAG_CRCERPIS
          - ETH_MMCRX_INT_FLAG_ALGNERPIS
          - ETH_MMCRX_INT_FLAG_UCGPIS
return  SET or RESET
INTStatus ETH_GetMMCTxInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH MMC 发送中断标志是否置位。
源码注释
name    ETH_GetMMCTxInterruptStatus.
fun     Checks whether the specified ETH MMC TX Interrupt flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MMCTX_INT_FLAG_SCOLGPIS
          - ETH_MMCTX_INT_FLAG_MCOLGPIS
          - ETH_MMCTX_INT_FLAG_GPKTIS
return  SET or RESET
FlagStatus ETH_GetMACTimeStampFlagStatus(ETH_Module* ETHx, uint32_t Flag)检查指定的 ETH MAC 时间戳标志是否置位。
源码注释
name    ETH_GetMACTimeStampFlagStatus.
fun     Checks whether the specified ETH MAC Time Stamp flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_MACTS_FLAG_SOVF
          - ETH_MACTS_FLAG_TARGT0
          - ETH_MACTS_FLAG_TRGTERR0
          - ETH_MACTS_FLAG_TXTSSIS
return  SET or RESET
FlagStatus ETH_GetFlushTransmitQueueStatus(ETH_Module* ETHx)检查 ETH 发送队列刷新位是否已清零。
源码注释
name    ETH_GetFlushTransmitQueueStatus.
fun     Checks whether the ETH Flush Transmit Queue bit is cleared or not.
param   ETHx (The input parameters must be the following values):
          - ETH
return  SET or RESET
note    The MTL Transmit Queue Operation Mode Register (ETH_MTLTXQOPMOD)
        should not be written until the Flush Transmit Queue bit is reset.
INTStatus ETH_GetMTLInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH MTL 中断标志是否置位。
源码注释
name    ETH_GetMTLInterruptStatus.
fun     Checks whether the specified ETH MTL Interrupt flag is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MTL_INT_FLAG_Q0IS
return  SET or RESET
INTStatus ETH_GetMTLQueueInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH MTL 队列中断标志是否置位。
源码注释
name    ETH_GetMTLQueueInterruptStatus.
fun     Checks whether the specified ETH MTL queue Interrupt flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MTLTXQUEUE_INT_FLAG_UNFIS
          - ETH_MTLRXQUEUE_INT_FLAG_OVFIS
return  SET or RESET
FlagStatus ETH_GetMTLCounterFlagStatus(ETH_Module* ETHx, uint32_t Flag)检查指定的 ETH MTL 计数器标志是否置位。
源码注释
name    ETH_GetMTLCounterFlagStatus.
fun     Checks whether the specified ETH MTL Counter flag is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_MTLCOUNTER_FLAG_UFCNTOVF
          - ETH_MTLCOUNTER_FLAG_OVFCNTOVF
          - ETH_MTLCOUNTER_FLAG_MISCNTOVF
return  SET or RESET
FlagStatus ETH_GetSoftwareResetStatus(ETH_Module* ETHx)检查 ETH 软件复位位(SWR)是否置位。
源码注释
name    ETH_GetSoftwareResetStatus.
fun     Checks whether the ETH software reset bit (SWR) is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
return  SET or RESET
note    1. Before reprogramming any ETH register, a value of zero should be
        read in SWR bit.
        2. The SWR bit must be read at least 4 CSR clock cycles after it is
        written to 1.
INTStatus ETH_GetDMAInterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH DMA 中断标志是否置位。
源码注释
name    ETH_GetDMAInterruptStatus.
fun     Checks whether the specified ETH DMA Interrupt flag is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_DMA_INT_FLAG_DMACH0
          - ETH_DMA_INT_FLAG_MTL
          - ETH_DMA_INT_FLAG_MAC
return  SET or RESET
uint32_t ETH_GetDMACh0TxProcessStatus(ETH_Module* ETHx)返回 ETH DMA 发送过程状态。
源码注释
name    ETH_GetDMACh0TxProcessStatus.
fun     Returns the ETH DMA Transmit Process Status.
param   ETHx (The input parameters must be the following values):
          - ETH
return  One of the following values:
          - ETH_DMACH0_TXPRO_FLAG_STOP
          - ETH_DMACH0_TXPRO_FLAG_RUN_FTTD
          - ETH_DMACH0_TXPRO_FLAG_RUN_WS
          - ETH_DMACH0_TXPRO_FLAG_RUN_RDS
          - ETH_DMACH0_TXPRO_FLAG_TSTMP_WS
          - ETH_DMACH0_TXPRO_FLAG_SUSPND
          - ETH_DMACH0_TXPRO_FLAG_RUN_CTD
uint32_t ETH_GetDMACh0RxProcessStatus(ETH_Module* ETHx)返回 ETH DMA 接收过程状态。
源码注释
name    ETH_GetDMACh0RxProcessStatus.
fun     Returns the ETH DMA Receive Process Status.
param   ETHx (The input parameters must be the following values):
          - ETH
return  One of the following values:
          - ETH_DMACH0_RXPRO_FLAG_STOP
          - ETH_DMACH0_RXPRO_FLAG_RUN_FTTD
          - ETH_DMACH0_RXPRO_FLAG_RUN_WRP
          - ETH_DMACH0_RXPRO_FLAG_SUSPND
          - ETH_DMACH0_RXPRO_FLAG_RUN_CRD
          - ETH_DMACH0_RXPRO_FLAG_TSTMP
          - ETH_DMACH0_RXPRO_FLAG_RUN_TRP
FlagStatus ETH_GetDMACh0FlagStatus(ETH_Module* ETHx, uint32_t Flag)检查指定的 ETH DMA 通道 0 标志是否置位。
源码注释
name    ETH_GetDMACh0FlagStatus.
fun     Checks whether the specified ETH DMA-CH0 flag is set or not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMACH0_FLAG_TI
          - ETH_DMACH0_FLAG_TPS
          - ETH_DMACH0_FLAG_TBU
          - ETH_DMACH0_FLAG_RI
          - ETH_DMACH0_FLAG_RBU
          - ETH_DMACH0_FLAG_RPS
          - ETH_DMACH0_FLAG_RWT
          - ETH_DMACH0_FLAG_ETI
          - ETH_DMACH0_FLAG_ERI
          - ETH_DMACH0_FLAG_FBE
          - ETH_DMACH0_FLAG_CDE
          - ETH_DMACH0_FLAG_AIS
          - ETH_DMACH0_FLAG_NIS
return  SET or RESET
INTStatus ETH_GetDMACh0InterruptStatus(ETH_Module* ETHx, uint32_t IntFlag)检查指定的 ETH DMA 通道 0 中断标志是否置位。
源码注释
name    ETH_GetDMACh0InterruptStatus.
fun     Checks whether the specified ETH DMA-CH0 Interrupt flag is set or
        not.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_DMACH0_INT_FLAG_TI
          - ETH_DMACH0_INT_FLAG_TPS
          - ETH_DMACH0_INT_FLAG_TBU
          - ETH_DMACH0_INT_FLAG_RI
          - ETH_DMACH0_INT_FLAG_RBU
          - ETH_DMACH0_INT_FLAG_RPS
          - ETH_DMACH0_INT_FLAG_RWT
          - ETH_DMACH0_INT_FLAG_ETI
          - ETH_DMACH0_INT_FLAG_ERI
          - ETH_DMACH0_INT_FLAG_FBE
          - ETH_DMACH0_INT_FLAG_CDE
          - ETH_DMACH0_INT_FLAG_AIS
          - ETH_DMACH0_INT_FLAG_NIS
return  SET or RESET
void ETH_ClearMACPHYInterruptFlag(ETH_Module* ETHx)清除 ETH 的 MAC PHY 中断挂起标志。
源码注释
name    ETH_ClearMACPHYInterruptFlag.
fun     Clears the MAC PHY Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_ClearPMTFlag(ETH_Module* ETHx, uint32_t Flag)清除 ETH 的 MAC PMT 挂起标志。
源码注释
name    ETH_ClearPMTFlag.
fun     Clears the MAC PMT pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_PMT_FLAG_WUPR
          - ETH_PMT_FLAG_MPR
return  none
void ETH_ClearMMCRxInterruptFlag(ETH_Module* ETHx, uint32_t IntFlag)清除 ETH 的 MMC 接收中断挂起标志。
源码注释
name    ETH_ClearMMCRxInterruptFlag.
fun     Clears the MMC RX Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MMCRX_INT_FLAG_BCGPIS
          - ETH_MMCRX_INT_FLAG_MCGPIS
          - ETH_MMCRX_INT_FLAG_CRCERPIS
          - ETH_MMCRX_INT_FLAG_ALGNERPIS
          - ETH_MMCRX_INT_FLAG_UCGPIS
return  none
void ETH_ClearMMCTxInterruptFlag(ETH_Module* ETHx, uint32_t IntFlag)清除 ETH 的 MMC 发送中断挂起标志。
源码注释
name    ETH_ClearMMCTxInterruptFlag.
fun     Clears the MMC TX Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MMCTX_INT_FLAG_SCOLGPIS
          - ETH_MMCTX_INT_FLAG_MCOLGPIS
          - ETH_MMCTX_INT_FLAG_GPKTIS
return  none
void ETH_ClearMACTimeStampFlag(ETH_Module* ETHx, uint32_t Flag)清除 ETH 的 MAC 时间戳挂起标志。
源码注释
name    ETH_ClearMACTimeStampFlag.
fun     Clears the MAC Time Stamp pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_MACTS_FLAG_SOVF
          - ETH_MACTS_FLAG_TARGT0
          - ETH_MACTS_FLAG_TRGTERR0
          - ETH_MACTS_FLAG_TXTSSIS
return  none
void ETH_ClearMACTxRxFlag(ETH_Module* ETHx, uint32_t Flag)清除 ETH 的 MAC 收发挂起标志。
源码注释
name    ETH_ClearMACTxRxFlag.
fun     Clears the MAC TX RX pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_MACTX_FLAG_TJT
          - ETH_MACTX_FLAG_NCARR
          - ETH_MACTX_FLAG_LCARR
          - ETH_MACTX_FLAG_EXDEF
          - ETH_MACTX_FLAG_LCOL
          - ETH_MACTX_FLAG_EXCOL
          - ETH_MACRX_FLAG_RWT
return  none
void ETH_ClearMACMDIOInterruptFlag(ETH_Module* ETHx)清除 ETH 的 MAC MDIO 中断挂起标志。
源码注释
name    ETH_ClearMACMDIOInterruptFlag.
fun     Clears the MAC MDIO Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
return  none
void ETH_ClearMTLQueueInterruptFlag(ETH_Module* ETHx, uint32_t IntFlag)清除 ETH 的 MTL 队列中断挂起标志。
源码注释
name    ETH_ClearMTLQueueInterruptFlag.
fun     Clears the MTL Queue Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_MTLTXQUEUE_INT_FLAG_UNFIS
          - ETH_MTLRXQUEUE_INT_FLAG_OVFIS
return  none
void ETH_ClearDMACh0Flag(ETH_Module* ETHx, uint32_t Flag)清除 ETH 的 DMA 通道 0 挂起标志。
源码注释
name    ETH_ClearDMACh0Flag.
fun     Clears the DMA Channel 0 pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Flag (The input parameters must be the following values):
          - ETH_DMACH0_FLAG_TI
          - ETH_DMACH0_FLAG_TPS
          - ETH_DMACH0_FLAG_TBU
          - ETH_DMACH0_FLAG_RI
          - ETH_DMACH0_FLAG_RBU
          - ETH_DMACH0_FLAG_RPS
          - ETH_DMACH0_FLAG_RWT
          - ETH_DMACH0_FLAG_ETI
          - ETH_DMACH0_FLAG_ERI
          - ETH_DMACH0_FLAG_FBE
          - ETH_DMACH0_FLAG_CDE
          - ETH_DMACH0_FLAG_AIS
          - ETH_DMACH0_FLAG_NIS
return  none
void ETH_ClearDMACh0InterruptFlag(ETH_Module* ETHx, uint32_t IntFlag)清除 ETH 的 DMA 通道 0 中断挂起标志。
源码注释
name    ETH_ClearDMACh0InterruptFlag.
fun     Clears the DMA Channel 0 Interrupt pending flag for the ETH.
param   ETHx (The input parameters must be the following values):
          - ETH
param   IntFlag (The input parameters must be the following values):
          - ETH_DMACH0_INT_FLAG_TI
          - ETH_DMACH0_INT_FLAG_TPS
          - ETH_DMACH0_INT_FLAG_TBU
          - ETH_DMACH0_INT_FLAG_RI
          - ETH_DMACH0_INT_FLAG_RBU
          - ETH_DMACH0_INT_FLAG_RPS
          - ETH_DMACH0_INT_FLAG_RWT
          - ETH_DMACH0_INT_FLAG_ETI
          - ETH_DMACH0_INT_FLAG_ERI
          - ETH_DMACH0_INT_FLAG_FBE
          - ETH_DMACH0_INT_FLAG_CDE
          - ETH_DMACH0_INT_FLAG_AIS
          - ETH_DMACH0_INT_FLAG_NIS
return  none
void ETH_ConfigMACInterrupt(ETH_Module* ETHx, uint32_t Interrupt, FunctionalStatus Cmd)使能或失能指定的 ETH MAC 中断。
源码注释
name    ETH_ConfigMACInterrupt.
fun     Enables or disables the specified ETH MAC interrupts.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Interrupt (The input parameters must be the following values):
          - ETH_MAC_INT_PHY
          - ETH_MAC_INT_PMT
          - ETH_MAC_INT_TS
          - ETH_MAC_INT_TX
          - ETH_MAC_INT_RX
          - ETH_MAC_INT_MDIO
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ConfigMMCRxInterrupt(ETH_Module* ETHx, uint32_t Interrupt, FunctionalStatus Cmd)使能或失能指定的 ETH MMC 接收中断。
源码注释
name    ETH_ConfigMMCRxInterrupt.
fun     Enables or disables the specified ETH MMC RX interrupts.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Interrupt (The input parameters must be the following values):
          - ETH_MMCRX_INT_BCGPIM
          - ETH_MMCRX_INT_MCGPIM
          - ETH_MMCRX_INT_CRCERPIM
          - ETH_MMCRX_INT_ALGNERPIM
          - ETH_MMCRX_INT_UCGPIM
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ConfigMMCTxInterrupt(ETH_Module* ETHx, uint32_t Interrupt, FunctionalStatus Cmd)使能或失能指定的 ETH MMC 发送中断。
源码注释
name    ETH_ConfigMMCTxInterrupt.
fun     Enables or disables the specified ETH MMC TX interrupts.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Interrupt (The input parameters must be the following values):
          - ETH_MMCTX_INT_SCOLGPIM
          - ETH_MMCTX_INT_MCOLGPIM
          - ETH_MMCTX_INT_GPKTIM
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ConfigMTLQueueInterrupt(ETH_Module* ETHx, uint32_t Interrupt, FunctionalStatus Cmd)使能或失能指定的 ETH MTL 队列中断。
源码注释
name    ETH_ConfigMTLQueueInterrupt.
fun     Enables or disables the specified ETH MTL Queue interrupts.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Interrupt (The input parameters must be the following values):
          - ETH_MTLTXQUEUE_INT_UIE
          - ETH_MTLRXQUEUE_INT_OIE
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none
void ETH_ConfigDMACh0Interrupt(ETH_Module* ETHx, uint32_t Interrupt, FunctionalStatus Cmd)使能或失能指定的 ETH DMA 通道 0 中断。
源码注释
name    ETH_ConfigDMACh0Interrupt.
fun     Enables or disables the specified ETH DMA Channel 0 interrupts.
param   ETHx (The input parameters must be the following values):
          - ETH
param   Interrupt (The input parameters must be the following values):
          - ETH_DMACH0_INT_TI
          - ETH_DMACH0_INT_TPS
          - ETH_DMACH0_INT_TBU
          - ETH_DMACH0_INT_RI
          - ETH_DMACH0_INT_RBU
          - ETH_DMACH0_INT_RPS
          - ETH_DMACH0_INT_RWT
          - ETH_DMACH0_INT_ETI
          - ETH_DMACH0_INT_ERI
          - ETH_DMACH0_INT_FBE
          - ETH_DMACH0_INT_CDE
          - ETH_DMACH0_INT_AIS
          - ETH_DMACH0_INT_NIS
param   Cmd (The input parameters must be the following values):
          - ENABLE
          - DISABLE
return  none

5.10.3 Demo 示例说明

本节逐个说明 ETH 的 12 个示例。

5.10.3.1 HTTP_Client HTTP 客户端获取网页

工程目录: projects/n32h49x_EVAL/examples/ETH/HTTP_Client/

初始化串口与网络接口→解析目标主机域名→建立 TCP 连接→发送 HTTP GET 请求→打印服务器返回内容

项目 源码解读
功能 作为 HTTP 客户端向配置的主机名发起 GET 请求。
接收并打印服务器返回的响应。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/http_client.c:SOCKET 与 NETCONN 两套客户端实现。
inc/http_client.h、inc/main.h:宏定义与任务声明。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
HOST_NAME 为 www.example.com,USE_NETCONN 与 USE_SOCKET 二选一。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
NetworkInitialize 依次调用 tcpip_init、netif_add、netif_set_default 与 netif_set_up。
任务打印本机 IP 后调用 http_client_init 创建客户端线程。
客户端线程用 dns_gethostbyname 解析域名,send 发送 GET 请求,recv 接收响应并打印。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 串口打印本机 IP 与服务器返回的 HTTP 响应头。
不便访问外网时把 USE_PHPSTUDY 置 1 并在本机搭建服务器测试。
使用提示 HOST_NAME 与实际解析出的 IP 要对应,改主机名时须同步改解析地址。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例在 FreeRTOS 下把 lwIP 接入 ETH 驱动,再由应用线程发起一次 HTTP 请求。

数据路径: 域名解析回调 → RemoteAddr → connect → send 发送 SendBuff 中的 GET 请求 → recv 收取响应 → 串口打印

1. 任务先初始化网络再创建客户端线程。 NetworkApplicationTask 调用 NetworkInitialize 添加网络接口,打印地址信息后交给 http_client_init。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
#if LWIP_DHCP
        log_info("The IP address is dynamically assigned by DHCP, if you need to use a static IP address, please redefine LWIP_DHCP in the lwipopts.h file\n");
        /* Enable DHCP and determine whether it is enabled successfully */
        if (dhcp_start(&sNetif) == ERR_OK)
        {
            log_info("DHCP initialization successful, please wait...\n");
            /* Wait for IP address assignment to complete */
            while (ip_addr_cmp(&sNetif.ip_addr, &ipaddr))
            {
                /* System can switch to other tasks */
                vTaskDelay(5);
            }
        }
        else
        {
            log_info("DHCP initialization failure\n");
        }
#endif
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize HTTP client task */
    http_client_init();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 用非阻塞方式解析域名。 SOCKET 线程先调用 dns_gethostbyname,命中本地缓存直接取地址,否则等待回调写入 RemoteAddr。

src/http_client.csrc/http_client.c · http_client_socket_thread
#if LWIP_DNS && (!USE_PHPSTUDY)
    ip4_addr_t dnsAddr;
    /* Non-blocking domain name resolution */
    err_t Err = dns_gethostbyname(HOST_NAME, &dnsAddr, http_dns_found_callback, NULL);
    /* Get IP from local resolver cache */
    if (Err == ERR_OK)
    {
        RemoteAddr.addr = dnsAddr.addr;
    }
    /* Get IP from DNS server */
    else if (Err == ERR_INPROGRESS)
    {
        while (RemoteAddr.addr == 0)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }
    else
    {
        printf("dns_gethostbyname err, err code: %d\n", Err);
    }
#else
    /* Set the server IP address */
    IP4_ADDR(&RemoteAddr, REMOTE_IP_ADDR0, REMOTE_IP_ADDR1, REMOTE_IP_ADDR2, REMOTE_IP_ADDR3);

3. 把线程创建封装成初始化函数。 http_client_init 按 USE_NETCONN、USE_SOCKET 宏选择创建哪一种客户端线程。

src/http_client.csrc/http_client.c · http_client_init
void http_client_init(void)
{
    printf("\r\nCall %s\r\n", __FUNCTION__);
    printf("Set USE_NETCONN to 1 to use the NETCONN API\n");
    printf("Set USE_NETCONN to 0 to use the SOCKET API\n");

    /* Create a http client task thread */
#if USE_NETCONN
    sys_thread_new("http_client_netconn_thread", /* Task name */
                   http_client_netconn_thread,   /* Task entry function */
                   NULL,                         /* Task entry function parameter */
                   HTTP_CLIENT_TASK_STACK_SIZE,  /* Task stack size */
                   HTTP_CLIENT_TASK_PRIORITY);   /* Task priority */
#endif /* USE_NETCONN */

#if USE_SOCKET
    sys_thread_new("http_client_socket_thread",  /* Task name */
                   http_client_socket_thread,    /* Task entry function */
                   NULL,                         /* Task entry function parameter */
                   HTTP_CLIENT_TASK_STACK_SIZE,  /* Task stack size */
                   HTTP_CLIENT_TASK_PRIORITY);   /* Task priority */
#endif /* USE_SOCKET */

对应源码: src/main.c、src/http_client.c

5.10.3.2 HTTP_Server HTTP 服务器返回网页

工程目录: projects/n32h49x_EVAL/examples/ETH/HTTP_Server/

初始化串口与网络接口→监听本地端口→接受浏览器连接→识别 GET 请求→返回内置网页

项目 源码解读
功能 作为 HTTP 服务器监听本地端口。
对浏览器的 GET 请求返回内置 HTML 页面。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/http_server.c:SOCKET 与 NETCONN 两套服务器实现与内置网页数据。
inc/http_server.h、inc/main.h:宏定义与任务声明。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
服务器端口由 LOCAL_PORT 指定,USE_NETCONN 与 USE_SOCKET 二选一。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
NetworkInitialize 依次调用 tcpip_init、netif_add、netif_set_default 与 netif_set_up。
任务打印本机 IP 后调用 http_server_init 创建服务器线程。
服务器线程绑定端口、监听连接,读到以 GET 开头的报文时依次写回响应头与网页数据。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 浏览器访问开发板 IP 192.168.0.32,显示内置测试页面。
串口打印本机 IP 信息。
使用提示 PC 与开发板需处于同一网段。
内置页面为静态内容,修改网页需改 http_server.c 中的数据数组。
核心代码解读与移植

示例把开发板做成一个极简 HTTP 服务器,收到 GET 请求即回送一段固定网页。

数据路径: read 读入请求 → 判断首字节是否为 GET → write 写回 htmlHeader → write 写回 htmlData → 浏览器渲染页面

1. 任务先初始化网络再创建服务器线程。 NetworkApplicationTask 完成网络初始化后调用 http_server_init。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
#if LWIP_DHCP
        log_info("The IP address is dynamically assigned by DHCP, if you need to use a static IP address, please redefine LWIP_DHCP in the lwipopts.h file\n");
        /* Enable DHCP and determine whether it is enabled successfully */
        if (dhcp_start(&sNetif) == ERR_OK)
        {
            log_info("DHCP initialization successful, please wait...\n");
            /* Wait for IP address assignment to complete */
            while (ip_addr_cmp(&sNetif.ip_addr, &ipaddr))
            {
                /* System can switch to other tasks */
                vTaskDelay(5);
            }
        }
        else
        {
            log_info("DHCP initialization failure\n");
        }
#endif
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize HTTP server task */
    http_server_init();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 识别 GET 请求并回送页面。 服务器线程读入报文,判断前五个字节是否为 GET 与斜杠,命中后先写响应头再写网页数据。

src/http_server.csrc/http_server.c · http_server_socket_thread
                /* Check if it is an HTTP GET command packet */
                if ((Len >= 5) && (RecvBuf[0] == 'G') && (RecvBuf[1] == 'E') && (RecvBuf[2] == 'T') && (RecvBuf[3] == ' ') && (RecvBuf[4] == '/'))
                {
                    /* Sends the HTTP HTML response header */
                    write(SockNew, htmlHeader, sizeof(htmlHeader) - 1);

                    /* Sends HTTP HTML data */
                    write(SockNew, htmlData, sizeof(htmlData) - 1);

3. 把线程创建封装成初始化函数。 http_server_init 按宏选择创建 SOCKET 或 NETCONN 服务器线程。

src/http_server.csrc/http_server.c · http_server_init
void http_server_init(void)
{
    printf("\r\nCall %s\r\n", __FUNCTION__);
    printf("Set USE_NETCONN to 1 to use the NETCONN API\n");
    printf("Set USE_NETCONN to 0 to use the SOCKET API\n");
    /* Create a HTTP server task thread */
#if USE_NETCONN
    sys_thread_new("http_server_netconn_thread", /* Task name */
                   http_server_netconn_thread,   /* Task entry function */
                   NULL,                         /* Task entry function parameter */
                   HTTP_SERVER_TASK_STACK_SIZE,  /* Task stack size */
                   HTTP_SERVER_TASK_PRIORITY);   /* Task priority */
#endif /* USE_NETCONN */

#if USE_SOCKET
    sys_thread_new("http_server_socket_thread",  /* Task name */
                   http_server_socket_thread,    /* Task entry function */
                   NULL,                         /* Task entry function parameter */
                   HTTP_SERVER_TASK_STACK_SIZE,  /* Task stack size */
                   HTTP_SERVER_TASK_PRIORITY);   /* Task priority */
#endif /* USE_SOCKET */

对应源码: src/main.c、src/http_server.c

5.10.3.3 iPerf_Netconn iPerf 吞吐测试(NETCONN)

工程目录: projects/n32h49x_EVAL/examples/ETH/iPerf_Netconn/

初始化串口与网络接口→打印菜单并读取串口选择→按选择建立服务端或客户端→持续收发定长数据→用 JPerf 统计吞吐

项目 源码解读
功能 用 NETCONN 接口实现 iPerf 服务器与客户端。
服务端模式测接收吞吐,客户端模式测发送吞吐。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/iperf.c:NETCONN 版本的服务器、客户端线程与模式选择。
inc/iperf.h、inc/main.h:宏定义与任务声明。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
端口由 IPERF_PORT 指定,每次测试数据长度为 IPERF_DATA_LEN。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
任务打印本机 IP 后调用 LwIP_iPerfInit。
LwIP_iPerfModeSelect 从串口读入字符 S 或 C 决定模式。
服务端线程 netconn_listen 后循环接收,客户端线程持续 netconn_write 发送定长缓冲。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 用 JPerf 连接开发板 IP 与 IPERF_PORT 进行吞吐测试。
串口打印模式菜单与连接信息。
使用提示 切换服务端与客户端模式需先复位开发板。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例用 NETCONN 接口实现 iPerf,通过串口选择服务器或客户端模式。

数据路径: 串口读入 S 或 C → LwIP_iPerfInit 创建对应线程 → 服务端 netconn_accept 后收包,客户端 netconn_write 发包 → JPerf 统计速率

1. 任务先初始化网络再启动物理测速。 NetworkApplicationTask 完成网络初始化后调用 LwIP_iPerfInit。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize iperf task */
    LwIP_iPerfInit();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 用串口字符选择工作模式。 LwIP_iPerfModeSelect 打印菜单并读取一个字符,S 置服务端模式,C 置客户端模式。

src/iperf.csrc/iperf.c · LwIP_iPerfModeSelect
static bool LwIP_iPerfModeSelect(bool *serverMode, bool *clientMode)
{
    char code;

    printf("\r\n");
    printf("S: TCP Server Mode\n");
    printf("C: TCP Client Mode\n");
    printf("Please enter 'S' or 'C' to select the mode: ");

    code = getchar();
    printf("%c\n", code);

    switch (code)
    {
        case 'S':
            *serverMode = true;
            *clientMode = false;
            break;

        case 'C':
            *serverMode = false;
            *clientMode = true;
            break;

        default:
            return false;
    }

    return true;

3. 服务端建链并进入监听。 iperf_server_netconn_thread 创建连接标识、绑定 IPERF_PORT 后开始监听。

src/iperf.csrc/iperf.c · iperf_server_netconn_thread
    Conn = netconn_new(NETCONN_TCP);
    LWIP_ERROR("Invalid Conn\n", (Conn != NULL), while (1););
    /* Bind local port and IP address */
    netconn_bind(Conn, IP_ADDR_ANY, IPERF_PORT);
#endif /* LWIP_IPV6 */

    printf("Local port: %d\n", IPERF_PORT);
    printf("Start listening...\n");

    /* Tell connection to go into listening mode */
    netconn_listen(Conn);

对应源码: src/main.c、src/iperf.c

5.10.3.4 iPerf_Socket iPerf 吞吐测试(SOCKET)

工程目录: projects/n32h49x_EVAL/examples/ETH/iPerf_Socket/

初始化串口与网络接口→打印菜单并读取串口选择→按选择建立服务端或客户端→持续收发定长数据→用 JPerf 统计吞吐

项目 源码解读
功能 用 SOCKET 接口实现 iPerf 服务器与客户端。
服务端模式测接收吞吐,客户端模式测发送吞吐。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/iperf.c:SOCKET 版本的服务器、客户端线程与模式选择。
inc/iperf.h、inc/main.h:宏定义与任务声明。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
端口由 IPERF_PORT 指定,发送缓冲用 pvPortMalloc 分配 IPERF_DATA_LEN 字节。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
任务打印本机 IP 后调用 LwIP_iPerfInit。
LwIP_iPerfModeSelect 从串口读入字符决定模式。
服务端线程 socket 与 listen 后循环 read 收包,客户端线程持续 send 定长缓冲。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 用 JPerf 连接开发板 IP 与 IPERF_PORT 进行吞吐测试。
串口打印模式菜单与连接信息。
使用提示 切换服务端与客户端模式需先复位开发板。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例用 SOCKET 接口实现 iPerf,其余结构与 NETCONN 版本一致。

数据路径: 串口读入 S 或 C → LwIP_iPerfInit 创建对应线程 → 服务端 accept 后收包,客户端 send 发包 → JPerf 统计速率

1. 任务先初始化网络再启动物理测速。 NetworkApplicationTask 完成网络初始化后调用 LwIP_iPerfInit。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize iperf task */
    LwIP_iPerfInit();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 用串口字符选择工作模式。 LwIP_iPerfModeSelect 打印菜单并读取一个字符,返回是否选择成功。

src/iperf.csrc/iperf.c · LwIP_iPerfModeSelect
static bool LwIP_iPerfModeSelect(bool *serverMode, bool *clientMode)
{
    char code;

    printf("\r\n");
    printf("S: TCP Server Mode\n");
    printf("C: TCP Client Mode\n");
    printf("Please enter 'S' or 'C' to select the mode: ");

    code = getchar();
    printf("%c\n", code);

    switch (code)
    {
        case 'S':
            *serverMode = true;
            *clientMode = false;
            break;

        case 'C':
            *serverMode = false;
            *clientMode = true;
            break;

        default:
            return false;
    }

    return true;

3. 服务端建链并进入监听。 iperf_server_socket_thread 创建套接字、绑定端口后开始监听。

src/iperf.csrc/iperf.c · iperf_server_socket_thread
    Sock = socket(AF_INET, SOCK_STREAM, 0);
    if (Sock < 0)
    {
        printf("socket error\n");
        while (1)
        {
        }
    }

    /* Set the server address information */
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_addr.s_addr = INADDR_ANY;
    ServerAddr.sin_port = htons(IPERF_PORT);

    /* Bind local port and IP address */
    if (bind(Sock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr)) != 0)
    {
        printf("Unable to bind\n");
        closesocket(Sock);
        free(pData);
        while (1)
        {
        }
    }

    printf("Local port: %d\n", IPERF_PORT);
    printf("Start listening...\n");

    /* Tell connection to go into listening mode */
    if (listen(Sock, SOCKET_LISTEN_BACKLOG) != 0)

对应源码: src/main.c、src/iperf.c

5.10.3.5 NetBIOS NetBIOS 名称服务(NBNS)

工程目录: projects/n32h49x_EVAL/examples/ETH/NetBIOS/

初始化系统时钟与 SysTick→配置网络接口与 lwIP→注册 NetBIOS 名称→应答名称查询广播→用主机名 ping 通设备

项目 源码解读
功能 作为 NetBIOS 名称服务器应答名称查询。
让设备可以用主机名被 ping 通。
源码组成 src/main.c:无操作系统下的网络初始化与主循环。
src/netbiosns.c:NetBIOS 名称服务实现。
inc/main.h:NETBIOS_HOST_NAME 等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
NETBIOS_HOST_NAME 主机名长度须小于 15 个字符。
程序与数据路径 main 配置 SysTick 为 1ms 时基后调用 NetworkInitialize。
NetworkInitialize 调用 lwip_init 与 netif_add 并挂载 ethernet_input。
随后调用 netbiosns_init 与 netbiosns_set_name 注册名称。
主循环轮询 ReceiveDataFlag 调用 ethernetif_input,并周期性调用 sys_check_timeouts。
主要 API ETH_Init、ETH_Start、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用主机名 ping 开发板。
串口打印本机 IP 与 NetBIOS 名称。
使用提示 主机名长度须小于 15 个字符。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例在无操作系统模式下让 lwIP 应答 NetBIOS 名称查询,使设备可按名称访问。

数据路径: main 配置 SysTick → NetworkInitialize → netbiosns_init 绑定 NBNS 端口 → 主循环 ethernetif_input 与 sys_check_timeouts → 收到名称查询即回送应答

1. 启动、时基与网络初始化。 main 打印启动信息后取出系统时钟,把 SysTick 配置为 1ms 时基并设置中断优先级,随后调用 NetworkInitialize 完成 lwIP 与网卡初始化。

src/main.csrc/main.c · main
    log_info("ETH no RTOS NetBIOS Name Service (NBNS) demo go...\r\n");

    /* Get clock frequency value */
    RCC_GetClocksFreqValue(&clk);

    /* Configure SysTick to generate a 1ms time base */
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    SysTick_Config(clk.HclkFreq / 1000);
    NVIC_SetPriority(SysTick_IRQn, 1);

2. lwIP 初始化与网卡注册。 NetworkInitialize 写入 IP、掩码与网关,调用 lwip_init 初始化协议栈,再用 netif_add 注册网卡并挂载 ethernet_input。

src/main.csrc/main.c · NetworkInitialize
    IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
    IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
    IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);

    /* Initilialize the LwIP stack without RTOS */
    lwip_init();

    /* add the network interface (IPv4/IPv6) without RTOS */
    netif_add(&sNetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

    /* Registers the default network interface */
    netif_set_default(&sNetif);

3. 注册 NetBIOS 名称并轮询收包。 main 调用 netbiosns_init 与 netbiosns_set_name 注册名称,再在循环中检测 ReceiveDataFlag 调用 ethernetif_input,并周期性调用 sys_check_timeouts。

src/main.csrc/main.c · main
    netbiosns_init();
    /* Note that the hostname must be less than 15 characters */
    netbiosns_set_name(NETBIOS_HOST_NAME);

    log_info("Local NetBIOS name: %s\n", NETBIOS_HOST_NAME);
    log_info("You can test the ping command with this name\n");

    while (1)
    {
        if (ReceiveDataFlag)
        {
            ReceiveDataFlag = 0;

            /* Receive data and LwIP process */
            ethernetif_input(&sNetif);
        }

        /* Handling LwIP timeout */
        sys_check_timeouts();

对应源码: src/main.c

5.10.3.6 PING 裸机 ping 回复(无 OS)

工程目录: projects/n32h49x_EVAL/examples/ETH/PING/

初始化系统时钟与 SysTick→配置网络接口与 lwIP→主循环轮询接收标志→调用 lwIP 超时处理→回应 ping 请求

项目 源码解读
功能 在无操作系统模式下验证 lwIP 与 ETH 的基本收发。
回应 PC 的 ping 请求。
源码组成 src/main.c:网络初始化、SysTick 时基与主循环。
inc/main.h:IP、网关、端口与 RMII 引脚宏。
bsp/ethernetif.c、bsp/lwipopts.h:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
SysTick 配置为 1ms 时基供 lwIP 使用。
程序与数据路径 main 配置 SysTick 为 1ms 时基并设置优先级。
NetworkInitialize 调用 lwip_init、netif_add、netif_set_default 与 netif_set_up,并挂载 ethernet_input。
主循环检测 ReceiveDataFlag,置位时清零并调用 ethernetif_input。
循环末尾调用 sys_check_timeouts 处理 lwIP 定时事件。
主要 API ETH_Init、ETH_Start、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上执行 ping 192.168.0.32,应能收到回复。
串口打印本机 IP 信息。
使用提示 无操作系统模式必须自行提供 sys_now 时基。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例是最小化的无操作系统 lwIP 移植,用主循环轮询方式收发数据。

数据路径: SysTick 产生 1ms 时基 → sys_now 返回时基 → 主循环 ethernetif_input 收包 → sys_check_timeouts 处理超时 → 回送 ping 应答

1. 建立 1ms 时基。 main 读取时钟频率后用 SysTick 产生 1ms 中断,供 lwIP 的超时机制使用。

src/main.csrc/main.c · main
    RCC_GetClocksFreqValue(&clk);

    /* Configure SysTick to generate a 1ms time base */
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    SysTick_Config(clk.HclkFreq / 1000);
    NVIC_SetPriority(SysTick_IRQn, 1);

2. 主循环轮询接收并处理超时。 收到接收标志时先清零再调用 ethernetif_input,随后调用 sys_check_timeouts。

src/main.csrc/main.c · main
        if (ReceiveDataFlag)
        {
            ReceiveDataFlag = 0;

            /* Receive data and LwIP process */
            ethernetif_input(&sNetif);
        }

        /* Handling LwIP timeout */
        sys_check_timeouts();

3. 无操作系统方式添加网络接口。 NetworkInitialize 用 lwip_init 初始化协议栈,netif_add 的输入函数传入 ethernet_input。

src/main.csrc/main.c · NetworkInitialize
void NetworkInitialize(void)
{
    /* IP addresses initialization */
    IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
    IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
    IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);

    /* Initilialize the LwIP stack without RTOS */
    lwip_init();

    /* add the network interface (IPv4/IPv6) without RTOS */
    netif_add(&sNetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

    /* Registers the default network interface */
    netif_set_default(&sNetif);

    if (netif_is_link_up(&sNetif))
    {
        /* When the netif is fully configured this function must be called */
        netif_set_up(&sNetif);
    }
    else
    {
        /* When the netif link is down this function must be called */
        netif_set_down(&sNetif);

对应源码: src/main.c

5.10.3.7 TCP_Client TCP 回显客户端(带 OS)

工程目录: projects/n32h49x_EVAL/examples/ETH/TCP_Client/

初始化串口与网络接口→连接远端服务器→阻塞接收数据→组装回显字符串→发送回服务器

项目 源码解读
功能 作为 TCP 客户端连接远端服务器。
把收到的数据加前缀后回发给服务器。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/tcp_client.c:SOCKET 与 NETCONN 两套客户端实现。
inc/tcp_client.h、inc/main.h:远端 IP、端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
服务器地址由 REMOTE_IP_ADDR0~3 与 REMOTE_PORT 指定。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
任务打印本机 IP 后调用 tcp_client_init 创建客户端线程。
客户端线程用 connect 连接服务器,成功后用 recv 阻塞接收。
收到数据后用 snprintf 组装回显字符串并用 send 发出。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手建立 TCP 服务器,端口与 REMOTE_PORT 一致。
服务器发送数据后应收到带前缀的回显。
使用提示 REMOTE_IP_ADDR0~3 要指向实际服务器地址。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例在 FreeRTOS 下实现 TCP 回显客户端,收到数据后加前缀再回发。

数据路径: connect 连接服务器 → recv 阻塞接收 → snprintf 加前缀 → send 回发 → 断线后延时重连

1. 任务先初始化网络再创建客户端线程。 NetworkApplicationTask 完成网络初始化后调用 tcp_client_init。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize TCP client task */
    tcp_client_init();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 用宏组装服务器地址。 SOCKET 线程把远端 IP 与端口写入 sockaddr_in 结构,供后续 connect 使用。

src/tcp_client.csrc/tcp_client.c · tcp_client_socket_thread
    /* Set the server IP address */
    IP4_ADDR(&IpAddr, REMOTE_IP_ADDR0, REMOTE_IP_ADDR1, REMOTE_IP_ADDR2, REMOTE_IP_ADDR3);
    printf("Server IP: %d.%d.%d.%d\n", REMOTE_IP_ADDR0, REMOTE_IP_ADDR1, REMOTE_IP_ADDR2, REMOTE_IP_ADDR3);
    printf("Server Port: %d\n", REMOTE_PORT);

    /* Set the server address information */
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_addr.s_addr = IpAddr.addr;
    ServerAddr.sin_port = htons(REMOTE_PORT);

3. 把线程创建封装成初始化函数。 tcp_client_init 按宏选择创建 SOCKET 或 NETCONN 客户端线程。

src/tcp_client.csrc/tcp_client.c · tcp_client_init
void tcp_client_init(void)
{
    printf("\r\nCall %s\r\n", __FUNCTION__);
    printf("Set USE_NETCONN to 1 to use the NETCONN API\n");
    printf("Set USE_NETCONN to 0 to use the SOCKET API\n");

    /* Create a TCP client task thread */
#if USE_NETCONN
    sys_thread_new("tcp_client_netconn_thread", /* Task name */
                   tcp_client_netconn_thread,   /* Task entry function */
                   NULL,                        /* Task entry function parameter */
                   TCP_CLIENT_TASK_STACK_SIZE,  /* Task stack size */
                   TCP_CLIENT_TASK_PRIORITY);   /* Task priority */
#endif /* USE_NETCONN */

#if USE_SOCKET
    sys_thread_new("tcp_client_socket_thread",  /* Task name */
                   tcp_client_socket_thread,    /* Task entry function */
                   NULL,                        /* Task entry function parameter */
                   TCP_CLIENT_TASK_STACK_SIZE,  /* Task stack size */
                   TCP_CLIENT_TASK_PRIORITY);   /* Task priority */
#endif /* USE_SOCKET */

对应源码: src/main.c、src/tcp_client.c

5.10.3.8 TCP_Client_RAW TCP 回显客户端(RAW)

工程目录: projects/n32h49x_EVAL/examples/ETH/TCP_Client_RAW/

初始化系统时钟与 SysTick→配置网络接口与 lwIP→连接远端服务器→注册连接与收发回调→回显并在断线后重连

项目 源码解读
功能 用 RAW API 实现 TCP 回显客户端。
连接成功后周期发送消息,收到数据即回显。
源码组成 src/main.c:无操作系统下的网络初始化与主循环。
src/tcp_client.c:RAW 回调与连接管理。
inc/tcp_client.h、inc/main.h:远端 IP、端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
服务器地址由 REMOTE_IP_ADDR0~3 与 REMOTE_PORT 指定。
程序与数据路径 main 配置 SysTick 后调用 NetworkInitialize,再调用 tcp_client_init。
tcp_client_init 新建控制块并调用 tcp_connect 发起连接。
连接回调注册定时发送与接收回调。
主循环调用 ethernetif_input 与 sys_check_timeouts 驱动协议栈。
主要 API ETH_Init、ETH_Start、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手建立 TCP 服务器。
连接成功后客户端周期发送消息,服务器发送的数据被回显。
使用提示 断开连接后回调会重新调用初始化函数发起重连。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例用 lwIP RAW API 实现无操作系统的回显客户端,通过回调驱动收发。

数据路径: tcp_connect 发起连接 → 连接回调注册定时与接收回调 → 定时回调 tcp_write 发送、接收回调回显 → 断线回调重新连接

1. 主循环驱动协议栈。 收到接收标志时调用 ethernetif_input,随后调用 sys_check_timeouts。

src/main.csrc/main.c · main
        if (ReceiveDataFlag)
        {
            ReceiveDataFlag = 0;

            /* Receive data and LwIP process */
            ethernetif_input(&sNetif);
        }

        /* Handling LwIP timeout */
        sys_check_timeouts();

2. 连接成功后注册回调。 tcp_connect_callback 在连接建立后注册定时发送回调与接收回调。

src/tcp_client.csrc/tcp_client.c · tcp_connect_callback
static err_t tcp_connect_callback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
    LWIP_UNUSED_ARG(arg);

    if ((err != ERR_OK) || (tpcb == NULL))
    {
        return ERR_VAL;
    }

    printf("Connect successful!!\n");

    /* Registers a callback function for periodic processing */
    tcp_poll(tpcb, tcp_poll_callback, 10);

    /* Registers a callback function for receiving */
    tcp_recv(tpcb, tcp_recv_callback);

3. 初始化时发起连接。 tcp_client_init 新建控制块、调用 tcp_connect 并发起连接,再注册错误回调。

src/tcp_client.csrc/tcp_client.c · tcp_client_init
void tcp_client_init(void)
{
    ip4_addr_t RemoteIpAddr;

    /* Set the server IP address */
    IP4_ADDR(&RemoteIpAddr, REMOTE_IP_ADDR0, REMOTE_IP_ADDR1, REMOTE_IP_ADDR2, REMOTE_IP_ADDR3);

    /* Creates a new TCP protocol control block */
    tcp_raw_pcb = tcp_new();

    if (FirstInitFlag)
    {
        FirstInitFlag = 0U;
        printf("Server IP: %d.%d.%d.%d\n", REMOTE_IP_ADDR0, REMOTE_IP_ADDR1, REMOTE_IP_ADDR2, REMOTE_IP_ADDR3);
        printf("Try to connect to the server...\n");
    }

    /* Try to connect to the server */
    tcp_connect(tcp_raw_pcb, &RemoteIpAddr, REMOTE_PORT, tcp_connect_callback);

    /* Registers a callback function for error handling */
    tcp_err(tcp_raw_pcb, tcp_err_callback);

对应源码: src/main.c、src/tcp_client.c

5.10.3.9 TCP_Server TCP 回显服务器(带 OS)

工程目录: projects/n32h49x_EVAL/examples/ETH/TCP_Server/

初始化串口与网络接口→绑定本地端口并监听→接受客户端连接→阻塞接收数据→把数据原样回发

项目 源码解读
功能 作为 TCP 服务器监听本地端口。
把收到的数据原样回发给客户端。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/tcp_server.c:SOCKET 与 NETCONN 两套服务器实现。
inc/tcp_server.h、inc/main.h:本地端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
监听端口由 LOCAL_PORT 指定。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
任务打印本机 IP 后调用 tcp_server_init 创建服务器线程。
服务器线程创建套接字、绑定端口并进入监听。
接受连接后用 read 阻塞接收,收到数据即 write 回发。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手连接 192.168.0.32 的 LOCAL_PORT。
发送的数据被原样回显。
使用提示 LOCAL_PORT 不能与已占用端口冲突。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例在 FreeRTOS 下实现 TCP 回显服务器,接受连接后把数据原样回发。

数据路径: socket 与 bind 绑定端口 → listen 监听 → accept 接受连接 → read 收数据 → write 原样回发

1. 任务先初始化网络再创建服务器线程。 NetworkApplicationTask 完成网络初始化后调用 tcp_server_init。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize TCP server task */
    tcp_server_init();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 建链并进入监听。 SOCKET 线程创建套接字、绑定端口后调用 listen 进入监听。

src/tcp_server.csrc/tcp_server.c · tcp_server_socket_thread
    /* Creates a socket */
    Sock = socket(AF_INET, SOCK_STREAM, 0);
    if (Sock < 0)
    {
        printf("socket error\n");
        while (1);
    }

    /* Set the server address information */
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_addr.s_addr = INADDR_ANY;
    ServerAddr.sin_port = htons(LOCAL_PORT);

    /* Bind local port and IP address */
    if (bind(Sock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr)) != 0)
    {
        printf("Unable to bind\n");
        closesocket(Sock);
        free(pData);
        while (1);
    }

    printf("Local port: %d\n", LOCAL_PORT);
    printf("Start listening...\n");

    /* Tell connection to go into listening mode */
    if (listen(Sock, SOCKET_LISTEN_BACKLOG) != 0)

3. 把线程创建封装成初始化函数。 tcp_server_init 按宏选择创建 SOCKET 或 NETCONN 服务器线程。

src/tcp_server.csrc/tcp_server.c · tcp_server_init
void tcp_server_init(void)
{
    printf("\r\nCall %s\r\n", __FUNCTION__);
    printf("Set USE_NETCONN to 1 to use the NETCONN API\n");
    printf("Set USE_NETCONN to 0 to use the SOCKET API\n");
    /* Create a TCP server task thread */
#if USE_NETCONN
    sys_thread_new("tcp_server_netconn_thread", /* Task name */
                   tcp_server_netconn_thread,   /* Task entry function */
                   NULL,                        /* Task entry function parameter */
                   TCP_SERVER_TASK_STACK_SIZE,  /* Task stack size */
                   TCP_SERVER_TASK_PRIORITY);   /* Task priority */
#endif /* USE_NETCONN */

#if USE_SOCKET
    sys_thread_new("tcp_server_socket_thread",  /* Task name */
                   tcp_server_socket_thread,    /* Task entry function */
                   NULL,                        /* Task entry function parameter */
                   TCP_SERVER_TASK_STACK_SIZE,  /* Task stack size */
                   TCP_SERVER_TASK_PRIORITY);   /* Task priority */
#endif /* USE_SOCKET */

对应源码: src/main.c、src/tcp_server.c

5.10.3.10 TCP_Server_RAW TCP 回显服务器(RAW)

工程目录: projects/n32h49x_EVAL/examples/ETH/TCP_Server_RAW/

初始化系统时钟与 SysTick→配置网络接口与 lwIP→新建控制块并监听→接受连接并注册回调→在接收回调中回显

项目 源码解读
功能 用 RAW API 实现 TCP 回显服务器。
接受连接后在接收回调中把数据回发。
源码组成 src/main.c:无操作系统下的网络初始化与主循环。
src/tcp_server.c:RAW 回调与服务器初始化。
inc/tcp_server.h、inc/main.h:本地端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
监听端口由 LOCAL_PORT 指定。
程序与数据路径 main 配置 SysTick 后调用 NetworkInitialize,再调用 tcp_server_init。
tcp_server_init 新建控制块、绑定端口并调用 tcp_listen。
随后用 tcp_accept 注册接受回调。
主循环调用 ethernetif_input 与 sys_check_timeouts 驱动协议栈。
主要 API ETH_Init、ETH_Start、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手连接 192.168.0.32 的 LOCAL_PORT。
发送的数据被原样回显。
使用提示 接收回调中需要及时 pbuf_free 释放缓冲区。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例用 lwIP RAW API 实现无操作系统的回显服务器,在回调中完成收发。

数据路径: tcp_new 与 tcp_bind 绑定端口 → tcp_listen 监听 → tcp_accept 注册接受回调 → 接收回调 tcp_write 回显 → pbuf_free 释放

1. 主循环驱动协议栈。 收到接收标志时调用 ethernetif_input,随后调用 sys_check_timeouts。

src/main.csrc/main.c · main
        if (ReceiveDataFlag)
        {
            ReceiveDataFlag = 0;

            /* Receive data and LwIP process */
            ethernetif_input(&sNetif);
        }

        /* Handling LwIP timeout */
        sys_check_timeouts();

2. 接收回调中回显并释放缓冲区。 tcp_recv_callback 更新接收窗口后用 tcp_write 回发,再释放 pbuf。

src/tcp_server.csrc/tcp_server.c · tcp_recv_callback
static err_t tcp_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
    err_t retErr = ERR_OK;

    LWIP_UNUSED_ARG(arg);

    if (tpcb == NULL)
    {
        return ERR_VAL;
    }

    /* Valid data received, sent back to remote */
    if (p != NULL)
    {
        /*Update the receive data window*/
        tcp_recved(tpcb, p->tot_len);

        /*Echo the received data*/
        tcp_write(tpcb, p->payload, p->tot_len, 1);

        memset(p->payload, 0, p->tot_len);
        pbuf_free(p);

3. 初始化时建链并注册接受回调。 tcp_server_init 新建控制块、绑定并监听,再注册接受回调。

src/tcp_server.csrc/tcp_server.c · tcp_server_init
void tcp_server_init(void)
{
    err_t err;

    /* Creates a new TCP protocol control block */
    tcp_raw_pcb = tcp_new();

    /* Bind local port and IP address */
    err = tcp_bind(tcp_raw_pcb, IP_ADDR_ANY, LOCAL_PORT);
    if (err == ERR_OK)
    {
        printf("Bind local IP address and port successful!!\n");
        printf("Local port: %d\n", LOCAL_PORT);
        /* Listen for client connections */
        tcp_raw_pcb = tcp_listen(tcp_raw_pcb);
        /* Accept the connection from the client and register the callback function */
        tcp_accept(tcp_raw_pcb, tcp_accept_callback);

对应源码: src/main.c、src/tcp_server.c

5.10.3.11 UDP UDP 回显(带 OS)

工程目录: projects/n32h49x_EVAL/examples/ETH/UDP/

初始化串口与网络接口→绑定本地端口→阻塞接收数据报→组装回显字符串→回发到来源地址

项目 源码解读
功能 作为 UDP 服务器绑定本地端口。
把收到的数据报加前缀后回发到来源地址。
源码组成 src/main.c:FreeRTOS 下的网络初始化与 NetworkApplicationTask。
src/udp_echo.c:SOCKET 与 NETCONN 两套回显实现。
inc/udp_echo.h、inc/main.h:本地端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h、bsp/sys_arch.c:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
本地端口由 LOCAL_PORT 指定。
程序与数据路径 main 创建 NetworkApplicationTask 并启动调度器。
任务打印本机 IP 后调用 udp_echo_init 创建回显线程。
回显线程创建套接字并绑定本地端口。
用 recvfrom 阻塞接收,收到后加前缀再用 sendto 回发。
主要 API ETH_Init、ETH_StartIT、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手以 UDP 方式向 LOCAL_PORT 发送数据。
收到带前缀的回显。
使用提示 LOCAL_PORT 不能与已占用端口冲突。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例在 FreeRTOS 下实现 UDP 回显,收到数据报后加前缀回发到来源地址。

数据路径: socket 与 bind 绑定端口 → recvfrom 收数据报并得到来源地址 → snprintf 加前缀 → sendto 回发

1. 任务先初始化网络再创建回显线程。 NetworkApplicationTask 完成网络初始化后调用 udp_echo_init。

src/main.csrc/main.c · NetworkApplicationTask
static void NetworkApplicationTask(void)
{
    /* Network initialization, adding network interfaces */
    NetworkInitialize();

    /* Link successful, print local IP information */
    if (netif_is_up(&sNetif))
    {
        log_info("Network connection successful\n");
        log_info("Local IP: %s\n", ipaddr_ntoa(&sNetif.ip_addr));
        log_info("Netmask: %s\n", ipaddr_ntoa(&sNetif.netmask));
        log_info("Gateway: %s\n", ipaddr_ntoa(&sNetif.gw));
    }
    else
    {
        log_info("Network not connected\n");
        while (1)
        {
            /* System can switch to other tasks */
            vTaskDelay(5);
        }
    }

    /* Initialize UDP echo task */
    udp_echo_init();

    /* Close the task */
    vTaskDelete(NetworkApplicationTaskHandle);

2. 组装本地地址并绑定端口。 SOCKET 线程把端口写入 sockaddr_in 结构后用 bind 绑定。

src/udp_echo.csrc/udp_echo.c · udp_echo_socket_thread
    LocalAddr.sin_port = htons(LOCAL_PORT);

    printf("Local port: %d\n", LOCAL_PORT);

3. 把线程创建封装成初始化函数。 udp_echo_init 按宏选择创建 SOCKET 或 NETCONN 回显线程。

src/udp_echo.csrc/udp_echo.c · udp_echo_init
void udp_echo_init(void)
{
    printf("\r\nCall %s\r\n", __FUNCTION__);
    printf("Set USE_NETCONN to 1 to use the NETCONN API\n");
    printf("Set USE_NETCONN to 0 to use the SOCKET API\n");

    /* Create a UDP echo task thread */
#if USE_NETCONN
    sys_thread_new("udp_echo_netconn_thread", /* Task name */
                   udp_echo_netconn_thread,   /* Task entry function */
                   NULL,                      /* Task entry function parameter */
                   UDP_ECHO_TASK_STACK_SIZE,  /* Task stack size */
                   UDP_ECHO_TASK_PRIORITY);   /* Task priority */
#endif /* USE_NETCONN */

#if USE_SOCKET
    sys_thread_new("udp_echo_socket_thread",  /* Task name */
                   udp_echo_socket_thread,    /* Task entry function */
                   NULL,                      /* Task entry function parameter */
                   UDP_ECHO_TASK_STACK_SIZE,  /* Task stack size */
                   UDP_ECHO_TASK_PRIORITY);   /* Task priority */
#endif /* USE_SOCKET */

对应源码: src/main.c、src/udp_echo.c

5.10.3.12 UDP_RAW UDP 回显(RAW)

工程目录: projects/n32h49x_EVAL/examples/ETH/UDP_RAW/

初始化系统时钟与 SysTick→配置网络接口与 lwIP→新建控制块并绑定端口→注册接收回调→在回调中原样回发

项目 源码解读
功能 用 RAW API 实现 UDP 回显。
在接收回调中把数据报原样回发给来源地址。
源码组成 src/main.c:无操作系统下的网络初始化与主循环。
src/udp_echo.c:RAW 接收回调与初始化。
inc/udp_echo.h、inc/main.h:本地端口等宏定义。
bsp/ethernetif.c、bsp/lwipopts.h:lwIP 与 ETH 的适配层。
readme.txt:功能、环境与使用说明。
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
关键配置 时钟源 HSI+PLL,系统频率 240MHz,串口 PA9 波特率 115200。
RMII 接口使用 MDC-PC1、MDIO-PA2、REF_CLK-PA1、CRS_DV-PA7。
默认静态 IP 192.168.0.32、掩码 255.255.255.0、网关 192.168.0.1。
本地端口由 LOCAL_PORT 指定。
程序与数据路径 main 配置 SysTick 后调用 NetworkInitialize,再调用 udp_echo_init。
udp_echo_init 新建 UDP 控制块并用 udp_bind 绑定端口。
随后用 udp_recv 注册接收回调。
主循环调用 ethernetif_input 与 sys_check_timeouts 驱动协议栈。
主要 API ETH_Init、ETH_Start、ETH_Transmit、ETH_GetRxDataBuffer、ETH_GetRxDataLength、ETH_BuildRxDescriptors
验证方法 在 PC 上用网络调试助手以 UDP 方式向 LOCAL_PORT 发送数据。
收到原样的回显。
使用提示 接收回调中需要及时 pbuf_free 释放缓冲区。
PC 与开发板需处于同一网段。
核心代码解读与移植

示例用 lwIP RAW API 实现无操作系统的 UDP 回显,在回调中完成收发。

数据路径: udp_new_ip_type 新建控制块 → udp_bind 绑定端口 → udp_recv 注册回调 → 回调中 udp_sendto 回发 → pbuf_free 释放

1. 主循环驱动协议栈。 收到接收标志时调用 ethernetif_input,随后调用 sys_check_timeouts。

src/main.csrc/main.c · main
        if (ReceiveDataFlag)
        {
            ReceiveDataFlag = 0;

            /* Receive data and LwIP process */
            ethernetif_input(&sNetif);
        }

        /* Handling LwIP timeout */
        sys_check_timeouts();

2. 接收回调中原样回发。 udp_recv_callback 用 udp_sendto 把整包回发到来源地址,再释放 pbuf。

src/udp_echo.csrc/udp_echo.c · udp_recv_callback
static void udp_recv_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
    LWIP_UNUSED_ARG(arg);

    if (p != NULL)
    {
        /* Send received packet back to sender */
        udp_sendto(upcb, p, addr, port);
        /* Free the pbuf */
        pbuf_free(p);

3. 初始化时绑定端口并注册回调。 udp_echo_init 新建控制块、绑定端口后注册接收回调。

src/udp_echo.csrc/udp_echo.c · udp_echo_init
void udp_echo_init(void)
{
    err_t err;

    /* Creates a new UDP protocol control block */
    udp_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);

    /* Bind local IP address and port */
    err = udp_bind(udp_raw_pcb, IP_ANY_TYPE, LOCAL_PORT);
    if (err == ERR_OK)
    {
        printf("Bind local IP address and port successful!!\n");
        printf("Local port: %d\n", LOCAL_PORT);
        /* Registers a callback function to receive data */
        udp_recv(udp_raw_pcb, udp_recv_callback, NULL);

对应源码: src/main.c、src/udp_echo.c

5.10.4 注意与排障

下表列出用户手册中与使用该外设直接相关的操作约束。

场景 检查与建议 来源
软件复位后立即访问寄存器 软件复位后需轮询 DMA 模式寄存器第 0 位直到清零,确认复位完成后再配置寄存器。 用户手册
描述符环长度不足 发送与接收描述符环长度必须至少为 4;
从环起点到终点的描述符地址不得跨越 4GB 边界。
用户手册
MAC 接收器使能过早 若 MAC 初始化先于 DMA 配置完成,须等 DMA 激活后才能使能 MAC 接收器,否则接收帧会填满 Rx FIFO 并上溢。 用户手册
同一寄存器连续写入 对同一寄存器位置的两次写操作之间至少间隔四个目标时钟周期(PHY 接收时钟、PHY 发送时钟或 PTP 时钟)。 用户手册
运行中修改配置 MAC 发送或接收数据时不得更改双工模式、速率、端口或 Loopback;
DMA 处于活动状态时不得更改 DMA 相关配置。
用户手册
RMII 速率与参考时钟 RMII 仅支持 10Mbps 与 100Mbps,不支持 1000Mbps;
RMII 参考时钟需提供连续的 50MHz 输入。
用户手册
重新启动时间戳 若通过清除 MAC 时间戳控制寄存器第 0 位关闭时间戳,需重复全部初始化步骤才能重新启动时间戳。 用户手册
国民技术开发套件文档中心

按芯片系列在线查阅国民技术 MCU 开发套件的芯片资料、SDK、驱动 API 与 Demo 示例说明。

© 2026 国民技术股份有限公司 版权所有粤ICP备11069485号本站使用不含 Cookie 的匿名访问统计