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.

RTOS/TM4C123GE6PM: How to trigger a GPIO interrupt and run a function

Part Number: TM4C123GE6PM
Other Parts Discussed in Thread: EK-TM4C123GXL

Tool/software: TI-RTOS

Hello,

I have some questions about how to run a function after triggering a GPIO interrupt. I have gone through the GPIO interrupt example and I had some issues modifying it to work with what I want to do.

  • Task: Measure the time it takes for a GPIO to go from "high" to "low". When GPIO is "low", trigger an interrupt to runs a function that records that time (in ticks) down.

Below is what I wrote in order to achieve to task above. The code was able to run but immediately exited (infinite while loop doing nothing). I was hoping if someone could explain to me where I went wrong and how to address it.

=====Code======

int main (void)

{

//assume all peripherals and basic initial functions have been called

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //setup

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4); //set to output to drive pin high later

/* My sensor to connected to PF4 and I want it to flag an interrupt when it detects a "low" in the pin */

GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_LOW_LEVEL);

/* Tells RTOS which function to go to when interrupt occurs, I think this where I am having issues since the first parameter should be an int index number in an array */
GPIO_setCallback(GPIO_PIN_4, lightSensorCalculation);

/* Enable interrupts; same issue here */
GPIO_enableInt(GPIO_PIN_4);


GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x00000010); //set pin to high (I think, but was not able to verify in datasheet)
SysTickPeriodSet(1000); //setup for "tick counter"
SysTickEnable();
SysCtlDelay(100); //delay to give GPIO time to be set to high
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4); //now set to read the pin

/* Start BIOS */
BIOS_start();

}

/* I want this function to run when interrupt is triggered when the GPIO detects a "low" */

void lightSensorCalculation(void)

{

uint32_t lightSensorValue = 0;

lightSensorValue = SysTickValueGet(); //get number of ticks since setting GPIO to high

UARTprintf("%d\n", lightSensorValue); //print to PuTTy

HWREG(NVIC_ST_CURRENT) = 1; //reset tick count

SysTickDisable(); //disable tick function to prevent it from counting again
}

===End of Code===

I have also attached my entire file for those that want to see everything. Thank you in advance for your help.

/cfs-file/__key/communityserver-discussions-components-files/908/light_5F00_sensor_5F00_demo.txt

  • Hi Henry,
    Shouldn't you configure PF4 as input pin first using GPIOPinTypeGPIOInput instead and provide a high->low input on the pin for the GPIO to generate an interrupt?
  • Hi Charles, thank you for responding so quickly.

    I'm not sure I am following you. I should clarify why I thought it made sense to go from output to input.

    So the idea was that I would make the pin output and drive high to my light reflectance sensor. I would then switch the pin to input so the Tiva-C can read it and detect when it will be low and trigger an interrupt.

    The method I am using was recommended by the sensor's manufacturer. The 4-step instruction is under "Interfacing the QTR-1RC output to a digital I/O line" www.pololu.com/.../2459
  • Hi Henry,
    If you are configuring the pin as a push-pull then your external device needs to drive the input. Otherwise, the pin is left floating. Can you monitor the PF4 on the scope?
  • Not at this time, however this is a class project and the method has been implemented before with the same components so I am confident that the idea works, it's just my code that's causing the issue. Would you suggest implementing this in a different way?

    I plan to revisit the RTOS workshop labs again, but I don't recall them using any GPIO interrupts.
  • If your project is simple I will suggest you start with a non-RTOS project. There is non-RTOS GPIO interrupt example in TivaWare library.
  • I think that would be ideal for the time being given my time constraint. Would I be able to find this on the resource explorer? I'm not sure what you mean by TivaWare library.
  • YOu can download the TivaWare library from this link.

    Once you install the TivaWare library you can find many examples under <TivaWare_installation>\examples\boards\ek-tm4c123gxl. There is an interrupts example under the directory where the GPIO pins are used to configured for interrupt generation.

    While not a TI-RTOS expert, I think your existing code may have problem with the GPIO_setCallback() on how you associate the callback function with the pin. When you use  GPIO_setCallback(), you are supposed to specify the index number for the first parameter to the callback function array. Take a look at the GPIO Interrupt example that you can download as shown below. In this example, GPIO_setCallback(Board_BUTTON0, gpioButtonFxn0) is used to register the callback gpioButtonFxn0 with Board_BUTTON0. And Board_BUTTON0 is an index 0 to the callback function array. Check the EK_TM4C123GXL_GPIOName in EK_TM4C123GXL.h file where Board_BUTTON0 which is equal to EK_TM4C123GXL_SW1 is the first element of the arrary. You can try to #define GPIO_PIN_4 EK_TM4C123GXL_SW1 and see if that will work. I think there is some learning curve on TI-RTOS. If you can do away with RTOS for your project I will suggest you do your project without it to get the basics working first. I'm learning TI-RTOS myself too. 

  • Ah, yes this was the example that I was referencing initially; however, I did not think of swapping out Board_BUTTON0 for GPIO_PIN_4 EK. Thank you for that suggestion.

    And just to be clear, is the image that you are referencing the TivaWare example or the RTOS example on resource explorer? Because I thought that GPIO Interrupt example uses RTOS.

    Thank you
  • The image is for the TI-RTOS GPIO Interrupt example. As mentioned you should download the TivaWare library from http://www.ti.com/tool/SW-TM4C. Under TivaWare you can find all the non-TIRTOS examples.
  • Thank you for your assistance, I will download those and look through them!