我使用的是
STM32L073RZT6 MCU 和 Cube IDE V1.11。使用 .IOC,我在 PB7-6 上实例化了 UART1,启用了全局中断并为 RX 提供了 DMA 服务。在 PC 端,我将 PuTTy 用作嗅探器,将 UART 转 USB 电缆用于 COMS。我在下面提供了配置、PuTTy 输出和代码片段的屏幕截图。每次我向 MCU 传输一个字符时,rx 回调都会运行两次,但是在第二次运行时,rx 缓冲区似乎被重置并传输“无”。虽然我对这种添加的行为感到困惑,但我主要关心的是为什么它会运行两次回调。任何指导将不胜感激。谢谢你。
- /* USER CODE BEGIN PV */
- volatile uint8_t uart_tx_buffer[4];
- volatile uint8_t uart_rx_buffer[4];
- const uint8_t uart_default_msg[8] = {'W', 'A', 'I', 'T', 'I', 'N', 'G', 0x0D};
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_USART1_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- 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_DMA_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- HAL_UART_Receive_DMA(&huart1, uart_rx_buffer, 1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_UART_Transmit(&huart1, uart_default_msg, 8, 100);
- HAL_Delay(10000);
- }
- /* USER CODE END 3 */
- }
- /* USER CODE BEGIN 4 */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
- int i = 0;
- switch(uart_rx_buffer[0]){
- case('p'):
- uart_tx_buffer[0] = 'p';
- uart_tx_buffer[1] = 'u';
- uart_tx_buffer[2] = 'l';
- uart_tx_buffer[3] = 's';
- i++;
- HAL_UART_Transmit(&huart1, uart_tx_buffer, 4, 100);
- break;
- case('h'):
- uart_tx_buffer[0] = 'h';
- uart_tx_buffer[1] = 'e';
- uart_tx_buffer[2] = 'a';
- uart_tx_buffer[3] = 't';
- i++;
- HAL_UART_Transmit(&huart1, uart_tx_buffer, 4, 100);
- break;
- default:
- uart_tx_buffer[0] = 'N';
- uart_tx_buffer[1] = 'o';
- uart_tx_buffer[2] = 'n';
- uart_tx_buffer[3] = 'e';
- HAL_UART_Transmit(&huart1, uart_tx_buffer, 4, 100);
- i++;
- break;
- }
- // HAL_UART_Receive_DMA(&huart1, uart_rx_buffer, 1);
- }
- /* USER CODE END 4 */
.