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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| #include <stdio.h> #include <stdint.h> #include <string.h> #include <stdbool.h>
typedef enum { UART_BAUD_9600 = 9600, UART_BAUD_115200 = 115200, UART_BAUD_230400 = 230400 } uart_baudrate_t;
typedef enum { UART_DATABITS_8 = 8, UART_DATABITS_9 = 9 } uart_databits_t;
typedef enum { UART_PARITY_NONE, UART_PARITY_EVEN, UART_PARITY_ODD } uart_parity_t;
typedef enum { UART_STOPBITS_1, UART_STOPBITS_2 } uart_stopbits_t;
typedef struct { uint8_t uart_id; uart_baudrate_t baudrate; uart_databits_t databits; uart_parity_t parity; uart_stopbits_t stopbits; bool flow_control; } uart_config_t;
typedef struct { bool (*init)(const uart_config_t* config); bool (*send_byte)(uint8_t uart_id, uint8_t data); bool (*send_data)(uint8_t uart_id, const uint8_t* data, size_t length); bool (*receive_byte)(uint8_t uart_id, uint8_t* data, uint32_t timeout_ms); size_t (*receive_data)(uint8_t uart_id, uint8_t* buffer, size_t max_length, uint32_t timeout_ms); bool (*is_tx_complete)(uint8_t uart_id); size_t (*get_rx_count)(uint8_t uart_id); } uart_hal_t;
#define UART_BUFFER_SIZE 256 static uint8_t uart_tx_buffer[4][UART_BUFFER_SIZE]; static uint8_t uart_rx_buffer[4][UART_BUFFER_SIZE]; static size_t uart_tx_count[4] = {0}; static size_t uart_rx_count[4] = {0};
bool stm32_uart_init(const uart_config_t* config) { printf("[STM32] 初始化UART%d:\n", config->uart_id); printf(" 波特率: %d\n", config->baudrate); printf(" 数据位: %d\n", config->databits); printf(" 校验位: %s\n", config->parity == UART_PARITY_NONE ? "无" : config->parity == UART_PARITY_EVEN ? "偶" : "奇"); printf(" 停止位: %d\n", config->stopbits + 1); printf(" 流控制: %s\n", config->flow_control ? "启用" : "禁用"); uart_tx_count[config->uart_id] = 0; uart_rx_count[config->uart_id] = 0; return true; }
bool stm32_uart_send_byte(uint8_t uart_id, uint8_t data) { if (uart_tx_count[uart_id] < UART_BUFFER_SIZE) { uart_tx_buffer[uart_id][uart_tx_count[uart_id]++] = data; printf("[STM32] UART%d发送: 0x%02X ('%c')\n", uart_id, data, (data >= 32 && data < 127) ? data : '.'); return true; } return false; }
bool stm32_uart_send_data(uint8_t uart_id, const uint8_t* data, size_t length) { printf("[STM32] UART%d发送数据 (%zu字节): ", uart_id, length); for (size_t i = 0; i < length; i++) { if (stm32_uart_send_byte(uart_id, data[i])) { printf("%02X ", data[i]); } else { printf("\n[STM32] 发送缓冲区满\n"); return false; } } printf("\n"); return true; }
bool stm32_uart_receive_byte(uint8_t uart_id, uint8_t* data, uint32_t timeout_ms) { if (uart_rx_count[uart_id] > 0) { *data = uart_rx_buffer[uart_id][--uart_rx_count[uart_id]]; printf("[STM32] UART%d接收: 0x%02X\n", uart_id, *data); return true; } printf("[STM32] UART%d接收超时\n", uart_id); return false; }
size_t stm32_uart_receive_data(uint8_t uart_id, uint8_t* buffer, size_t max_length, uint32_t timeout_ms) { size_t received = 0; printf("[STM32] UART%d接收数据 (最大%zu字节)\n", uart_id, max_length); const char* test_data = "Hello"; size_t test_len = strlen(test_data); size_t copy_len = (test_len < max_length) ? test_len : max_length; memcpy(buffer, test_data, copy_len); received = copy_len; printf("[STM32] 实际接收: %zu字节\n", received); return received; }
bool stm32_uart_is_tx_complete(uint8_t uart_id) { return true; }
size_t stm32_uart_get_rx_count(uint8_t uart_id) { return uart_rx_count[uart_id]; }
bool arduino_uart_init(const uart_config_t* config) { printf("[Arduino] Serial.begin(%d)\n", config->baudrate); return true; }
bool arduino_uart_send_byte(uint8_t uart_id, uint8_t data) { printf("[Arduino] Serial.write(0x%02X)\n", data); return true; }
bool arduino_uart_send_data(uint8_t uart_id, const uint8_t* data, size_t length) { printf("[Arduino] Serial.write(data, %zu)\n", length); return true; }
bool arduino_uart_receive_byte(uint8_t uart_id, uint8_t* data, uint32_t timeout_ms) { printf("[Arduino] Serial.read()\n"); *data = 'A'; return true; }
size_t arduino_uart_receive_data(uint8_t uart_id, uint8_t* buffer, size_t max_length, uint32_t timeout_ms) { printf("[Arduino] Serial.readBytes(buffer, %zu)\n", max_length); return 0; }
bool arduino_uart_is_tx_complete(uint8_t uart_id) { return true; }
size_t arduino_uart_get_rx_count(uint8_t uart_id) { return 0; }
uart_hal_t stm32_uart_hal = { .init = stm32_uart_init, .send_byte = stm32_uart_send_byte, .send_data = stm32_uart_send_data, .receive_byte = stm32_uart_receive_byte, .receive_data = stm32_uart_receive_data, .is_tx_complete = stm32_uart_is_tx_complete, .get_rx_count = stm32_uart_get_rx_count };
uart_hal_t arduino_uart_hal = { .init = arduino_uart_init, .send_byte = arduino_uart_send_byte, .send_data = arduino_uart_send_data, .receive_byte = arduino_uart_receive_byte, .receive_data = arduino_uart_receive_data, .is_tx_complete = arduino_uart_is_tx_complete, .get_rx_count = arduino_uart_get_rx_count };
typedef struct { uint8_t uart_id; uart_hal_t* hal; uart_config_t config; } serial_port_t;
bool serial_init(serial_port_t* port, uint8_t uart_id, uart_baudrate_t baudrate, uart_hal_t* hal) { port->uart_id = uart_id; port->hal = hal; port->config.uart_id = uart_id; port->config.baudrate = baudrate; port->config.databits = UART_DATABITS_8; port->config.parity = UART_PARITY_NONE; port->config.stopbits = UART_STOPBITS_1; port->config.flow_control = false; return hal->init(&port->config); }
bool serial_print(serial_port_t* port, const char* str) { return port->hal->send_data(port->uart_id, (const uint8_t*)str, strlen(str)); }
bool serial_println(serial_port_t* port, const char* str) { bool result = serial_print(port, str); result &= port->hal->send_data(port->uart_id, (const uint8_t*)"\r\n", 2); return result; }
size_t serial_read_line(serial_port_t* port, char* buffer, size_t max_length, uint32_t timeout_ms) { return port->hal->receive_data(port->uart_id, (uint8_t*)buffer, max_length - 1, timeout_ms); }
void uart_hal_demo() { printf("=== 串口通信抽象层演示 ===\n"); printf("\nSTM32串口通信:\n"); serial_port_t stm32_serial; serial_init(&stm32_serial, 1, UART_BAUD_115200, &stm32_uart_hal); serial_println(&stm32_serial, "Hello from STM32!"); serial_print(&stm32_serial, "Temperature: "); serial_println(&stm32_serial, "25.6C"); char rx_buffer[64]; size_t received = serial_read_line(&stm32_serial, rx_buffer, sizeof(rx_buffer), 1000); if (received > 0) { rx_buffer[received] = '\0'; printf("接收到: %s\n", rx_buffer); } printf("\nArduino串口通信:\n"); serial_port_t arduino_serial; serial_init(&arduino_serial, 0, UART_BAUD_9600, &arduino_uart_hal); serial_println(&arduino_serial, "Hello from Arduino!"); printf("\n"); }
int main() { uart_hal_demo(); return 0; }
|