SDK中给出demo基本上都是UART1的demo,那么要用到UART2怎么办?
代码请看:
#if 1 ///200203 JIM UART2 DRIVER FOR JL
void user_uart_write(char a)
{
JL_UART2->BUF = a;
__asm__ volatile("csync");
while((JL_UART2->CON & BIT(15)) == 0);//TX IDLE
}
void user_uart_err(void)
{
user_uart_write(0);
}
void user_uart_nocmd(void)
{
user_uart_write(1);
}
void user_uart_ok(void)
{
user_uart_write(2);
}
void user_uart_send_init(void)
{
user_uart_write('O');
user_uart_write('K');
}
void user_common_uart_isr(u8 uto_buf, void *p, u8 isr_flag)
{
if (UART_ISR_TYPE_DATA_COME == isr_flag) {
switch (uto_buf)
{
case 0x00:///播放暂停
task_post_msg(NULL, 1, MSG_BT_PP);
user_uart_ok();
break;
case 0x01:///上一曲
task_post_msg(NULL, 1, MSG_BT_PREV_FILE);
user_uart_ok();
break;
case 0x02:///下一曲
task_post_msg(NULL, 1, MSG_BT_NEXT_FILE);
user_uart_ok();
break;
case 0x03:///音量加
task_post_msg(NULL, 1, MSG_VOL_UP);
user_uart_ok();
break;
case 0x04:///音量减
task_post_msg(NULL, 1, MSG_VOL_DOWN);
user_uart_ok();
break;
default:
user_uart_nocmd();
break;
}
}
}
static s32 user_test_uart_init(u32 baud)
{
u32 status = 0;
JL_IOMAP->CON1 |= BIT(15) | BIT(14);
JL_PORTC->OUT |= BIT(4) ;
JL_PORTC->DIR |= BIT(5) ;
JL_PORTC->DIR &= ~BIT(4) ;
JL_UART2->BAUD = (UART_CLK / baud) / 4 - 1;
uart_info[2].callback_fun = user_common_uart_isr;
IRQ_REQUEST(IRQ_UART2_IDX, uart2_isr_fun);
JL_UART2->CON = BIT(13) | BIT(12) | BIT(10) | BIT(5) | BIT(7) | BIT(3) | BIT(0);
user_uart_send_init();
return 0;
}
#endif
找个地方调用 user_test_uart_init即可。user_uart_write是串口发送一个字节,user_common_uart_isr是串口接收中断,当串口接收到数据时会进入串口接收中断,每收到一个字节中断一次。
2020.06.11更新 :
朋友反馈UART2在蓝牙播放歌曲时,中断接收数据会丢字节,实测确实如此,有知道如何修改的朋友望不吝赐教,多谢。