Procházet zdrojové kódy

1. 新增 Com_usart3 模块,xStreamBuffer收发,缓冲置AXI SRAM,USART3(PB10/PB11,921600,FIFO) + DMA1 S0
2.中断优先级调整
3.修复复位重枚举导致开头打印被吞:CDC_Transmit_FS未CONFIGURED拒发和Task_Usb_Send首挂载宽限500ms
4.新增 Mid_pid PID中间层

zhanghongyi před 3 měsíci
rodič
revize
a194ce9cb0

+ 12 - 3
App/Src/App_freertosTask.c

@@ -7,6 +7,7 @@
 #include "Int_gt1151.h"
 #include "Int_gt1151.h"
 #include "Ui.h"
 #include "Ui.h"
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
+#include "Com_usart3.h"
 
 
 #define TASK_CPLD_PRIORITY          4
 #define TASK_CPLD_PRIORITY          4
 #define TASK_CPLD_STACK_SIZE        512
 #define TASK_CPLD_STACK_SIZE        512
@@ -30,8 +31,6 @@ void App_Task_Create(void)
 {
 {
     BaseType_t ret;
     BaseType_t ret;
 
 
-    Com_usb_cdc_init();
-
     ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
     ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
     if (ret != pdPASS)
     if (ret != pdPASS)
     {
     {
@@ -118,9 +117,19 @@ void Task_Test(void *argument)
 {
 {
     (void)argument;
     (void)argument;
     printf("Test task running...\r\n");
     printf("Test task running...\r\n");
+    static const uint8_t tx_frame[] = "USART3 Test Frame ABC 123\r\n";
+
     while (1)
     while (1)
     {
     {
-        /* TODO: 测试 */
+        uint32_t sent = Com_usart3_send(tx_frame, sizeof(tx_frame) - 1);
+        printf("[USART3 TX] sent=%lu\r\n", (unsigned long)sent);
+
+        while (Com_usart3_available() > 0)
+        {
+            uint8_t b;
+            Com_usart3_read(&b, 1);
+            printf("%c", b);
+        }
         vTaskDelay(pdMS_TO_TICKS(5000));
         vTaskDelay(pdMS_TO_TICKS(5000));
     }
     }
 }
 }

+ 19 - 0
Com/Inc/Com_usart3.h

@@ -0,0 +1,19 @@
+#ifndef __COM_USART3_H
+#define __COM_USART3_H
+
+#include <stdint.h>
+
+/* 本模块专用 USART3:PB10=TX, PB11=RX, 波特率 921600 */
+
+#define COM_USART3_STREAM_SIZE   4096
+#define COM_USART3_RX_DMA_SIZE   1024
+
+#define COM_USART3_TX_TASK_PRIORITY 3
+#define COM_USART3_TX_TASK_STACK    512
+
+void     Com_usart3_init(void);
+uint32_t Com_usart3_send(const uint8_t *data, uint32_t len);
+uint16_t Com_usart3_available(void);
+uint32_t Com_usart3_read(uint8_t *buf, uint32_t len);
+
+#endif /* __COM_USART3_H */

+ 4 - 0
Com/Inc/Com_usb_cdc.h

@@ -8,6 +8,10 @@
 #define COM_USB_SEND_TASK_PRIORITY 3
 #define COM_USB_SEND_TASK_PRIORITY 3
 #define COM_USB_SEND_TASK_STACK    512
 #define COM_USB_SEND_TASK_STACK    512
 
 
+/* 首次挂载宽限(ms):设备 CONFIGURED 后等待终端重连再发首包,
+   避免第一包掉进主机重枚举窗口被丢弃;酌情调大(如 1000) */
+#define COM_USB_BOOT_HOLDOFF_MS   500
+
 void Com_usb_cdc_init(void);
 void Com_usb_cdc_init(void);
 void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len);
 void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len);
 uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len);
 uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len);

+ 243 - 0
Com/Src/Com_usart3.c

