1039 views|3 replies

6827

Posts

11

Resources
The OP
 

[DigiKey "Smart Manufacturing, Non-stop Happiness" Creative Competition] Porting FreeRTOS + LVGL + Lwip to implement TCPclient [Copy link]

 

【Foreword】

Previously, I used rtthread studio+lvgl to implement a basic demo, but there were problems in porting lwip, so I changed to freertos+lvgl+lwip to build the development environment.

【Development environment】

Win11 + STM32CubeIde

【Transplantation steps】

  1. Install stm32cubeide. The official website has detailed tutorials, which will not be explained in detail here.
  2. After installation, open stm32cubeide, create a new stm32 project, and select stm32f746G-Discovery as the target development board. Generate the project according to the default options. The default OS running in the project is freeRTos.
  3. Download the open source source code of stm32f746-discoer from github: git clone --recursive
    链接已隐藏,如需查看请登录或者注册
  4. After downloading, we copy the STM32F746G-Discovery and Components in the lvgl, hal_stm_lvgl and Uilitites directories shown in the figure below to the specified directory of the project.
  5. Copy lvgl.conf.h to the project directory.
  6. After compiling, follow the prompts to add the directory referenced by the file. I will not record them one by one here.
  7. Add Lwip. In the configuration interface, we turn on LWIP, disable DHCP, and configure the network card according to the development environment.
  8. Select the network PHY chip. Our version uses LAN8742:
  9. Add two tasks in freertos, task 1 is lvgl, task 2 is lwip, and create a queue for communication between the two tasks:
  10. In Task 1, we add a button and a label in lvgl. The code is as follows:
lv_obj_t *label;

void lvgl_lable_test(){
    /* 创建??????个标?????? */
    label = lv_label_create(lv_scr_act());
    if (NULL != label)
    {
        // lv_obj_set_x(label, 90);                         // 设置控件的X坐标
        // lv_obj_set_y(label, 100);                        // 设置控件的Y坐标
        // lv_obj_set_size(label, 60, 20);                  // 设置控件大小
        lv_label_set_text(label, "Counter");                // 初始显示 0
        // lv_obj_center(label);                            // 居中显示
        lv_obj_align(label, LV_ALIGN_CENTER, 0, -50);       // 居中显示后,向上偏移50
    }
}

/**
 * [url=home.php?mod=space&uid=159083]@brief[/url] 按钮事件回调函数
 */
static void btn_event_callback(lv_event_t* event)
{
    static uint32_t counter = 1;

    lv_obj_t* btn = lv_event_get_target(event);                 //获取事件对象
    if (btn != NULL)
    {
        lv_label_set_text_fmt(label, "%d", counter);            //设置显示内容
        lv_obj_align(label, LV_ALIGN_CENTER, 0, -50);           // 居中显示后,向上偏移50
        if(osMessagePut(myQueue01Handle, counter, 100) != osOK)
        {
        	printf("send error\r\n");
        }
        counter++;
    }
}

/**
 * @brief 创建按钮
 */
void lvgl_button_test(){
    /* 在当前界面中创建??????个按?????? */
    lv_obj_t* btn = lv_btn_create(lv_scr_act());                                        // 创建Button对象
    if (btn != NULL)
    {
        lv_obj_set_size(btn, 80, 20);                                                   // 设置对象宽度和高??????
        // lv_obj_set_pos(btn, 90, 200);                                                // 设置按钮的X和Y坐标
        lv_obj_add_event_cb(btn, btn_event_callback, LV_EVENT_CLICKED, NULL);           // 给对象添加CLICK事件和事件处理回调函??????
        lv_obj_align(btn, LV_ALIGN_CENTER, 0, 50);                                      // 居中显示后,向下偏移50

        lv_obj_t* btn_label = lv_label_create(btn);                                     // 基于Button对象创建Label对象
        if (btn_label != NULL)
        {
            lv_label_set_text(btn_label, "button");                                     // 设置显示内容
            lv_obj_center(btn_label);                                                   // 对象居中显示
        }
    }
}

