Fix the command "test_tcp_client" bug in ch32v208rbt6

Now the command "test_tcp_client" can be used normally.
1. Connect the board to PC with netline.
2. The board's ip address is set as 192.168.1.10, and the PC ip address is set as 192.168.1.100.
3. Open the TcpIpDebug tool in PC, and open the server mode.
4. Input the command "test_tcp_client" to connect TCP server.
5. input some contents in TcpIpDebug and send them to TCP client, and they will be sent to PC as well.
This commit is contained in:
huoyujia081 2024-06-12 15:40:10 +08:00
parent 816b800643
commit 700ce53b50
5 changed files with 37 additions and 11 deletions

View File

@ -24,6 +24,7 @@ please refer to the "CH32V30x Evaluation Board Manual" under the CH32V307EVT\EVT
#include "eth_driver.h"
#include "string.h"
#include "xs_base.h"
#include "ping.h"
extern uint32_t SystemCoreClock;
#define KEEPALIVE_ENABLE 1 // Enable keep alive function
@ -32,6 +33,7 @@ uint8_t MACAddr[6]; // MAC address
uint8_t IPAddr[4] = { 192, 168, 1, 10 }; // IP address
uint8_t GWIPAddr[4] = { 192, 168, 1, 1 }; // Gateway IP address
uint8_t IPMask[4] = { 255, 255, 255, 0 }; // subnet mask
uint8_t DESIP[4] = { 192, 168, 1, 100 }; // destination IP address
uint8_t MyBuf[RECE_BUF_LEN];
uint8_t socket[WCHNET_MAX_SOCKET_NUM]; // Save the currently connected socket
@ -144,13 +146,25 @@ void WCHNET_DataLoopback(uint8_t id)
*
* @return 0 or TIME_OUT
*/
int WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat)
int WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat, int commandType)
{
uint8_t i;
u32 len;
if (intstat & SINT_STAT_RECV) // receive data
{
WCHNET_DataLoopback(socketid); // Data loopback
switch (commandType) {
case 0:
WCHNET_DataLoopback(socketid); // Data loopback
break;
case 1:
len = WCHNET_SocketRecvLen(socketid,NULL); //get socket buffer data length
WCHNET_SocketRecv(socketid,MyBuf,&len); //Read the data of the receive buffer into MyBuf
WCHNET_ICMPRecvData( len,MyBuf );
}
// WCHNET_DataLoopback(socketid); // Data loopback
// len = WCHNET_SocketRecvLen(socketid,NULL); //get socket buffer data length
// WCHNET_SocketRecv(socketid,MyBuf,&len); //Read the data of the receive buffer into MyBuf
// WCHNET_ICMPRecvData( len,MyBuf );
}
if (intstat & SINT_STAT_CONNECT) // connect successfully
{
@ -175,6 +189,8 @@ int WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat)
break;
}
}
extern uint8_t connectState;
connectState = 0;
KPrintf("TCP Disconnect\r\n");
}
if (intstat & SINT_STAT_TIM_OUT) // timeout disconnect
@ -198,7 +214,7 @@ int WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat)
*
* @return 0 or SockInt
*/
int WCHNET_HandleGlobalInt(void)
int WCHNET_HandleGlobalInt(int commandType)
{
uint8_t intstat;
uint16_t i;
@ -216,14 +232,16 @@ int WCHNET_HandleGlobalInt(void)
if (intstat & GINT_STAT_PHY_CHANGE) // PHY status change
{
i = WCHNET_GetPHYStatus();
if (i & PHY_Linked_Status)
if (i & PHY_Linked_Status) {
ICMPSuc = ICMP_SOKE_CON;
KPrintf("PHY Link Success\r\n");
}
}
if (intstat & GINT_STAT_SOCKET) { // socket related interrupt
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
socketint = WCHNET_GetSocketInt(i);
if (socketint) {
return WCHNET_HandleSockInt(i, socketint);
return WCHNET_HandleSockInt(i, socketint, commandType);
}
}
}

View File

@ -11,6 +11,7 @@
*******************************************************************************/
#include "string.h"
#include "eth_driver.h"
#include "ch32v20x.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 */

View File

@ -391,7 +391,7 @@ int ping(int argc, char *argv[]) {
/*Query the Ethernet global interrupt,
* if there is an interrupt, call the global interrupt handler*/
if (WCHNET_QueryGlobalInt()) {
WCHNET_HandleGlobalInt();
WCHNET_HandleGlobalInt(1);
}
WCHNET_PINGCmd();
if (ICMPSuc == ICMP_KEEP_NO) {

View File

@ -4,10 +4,12 @@
#include "wchnet.h"
#include "xs_base.h"
uint16_t desport = 1000; // destination port
uint16_t srcport = 1000; // source port
extern uint8_t DESIP[4];
uint8_t SocketId;
uint8_t connectState = 1;
/*********************************************************************
* @fn TCP client
@ -18,8 +20,8 @@ uint8_t SocketId;
int Tcp_Client(void)
{
uint8_t i;
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++)
WCHNET_CreateTcpSocket(DESIP, srcport, desport, &SocketId); // Create TCP Socket
// for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++)
WCHNET_CreateTcpSocket(DESIP, srcport, desport, &SocketId); // Create TCP Socket
while (1) {
/*Ethernet library main task function,
@ -28,16 +30,21 @@ int Tcp_Client(void)
/*Query the Ethernet global interrupt,
* if there is an interrupt, call the global interrupt handler*/
if (WCHNET_QueryGlobalInt()) {
if (WCHNET_HandleGlobalInt() == TIME_OUT) {
if (WCHNET_HandleGlobalInt(0) == TIME_OUT) {
WCHNET_CreateTcpSocket(DESIP, srcport, desport, &SocketId);
}
}
/* only one connection */
if (connectState == 0) {
break;
}
}
}
int test_tcp_client(int argc, char* argv[])
{
KPrintf("TCPClient Test\r\n");
connectState = 1;
Tcp_Client();
return 0;
}

View File

@ -31,7 +31,7 @@ uint8_t InitHwEth();
void WCHNET_CreateTcpSocket(uint8_t* DESIP, uint16_t srcport, uint16_t desport, uint8_t* SocketId);
void WCHNET_CreateTcpSocketListen(uint16_t srcport, uint8_t* SocketId);
int WCHNET_HandleGlobalInt(void);
int WCHNET_HandleGlobalInt(int commandType);
#define TIME_OUT -1