TI-RTOS - HWI Button Presses
For my Hearing Guard System, I intend to use multiple menus to allow the user to set items such as Db Threshold, View Alerts, Connect to Base Unit, and so. This required user input and at this time, the on board Buttons on the MSP432 seemed to be the best option. I plan to look at using the touch pads on the Sharp LCD, but the Buttons were easy to implement which allowed me to proceed with configuring the menus.
To enable the push Buttons (SW1, SW2) on the MSP432 from TI-RTOS is fairly simple and most of the dirty work has already been implemented in TI-RTOS. There are multiple options for handling the MSP432 Button presses including creating a Task to poll for the presses, Software Interrupt, or Hardware Interrupt. For my purposes, I wanted the Button presses to be Event Driven where the Button presses are handled only when the they are pressed, so I opted for a Hardware Interrupt option. As a reference, there is an GPIO Interrupt example in the MSP432 TI-RTOS package.
TI-RTOS MSP432 GPIO Interrupt example:
gpiointerrupt_MSP_EXP432P401R_TI
This example contained exactly what I was looking for, so I just incorporated the code into my project.
The first thing is to ensure the GPIO.h and Board.h header files are included:
/* TI-RTOS Header files */ #include <ti/drivers/GPIO.h> /* Example/Board Header files */ #"Board.h"
Then in the main function, add the GPIO Init method to enable the GPIO devices.
/* Call board init functions */
Board_initGeneral();
Board_initGPIO();
Board_initUART();
To handle the Button press events, callback methods are required for each Button. These callback methods will be called but the Interrupt handler when a Button event occurs. These will be defined later.
/* install Button callback */
GPIO_setCallback(Board_BUTTON0, gpioButtonFxn0);
To ensure the Interrupts are registered for the Buttons, a call to GPIO_enableInt is required.
/* Enable interrupts */
GPIO_enableInt(Board_BUTTON0);
To handle the second Button interrupt, (Button1), in the example an if statement was added to check for input pins for Button0 and Button1 in which case Interrupts will be enabled for Button1 if it exists.
/*
* If more than one input pin is available for your device, interrupts
* will be enabled on Board_BUTTON1.
*/
if (Board_BUTTON0 != Board_BUTTON1) {
/* install Button callback */
GPIO_setCallback(Board_BUTTON1, gpioButtonFxn1);
GPIO_enableInt(Board_BUTTON1);
}
Now, the only thing to do is to create the Callback methods to handle the button press events. In the example, when Button0 is pressed, Board_LED0 (LED1) is toggled.
/*
* ======== gpioButtonFxn0 ========
* Callback function for the GPIO interrupt on Board_BUTTON0.
*/
void gpioButtonFxn0(unsigned int index)
{
/* Clear the GPIO interrupt and toggle an LED */
GPIO_toggle(Board_LED0);
if (count++ == 100) {
count = 0;
}
}
If Button1 is pressed, Board_LED0 (LED2: Red) is toggled.
/*
* ======== gpioButtonFxn1 ========
* Callback function for the GPIO interrupt on Board_BUTTON1.
* This may not be used for all boards.
*/
void gpioButtonFxn1(unsigned int index)
{
/* Clear the GPIO interrupt and toggle an LED */
GPIO_toggle(Board_LED1);
if (count++ == 100) {
count = 0;
}
}
From here, the callback methods can be changed to perform the routine required for an project.
Top Comments