AI工具写的环形缓冲区

This commit is contained in:
2025-08-11 20:11:46 +08:00
parent c6c90800d4
commit 4a488429bf
13 changed files with 470 additions and 154 deletions

34
Src/uart_ring_buffer.c Normal file
View File

@@ -0,0 +1,34 @@
#include "uart_ring_buffer.h"
static volatile uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
static volatile uint16_t uart_rx_write = 0;
static volatile uint16_t uart_rx_read = 0;
void uart_ring_buffer_init(void) {
uart_rx_write = 0;
uart_rx_read = 0;
}
uint16_t uart_rx_available(void) {
return (uart_rx_write + UART_RX_BUFFER_SIZE - uart_rx_read) % UART_RX_BUFFER_SIZE;
}
int uart_rx_get(void) {
if (uart_rx_read == uart_rx_write) return -1; // 空
uint8_t data = uart_rx_buffer[uart_rx_read];
uart_rx_read = (uart_rx_read + 1) % UART_RX_BUFFER_SIZE;
return data;
}
void uart_rx_put(uint8_t data) {
uint16_t next = (uart_rx_write + 1) % UART_RX_BUFFER_SIZE;
if (next != uart_rx_read) { // 缓冲区未满
uart_rx_buffer[uart_rx_write] = data;
uart_rx_write = next;
}
}
void uart_rx_clear(void) {
uart_rx_write = 0;
uart_rx_read = 0;
}