@@ -0,0 +1,243 @@
+#include "Com_usart3.h"
+
+#include "usart.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "stream_buffer.h"
+
+/* usart.c(USART3) 内定义的 DMA 句柄 */
+extern DMA_HandleTypeDef hdma_usart3_rx;
+
+/*
+ * 本模块专用 USART3:PB10=TX, PB11=RX, 波特率 921600。
+ *
+ * 数据缓冲统一放置到 AXI SRAM (0x24000000)。
+ * 原因:DMA1 无法访问 DTCM(0x20000000,Keil 默认变量区),
+ *       AXI SRAM 可由 DMA1 访问且当前空闲(LVGL 显存放在 SDRAM)。
+ * 注意:AXI SRAM 被 MPU 配置为 CACHEABLE,收发需做 D-Cache 维护。
+ * 注意:s_tx_stream/s_rx_stream 为 FreeRTOS 动态堆分配(heap_4),
+ *       不落 AXI SRAM——与 USB CDC 的 xUsbTxStream 处理方式一致。
+ */
+#define COM_USART3_AXI_SRAM_SECTION  __attribute__((section(".ARM.__at_0x24000000")))
+
+COM_USART3_AXI_SRAM_SECTION static uint8_t s_rx_dma_buf[COM_USART3_RX_DMA_SIZE];
+/* 暂存待发送的数据块(TX 发送任务从 TX 流缓冲取出后经此交给 DMA) */
+COM_USART3_AXI_SRAM_SECTION static uint8_t s_tx_chunk[COM_USART3_STREAM_SIZE];
+
+static StreamBufferHandle_t s_tx_stream = NULL;
+static StreamBufferHandle_t s_rx_stream = NULL;
+
+static TaskHandle_t s_tx_task_handle = NULL;
+
+/* 上一个 DMA 接收写入位置(用于计算新数据) */
+static volatile uint32_t s_rx_prev_pos = 0;
+
+static uint8_t s_rx_started = 0;
+
+static void Task_Usart3_Send(void *argument);
+
+/**
+ * @brief (USART3) 从 DMA 缓冲读取新接收的数据(空闲事件 + 半满/全满皆调用)
+ * @note  在中断上下文中执行
+ */
+static void Usart3_Drain_Rx(void)
+{
+    uint32_t cur_pos = (uint32_t)(COM_USART3_RX_DMA_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx));
+    uint32_t new_bytes;
+    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+
+    if (cur_pos >= s_rx_prev_pos)
+    {
+        new_bytes = cur_pos - s_rx_prev_pos;
+    }
+    else
+    {
+        new_bytes = (COM_USART3_RX_DMA_SIZE - s_rx_prev_pos) + cur_pos;
+    }
+
+    if (new_bytes > COM_USART3_RX_DMA_SIZE)
+    {
+        new_bytes = COM_USART3_RX_DMA_SIZE;
+    }
+
+    if (new_bytes == 0)
+    {
+        return;
+    }
+
+    /* DMA 已写入 AXI SRAM,先使缓存失效再读取。
+       回绕时两段分别是 [prev..尾) 与 [0..remainder),须分段失效,
+       一次性跨缓冲区尾失效无法覆盖 [0..remainder) 对应的缓存行。 */
+    if ((s_rx_prev_pos + new_bytes) <= COM_USART3_RX_DMA_SIZE)
+    {
+        SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)new_bytes);
+    }
+    else
+    {
+        uint32_t first_len = COM_USART3_RX_DMA_SIZE - s_rx_prev_pos;
+        SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)first_len);
+        SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[0],
+                                     (int32_t)(new_bytes - first_len));
+    }
+
+    /* 环形回绕:跨过 DMA 缓冲末尾时拆成两段写入流缓冲(满则丢弃余下) */
+    if ((s_rx_prev_pos + new_bytes) <= COM_USART3_RX_DMA_SIZE)
+    {
+        xStreamBufferSendFromISR(s_rx_stream,
+                                 &s_rx_dma_buf[s_rx_prev_pos],
+                                 new_bytes,
+                                 &xHigherPriorityTaskWoken);
+    }
+    else
+    {
+        uint32_t first_len = COM_USART3_RX_DMA_SIZE - s_rx_prev_pos;
+        xStreamBufferSendFromISR(s_rx_stream,
+                                 &s_rx_dma_buf[s_rx_prev_pos],
+                                 first_len,
+                                 &xHigherPriorityTaskWoken);
+        xStreamBufferSendFromISR(s_rx_stream,
+                                 &s_rx_dma_buf[0],
+                                 new_bytes - first_len,
+                                 &xHigherPriorityTaskWoken);
+    }
+
+    s_rx_prev_pos = cur_pos;
+    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+}
+
+/**
+ * @brief (USART3) 空闲事件 / 接收事件回调(HAL,中断上下文)
+ * @note  CIRCULAR 模式下 IDLE/HT/TC 事件不会停止接收进程,
+ *        DMA 持续运行,无需在此重启接收。
+ */
+void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
+{
+    if (huart->Instance == USART3)
+    {
+        (void)Size;
+        Usart3_Drain_Rx();
+    }
+}
+
+/**
+ * @brief (USART3) 接收完成回调(半满/全满,CIRCULAR 模式下由 DMA 触发)
+ */
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
+{
+    if (huart->Instance == USART3)
+    {
+        Usart3_Drain_Rx();
+    }
+}
+
+void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
+{
+    if (huart->Instance == USART3)
+    {
+        Usart3_Drain_Rx();
+    }
+}
+
+/**
+ * @brief (USART3) TX DMA 发送完成回调(中断上下文)
+ */
+void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
+{
+    if (huart->Instance == USART3)
+    {
+        /* 通知发送任务取下一块继续发送 */
+        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+        vTaskNotifyGiveFromISR(s_tx_task_handle, &xHigherPriorityTaskWoken);
+        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+    }
+}
+
+/**
+ * @brief USART3 发送任务:从 TX 流缓冲取数据块,经 DMA 发送。
+ *        xStreamBufferReceive 无数据时阻塞,有数据自动唤醒(无需手动 notify)。
+ */
+static void Task_Usart3_Send(void *argument)
+{
+    (void)argument;
+
+    for (;;)
+    {
+        size_t n = xStreamBufferReceive(s_tx_stream, s_tx_chunk,
+                                        COM_USART3_STREAM_SIZE, portMAX_DELAY);
+
+        /* DMA 读取前将数据刷出到内存(避 AXI SRAM 缓存) */
+        SCB_CleanDCache_by_Addr((uint32_t *)s_tx_chunk, (int32_t)n);
+
+        HAL_UART_Transmit_DMA(&huart3, s_tx_chunk, (uint16_t)n);
+        /* 等待发送完成回调通知后再取下一块 */
+        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
+    }
+}
+
+/**
+ * @brief 初始化 USART3 通信:启动 RX DMA 接收、创建发送任务
+ * @note  需在 MX_USART3_UART_Init() 之后、调度器启动之前调用
+ */
+void Com_usart3_init(void)
+{
+    if (s_tx_stream == NULL)
+    {
+        s_tx_stream = xStreamBufferCreate(COM_USART3_STREAM_SIZE, 1);
+    }
+    if (s_rx_stream == NULL)
+    {
+        s_rx_stream = xStreamBufferCreate(COM_USART3_STREAM_SIZE, 1);
+    }
+
+    /* 启动 RX:CIRCULAR DMA + 空闲中断 */
+    if (s_rx_started == 0)
+    {
+        HAL_UARTEx_ReceiveToIdle_DMA(&huart3, s_rx_dma_buf, COM_USART3_RX_DMA_SIZE);
+        s_rx_started = 1;
+    }
+
+    /* 创建发送任务 */
+    if (s_tx_task_handle == NULL)
+    {
+        xTaskCreate(Task_Usart3_Send, "usart3SendTask", COM_USART3_TX_TASK_STACK,
+                    NULL, COM_USART3_TX_TASK_PRIORITY, &s_tx_task_handle);
+    }
+}
+
+/**
+ * @brief (USART3) 发送数据:阻塞最多 100ms 写入 TX 流缓冲
+ * @return 实际写入 TX 流缓冲的字节数(满则丢弃余下)
+ */
+uint32_t Com_usart3_send(const uint8_t *data, uint32_t len)
+{
+    if (s_tx_stream == NULL || data == NULL)
+    {
+        return 0;
+    }
+    return (uint32_t)xStreamBufferSend(s_tx_stream, data, len, pdMS_TO_TICKS(100));
+}
+
+/**
+ * @brief (USART3) 查询 RX 流缓冲可读字节数
+ */
+uint16_t Com_usart3_available(void)
+{
+    if (s_rx_stream == NULL)
+    {
+        return 0;
+    }
+    return (uint16_t)xStreamBufferBytesAvailable(s_rx_stream);
+}
+
+/**
+ * @brief (USART3) 从 RX 流缓冲读取数据(非阻塞)
+ * @return 实际读取字节数
+ */
+uint32_t Com_usart3_read(uint8_t *buf, uint32_t len)
+{
+    if (s_rx_stream == NULL || buf == NULL)
+    {
+        return 0;
+    }
+    return (uint32_t)xStreamBufferReceive(s_rx_stream, buf, len, 0);
+}

