71 lines
1.8 KiB
C
71 lines
1.8 KiB
C
//
|
|
// Created by dongl on 25-8-27.
|
|
//
|
|
|
|
#include <magnitude/magnitude.h>
|
|
|
|
#include <magnitude/io/io.h>
|
|
#include <magnitude/io/ringbuf.h>
|
|
#include <magnitude/irq/irq.h>
|
|
|
|
#include <serial/bspuart.h>
|
|
#include <irq/bspgic.h>
|
|
|
|
#include <bsp.h>
|
|
|
|
|
|
// 环形缓冲区控制块
|
|
static magnitude_ring_buffer_t mag_print_tx; // 待发送数据
|
|
//static magnitude_ring_buffer_t mag_print_rx; // 待接收数据
|
|
// 环形缓冲区空间
|
|
static char mag_uart_tx[MAGNITUDE_PRINT_BUFFER_SIZE];
|
|
// static char mag_uart_rx[MAGNITUDE_PRINT_BUFFER_SIZE];
|
|
|
|
static void uart_isr_handler ( void *arg ) {
|
|
magnitude_ring_buffer_t *tx_rb = (magnitude_ring_buffer_t *) arg;
|
|
char c;
|
|
|
|
// 检查是否有数据待发送
|
|
if ( magnitude_ring_buffer_get(tx_rb, &c) ) {
|
|
// 将一个字符写入 UART 数据寄存器
|
|
bsp_uart_putc(c);
|
|
}
|
|
|
|
// 清除 UART 中断标志,为下一次中断做准备
|
|
bsp_uart_clear_interrupt();
|
|
}
|
|
|
|
void magnitude_serial_up ( void ) {
|
|
// 初始化环形缓冲区
|
|
magnitude_ring_buffer_create(
|
|
&mag_print_tx,
|
|
mag_uart_tx,
|
|
MAGNITUDE_PRINT_BUFFER_SIZE);
|
|
// 使能串口
|
|
bsp_uart_up();
|
|
// 安装 UART IRQ handler
|
|
magnitude_irq_install_handler(IRQ_UART, uart_isr_handler, &mag_print_tx);
|
|
// 启用中断号
|
|
bsp_gic_enable_irq(IRQ_UART);
|
|
}
|
|
|
|
ssize_t magnitude_putc ( char c ) {
|
|
if ( !magnitude_ring_buffer_put(&mag_print_tx, c) )
|
|
return 0; // 缓冲区满
|
|
|
|
// 如果 FIFO 空,则手动写一个字符,触发中断循环
|
|
if ( bsp_uart_tx_fifo_has_space() ) { // TXFF = 0 表示有空位
|
|
char ch;
|
|
if (magnitude_ring_buffer_get(&mag_print_tx, &ch) == 0) {
|
|
bsp_uart_putc(ch);
|
|
}
|
|
}
|
|
|
|
// 写入环形缓冲区
|
|
return magnitude_ring_buffer_put(&mag_print_tx, c);
|
|
}
|
|
|
|
|
|
|
|
|