mcu_ab568x/userboot240328/app/platform/bsp/bsp_uart.c
2025-05-30 18:03:10 +08:00

105 lines
2.8 KiB
C

#include "include.h"
#define UART_EXAMPLE_EN 0 ///普通uart示例程序
#if UART_EXAMPLE_EN
//u32 uart_buf = 0;
//u8 uart_rx_flag = 0;
///发送数据
AT(.com_text.uart)
void uart1_putc(char ch)
{
while(!(UART1CON & BIT(8)));
UART1DATA = ch;
}
///打印字符串
AT(.com_text.uart)
void uart1_puts(const char *str)
{
while(*str){
uart1_putc(*str++);
}
}
///串口中断处理
AT(.com_text.uart)
void uart1_isr(void)
{
if(UART1CON & BIT(9)){ //接收完成
UART1CPND |= BIT(9);
//需要把数据保存起来
// uart_buf = UART1DATA;
// uart_rx_flag = 1;
}
}
void uart1_rx_printf(void)
{
if(uart_rx_flag) {
printf("\nuart_buf = 0x%X\n", uart_buf);
uart_rx_flag = 0;
}
}
///串口初始化
AT(.text.at_mode)
void uart1_init(u32 baud)
{
u32 baud_cfg;
//PA3_RX,PA4_TX(G2)
GPIOAFEN |= BIT(3)|BIT(4);
GPIOADIR |= BIT(3);
GPIOADIR &= ~BIT(4);
GPIOAPU |= BIT(3)|BIT(4);
GPIOADE |= BIT(3)|BIT(4);
//mapping uart1(tx/rx) -> G2
FUNCMCON0 |= (0xf << 28)|(0xf << 24);
FUNCMCON0 |= (0x2 << 28)|(0x2 << 24);
CLKGAT0 |= BIT(14); //enable uart1 clk
UART1CON = 0;
UART1CON |= BIT(5); //clock src select uart_inc
CLKCON1 |= BIT(14); //uart_inc select x26m_clkdiv2
baud_cfg = ((XOSC_CLK_HZ/2+ baud/2)/baud) - 1; //baud_cfg=(串口时钟/波特率)-1; 四舍五入
UART1BAUD = (baud_cfg << 16)|baud_cfg;
UART1CON |= (BIT(0)|BIT(2)|BIT(7)); //使能uart,接收中断
register_isr(IRQ_UART_VECTOR, uart1_isr);
PICPR &= ~BIT(IRQ_UART_VECTOR);
PICEN |= BIT(IRQ_UART_VECTOR);
}
//vusb作串口收发用
AT(.text.at_mode)
void vusb_uart1_init(u32 baud)
{
u32 baud_cfg;
//VUSB_TX+RX(G3)
PWRCON0 |= BIT(30);
RTCCON &= ~BIT(6);
//单线收发map
FUNCMCON0 |= (0xf << 28) | (0xf << 24);
FUNCMCON0 |= (0x7 << 28) | (0x3 << 24); //单线时RXMAP配0x7
CLKGAT0 |= BIT(21); //enable uart1 clk
UART1CON = 0;
UART1CON |= BIT(5); //clock src select uart_inc
CLKCON1 |= BIT(14); //uart_inc select x26m_clkdiv2
baud_cfg = ((XOSC_CLK_HZ/2+ baud/2)/baud) - 1; //baud_cfg=(串口时钟/波特率)-1; 四舍五入
UART1BAUD = (baud_cfg << 16)|baud_cfg;
UART1CON |= (BIT(0) | BIT(2) | BIT(6) | BIT(7)); //使能uart,接收中断,BIT(6)单线
register_isr(IRQ_UART_VECTOR, uart1_isr);
PICPR &= ~BIT(IRQ_UART_VECTOR);
PICEN |= BIT(IRQ_UART_VECTOR);
}
#endif