Exemple #1
0
/**************************************************************************//**
 * @brief  Setup the CMU
 *****************************************************************************/
void setupCMU(void)
{
  /* Ensure core frequency has been updated */
  SystemCoreClockUpdate();

  /* Select clock source for HF clock. */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO);
  /* Select clock source for LFA clock. */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFRCO);
  /* Disable clock source for LFB clock. */
  CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_Disabled);

  /* Enable HF peripheral clock. */
  CMU_ClockEnable(cmuClock_HFPER, true);
  /* Enable clock for GPIO. */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* Enable clock for LCD. */
  CMU_ClockEnable(cmuClock_LCD, true);
  /* Enable clock for ACMP0. */
  CMU_ClockEnable(cmuClock_ACMP0, true);
  /* Enable clock for PRS. */
  CMU_ClockEnable(cmuClock_PRS, true);
  /* Enable CORELE clock. */
  CMU_ClockEnable(cmuClock_CORELE, true);
  /* Enable clock for PCNT. */
  CMU_ClockEnable(cmuClock_PCNT0, true);
  /* Enable clock on RTC. */
  CMU_ClockEnable(cmuClock_RTC, true);
  /* Enable clock for LESENSE. */
  CMU_ClockEnable(cmuClock_LESENSE, true);
  /* Enable clock divider for LESENSE. */
  CMU_ClockDivSet(cmuClock_LESENSE, cmuClkDiv_1);
  /* Enable clock divider for RTC. */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32768);
}
/**************************************************************************//**
 * @brief  Main function
 *****************************************************************************/
int main(void)
{
  /* Initialize chip */
  CHIP_Init();
  
  setupSWO();

  /* Enable HFXO */
  CMU_OscillatorEnable(cmuOsc_HFXO, true, true);

  /* Switch HFCLK to HFXO */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);

  /* Turn off HFRCO */
  CMU_OscillatorEnable(cmuOsc_HFRCO, false, false);

  BSP_LedsInit();

  while (1)
  {
    /* Blink led three times, with a factor 1 million delay */
    blink_led(1000000, 3);

    /* Turn on LFRCO */
    CMU_OscillatorEnable(cmuOsc_LFRCO, true, true);

    /* Select LFRCO */
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_LFRCO);

    /* Maximum prescaling for smalles frequency (64 Hz) */
    CMU_ClockDivSet(cmuClock_CORE, cmuClkDiv_512);

    /* Turn off HFXO */
    CMU_OscillatorEnable(cmuOsc_HFXO, false, false);

    /* Blink led three times, with a factor 1 delay */
    blink_led(1, 3);

    /* Turn on HFXO */
    CMU_OscillatorEnable(cmuOsc_HFXO, true, true);

    /* Select HFXO */
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);

    /* No prescaling for maximum clock frequency (32 MHz) */
    CMU_ClockDivSet(cmuClock_CORE, cmuClkDiv_1);

    /* Turn off LFRCO */
    CMU_OscillatorEnable(cmuOsc_LFRCO, false, false);
  }
}
Exemple #3
0
/**
 * Initialize RTC Based on Crystal Oscillator
 */
