AC98N sdk 增加一路串口
📎 原始文档:https://www.kdocs.cn/l/cuKBTik5kdkJ AC98N sdk 增加一路串口
说明:
SDK默认使用串口0和耳机通讯、使用串口1作为debug打印,两个串口均被占用。
若应用场景中需要额外多一路串口,可以把与耳机通讯的串口和打印的串口复用起来,下面介绍改法。
修改:
- 在uart.c里面添加串口1的驱动,并根据自己的应用初始化、调用发送接口。
#define TCFG_UART1_BAUD 115200L
#define UART1_RX_IO IO_PORT_1_3
#define UART1_TX_IO IO_PORT_1_4
#define UART1_PACKET_SIZE 11 // 可根据自己协议包长度自己配置,大于一帧的长度即可
idata u8 uart1_buffer[UART1_PACKET_SIZE];
// 使用默认的UART1的B组口, rx-P13, tx-P14
void uart1_rx_io_init(void)
{
gpio_set_dir(1, 3, 1);
gpio_set_die(1, 3, 1);
gpio_set_pull_up(1, 3, 1);
gpio_set_pull_down(1, 3, 0);
gpio_set_hd(1, 3, 0);
}
void uart1_tx_io_init(void)
{
gpio_set_dir(1, 4, 0);
gpio_set_die(1, 4, 1);
gpio_set_pull_up(1, 4, 0);
gpio_set_pull_down(1, 4, 0);
gpio_set_hd(1, 4, 0);
}
void uart1_init(void)
{
// log_puts("uart1\n");
UART1_IO_EN();
UART1_IOS(UART1_IOS_P14_P13); //B group
UART1_BAUD = (UART_BAUD_DIVS3_SRC(TCFG_UART1_BAUD) & 0xff00) >> 8;
UART1_BAUD = (UART_BAUD_DIVS3_SRC(TCFG_UART1_BAUD) & 0x00ff) >> 0;
UART1_CON1 = BIT(4) | BIT(5);
UART1_CON0 = BIT(4) | BIT(0) | BIT(3);
uart1_rx_io_init();
uart1_tx_io_init();
UART1_IE(1);
UART1_IP(1);
UART1_BUF = 0;
}
void uart1_close()
{
// log_puts("uart1_close\n");
UART1_IE(0);
UART1_CON0 = 0;
UART1_CON1 = BIT(5) | BIT(4);
}
void uart1_write(u8 idata *buf, u8 len)
{
u8 i;
// log_puts("uart1_write\n");
for (i = 0; i < len; i++) {
UART1_BUF = buf[i];
while (!(UART1_CON1 & 0x80)){
}
UART1_CON1 |= BIT(5);
}
}
data u8 uart1_rx_cnt = 0;
u8 uart1_rx_packet_over = 0;
#define UART1_MARK0 0x55 //自己配置个魔数,用于帧判断
#define UART1_MARK1 0xAA
static void uart1_recv_data(u8 data_) //isr
{
if(uart1_rx_packet_over == 0) {
if (uart1_rx_cnt == 0) {
if ((data_ == UART1_MARK0)) {
uart1_buffer[0] = data_;
uart1_rx_cnt++;
}
} else if (uart1_rx_cnt == 1) {
if (data_ == UART1_MARK1) {
uart1_buffer[1] = data_;
uart1_rx_cnt++;
}
} else {
uart1_buffer[uart1_rx_cnt] = data_;
uart1_rx_cnt++;
if(uart1_rx_cnt == uart1_buffer[4] + 5) {
uart1_rx_cnt = 0;
uart1_rx_packet_over = 1;
} else {
}
}
return;
}
__redo_recv:
uart1_rx_cnt = 0;
return;
}
void uart1_isr(void) interrupt UART1_INT
{
UART1_CON1 |= BIT(4);
// log_putchar('/'); //中断里不能加打印,调试可以打开putchar,调试完记得关闭
uart1_recv_data(UART1_BUF);
}
- UART0复用打印与和耳机通讯 app_config.h
#define TCFG_DEBUG_USED_UARTX 0 //0=使用UART0打印, 1=使用UART1打印
chargebox_comm.c
分别在enter_hook()和exit_hook()函数最后加上uart_uninit()和uart_init()。
//进入通信时,需要关闭的一些模块
static void enter_hook(u8 chl)
{
vout_pwr_ctrl(chl, 0);
#if TCFG_HALL_ENABLE
//防止信号串扰 在耳机通信开耳机上拉。
if (vout_inserdet_en_get(EAR_L)) {
gpio_set_pull_up(EAR_GROUP_L, EAR_PORT_L, 1);
}
if (vout_inserdet_en_get(EAR_R)) {
gpio_set_pull_up(EAR_GROUP_R, EAR_PORT_R, 1);
}
#else
gpio_set_pull_up(EAR_GROUP_L, EAR_PORT_L, 1);
gpio_set_pull_up(EAR_GROUP_R, EAR_PORT_R, 1);
#endif
#if TCFG_DEBUG_ENABLE
uart_uninit();
#endif
}
//退出通信时,需要恢复的一些模块
static void exit_hook(u8 chl)
{
if (ear_info[chl].charge && (!discharge_sw_check())) {
vout_pwr_ctrl(chl, 1);
}
//关闭上拉
gpio_set_pull_up(EAR_GROUP_L, EAR_PORT_L, 0);
gpio_set_pull_up(EAR_GROUP_R, EAR_PORT_R, 0);
#if TCFG_DEBUG_ENABLE
uart_init();
#endif
}