/** ****************************************************************************** * @file usart.h * @brief This file contains all the function prototypes for * the usart.c file ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2024 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __USART_H__ #define __USART_H__ #ifdef __cplusplus extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "main.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ extern UART_HandleTypeDef huart2; /* USER CODE BEGIN Private defines */ extern uint8_t Serial2RxBuffer[1]; /* USER CODE END Private defines */ void MX_USART2_UART_Init(void); /* USER CODE BEGIN Prototypes */ int __io_putchar(int ch); void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart); /* USER CODE END Prototypes */ #ifdef __cplusplus } #endif #endif /* __USART_H__ */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 解釋這段程式ˇ
时间: 2025-08-29 08:50:45 AIGC 浏览: 18
這段程式碼是 STM32 微控制器專案中的一個 `usart.h` 頭文件(header file),主要用於宣告與 USART(Universal Synchronous/Asynchronous Receiver/Transmitter)串列通訊相關的函數、變數與初始化設定。以下為逐段解釋:
---
### 1. 標頭註解(License & Copyright)
```c
/**
******************************************************************************
* @file usart.h
* @brief This file contains all the function prototypes for
* the usart.c file
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2024 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
```
- **用途**:說明此文件為 STM32CubeMX 產生的 USART 驅動頭文件。
- **授權資訊**:採用 BSD-3-Clause 授權條款,允許使用於商業用途,但需保留原始聲明。
---
### 2. 避免重複包含的防護宏
```c
#ifndef __USART_H__
#define __USART_H__
```
- **用途**:防止頭文件被重複 include,避免編譯錯誤。
---
### 3. C++ 兼容性宣告
```c
#ifdef __cplusplus
extern "C" {
#endif
```
- **用途**:如果該頭文件在 C++ 程式中被引用,使用 `extern "C"` 可避免 C++ 的名稱重整(name mangling)造成連結錯誤。
---
### 4. 包含其他頭文件
```c
#include "main.h"
```
- **用途**:引入專案主頭文件 `main.h`,其中包含 HAL(Hardware Abstraction Layer)庫的設定與其他硬體相關定義。
---
### 5. 使用者自訂宣告區(USER CODE BEGIN Includes)
```c
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
```
- **用途**:供開發者自行加入額外的 include 檔案或自定義設定。
---
### 6. 外部變數宣告
```c
extern UART_HandleTypeDef huart2;
```
- **用途**:宣告一個 UART_HandleTypeDef 結構變數 `huart2`,此為 USART2 的控制碼(handle),用於 HAL 函數中管理 USART2 的狀態與設定。
---
### 7. 使用者自訂私有變數宣告(USER CODE BEGIN Private defines)
```c
/* USER CODE BEGIN Private defines */
extern uint8_t Serial2RxBuffer[1];
/* USER CODE END Private defines */
```
- **用途**:宣告一個大小為 1 的接收緩衝區 `Serial2RxBuffer`,通常用於儲存 USART2 接收到的單一字元。
---
### 8. 函數原型宣告
```c
void MX_USART2_UART_Init(void);
```
- **用途**:宣告 USART2 的初始化函數,此函數由 STM32CubeMX 產生,負責設置 USART2 的基本參數(如波特率、資料位、停止位等)。
---
### 9. 使用者自訂函數原型(USER CODE BEGIN Prototypes)
```c
/* USER CODE BEGIN Prototypes */
int __io_putchar(int ch);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
/* USER CODE END Prototypes */
```
- `__io_putchar(int ch)`:
- **用途**:重導 `printf()` 函數輸出至串列埠(USART),讓 `printf()` 的輸出透過 USART 傳送。
- `HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)`:
- **用途**:USART 接收完成的回呼函數,當使用 `HAL_UART_Receive_IT()` 函數接收資料完成時會自動呼叫此函數,用於處理接收資料。
---
### 10. 結束 C++ 兼容性宣告
```c
#ifdef __cplusplus
}
#endif
```
- **用途**:結束 `extern "C"` 的宣告區塊。
---
### 11. 結束防護宏
```c
#endif /* __USART_H__ */
```
- **用途**:結束 `#ifndef __USART_H__` 的條件式編譯。
---
## 總結
此頭文件主要功能如下:
1. 宣告 USART2 的初始化函數。
2. 宣告與 USART2 相關的控制碼與緩衝區。
3. 提供 `printf()` 輸出至串列埠的重導函數。
4. 提供 USART 接收完成的回呼函數原型。
5. 支援 C++ 專案的相容性。
---
###
阅读全文