void rtc_crystal_init(void)
{
  RTC_Init_TypeDef init;

  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);

  /* Use the prescaler to reduce power consumption. */
  CMU_ClockDivSet(cmuClock_RTC, RTC_PRESCALE);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);

  init.enable   = false; /* Start disabled to reset counter */
  init.debugRun = false;
  init.comp0Top = false; /* Count to max before wrapping */
  RTC_Init(&init);

  /* Disable interrupt generation from RTC0 */
  RTC_IntDisable(_RTC_IF_MASK);

  /* Enable interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);

  /* Start RTC counter */
  RTC_Enable(true);
}
Exemple #4
0
void rtt_init(void)
{
    /* prescaler of 32768 = 1 s of resolution and overflow each 194 days */
    CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32768);

    /* enable clocks */
    CMU_ClockEnable(cmuClock_CORELE, true);
    CMU_ClockEnable(cmuClock_RTC, true);

    /* reset and initialize peripheral */
    EFM32_CREATE_INIT(init, RTC_Init_TypeDef, RTC_INIT_DEFAULT,
        .conf.enable = false,
        .conf.comp0Top = false
    );

    RTC_Reset();
    RTC_Init(&init.conf);

    /* enable interrupts */
    RTC_IntEnable(RTC_IEN_OF);

    NVIC_ClearPendingIRQ(RTC_IRQn);
    NVIC_EnableIRQ(RTC_IRQn);

    /* enable peripheral */
    RTC_Enable(true);
}
// This is the timekeeping clock
void setup_rtc()
{
	// Ensure LE modules are accessible
	CMU_ClockEnable(cmuClock_CORELE, true);

	// Enable LFACLK in CMU (will also enable oscillator if not enabled)
	CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFRCO);

	// Use the prescaler to reduce power consumption. 2^15
	//CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32768);
	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_512);

	//uint32_t rtcFreq = CMU_ClockFreqGet(cmuClock_RTC);

	// Enable clock to RTC module
	CMU_ClockEnable(cmuClock_RTC, true);

	// Set up the Real Time Clock as long-time timekeeping
	RTC_Init_TypeDef init = RTC_INIT_DEFAULT;
	init.comp0Top = false;
	RTC_Init(&init);

	// Enabling Interrupt from RTC
	NVIC_EnableIRQ(RTC_IRQn);
}
Exemple #6
0
/*---------------------------------------------------------------------------*/
void
rtimer_arch_init(void)
{
  RTC_Init_TypeDef init = RTC_INIT_DEFAULT;

  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);

  /* Don't use prescaler to have highest precision */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);

  init.enable   = false; /* Start disabled to reset counter */
  init.debugRun = false;
  init.comp0Top = false; /* Don't Reset Count on comp0 */
  RTC_Init(&init);

  /* Disable interrupt generation from RTC0 */
  RTC_IntDisable(_RTC_IF_MASK);

  /* Enable interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);

  /* Start RTC counter */
  RTC_Enable(true);
}
Exemple #7
0
void rtcSetup(){
	RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;

	/* Enabling clock to LE configuration register */
	CMU_ClockEnable(cmuClock_CORELE, true);

	/* Selecting crystal oscillator to drive LF clock */
	CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);

	/* 32 clock division to save energy */
	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32768);

	/* Providing clock to RTC */
	CMU_ClockEnable(cmuClock_RTC, true);

	/* Initialize RTC */
	rtcInit.enable   = false;  /* Do not start RTC after initialization is complete. */
	rtcInit.debugRun = false;  /* Halt RTC when debugging. */
	rtcInit.comp0Top = true;   /* Wrap around on COMP0 match. */
	RTC_Init(&rtcInit);

	/* Interrupt every minute */
	RTC_CompareSet(0, ((RTC_FREQ / CLOCK_DIVISION) * 10 ) - 1 );

	/* Enable interrupt */
	NVIC_EnableIRQ(RTC_IRQn);
	RTC_IntEnable(RTC_IEN_COMP0);

	/* Start Counter */
	RTC_Enable(true);

}
Exemple #8
0
/************************************************************************************//**
** \brief     Initializes the UART communication interface.
** \return    none.
**
****************************************************************************************/
static void BootComUartInit(void)
{
  LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT;

  /* configure GPIO pins */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* to avoid false start, configure output as high */
  GPIO_PinModeSet(gpioPortC, 6, gpioModePushPull, 1);
  GPIO_PinModeSet(gpioPortC, 7, gpioModeInput, 0);
  /* enable CORE LE clock in order to access LE modules */
  CMU_ClockEnable(cmuClock_CORELE, true);
  /* select LFXO for LEUARTs (and wait for it to stabilize) */
  CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
  /* do not prescale clock */
  CMU_ClockDivSet(cmuClock_LEUART1, cmuClkDiv_1);
  /* enable LEUART1 clock */
  CMU_ClockEnable(cmuClock_LEUART1, true);
  /* configure LEUART */
  init.enable = leuartDisable;
  LEUART_Init(LEUART1, &init);
  LEUART_BaudrateSet(LEUART1, 0, BOOT_COM_UART_BAUDRATE);
  /* enable pins at default location */
  LEUART1->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN;
  /* clear previous RX interrupts */
  LEUART_IntClear(LEUART1, LEUART_IF_RXDATAV);
  /* finally enable it */
  LEUART_Enable(LEUART1, leuartEnable);
} /*** end of BootUartComInit ***/
Exemple #9
0
/**************************************************************************//**
 * @brief Enables LFACLK and selects LFXO as clock source for RTC
 *        Sets up the RTC to generate an interrupt every minute.
 *****************************************************************************/
void rtcSetup(void)
{
  RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;

  /* Enable LE domain registers */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable LFXO as LFACLK in CMU. This will also start LFXO */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);

  /* Set a clock divisor of 32 to reduce power conumption. */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32);

  /* Enable RTC clock */
  CMU_ClockEnable(cmuClock_RTC, true);

  /* Initialize RTC */
  rtcInit.enable   = false;  /* Do not start RTC after initialization is complete. */
  rtcInit.debugRun = false;  /* Halt RTC when debugging. */
  rtcInit.comp0Top = true;   /* Wrap around on COMP0 match. */
  RTC_Init(&rtcInit);

  /* Interrupt every minute */
  RTC_CompareSet(0, ((RTC_FREQ / 32) * 60 ) - 1 );

  /* Enable interrupt */
  NVIC_EnableIRQ(RTC_IRQn);
  RTC_IntEnable(RTC_IEN_COMP0);

  /* Start Counter */
  RTC_Enable(true);
}
/**************************************************************************//**
 * @brief Enables LFACLK and selects LFXO as clock source for RTC
 *        Sets up the RTC to generate an interrupt every second.
 *****************************************************************************/
static void rtcSetup(unsigned int frequency)
{
  RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;

  palClockSetup(cmuClock_LFA);

  /* Set the prescaler. */
  CMU_ClockDivSet( cmuClock_RTC, cmuClkDiv_2 );
  
  /* Enable RTC clock */
  CMU_ClockEnable(cmuClock_RTC, true);

  /* Initialize RTC */
  rtcInit.enable   = false;  /* Do not start RTC after initialization is complete. */
  rtcInit.debugRun = false;  /* Halt RTC when debugging. */
  rtcInit.comp0Top = true;   /* Wrap around on COMP0 match. */

  RTC_Init(&rtcInit);

  /* Interrupt at given frequency. */
  RTC_CompareSet(0, ((CMU_ClockFreqGet(cmuClock_RTC) / frequency) - 1) & _RTC_COMP0_MASK );

#ifndef INCLUDE_PAL_GPIO_PIN_AUTO_TOGGLE_HW_ONLY
  /* Enable interrupt */
  NVIC_EnableIRQ(RTC_IRQn);
  RTC_IntEnable(RTC_IEN_COMP0);
#endif
  RTC_CounterReset();
  /* Start Counter */
  RTC_Enable(true);
}
Exemple #11
0
/**************************************************************************//**
 * @brief  Setup the CMU
 *****************************************************************************/
