본문 바로가기
DSP, MCU/STM32 (ARM Cortex-M)

STM32 ] lwIP UDP Echo Sever, 속도 테스트

by eteo 2022. 9. 25.

 

 

udp_echoserver.h
0.00MB
udp_echoserver.c
0.00MB

 

 

위 헤더파일과 소스파일을 추가하고 while 문 전에 udp_echoserver_init(); 함수를 추가한다. 그밖에는 이전과 설정이 똑같다.

 

이건 STM사 공식 LwIP_UDP_Echo_Server 예제에 포함된 파일이다.

 

/* USER CODE BEGIN Includes */
#include "udp_echoserver.h"
/* USER CODE END Includes */

//...

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_RTC_Init();
  MX_LWIP_Init();
  /* USER CODE BEGIN 2 */
  udp_echoserver_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  MX_LWIP_Process();
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

 

 

udp_echoserver.c

#define UDP_SERVER_PORT    7   /* define the UDP local connection port */
#define UDP_CLIENT_PORT    7   /* define the UDP remote connection port */

//...

/**
  * @brief  Initialize the server application.
  * @param  None
  * @retval None
  */
void udp_echoserver_init(void)
{
   struct udp_pcb *upcb;
   err_t err;
   
   /* Create a new UDP control block  */
   upcb = udp_new();
   
   if (upcb)
   {
     /* Bind the upcb to the UDP_PORT port */
     /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
      err = udp_bind(upcb, IP_ADDR_ANY, UDP_SERVER_PORT);
      
      if(err == ERR_OK)
      {
        /* Set a receive callback for the upcb */
        udp_recv(upcb, udp_echoserver_receive_callback, NULL);
      }
      else
      {
        udp_remove(upcb);
      }
   }
}

 

과정을 보면 이전 글과 거의 같다. udp_new() 로 UDP 컨트롤 블락을 생성하고 udp_bind() 함수를 통해 생성된 블락과 local IP 주소 및 Port를 바인드 한다.IP 주소 인자는 IP_ADDR_ANY 라고 되어있으니 DHCP 서버를 enable하고 동적 IP를 사용해도 된다. 

 

UDP_SERVER_PORT 와 UDP_CLIENT_PORT 포트는 7번 포트로 define 되어있다. 7번 포트는 원래 연결확인 용도로 echo protocol 용도로 많이 쓰인다.

 

 

 

콜백함수는 connect - send - disconnect - free의 과정으로 수신된 데이터를 그대로 재전송하도록만 짜있다. 

/**
  * @brief This function is called when an UDP datagrm has been received on the port UDP_PORT.
  * @param arg user supplied argument (udp_pcb.recv_arg)
  * @param pcb the udp_pcb which received data
  * @param p the packet buffer that was received
  * @param addr the remote IP address from which the packet was received
  * @param port the remote port from which the packet was received
  * @retval None
  */
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{

  /* Connect to the remote client */
  udp_connect(upcb, addr, UDP_CLIENT_PORT);
    
  /* Tell the client that we have accepted it */
  udp_send(upcb, p);

  /* free the UDP connection, so we can accept new clients */
  udp_disconnect(upcb);
	
  /* Free the p buffer */
  pbuf_free(p);
   
}

 

 

 

에코백이 잘 되는 것을 확인할 수 있다.

 

 

 

 

아래는 WIZnet 사의 TCP, UDP 속도 테스트 프로그램이다.

AX1_AX2.zip
1.85MB

 

먼저 CPUTICK 을 눌러준다.

그럼 조금 있다 TICK 값이 뜨고 확인을 눌러주면 된다.

먼저 UDP Port를 Open 하고

 

다음 루프백 테스트를 해본다. UR을 누르면 테스트 카운트를 지정할 수 있다.

 

 

테스트에 사용할 파일을 선택한다.

 

측정결과 15 Mbps 정도 나온다.