App_freertosTask.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "App_freertosTask.h"
  2. #include <stdio.h>
  3. #include "lvgl.h"
  4. #include "lv_port_disp.h"
  5. #include "lv_port_indev.h"
  6. #include "Int_gt1151.h"
  7. #include "Ui.h"
  8. #include "Com_usb_cdc.h"
  9. #include "Com_usart3.h"
  10. #include "Com_protocol.h"
  11. #include "Mid_pid.h"
  12. #define TASK_CPLD_PRIORITY 4
  13. #define TASK_CPLD_STACK_SIZE 512
  14. #define TASK_LVGL_PRIORITY 3
  15. #define TASK_LVGL_STACK_SIZE 2048
  16. #define TASK_TEST_PRIORITY 1
  17. #define TASK_TEST_STACK_SIZE 512
  18. /* CPLD 控制周期 ( 30s 超时; 1s 周期远小于它) */
  19. #define CPLD_SEND_PERIOD_MS 1000
  20. TaskHandle_t xCPLDProcessTaskHandle = NULL;
  21. TaskHandle_t xLVGLDisplayTaskHandle = NULL;
  22. TaskHandle_t xTestTaskHandle = NULL;
  23. /**
  24. * @brief 创建各个任务
  25. *
  26. */
  27. void App_Task_Create(void)
  28. {
  29. BaseType_t ret;
  30. ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
  31. if (ret != pdPASS)
  32. {
  33. printf("[ERR] CPLD process task create failed\r\n");
  34. }
  35. ret = xTaskCreate(Task_LVGL_Display, "lvglDisplayTask", TASK_LVGL_STACK_SIZE, NULL, TASK_LVGL_PRIORITY, &xLVGLDisplayTaskHandle);
  36. if (ret != pdPASS)
  37. {
  38. printf("[ERR] LVGL display task create failed\r\n");
  39. }
  40. ret = xTaskCreate(Task_Test, "testTask", TASK_TEST_STACK_SIZE, NULL, TASK_TEST_PRIORITY, &xTestTaskHandle);
  41. if (ret != pdPASS)
  42. {
  43. printf("[ERR] Test task create failed\r\n");
  44. }
  45. }
  46. /**
  47. * @brief 处理 CPLD 通信的任务
  48. *
  49. * 主机角色: 周期组装 SET_ALL(0x01) 帧并经 USART3(921600) 下发,
  50. * payload 取 Mid 层共享输出缓冲 (24 PTC + 8 FAN)。
  51. * 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
  52. *
  53. * @param argument
  54. */
  55. void Task_CPLD_Process(void *argument)
  56. {
  57. (void)argument;
  58. uint8_t frame[COM_PROTOCOL_FRAME_MAX_LEN];
  59. uint16_t flen;
  60. printf("CPLD process task running...\r\n");
  61. Mid_PID_Init();
  62. while (1)
  63. {
  64. flen = Com_protocol_set_all(Mid_PID_GetOutputDuty(), frame);
  65. if (flen > 0)
  66. {
  67. Com_usart3_send(frame, flen);
  68. }
  69. vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
  70. }
  71. }
  72. /**
  73. * @brief 处理 LVGL 显示的任务
  74. *
  75. * @param argument
  76. */
  77. void Task_LVGL_Display(void *argument)
  78. {
  79. (void)argument;
  80. printf("LVGL display task running...\r\n");
  81. /* LVGL 内核初始化 */
  82. lv_init();
  83. /* 注册显示驱动:LTDC + SDRAM 静态显存 + 双全屏缓冲 */
  84. lv_port_disp_init();
  85. /* 初始化 GT1151Q 电容触摸芯片 */
  86. if (Int_gt1151_init() == 0)
  87. {
  88. printf("GT1151 Init OK\r\n");
  89. /* 注册触摸输入设备 (注意: 需在 lv_init 之后、主循环 lv_timer_handler 之前) */
  90. lv_port_indev_init();
  91. }
  92. else
  93. {
  94. printf("[ERR] GT1151 Init failed\r\n");
  95. }
  96. /* 创建工业设备主页 (UI 页面层) */
  97. Ui_init();
  98. /* 假数据: 设备就绪 */
  99. Ui_set_ready(true);
  100. while (1)
  101. {
  102. /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
  103. lv_timer_handler();
  104. vTaskDelay(pdMS_TO_TICKS(5));
  105. }
  106. }
  107. /**
  108. * @brief 处理测试的任务
  109. *
  110. * @param argument
  111. */
  112. void Task_Test(void *argument)
  113. {
  114. (void)argument;
  115. printf("Test task running...\r\n");
  116. static const uint8_t tx_frame[] = "USART3 Test Frame ABC 123\r\n";
  117. while (1)
  118. {
  119. uint32_t sent = Com_usart3_send(tx_frame, sizeof(tx_frame) - 1);
  120. printf("[USART3 TX] sent=%lu\r\n", (unsigned long)sent);
  121. while (Com_usart3_available() > 0)
  122. {
  123. uint8_t b;
  124. Com_usart3_read(&b, 1);
  125. printf("%c", b);
  126. }
  127. vTaskDelay(pdMS_TO_TICKS(5000));
  128. }
  129. }