void CAPLESENSE_setupCMU(void)
{
  /* Ensure core frequency has been updated */
  SystemCoreClockUpdate();

  /* Select clock source for HF clock. */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO);
  /* Select clock source for LFA clock. */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFRCO);
  /* Select clock source for LFB clock. */
  CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_Disabled);

  /* Enable HF peripheral clock. */
  CMU_ClockEnable(cmuClock_HFPER, 1);
  /* Enable clock for GPIO. */
  CMU_ClockEnable(cmuClock_GPIO, 1);
  /* Enable clock for ACMP0. */
  CMU_ClockEnable(cmuClock_ACMP0, 1);
  /* Enable clock for ACMP1. */
  CMU_ClockEnable(cmuClock_ACMP1, 1);
  /* Enable CORELE clock. */
  CMU_ClockEnable(cmuClock_CORELE, 1);
  /* Enable clock for LESENSE. */
  CMU_ClockEnable(cmuClock_LESENSE, 1);

  /* Enable clock divider for LESENSE. */
  CMU_ClockDivSet(cmuClock_LESENSE, cmuClkDiv_1);
}
/***************************************************************************//**
 * @brief
 *	Enables LFACLK and selects LFXO as clock source for RTC
 *
 * @param osc
 *	Oscillator
 ******************************************************************************/
void RTC_Setup(CMU_Select_TypeDef osc)
{
  RTC_Init_TypeDef init;

  rtcInitialized = 1;

  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable osc as LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, osc);

  /* Use a 32 division prescaler to reduce power consumption. */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32);

  rtcFreq = CMU_ClockFreqGet(cmuClock_RTC);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);

  init.enable   = false;
  init.debugRun = false;
  init.comp0Top = false; /* Count to max before wrapping */
  RTC_Init(&init);

  /* Disable interrupt generation from RTC0 */
  RTC_IntDisable(_RTC_IF_MASK);

  /* Enable interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);
}
/***************************************************************************//**
 * @brief Enables LFACLK and selects osc as clock source for RTC
 ******************************************************************************/
void RTC_Setup(CMU_Select_TypeDef osc)
{
  RTC_Init_TypeDef init;

  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable osc as LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, osc);

  /* No division prescaler to increase accuracy. */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);

  init.enable   = false;
  init.debugRun = false;
  init.comp0Top = true; /* Count only to top before wrapping */
  RTC_Init(&init);
  
  RTC_CompareSet(0, ((LFRCOFREQ * WAKEUP_US) / 1000000));
    
  /* Disable interrupt generation from RTC0, is enabled before each sample-run. */
  RTC_IntDisable(_RTC_IF_MASK);
  
  /* Enable interrupts in NVIC */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);  
}
void os_idle_demon(void)
{
  RTC_Init_TypeDef init;
  unsigned int sleep;

  /* The idle demon is a system thread, running when no other thread is      */
  /* ready to run.                                                           */

  /* Enable system clock for RTC */

  /* LFXO setup */
  /* Use 70% boost */
  CMU->CTRL = (CMU->CTRL & ~_CMU_CTRL_LFXOBOOST_MASK) | CMU_CTRL_LFXOBOOST_70PCENT;

  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* Enable osc as LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);

  /* Use a 32 division prescaler to reduce power consumption. */
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);

  init.enable   = false;
  init.debugRun = false;
  init.comp0Top = false; /* Count to max value before wrapping */

  RTC_Init(&init);

  /* Disable interrupt generation from RTC0 */
  RTC_IntDisable(_RTC_IF_MASK);

  /* Enable interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);

  for (;;)
  {
    /* os_suspend stops scheduler and returns time to next event in OS_TICK units */
    sleep = os_suspend();
    if (sleep)
    {
      RTC_CompareSet(0, sleep - 1);
      RTC_IntClear(RTC_IFC_COMP0);
      RTC_IntEnable(RTC_IF_COMP0);
      RTC_CounterReset();
      /* Enter EM2 low power mode - could be replaced with EM1 if required */
      EMU_EnterEM2(true);
      /* get information how long we were in sleep */
      sleep = RTC_CounterGet();
      RTC_Enable(false);
    };
    /* resume scheduler providing information how long MCU was sleeping */
    os_resume(sleep);
  }
}
/**************************************************************************//**
 * @brief  Start LFRCO for RTC
 * Starts the low frequency RC oscillator (LFRCO) and routes it to the RTC
 *****************************************************************************/