+ 35 - 12
Com/Src/Com_usb_cdc.c

@@ -9,10 +9,16 @@
 
 
 #include "usbd_cdc_if.h"
 #include "usbd_cdc_if.h"
 
 
+/* usb_device.c 中定义的 USB 设备句柄 */
+extern USBD_HandleTypeDef hUsbDeviceFS;
+
 StreamBufferHandle_t xUsbTxStream = NULL;
 StreamBufferHandle_t xUsbTxStream = NULL;
 StreamBufferHandle_t xUsbRxStream = NULL;
 StreamBufferHandle_t xUsbRxStream = NULL;
 TaskHandle_t xUsbSendTaskHandle = NULL;
 TaskHandle_t xUsbSendTaskHandle = NULL;
 
 
+/* 首次挂载宽限标志:设备未 CONFIGURED 时重置,CONFIGURED 后等宽限才发首包 */
+static uint8_t s_attach_hold_ok = 0;
+
 /**
 /**
  * @brief USB CDC 发送任务
  * @brief USB CDC 发送任务
  *        从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
  *        从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
@@ -30,21 +36,38 @@ static void Task_Usb_Send(void *argument)
 
 
     for (;;)
     for (;;)
     {
     {
-        got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
-        if (got > 0)
+        if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
         {
         {
-            uint32_t sent = 0;
-            while (sent < got)
+            /* 未枚举:重置宽限并低频轮询,不空转 */
+            s_attach_hold_ok = 0;
+            vTaskDelay(pdMS_TO_TICKS(10));
+        }
+        else
+        {
+            if (s_attach_hold_ok == 0)
             {
             {
-                uint16_t chunk = (uint16_t)(got - sent);
-                if (CDC_Transmit_FS(&buf[sent], chunk) == USBD_OK)
-                {
-                    sent += chunk;
-                }
-                else
+                /* CONFIGURED 后先等终端(COM_B)重连再发首包,
+                   避免第一包掉进主机重枚举窗口被丢弃 */
+                vTaskDelay(pdMS_TO_TICKS(COM_USB_BOOT_HOLDOFF_MS));
+                s_attach_hold_ok = 1;
+            }
+
+            got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
+            if (got > 0)
+            {
+                uint32_t sent = 0;
+                while (sent < got)
                 {
                 {
-                    /* 端点忙,稍后重试 */
-                    vTaskDelay(pdMS_TO_TICKS(1));
+                    uint16_t chunk = (uint16_t)(got - sent);
+                    if (CDC_Transmit_FS(&buf[sent], chunk) == USBD_OK)
+                    {
+                        sent += chunk;
+                    }
+                    else
+                    {
+                        /* 端点忙,稍后重试 */
+                        vTaskDelay(pdMS_TO_TICKS(1));
+                    }
                 }
                 }
             }
             }
         }
         }

