This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129X UART 中断问题

Other Parts Discussed in Thread: EK-TM4C1294XL, MAX3232

你好,

我测试C:\ti\TivaWare_C_Series-2.1.0.12573\examples\boards\ek-tm4c1294xl\uart_echo下的例子,使用串口调试助手可以正常收发,但是将中断作如下修改后无法再次进入中断,另外发现只有调用发送相关的函数才能再次进入中断,请问如果我只在中断中接受数据应该怎么样做?谢谢

uint8_t temp = 0;
void
UARTIntHandler(void)
{
uint32_t ui32Status;

//
// Get the interrrupt status.
//
ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

//
// Clear the asserted interrupts.
//
ROM_UARTIntClear(UART0_BASE, ui32Status);

//
// Loop while there are characters in the receive FIFO.
//
while(ROM_UARTCharsAvail(UART0_BASE))
{
//
// Read the next character from the UART and write it back to the UART.
//
// ROM_UARTCharPutNonBlocking(UART0_BASE,
// UARTCharGetNonBlocking(UART0_BASE));
temp = UARTCharGetNonBlocking(UART0_BASE);//将接收到的数据放入一个变量
}
}

  • 你是不是没有清接收缓冲区呢,很简单,你读一下接收中断标志,接收缓冲是否已经满了。

  • 看你的描述,像是使能了串口发送中断。可以的话,检查一下你的UART初始化程序。

    谢谢

  • 你好,串口部分初始化代码如下:

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    ROM_UARTConfigSetExpClk(UART0_BASE, ui32SysClock, 115200,
    (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
    UART_CONFIG_PAR_NONE));

    //
    // Enable the UART interrupt.
    //
    ROM_IntEnable(INT_UART0);
    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

    根据代码使能的应该是接收中断和接收超时中断,如果调用一次UARTSend();可以进入一次中断函数,

    不明白为何与UARTSend()有关系?

    谢谢你的指导!


  • 你可以查看一下在中断响应函数里面Status = UARTIntStatus(UART0_BASE, 1);的staus的值,看下是中断标志位,确定接收中断的标志位情况。

    毕竟UART的接收和发送中断的响应函数是一个。

  • Hi, Ken

    如果使能下面这句,可以每次进入中断,获取到的Status = UARTIntStatus(UART0_BASE, 1);为0x0040(即是UART_INT_RT);

    ROM_UARTCharPutNonBlocking(UART0_BASE, UARTCharGetNonBlocking(UART0_BASE));

    如果使用ROM_UARTIntEnable()使能UART_INT_RX或者 UART_INT_TX,从未在中断中判断到这两个中断标志,也没有进入过中断,

    这种情况如何解决,我的目的只是在中断中接收数据保存起来,如下:

    void
    UARTIntHandler(void)
    {
    uint32_t ui32Status;

    //
    // Get the interrrupt status.
    //
    ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    ROM_UARTIntClear(UART0_BASE, ui32Status);

    //
    // Loop while there are characters in the receive FIFO.
    //

    while(ROM_UARTCharsAvail(UART0_BASE))
    {
    temp = UARTCharGetNonBlocking(UART0_BASE);
    }
    }

    如果你身边有板子,麻烦你测试一下这个问题,

    非常感谢!

  • 我手上没有1294的板子,就用123GH6PGE的板子测了一下,程序每次都能进中断,你可以参考一下:

    //*****************************************************************************
    //
    // uart_echo.c - Example for reading data from and writing data to the UART in
    //               an interrupt driven fashion.
    //
    // Copyright (c) 2011-2014 Texas Instruments Incorporated.  All rights reserved.
    // Software License Agreement
    //
    // Texas Instruments (TI) is supplying this software for use solely and
    // exclusively on TI's microcontroller products. The software is owned by
    // TI and/or its suppliers, and is protected under applicable copyright
    // laws. You may not combine this software with "viral" open-source
    // software in order to form a larger program.
    //
    // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
    // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
    // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
    // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
    // DAMAGES, FOR ANY REASON WHATSOEVER.
    //
    // This is part of revision 2.1.0.12573 of the DK-TM4C123G Firmware Package.
    //
    //*****************************************************************************
    
    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "driverlib/rom.h"
    #include "grlib/grlib.h"
    #include "drivers/cfal96x64x16.h"
    
    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>UART Echo (uart_echo)</h1>
    //!
    //! This example application utilizes the UART to echo text.  The first UART
    //! (connected to the USB debug virtual serial port on the evaluation board)
    //! will be configured in 115,200 baud, 8-n-1 mode.  All characters received on
    //! the UART are transmitted back to the UART.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    //*****************************************************************************
    //
    // The UART interrupt handler.
    //
    //*****************************************************************************
    void
    UARTIntHandler(void)
    {
        uint32_t ui32Status;
        uint8_t Temp;
        //
        // Get the interrrupt status.
        //
        ui32Status = ROM_UARTIntStatus(UART0_BASE, true);
    
        //
        // Clear the asserted interrupts.
        //
        ROM_UARTIntClear(UART0_BASE, ui32Status);
    
        //
        // Loop while there are characters in the receive FIFO.
        //
        while(ROM_UARTCharsAvail(UART0_BASE))
        {
            //
            // Read the next character from the UART and write it back to the UART.
            //
       //     ROM_UARTCharPutNonBlocking(UART0_BASE,
       //                                ROM_UARTCharGetNonBlocking(UART0_BASE));
        	Temp = ROM_UARTCharGetNonBlocking(UART0_BASE);
        }
    }
    
    //*****************************************************************************
    //
    // Send a string to the UART.
    //
    //*****************************************************************************
    void
    UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
    {
        //
        // Loop while there are more characters to send.
        //
        while(ui32Count--)
        {
            //
            // Write the next character to the UART.
            //
            ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
        }
    }
    
    //*****************************************************************************
    //
    // This example demonstrates how to send a string of data to the UART.
    //
    //*****************************************************************************
    int
    main(void)
    {
        tRectangle sRect;
        tContext sContext;
    
        //
        // Enable lazy stacking for interrupt handlers.  This allows floating-point
        // instructions to be used within interrupt handlers, but at the expense of
        // extra stack usage.
        //
        ROM_FPULazyStackingEnable();
    
        //
        // Set the clocking to run directly from the crystal.
        //
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                           SYSCTL_XTAL_16MHZ);
    
        //
        // Initialize the display driver.
        //
        CFAL96x64x16Init();
    
        //
        // Initialize the graphics context.
        //
        GrContextInit(&sContext, &g_sCFAL96x64x16);
    
        //
        // Fill the top part of the screen with blue to create the banner.
        //
        sRect.i16XMin = 0;
        sRect.i16YMin = 0;
        sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
        sRect.i16YMax = 9;
        GrContextForegroundSet(&sContext, ClrDarkBlue);
        GrRectFill(&sContext, &sRect);
    
        //
        // Change foreground for white text.
        //
        GrContextForegroundSet(&sContext, ClrWhite);
    
        //
        // Put the application name in the middle of the banner.
        //
        GrContextFontSet(&sContext, g_psFontFixed6x8);
        GrStringDrawCentered(&sContext, "uart-echo", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 4, 0);
    
        //
        // Initialize the display and write some instructions.
        //
        GrStringDrawCentered(&sContext, "Connect a", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 20, false);
        GrStringDrawCentered(&sContext, "terminal", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 30, false);
        GrStringDrawCentered(&sContext, "to UART0.", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 40, false);
        GrStringDrawCentered(&sContext, "115000,N,8,1", -1,
                             GrContextDpyWidthGet(&sContext) / 2, 50, false);
    
        //
        // Enable the peripherals used by this example.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
        //
        // Enable processor interrupts.
        //
        ROM_IntMasterEnable();
    
        //
        // Set GPIO A0 and A1 as UART pins.
        //
        ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
        //
        // Configure the UART for 115,200, 8-N-1 operation.
        //
        ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
                                (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                                 UART_CONFIG_PAR_NONE));
    
        //
        // Enable the UART interrupt.
        //
        ROM_IntEnable(INT_UART0);
        ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
    
        //
        // Prompt for text to be entered.
        //
        UARTSend((uint8_t *)"Enter text: ", 12);
    
        //
        // Loop forever echoing data through the UART.
        //
        while(1)
        {
        }
    }
    
  • Hi,Ken

    谢谢你,我再找找问题,有解决方法我贴出来;如果你拿到129的板子,也麻烦你验证一下!

  • Hi,Ken

    经过测试发现:不是代码的问题,应该是串口电平匹配的问题,我使用板载的Debugger的串口调试是进不了中断的,使用外部的RS232转TTL(max3232)可以正常进入中断,测试过两块TM4C129X Developmet Board都是这样的情况。所以我判断应该是板载的usb转串口电平与MCU IO不匹配,不知你有什么见解?

    谢谢!