#include "em_device.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_chip.h"
#include "em_gpio.h"
#include "em_timer.h"
// Desired frequency in Hz
// Min: 145 Hz, Max: 9.5 MHz with default settings
#define OUT_FREQ 1000
// Default prescale value
#define TIMER0_PRESCALE timerPrescale1
/**************************************************************************//**
* @brief
* GPIO initialization
*****************************************************************************/
void initGpio(void)
{
// Enable GPIO clock
CMU_ClockEnable(cmuClock_GPIO, true);
// Configure PC10 as output
GPIO_PinModeSet(gpioPortC, 10, gpioModePushPull, 0);
}
/**************************************************************************//**
* @brief
* TIMER initialization
*****************************************************************************/
void initTimer(void)
{
// Enable clock for TIMER0 module
CMU_ClockEnable(cmuClock_TIMER0, true);
// Configure TIMER0 Compare/Capture for output compare
TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
timerCCInit.mode = timerCCModeCompare;
timerCCInit.cmoa = timerOutputActionToggle;
TIMER_InitCC(TIMER0, 0, &timerCCInit);
// Set route to Location 15 and enable
// TIM0_CC0 #15 is PC10
TIMER0->ROUTELOC0 |= TIMER_ROUTELOC0_CC0LOC_LOC15;
TIMER0->ROUTEPEN |= TIMER_ROUTEPEN_CC0PEN;
// Set Top value
// Note each overflow event constitutes 1/2 the signal period
uint32_t topValue = CMU_ClockFreqGet(cmuClock_HFPER) / (2*OUT_FREQ * (1 << TIMER0_PRESCALE))-1;
TIMER_TopSet(TIMER0, topValue);
// Initialize and start timer with defined prescale
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
timerInit.prescale = TIMER0_PRESCALE;
TIMER_Init(TIMER0, &timerInit);
}
/**************************************************************************//**
* @brief
* Main function
*****************************************************************************/
int main(void)
{
// Chip errata
CHIP_Init();
// Init DCDC regulator with kit specific parameters
EMU_DCDCInit_TypeDef dcdcInit = EMU_DCDCINIT_DEFAULT;
EMU_DCDCInit(&dcdcInit);
// Initialization
initGpio();
initTimer();
while (1) {
EMU_EnterEM1(); // Enter EM1 (won't exit)
}
}