+ 52 - 0
Core/Inc/dma.h

@@ -0,0 +1,52 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    dma.h
+  * @brief   This file contains all the function prototypes for
+  *          the dma.c file
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __DMA_H__
+#define __DMA_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+/* DMA memory to memory transfer handles -------------------------------------*/
+
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+void MX_DMA_Init(void);
+
+/* USER CODE BEGIN Prototypes */
+
+/* USER CODE END Prototypes */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DMA_H__ */
+

+ 3 - 0
Core/Inc/stm32h7xx_it.h

@@ -55,7 +55,10 @@ void SVC_Handler(void);
 void DebugMon_Handler(void);
 void DebugMon_Handler(void);
 void PendSV_Handler(void);
 void PendSV_Handler(void);
 void SysTick_Handler(void);
 void SysTick_Handler(void);
+void DMA1_Stream0_IRQHandler(void);
+void DMA1_Stream1_IRQHandler(void);
 void TIM2_IRQHandler(void);
 void TIM2_IRQHandler(void);
+void USART3_IRQHandler(void);
 void OTG_FS_IRQHandler(void);
 void OTG_FS_IRQHandler(void);
 /* USER CODE BEGIN EFP */
 /* USER CODE BEGIN EFP */
 
 

+ 3 - 0
Core/Inc/usart.h

@@ -34,11 +34,14 @@ extern "C" {
 
 
 extern UART_HandleTypeDef huart1;
 extern UART_HandleTypeDef huart1;
 
 
+extern UART_HandleTypeDef huart3;
+
 /* USER CODE BEGIN Private defines */
 /* USER CODE BEGIN Private defines */
 
 
 /* USER CODE END Private defines */
 /* USER CODE END Private defines */
 
 
 void MX_USART1_UART_Init(void);
 void MX_USART1_UART_Init(void);
+void MX_USART3_UART_Init(void);
 
 
 /* USER CODE BEGIN Prototypes */
 /* USER CODE BEGIN Prototypes */
 
 

+ 58 - 0
Core/Src/dma.c

@@ -0,0 +1,58 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    dma.c
+  * @brief   This file provides code for the configuration
+  *          of all the requested memory to memory DMA transfers.
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "dma.h"
+
+/* USER CODE BEGIN 0 */
+
+/* USER CODE END 0 */
+
+/*----------------------------------------------------------------------------*/
+/* Configure DMA                                                              */
+/*----------------------------------------------------------------------------*/
+
+/* USER CODE BEGIN 1 */
+
+/* USER CODE END 1 */
+
+/**
+  * Enable DMA controller clock
+  */
+void MX_DMA_Init(void)
+{
+
+  /* DMA controller clock enable */
+  __HAL_RCC_DMA1_CLK_ENABLE();
+
+  /* DMA interrupt init */
+  /* DMA1_Stream0_IRQn interrupt configuration */
+  HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 6, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
+  /* DMA1_Stream1_IRQn interrupt configuration */
+  HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 7, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
+
+}
+
+/* USER CODE BEGIN 2 */
+
+/* USER CODE END 2 */
+

+ 7 - 1
Core/Src/main.c

@@ -18,6 +18,7 @@
 /* USER CODE END Header */
 /* USER CODE END Header */
 /* Includes ------------------------------------------------------------------*/
 /* Includes ------------------------------------------------------------------*/
 #include "main.h"
 #include "main.h"
+#include "dma.h"
 #include "dma2d.h"
 #include "dma2d.h"
 #include "i2c.h"
 #include "i2c.h"
 #include "ltdc.h"
 #include "ltdc.h"
@@ -37,6 +38,8 @@
 #include "FreeRTOS.h"
 #include "FreeRTOS.h"
 #include "task.h"
 #include "task.h"
 #include "App_freertosTask.h"
 #include "App_freertosTask.h"
+#include "Com_usart3.h"
+#include "Com_usb_cdc.h"
 /* USER CODE END Includes */
 /* USER CODE END Includes */
 
 
 /* Private typedef -----------------------------------------------------------*/
 /* Private typedef -----------------------------------------------------------*/
@@ -117,6 +120,7 @@ int main(void)
 
 
   /* Initialize all configured peripherals */
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_GPIO_Init();
+  MX_DMA_Init();
   MX_FMC_Init();
   MX_FMC_Init();
   MX_QUADSPI_Init();
   MX_QUADSPI_Init();
   MX_USART1_UART_Init();
   MX_USART1_UART_Init();
@@ -124,10 +128,12 @@ int main(void)
   MX_LTDC_Init();
   MX_LTDC_Init();
   MX_DMA2D_Init();
   MX_DMA2D_Init();
   MX_USB_DEVICE_Init();
   MX_USB_DEVICE_Init();
+  MX_USART3_UART_Init();
   /* USER CODE BEGIN 2 */
   /* USER CODE BEGIN 2 */
-  printf("hello h743\r\n");
   Int_sdram_init();
   Int_sdram_init();
   Int_lcd_init();
   Int_lcd_init();
+  Com_usb_cdc_init();
+  Com_usart3_init();
   printf("System Init OK\r\n");
   printf("System Init OK\r\n");
   /* USER CODE END 2 */
   /* USER CODE END 2 */
 
 

+ 45 - 0
Core/Src/stm32h7xx_it.c

@@ -58,6 +58,9 @@
 
 
 /* External variables --------------------------------------------------------*/
 /* External variables --------------------------------------------------------*/
 extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
 extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
+extern DMA_HandleTypeDef hdma_usart3_rx;
+extern DMA_HandleTypeDef hdma_usart3_tx;
+extern UART_HandleTypeDef huart3;
 extern TIM_HandleTypeDef htim2;
 extern TIM_HandleTypeDef htim2;
 
 
 /* USER CODE BEGIN EV */
 /* USER CODE BEGIN EV */
@@ -205,6 +208,34 @@ void SysTick_Handler(void)
 /* please refer to the startup file (startup_stm32h7xx.s).                    */
 /* please refer to the startup file (startup_stm32h7xx.s).                    */
 /******************************************************************************/
 /******************************************************************************/
 
 
+/**
+  * @brief This function handles DMA1 stream0 global interrupt.
+  */
+void DMA1_Stream0_IRQHandler(void)
+{
+  /* USER CODE BEGIN DMA1_Stream0_IRQn 0 */
+
+  /* USER CODE END DMA1_Stream0_IRQn 0 */
+  HAL_DMA_IRQHandler(&hdma_usart3_rx);
+  /* USER CODE BEGIN DMA1_Stream0_IRQn 1 */
+
+  /* USER CODE END DMA1_Stream0_IRQn 1 */
+}
+
+/**
+  * @brief This function handles DMA1 stream1 global interrupt.
+  */
+void DMA1_Stream1_IRQHandler(void)
+{
+  /* USER CODE BEGIN DMA1_Stream1_IRQn 0 */
+
+  /* USER CODE END DMA1_Stream1_IRQn 0 */
+  HAL_DMA_IRQHandler(&hdma_usart3_tx);
+  /* USER CODE BEGIN DMA1_Stream1_IRQn 1 */
+
+  /* USER CODE END DMA1_Stream1_IRQn 1 */
+}
+
 /**
 /**
   * @brief This function handles TIM2 global interrupt.
   * @brief This function handles TIM2 global interrupt.
   */
   */
@@ -219,6 +250,20 @@ void TIM2_IRQHandler(void)
   /* USER CODE END TIM2_IRQn 1 */
   /* USER CODE END TIM2_IRQn 1 */
 }
 }
 
 
