외부 노이즈에 의해 소켓생성 관련 오류 발생

ioLibrary 4.0 - 아래 2함수를 각각 단독으로 사용하여 연속으로 data 200개를 송신하는데 성공하였습니다.
loopback_tcps()
loopback_udps()

각각 1주기 걸리는 시간이 300us정도되는것 같습니다.
1sec / 300us = 3,333 → 3,333 * 200byte = 666,666 → 666,666 * 8 = 5,333,333 → SPI CLOCK 20MHZ - 5.3Mbps
계산이 맞는지 모르겠네요…이게 중요한게 아니고
연속으로 잘 송신하다가 보드의 40cm 옆에서 고주파 인두기를 껐다가 켰다가를 반복하면 아래의 오류가 발생합니다.

  1. socket() - SOCKERR_SOCKINIT 또는 SOCKERR_SOCKMODE 값이 리턴됨
  2. loopback_tcps() → 송신 잘하다가 SOCK_LISTEN 으로 바뀜
  3. loopback_udps() → sendto error : -5, -13, -4, -7 발생
  4. sendto() → tmp = getSn_IR(sn); 오류발생하여 while(1) 을 빠져 나오지 못함
  5. socket() → while (getSn_SR(sn) == SOCK_CLOSED) 를 빠져나오지 못함.

1~3번은 초기화를 다시 실행하면 송신이 재개됩니다.
- loopback_tcps() 실행때는 재 접속해야 함
4~5번은 어떻게 처리해야 할지 모르겠습니다.

위와 같은 오류가 자주 발생하는게 맞나요?
while문은 빠져 나오려면 어떻게 해야 하나요?
socket.c 파일내의 모든 while문은 동일한 문제를 안고 있는거지요?
보드를 reset 하지 않고 해결할 방법이 있으면 좋겠습니다.

아래는 loopback.c 내용입니다.

#include <stdio.h>
#include “main.h”
#include “loopback.h”
#include “socket.h”
#include “wizchip_conf.h”
#include “stm32f4xx_hal.h”

extern volatile uint16_t SockListenTimer;

#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK

uint16_t txnum = 0;

