-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
74 lines (62 loc) · 1.7 KB
/
example.cpp
File metadata and controls
74 lines (62 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file example.cpp
* @brief Example usage of STM32BufferedSerial class.
*
* This example demonstrates non-blocking UART echo using interrupts.
* It uses the HAL UART interrupt callbacks to manage RX/TX buffering.
*/
#include "STM32BufferedSerial.hpp"
#include "main.h"
// UART handle generated by CubeMX (e.g., huart2)
extern UART_HandleTypeDef huart2;
// Instantiate a buffered serial object (256-byte buffers)
STM32BufferedSerial serial(&huart2, 256);
/**
* @brief Initialize peripherals and serial communication.
*/
void setup() {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
serial.begin();
// Register instance for interrupt callbacks
STM32BufferedSerial::registerInstance(&huart2, &serial);
// Send startup message
const char* msg = "STM32 Buffered Serial Example\r\n";
serial.write(reinterpret_cast<const uint8_t*>(msg), strlen(msg));
}
/**
* @brief Main loop: echo received characters.
*/
void loop() {
if (serial.available()) {
int c = serial.read();
if (c >= 0) {
serial.write((uint8_t)c); // Echo back
}
}
}
/**
* @brief HAL RX complete callback (called by HAL when a byte is received)
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleRxComplete();
}
/**
* @brief HAL TX complete callback (called by HAL when a byte is transmitted)
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart) {
if (auto inst = STM32BufferedSerial::fromHandle(huart))
inst->handleTxComplete();
}
/**
* @brief Main entry point.
*/
int main(void) {
setup();
while (1) {
loop();
}
}