An interrupt-driven, buffered UART class for STM32 HAL
STM32 HAL を用いた割り込み駆動型バッファ付き UART 通信クラス
STM32BufferedSerial provides a non-blocking, interrupt-driven UART interface for STM32 HAL.
It uses circular buffers for both RX and TX, allowing smooth serial communication without blocking the CPU.
Multiple UART instances are supported.
- Interrupt-driven, non-blocking communication
- Circular buffers for RX and TX
- Supports up to 6 UART instances
- Works with HAL UART callbacks (
HAL_UART_RxCpltCallback,HAL_UART_TxCpltCallback) - Automatically re-arms RX to handle HAL busy states
- Drop-in replacement for
HAL_UART_Transmit_IT()/HAL_UART_Receive_IT()
#include "STM32BufferedSerial.hpp"
extern UART_HandleTypeDef huart2;
STM32BufferedSerial serial(&huart2, 256);
void setup() {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
serial.begin();
STM32BufferedSerial::registerInstance(&huart2, &serial);
const char* msg = "STM32 Buffered Serial Example\r\n";
serial.write(reinterpret_cast<const uint8_t*>(msg), strlen(msg));
}
void loop() {
if (serial.available()) {
int c = serial.read();
serial.write((uint8_t)c); // Echo back
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleRxComplete();
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleTxComplete();
}-
Call
STM32BufferedSerial::registerInstance()afterMX_USARTx_UART_Init(). -
Implement
HAL_UART_RxCpltCallback()andHAL_UART_TxCpltCallback()as shown. -
RX interrupt automatically restarts internally.
-
Sending data:
serial.write((uint8_t*)"Hello\r\n", 7);
-
Reading data:
if (serial.available()) { int c = serial.read(); }
-
Copy
STM32BufferedSerial.hppandexample.cppinto your project (e.g.,Core/IncandCore/Src). -
Include the header in your source file:
#include "STM32BufferedSerial.hpp"
-
Ensure the following HAL headers are available:
stm32f4xx_hal.husart.hmain.h
shoyo Date: 2025-10-14
MIT License (or another open-source license of your choice)
STM32CubeIDE 1.19.0 NUCLEO-F446RE
STM32BufferedSerial は、STM32 HAL を利用した割り込み駆動型の UART 通信クラスです。
リングバッファ(循環バッファ)を使用することで、CPU をブロックせずに安定したシリアル通信を実現します。
複数の UART インスタンスを同時に扱うことができます。
- 割り込み駆動による非ブロッキング通信
- RX / TX 両方にリングバッファを採用
- 最大 6 個の UART インスタンスに対応
- HAL の UART コールバック関数(
HAL_UART_RxCpltCallback,HAL_UART_TxCpltCallback)に対応 - HAL の busy 状態を安全に回避して自動で受信再開
HAL_UART_Transmit_IT()/HAL_UART_Receive_IT()の代替として利用可能
#include "STM32BufferedSerial.hpp"
extern UART_HandleTypeDef huart2;
// UART2 を使用するインスタンス(バッファサイズ 256 バイト)
STM32BufferedSerial serial(&huart2, 256);
void setup() {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
serial.begin();
STM32BufferedSerial::registerInstance(&huart2, &serial);
const char* msg = "STM32 Buffered Serial Example\r\n";
serial.write(reinterpret_cast<const uint8_t*>(msg), strlen(msg));
}
void loop() {
if (serial.available()) {
int c = serial.read();
serial.write((uint8_t)c); // エコーバック
}
}
// 受信完了コールバック
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleRxComplete();
}
// 送信完了コールバック
void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleTxComplete();
}-
MX_USARTx_UART_Init()の 後にSTM32BufferedSerial::registerInstance()を呼び出してください。 -
HAL のコールバック関数内で
handleRxComplete()/handleTxComplete()を呼び出します。 -
RX 割り込みは内部で自動的に再開されます。
-
データ送信例:
serial.write((uint8_t*)"Hello\r\n", 7);
-
データ受信例:
if (serial.available()) { int c = serial.read(); }
-
STM32BufferedSerial.hppとexample.cppを プロジェクト内(例:Core/Inc,Core/Src)にコピーします。 -
使用ファイル内でヘッダをインクルード:
#include "STM32BufferedSerial.hpp"
-
以下の HAL 関連ヘッダが含まれていることを確認:
stm32f4xx_hal.husart.hmain.h
shoyo 作成日: 2025-10-14
MIT ライセンス(または任意のオープンソースライセンス)
このクラスは、STM32 の UART 通信をより柔軟かつ安全に扱うための軽量ライブラリです。 割り込みによる処理分離で、リアルタイムタスクと UART 通信を両立させることが可能です。
STM32CubeIDE 1.19.0 NUCLEO-F446RE