+/**
+  * @brief This function handles USART3 global interrupt.
+  */
+void USART3_IRQHandler(void)
+{
+  /* USER CODE BEGIN USART3_IRQn 0 */
+
+  /* USER CODE END USART3_IRQn 0 */
+  HAL_UART_IRQHandler(&huart3);
+  /* USER CODE BEGIN USART3_IRQn 1 */
+
+  /* USER CODE END USART3_IRQn 1 */
+}
+
 /**
 /**
   * @brief This function handles USB OTG FS global interrupt.
   * @brief This function handles USB OTG FS global interrupt.
   */
   */

+ 146 - 0
Core/Src/usart.c

@@ -25,6 +25,9 @@
 /* USER CODE END 0 */
 /* USER CODE END 0 */
 
 
 UART_HandleTypeDef huart1;
 UART_HandleTypeDef huart1;
+UART_HandleTypeDef huart3;
+DMA_HandleTypeDef hdma_usart3_rx;
+DMA_HandleTypeDef hdma_usart3_tx;
 
 
 /* USART1 init function */
 /* USART1 init function */
 
 
@@ -69,6 +72,51 @@ void MX_USART1_UART_Init(void)
 
 
   /* USER CODE END USART1_Init 2 */
   /* USER CODE END USART1_Init 2 */
 
 
+}
+/* USART3 init function */
+
+void MX_USART3_UART_Init(void)
+{
+
+  /* USER CODE BEGIN USART3_Init 0 */
+
+  /* USER CODE END USART3_Init 0 */
+
+  /* USER CODE BEGIN USART3_Init 1 */
+
+  /* USER CODE END USART3_Init 1 */
+  huart3.Instance = USART3;
+  huart3.Init.BaudRate = 921600;
+  huart3.Init.WordLength = UART_WORDLENGTH_8B;
+  huart3.Init.StopBits = UART_STOPBITS_1;
+  huart3.Init.Parity = UART_PARITY_NONE;
+  huart3.Init.Mode = UART_MODE_TX_RX;
+  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
+  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
+  huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
+  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT;
+  huart3.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
+  if (HAL_UART_Init(&huart3) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_2) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_4) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_EnableFifoMode(&huart3) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN USART3_Init 2 */
+
+  /* USER CODE END USART3_Init 2 */
+
 }
 }
 
 
 void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
 void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
@@ -110,6 +158,80 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
 
 
   /* USER CODE END USART1_MspInit 1 */
   /* USER CODE END USART1_MspInit 1 */
   }
   }