void startLfxoForRtc(uint8_t freq)
{
    /* Starting LFRCO and waiting until it is stable */
    CMU_OscillatorEnable(cmuOsc_LFXO, true, true);

    /* Routing the LFRCO clock to the RTC */
    CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
    CMU_ClockEnable(cmuClock_RTC, true);

    /* Set Clock prescaler */
    if(freq == HWTIMER_FREQ_1MS)
    	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32);
    else
    	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1);

    /* Enabling clock to the interface of the low energy modules */
    CMU_ClockEnable(cmuClock_CORELE, true);
}
/**************************************************************************//**
 * @brief vPortSetupTimerInterrupt
 * Override the default definition of vPortSetupTimerInterrupt() that is weakly
 * defined in the FreeRTOS Cortex-M3, which set source of system tick interrupt
 *****************************************************************************/
void vPortSetupTimerInterrupt(void)
{
  /* Set our data about timer used as system ticks*/
  ulTimerReloadValueForOneTick = SYSTICK_LOAD_VALUE ;
  #if (configUSE_TICKLESS_IDLE == 1)
  xMaximumPossibleSuppressedTicks = TIMER_CAPACITY / (SYSTICK_LOAD_VALUE);
  ulStoppedTimerCompensation      = TIMER_COMPENSATION / (configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ);
#endif /* (configUSE_TICKLESS_IDLE == 1) */
  /* Configure RTC as system tick source */
  /* Structure of RTC init */
  RTC_Init_TypeDef init;
#if (configCRYSTAL_IN_EM2 == 1)
  /* LFXO setup */
  /* For cut D, use 70% boost */
  CMU->CTRL    = (CMU->CTRL & ~_CMU_CTRL_LFXOBOOST_MASK) | CMU_CTRL_LFXOBOOST_70PCENT;
  #if defined( EMU_AUXCTRL_REDLFXOBOOST )
  EMU->AUXCTRL = (EMU->AUXCTRL & ~_EMU_AUXCTRL_REDLFXOBOOST_MASK) | EMU_AUXCTRL_REDLFXOBOOST;
  #endif
#else
  /* RC oscillator */
  CMU_OscillatorEnable(cmuOsc_LFRCO, true, true);
#endif
  /* Ensure LE modules are accessible */
  CMU_ClockEnable(cmuClock_CORELE, true);
#if (configCRYSTAL_IN_EM2 == 1)
  /* Enable osc as LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
#else
  /* Enable osc as LFACLK in CMU (will also enable oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFRCO);
#endif
  /* Set 2 times divider to reduce energy*/
  CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_2);

  /* Enable clock to RTC module */
  CMU_ClockEnable(cmuClock_RTC, true);
  init.enable   = false;
  init.debugRun = false;
  init.comp0Top = false;         /* Count to max value before wrapping */
  /* Initialization of RTC */
  RTC_Init(&init);

  /* Disable interrupt generation from RTC0 */
  RTC_IntDisable(RTC_IFC_COMP0);

  /* Tick interrupt MUST execute at the lowest interrupt priority. */
  NVIC_SetPriority(RTC_IRQn, 255);

  /* Enable interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);
  RTC_CompareSet(0, SYSTICK_LOAD_VALUE);
  RTC_IntClear(RTC_IFC_COMP0);
  RTC_IntEnable(RTC_IF_COMP0);
  RTC_Enable(true);
  //RTC_CounterReset();
}
Exemple #17
0
/******************************************************************//**
* @brief
*	Start LFXO for RTC
*
* @details
*    Starts the LFXO and routes it to the RTC. RTC clock is prescaled to save energy.
*
* @note
*********************************************************************/
static void startLfxoForRtc(void)
{
	/* Starting LFXO and waiting until it is stable */
	CMU_OscillatorEnable(cmuOsc_LFXO, true, true);

	/* Routing the LFXO clock to the RTC */
	CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_32768);
	CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
	CMU_ClockEnable(cmuClock_RTC, true);
}
/**************************************************************************//**
 * @brief  Start oscillator for RTC
 * Starts the low frequency oscillator and routes it to the RTC
 *****************************************************************************/
void startOscForRtc(uint8_t freq)
{
    /* Start the oscillator, wait until stable, then route it to the RTC */
#ifdef HW_USE_LFXO
    CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
    CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
#else
    CMU_OscillatorEnable(cmuOsc_LFRCO, true, true);
    CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFRCO);
#endif
    CMU_ClockEnable(cmuClock_RTC, true);

    /* Set Clock prescaler */
    if(freq == HWTIMER_FREQ_1MS)
    	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_32);
    else
    	CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1);

    /* Enabling clock to the interface of the low energy modules */
    CMU_ClockEnable(cmuClock_CORELE, true);
}
Exemple #19
0
/**************************************************************************//**
 * @brief Enables LFACLK and selects LFXO as clock source for RTC
 *        Sets up the RTC to generate an interrupt every second.
 *****************************************************************************/
static void rtcSetup(unsigned int frequency)
{
  RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;

  /* Enable LE domain registers */
  if ( !( CMU->HFCORECLKEN0 & CMU_HFCORECLKEN0_LE) )
  {
    CMU_ClockEnable(cmuClock_CORELE, true);
  }

#ifdef PAL_RTC_CLOCK_LFXO
  /* LFA with LFXO setup is relatively time consuming. Therefore, check if it
     already enabled before calling. */
  if ( !(CMU->STATUS & CMU_STATUS_LFXOENS) )
  {
    CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
  }
  if ( cmuSelect_LFXO != CMU_ClockSelectGet(cmuClock_LFA) )
  {
    CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
  }
#elif defined PAL_RTC_CLOCK_LFRCO
  /* Enable LFACLK in CMU (will also enable LFRCO oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFRCO);
#elif defined PAL_RTC_CLOCK_ULFRCO
  /* Enable LFACLK in CMU (will also enable ULFRCO oscillator if not enabled) */
  CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_ULFRCO);