void StartTask1(void const * argument)
{
  /* init code for USB_HOST */
  MX_USB_HOST_Init();

  /* init code for LWIP */
//  MX_LWIP_Init();
  /* USER CODE BEGIN StartTask1 */
  lv_init();

	tft_init();
	touchpad_init();
	lvgl_lable_test();
	lvgl_button_test();
//	client_socket_init();
  /* Infinite loop */
  for(;;)
  {
	  lv_task_handler();
	  osDelay(10);
  }
  /* USER CODE END StartTask1 */
}
  1. Task 2 is to create the lwip task, which is mainly to connect to the TCP server and wait for the key to send information. If the queue message is received, the message will be sent to the TCP server.
    void StartTaskLwip(void const * argument)
    {
      /* USER CODE BEGIN StartTaskLwip */
    	osEvent event;
    	MX_LWIP_Init();
    	int sock;
    	  struct sockaddr_in address;
    	  uint8_t send_buf[64]= {0};
    	  /* create a TCP socket */
    	  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    	  {
    	      printf("Socket error\n");
    	    return;
    	  }
    
    	  address.sin_family = AF_INET;
    	  address.sin_port = htons(PORT);
    	  address.sin_addr.s_addr = inet_addr(IP_ADDR);
    	  memset(&(address.sin_zero), 0, sizeof(address.sin_zero));
    
    	  if (connect(sock,(struct sockaddr *)&address,sizeof(struct sockaddr)) == -1)
    	  {
    	      printf("Connect failed!\n");
    	      closesocket(sock);
    	   }
    	  else
    	  {
    	      printf("Connect to iperf server successful!\n");
    	  }
    	  while (1)
    	  {
    		  event = osMessageGet(myQueue01Handle, 100);
    		  if(event.status == osEventMessage)
    		  {
    			  sprintf(send_buf,"get messege %d\r\n",event.value.v);
    			  if (write(sock,send_buf,sizeof(send_buf)) < 0)
    			   break;
    			  memset(send_buf,0,sizeof(send_buf));
    		  }
    
    	      vTaskDelay(10);
    	  }
    	  closesocket(sock);
      /* USER CODE END StartTaskLwip */
    }

  2. At the same time, we need to open the two macros LWIP_NETCONN and LWIP_SOCKET in lwipopts.h, as follows:

By then, our transplantation work will be completed.

[Experimental results]

We press the button on the touch screen and receive the information sent by the development board on the TCP server:

【Experience】

Stm32cubeIDE has been officially upgraded and is now becoming more and more user-friendly. In addition, the official driver has been provided for the development board, allowing developers to get started very quickly. FreeRTos is much better than RTT. It seems that domestic RTT still has a long way to go and the ecosystem needs to be further strengthened.

This post is from DigiKey Technology Zone

Latest reply

RTT has a good software ecosystem, RT Thread Studio. ST mainly uses FreeRTOS.   Details Published on 2023-10-26 19:21
 
 

280

Posts

7

Resources
2
 

The boss is so skilled in using the stm32 tool software, awesome

This post is from DigiKey Technology Zone

Comments

Thank you for your affirmation, I will continue to work hard and strive to share more works!  Details Published on 2023-10-26 09:43
 
 
 

6827

Posts

11

Resources
3
 
sipower posted on 2023-10-26 09:23 You are so skilled in using the stm32 tool software, awesome

Thank you for your affirmation, I will continue to work hard and strive to share more works!

This post is from DigiKey Technology Zone
 
 
 

6748

Posts

2

Resources
4
 

RTT has a good software ecosystem, RT Thread Studio. ST mainly uses FreeRTOS.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Happy Knowledge: How is the CPU made?

If calculated by price/weight, CPU is much more expensive than gold. Almost everyone knows that CPU is mainly made of si ...

Recommended wireless router products from four major brands

Source: Zhongguancun Online The most well-known brands in the wireless market are definitely TP-Link, D-Link , Netgear ...

About the use of CAN bus

What I don't quite understand is that the program sends a frame of data every 500ms, but the actual time interval betwee ...

Hey guys, this error says that my pin is not numbered, but does the number I wrote next to it count as the number? How can I solve this problem?

526062526063

MicroPython has solved the ESP32's NeoPixel display bug

With the update of https://github.com/micropython/micropython/pull/7985 , the occasional random flash bug of neopixel di ...

[Flower carving hands-on] Interesting and fun music visualization series of small projects (19) - full-body fiber optic lamp

I suddenly had the urge to do a series of topics on music visualization. This topic is a bit difficult and covers a wide ...

[Flower carving DIY] Interesting and fun music visualization series of small projects (20) - jewelry box mirror lamp

I suddenly had the urge to do a series of topics on music visualization. This topic is a bit difficult and covers a wide ...

Some use cases of MicroPython mpremote tool, chat

# Some use cases for the MicroPython mpremote tool, I've tried a lot of different third-party micropython tools, some ar ...

[2022 Digi-Key Innovation Design Competition Participation Award Review] Bought the K210 and it’s pretty good

669371 669372 669373 I won a participation prize, so I quickly placed an order. Haha, it arrived today, I’m posting the ...

48 "Ten Thousand Miles" Raspberry Pi Car——PicoW Learning (C Language TCP Communication)

This post was last edited by lb8820265 on 2023-11-12 19:59 Regarding the writing of PicoW's WiFi function program, the ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list