+  else if(uartHandle->Instance==USART3)
+  {
+  /* USER CODE BEGIN USART3_MspInit 0 */
+
+  /* USER CODE END USART3_MspInit 0 */
+
+  /** Initializes the peripherals clock
+  */
+    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;
+    PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
+    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
+    {
+      Error_Handler();
+    }
+
+    /* USART3 clock enable */
+    __HAL_RCC_USART3_CLK_ENABLE();
+
+    __HAL_RCC_GPIOB_CLK_ENABLE();
+    /**USART3 GPIO Configuration
+    PB10     ------> USART3_TX
+    PB11     ------> USART3_RX
+    */
+    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
+    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
+    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+
+    /* USART3 DMA Init */
+    /* USART3_RX Init */
+    hdma_usart3_rx.Instance = DMA1_Stream0;
+    hdma_usart3_rx.Init.Request = DMA_REQUEST_USART3_RX;
+    hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
+    hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
+    hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
+    hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+    hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
+    hdma_usart3_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
+    hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+    if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
+    {
+      Error_Handler();
+    }
+
+    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
+
+    /* USART3_TX Init */
+    hdma_usart3_tx.Instance = DMA1_Stream1;
+    hdma_usart3_tx.Init.Request = DMA_REQUEST_USART3_TX;
+    hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
+    hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
+    hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
+    hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+    hdma_usart3_tx.Init.Mode = DMA_NORMAL;
+    hdma_usart3_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
+    hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+    if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
+    {
+      Error_Handler();
+    }
+
+    __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
+
+    /* USART3 interrupt Init */
+    HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
+    HAL_NVIC_EnableIRQ(USART3_IRQn);
+  /* USER CODE BEGIN USART3_MspInit 1 */
+
+  /* USER CODE END USART3_MspInit 1 */
+  }
 }
 }
 
 
 void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
 void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
@@ -133,6 +255,30 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
 
 
   /* USER CODE END USART1_MspDeInit 1 */
   /* USER CODE END USART1_MspDeInit 1 */
   }
   }
+  else if(uartHandle->Instance==USART3)
+  {
+  /* USER CODE BEGIN USART3_MspDeInit 0 */
+
+  /* USER CODE END USART3_MspDeInit 0 */
+    /* Peripheral clock disable */
+    __HAL_RCC_USART3_CLK_DISABLE();
+
+    /**USART3 GPIO Configuration
+    PB10     ------> USART3_TX
+    PB11     ------> USART3_RX
+    */
+    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
+
+    /* USART3 DMA DeInit */
+    HAL_DMA_DeInit(uartHandle->hdmarx);
+    HAL_DMA_DeInit(uartHandle->hdmatx);
+
+    /* USART3 interrupt Deinit */
+    HAL_NVIC_DisableIRQ(USART3_IRQn);
+  /* USER CODE BEGIN USART3_MspDeInit 1 */
+
+  /* USER CODE END USART3_MspDeInit 1 */
+  }
 }
 }
 
 
 /* USER CODE BEGIN 1 */
 /* USER CODE BEGIN 1 */

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 826 - 755
MDK-ARM/STMH7_temperature_control_board.uvprojx


+ 83 - 0
Mid/Int/Mid_pid.h

@@ -0,0 +1,83 @@
+#ifndef MID_PID_H
+#define MID_PID_H
+
+#include "stdint.h"
+
+/* PID通道数量定义 */
+#define PID_CHANNEL_MAX  24
+
+/* PID模式 */
+typedef enum {
+    PID_MODE_NORMAL = 0,    //正常模式
+    PID_MODE_MANUAL,        //手动模式
+    PID_MODE_AUTO           //自动模式未实现
+} pid_mode_t;
+
+/* PID输出限幅 */
+typedef struct {
+    float output_min;       //输出最小值
+    float output_max;       //输出最大值
+} pid_limit_t;
+
+/* PID增益结构 */
+typedef struct {
+    float kp;       //比例
+    float ki;       //积分
+    float kd;       //微分
+} pid_gain_t;
+
+/* PID计算结果 */
+typedef struct {
+    float setpoint;         //目标值
+    float process_value;    //测量值
+    float output;           //输出值
+    float error;            //当前误差值
+    float prev_error;       //上一次的误差
+    float integral;         //积分累加值
+} pid_calc_t;
+
+/* PID通道状态 */
+typedef struct {
+    uint8_t ch;             //通道号
+    uint8_t enable;         //使能标志(1运行、0停止)
+    pid_mode_t mode;        //工作模式
+    pid_gain_t gain;        //增益参数
+    pid_limit_t limit;      //输出限幅
+    pid_calc_t calc;        //计算相关
+    float deadband;         //死区范围
+    float sample_time;      //采样时间(ms 目前未使用)
+    uint32_t last_update;   //上次更新的时间戳(目前未使用)
+} pid_ch_t;
+
+/* PID全局配置 */
+typedef struct {
+    uint8_t total_ch;                       //总通道数
+    pid_ch_t channels[PID_CHANNEL_MAX];     //总定义
+} pid_handle_t;                             //句柄
+
+/* PID状态 */
+typedef enum {
+    PID_STA_OK = 0,         //操作成功
+    PID_STA_ERR_PARAM,      //参数错误
+    PID_STA_ERR_CHANNEL,    //通道号错误
+    PID_STA_ERR_MODE        //模式错误
+} pid_status_t;
+
+/* API */
+void Mid_PID_Init(void);
+void Mid_PID_DeInit(void);
+pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit);
+pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info);
+pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint);
+pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode);
+pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output);
+float Mid_PID_Calc(uint8_t ch, float pv);
+void Mid_PID_UpdateAll(void);
+pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable);
+uint8_t Mid_PID_IsEnabled(uint8_t ch);
+
+/* 执行器控制 */
+void Mid_PID_HeaterControl(uint8_t ch, float output);
+void Mid_PID_FanControl(uint8_t ch, float temp);
+
+#endif

