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.

CCS/EK-TM4C1294XL: Switch interrupt issue on the TM4C1294XL microcontroller - on board switches not working as expected

Part Number: EK-TM4C1294XL

Tool/software: Code Composer Studio

Hi there, 

recently I have been trying to get some switch interrupts working using the on board switches (USR_SW1 and USR_SW2) in PORTJ. As far as I can tell I am correctly setting up the interrupt and it is working. So for example, if I hit a button when running in debug I see the code jump to my interrupt called (IntButtonPress). When my interrupt calls, I have two if statements inside this. 

This seems to be the bit of the code that is malfunctioning. 

Checks that run in my IntButtonPress interrupt: 

if(GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0) == 0x00)  - This if my first check on SW1

if(GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_1) == 0x00)  - This is my second check on SW2 

If I press button "SW1" the code always enters my first check and lights up an LED (see attached code). However if I press "SW2", the interrupt works but will either enter the first check instead of the second check or enter neither. This is not correct as my first if statement is only checking for SW1 where the pin should be high (I programmed the pins to be pull-ups).Even with SW2 pressed, the code never enters my second check for SW2. 

So my question is, why is this happening?  

Cheers, David

Here is my attached code: 

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/flash.h"
#include "driverlib/uart.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"

uint32_t ui32SysClock; // system clock


//*****************************************************************************

//****************************************************************************
//
// System clock rate in Hz.
//
//****************************************************************************
uint32_t g_ui32SysClock;

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// Attempting to use interrupts to trigger LEDs and UART transmission
//
//*****************************************************************************
void
Switch_LED_Setup(void){
     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);        //On-board switches (SW1 and SW2)
     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);        //On-board LEDs (D1 and D2)
     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);        //On-board LEDs (D3 and D4)
     while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOJ) && !SysCtlPeripheralReady(SYSCTL_PERIPH_GPION) && !SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
     {
     }

     GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0);  //Enable SW1 to be configured as input - PORTJ
     HWREG(GPIO_PORTJ_BASE + GPIO_O_PUR) = GPIO_PIN_0;   //Weak Pull up is enabled to detect button press

     GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_1);  //Enable SW2 with same settings
     HWREG(GPIO_PORTJ_BASE + GPIO_O_PUR) = GPIO_PIN_1;


     GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); // D4  - All leds set to output
     GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4); // D3
     GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0); // D2
     GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1); // D1
}
void
UART_transmit(void){

        //
        // Enable UART peripheral
        //

        SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

        //
        // Set GPIO A0 and A1 as UART pins
        //
        GPIOPinConfigure(GPIO_PA0_U0RX);
        GPIOPinConfigure(GPIO_PA1_U0TX);
        GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

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

}
//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void IntButtonPress(void)
{

    GPIOIntClear(GPIO_PORTJ_BASE, GPIO_INT_PIN_0);      //Clear both interrupts
    GPIOIntClear(GPIO_PORTJ_BASE, GPIO_INT_PIN_1);

    if(GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0) == 0x00)           //PTB switch
        {

            /*UARTCharPutNonBlocking(UART0_BASE, 'O');  //Commented this section out for now
            UARTCharPutNonBlocking(UART0_BASE, 'U');
            UARTCharPutNonBlocking(UART0_BASE, 'T');
            UARTCharPutNonBlocking(UART0_BASE, '0');*/
            GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, GPIO_PIN_0);
            SysCtlDelay(16000000u / 3u);                //Provides a small delay
            SysCtlDelay(16000000u / 3u);

        }



    if(GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_1) == 0x00)
        {
//            UARTCharPutNonBlocking(UART0_BASE, 'O'); //Commented this section out for now
//            UARTCharPutNonBlocking(UART0_BASE, 'U');
//            UARTCharPutNonBlocking(UART0_BASE, 'T');
//            UARTCharPutNonBlocking(UART0_BASE, '0');
            GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, GPIO_PIN_1);
            SysCtlDelay(16000000u / 3u);
            SysCtlDelay(16000000u / 3u);

        }

    GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x00);
}

//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int main(void)
{
    Switch_LED_Setup();
    g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                    SYSCTL_OSC_MAIN |
                                                    SYSCTL_USE_PLL |
                                                    SYSCTL_CFG_VCO_480), 120000000);
    IntMasterDisable();         //To ensure interrupt doesn't act early

    GPIOIntTypeSet(GPIO_PORTJ_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_FALLING_EDGE); //Interrupt setup
    IntEnable(INT_GPIOJ);

    GPIOIntEnable(GPIO_PORTJ_BASE, GPIO_INT_PIN_0 | GPIO_INT_PIN_1);

    GPIOIntClear(GPIO_PORTJ_BASE, GPIO_INT_PIN_0);      //Make sure flags are cleared
    GPIOIntClear(GPIO_PORTJ_BASE, GPIO_INT_PIN_1);

    IntMasterEnable();

    while(1)
    {
    }
}