Experiment: Software Design for External Interrupts with Buttons

15.3.2

Software Design

15.3.2.1

Create New Project

For e2studio Development Environment

Copy our previous e2s project “11_GPIO_LED“, then rename the project folder to “16_ICU_External_IRQ“, and finally import it into our e2studio workspace.

For Keil Development Environment

Copy our previous Keil project “11_GPIO_LED“, then rename the project folder to “16_ICU_External_IRQ“, and double-click the Keil project file inside that folder to open the project.

After creating the project, create a new folder named “key” under the “src” folder in the project root directory, and then create the source file and header file inside the “key” folder: “bsp_key_irq.c” and “bsp_key_irq.h“. The project file structure is as follows.

List 1: File Structure

Swipe left or right to view the full content

16_ICU_External_IRQ ├─ ...... └─ src ├─ led │ ├─ bsp_led.c │ └─ bsp_led.h ├─ key │ ├─ bsp_key_irq.c │ └─ bsp_key_irq.h └─ hal_entry.c

12.3.2.2

FSP Configuration

First, open the FSP configuration interface for the “16_ICU_External_IRQ” project, and then we need to configure the chip’s pins and their corresponding functions in this interface.

Taking the Wildfire Qiming 6M5 development board as an example, first configure the pins connected to the ICU interrupt channels.

Experiment: Software Design for External Interrupts with Buttons

Click to view the full image

Then, click “Stacks” -> “NewStack” -> “Input” -> “ExternalIRQ” to add the external interrupt module, as shown in the figure below. Since two buttons are used, two external interrupts are needed, so we need to add 2 external interrupt modules here.

Experiment: Software Design for External Interrupts with Buttons

Click to view the full image

The first external interrupt module is configured for button 1’s external interrupt, set as shown in the figure below:

Experiment: Software Design for External Interrupts with Buttons

Click to view the full image

The second external interrupt module is configured for button 2’s external interrupt, set as shown in the figure below:

Experiment: Software Design for External Interrupts with Buttons

Click to view the full image

The properties of the external interrupt module (r_icu) are introduced in the table below.

Table 4: Introduction to External IRQ Properties:

Development Board Button Pin Allocation and Interrupt Numbers

Experiment: Software Design for External Interrupts with Buttons

Click to view the full image

After configuration is complete, press the shortcut key “Ctrl+S” to save, and finally click the “GenerateProjectContent” button in the upper right corner to let the software automatically generate the configuration code.

Experiment: Software Design for External Interrupts with Buttons

15.3.2.3

Button External Interrupt Initialization Function

List 2 Code Listing 16_1:

External Interrupt Initialization Function: File Structure

Swipe left or right to view the full content

/* KEY External Interrupt Initialization Function */void Key_IRQ_Init(void){fsp_err_t err = FSP_SUCCESS;/* Open ICU module */ err = R_ICU_ExternalIrqOpen(&g_external_irq9_ctrl, &g_external_irq9_cfg); err = R_ICU_ExternalIrqOpen(&g_external_irq10_ctrl, &g_external_irq10_cfg);/* Enable Interrupt */ err = R_ICU_ExternalIrqEnable(&g_external_irq9_ctrl); err = R_ICU_ExternalIrqEnable(&g_external_irq10_ctrl); }
  1. R_ICU_ExternalIrqOpen() is used to open an instance of an external interrupt module.

2. R_ICU_ExternalIrqEnable() is used to enable the external interrupt, which can then trigger the external interrupt.

15.3.2.4

Enable FPU

Since the Callback properties configured in the previous FSP configuration are all the same interrupt callback function, we only need to implement one interrupt callback function. Of course, the interrupt callback function can also be set to two different functions defined by the user.

Both buttons share the key_external_irq_callback interrupt callback function, and the p_args->channel parameter is used to determine which interrupt channel generated the interrupt, thus identifying which button was pressed. The function is as follows:

List 3 Code Listing 16_2:Button Interrupt Callback Function

Swipe left or right to view the full content

/* Button Press Flag */volatile bool key1_sw2_press = false;volatile bool key2_sw3_press = false;/* Button Interrupt Callback Function */void key_external_irq_callback(external_irq_callback_args_t *p_args){/* Determine Interrupt Channel */if (9 == p_args->channel) { key1_sw2_press = true; // Button KEY1_SW2 pressed }else if(10 == p_args->channel) { key2_sw3_press = true; // Button KEY2_SW3 pressed }}

15.3.2.5

hal_entry Entry Function

The hal_entry entry function is as follows:

List 4 Code Listing 16_3: hal_entry Entry Function

Swipe freely to view the full content

/* User Header File Inclusion */#include "led/bsp_led.h"#include "key/bsp_key_irq.h" extern volatile bool key1_sw2_press; extern volatile bool key2_sw3_press;void hal_entry(void){/* TODO: add your own code here */ LED_Init(); // LED Initialization Key_IRQ_Init(); // KEY External Interrupt Initialization while(1) {/* Check if Button KEY1_SW2 is pressed */if (key1_sw2_press) { key1_sw2_press = false; // Clear Flag LED2_TOGGLE; }// LED2 Toggle /* Check if Button KEY2_SW3 is pressed */if (key2_sw3_press) { key2_sw3_press = false; // Clear Flag LED3_TOGGLE; }// LED3 Toggle // LED1 Blinks to indicate the program is running LED1_TOGGLE; R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); // Delay 1 second // After the button is pressed and triggers the interrupt, LED2 or LED3 will be delayed for up to 1 second to toggle in the main loop }#if BSP_TZ_SECURE_BUILD/* Enter non-secure code */ R_BSP_NonSecureEnter();#endif}

15.3.3

Download Verification

Download the compiled program to the development board and reset it. Press user button 1 and button 2 to control the on/off of LED2 and LED3 respectively. Meanwhile, LED1 will toggle every second.

Experiment: Software Design for External Interrupts with Buttons

Need Technical Support?

If you have any questions while using Renesas MCU/MPU products, you can scan the QR code below or copy the URL into your browser to access the Renesas Technical Forum for answers or to get online technical support.

Experiment: Software Design for External Interrupts with Buttons

https://community-ja.renesas.com/zh/forums-groups/mcu-mpu/

To be continued

Recommended Reading

Experiment: Software Design for External Interrupts with Buttons

External Pin Interrupt – Practical Guide to Renesas RA Series FSP Library Development (37)

Experiment: Software Design for External Interrupts with Buttons

Software Design – Practical Guide to Renesas RA Series FSP Library Development (38)

Experiment: Software Design for External Interrupts with Buttons

Introduction to Interrupt Processes – Practical Guide to Renesas RA Series FSP Library Development (39)

Experiment: Software Design for External Interrupts with ButtonsExperiment: Software Design for External Interrupts with Buttons

Leave a Comment