+ 302 - 0
Mid/Src/Mid_pid.c

@@ -0,0 +1,302 @@
+#include "Mid_pid.h"
+#include <string.h>
+#include <stdlib.h>
+
+static pid_handle_t g_pid_handle;
+
+#define PID_SAMPLE_TIME_MS  100
+
+/*-------------------------------------------------*/
+/* PID初始化,直接一次性全部初始化,也可对单通道初始化 */
+/* 参  数: 无                                       */
+/* 返  回值: 无                                     */
+/*-------------------------------------------------*/
+void Mid_PID_Init(void)
+{
+    uint8_t i;
+    memset(&g_pid_handle, 0, sizeof(pid_handle_t));
+    g_pid_handle.total_ch = PID_CHANNEL_MAX;
+
+    for(i = 0; i < PID_CHANNEL_MAX; i++) {
+        g_pid_handle.channels[i].ch = i;
+        g_pid_handle.channels[i].enable = 1;
+        g_pid_handle.channels[i].mode = PID_MODE_NORMAL;
+        g_pid_handle.channels[i].gain.kp = 2.6f;
+        g_pid_handle.channels[i].gain.ki = 0.125f;
+        g_pid_handle.channels[i].gain.kd = 0.04f;
+        g_pid_handle.channels[i].limit.output_min = 0.0f;
+        g_pid_handle.channels[i].limit.output_max = 100.0f;
+        g_pid_handle.channels[i].deadband = 0.0f;
+        g_pid_handle.channels[i].sample_time = PID_SAMPLE_TIME_MS;
+        g_pid_handle.channels[i].last_update = 0;
+    }
+}
+
+/*-------------------------------------------------*/
+/* PID反初始化                                      */
+/* 参  数: 无                                       */
+/* 返  回值: 无                                     */
+/*-------------------------------------------------*/
+void Mid_PID_DeInit(void)
+{
+    memset(&g_pid_handle, 0, sizeof(pid_handle_t));
+}
+
+/*-------------------------------------------------*/
+/* PID设置通道参数                                  */
+/* 参  数ch: 通道号 gain: 增益 limit: 限幅         */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    if(gain != NULL) {
+        g_pid_handle.channels[ch].gain.kp = gain->kp;
+        g_pid_handle.channels[ch].gain.ki = gain->ki;
+        g_pid_handle.channels[ch].gain.kd = gain->kd;
+    }
+
+    if(limit != NULL) {
+        g_pid_handle.channels[ch].limit.output_min = limit->output_min;
+        g_pid_handle.channels[ch].limit.output_max = limit->output_max;
+    }
+
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID获取通道信息                                  */
+/* 参  数ch: 通道号 info: 信息结构体指针            */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    if(info != NULL) {
+        memcpy(info, &g_pid_handle.channels[ch], sizeof(pid_ch_t));
+    }
+
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID设置目标值                                    */
+/* 参  数ch: 通道号 setpoint: 目标值               */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    g_pid_handle.channels[ch].calc.setpoint = setpoint;
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID设置模式                                      */
+/* 参  数ch: 通道号 mode: 模式                     */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    if(mode >= PID_MODE_AUTO) {
+        return PID_STA_ERR_MODE;
+    }
+
+    g_pid_handle.channels[ch].mode = mode;
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID设置手动输出                                  */
+/* 参  数ch: 通道号 output: 输出值                 */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    if(g_pid_handle.channels[ch].mode != PID_MODE_MANUAL) {
+        return PID_STA_ERR_MODE;
+    }
+
+    /* 限幅 */
+    if(output < g_pid_handle.channels[ch].limit.output_min) {
+        output = g_pid_handle.channels[ch].limit.output_min;
+    }
+    if(output > g_pid_handle.channels[ch].limit.output_max) {
+        output = g_pid_handle.channels[ch].limit.output_max;
+    }
+
+    g_pid_handle.channels[ch].calc.output = output;
+
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID计算                                          */
+/* 参  数ch: 通道号 pv: 过程值(反馈值)             */
+/* 返  回值: PID输出值                             */
+/*-------------------------------------------------*/
+float Mid_PID_Calc(uint8_t ch, float pv)
+{
+    pid_ch_t *pch;
+    float error, p_term, i_term, d_term;
+    float output;
+
+    if(ch >= PID_CHANNEL_MAX) {
+        return 0.0f;
+    }
+
+    pch = &g_pid_handle.channels[ch];
+
+    if(!pch->enable) {
+        return 0.0f;
+    }
+
+    if(pch->mode == PID_MODE_MANUAL) {
+        return pch->calc.output;
+    }
+
+    /* 计算误差 */
+    error = pch->calc.setpoint - pv;
+
+    /* 死区处理 */
+    if(pch->deadband > 0.0f) {
+        if((error > 0 && error < pch->deadband) ||
+           (error < 0 && error > -pch->deadband)) {
+            error = 0;
+        }
+    }
+
+    /* PID计算 */
+    p_term = pch->gain.kp * error;
+    i_term = pch->calc.integral;
+    d_term = pch->gain.kd * (error - pch->calc.prev_error);
+    pch->calc.prev_error = error;
+
+    output = p_term + i_term + d_term;
+
+    /* 输出限幅 */
+    if(output < pch->limit.output_min) {
+        output = pch->limit.output_min;
+    }
+    if(output > pch->limit.output_max) {
+        output = pch->limit.output_max;
+    }
+
+    /* 退饱和积分: 仅在输出未饱和时累加积分 */
+    if(output > pch->limit.output_min && output < pch->limit.output_max) {
+        pch->calc.integral += pch->gain.ki * error;
+        if(pch->calc.integral < pch->limit.output_min) {
+            pch->calc.integral = pch->limit.output_min;
+        }
+        if(pch->calc.integral > pch->limit.output_max) {
+            pch->calc.integral = pch->limit.output_max;
+        }
+    }
+
+    /* 更新状态 */
+    pch->calc.process_value = pv;
+    pch->calc.error = error;
+    pch->calc.output = output;
+
+    return output;
+}
+
+/*-------------------------------------------------*/
+/* PID更新所有通道                                  */
+/* 参  数: 无                                       */
+/* 返  回值: 无                                     */
+/*-------------------------------------------------*/
+void Mid_PID_UpdateAll(void)
+{
+    uint8_t i;
+    for(i = 0; i < g_pid_handle.total_ch; i++) {
+        /* 占位 - 实际使用时需要在外部循环调用Mid_PID_Calc */
+    }
+}
+
+/*-------------------------------------------------*/
+/* PID使能/禁用通道                                 */
+/* 参  数ch: 通道号 enable: 1-使能 0-禁用           */
+/* 返  回值: PID状态                               */
+/*-------------------------------------------------*/
+pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return PID_STA_ERR_CHANNEL;
+    }
+
+    g_pid_handle.channels[ch].enable = enable ? 1 : 0;
+
+    if(!enable) {
+        /* 禁用时清除积分和历史 */
+        g_pid_handle.channels[ch].calc.integral = 0;
+        g_pid_handle.channels[ch].calc.prev_error = 0;
+    }
+
+    return PID_STA_OK;
+}
+
+/*-------------------------------------------------*/
+/* PID查询通道是否使能                              */
+/* 参  数ch: 通道号                                */
+/* 返  回值: 1-使能 0-禁用                         */
+/*-------------------------------------------------*/
+uint8_t Mid_PID_IsEnabled(uint8_t ch)
+{
+    if(ch >= PID_CHANNEL_MAX) {
+        return 0;
+    }
+    return g_pid_handle.channels[ch].enable;
+}
+
+
+/*-------------------------------------------------*/
+/* PID输出直接给到加热器功率                        */
+/* 参  数ch: 通道号(0~7) output: PID输出(0~100)    */
+/* 返  回值: 无                                     */
+/*-------------------------------------------------*/
+void Mid_PID_HeaterControl(uint8_t ch, float output)
+{
+    // TODO
+}
+
+/*-------------------------------------------------*/
+/* 根据实测温度阶梯控制风扇转速                        */
+/* 参  数ch: 通道号(0~7) temp: 实测温度             */
+/* 返  回值: 无                                     */
+/*-------------------------------------------------*/
+void Mid_PID_FanControl(uint8_t ch, float temp)
+{
+    uint8_t speed = 0;
+
+    if(temp > 80.0f) {
+        speed = 100;
+    } else if(temp > 75.0f) {
+        speed = 80;
+    } else if(temp > 70.0f) {
+        speed = 50;
+    } else if(temp > 65.0f) {
+        speed = 20;
+    } else {
+        speed = 0;
+    }
+
+    // TODO
+}

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 130 - 69
STMH7_temperature_control_board.ioc


+ 5 - 0
USB_DEVICE/App/usbd_cdc_if.c

@@ -284,6 +284,11 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
   uint8_t result = USBD_OK;
   uint8_t result = USBD_OK;
   /* USER CODE BEGIN 7 */
   /* USER CODE BEGIN 7 */
   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
+  /* 主机未完成枚举/未配置时拒发:避免提前把包发进主机重枚举窗口而丢失,
+     由调用方(Task_Usb_Send)重试,数据保持在流缓冲中不丢 */
+  if (hcdc == NULL || hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED){
+    return USBD_FAIL;
+  }
   if (hcdc->TxState != 0){
   if (hcdc->TxState != 0){
     return USBD_BUSY;
     return USBD_BUSY;
   }
   }

+ 1 - 1
USB_DEVICE/Target/usbd_conf.c

@@ -102,7 +102,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
     __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
     __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
 
 
     /* Peripheral interrupt init */
     /* Peripheral interrupt init */
-    HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
+    HAL_NVIC_SetPriority(OTG_FS_IRQn, 9, 0);
     HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
     HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
   /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */
   /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */