打印
[ARM入门]

串口接收中断

[复制链接]
1811|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小白咚|  楼主 | 2018-4-24 21:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一个串口接收中断程序,运行后还没发送数据就一直不停的进入中断,主程序和中断服务程序如下..
//*****************************************************************************
//
// UART1 Interrupt Service Routine
//
//*****************************************************************************
void
am_uart1_isr(void)
{
    uint32_t status;
    uint32_t rxSize, txSize;

    status = am_hal_uart_int_status_get(1, false);

    am_hal_uart_int_clear(1, status);               

    if (status & (AM_HAL_UART_INT_RX_TMOUT | AM_HAL_UART_INT_TX | AM_HAL_UART_INT_RX))
    {
        am_hal_uart_service_buffered_timeout_save(1, status);
    }

    if (status & (AM_HAL_UART_INT_RX_TMOUT))
    {
        g_ui8RxTimeoutFlag = 1;
        am_util_stdio_printf("\nTMOUT, ");
    }

    am_hal_uart_get_status_buffered(1, &rxSize, &txSize);

    if (status & (AM_HAL_UART_INT_RX))
    {
        am_util_stdio_printf(", RX ");
    }

    if (status & (AM_HAL_UART_INT_TXCMP))
    {
        am_util_stdio_printf(", TXCMP");
    }

    am_hal_uart_get_status_buffered(1, &rxSize, &txSize);

    if (rxSize >= UART_BUFFER_SIZE / 2)
    {
        g_ui8BufferFullFlag = 1;
        am_util_stdio_printf(", RXF");
    }
}


//*****************************************************************************
//
// Main function.
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32Module = AM_BSP_UART_PRINT_INST;
    char pcTempBuf[UART_BUFFER_SIZE + 1];
    uint32_t ui32Length;

    //
    // Set the clock frequency.
    //
    am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);

    //
    // Set the default cache configuration
    //
    am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);

    //
    // Configure the board for low power operation.
    //
    am_bsp_low_power_init();

//    //
//    // Initialize the printf interface for UART output.
//    //
    am_util_stdio_printf_init((am_util_stdio_print_char_t) am_bsp_itm_string_print);
    am_bsp_pin_enable(ITM_SWO);
    am_bsp_debug_printf_enable();
    am_hal_itm_enable();
    am_util_stdio_printf_init((am_util_stdio_print_char_t)am_bsp_uart_string_print);



    //
    // Initialize and Enable the UART.
    //
    uart_init(ui32Module);
    uart_enable(ui32Module);

    am_hal_interrupt_master_enable();

    //
    // Print the banner.
    //
    am_util_stdio_printf("UART FIFO Example\n");
    am_util_stdio_printf("Note - the UART terminal operates at 115,200 BAUD, 8 bit, no parity.\n");
    uart_transmit_delay(ui32Module);
    am_util_stdio_printf("\n\tEcho back the UART received data.\n");
    uart_transmit_delay(ui32Module);
    am_util_stdio_printf("\t");
    uart_transmit_delay(ui32Module);

    //
    // Loop forever writing chars to the stimulus register.
    //
    while (1)
    {
        //
        // Need to retrieve and echo received date for loopback
        // if either all the data has been received, or if we are in danger
        // of overflowing the receive buffer
        //
        if (g_ui8RxTimeoutFlag || g_ui8BufferFullFlag)
        {

            g_ui8RxTimeoutFlag = 0;
            g_ui8BufferFullFlag = 0;

            memset(pcTempBuf, 0x00, sizeof(pcTempBuf));

            ui32Length = am_hal_uart_char_receive_buffered(ui32Module, pcTempBuf, UART_BUFFER_SIZE);

            if (ui32Length)
            {
                if ( pcTempBuf[0] == '\n' )
                {
                    am_util_stdio_printf("START_TX (char='\\n')");
                }
                else
                {
                    am_util_stdio_printf("START_TX (char='%c')", pcTempBuf[0]);
                }

                //
                // We have overallocated the pcTempBuf, to make sure ui32Length==UART_BUFFER_SIZE is ok
                //
                pcTempBuf[ui32Length] = 0;
                am_hal_uart_string_transmit_buffered(ui32Module, pcTempBuf);
            }
        }
    }
}

相关帖子

沙发
ayb_ice| | 2018-4-25 08:45 | 只看该作者
应该是有标志没有清除

使用特权

评论回复
板凳
669686lf| | 2018-5-18 11:25 | 只看该作者
我的通讯也不正常

使用特权

评论回复
地板
kingkits| | 2018-5-21 13:05 | 只看该作者
懂得串口通讯的都知道:
串口中断里不应有printf函数(除非有极特殊的处理方法)
另外,如果打开TXE中断,在没有数据传送时,应该及时关闭TXE

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

38

帖子

0

粉丝