text: /***************************************************************************//**
   * @file main_tg11.c
   * @brief This project demonstrates output compare in EM2 using the LETIMER.
   * After 1 second an output pin is set high (see README)
   *******************************************************************************
   * # License
   * <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
   *******************************************************************************
   *
   * SPDX-License-Identifier: Zlib
   *
   * The licensor of this software is Silicon Laboratories Inc.
   *
   * This software is provided 'as-is', without any express or implied
   * warranty. In no event will the authors be held liable for any damages
   * arising from the use of this software.
   *
   * Permission is granted to anyone to use this software for any purpose,
   * including commercial applications, and to alter it and redistribute it
   * freely, subject to the following restrictions:
   *
   * 1. The origin of this software must not be misrepresented; you must not
   *    claim that you wrote the original software. If you use this software
   *    in a product, an acknowledgment in the product documentation would be
   *    appreciated but is not required.
   * 2. Altered source versions must be plainly marked as such, and must not be
   *    misrepresented as being the original software.
   * 3. This notice may not be removed or altered from any source distribution.
   *
   *******************************************************************************
   * # Evaluation Quality
   * This code has been minimally tested to ensure that it builds and is suitable 
   * as a demonstration for evaluation purposes only. This code will be maintained
   * at the sole discretion of Silicon Labs.
   ******************************************************************************/
  
  #include "em_device.h"
  #include "em_chip.h"
  #include "em_cmu.h"
  #include "em_emu.h"
  #include "em_gpio.h"
  #include "em_letimer.h"
  
  #include "bsp.h"
  
  /**************************************************************************//**
   * @brief GPIO initialization
   *****************************************************************************/
  void initGpio(void)
  {
    // Enable GPIO and clock
    CMU_ClockEnable(cmuClock_GPIO, true);
  
    // Configure PD6 (Expanion Header pin 16) as output
    GPIO_PinModeSet(gpioPortD, 6, gpioModePushPull, 0);
  }
  
  /**************************************************************************//**
   * @brief TIMER initialization
   *****************************************************************************/
  void initLetimer(void)
  {
    LETIMER_Init_TypeDef letimerInit = LETIMER_INIT_DEFAULT;
  
    // Enable clock to the LE modules interface
    CMU_ClockEnable(cmuClock_HFLE, true);
  
    // Select LFXO for the LETIMER
    CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
    CMU_ClockEnable(cmuClock_LETIMER0, true);
  
    // Reload COMP0 on underflow, set output, and run in one-shot mode
    letimerInit.comp0Top  = true;
    letimerInit.ufoa0     = letimerUFOAToggle;
    letimerInit.repMode   = letimerRepeatOneshot;
    letimerInit.enable = false;
    letimerInit.topValue = 32000;
  
    // Need REP0 != 0 to run until the first event
    LETIMER_RepeatSet(LETIMER0, 0, 1);
  
    // Enable LETIMER0 output0 on PD6 (Route 00)
    LETIMER0->ROUTEPEN |=  LETIMER_ROUTEPEN_OUT0PEN;
    LETIMER0->ROUTELOC0 |= LETIMER_ROUTELOC0_OUT0LOC_LOC0;
  
    // Initialize and enable LETIMER
    LETIMER_Init(LETIMER0, &letimerInit);
  
     // Compare on wake-up interval count
    LETIMER_Enable(LETIMER0,true);
  }
  
  /**************************************************************************//**
   * @brief  Main function
   *****************************************************************************/
  int main(void)
  {
    // Chip errata
    CHIP_Init();
  
    // Initializations
    initGpio();
    initLetimer();
  
    // Enter low energy state
    while (1)
    {
      EMU_EnterEM2(true);
    }
  }
  