#else
#error No clock source for RTC defined.
#endif

  /* Set the prescaler. */
  CMU_ClockDivSet( cmuClock_RTC, cmuClkDiv_1 );

  /* Enable RTC clock */
  CMU_ClockEnable(cmuClock_RTC, true);

  /* Initialize RTC */
  rtcInit.enable   = false;  /* Do not start RTC after initialization is complete. */
  rtcInit.debugRun = false;  /* Halt RTC when debugging. */
  rtcInit.comp0Top = true;   /* Wrap around on COMP0 match. */
  RTC_Init(&rtcInit);

  /* Interrupt at given frequency. */
  RTC_CompareSet(0, (CMU_ClockFreqGet(cmuClock_RTC) / frequency) - 1 );

  /* Enable interrupt */
  NVIC_EnableIRQ(RTC_IRQn);
  RTC_IntEnable(RTC_IEN_COMP0);

  /* Start Counter */
  RTC_Enable(true);
}
Exemple #20
0
static void cpu_clock_init(void)
{
    SystemLFXOClockSet((uint32_t)EFM32_LFXO_FREQ);
	CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
	CMU_OscillatorEnable(cmuOsc_HFRCO, true, true);
	CMU_HFRCOBandSet(cmuHFRCOBand_28MHz);
//	/* HF Core clock is connected to external oscillator */
//	CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
	/* HF Core clock is connected to internal RC clock */
	CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO);
	/* RTC, LESENSE, LETIMER0, LCD is connected to external 36kHz oscillator */
	CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
	CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
	/* HFPER clock is divided by 2 - 14MHz */
	CMU_ClockDivSet(cmuClock_HFPER, cmuClkDiv_2);
	/* core, DMA etc. clock */
	CMU_ClockDivSet(cmuClock_CORE, cmuClkDiv_1);
	/* enabling clocks */
	CMU_ClockEnable(cmuClock_HFPER, true);
	CMU_ClockEnable(cmuClock_CORELE, true);
}
Exemple #21
0
/***************************************************************************//**
 * @brief
 *   Initialize board and LCD controller
 *
 * @details
 *   This function is called during initialization and when exited from AEM mode
 ******************************************************************************/
static void _InitController(void) {
  /* Initialize EBI banks (Board Controller, external PSRAM, ..) */
  BSP_Init(BSP_INIT_DK_EBI);
  /* If first word of user data page is non-zero, enable eA Profiler trace */
  BSP_TraceProfilerSetup();

  /* Configure for 32MHz HFXO operation of core clock */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);

  /* on Giant & Leopard we have to reduce core clock when
   * talking with slow TFT controller */
  CMU_ClockDivSet(cmuClock_CORE, cmuClkDiv_2);

  CMU_ClockEnable( cmuClock_GPIO, true);
  CMU_ClockEnable( cmuClock_ADC0, true);
  LCD_InitializeLCD();
}
static int efm32_uart_setup(struct uart *dev, const struct uart_params *params) {

	LEUART_TypeDef      *leuart = (void *) dev->base_addr;
	LEUART_Init_TypeDef init    = LEUART_INIT_DEFAULT;

	/* Enable CORE LE clock in order to access LE modules */
	CMU_ClockEnable(cmuClock_HFPER, true);

	/* Enable CORE LE clock in order to access LE modules */
	CMU_ClockEnable(cmuClock_GPIO, true);

	/* Enable CORE LE clock in order to access LE modules */
	CMU_ClockEnable(cmuClock_CORELE, true);

	/* Select LFXO for LEUARTs (and wait for it to stabilize) */
	CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);

	CMU_ClockEnable(cmuClock_LEUART0, true);

	/* Do not prescale clock */
	CMU_ClockDivSet(cmuClock_LEUART0, cmuClkDiv_1);

	/* Configure GPIO pin for UART TX */
	/* To avoid false start, configure output as high. */
	GPIO_PinModeSet( BSP_BCC_TXPORT, BSP_BCC_TXPIN, gpioModePushPull, 1 );
	/* Configure GPIO pin for UART RX */
	GPIO_PinModeSet( BSP_BCC_RXPORT, BSP_BCC_RXPIN, gpioModeInput, 1 );

	/* Configure LEUART */
	init.enable = leuartDisable;
	init.baudrate = 9600;
	LEUART_Init(leuart, &init);

	/* Enable the switch that enables UART communication. */
	GPIO_PinModeSet( BSP_BCC_ENABLE_PORT, BSP_BCC_ENABLE_PIN, gpioModePushPull, 1 );
	BSP_BCC_LEUART->ROUTE |= LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | BSP_BCC_LOCATION;

	/* Finally enable it */
	LEUART_Enable(leuart, leuartEnable);

	return 0;
}
void system_init(uint8_t* tx_buffer, uint16_t tx_buffer_size, uint8_t* rx_buffer, uint16_t rx_buffer_size)
{
    /* Chip errata */
    CHIP_Init();

    //if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1) ;

    // init clock
    CMU_ClockDivSet(cmuClock_HF, cmuClkDiv_2);       // Set HF clock divider to /2 to keep core frequency < 32MHz
    CMU_OscillatorEnable(cmuOsc_HFXO, true, true);   // Enable XTAL Osc and wait to stabilize
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO); // Select HF XTAL osc as system clock source. 48MHz XTAL, but we divided the system clock by 2, therefore our HF clock will be 24MHz

    led_init();
    //button_init();
    uart_init();

    // TODO not hardware specific
    queue_init_with_header(&tx_queue, tx_buffer, tx_buffer_size, 1, 30);
    queue_init(&rx_queue, rx_buffer, rx_buffer_size, 1);
}
void __efm32lg_mcu_init()
{
    /* Chip errata */
    CHIP_Init();


#ifdef HW_USE_HFXO
    // init clock with HFXO (external)
    CMU_ClockDivSet(cmuClock_HF, cmuClkDiv_2);		// 24 MHZ
    CMU_OscillatorEnable(cmuOsc_HFXO, true, true);   // Enable XTAL Osc and wait to stabilize
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO); // Select HF XTAL osc as system clock source. 48MHz XTAL, but we divided the system clock by 2, therefore our HF clock will be 24MHz
    //CMU_ClockDivSet(cmuClock_HFPER, cmuClkDiv_4); // TODO set HFPER clock divider (used for SPI) + disable gate clock when not used?
