169 lines
4.7 KiB
C
169 lines
4.7 KiB
C
#include "ch32v30x.h"
|
||
#include "string.h"
|
||
#include "eth_driver.h"
|
||
#include "dvp.h"
|
||
|
||
#define KEEPALIVE_ENABLE 1
|
||
|
||
u8 MACAddr[6];
|
||
u8 IPAddr[4] = {192, 168, 1, 10};
|
||
u8 GWIPAddr[4] = {192, 168, 1, 1};
|
||
u8 IPMask[4] = {255, 255, 255, 0};
|
||
u8 DESIP[4] = {192, 168, 1, 50};
|
||
u16 desport = 5512;
|
||
u16 srcport = 5511;
|
||
|
||
u8 SocketId;
|
||
u8 socket[WCHNET_MAX_SOCKET_NUM];
|
||
u8 SocketRecvBuf[WCHNET_MAX_SOCKET_NUM][RECE_BUF_LEN];
|
||
|
||
void mStopIfError(u8 iError)
|
||
{
|
||
if (iError == WCHNET_ERR_SUCCESS) return;
|
||
printf("Error: %02X\r\n", (u16)iError);
|
||
}
|
||
|
||
void TIM2_Init(void)
|
||
{
|
||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||
TIM_TimeBaseStructure.TIM_Period = WCHNETTIMERPERIOD * 1000 - 1;
|
||
TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock / 1000000 - 1;
|
||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||
TIM_Cmd(TIM2, ENABLE);
|
||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||
NVIC_EnableIRQ(TIM2_IRQn);
|
||
}
|
||
|
||
void WCHNET_CreateTcpSocket(void)
|
||
{
|
||
u8 i;
|
||
SOCK_INF TmpSocketInf;
|
||
memset((void *)&TmpSocketInf, 0, sizeof(SOCK_INF));
|
||
memcpy((void *)TmpSocketInf.IPAddr, DESIP, 4);
|
||
TmpSocketInf.DesPort = desport;
|
||
TmpSocketInf.SourPort = srcport++;
|
||
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
|
||
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
|
||
i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
|
||
printf("SocketId %d\r\n", SocketId);
|
||
mStopIfError(i);
|
||
i = WCHNET_SocketConnect(SocketId);
|
||
mStopIfError(i);
|
||
}
|
||
|
||
/* PC -> MCU 收到数据后回环,同时可在此处理PC下发的指令 */
|
||
void WCHNET_DataLoopback(u8 id)
|
||
{
|
||
u32 len;
|
||
u32 endAddr = SocketInf[id].RecvStartPoint + SocketInf[id].RecvBufLen;
|
||
|
||
if ((SocketInf[id].RecvReadPoint + SocketInf[id].RecvRemLen) > endAddr)
|
||
len = endAddr - SocketInf[id].RecvReadPoint;
|
||
else
|
||
len = SocketInf[id].RecvRemLen;
|
||
|
||
if (WCHNET_SocketSend(id, (u8 *)SocketInf[id].RecvReadPoint, &len) == WCHNET_ERR_SUCCESS)
|
||
WCHNET_SocketRecv(id, NULL, &len);
|
||
}
|
||
|
||
void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
|
||
{
|
||
u8 i;
|
||
|
||
if (intstat & SINT_STAT_RECV)
|
||
{
|
||
WCHNET_DataLoopback(socketid);
|
||
}
|
||
if (intstat & SINT_STAT_CONNECT)
|
||
{
|
||
#if KEEPALIVE_ENABLE
|
||
WCHNET_SocketSetKeepLive(socketid, ENABLE);
|
||
#endif
|
||
WCHNET_ModifyRecvBuf(socketid, (u32)SocketRecvBuf[socketid], RECE_BUF_LEN);
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
if (socket[i] == 0xff) { socket[i] = socketid; break; }
|
||
}
|
||
printf("TCP Connect Success, socket id: %d\r\n", socketid);
|
||
}
|
||
if (intstat & SINT_STAT_DISCONNECT)
|
||
{
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
if (socket[i] == socketid) { socket[i] = 0xff; break; }
|
||
}
|
||
printf("TCP Disconnect\r\n");
|
||
}
|
||
if (intstat & SINT_STAT_TIM_OUT)
|
||
{
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
if (socket[i] == socketid) { socket[i] = 0xff; break; }
|
||
}
|
||
printf("TCP Timeout\r\n");
|
||
WCHNET_CreateTcpSocket();
|
||
}
|
||
}
|
||
|
||
void WCHNET_HandleGlobalInt(void)
|
||
{
|
||
u8 intstat;
|
||
u16 i;
|
||
u8 socketint;
|
||
intstat = WCHNET_GetGlobalInt();
|
||
if (intstat & GINT_STAT_UNREACH) printf("GINT_STAT_UNREACH\r\n");
|
||
if (intstat & GINT_STAT_IP_CONFLI) printf("GINT_STAT_IP_CONFLI\r\n");
|
||
if (intstat & GINT_STAT_PHY_CHANGE) {
|
||
if (WCHNET_GetPHYStatus() & PHY_Linked_Status)
|
||
printf("PHY Link Success\r\n");
|
||
}
|
||
if (intstat & GINT_STAT_SOCKET) {
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
socketint = WCHNET_GetSocketInt(i);
|
||
if (socketint) WCHNET_HandleSockInt(i, socketint);
|
||
}
|
||
}
|
||
}
|
||
|
||
int main(void)
|
||
{
|
||
u8 i;
|
||
SystemCoreClockUpdate();
|
||
Delay_Init();
|
||
USART_Printf_Init(115200);
|
||
printf("TCPClient Test\r\nSystemClk:%d\r\n", SystemCoreClock);
|
||
printf("net version:%x\n", WCHNET_GetVer());
|
||
if (WCHNET_LIB_VER != WCHNET_GetVer()) printf("version error.\n");
|
||
|
||
WCHNET_GetMacAddr(MACAddr);
|
||
printf("mac addr:");
|
||
for (i = 0; i < 6; i++) printf("%x ", MACAddr[i]);
|
||
printf("\n");
|
||
|
||
DVP_Init();
|
||
TIM2_Init();
|
||
|
||
i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr);
|
||
mStopIfError(i);
|
||
if (i == WCHNET_ERR_SUCCESS) printf("WCHNET_LibInit Success\r\n");
|
||
|
||
#if KEEPALIVE_ENABLE
|
||
{
|
||
struct _KEEP_CFG cfg = {20000, 15000, 9};
|
||
WCHNET_ConfigKeepLive(&cfg);
|
||
}
|
||
#endif
|
||
|
||
memset(socket, 0xff, WCHNET_MAX_SOCKET_NUM);
|
||
WCHNET_CreateTcpSocket();
|
||
|
||
while (1)
|
||
{
|
||
DVP_Task();
|
||
WCHNET_MainTask();
|
||
if (WCHNET_QueryGlobalInt())
|
||
WCHNET_HandleGlobalInt();
|
||
}
|
||
}
|