10. USART-中斷串口收發(fā)實驗
10.1 實驗內(nèi)容
通過本實驗主要學習以下內(nèi)容:
? 使用中斷進行串口收發(fā)
10.2 實驗原理
10.2.1 串口寄存器介紹
串口有幾個非常重要的寄存器需要讀者理解。
數(shù)據(jù)接收寄存器(USART_RDATA)
數(shù)據(jù)發(fā)送寄存器(USART_RDATA)
發(fā)送時,除了發(fā)送數(shù)據(jù)寄存器,還有一個移位寄存器,當數(shù)據(jù)寫入數(shù)據(jù)寄存器中,移位寄存器空閑的情況下,數(shù)據(jù)從數(shù)據(jù)寄存器中轉(zhuǎn)移到移位寄存器,移位寄存器按照低bit——高bit的順序?qū)?shù)據(jù)移位到IO口上。
接收時,接收到的數(shù)據(jù)保存在數(shù)據(jù)接收寄存器中,CPU或DMA可以從該寄存器中讀接收到的數(shù)據(jù)。
狀態(tài)寄存器(USART_STAT ?)
我們需要特別理解TBE、TC、RBNE、IDLE、OREE這幾位。
IDLE一般用于串口DMA接受中,DMA接受中,MCU無法知道發(fā)送方的數(shù)據(jù)個數(shù),所以可以通過判斷IDLE位(或IDLE中斷)來判斷發(fā)送方一幀數(shù)據(jù)發(fā)送結(jié)束了。
以上就是串口寄存器的介紹。本實驗就是使用TBE中斷和RBNE中斷來實現(xiàn)中斷收發(fā)數(shù)據(jù),實驗原理是RBNE中斷用來接受數(shù)據(jù),IDLE中斷用于判斷發(fā)送方數(shù)據(jù)結(jié)束,TBE中斷用于發(fā)送數(shù)據(jù)。
10.3 硬件設(shè)計
本實驗使用P1接口的PA9和PA10實現(xiàn)串口功能,硬件設(shè)計請見上一章。
10.4 代碼解析
10.4.1 串口中斷發(fā)送函數(shù)
在driver_uart.c中定義了串口中斷發(fā)送函數(shù):
C
Drv_Err driver_uart_int_transmit(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
__IO uint64_t timeout = driver_tick;
while(uartx->uart_control.Com_Flag.Bits.SendState==1){
if((timeout+UART_TIMEOUT_MS) <= driver_tick) {
uartx->uart_control.Com_Flag.Bits.SendState=0;
return DRV_ERROR;
}
}
uartx->uart_control.Com_Flag.Bits.SendSuccess=0;
uartx->uart_control.Com_Flag.Bits.SendState=1;
uartx->uart_control.p_Send=pbuff;
uartx->uart_control.SendSize=length;
uartx->uart_control.SendCount=0;
usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
usart_interrupt_enable(uartx->uart_x,USART_INT_TBE);
return DRV_SUCCESS;
}
10.4.2 串口中斷接受函數(shù)
在driver_uart.c中定義了串口中斷接受函數(shù):
C
Drv_Err driver_uart_int_receive(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
__IO uint64_t timeout = driver_tick;
while(uartx->uart_control.Com_Flag.Bits.RecState==1){
if((timeout+UART_TIMEOUT_MS) <= driver_tick) {
uartx->uart_control.Com_Flag.Bits.RecState=0;
return DRV_ERROR;
}
}
if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR))
{
usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);
}
uartx->uart_control.Com_Flag.Bits.RecSuccess=0;
uartx->uart_control.Com_Flag.Bits.RecState=1;
uartx->uart_control.p_Rec=pbuff;
uartx->uart_control.RecSize=length;
uartx->uart_control.RecCount=0;
usart_flag_clear(uartx->uart_x,USART_FLAG_IDLE);
usart_interrupt_enable(uartx->uart_x,USART_INT_RBNE);
usart_interrupt_enable(uartx->uart_x,USART_INT_IDLE);
return DRV_SUCCESS;
}
10.4.3 main函數(shù)實現(xiàn)
以下為main函數(shù)代碼:
C
int main(void)
{
//延時、共用驅(qū)動部分初始化
driver_init();
//初始化UART為中斷模式,注冊接受完成(IDLE)回調(diào)函數(shù)
BOARD_UART.uart_mode_tx=MODE_INT;
BOARD_UART.uart_mode_rx=MODE_INT;
BOARD_UART.uart_idle_callback=user_receive_complete_callback;
bsp_uart_init(&BOARD_UART);
bsp_led_init(&LED2);
bsp_led_init(&LED1);
bsp_led_on(&LED2);
bsp_led_off(&LED1);
//使能UART中斷
nvic_irq_enable(USART0_IRQn,2,0);
delay_ms(100);
printf_log("uart interrupt mode sends and receives loopback packets of indefinite length.\r\n");
//啟動UART中斷接受,最長100byte
driver_uart_int_receive(&BOARD_UART,uart_rec_buff,100);
while (1)
{
//查詢到接受完成回調(diào)函數(shù)標志
if(uart_receive_complete_flag==SET)
{
uart_receive_complete_flag=RESET;
//啟動中斷方式發(fā)送剛接受到的數(shù)據(jù)
driver_uart_int_transmit(&BOARD_UART,uart_send_buff,uart_receive_count);
printf_log("\r\n The received data is %s\r\n",uart_send_buff);
memset(uart_send_buff,0,100);
}
}
}
本例程main函數(shù)首先進行了延時函數(shù)初始化,再初始化UART為中斷模式,接著配置串口BOARD_UART,開啟串口中斷NVIC,這里使用到了IDLE中斷,TBE中斷和RBNE中斷,然后配置串口D中斷接受,最長100個字節(jié),所以我們可以給串口發(fā)送100個字節(jié)以下長度的數(shù)據(jù)。在while(1)循環(huán)中循環(huán)查詢uart_receive_complete_flag標志位,當該標志位為“SET”時,表示IDLE中斷被觸發(fā),一幀數(shù)據(jù)接受完,最后將接收到的幀數(shù)據(jù)通過中斷發(fā)送方式原封不動發(fā)送到串口上。
10.4.4 中斷函數(shù)
在bsp_uart.c中定義了串口中斷處理函數(shù)
C
void USART0_IRQHandler(void)
{
driver_uart_int_handler(&BOARD_UART);
}
在driver_uart.c中定義了driver_uart_int_handler函數(shù):
C
Drv_Err driver_uart_int_handler(typdef_uart_struct *uartx)
{
Drv_Err uart_state=DRV_SUCCESS;
if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_RBNE)!=RESET)
{
if(uartx->uart_control.RecCount < uartx->uart_control.RecSize){
uartx->uart_control.p_Rec[uartx->uart_control.RecCount]=usart_data_receive(uartx->uart_x);
uartx->uart_control.RecCount++;
}
else{
usart_data_receive(uartx->uart_x);
uart_state=DRV_ERROR;
//err 溢出
}
if(uartx->uart_rbne_callback!=NULL){
uartx->uart_rbne_callback(uartx);
}
//callback
if(uartx->uart_control.RecCount == uartx->uart_control.RecSize){
uartx->uart_control.Com_Flag.Bits.RecSuccess=1;
uartx->uart_control.Com_Flag.Bits.RecState=0;
uartx->uart_control.RecCount=0;
}
}
if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_IDLE)!=RESET)
{
usart_interrupt_flag_clear(uartx->uart_x,USART_INT_FLAG_IDLE);
if( (uartx->uart_mode_rx==MODE_INT && uartx->uart_control.RecCount>0) \
||(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx)!=uartx->uart_control.RecSize))
{
uartx->uart_control.Com_Flag.Bits.RecSuccess=1;
uartx->uart_control.Com_Flag.Bits.RecState=0;
if(uartx->uart_mode_rx==MODE_DMA){
uartx->uart_control.RecCount=uartx->uart_control.RecSize-dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx);
}
//callback
if(uartx->uart_idle_callback!=NULL){
uartx->uart_idle_callback(uartx);
}
}
}
if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TBE)!=RESET)
{
usart_data_transmit(uartx->uart_x,uartx->uart_control.p_Send[uartx->uart_control.SendCount]);
uartx->uart_control.SendCount++;
if(uartx->uart_tbe_callback!=NULL){
uartx->uart_tbe_callback(uartx);
}
if(uartx->uart_control.SendCount >= uartx->uart_control.SendSize)
{
uartx->uart_control.SendCount=0;
usart_interrupt_disable(uartx->uart_x, USART_INT_TBE);
usart_interrupt_enable(uartx->uart_x, USART_INT_TC);
}
}
if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TC)!=RESET)
{
usart_interrupt_disable(uartx->uart_x, USART_INT_TC);
usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
if( !(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_tx_dma->dmax,uartx->uart_tx_dma->dma_chx)!=0) )
{
uartx->uart_control.Com_Flag.Bits.SendSuccess=1;
uartx->uart_control.Com_Flag.Bits.SendState=0;
if(uartx->uart_tc_callback!=NULL){
uartx->uart_tc_callback(uartx);
}
uartx->uart_control.SendCount=0;
}
}
if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR)==SET)
{
usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);
uart_state=DRV_ERROR;
}
return uart_state;
}
10.5 實驗結(jié)果
使用串口調(diào)試助手發(fā)送一幀數(shù)據(jù)到MCU,MCU會將這幀數(shù)據(jù)回發(fā)到串口調(diào)試助手中。