#else
    // init clock with HFRCO (internal)
    CMU_HFRCOBandSet(cmuHFRCOBand_21MHz);
    CMU_OscillatorEnable(cmuOsc_HFRCO, true, true);
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO);
#endif

    uint32_t hf = CMU_ClockFreqGet(cmuClock_HF);
}
/******************************************************************************
 * @brief   Configure RTC
 *
 *****************************************************************************/
void rtcSetup(void)
{
  RTC_Init_TypeDef rtcInit;

  /* Select LFXO as clock source for the RTC */
  CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
  CMU_ClockEnable(cmuClock_RTC, true);
  
  /* Set RTC pre-scaler */
  CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_1);
   
  /* Enable clock to low energy modules */
  CMU_ClockEnable(cmuClock_CORELE, true);

  /* RTC configuration struct */
  rtcInit.debugRun  = false;
  rtcInit.comp0Top  = false;
  rtcInit.enable    = false;

  /* Initialize RTC */
  RTC_Init(&rtcInit);

  /* Set RTC compare value for first display update */
  RTC_CompareSet(0, RTC_COUNTS_BETWEEN_DISPLAY );
  
  /* Set RTC compare value for first temperature compensation */
  RTC_CompareSet(1, RTC_COUNTS_BETWEEN_TC );

  /* Enable COMP0 interrupt to trigger display update, */
  /*        COMP1 interrupt to trigger temperature compensation, and */
  /*        OF interrupt to keep track of counter overflows */
  RTC_IntEnable(RTC_IEN_COMP0 | RTC_IEN_COMP1 | RTC_IEN_OF );
  
  /* Enable RTC interrupts */
  NVIC_ClearPendingIRQ(RTC_IRQn);
  NVIC_EnableIRQ(RTC_IRQn);

  /* Enable RTC */
  RTC_Enable(true);
}
Exemple #26
0
/**************************************************************************//**
 * @brief  Main function
 *****************************************************************************/
int main(void)
{
  uint32_t j;
  
  /* Chip errata */
  CHIP_Init();

  /* Select clock source for HF clock. */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
  
  /* Prescale the core clock -> HF/4 = 32/4 = 8Mhz */
  CMU_ClockDivSet(cmuClock_CORE, cmuClkDiv_4);

  /* Configure push button interrupts. */
  gpioSetup();
  
  /* configure SWO output for debugging. */
  setupSWO();
  
  /* Init Segment LCD without boost. */
  SegmentLCD_Init(false);

  /* Turn on relevant symbols on the LCD. */
  SegmentLCD_Symbol(LCD_SYMBOL_GECKO, true);

  /* Print welcome text on the LCD. */
  SegmentLCD_Write("HiJack");

  /* Init the HiJack interface. */
  HIJACK_Init(NULL);
  
  /* While loop sending a range of values upon wakeup.  */
  while(1){    
    for(j = 0;j<254;j++){
      HIJACK_ByteTx(j);
      Delay(10000);
    }
    
  }   
}
Exemple #27
0
void debug_init ()
{
	GPIO_PinModeSet(PORT_SPK, PIN_SPK, gpioModePushPull, 0);

	LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT;

	CMU_ClockEnable(cmuClock_HFPER, true);
	CMU_ClockEnable(cmuClock_GPIO, true);

	CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
	CMU_ClockDivSet(cmuClock_LEUART0, cmuClkDiv_1);
	CMU_ClockEnable(cmuClock_LEUART0, true);

	init.refFreq =  0; //14MHz / 2 pre-scaled by 4 = 1750000;
	init.enable =   leuartDisable;
	init.baudrate = 9600;
	init.databits = leuartDatabits8;
	init.parity =   leuartNoParity;
	init.stopbits = leuartStopbits1;

	// Reseting and initializing LEUART0
	LEUART_Reset(LEUART0);
	LEUART_Init(LEUART0, &init);

	GPIO_PinModeSet(PORT_TXD, PIN_TXD, gpioModePushPull, 1);
	GPIO_PinModeSet(PORT_RXD, PIN_RXD, gpioModeInput, 0);

	LEUART0->ROUTE = LEUART_ROUTE_TXPEN | LEUART_ROUTE_RXPEN | LEUART_ROUTE_LOCATION_LOC0;
	LEUART0->CMD = LEUART_CMD_TXDIS | LEUART_CMD_RXDIS | LEUART_CMD_CLEARTX | LEUART_CMD_CLEARRX;
	LEUART0->CMD = LEUART_CMD_TXEN | LEUART_CMD_RXEN;

	// Eventually enable UART
	LEUART_Enable(LEUART0, leuartEnable);

	// print banner
    debug_str("\r\n============= DEBUG STARTED =============\r\n");
}
Exemple #28
0
/********************************************//**
 * \brief configure RTC to run from 32K external crystal
 * configure RTC divider to count seconds
 * \param
 * \param
 * \return
 *
 ***********************************************/
