I used to work with C but I am trying to learn using C++ for embedded programming. I want to do a basic GPIO interrupt example to understand the structure of a class and its usage.
My microcontroller is an M031SD2AE which is Nuvoton made and ARM Cortex M0 based. This code does not work. It never goes into the GPIO interrupt. What seems to be a problem here?
This is my code:
main.cpp:
#include "M031Series.h"
#include "TSMBTN.h"
uint32_t u32count;
void GPABGH_IRQHandler(void)
{
if(GPIO_GET_INT_FLAG(PB, BIT2))
{
u32count++;
if(u32count == 5)
{
LED = 0;
}
}
GPIO_CLR_INT_FLAG(PB,BIT2);
}
int main(void)
{
TSMBTN.sys_init();
TSMBTN.gpio_init();
while(1);
}
TSMBTN.cpp:
#include "TSMBTN.h"
#define LED PB14
TsmBtn TSMBTN;
void TsmBtn::gpio_init()
{
GPIO_SetMode(PB,BIT2,GPIO_MODE_INPUT); //PB2->input mode
GPIO_EnableInt(PB,2,GPIO_INT_RISING); //PB2->interrupt rising edge enabled
NVIC_EnableIRQ(GPIO_PAPBPGPH_IRQn);
GPIO_SetMode(PB,BIT14,GPIO_MODE_OUTPUT);
}
void TsmBtn::sys_init()
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable clock source */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for clock source ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Disable PLL first to avoid unstable when setting PLL */
CLK_DisablePLL();
/* Set PLL frequency */
CLK->PLLCTL = (CLK->PLLCTL & ~(0x000FFFFFul)) | 0x0008C03Eul;
/* Waiting for PLL ready */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* If the defines do not exist in your project, please refer to the related clk.h in the Header folder appended to the tool package. */
/* Set HCLK clock */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Set PCLK-related clock */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1);
/* Enable IP clock */
CLK_EnableModuleClock(ISP_MODULE);
CLK_EnableSysTick(CLK_CLKSEL0_STCLKSEL_HCLK, 0);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/* Lock protected registers */
SYS_LockReg();
}
void TsmBtn::led_on()
{
LED = 0;
}
TSMBTN.h:
#include "M031Series.h"
class TsmBtn
{
public:
void gpio_init();
void led_on();
void sys_init();
};
extern TsmBtn TSMBTN;