int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
{
int32_t ret;
uint16_t size = 0, sentsize=0;
uint8_t read_status;

#ifdef LOOPBACK_DEBUG
uint8_t destip[4];
uint16_t destport;
#endif

read_status = getSn_SR(sn);

switch(read_status)
{
case SOCK_ESTABLISHED :
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
SockListenTimer = 0;
if(getSn_IR(sn) & Sn_IR_CON)
{
#ifdef LOOPBACK_DEBUG
getSn_DIPR(sn, destip);
destport = getSn_DPORT(sn);

		printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);

#endif
setSn_IR(sn,Sn_IR_CON);
}

     //------------연속 송신-----------------------
     sentsize = 0;
     size = 150;
     while(size != sentsize)
     {
    	 ++txnum;
    	 buf[0] = 0xff;
    	 buf[1] = 0xff;
    	 buf[2] = txnum>>24;
    	 buf[3] = txnum>>16;
    	 buf[4] = txnum>>8;
    	 buf[5] = txnum;

#ifdef LOOPBACK_DEBUG
printf(“send : %X\r\n”,txnum);
//printf(“ret :%d\r\n”, sent_data_len);
#endif
HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
ret = send(sn, buf+sentsize, size-sentsize);
HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
if(ret < 0)
{
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
close(sn);
#ifdef LOOPBACK_DEBUG
printf(“Socket Close !!!\r\n”);
#endif
return ret;
}
sentsize += ret; // Don’t care SOCKERR_BUSY, because it is zero.
}

     //------------loopback----------------------

// if((size = getSn_RX_RSR(sn)) > 0) // Don’t need to check SOCKERR_BUSY because it doesn’t not occur.
// {
// if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
// ret = recv(sn, buf, size);
//
// if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
// size = (uint16_t) ret;
// sentsize = 0;
//
// while(size != sentsize)
// {
// ++txnum;
// buf[0] = 0xff;
// buf[1] = 0xff;
// buf[2] = txnum>>24;
// buf[3] = txnum>>16;
// buf[4] = txnum>>8;
// buf[5] = txnum;
// size = 200;
//
//#ifdef LOOPBACK_DEBUG
// printf(“send : %X\r\n”,txnum);
// //printf(“ret :%d\r\n”, sent_data_len);
//#endif
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
// ret = send(sn, buf+sentsize, size-sentsize);
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
// if(ret < 0)
// {
//// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
// close(sn);
//#ifdef LOOPBACK_DEBUG
// printf(“Socket Close !!!\r\n”);
//#endif
// return ret;
// }
// sentsize += ret; // Don’t care SOCKERR_BUSY, because it is zero.
// }
// }
break;
case SOCK_CLOSE_WAIT :
SockListenTimer = 0;
#ifdef LOOPBACK_DEBUG
printf(“%d:CloseWait\r\n”,sn);
#endif
if((ret = disconnect(sn)) != SOCK_OK) return ret;
#ifdef LOOPBACK_DEBUG
printf(“%d:Socket Closed\r\n”, sn);
#endif
break;
case SOCK_INIT :
SockListenTimer = 0;
#ifdef LOOPBACK_DEBUG
printf(“%d:Listen, TCP server loopback, port [%d]\r\n”, sn, port);
#endif
if( (ret = listen(sn)) != SOCK_OK) return ret;
break;
case SOCK_CLOSED:
SockListenTimer = 0;
#ifdef LOOPBACK_DEBUG
printf(“%d:TCP server loopback start\r\n”,sn);
#endif
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn)
{
#ifdef LOOPBACK_DEBUG
printf(“%d:socker error SOCKERR_SOCKINIT\r\n”,ret);
#endif
WIZ_ChipInit();
WIZ_NetworkInit();
return ret;
}
#ifdef LOOPBACK_DEBUG
printf(“%d:Socket opened\r\n”,sn);
#endif
break;
case SOCK_LISTEN :
#ifdef LOOPBACK_DEBUG
printf(“%d:Socket Listen\r\n”,sn);
#endif
if(SockListenTimer > 5000)
{
SockListenTimer = 0;
WIZ_ChipInit();
WIZ_NetworkInit();
}
break;
default:
SockListenTimer = 0;
break;
}
return 1;
}

int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{
int32_t ret; // return value for SOCK_ERRORs
uint16_t size = 0, sentsize=0;

// Destination (TCP Server) IP info (will be connected)
// >> loopback_tcpc() function parameter
// >> Ex)
// uint8_t destip[4] = {192, 168, 0, 214};
// uint16_t destport = 5000;

// Port number for TCP client (will be increased)
static uint16_t any_port = 50000;

// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The ‘Sn_SR’ controlled by Sn_CR command or Packet send/recv status)
switch(getSn_SR(sn))
{
case SOCK_ESTABLISHED :
if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
{
#ifdef LOOPBACK_DEBUG
printf(“%d:Connected to - %d.%d.%d.%d : %d\r\n”,sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to ‘1’
}

     //////////////////////////////////////////////////////////////////////////////////////////////
     // Data Transaction Parts; Handle the [data receive and send] process
     //////////////////////////////////////////////////////////////////////////////////////////////
	 if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
     {
		if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
		ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)

		if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
		size = (uint16_t) ret;
		sentsize = 0;

		// Data sentsize control
		while(size != sentsize)
		{
			ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
			if(ret < 0) // Send Error occurred (sent data length < 0)
			{
				close(sn); // socket close
				return ret;
			}
			sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
		}
     }
	 //////////////////////////////////////////////////////////////////////////////////////////////
     break;

  case SOCK_CLOSE_WAIT :

#ifdef LOOPBACK_DEBUG
//printf(“%d:CloseWait\r\n”,sn);
#endif
if((ret=disconnect(sn)) != SOCK_OK) return ret;
#ifdef LOOPBACK_DEBUG
printf(“%d:Socket Closed\r\n”, sn);
#endif
break;

  case SOCK_INIT :

#ifdef LOOPBACK_DEBUG
printf(“%d:Try to connect to the %d.%d.%d.%d : %d\r\n”, sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
break;

  case SOCK_CLOSED:
	  close(sn);
	  if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
     if(any_port == 0xffff) any_port = 50000;
     return ret; // TCP socket open with 'any_port' port number
    } 

#ifdef LOOPBACK_DEBUG
//printf(“%d:TCP client loopback start\r\n”,sn);
//printf(“%d:Socket opened\r\n”,sn);
#endif
break;
default:
break;
}
return 1;
}