void initClock(void)
{
    // set RTC to generate interrupts every second time and date will be maintained by software
    /* Starting LFXO and waiting until it is stable */
    CMU_OscillatorEnable(cmuOsc_LFXO, true, true);

    /* Routing the LFXO clock to the RTC */
    CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
    CMU_ClockEnable(cmuClock_RTC, true);
    /* Enabling clock to the interface of the low energy modules */
    CMU_ClockEnable(cmuClock_CORELE, true);

    /* Setting up RTC */
    /* Prescaler of 15 = 1 s of resolution and overflow each 194 days */
    CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_32768);
    RTC_CompareSet(0, RTC_COUNT_BETWEEN_WAKEUP);
    RTC_IntEnable(RTC_IFC_COMP0);

    /* Enabling Interrupt from RTC */
    NVIC_EnableIRQ(RTC_IRQn);

    RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;
    RTC_Init(&rtcInit);
}
Exemple #29
0
void IO_Init(void)
{
    I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
    RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT;
    USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT;
    RTC_Init_TypeDef rtcInits = RTC_INIT_DEFAULT;
    ADC_Init_TypeDef adcInit = ADC_INIT_DEFAULT;
    volatile uint32_t test = 0;
    
    /* Enable LE clock and LFXO oscillator */
    CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_LE;
    CMU->OSCENCMD |= CMU_OSCENCMD_LFXOEN;
    /* Wait until LFXO ready */
    /* Note that this could be done more energy friendly with an interrupt in EM1 */
    while (!(CMU->STATUS & CMU_STATUS_LFXORDY)) ;
    
    /* Select LFXO as clock source for LFACLK */
    CMU->LFCLKSEL = (CMU->LFCLKSEL & ~_CMU_LFCLKSEL_LFA_MASK) | CMU_LFCLKSEL_LFA_LFXO;
    
    CMU->OSCENCMD = CMU_OSCENCMD_LFRCOEN;
    
    /* Enable GPIO clock */
    CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
    /* Initialize USART */
    CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_UART1;
    
    GPIO->P[4].DOUT |= (1 << 2);
    GPIO->P[4].MODEL =
    GPIO_P_MODEL_MODE2_PUSHPULL
    | GPIO_P_MODEL_MODE3_INPUT;
    
    
    
    
    GPIO->P[1].DOUT |= (1 << 3);
    /* Pin PB3 is configured to Push-pull */
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE3_MASK) | GPIO_P_MODEL_MODE3_PUSHPULL;
    /* Pin PB4 is configured to Input enabled */
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE4_MASK) | GPIO_P_MODEL_MODE4_INPUT;
    /* Pin PB5 is configured to Push-pull */
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE5_MASK) | GPIO_P_MODEL_MODE5_PUSHPULL;
    
    /* To avoid false start, configure output NRF_CSN as high on PB2 */
    GPIO->P[1].DOUT |= (1 << 2);
    /* Pin PB2 (NRF_CSN) is configured to Push-pull */
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE2_MASK) | GPIO_P_MODEL_MODE2_PUSHPULL;
    /* NRF_CE set to initial low */
    GPIO->P[1].DOUT &= ~(1 << 1);
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE1_MASK) | GPIO_P_MODEL_MODE1_PUSHPULL;
    // NRF_INT
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE0_MASK) | GPIO_P_MODEL_MODE0_INPUT;
    // RXEN
    GPIO->P[0].DOUT &= ~(1 << 2);
    GPIO->P[0].MODEL = (GPIO->P[0].MODEL & ~_GPIO_P_MODEL_MODE2_MASK) | GPIO_P_MODEL_MODE2_PUSHPULL;
    
    // US2_CS (MX25U64)
    GPIO->P[1].DOUT |= (1 << 6);
    GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE6_MASK) | GPIO_P_MODEL_MODE6_PUSHPULL;
    
    
    
    // I2C0 SCL
    // I2C0 SDA
    /* Enable clock for I2C0 */
    CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_I2C0;
    /* Enable signals SDA, SCL */
    I2C0->ROUTE |= I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | I2C_ROUTE_LOCATION_LOC3;
    
    /* Enable clock for LETIMER0 */
    CMU->LFACLKEN0 |= CMU_LFACLKEN0_LETIMER0;
    
    /* Enable clock for RTC */
    CMU->LFACLKEN0 |= CMU_LFACLKEN0_RTC;
    
    
    CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_USART2;
    usartInit.msbf = true;
    usartInit.clockMode = usartClockMode0;
    usartInit.baudrate = 7000000;
    USART_InitSync(USART2, &usartInit);
    //USART_Enable(USART1, usartEnable);
    USART2->ROUTE = (USART2->ROUTE & ~_USART_ROUTE_LOCATION_MASK) | USART_ROUTE_LOCATION_LOC1;
    /* Enable signals TX, RX, CLK */
    USART2->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN;
    
    GPIO_PinModeSet(gpioPortA, 0, gpioModeWiredAnd, 1);
    GPIO_PinModeSet(gpioPortA, 1, gpioModeWiredAnd, 1);
    GPIO_PinModeSet(gpioPortA, 3, gpioModeWiredAnd, 1);
    
    
    
    /* Use location 3: SDA - Pin D14, SCL - Pin D15 */
    /* Output value must be set to 1 to not drive lines low... We set */
    /* SCL first, to ensure it is high before changing SDA. */
    GPIO_PinModeSet(gpioPortD, 15, gpioModeWiredAnd, 1);
    GPIO_PinModeSet(gpioPortD, 14, gpioModeWiredAnd, 1);
    I2C_Init(I2C0, &i2cInit);
    I2C0->CLKDIV=1;
    
    // MMA_INT
    GPIO_PinModeSet(gpioPortA, 15, gpioModeInput, 0);
    GPIO_IntConfig(gpioPortA, 15, false, true, true);
    
    // NRF_INT
    GPIO_IntConfig(gpioPortB, 0, false, true, true);
    
    NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn);
    NVIC_EnableIRQ(GPIO_EVEN_IRQn);
    NVIC_ClearPendingIRQ(GPIO_ODD_IRQn);
    NVIC_EnableIRQ(GPIO_ODD_IRQn);
    CMU_ClockEnable(cmuClock_RTC, true);
    CMU_ClockEnable(cmuClock_ADC0, true);
    
    /* Each RTC tick is  second */
    
    CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1);
    
    RTC_Reset();
    /* Enable RTC */
    //RTC_Enable(true);
    rtcInits.comp0Top = false;
    RTC_Init(&rtcInits);
    // RTC_Init(&rtcInit);
    //test = NRF_Status();
    //buf[0] = test;

}
/***************************************************************************//**
 * @brief
 *    Initialize RTCDRV driver.
 *
 * @details
 *    Will enable all necessary clocks. Initializes internal datastructures
 *    and configures the underlying hardware timer.
 *
 * @return
 *    @ref ECODE_EMDRV_RTCDRV_OK.
 ******************************************************************************/
