Part Number: AM243x-lp
Tool/software:
When using AM243x-lp as a client to do TCP/IP communication, it was found that the abnormal interruption(unplug the network cable or power off the server device) would cause the usage rate of MCU's CPU to rise sharply, from the original 3%+ to 20%+. The code is modified based on ind_comms_sdk_am243x_09_02_00_08\mcu_plus_sdk\examples\networking\lwip\enet_cpsw_socket, which still uses the socket interface of LwIP. The server side runs on another development board. If the socket is opened and closed normally, the usage rate of MCU's CPU will not increase. If unplug the network cable or power off the server device, wait more than 8 seconds and then restore, the usage rate of MCU'CPU will rise from 3%+ to 20%+ after reconnecting. I also developed a linux version client and does the same thing, the CPU usage rate of running linux machine does not increase, so I think it is not a server side issue, it is probably a network protocol stack or network driver issue. Below is my test code on AM243x-lp.
static void AppSocket_simpleClient(void* pArg)
{
static const uint8_t LOGIN_MSG[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x01, 0x10, 0x14, 0x01, 0x00, 0x04, 0x08, // login
0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38}; // write 8888 8888, from address 5121
static const uint8_t READ_COMM_TIMEOUT_MSG[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x14, 0x4e, 0x00, 0x01}; // read 5195, Com time out for M100
static const uint8_t READ_UP_TIME_MSG[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x15, 0x87, 0x00, 0x02}; // read 5511 up time for M100
struct sockaddr* pAddr = pArg;
int32_t sock = -1, ret = 0;
struct timeval opt = {0}, tv = {0};
fd_set readset = {0}, writeset = {0}, errset = {0};
for (uint32_t i = 0; i < APP_SOCKET_NUM_ITERATIONS; i++)
{
//EnetAppUtils_print("<<< Iteration %d >>>> \r\n", i+1);
EnetAppUtils_print(" Connecting to: %s:%d \r\n", gHostServerIp4, SOCK_HOST_SERVER_PORT);
if (netstate == 0)
{
ClockP_usleep(4000000);
continue;
}
/* create the socket */
sock = lwip_socket(pAddr->sa_family, SOCK_STREAM, 0);
if (sock < 0)
{
EnetAppUtils_print("ERR: unable to open socket\r\n");
continue;
}
/* connect */
ret = lwip_connect(sock, pAddr, pAddr->sa_len);
if (ret != 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: unable to connect\r\n");
continue;
}
EnetAppUtils_print("Connected to host\r\n");
/* set recv timeout (100 ms) */
opt.tv_sec = 0;
opt.tv_usec = 100 * 1000;
ret = lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(opt));
if (ret != 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: set sockopt failed\r\n");
continue;
}
/*login rf modulator*/
ret = lwip_write(sock, LOGIN_MSG, sizeof(LOGIN_MSG));
if (ret != sizeof(LOGIN_MSG))
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket write failed for login rf modulator.\r\n");
continue;
}
ret = lwip_read(sock, gRxDataBuff, APP_SOCKET_MAX_RX_DATA_LEN);
if (ret <= 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket read failed for login rf modulator.\r\n");
continue;
}
for (uint32_t i=0; i<160; i++)
{
EnetAppUtils_print("Cycle number %d\r\n", i);
ret = lwip_write(sock, READ_COMM_TIMEOUT_MSG, sizeof(READ_COMM_TIMEOUT_MSG));
if (ret != sizeof(READ_COMM_TIMEOUT_MSG))
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: READ_COMM_TIMEOUT_MSG failed\r\n");
continue;
}
ret = lwip_read(sock, gRxDataBuff, APP_SOCKET_MAX_RX_DATA_LEN);
if (ret <= 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: READ_COMM_TIMEOUT_MSG failed\r\n");
continue;
}
ret = lwip_write(sock, READ_UP_TIME_MSG, sizeof(READ_UP_TIME_MSG));
if (ret != sizeof(READ_UP_TIME_MSG))
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: READ_UP_TIME_MSG failed\r\n");
continue;
}
ret = lwip_read(sock, gRxDataBuff, APP_SOCKET_MAX_RX_DATA_LEN);
if (ret <= 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: READ_UP_TIME_MSG failed\r\n");
continue;
}
ClockP_usleep(100000);
}
#if 0
/* Send data to Host */
ret = lwip_write(sock, APP_CLIENT_TX_MSG1, sizeof(APP_CLIENT_TX_MSG1));
if (ret != sizeof(APP_CLIENT_TX_MSG1))
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket write failed\r\n");
continue;
}
EnetAppUtils_print("Message to host: %s\r\n", APP_CLIENT_TX_MSG1);
FD_ZERO(&readset);
FD_ZERO(&writeset);
FD_ZERO(&errset);
FD_SET(sock, &readset);
FD_SET(sock, &writeset);
FD_SET(sock, &errset);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = lwip_select(sock + 1, &readset, &writeset, &errset, &tv);
if (ret <= 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket select failed\r\n");
continue;
}
EnetAppUtils_assert(!FD_ISSET(sock, &errset));
if (FD_ISSET(sock, &readset))
{
ret = lwip_read(sock, gRxDataBuff, APP_SOCKET_MAX_RX_DATA_LEN);
if (ret <= 0)
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket read failed\r\n");
continue;
}
gRxDataBuff[ret] = '\0';
EnetAppUtils_print("Message from host: %s\r\n", gRxDataBuff);
}
if (FD_ISSET(sock, &writeset))
{
ret = lwip_write(sock, APP_CLIENT_TX_MSG2, sizeof(APP_CLIENT_TX_MSG2));
if (ret != sizeof(APP_CLIENT_TX_MSG2))
{
ret = lwip_close(sock);
EnetAppUtils_print("ERR: socket write failed!\r\n");
continue;
}
EnetAppUtils_assert(ret == sizeof(APP_CLIENT_TX_MSG2));
EnetAppUtils_print("Message to host: %s\r\n", APP_CLIENT_TX_MSG2);
}
#endif
/* close */
ret = lwip_close(sock);
EnetAppUtils_print("Closed Socket connection\r\n");
ClockP_usleep(8000000);
}
return;
}