#include "sdk_project_config.h"
#include "bsp_sys.h"
#include "cola_device.h"
#include "cola_init.h"

typedef void (* isr_t)(void);

/*!
 * @brief: LPTMR IRQ handler.
 */
void LPTMR0_IRQHandler(void)
{
	 cola_timer_ticker();
    /* Clear compare flag */

    LPTMR_DRV_ClearCompareFlag(INST_LPTMR_0);
}
void CCIF_Handler(void);
/* If target is flash, insert this macro to locate callback function into RAM */
START_FUNCTION_DECLARATION_RAMSECTION
void CCIF_Callback(void)
END_FUNCTION_DECLARATION_RAMSECTION

void bsp_sys_init(void)
{
	CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
	CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
	PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0, g_pin_mux_InitConfigArr0);


    /* Init LPTMR as timer
	 *  -   interrupt after 1 second
	 *  -   SIRC as clock source
	 *  -   counter disabled
	 */
	LPTMR_DRV_Init(INST_LPTMR_0, &lptmr_1_config0, false);
	/* Enable IRQ for LPTMR */
	/* Install interrupt for Flash Command Complete event */
	INT_SYS_InstallHandler(FTFC_IRQn, CCIF_Handler, (isr_t*) 0);
	INT_SYS_EnableIRQ(FTFC_IRQn);
	/* Enable global interrupt */
	INT_SYS_EnableIRQGlobal();
	INT_SYS_EnableIRQ(LPTMR0_IRQn);
	INT_SYS_SetPriority(LPTMR0_IRQn, 0);
	/* Start LPTMR counter */
	LPTMR_DRV_StartCounter(INST_LPTMR_0);

}
sysclk_initcall(bsp_sys_init);
void CCIF_Handler(void)
{
    /* Disable Flash Command Complete interrupt */
    FTFx_FCNFG &= (~FTFx_FCNFG_CCIE_MASK);

    return;
}

/*!
  \brief Callback function for Flash operations
*/
START_FUNCTION_DEFINITION_RAMSECTION
void CCIF_Callback(void)
{
    /* Enable interrupt for Flash Command Complete */
    if ((FTFx_FCNFG & FTFx_FCNFG_CCIE_MASK) == 0u)
    {
        FTFx_FCNFG |= FTFx_FCNFG_CCIE_MASK;
    }
}
END_FUNCTION_DEFINITION_RAMSECTION