Ecode_t RTCDRV_Init( void )
{
  if ( rtcdrvIsInitialized == true ) {
    return ECODE_EMDRV_RTCDRV_OK;
  }
  rtcdrvIsInitialized = true;

  // Ensure LE modules are clocked.
  CMU_ClockEnable( cmuClock_CORELE, true );

#if defined( CMU_LFECLKEN0_RTCC )
  // Enable LFECLK in CMU (will also enable oscillator if not enabled).
  CMU_ClockSelectSet( cmuClock_LFE, RTCDRV_OSC );
#else
  // Enable LFACLK in CMU (will also enable oscillator if not enabled).
  CMU_ClockSelectSet( cmuClock_LFA, RTCDRV_OSC );
#endif

#if defined( RTCDRV_USE_RTC )
  // Set clock divider.
  CMU_ClockDivSet( cmuClock_RTC, RTC_DIVIDER );

  // Enable RTC module clock.
  CMU_ClockEnable( cmuClock_RTC, true );

  // Initialize RTC.
  RTC_Init( &initRTC );

#elif defined( RTCDRV_USE_RTCC )
  // Set clock divider.
  initRTCC.presc = (RTCC_CntPresc_TypeDef)CMU_DivToLog2( RTC_DIVIDER );

  // Enable RTCC module clock.
  CMU_ClockEnable( cmuClock_RTCC, true );

  // Initialize RTCC.
  RTCC_Init( &initRTCC );

  // Set up Compare channel.
  RTCC_ChannelInit( 1, &initRTCCCompareChannel );
#endif

  // Disable RTC/RTCC interrupt generation.
  RTC_INTDISABLE( RTC_ALL_INTS );
  RTC_INTCLEAR( RTC_ALL_INTS );

  RTC_COUNTERRESET();

  // Clear and then enable RTC interrupts in NVIC.
  NVIC_CLEARPENDINGIRQ();
  NVIC_ENABLEIRQ();

#if defined( EMDRV_RTCDRV_WALLCLOCK_CONFIG )
  // Enable overflow interrupt for wallclock.
  RTC_INTENABLE( RTC_OF_INT );
#endif

  // Reset RTCDRV internal data structures/variables.
  memset( timer, 0, sizeof( timer ) );
  inTimerIRQ             = false;
  rtcRunning             = false;
  startTimerNestingLevel = 0;
#if defined( EMODE_DYNAMIC )
  sleepBlocked           = false;
#endif

#if defined( EMDRV_RTCDRV_WALLCLOCK_CONFIG )
  wallClockOverflowCnt = 0;
  wallClockTimeBase    = 0;

#if defined( EMODE_NEVER_ALLOW_EM3EM4 )
  // Always block EM3 and EM4 if wallclock is running.
  SLEEP_SleepBlockBegin( sleepEM3 );
#endif

#endif

  return ECODE_EMDRV_RTCDRV_OK;
}