GPIO Button Interrupt via PDL

Lab Objective:

In this lab, participants will employ the Peripheral Driver Library (PDL) to set up GPIO interrupts for detecting button presses on the Infineon PSoC 6 microcontroller. Instead of traditional polling techniques, the system will be optimized to immediately react to button actuation via the interrupt-driven method. Through this activity, participants will gain insights into leveraging PDL for efficient interrupt handling, and learning to utilize hardware features to enhance system responsiveness while preserving CPU resources for other critical tasks.

🔥 Requirements

ResourcesLinks

Computer

💻

ModusToolbox™ software v3.0 or later

CY8CKIT-062S2-43012 Infineon Board

Technical Report

🚩 Let start

Create Application

Use Device Configurator and set the button pin to have a falling edge interrupt.

Coding

  • Coding: Open the main.c file and add the following code to the main(void) function.

main.c
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"

#define PORT_INTR_MASK  (0x00000001UL << CYBSP_USER_BTN_PORT_NUM)

void Interrupt_Handler(void){
	// Get interrupt cause
	uint32_t intrSrc = Cy_GPIO_GetInterruptCause0();
	/* Check if the interrupt was from the button's port */
	if(PORT_INTR_MASK == (intrSrc & PORT_INTR_MASK)){
		/* Clear the interrupt */
		Cy_GPIO_ClearInterrupt(CYBSP_USER_BTN_PORT, CYBSP_USER_BTN_NUM);
		// Toggle LED
		Cy_GPIO_Inv(CYBSP_USER_LED_PORT, CYBSP_USER_LED_NUM);
	}
}

int main(void)
{
    cy_rslt_t result;

    /* Initialize the device and board peripherals */
    result = cybsp_init() ;
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    /* Enable global interrupts */
    __enable_irq();

    // Interrupt config structure
     cy_stc_sysint_t intrCfg =
 	{
 		/*.intrSrc =*/ CYBSP_USER_BTN_IRQ,
 		/*.intrPriority =*/ 3UL
 	};

     /* Initialize the interrupt with vector for Interrupt_Handler */
 	Cy_SysInt_Init(&intrCfg, &Interrupt_Handler);
 	/* Send the button through the glitch filter */
 	Cy_GPIO_SetFilter(CYBSP_USER_BTN_PORT, CYBSP_USER_BTN_NUM);

	
 	/* Enable the interrupt*/
 	NVIC_EnableIRQ(intrCfg.intrSrc);


    for (;;)
    {
    	/* Do nothing */
    }
}

Build the Application

Launching the Application

Note: Before launching the program to the board, make sure that you have already connected the board to the computer through a USB cable.

Result

🎉 Congratulations! You can now complete Lab105

Workshop:

Exercise 1: Change From USER LED to RGB Green/Red/Blue

Exercise 2: Add Loop LED to the Loop function

// RED LED turn on and Turn of
Cy_GPIO_Clr(CYBSP_LED_RGB_RED_PORT, CYBSP_LED_RGB_RED_NUM); // Turn ON the Red LED
Cy_SysLib_Delay(1000U);

Cy_GPIO_Set(CYBSP_LED_RGB_RED_PORT, CYBSP_LED_RGB_RED_NUM); // Turn OFF the Red LED
Cy_SysLib_Delay(1000U);

Supported toolchains (make variable 'TOOLCHAIN')

  • GNU Arm® embedded compiler v10.3.1 (GCC_ARM) - Default value of TOOLCHAIN

  • Arm® compiler v6.16 (ARM)

  • IAR C/C++ compiler v9.30.1 (IAR)

Supported kits (make variable 'TARGET')

ResourcesLinks

ModusToolbox™ Software Training

Other resources

Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.

Document history

Document title: BILL_MTB-106GPIO-PDL Button interrupt

VersionDescription of change

1.0.0

Lab 106: Learn basic GPIO control with PSoC 6 using Interrupt Button to toggle the LED via PDL

Authors:

  • Assoc. Prof. Wiroon Sriborrirux

  • Mr. Sriengchhun Chheang

  • Mr. Sabol Socare


© BDH Corporation, 2022-2023

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University