feat:新建项目文件
BLV主机C1P模块
This commit is contained in:
698
NetLib/eth_driver.c
Normal file
698
NetLib/eth_driver.c
Normal file
@@ -0,0 +1,698 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : eth_driver.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2024/05/05
|
||||
* Description : eth program body.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "eth_driver.h"
|
||||
#include "net_config.h"
|
||||
|
||||
__attribute__((__aligned__(4))) ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB]; /* MAC receive descriptor, 4-byte aligned*/
|
||||
__attribute__((__aligned__(4))) ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB]; /* MAC send descriptor, 4-byte aligned */
|
||||
|
||||
__attribute__((__aligned__(4))) uint8_t MACRxBuf[ETH_RXBUFNB*ETH_RX_BUF_SZE]; /* MAC receive buffer, 4-byte aligned */
|
||||
__attribute__((__aligned__(4))) uint8_t MACTxBuf[ETH_TXBUFNB*ETH_TX_BUF_SZE]; /* MAC send buffer, 4-byte aligned */
|
||||
|
||||
__attribute__((__aligned__(4))) SOCK_INF SocketInf[WCHNET_MAX_SOCKET_NUM]; /* Socket information table, 4-byte alignment */
|
||||
__attribute__((__aligned__(4))) uint8_t RemoteIp[4]; /* DNS information table, 4-byte alignment */
|
||||
const uint16_t MemNum[8] = {WCHNET_NUM_IPRAW,
|
||||
WCHNET_NUM_UDP,
|
||||
WCHNET_NUM_TCP,
|
||||
WCHNET_NUM_TCP_LISTEN,
|
||||
WCHNET_NUM_TCP_SEG,
|
||||
WCHNET_NUM_IP_REASSDATA,
|
||||
WCHNET_NUM_PBUF,
|
||||
WCHNET_NUM_POOL_BUF
|
||||
};
|
||||
const uint16_t MemSize[8] = {WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IPRAW_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_UDP_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB_LISTEN),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_SEG),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IP_REASSDATA),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF) + WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_POOL_BUF)
|
||||
};
|
||||
__attribute__((__aligned__(4)))uint8_t Memp_Memory[WCHNET_MEMP_SIZE];
|
||||
__attribute__((__aligned__(4)))uint8_t Mem_Heap_Memory[WCHNET_RAM_HEAP_SIZE];
|
||||
__attribute__((__aligned__(4)))uint8_t Mem_ArpTable[WCHNET_RAM_ARP_TABLE_SIZE];
|
||||
|
||||
uint16_t gPHYAddress;
|
||||
volatile uint32_t LocalTime;
|
||||
volatile uint8_t PhyWaitNegotiationSuc = 0;
|
||||
ETH_DMADESCTypeDef *pDMARxSet;
|
||||
ETH_DMADESCTypeDef *pDMATxSet;
|
||||
|
||||
volatile uint8_t LinkSta = 0; //0:Link down 1:Link up
|
||||
uint8_t LinkVaildFlag = 0; //0:invalid 1:valid
|
||||
uint8_t AccelerateLinkFlag = 0; //0:invalid 1:valid
|
||||
uint8_t LinkProcessingStep = 0;
|
||||
uint32_t LinkProcessingTime = 0;
|
||||
uint32_t TaskExecutionTime = 0;
|
||||
void ETH_LinkDownCfg(void);
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_TimeIsr
|
||||
*
|
||||
* @brief
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_TimeIsr( uint16_t timperiod )
|
||||
{
|
||||
LocalTime += timperiod;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_QueryPhySta
|
||||
*
|
||||
* @brief Query external PHY status
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
|
||||
void WCHNET_QueryPhySta(void)
|
||||
{
|
||||
if(PhyWaitNegotiationSuc)
|
||||
{
|
||||
ETH_PHYLink();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_CheckPHYPN
|
||||
*
|
||||
* @brief check PHY PN polarity
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_CheckPHYPN(uint16_t time)
|
||||
{
|
||||
uint16_t phy_stat;
|
||||
//check PHY PN
|
||||
if((LinkProcessingStep == 0)||(LocalTime >= LinkProcessingTime))
|
||||
{
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE0 );
|
||||
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_STATUS1);
|
||||
if(phy_stat & (1<<4))
|
||||
{
|
||||
if(LinkProcessingStep == 0)
|
||||
{
|
||||
LinkProcessingStep = 1;
|
||||
LinkProcessingTime = LocalTime + time;
|
||||
}
|
||||
else {
|
||||
LinkProcessingStep = 0;
|
||||
LinkProcessingTime = 0;
|
||||
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_ANER);
|
||||
if((time == 200) || ((phy_stat & 1) == 0))
|
||||
{
|
||||
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_CONTROL1);
|
||||
phy_stat |= 1;
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_CONTROL1, phy_stat );
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
LinkProcessingStep = 0;
|
||||
LinkProcessingTime = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_AccelerateLink
|
||||
*
|
||||
* @brief accelerate Link processing
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_AccelerateLink(void)
|
||||
{
|
||||
uint16_t phy_stat;
|
||||
if(AccelerateLinkFlag == 0)
|
||||
{
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, 99 );
|
||||
phy_stat = ETH_ReadPHYRegister( gPHYAddress, 0x19);
|
||||
if((phy_stat & 0xf) == 3)
|
||||
{
|
||||
AccelerateLinkFlag = 1;
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE0 );
|
||||
phy_stat = 0x4;
|
||||
ETH_WritePHYRegister(gPHYAddress, 0x16, phy_stat );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_CheckLinkVaild
|
||||
*
|
||||
* @brief check whether Link is valid
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_CheckLinkVaild(void)
|
||||
{
|
||||
uint16_t phy_stat, phy_bcr;
|
||||
|
||||
if(LinkVaildFlag == 0)
|
||||
{
|
||||
phy_bcr = ETH_ReadPHYRegister( PHY_ADDRESS, PHY_BCR);
|
||||
if((phy_bcr & (1<<13)) == 0) //Do nothing if Link mode is 10M.
|
||||
{
|
||||
LinkVaildFlag = 1;
|
||||
LinkProcessingTime = 0;
|
||||
return;
|
||||
}
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, 99 );
|
||||
phy_stat = ETH_ReadPHYRegister( gPHYAddress, 0x1b);
|
||||
if((phy_stat & (1<<2)) == 0)
|
||||
{
|
||||
LinkProcessingTime++;
|
||||
if(LinkProcessingTime == 5)
|
||||
{
|
||||
LinkProcessingTime = 0;
|
||||
phy_stat = ETH_ReadPHYRegister(gPHYAddress, PHY_BCR);
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_BCR, PHY_Reset );
|
||||
Delay_Us(100);
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_BCR, phy_stat );
|
||||
ETH_LinkDownCfg();
|
||||
}
|
||||
}
|
||||
else {
|
||||
LinkVaildFlag = 1;
|
||||
LinkProcessingTime = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_LinkProcessing
|
||||
*
|
||||
* @brief process Link stage task
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_LinkProcessing(void)
|
||||
{
|
||||
u16 phy_bcr;
|
||||
|
||||
if(LocalTime >= TaskExecutionTime)
|
||||
{
|
||||
TaskExecutionTime = LocalTime + 10; //execution cycle:10ms
|
||||
if(LinkSta == 0) //Link down
|
||||
{
|
||||
phy_bcr = ETH_ReadPHYRegister( PHY_ADDRESS, PHY_BCR);
|
||||
if(phy_bcr & PHY_AutoNegotiation) //auto-negotiation is enabled
|
||||
{
|
||||
WCHNET_CheckPHYPN(300); //check PHY PN
|
||||
WCHNET_AccelerateLink(); //accelerate Link processing
|
||||
}
|
||||
else { //auto-negotiation is disabled
|
||||
if((phy_bcr & (1<<13)) == 0) // 10M
|
||||
{
|
||||
WCHNET_CheckPHYPN(200); //check PHY PN
|
||||
}
|
||||
}
|
||||
}
|
||||
else { //Link up
|
||||
WCHNET_CheckLinkVaild(); //check whether Link is valid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_MainTask
|
||||
*
|
||||
* @brief library main task function
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_MainTask(void)
|
||||
{
|
||||
WCHNET_NetInput( ); /* Ethernet data input */
|
||||
WCHNET_PeriodicHandle( ); /* Protocol stack time-related task processing */
|
||||
WCHNET_QueryPhySta();
|
||||
WCHNET_LinkProcessing();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LinkUpCfg
|
||||
*
|
||||
* @brief When the PHY is connected, configure the relevant functions.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_LinkUpCfg(void)
|
||||
{
|
||||
uint16_t phy_stat;
|
||||
|
||||
LinkSta = 1;
|
||||
AccelerateLinkFlag = 0;
|
||||
LinkProcessingStep = 0;
|
||||
LinkProcessingTime = 0;
|
||||
PhyWaitNegotiationSuc = 0;
|
||||
ETH_Start( );
|
||||
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE0 );
|
||||
phy_stat = 0x0;
|
||||
ETH_WritePHYRegister(gPHYAddress, 0x16, phy_stat );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LinkDownCfg
|
||||
*
|
||||
* @brief When the PHY is disconnected, configure the relevant functions.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_LinkDownCfg(void)
|
||||
{
|
||||
LinkSta = 0;
|
||||
LinkVaildFlag = 0;
|
||||
LinkProcessingTime = 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_PHYLink
|
||||
*
|
||||
* @brief Configure MAC parameters after the PHY Link is successful.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_PHYLink( void )
|
||||
{
|
||||
uint32_t phy_stat, phy_anlpar, phy_bcr;
|
||||
|
||||
phy_stat = ETH_ReadPHYRegister( PHY_ADDRESS, PHY_BSR );
|
||||
phy_anlpar = ETH_ReadPHYRegister( PHY_ADDRESS, PHY_ANLPAR);
|
||||
phy_bcr = ETH_ReadPHYRegister( gPHYAddress, PHY_BCR);
|
||||
WCHNET_PhyStatus( phy_stat );
|
||||
|
||||
if(phy_stat & PHY_Linked_Status) //LinkUp
|
||||
{
|
||||
if(phy_bcr & PHY_AutoNegotiation)
|
||||
{
|
||||
if(phy_anlpar == 0)
|
||||
{
|
||||
ETH_LinkUpCfg();
|
||||
}
|
||||
else {
|
||||
if(phy_stat & PHY_AutoNego_Complete)
|
||||
{
|
||||
ETH_LinkUpCfg();
|
||||
}
|
||||
else{
|
||||
PhyWaitNegotiationSuc = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ETH_LinkUpCfg();
|
||||
}
|
||||
}
|
||||
else { //LinkDown
|
||||
/*Link down*/
|
||||
ETH_LinkDownCfg();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_CheckPhyInterruptStatus
|
||||
*
|
||||
* @brief MAC check PHY interrupt status.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_CheckPhyInterruptStatus( void )
|
||||
{
|
||||
uint16_t phyIntStat;
|
||||
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE17 );
|
||||
phyIntStat = ETH_ReadPHYRegister( gPHYAddress, PHY_WOL_STATUS);
|
||||
|
||||
if(phyIntStat & WOL_DONE_INT)
|
||||
{
|
||||
/* Wol done */
|
||||
}
|
||||
else /* Link status change*/
|
||||
{
|
||||
ETH_PHYLink();
|
||||
}
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE0 );
|
||||
phyIntStat = ETH_ReadPHYRegister( gPHYAddress, 0x1E); /* Clear the Interrupt status */
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn PHY_InterruptInit
|
||||
*
|
||||
* @brief Configure PHY interrupt function
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void PHY_InterruptInit(void)
|
||||
{
|
||||
uint16_t RegValue;
|
||||
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE7 );
|
||||
/* Configure interrupt function */
|
||||
RegValue = ETH_ReadPHYRegister(gPHYAddress, PHY_INTERRUPT_MASK);
|
||||
RegValue |= 0x01 << 13;
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_INTERRUPT_MASK, RegValue );
|
||||
/* Clear the Interrupt status */
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE0 );
|
||||
ETH_ReadPHYRegister( gPHYAddress, PHY_INTERRUPT_IND);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn PHY_LEDCfg
|
||||
*
|
||||
* @brief Configure PHY LED function
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void PHY_LEDCfg(void)
|
||||
{
|
||||
uint16_t RegValue;
|
||||
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_PAG_SEL, PHY_REG_PAGE7 );
|
||||
//turn on LED--PHY
|
||||
RegValue = ETH_ReadPHYRegister(gPHYAddress, PHY_INTERRUPT_MASK);
|
||||
RegValue |= 1<<9;
|
||||
ETH_WritePHYRegister(gPHYAddress, PHY_INTERRUPT_MASK, RegValue );
|
||||
|
||||
//<2F><>link_led<65><64>ӳ<EFBFBD>䵽PB17
|
||||
R32_AFIO_PCFR1 |= 1<<30;
|
||||
|
||||
//<2F><>act_led<65><64>ӳ<EFBFBD>䵽PB6
|
||||
R32_AFIO_PCFR2 |= 1<<1;
|
||||
|
||||
//turn on LED--MCU <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CH564RM V1.1<EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ж<EFBFBD>Ӧ<EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʳ<EFBFBD><EFBFBD><EFBFBD>
|
||||
ETH_LED_CTRL = 0x05;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_RegInit
|
||||
*
|
||||
* @brief ETH register initialization.
|
||||
*
|
||||
* @param ETH_InitStruct:initialization struct.
|
||||
* PHYAddress:PHY address.
|
||||
*
|
||||
* @return Initialization status.
|
||||
*/
|
||||
uint32_t ETH_RegInit( ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress )
|
||||
{
|
||||
uint32_t tmpreg = 0;
|
||||
|
||||
/*---------------------- Physical layer configuration -------------------*/
|
||||
/* Set the SMI interface clock, set as the main frequency divided by 42 */
|
||||
tmpreg = ETH->MACMIIAR;
|
||||
tmpreg &= MACMIIAR_CR_MASK;
|
||||
tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
|
||||
ETH->MACMIIAR = (uint32_t)tmpreg;
|
||||
|
||||
/*------------------------ MAC register configuration ----------------------- --------------------*/
|
||||
tmpreg = ETH->MACCR;
|
||||
tmpreg &= MACCR_CLEAR_MASK;
|
||||
tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
|
||||
ETH_InitStruct->ETH_Jabber |
|
||||
ETH_InitStruct->ETH_InterFrameGap |
|
||||
ETH_InitStruct->ETH_ChecksumOffload |
|
||||
ETH_InitStruct->ETH_AutomaticPadCRCStrip |
|
||||
ETH_InitStruct->ETH_DeferralCheck |
|
||||
(1 << 20));
|
||||
/* Write MAC Control Register */
|
||||
ETH->MACCR = (uint32_t)tmpreg;
|
||||
ETH->MACFFR = (uint32_t)(ETH_InitStruct->ETH_ReceiveAll |
|
||||
ETH_InitStruct->ETH_SourceAddrFilter |
|
||||
ETH_InitStruct->ETH_PassControlFrames |
|
||||
ETH_InitStruct->ETH_BroadcastFramesReception |
|
||||
ETH_InitStruct->ETH_DestinationAddrFilter |
|
||||
ETH_InitStruct->ETH_PromiscuousMode |
|
||||
ETH_InitStruct->ETH_MulticastFramesFilter |
|
||||
ETH_InitStruct->ETH_UnicastFramesFilter);
|
||||
/*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
|
||||
/* Write to ETHERNET MACHTHR */
|
||||
ETH->MACHTHR = (uint32_t)ETH_InitStruct->ETH_HashTableHigh;
|
||||
/* Write to ETHERNET MACHTLR */
|
||||
ETH->MACHTLR = (uint32_t)ETH_InitStruct->ETH_HashTableLow;
|
||||
/*----------------------- ETHERNET MACFCR Configuration --------------------*/
|
||||
/* Get the ETHERNET MACFCR value */
|
||||
tmpreg = ETH->MACFCR;
|
||||
/* Clear xx bits */
|
||||
tmpreg &= MACFCR_CLEAR_MASK;
|
||||
tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
|
||||
ETH_InitStruct->ETH_UnicastPauseFrameDetect |
|
||||
ETH_InitStruct->ETH_ReceiveFlowControl |
|
||||
ETH_InitStruct->ETH_TransmitFlowControl);
|
||||
ETH->MACFCR = (uint32_t)tmpreg;
|
||||
|
||||
ETH->MACVLANTR = (uint32_t)(ETH_InitStruct->ETH_VLANTagComparison |
|
||||
ETH_InitStruct->ETH_VLANTagIdentifier);
|
||||
|
||||
tmpreg = ETH->DMAOMR;
|
||||
tmpreg &= DMAOMR_CLEAR_MASK;
|
||||
tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
|
||||
ETH_InitStruct->ETH_FlushReceivedFrame |
|
||||
ETH_InitStruct->ETH_TransmitStoreForward |
|
||||
ETH_InitStruct->ETH_ForwardErrorFrames |
|
||||
ETH_InitStruct->ETH_ForwardUndersizedGoodFrames);
|
||||
ETH->DMAOMR = (uint32_t)tmpreg;
|
||||
|
||||
/* Reset the physical layer */
|
||||
ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_Reset);
|
||||
return ETH_SUCCESS;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_Configuration
|
||||
*
|
||||
* @brief Ethernet configure.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_Configuration( uint8_t *macAddr )
|
||||
{
|
||||
ETH_InitTypeDef ETH_InitStructure;
|
||||
uint16_t timeout = 10000;
|
||||
|
||||
/* Configure Ethernet for normal operating mode*/
|
||||
ETH->PHY_CR |= 1<<31;
|
||||
ETH->PHY_CR &= ~(1<<30);
|
||||
|
||||
gPHYAddress = PHY_ADDRESS;
|
||||
|
||||
/* Software reset */
|
||||
ETH_SoftwareReset();
|
||||
|
||||
/* Wait for software reset */
|
||||
do{
|
||||
Delay_Us(10);
|
||||
if( !--timeout ) break;
|
||||
}while(ETH->DMABMR & ETH_DMABMR_SR);
|
||||
|
||||
/* ETHERNET Configuration */
|
||||
/* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
|
||||
ETH_StructInit(Ð_InitStructure);
|
||||
/* Fill ETH_InitStructure parameters */
|
||||
/*------------------------ MAC -----------------------------------*/
|
||||
#if HARDWARE_CHECKSUM_CONFIG
|
||||
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
|
||||
#endif
|
||||
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
|
||||
/* Filter function configuration */
|
||||
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
|
||||
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Enable;
|
||||
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
|
||||
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
|
||||
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
|
||||
/*------------------------ DMA -----------------------------------*/
|
||||
/* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
|
||||
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
|
||||
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
|
||||
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
|
||||
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
|
||||
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Enable;
|
||||
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Enable;
|
||||
/* Configure Ethernet */
|
||||
ETH_RegInit( Ð_InitStructure, gPHYAddress );
|
||||
|
||||
/* Configure MAC address */
|
||||
ETH->MACA0HR = (uint32_t)((macAddr[5]<<8) | macAddr[4]);
|
||||
ETH->MACA0LR = (uint32_t)(macAddr[0] | (macAddr[1]<<8) | (macAddr[2]<<16) | (macAddr[3]<<24));
|
||||
|
||||
/* Mask the interrupt that Tx good frame count counter reaches half the maximum value */
|
||||
ETH->MMCTIMR = ETH_MMCTIMR_TGFM;
|
||||
/* Mask the interrupt that Rx good unicast frames counter reaches half the maximum value */
|
||||
/* Mask the interrupt that Rx crc error counter reaches half the maximum value */
|
||||
ETH->MMCRIMR = ETH_MMCRIMR_RGUFM | ETH_MMCRIMR_RFAEM | ETH_MMCRIMR_RFCEM;
|
||||
|
||||
PHY_InterruptInit();
|
||||
|
||||
ETH_DMAITConfig(ETH_DMA_IT_NIS |\
|
||||
ETH_DMA_IT_R |\
|
||||
ETH_DMA_IT_T |\
|
||||
ETH_DMA_IT_AIS |\
|
||||
ETH_DMA_IT_RBU |\
|
||||
ETH_DMA_IT_PHYSR,\
|
||||
ENABLE);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_TxPktChainMode
|
||||
*
|
||||
* @brief Ethernet sends data frames in chain mode.
|
||||
*
|
||||
* @param len Send data length
|
||||
* pBuff send buffer pointer
|
||||
*
|
||||
* @return Send status.
|
||||
*/
|
||||
uint32_t ETH_TxPktChainMode(uint16_t len, uint32_t *pBuff )
|
||||
{
|
||||
/* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
|
||||
if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
|
||||
{
|
||||
/* Return ERROR: OWN bit set */
|
||||
return ETH_ERROR;
|
||||
}
|
||||
/* Setting the Frame Length: bits[12:0] */
|
||||
DMATxDescToSet->ControlBufferSize = (len & ETH_DMATxDesc_TBS1);
|
||||
DMATxDescToSet->Buffer1Addr = (uint32_t)pBuff;
|
||||
|
||||
/* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
|
||||
#if HARDWARE_CHECKSUM_CONFIG
|
||||
DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS | ETH_DMATxDesc_CIC_TCPUDPICMP_Full;
|
||||
#else
|
||||
DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
|
||||
#endif
|
||||
|
||||
/* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
|
||||
DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
|
||||
|
||||
/* Clear TBUS ETHERNET DMA flag */
|
||||
ETH->DMASR = ETH_DMASR_TBUS;
|
||||
/* Resume DMA transmission*/
|
||||
ETH->DMATPDR = 0;
|
||||
|
||||
/* Update the ETHERNET DMA global Tx descriptor with next Tx descriptor */
|
||||
/* Chained Mode */
|
||||
/* Selects the next DMA Tx descriptor list for next buffer to send */
|
||||
DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
|
||||
/* Return SUCCESS */
|
||||
return ETH_SUCCESS;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_ETHIsr
|
||||
*
|
||||
* @brief Ethernet Interrupt Service Routine
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void WCHNET_ETHIsr(void)
|
||||
{
|
||||
uint32_t intStat;
|
||||
|
||||
intStat = ETH->DMASR;
|
||||
if (intStat & ETH_DMA_FLAG_AIS)
|
||||
{
|
||||
if (intStat & ETH_DMA_FLAG_RBU)
|
||||
{
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_RBU);
|
||||
}
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_AIS);
|
||||
}
|
||||
|
||||
if( intStat & ETH_DMA_FLAG_NIS )
|
||||
{
|
||||
if( intStat & ETH_DMA_FLAG_R )
|
||||
{
|
||||
/*If you don't use the Ethernet library,
|
||||
* you can do some data processing operations here*/
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_R);
|
||||
}
|
||||
if( intStat & ETH_DMA_FLAG_T )
|
||||
{
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_T);
|
||||
}
|
||||
if( intStat & ETH_DMA_FLAG_PHYSR)
|
||||
{
|
||||
ETH_CheckPhyInterruptStatus( );
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_PHYSR);
|
||||
}
|
||||
ETH_DMAClearITPendingBit(ETH_DMA_FLAG_NIS);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_Init
|
||||
*
|
||||
* @brief Ethernet initialization.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_Init( uint8_t *macAddr )
|
||||
{
|
||||
ETH_Configuration( macAddr );
|
||||
PHY_LEDCfg();
|
||||
ETH_DMATxDescChainInit(DMATxDscrTab, MACTxBuf, ETH_TXBUFNB);
|
||||
ETH_DMARxDescChainInit(DMARxDscrTab, MACRxBuf, ETH_RXBUFNB);
|
||||
pDMARxSet = DMARxDscrTab;
|
||||
pDMATxSet = DMATxDscrTab;
|
||||
NVIC_EnableIRQ(ETH_IRQn);
|
||||
NVIC_SetPriority(ETH_IRQn, 0);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LibInit
|
||||
*
|
||||
* @brief Ethernet library initialization program
|
||||
*
|
||||
* @return command status
|
||||
*/
|
||||
uint8_t ETH_LibInit( uint8_t *ip, uint8_t *gwip, uint8_t *mask, uint8_t *macaddr )
|
||||
{
|
||||
uint8_t s;
|
||||
struct _WCH_CFG cfg;
|
||||
|
||||
memset(&cfg,0,sizeof(cfg));
|
||||
cfg.TxBufSize = ETH_TX_BUF_SZE;
|
||||
cfg.TCPMss = WCHNET_TCP_MSS;
|
||||
cfg.HeapSize = WCHNET_MEM_HEAP_SIZE;
|
||||
cfg.ARPTableNum = WCHNET_NUM_ARP_TABLE;
|
||||
cfg.MiscConfig0 = WCHNET_MISC_CONFIG0;
|
||||
cfg.MiscConfig1 = WCHNET_MISC_CONFIG1;
|
||||
cfg.net_send = ETH_TxPktChainMode;
|
||||
cfg.CheckValid = WCHNET_CFG_VALID;
|
||||
s = WCHNET_ConfigLIB(&cfg);
|
||||
if( s ){
|
||||
return (s);
|
||||
}
|
||||
s = WCHNET_Init(ip,gwip,mask,macaddr);
|
||||
ETH_Init( macaddr );
|
||||
return (s);
|
||||
}
|
||||
|
||||
/******************************** endfile @ eth_driver ******************************/
|
||||
55
NetLib/eth_driver.h
Normal file
55
NetLib/eth_driver.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/********************************** (C) COPYRIGHT ************* ******************
|
||||
* File Name : eth_driver.h
|
||||
* Author : WCH
|
||||
* Version : V1.3.0
|
||||
* Date : 2024/05/05
|
||||
* Description : This file contains the headers of the ETH Driver.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __ETH_DRIVER__
|
||||
#define __ETH_DRIVER__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "debug.h"
|
||||
#include "wchnet.h"
|
||||
|
||||
#define PHY_ADDRESS 1
|
||||
|
||||
#define ROM_CFG_USERADR_ID 0X30CC
|
||||
|
||||
#define PHY_ANLPAR_SELECTOR_FIELD 0x1F
|
||||
#define PHY_ANLPAR_SELECTOR_VALUE 0x01 /* 5B'00001 */
|
||||
|
||||
#ifndef WCHNETTIMERPERIOD
|
||||
#define WCHNETTIMERPERIOD 10 /* Timer period, in Ms. */
|
||||
#endif
|
||||
|
||||
extern ETH_DMADESCTypeDef *DMATxDescToSet;
|
||||
extern ETH_DMADESCTypeDef *DMARxDescToGet;
|
||||
extern SOCK_INF SocketInf[ ];
|
||||
extern uint8_t RemoteIp[4];
|
||||
|
||||
#define ETH_LED_CTRL (*((volatile uint32_t *)0x40400158))
|
||||
|
||||
void ETH_PHYLink( void );
|
||||
void WCHNET_ETHIsr( void );
|
||||
void WCHNET_MainTask( void );
|
||||
void ETH_LedConfiguration(void);
|
||||
void ETH_Init( uint8_t *macAddr );
|
||||
void ETH_LedLinkSet( uint8_t mode );
|
||||
void ETH_LedDataSet( uint8_t mode );
|
||||
void WCHNET_TimeIsr( uint16_t timperiod );
|
||||
void ETH_Configuration( uint8_t *macAddr );
|
||||
uint8_t ETH_LibInit( uint8_t *ip, uint8_t *gwip, uint8_t *mask, uint8_t *macaddr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
BIN
NetLib/libCH56xNET.a
Normal file
BIN
NetLib/libCH56xNET.a
Normal file
Binary file not shown.
170
NetLib/net_config.h
Normal file
170
NetLib/net_config.h
Normal file
@@ -0,0 +1,170 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : net_config.h
|
||||
* Author : WCH
|
||||
* Version : V1.30
|
||||
* Date : 2024/05/05
|
||||
* Description : This file contains the configurations of
|
||||
* Ethernet protocol stack library
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __NET_CONFIG_H__
|
||||
#define __NET_CONFIG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* socket configuration, IPRAW + UDP + TCP + TCP_LISTEN = number of sockets
|
||||
*/
|
||||
#define WCHNET_NUM_IPRAW 1 /* Number of IPRAW connections */
|
||||
|
||||
#define WCHNET_NUM_UDP 4 /* The number of UDP connections */
|
||||
|
||||
#define WCHNET_NUM_TCP 1 /* Number of TCP connections */
|
||||
|
||||
#define WCHNET_NUM_TCP_LISTEN 1 /* Number of TCP listening */
|
||||
|
||||
/* The number of sockets, the maximum is 31 */
|
||||
#define WCHNET_MAX_SOCKET_NUM (WCHNET_NUM_IPRAW+WCHNET_NUM_UDP+WCHNET_NUM_TCP+WCHNET_NUM_TCP_LISTEN)
|
||||
|
||||
#define WCHNET_TCP_MSS 768 /* Size of TCP MSS Ĭ<><C4AC>:1460*/
|
||||
|
||||
#define WCHNET_NUM_POOL_BUF (WCHNET_NUM_TCP*2+2) /* The number of POOL BUFs, the number of receive queues */
|
||||
|
||||
/*********************************************************************
|
||||
* MAC queue configuration
|
||||
*/
|
||||
#define ETH_TXBUFNB 2 /* The number of descriptors sent by the MAC */
|
||||
|
||||
#define ETH_RXBUFNB 4 /* Number of MAC received descriptors */
|
||||
|
||||
#ifndef ETH_MAX_PACKET_SIZE
|
||||
#define ETH_RX_BUF_SZE 1520 /* MAC receive buffer length, an integer multiple of 4 */
|
||||
#define ETH_TX_BUF_SZE 1520 /* MAC send buffer length, an integer multiple of 4 */
|
||||
#else
|
||||
#define ETH_RX_BUF_SZE ETH_MAX_PACKET_SIZE
|
||||
#define ETH_TX_BUF_SZE ETH_MAX_PACKET_SIZE
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Functional configuration
|
||||
*/
|
||||
#define WCHNET_PING_ENABLE 1 /* PING is enabled, PING is enabled by default */
|
||||
|
||||
#define TCP_RETRY_COUNT 20 /* The number of TCP retransmissions, the default value is 20 */
|
||||
|
||||
#define TCP_RETRY_PERIOD 10 /* TCP retransmission period, the default value is 10, the unit is 50ms */
|
||||
|
||||
#define SOCKET_SEND_RETRY 1 /* Send failed retry configuration, 1: enable, 0: disable */
|
||||
|
||||
#define HARDWARE_CHECKSUM_CONFIG 0 /* Hardware checksum checking and insertion configuration, 1: enable, 0: disable */
|
||||
|
||||
#define FINE_DHCP_PERIOD 8 /* Fine DHCP period, the default value is 8, the unit is 250ms */
|
||||
|
||||
#define CFG0_TCP_SEND_COPY 1 /* TCP send buffer copy, 1: copy, 0: not copy */
|
||||
|
||||
#define CFG0_TCP_RECV_COPY 1 /* TCP receive replication optimization, internal debugging use */
|
||||
|
||||
#define CFG0_TCP_OLD_DELETE 0 /* Delete oldest TCP connection, 1: enable, 0: disable */
|
||||
|
||||
#define CFG0_IP_REASS_PBUFS 0 /* Number of reassembled IP PBUFs */
|
||||
|
||||
#define CFG0_TCP_DEALY_ACK_DISABLE 1 /* 1: disable TCP delay ACK 0: enable TCP delay ACK */
|
||||
|
||||
/*********************************************************************
|
||||
* Memory related configuration
|
||||
*/
|
||||
/* If you want to achieve a higher transmission speed,
|
||||
* try to increase RECE_BUF_LEN to (WCHNET_TCP_MSS*4)
|
||||
* and increase WCHNET_NUM_TCP_SEG to (WCHNET_NUM_TCP*4)*/
|
||||
#define RECE_BUF_LEN (WCHNET_TCP_MSS*2) /* socket receive buffer size */
|
||||
|
||||
#define WCHNET_NUM_PBUF WCHNET_NUM_POOL_BUF /* Number of PBUF structures */
|
||||
|
||||
#define WCHNET_NUM_TCP_SEG (WCHNET_NUM_TCP*2) /* The number of TCP segments used to send */
|
||||
|
||||
#define WCHNET_MEM_HEAP_SIZE (((WCHNET_TCP_MSS+0x10+54+8)*WCHNET_NUM_TCP_SEG)+ETH_TX_BUF_SZE+64+2*0x18) /* memory heap size */
|
||||
|
||||
#define WCHNET_NUM_ARP_TABLE 50 /* Number of ARP lists */
|
||||
|
||||
#define WCHNET_MEM_ALIGNMENT 4 /* 4 byte alignment */
|
||||
|
||||
#if CFG0_IP_REASS_PBUFS
|
||||
#define WCHNET_NUM_IP_REASSDATA 2 /* Number of reassembled IP structures */
|
||||
/*1: When using the fragmentation function,
|
||||
* ensure that the size of WCHNET_SIZE_POOL_BUF is large enough to store a single fragmented packet*/
|
||||
#define WCHNET_SIZE_POOL_BUF (((1500 + 14 + 4) + 3) & ~3) /* Buffer size for receiving a single packet */
|
||||
/*2: When creating a socket that can receive fragmented packets,
|
||||
* ensure that "RecvBufLen" member of the "struct _SOCK_INF" structure
|
||||
* (the parameter initialized when calling WCHNET_SocketCreat) is sufficient
|
||||
* to receive a complete fragmented packet */
|
||||
#else
|
||||
#define WCHNET_NUM_IP_REASSDATA 0 /* Number of reassembled IP structures */
|
||||
#define WCHNET_SIZE_POOL_BUF (((WCHNET_TCP_MSS + 40 + 14 + 4) + 3) & ~3) /* Buffer size for receiving a single packet */
|
||||
#endif
|
||||
|
||||
/* Check receive buffer */
|
||||
#if(WCHNET_NUM_POOL_BUF * WCHNET_SIZE_POOL_BUF < ETH_RX_BUF_SZE)
|
||||
#error "WCHNET_NUM_POOL_BUF or WCHNET_TCP_MSS Error"
|
||||
#error "Please Increase WCHNET_NUM_POOL_BUF or WCHNET_TCP_MSS to make sure the receive buffer is sufficient"
|
||||
#endif
|
||||
/* Check the configuration of the SOCKET quantity */
|
||||
#if( WCHNET_NUM_TCP_LISTEN && !WCHNET_NUM_TCP )
|
||||
#error "WCHNET_NUM_TCP Error,Please Configure WCHNET_NUM_TCP >= 1"
|
||||
#endif
|
||||
/* Check byte alignment must be a multiple of 4 */
|
||||
#if((WCHNET_MEM_ALIGNMENT % 4) || (WCHNET_MEM_ALIGNMENT == 0))
|
||||
#error "WCHNET_MEM_ALIGNMENT Error,Please Configure WCHNET_MEM_ALIGNMENT = 4 * N, N >=1"
|
||||
#endif
|
||||
/* TCP maximum segment length */
|
||||
#if((WCHNET_TCP_MSS > 1460) || (WCHNET_TCP_MSS < 60))
|
||||
#error "WCHNET_TCP_MSS Error,Please Configure WCHNET_TCP_MSS >= 60 && WCHNET_TCP_MSS <= 1460"
|
||||
#endif
|
||||
/* Number of ARP cache tables */
|
||||
#if((WCHNET_NUM_ARP_TABLE > 0X7F) || (WCHNET_NUM_ARP_TABLE < 1))
|
||||
#error "WCHNET_NUM_ARP_TABLE Error,Please Configure WCHNET_NUM_ARP_TABLE >= 1 && WCHNET_NUM_ARP_TABLE <= 0X7F"
|
||||
#endif
|
||||
/* Check POOL BUF configuration */
|
||||
#if(WCHNET_NUM_POOL_BUF < 1)
|
||||
#error "WCHNET_NUM_POOL_BUF Error,Please Configure WCHNET_NUM_POOL_BUF >= 1"
|
||||
#endif
|
||||
/* Check PBUF structure configuration */
|
||||
#if(WCHNET_NUM_PBUF < 1)
|
||||
#error "WCHNET_NUM_PBUF Error,Please Configure WCHNET_NUM_PBUF >= 1"
|
||||
#endif
|
||||
/* Check IP Assignment Configuration */
|
||||
#if(CFG0_IP_REASS_PBUFS && ((WCHNET_NUM_IP_REASSDATA > 10) || (WCHNET_NUM_IP_REASSDATA < 1)))
|
||||
#error "WCHNET_NUM_IP_REASSDATA Error,Please Configure WCHNET_NUM_IP_REASSDATA < 10 && WCHNET_NUM_IP_REASSDATA >= 1 "
|
||||
#endif
|
||||
/* Check the number of reassembled IP PBUFs */
|
||||
#if(CFG0_IP_REASS_PBUFS > WCHNET_NUM_POOL_BUF)
|
||||
#error "WCHNET_NUM_POOL_BUF Error,Please Configure CFG0_IP_REASS_PBUFS < WCHNET_NUM_POOL_BUF"
|
||||
#endif
|
||||
/* Check Timer period, in Ms. */
|
||||
#if(WCHNETTIMERPERIOD > 50)
|
||||
#error "WCHNETTIMERPERIOD Error,Please Configure WCHNETTIMERPERIOD < 50"
|
||||
#endif
|
||||
|
||||
/* Configuration value 0 */
|
||||
#define WCHNET_MISC_CONFIG0 (((CFG0_TCP_SEND_COPY) << 0) |\
|
||||
((CFG0_TCP_RECV_COPY) << 1) |\
|
||||
((CFG0_TCP_OLD_DELETE) << 2) |\
|
||||
((CFG0_IP_REASS_PBUFS) << 3) |\
|
||||
((CFG0_TCP_DEALY_ACK_DISABLE) << 8))
|
||||
/* Configuration value 1 */
|
||||
#define WCHNET_MISC_CONFIG1 (((WCHNET_MAX_SOCKET_NUM)<<0)|\
|
||||
((WCHNET_PING_ENABLE) << 13) |\
|
||||
((TCP_RETRY_COUNT) << 14) |\
|
||||
((TCP_RETRY_PERIOD) << 19) |\
|
||||
((SOCKET_SEND_RETRY) << 25) |\
|
||||
((HARDWARE_CHECKSUM_CONFIG) << 26)|\
|
||||
((FINE_DHCP_PERIOD) << 27))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
1095
NetLib/net_function.c
Normal file
1095
NetLib/net_function.c
Normal file
File diff suppressed because it is too large
Load Diff
160
NetLib/net_function.h
Normal file
160
NetLib/net_function.h
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* net_function.h
|
||||
*
|
||||
* Created on: May 21, 2025
|
||||
* Author: cc
|
||||
*/
|
||||
|
||||
#ifndef NET_FUNCTION_H_
|
||||
#define NET_FUNCTION_H_
|
||||
|
||||
#include "ch564.h"
|
||||
#include "eth_driver.h"
|
||||
#include "net_config.h"
|
||||
#include "debug.h"
|
||||
|
||||
#define KEEPALIVE_ENABLE 0 //Enable keep alive function ʹ<><CAB9>TCP<43><50><EFBFBD><EFBFBD><EEB9A6>
|
||||
#define NET_Socket_Num_Max 4 //<2F><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD>ഴ<EFBFBD><E0B4B4>4<EFBFBD><34><EFBFBD><EFBFBD><D7BD>֣<EFBFBD> <20><EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:WCHNET_MAX_SOCKET_NUM
|
||||
#define DOMAINNAME_MAX 64 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD>
|
||||
|
||||
#define SERVER_NAME_DNS "www.boonlive-rcu.com" //<2F>ƶ˷<C6B6><CBB7><EFBFBD><EFBFBD><EFBFBD> - Ĭ<><C4AC>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define TFTPSERVER_NAME_DNS "blv-tftp-log.blv-oa.com" //TFTP<54><50>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - Ĭ<><C4AC>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define SERVER_COMM_Port 3339
|
||||
|
||||
#define SocketIdnex_BLVSeriver 0x00 //<2F><EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>±<EFBFBD>
|
||||
|
||||
#define Net_DNS_Port 53 //DNS<4E><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NET_INIT = 0,
|
||||
NET_PHY_WAIT,
|
||||
NET_DHCP,
|
||||
NET_DHCP_WAIT,
|
||||
NET_TFTP,
|
||||
NET_DNS,
|
||||
NET_DNS_WAIT,
|
||||
NET_WAIT,
|
||||
NET_COMPLETE,
|
||||
|
||||
NET_WAIT_MAC_RESTART,
|
||||
//NET_START_TO_INIT,
|
||||
NET_CON_WAIT,
|
||||
NET_SOCKET_WAIT,
|
||||
}NET_STA;
|
||||
|
||||
enum UDP_INTERNAL_STA{
|
||||
STA_INIT = 0,
|
||||
STA_INIT_WAIT = 1,
|
||||
STA_INIT_IDLE = 2,
|
||||
STA_Realy_Upgrade = 3,
|
||||
STA_INIT_CONNECT,
|
||||
STA_LOG,
|
||||
STA_SEND_WAIT,
|
||||
STA_SEND_LOG,
|
||||
STA_SEND_RETRY,
|
||||
STA_END,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t device_ip[4]; // <20>豸 IP
|
||||
uint8_t gateway[4]; // <20><><EFBFBD>ص<EFBFBD>ַ
|
||||
uint8_t subnet[4]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t mac_addr[6]; // <20><><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>MAC<41><43>ַ
|
||||
uint8_t dns_server_ip[4]; // DNS<4E><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַ
|
||||
uint8_t dns_server2_ip[4]; // DNS<4E><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַ
|
||||
uint8_t SocketId[NET_Socket_Num_Max];
|
||||
|
||||
}WCHNET_INFO_T;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t init_flag:1; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־λ
|
||||
uint8_t register_flag:1; //ע<><D7A2><EFBFBD><EFBFBD>־λ
|
||||
uint8_t search_ack_flag:1; //<2F><><EFBFBD>ͱ<EFBFBD>־λ
|
||||
uint8_t dhcp_en:1; //DHCP<43>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t dns_sta:4; //DNS״̬
|
||||
|
||||
uint8_t dhcp_flg:1; //DHCP<43><50><EFBFBD><EFBFBD>ip<69>ɹ<EFBFBD><C9B9><EFBFBD>־
|
||||
uint8_t dns_flg:1; //DNS<4E><53>ȡip<69>ɹ<EFBFBD><C9B9><EFBFBD>־
|
||||
uint8_t Udp_Internal_sta:4; //UDP״̬<D7B4><CCAC>״̬
|
||||
uint8_t PHY_State:2; //NET PHY״̬ 0x00:<3A><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ӣ<EFBFBD>0x01:<3A><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DNS״̬
|
||||
|
||||
uint8_t register_num:4; //ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t dns_fail:4; //DNSʧ<53>ܼ<EFBFBD><DCBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>DNS<4E><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
||||
|
||||
uint8_t server_dns_flag:1; //DNS<4E>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t tftp_dns_flag:1; //DNS<4E>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>tftp<74><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t mqtt_dns_flag:1; //MQTT<54>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
|
||||
uint8_t online_state:3; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ 0:<3A><><EFBFBD><EFBFBD> 1:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2:<3A><><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3:<3A>ƶ˷<C6B6><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t sync_tick:2; //ͬ<><CDAC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
uint8_t udp_dns_flag:1; //udp<64><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
|
||||
uint8_t server_select:1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1> 0x00:Ĭ<><C4AC><EFBFBD>ƶ˷<C6B6><CBB7><EFBFBD><EFBFBD><EFBFBD>,0x01:<3A><><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t net_retry_flag:1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>־λ -
|
||||
uint8_t net_retry_num:5; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD> - <20><><EFBFBD><EFBFBD>DHCP<43><50><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>DNS<4E><53><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
|
||||
|
||||
uint8_t con_flag:1; //ֱ<><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t rcu_reboot_flag:1; //RCU<43><55><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
|
||||
uint8_t udp_scan_cnt:6; //udpɨ<70><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
uint8_t active_cmd_flag; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
|
||||
|
||||
uint8_t net_sta; //<2F><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>ǰ״̬
|
||||
uint8_t udp_sta; //UDP<44><50><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
||||
uint8_t ServerDename[DOMAINNAME_MAX]; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t dis_ip[4]; //<2F>ƶ˷<C6B6><CBB7><EFBFBD><EFBFBD><EFBFBD>ip<69><70>ַ
|
||||
uint8_t tftp_ip[4]; //TFTP<54><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t goal_ip[4]; //Ŀ<><C4BF>ip,
|
||||
|
||||
uint8_t udp_send_flag; //<2F><><EFBFBD>ͱ<EFBFBD><CDB1><EFBFBD>
|
||||
uint8_t udp_retry_cnt; //<2F>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t udp_retry_num; //<2F>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint16_t local_port; //<2F><><EFBFBD>ض˿<D8B6>
|
||||
uint16_t goal_port; //Ŀ<><C4BF>port
|
||||
uint16_t dis_port; //<2F>ƶ˷<C6B6><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
|
||||
uint16_t frame_no; //<2F><><EFBFBD>͵<EFBFBD>֡<EFBFBD><D6A1>
|
||||
uint16_t ack_frame; //ACK֡<4B><D6A1>
|
||||
|
||||
uint16_t udp_timesync_cnt; //ʱ<><CAB1>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - 1sһ<73><D2BB>
|
||||
uint16_t udp_periodic_cnt; //<2F><><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD> - 1sһ<73><D2BB>
|
||||
uint16_t udp_periodic_time; //<2F><><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <20><>λ:S
|
||||
|
||||
uint32_t udp_retry_tick; //<2F>ط<EFBFBD><D8B7><EFBFBD>ʱʱ<CAB1><CAB1><EFBFBD><EFBFBD>
|
||||
uint32_t udp_retry_time; //<2F>ط<EFBFBD><D8B7><EFBFBD>ʱʱ<CAB1><CAB1>
|
||||
|
||||
uint32_t con_tick; //ֱ<><D6B1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> 2023-05-27
|
||||
|
||||
uint32_t udp_idle_tick; //UDP<44><50><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
||||
uint32_t udp_online_tick; //UDP<44><50><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
||||
uint32_t udp_online_time; //UDP<44><50><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
|
||||
uint32_t wait_cot; //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint32_t register_tick; //ע<><D7A2><EFBFBD><EFBFBD>ʱ
|
||||
uint32_t search_ack_tick; //<2F><><EFBFBD>ͼ<EFBFBD>ʱ
|
||||
|
||||
}DEVICE_NET_APPINFO;
|
||||
|
||||
/*TFTP <20><><EFBFBD><EFBFBD>LOG<4F><47><EFBFBD>ض<EFBFBD><D8B6><EFBFBD>*/
|
||||
#define TFTP_Destination_Port 69 //TFTP<54><50><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
|
||||
#define TFTP_LOG_Local_Port 65500 //TFTP LOG<4F><47><EFBFBD>䱾<EFBFBD>ض˿<D8B6>
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t Port; //TFTP LOG<4F><47><EFBFBD>䱾<EFBFBD>ض˿<D8B6>
|
||||
uint16_t Time; //<2F><>־<EFBFBD>ϱ<EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
uint8_t DN_Lens; //TFTP<54><50>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
char DomainName[DOMAINNAME_MAX]; //TFTP<54><50>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}TFTP_LOG;
|
||||
|
||||
extern WCHNET_INFO_T g_netinfo;
|
||||
extern DEVICE_NET_APPINFO server_info;
|
||||
|
||||
uint8_t WCHNET_LIB_Init(void);
|
||||
void WCHNET_HandleGlobalInt(void);
|
||||
|
||||
void NetWork_Task(void);
|
||||
|
||||
#endif /* NET_FUNCTION_H_ */
|
||||
605
NetLib/wchnet.h
Normal file
605
NetLib/wchnet.h
Normal file
@@ -0,0 +1,605 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : wchnet.h
|
||||
* Author : WCH
|
||||
* Version : V1.90
|
||||
* Date : 2023/05/12
|
||||
* Description : This file contains the headers of
|
||||
* the Ethernet protocol stack library.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __WCHNET_H__
|
||||
#define __WCHNET_H__
|
||||
|
||||
#include "stdint.h"
|
||||
#ifndef NET_LIB
|
||||
#include "net_config.h"
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define WCHNET_LIB_VER 0x1A //the library version number
|
||||
#define WCHNET_CFG_VALID 0x12345678 //Configuration value valid flag
|
||||
|
||||
/* LED state @LED_STAT */
|
||||
#define LED_ON 0
|
||||
#define LED_OFF 1
|
||||
|
||||
/* PHY state @PHY_STAT */
|
||||
#define PHY_LINK_SUCCESS (1 << 2) //PHY connection success
|
||||
#define PHY_AUTO_SUCCESS (1 << 5) //PHY auto negotiation completed
|
||||
|
||||
/* Library initialization state @CFG_INIT_STAT */
|
||||
#define INIT_OK 0x00
|
||||
#define INIT_ERR_RX_BUF_SIZE 0x01
|
||||
#define INIT_ERR_TCP_MSS 0x02
|
||||
#define INIT_ERR_HEAP_SIZE 0x03
|
||||
#define INIT_ERR_ARP_TABLE_NEM 0x04
|
||||
#define INIT_ERR_MISC_CONFIG0 0x05
|
||||
#define INIT_ERR_MISC_CONFIG1 0x06
|
||||
#define INIT_ERR_FUNC_SEND 0x09
|
||||
#define INIT_ERR_CHECK_VALID 0xFF
|
||||
|
||||
/* Socket protocol type */
|
||||
#define PROTO_TYPE_IP_RAW 0 //IP layer raw data
|
||||
#define PROTO_TYPE_UDP 2 //UDP protocol
|
||||
#define PROTO_TYPE_TCP 3 //TCP protocol
|
||||
|
||||
/* interrupt status */
|
||||
/* The following are the states
|
||||
* that GLOB_INT will generate */
|
||||
#define GINT_STAT_UNREACH (1 << 0) //unreachable interrupt
|
||||
#define GINT_STAT_IP_CONFLI (1 << 1) //IP conflict interrupt
|
||||
#define GINT_STAT_PHY_CHANGE (1 << 2) //PHY state change interrupt
|
||||
#define GINT_STAT_SOCKET (1 << 4) //socket related interrupt
|
||||
|
||||
/* The following are the states
|
||||
* that Sn_INT will generate*/
|
||||
#define SINT_STAT_RECV (1 << 2) //the socket receives data or the receive buffer is not empty
|
||||
#define SINT_STAT_CONNECT (1 << 3) //connect successfully,generated in TCP mode
|
||||
#define SINT_STAT_DISCONNECT (1 << 4) //disconnect,generated in TCP mode
|
||||
#define SINT_STAT_TIM_OUT (1 << 6) //timeout disconnect,generated in TCP mode
|
||||
|
||||
|
||||
/* Definitions for error constants. @ERR_T */
|
||||
#define ERR_T
|
||||
#define WCHNET_ERR_SUCCESS 0x00 //No error, everything OK
|
||||
#define WCHNET_ERR_BUSY 0x10 //busy
|
||||
#define WCHNET_ERR_MEM 0x11 //Out of memory error
|
||||
#define WCHNET_ERR_BUF 0x12 //Buffer error
|
||||
#define WCHNET_ERR_TIMEOUT 0x13 //Timeout
|
||||
#define WCHNET_ERR_RTE 0x14 //Routing problem
|
||||
#define WCHNET_ERR_ABRT 0x15 //Connection aborted
|
||||
#define WCHNET_ERR_RST 0x16 //Connection reset
|
||||
#define WCHNET_ERR_CLSD 0x17 //Connection closed
|
||||
#define WCHNET_ERR_CONN 0x18 //Not connected
|
||||
#define WCHNET_ERR_VAL 0x19 //Illegal value
|
||||
#define WCHNET_ERR_ARG 0x1a //Illegal argument
|
||||
#define WCHNET_ERR_USE 0x1b //Address in use
|
||||
#define WCHNET_ERR_IF 0x1c //Low-level netif error
|
||||
#define WCHNET_ERR_ISCONN 0x1d //Already connected
|
||||
#define WCHNET_ERR_INPROGRESS 0x1e //Operation in progress
|
||||
#define WCHNET_ERR_SOCKET_MEM 0X20 //Socket information error
|
||||
#define WCHNET_ERR_UNSUPPORT_PROTO 0X21 //unsupported protocol type
|
||||
#define WCHNET_RET_ABORT 0x5F //command process fail
|
||||
#define WCHNET_ERR_UNKNOW 0xFA //unknow
|
||||
|
||||
/* unreachable condition related codes */
|
||||
#define UNREACH_CODE_HOST 0 //host unreachable
|
||||
#define UNREACH_CODE_NET 1 //network unreachable
|
||||
#define UNREACH_CODE_PROTOCOL 2 //protocol unreachable
|
||||
#define UNREACH_CODE_PROT 3 //port unreachable
|
||||
/*For other values, please refer to the RFC792 document*/
|
||||
|
||||
/* TCP disconnect related codes */
|
||||
#define TCP_CLOSE_NORMAL 0 //normal disconnect,a four-way handshake
|
||||
#define TCP_CLOSE_RST 1 //reset the connection and close
|
||||
#define TCP_CLOSE_ABANDON 2 //drop connection, and no termination message is sent
|
||||
|
||||
/* socket state code */
|
||||
#define SOCK_STAT_CLOSED 0X00 //socket close
|
||||
#define SOCK_STAT_OPEN 0X05 //socket open
|
||||
|
||||
/* TCP state code */
|
||||
#define TCP_CLOSED 0 //TCP close
|
||||
#define TCP_LISTEN 1 //TCP listening
|
||||
#define TCP_SYN_SENT 2 //SYN send, connect request
|
||||
#define TCP_SYN_RCVD 3 //SYN received, connection request received
|
||||
#define TCP_ESTABLISHED 4 //TCP connection establishment
|
||||
#define TCP_FIN_WAIT_1 5 //WAIT_1 state
|
||||
#define TCP_FIN_WAIT_2 6 //WAIT_2 state
|
||||
#define TCP_CLOSE_WAIT 7 //wait to close
|
||||
#define TCP_CLOSING 8 //closing
|
||||
#define TCP_LAST_ACK 9 //LAST_ACK
|
||||
#define TCP_TIME_WAIT 10 //2MSL wait
|
||||
|
||||
/* The following values are fixed and cannot be changed */
|
||||
#define WCHNET_MEM_ALIGN_SIZE(size) (((size) + WCHNET_MEM_ALIGNMENT - 1) & ~(WCHNET_MEM_ALIGNMENT - 1))
|
||||
#define WCHNET_SIZE_IPRAW_PCB 0x1C //IPRAW PCB size
|
||||
#define WCHNET_SIZE_UDP_PCB 0x20 //UDP PCB size
|
||||
#define WCHNET_SIZE_TCP_PCB 0xB4 //TCP PCB size
|
||||
#define WCHNET_SIZE_TCP_PCB_LISTEN 0x24 //TCP LISTEN PCB size
|
||||
#define WCHNET_SIZE_IP_REASSDATA 0x20 //IP reassembled Management
|
||||
#define WCHNET_SIZE_PBUF 0x10 //Packet Buf
|
||||
#define WCHNET_SIZE_TCP_SEG 0x14 //TCP SEG structure
|
||||
#define WCHNET_SIZE_MEM 0x08 //sizeof(struct mem)
|
||||
#define WCHNET_SIZE_ARP_TABLE 0x18 //sizeof ARP table
|
||||
|
||||
#define WCHNET_MEMP_SIZE ((WCHNET_MEM_ALIGNMENT - 1) + \
|
||||
(WCHNET_NUM_IPRAW * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IPRAW_PCB)) + \
|
||||
(WCHNET_NUM_UDP * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_UDP_PCB)) + \
|
||||
(WCHNET_NUM_TCP * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB)) + \
|
||||
(WCHNET_NUM_TCP_LISTEN * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB_LISTEN)) + \
|
||||
(WCHNET_NUM_TCP_SEG * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_SEG)) + \
|
||||
(WCHNET_NUM_IP_REASSDATA * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IP_REASSDATA)) + \
|
||||
(WCHNET_NUM_PBUF * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF)) + \
|
||||
(WCHNET_NUM_POOL_BUF * (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF) + WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_POOL_BUF))))
|
||||
|
||||
#define HEAP_MEM_ALIGN_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_MEM))
|
||||
#define WCHNET_RAM_HEAP_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_MEM_HEAP_SIZE) + HEAP_MEM_ALIGN_SIZE )
|
||||
#define WCHNET_RAM_ARP_TABLE_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_ARP_TABLE) * WCHNET_NUM_ARP_TABLE)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t length;
|
||||
uint32_t buffer;
|
||||
}ETHFrameType;
|
||||
|
||||
/* LED callback type */
|
||||
typedef void (*led_callback)( uint8_t setbit );
|
||||
|
||||
/* net send callback type */
|
||||
typedef uint32_t (*eth_tx_set )( uint16_t len, uint32_t *pBuff );
|
||||
|
||||
/* net receive callback type */
|
||||
typedef uint32_t (*eth_rx_set )( ETHFrameType *pkt );
|
||||
|
||||
/* DNS callback type */
|
||||
typedef void (*dns_callback)( const char *name, uint8_t *ipaddr, void *callback_arg );
|
||||
|
||||
/* DHCP callback type */
|
||||
typedef uint8_t (*dhcp_callback)( uint8_t status, void * );
|
||||
|
||||
/* socket receive callback type */
|
||||
struct _SOCK_INF;
|
||||
typedef void (*pSockRecv)( struct _SOCK_INF *, uint32_t, uint16_t, uint8_t *, uint32_t);
|
||||
|
||||
/* Socket information struct */
|
||||
typedef struct _SOCK_INF
|
||||
{
|
||||
uint32_t IntStatus; //interrupt state
|
||||
uint32_t SockIndex; //Socket index value
|
||||
uint32_t RecvStartPoint; //Start pointer of the receive buffer
|
||||
uint32_t RecvBufLen; //Receive buffer length
|
||||
uint32_t RecvCurPoint; //current pointer to receive buffer
|
||||
uint32_t RecvReadPoint; //The read pointer of the receive buffer
|
||||
uint32_t RecvRemLen; //The length of the remaining data in the receive buffer
|
||||
uint32_t ProtoType; //protocol type
|
||||
uint32_t SockStatus; //Low byte Socket state, the next low byte is TCP state, only meaningful in TCP mode
|
||||
uint32_t DesPort; //destination port
|
||||
uint32_t SourPort; //Source port, protocol type in IPRAW mode
|
||||
uint8_t IPAddr[4]; //Socket destination IP address
|
||||
void *Resv1; //Reserved, for internal use, for saving individual PCBs
|
||||
void *Resv2; //Reserved, used internally, used by TCP Server
|
||||
pSockRecv AppCallBack; //receive callback function
|
||||
} SOCK_INF;
|
||||
|
||||
struct _WCH_CFG
|
||||
{
|
||||
uint32_t TxBufSize; //MAC send buffer size, reserved for use
|
||||
uint32_t TCPMss; //TCP MSS size
|
||||
uint32_t HeapSize; //heap memory size
|
||||
uint32_t ARPTableNum; //Number of ARP lists
|
||||
uint32_t MiscConfig0; //Miscellaneous Configuration 0
|
||||
/* Bit 0 TCP send buffer copy 1: copy, 0: not copy */
|
||||
/* Bit 1 TCP receive replication optimization, used for internal debugging */
|
||||
/* bit 2 delete oldest TCP connection 1: enable, 0: disable */
|
||||
/* Bits 3-7 Number of PBUFs of IP segments */
|
||||
/* Bit 8 TCP Delay ACK disable */
|
||||
uint32_t MiscConfig1; //Miscellaneous Configuration 1
|
||||
/* Bits 0-7 Number of Sockets*/
|
||||
/* Bits 8-12 Reserved */
|
||||
/* Bit 13 PING enable, 1: On 0: Off */
|
||||
/* Bits 14-18 TCP retransmission times */
|
||||
/* Bits 19-23 TCP retransmission period, in 50 milliseconds */
|
||||
/* bit 25 send failed retry, 1: enable, 0: disable */
|
||||
/* bit 26 Select whether to perform IPv4 checksum check on
|
||||
* the TCP/UDP/ICMP header of the received frame payload by hardware,
|
||||
* and calculate and insert the checksum of the IP header and payload of the sent frame by hardware.*/
|
||||
/* Bits 27-31 period (in 250 milliseconds) of Fine DHCP periodic process */
|
||||
led_callback led_link; //PHY Link Status Indicator
|
||||
led_callback led_data; //Ethernet communication indicator
|
||||
eth_tx_set net_send; //Ethernet send
|
||||
eth_rx_set net_recv; //Ethernet receive
|
||||
uint32_t CheckValid; //Configuration value valid flag, fixed value @WCHNET_CFG_VALID
|
||||
};
|
||||
|
||||
struct _NET_SYS
|
||||
{
|
||||
uint8_t IPAddr[4]; //IP address
|
||||
uint8_t GWIPAddr[4]; //Gateway IP address
|
||||
uint8_t MASKAddr[4]; //subnet mask
|
||||
uint8_t MacAddr[8]; //MAC address
|
||||
uint8_t UnreachIPAddr[4]; //Unreachable IP address
|
||||
uint32_t RetranCount; //number of retries,default is 10 times
|
||||
uint32_t RetranPeriod; //Retry period, unit MS, default 500MS
|
||||
uint32_t PHYStat; //PHY state code
|
||||
uint32_t NetStat; //The status of the Ethernet, including whether it is open, etc.
|
||||
uint32_t MackFilt; //MAC filtering, the default is to receive broadcasts, receive local MAC
|
||||
uint32_t GlobIntStatus; //global interrupt
|
||||
uint32_t UnreachCode; //unreachable code
|
||||
uint32_t UnreachProto; //unreachable protocol
|
||||
uint32_t UnreachPort; //unreachable port
|
||||
uint32_t SendFlag;
|
||||
uint32_t Flags;
|
||||
};
|
||||
|
||||
/* KEEP LIVE configuration structure */
|
||||
struct _KEEP_CFG
|
||||
{
|
||||
uint32_t KLIdle; //KEEPLIVE idle time, in ms
|
||||
uint32_t KLIntvl; //KEEPLIVE period, in ms
|
||||
uint32_t KLCount; //KEEPLIVE times
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Library initialization .
|
||||
*
|
||||
* @param ip - IP address pointer
|
||||
* @param gwip - Gateway address pointer
|
||||
* @param mask - Subnet mask pointer
|
||||
* @param macaddr - MAC address pointer
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_Init(const uint8_t *ip, const uint8_t *gwip, const uint8_t *mask, const uint8_t *macaddr);
|
||||
|
||||
/**
|
||||
* @brief get library version
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return library version
|
||||
*/
|
||||
uint8_t WCHNET_GetVer(void);
|
||||
|
||||
/**
|
||||
* @brief Get MAC address.
|
||||
*
|
||||
* @param(in) macaddr - MAC address
|
||||
*
|
||||
* @param(out) MAC address
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_GetMacAddr(uint8_t *macaddr);
|
||||
|
||||
/**
|
||||
* @brief Library parameter configuration.
|
||||
*
|
||||
* @param cfg - Configuration parameter @_WCH_CFG
|
||||
*
|
||||
* @return Library configuration initialization state @CFG_INIT_STAT
|
||||
*/
|
||||
uint8_t WCHNET_ConfigLIB(struct _WCH_CFG *cfg);
|
||||
|
||||
/**
|
||||
* @brief Handle periodic tasks in the protocol stack
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_PeriodicHandle(void);
|
||||
|
||||
/**
|
||||
* @brief Ethernet data input. Always called in the main program,
|
||||
* or called after the reception interrupt is detected.
|
||||
*
|
||||
* @param
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_NetInput( void );
|
||||
|
||||
/**
|
||||
* @brief Ethernet interrupt service function. Called after
|
||||
* Ethernet interrupt is generated.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ETHIsr(void);
|
||||
|
||||
/**
|
||||
* @brief Get PHY status
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return PHY status @PHY_STAT
|
||||
*/
|
||||
uint8_t WCHNET_GetPHYStatus(void);
|
||||
|
||||
/**
|
||||
* @brief Query global interrupt status.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return GLOB_INT
|
||||
*/
|
||||
uint8_t WCHNET_QueryGlobalInt(void);
|
||||
|
||||
/**
|
||||
* @brief Read global interrupt and clear it.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return GLOB_INT
|
||||
*/
|
||||
uint8_t WCHNET_GetGlobalInt(void);
|
||||
|
||||
/**
|
||||
* @brief create socket
|
||||
*
|
||||
* @param(in) *socketid - socket variable pointer
|
||||
* @param socinf - Configuration parameters for creating sockets @SOCK_INF
|
||||
*
|
||||
* @param(out) *socketid - socket value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketCreat( uint8_t *socketid, SOCK_INF *socinf);
|
||||
|
||||
/**
|
||||
* @brief Socket sends data.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - the first address of send buffer
|
||||
* @param(in) *len - pointer to the length of the data expected to be sent
|
||||
*
|
||||
* @param(out) *len - pointer to the length of the data sent actually
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketSend( uint8_t socketid, uint8_t *buf, uint32_t *len);
|
||||
|
||||
/**
|
||||
* @brief Socket receives data.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - the first address of receive buffer
|
||||
* @param(in) *len - pointer to the length of the data expected to be read
|
||||
*
|
||||
* @param(out) *buf - the first address of data buffer
|
||||
* @param(out) *len - pointer to the length of the data read actually
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketRecv( uint8_t socketid, uint8_t *buf, uint32_t *len);
|
||||
|
||||
/**
|
||||
* @brief Get socket interrupt, and clear socket interrupt.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return Sn_INT
|
||||
*/
|
||||
uint8_t WCHNET_GetSocketInt( uint8_t socketid );
|
||||
|
||||
/**
|
||||
* @brief Get the length of the data received by socket.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param(in) *bufaddr - the first address of receive buffer
|
||||
*
|
||||
* @param(out) *bufaddr - the first address of data buffer
|
||||
*
|
||||
* @return the length of the data
|
||||
*/
|
||||
uint32_t WCHNET_SocketRecvLen( uint8_t socketid, uint32_t *bufaddr);
|
||||
|
||||
/**
|
||||
* @brief TCP connect. Used in TCP Client mode.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketConnect( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief TCP listen. Used in TCP SERVER mode.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketListen( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief Close socket.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param mode - the way of disconnection.Used in TCP connection.
|
||||
* @TCP disconnect related codes
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketClose( uint8_t socketid, uint8_t mode );
|
||||
|
||||
/**
|
||||
* @brief Modify socket receive buffer.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param bufaddr - Address of the receive buffer
|
||||
* @param bufsize - Size of the receive buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ModifyRecvBuf( uint8_t socketid, uint32_t bufaddr, uint32_t bufsize);
|
||||
|
||||
/**
|
||||
* @brief UDP send, specify the target IP and target port
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - Address of the sent data
|
||||
* @param(in) *slen - Address of the sent length
|
||||
* @param *sip - destination IP address
|
||||
* @param port - destination port
|
||||
*
|
||||
* @param(out) *slen - actual length sent
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketUdpSendTo( uint8_t socketid, uint8_t *buf, uint32_t *slen, uint8_t *sip, uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Convert ASCII address to network address.
|
||||
*
|
||||
* @param *cp - ASCII address to be converted, such as "192.168.1.2"
|
||||
* @param(in) *addr - First address of the memory stored in the converted network address
|
||||
* @param(out) *addr - Converted network address, such as 0xC0A80102
|
||||
* @return 0 - Success. Others - Failure.
|
||||
*/
|
||||
uint8_t WCHNET_Aton(const char *cp, uint8_t *addr);
|
||||
|
||||
/**
|
||||
* @brief Convert network address to ASCII address.
|
||||
*
|
||||
* @param *ipaddr - socket id value
|
||||
*
|
||||
* @return Converted ASCII address
|
||||
*/
|
||||
uint8_t *WCHNET_Ntoa( uint8_t *ipaddr);
|
||||
|
||||
/**
|
||||
* @brief Set socket TTL.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param ttl - TTL value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SetSocketTTL( uint8_t socketid, uint8_t ttl);
|
||||
|
||||
/**
|
||||
* @brief Start TCP retry sending immediately.
|
||||
*
|
||||
* @param socketid - TTL value
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_RetrySendUnack( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief Query the packets that are not sent successfully.
|
||||
*
|
||||
* @param socketid - TTL value
|
||||
* @param(in) *addrlist - pointer to the address of the address list
|
||||
* @param lislen - Length of the list
|
||||
*
|
||||
* @param(out) *addrlist - Address list of the data packets that are not sent successfully
|
||||
*
|
||||
* @return Number of unsent and unacknowledged segments
|
||||
*/
|
||||
uint8_t WCHNET_QueryUnack( uint8_t socketid, uint32_t *addrlist, uint16_t lislen );
|
||||
|
||||
/**
|
||||
* @brief Start DHCP.
|
||||
*
|
||||
* @param dhcp - Application layer callback function
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_DHCPStart( dhcp_callback dhcp );
|
||||
|
||||
/**
|
||||
* @brief Stop DHCP.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_DHCPStop( void );
|
||||
|
||||
/**
|
||||
* @brief Configure DHCP host name.
|
||||
*
|
||||
* @param *name - First address of DHCP host name
|
||||
*
|
||||
* @return 0 - Success. Others - Failure.
|
||||
*/
|
||||
uint8_t WCHNET_DHCPSetHostname(char *name);
|
||||
|
||||
/**
|
||||
* @brief Initialize the resolver: set up the UDP pcb and configure the default server
|
||||
*
|
||||
* @param *dnsip - the IP address of dns server
|
||||
* @param port - the port number of dns server
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_InitDNS( uint8_t *dnsip, uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Stop DNS.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_DNSStop(void);
|
||||
|
||||
/**
|
||||
* Resolve a hostname (string) into an IP address.
|
||||
*
|
||||
* @param hostname - the hostname that is to be queried
|
||||
* @param addr - pointer to a struct ip_addr where to store the address if it is already
|
||||
* cached in the dns_table (only valid if ERR_OK is returned!)
|
||||
* @param found - a callback function to be called on success, failure or timeout (only if
|
||||
* ERR_INPROGRESS is returned!)
|
||||
* @param arg - argument to pass to the callback function
|
||||
*
|
||||
* @return @ERR_T
|
||||
* WCHNET_ERR_SUCCESS if hostname is a valid IP address string or the host name is already in the local names table.
|
||||
* ERR_INPROGRESS enqueue a request to be sent to the DNS server for resolution if no errors are present.
|
||||
*/
|
||||
uint8_t WCHNET_HostNameGetIp( const char *hostname, uint8_t *addr, dns_callback found, void *arg );
|
||||
|
||||
/**
|
||||
* @brief Configure KEEP LIVE parameter.
|
||||
*
|
||||
* @param *cfg - KEEPLIVE configuration parameter
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ConfigKeepLive( struct _KEEP_CFG *cfg );
|
||||
|
||||
/**
|
||||
* @brief Configure socket KEEP LIVE enable.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param enable - 1: Enabled. 0: Disabled.
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketSetKeepLive( uint8_t socketid, uint8_t enable );
|
||||
|
||||
/**
|
||||
* @brief Configure PHY state
|
||||
*
|
||||
* @param phy_stat - PHY state
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_PhyStatus( uint32_t phy_stat );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user