Hi.My test environment was to connect the MCU to the computer using a network cable directly,and phy is lan8720a.
The configuration of computer:
ip: 192.168.124.4
net mask: 255.255.255.0
gateway: 192.168.124.1
The configuration of mcu:
ip: 192.168.124.121
net mask: 255.255.255.0
gateway: 192.168.124.1
Computer acts as a tcp-server,use port 9000, and mcu acts as a tcp-client. I follow the routine to establish a TCP connection between the MCU and the computer, my code as follows:
void vTCPSend(void* param)
{
Socket_t xSocket;
BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0;
TaskHandle_t xRxTask = NULL;
size_t xLenToSend;
/* Set the IP address (192.168.0.200) and port (1500) of the remote socket
to which this client socket will transmit. */
struct freertos_sockaddr xRemoteAddress;
memset( &xRemoteAddress, 0, sizeof(xRemoteAddress) );
xRemoteAddress.sin_port = FreeRTOS_htons( 9000 );
xRemoteAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr_quick( 192, 168, 124, 4 );
xRemoteAddress.sin_family = FREERTOS_AF_INET4;
struct freertos_sockaddr xLocalAddress;
memset( &xLocalAddress, 0, sizeof(xLocalAddress) );
xLocalAddress.sin_port = FreeRTOS_htons( 9999 );
xLocalAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr_quick( 192, 168, 124, 121 );
xLocalAddress.sin_family = FREERTOS_AF_INET4;
static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 20000 );
static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 20000 );
while(1)
{
/* Create a socket. */
xSocket = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_STREAM,/* FREERTOS_SOCK_STREAM for TCP. */
FREERTOS_IPPROTO_TCP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
/* Set a time out so a missing reply does not cause the task to block indefinitely. */
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
// FreeRTOS_bind(xSocket, &xLocalAddress, sizeof(xLocalAddress));
if( FreeRTOS_connect( xSocket, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 )
{
const char buf[] = "Hello World";
unsigned short xTotalLengthToSend = strlen(buf);
/* Keep sending until the entire buffer has been sent. */
while( xAlreadyTransmitted < xTotalLengthToSend )
{
/* How many bytes are left to send? */
xLenToSend = xTotalLengthToSend - xAlreadyTransmitted;
xBytesSent = FreeRTOS_send( /* The socket being sent to. */
xSocket,
/* The data being sent. */
&( buf[ xAlreadyTransmitted ] ),
/* The remaining length of data to send. */
xLenToSend,
/* ulFlags. */
0 );
if( xBytesSent >= 0 )
{
/* Data was sent successfully. */
xAlreadyTransmitted += xBytesSent;
}
else
{
/* Error - break out of the loop for graceful socket close. */
break;
}
}
/* Initiate graceful shutdown. */
FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
/* Wait for the socket to disconnect gracefully (indicated by FreeRTOS\_recv()
returning a -pdFREERTOS_ERRNO_EINVAL error) before closing the socket. */
while( FreeRTOS_recv( xSocket, (char*)buf, xTotalLengthToSend, 0 ) >= 0 )
{
/* Wait for shutdown to complete. If a receive block time is used then
this delay will not be necessary as FreeRTOS_recv() will place the RTOS task
into the Blocked state anyway. */
vTaskDelay( pdTICKS_TO_MS( 250 ) );
/* Note - real applications should implement a timeout here, not just
loop forever. */
}
}
/* The socket has shut down and is safe to close. */
FreeRTOS_closesocket( xSocket );
/* Pause for a short while to ensure the network is not too congested. */
#define echoLOOP_DELAY ( ( TickType_t ) 150 / portTICK_PERIOD_MS )
vTaskDelay( echoLOOP_DELAY );
}
}
but log as follows:
FreeRTOS_IPInit
FreeRTOS_AddEndPoint: MAC: 00-00 IPv4: c0a87c79ip
prvIPTask started
PHY ID 7C0F0
xPhyReset: phyBMCR_RESET [0] ready
+TCP: advertise: 01E1 config 3100
prvEthernetUpdateConfig: LS mask 00 Force 1
Autonego ready: 00000004: full duplex 100 mbit high status
Link Status is high
eNetworkUp
IP Address: 192.168.124.121
Subnet Mask: 255.255.255.0
Gateway Address: 192.168.124.1
DNS Server Address: 0.0.0.0
FreeRTOS_connect: 1025 to c0a87c04ip:9000
Socket 1025 -> [192.168.124.4]:9000 State eCLOSED->eCONNECT_SYN
ARP c0a87c04ip miss using c0a87c04ip
ARP for c0a87c04ip (using c0a87c04ip): rc=0 00-00-00-00-00-00
Connect[c0a87c04ip:9000]: next timeout 1: 2000 ms
pxEasyFit: ARP c0a87c79ip -> c0a87c01ip
Heap: current 216 lowest 216
Network buffers: 60 lowest 58
Heap: current 1768 lowest 128
ARP c0a87c04ip miss using c0a87c04ip
ARP for c0a87c04ip (using c0a87c04ip): rc=0 00-00-00-00-00-00
Connect[c0a87c04ip:9000]: next timeout 2: 2000 ms
ARP c0a87c04ip miss using c0a87c04ip
ARP for c0a87c04ip (using c0a87c04ip): rc=0 00-00-00-00-00-00
Connect[c0a87c04ip:9000]: next timeout 3: 2000 ms
I don’t understand what’s going on. Can you help me analyze it?