int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
{
int32_t ret;
uint16_t size, sentsize;
// uint8_t destip[4]; //------------loopback
// uint16_t destport;
uint8_t destip[4] = {192,168,0,10};//------------연속 송신
uint16_t destport = 5000;
uint8_t read_status;

read_status = getSn_SR(sn);

switch(read_status)
{
case SOCK_UDP :
SockListenTimer = 0;

      //------------연속 송신-----------------------
       size = 200;
      sentsize = 0;
      while(sentsize != size)
      {
      	++txnum;
      	buf[0] = 0xff;
      	buf[1] = 0xff;
      	buf[2] = txnum>>24;
      	buf[3] = txnum>>16;
      	buf[4] = txnum>>8;
      	buf[5] = txnum;
      	size = 200;

      	HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
      	ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
      	HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);

#ifdef LOOPBACK_DEBUG
printf(“send : %X\r\n”,txnum);
//printf(“ret :%d\r\n”, sent_data_len);
#endif
if(ret < 0)
{
#ifdef LOOPBACK_DEBUG
printf(“%d: sendto error. %ld\r\n”,sn,ret);
#endif
WIZ_ChipInit();
WIZ_NetworkInit();
return ret;
}
sentsize += ret; // Don’t care SOCKERR_BUSY, because it is zero.
}

      //------------loopback----------------------

// if((size = getSn_RX_RSR(sn)) > 0)
// {
// if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
// ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
//#ifdef LOOPBACK_DEBUG
//// getSn_DIPR(sn, destip);
//// destport = getSn_DPORT(sn);
//// printf(“%d:Try to connect to the %d.%d.%d.%d : %d\r\n”, sn, destip[0], destip[1], destip[2], destip[3], destport);
//#endif
// if(ret <= 0)
// {
//#ifdef LOOPBACK_DEBUG
// printf(“%d: recvfrom error. %ld\r\n”,sn,ret);
//#endif
// return ret;
// }
// size = (uint16_t) ret;
// sentsize = 0;
// while(sentsize != size)
// {
// ++txnum;
// buf[0] = 0xff;
// buf[1] = 0xff;
// buf[2] = txnum>>24;
// buf[3] = txnum>>16;
// buf[4] = txnum>>8;
// buf[5] = txnum;
// size = 200;
//
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
// ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
// HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
// if(ret < 0)
// {
//#ifdef LOOPBACK_DEBUG
// printf(“%d: sendto error. %ld\r\n”,sn,ret);
//#endif
// return ret;
// }
// sentsize += ret; // Don’t care SOCKERR_BUSY, because it is zero.
// }
// }
break;
case SOCK_CLOSED:
SockListenTimer = 0;
#ifdef LOOPBACK_DEBUG
//printf(“%d:UDP loopback start\r\n”,sn);
#endif
if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
{
#ifdef LOOPBACK_DEBUG
printf(“%d:socker error SOCKERR_SOCKINIT\r\n”,ret);
#endif
WIZ_ChipInit();
WIZ_NetworkInit();
return ret;
}
#ifdef LOOPBACK_DEBUG
printf(“%d:Opened, UDP loopback, port [%d]\r\n”, sn, port);
#endif
break;
default :
SockListenTimer = 0;
break;
}
return 1;
}

#endif