frdmk28f - multiple GPIO interupt

cancel
Showing results for 
Search instead for 
Did you mean: 

frdmk28f - multiple GPIO interupt

367 Views
mylim
Contributor IV

Hi, I am new to this. I am trying out to understand the development environment. I am using FRDMK28F board using the gpio_input_interupt example. I manage to load and run the example and pressing SW2 to switch on the RED LED. 

I am trying to figure out for instance with multiple GPIO interupt. for example adding SW3 to switch on/off GREEN LED.

I have tried duplicating the codes and add them as follow but it didn't work. Can anyone advise. Thanks.


#define BOARD_LED_GPIO_SW3 BOARD_LED_GREEN_GPIO
#define BOARD_LED_GPIO_PIN_SW3 BOARD_LED_GREEN_GPIO_PIN

#define BOARD_SW_GPIO_SW3 BOARD_SW3_GPIO
#define BOARD_SW_PORT_SW3 BOARD_SW3_PORT
#define BOARD_SW_GPIO_PIN_SW3 BOARD_SW3_GPIO_PIN
#define BOARD_SW_IRQ_SW3 BOARD_SW3_IRQ
#define BOARD_SW_IRQ_HANDLER_SW3 BOARD_SW3_IRQ_HANDLER
#define BOARD_SW_NAME_SW3 BOARD_SW3_NAME


volatile bool g_ButtonPress_SW3 = false;

void BOARD_SW_IRQ_HANDLER_SW3(void)
{
GPIO_PortClearInterruptFlags(BOARD_SW_GPIO_SW3, 1U << BOARD_SW_GPIO_PIN_SW3);
g_ButtonPress_SW3 = true;
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}

#if (defined(FSL_FEATURE_PORT_HAS_NO_INTERRUPT) && FSL_FEATURE_PORT_HAS_NO_INTERRUPT)
GPIO_SetPinInterruptConfig(BOARD_SW_GPIO_SW3, BOARD_SW_GPIO_PIN_SW3, kGPIO_InterruptFallingEdge);
#else
PORT_SetPinInterruptConfig(BOARD_SW_PORT_SW3, BOARD_SW_GPIO_PIN_SW3, kPORT_InterruptFallingEdge);
#endif
EnableIRQ(BOARD_SW_IRQ_SW3);
GPIO_PinInit(BOARD_SW_GPIO_SW3, BOARD_SW_GPIO_PIN_SW3, &sw_config);


GPIO_PinInit(BOARD_LED_GPIO_SW3, BOARD_LED_GPIO_PIN_SW3, &led_config);

while (1)
{
if (g_ButtonPress)
{
PRINTF(" %s is pressed \r\n", BOARD_SW_NAME);
/* Toggle LED. */
GPIO_PortToggle(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);
/* Reset state of button. */
g_ButtonPress = false;
}

if (g_ButtonPress_SW3)
{
PRINTF(" %s is pressed \r\n", BOARD_SW_NAME_SW3);
/* Toggle LED. */
GPIO_PortToggle(BOARD_LED_GPIO_SW3, 1U << BOARD_LED_GPIO_PIN_SW3);
/* Reset state of button. */
g_ButtonPress_SW3 = false;
}
}

gpio_isr.png

5 Replies

110 Views
nxf51211
NXP Employee
NXP Employee

Hi,

The problems you are having are because you do not have the Clock Gating for PORTD configured.

If you're making the changes from the gpio_input_interupt example, you have to make sure that SW3 has its clock enabled (it's the one corresponding to PORTD) and its PIN (pin 0) is set as GPIO. This can be achieved by adding this two lines in your code:

    /* Port D Clock Gate Control: Clock enabled */
    CLOCK_EnableClock(kCLOCK_PortD);

    /* PORTD0 (pin A6) is configured as PTD0 */
    PORT_SetPinMux(PORTD, 0U, kPORT_MuxAsGpio);

The same thing needs to be done when you try to use the GREEN LED (Here the its PORTE and PIN7):

    /* Port E Clock Gate Control: Clock enabled */
    CLOCK_EnableClock(kCLOCK_PortE);

    /* PORTE6 (pin E2) is configured as PTE6 */
    PORT_SetPinMux(PORTE, 7U, kPORT_MuxAsGpio);

These lines have to go before setting the pin interrupt configuration. They usually go inside the BOARD_InitPins() function, but since you're making the modifications from the driver example, they are not initially added.

I hope this information can help you.

Ricardo Delsordo

110 Views
mylim
Contributor IV

Thank you. That helps me to solve the issue. 

However, can the enabling clock gate be done in something like the Device Initialization or Processor Expert in the old codewarrior environment?

0 Kudos

110 Views
nxf51211
NXP Employee
NXP Employee

Hi,

It is possible to enable Clock Gate by using Processor Expert on CodeWarrior. You can check the next link for more information on how to use this tool: https://www.nxp.com/docs/en/user-guide/PEXDRVGETSTARTEDUG.pdf 

I hope this information can help you.

Ricardo Delsordo

110 Views
mylim
Contributor IV

Thank you very much! :smileyhappy:

0 Kudos

110 Views
mjbcswitzerland
Specialist V

Hi

Each port has a single interrupt so you need to add a dispatcher to handle multiple interrupts.
See some video guides at https://www.youtube.com/watch?v=kWNlsAoMly4&list=PLWKlVb_MqDQFZAulrUywU30v869JBYi9Q which include GPIO interrupt discussions.
Code for PIO interrupt dispatching and DMA handling is available at the open source uTasker project on GitHub - including interrupt and DAM simulation in visual studio

Regards

Mark


Complete Kinetis solutions, training and support: http://www.utasker.com/kinetis.html
Kinetis K28:
- http://www.utasker.com/kinetis/FRDM-K28F.html

0 Kudos