GPIO Button Interrupt via HAL

Lab Objective:

In this lab, participants will configure the Infineon PSoC 6 microcontroller to detect button presses using GPIO interrupts via the Hardware Abstraction Layer (HAL). Rather than continuously polling the button's state, the microcontroller will be set up to respond immediately upon a button press through an interrupt mechanism. This hands-on experience aims to educate participants on the efficient use of hardware interrupts, showcasing how HAL can facilitate real-time responsiveness and optimize system performance by minimizing unnecessary CPU cycles.

🔥 Requirements

ResourcesLinks

Computer

💻

ModusToolbox™ software v3.0 or later

CY8CKIT-062S2-43012 Infineon Board

Technical Report

🚩 Let start

Create Application

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 GPIO_INTERRUPT_PRIORITY (7u)

//Interrupt handler
static void button_isr(void *handler_arg, cyhal_gpio_event_t event)
{
	cyhal_gpio_toggle(CYBSP_USER_LED);
}

// GPIO callback initialization structure
cyhal_gpio_callback_data_t cb_data =
{
	.callback     = button_isr,
	.callback_arg = NULL
};

int main(void)
{
    cy_rslt_t result;

    /* Initialize the device and board peripherals */
    result = cybsp_init() ;
    /*BSP init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    /* Initialize the User LED */
    result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
	/* GPIO init failed. Stop program execution */
	if (result != CY_RSLT_SUCCESS)
	{
		CY_ASSERT(0);
	}

	/* Initialize the user button */
	result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
	/* GPIO init failed. Stop program execution */
	if (result != CY_RSLT_SUCCESS)
	{
		CY_ASSERT(0);
	}

	/* Configure GPIO interrupt */
	cyhal_gpio_register_callback(CYBSP_USER_BTN, &cb_data);
	cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL, GPIO_INTERRUPT_PRIORITY, true);


	/* Enable global interrupts */
    __enable_irq();


    for (;;)
    {
    	/* Enter deep sleep mode */
		cyhal_syspm_deepsleep();


    }
}

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

	//cyhal_syspm_deepsleep();
    	cyhal_gpio_write(CYBSP_LED_RGB_BLUE, CYBSP_LED_STATE_ON);
    	cyhal_system_delay_ms(500);

    	cyhal_gpio_write(CYBSP_LED_RGB_BLUE, CYBSP_LED_STATE_OFF);
    	cyhal_system_delay_ms(500);

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-105GPIO-PDL Button Interrupt

VersionDescription of change

1.0.0

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

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