Note: This KBA has been marked as deprecated. A more updated KBA can be found here:Using Low-Energy Timers
Introduction
The Low Energy Timer is a 2 channel 16-bit down count timer and is available in energy mode EM2 DeepSleep, EM1 Sleep, and EM0 Active. Because of this, it can be used for timing and output generation when most of the device is powered down, allowing simple tasks to be performed while the power consumption of the system is kept at an absolute minimum. Timer runs from the LFACLK which can be clocked by the LFXO, LFRCO or HFCORECLKLE/2. In my example LFXO is used as an clock source and it has frequency of 32768 Hz. The LETIMER can be used to output a variety of waveforms with minimal software intervention. It can also be connected to the Real-Time Counter (RTC) using the Peripheral Reflex System (PRS), and can be configured to start counting on compare matches from the RTC. Supported waveforms:
Toggle output pin
Apply a positive pulse (pulse width of one LFACLKLETIMER period)
PWM
LETIMER also support interrupts from the following events:
Compare matches
Timer underflow
Repeat done
In this example, I show how to use Low Energy timer with a simple software example. This example is created for the BG13 but is easily portable to other devices as well. More detailed information about the Low Energy Timer can be found from the AN0026 document and EFR32xG1x Wireless Gecko Reference Manual.
Requirements:
Wireless Starter Kit and EFR32XG1X based radio board (Or your own solution with minor modifications to the code).
Implementation
I have divided the implementation to 6 steps. Example application file, which can be copied on top of SoC - Empty, is also attached below.
Create a new SoC – Empty project for your preferred device, Bluetooth SDK and compiler.
After the project is created, the BLE GATT Configurator is opened automatically and it shows the GATT database of the project. The project template includes a minimal GATT database that includes three services (Generic Access, Device Information and Silicon Labs OTA).
Copy em_letimer.c and em_letimer.h file to the project folder.
Find your project folder in Simplicity Studio and open emlib folder.
Navigate to emlib folder in the SDK directories (location is at C:\SiliconLabs\SimplicityStudio\vX\developer\sdks\gecko_sdk_suite\vY\platform\emlib\src (and inc)).
Find em_letimer.c/.h file and copy it to your project emlib folder in Simplicity Studio.
Include LE Timer, CMU and GPIO libraries in app.c file. These libraries are required for PWM signal.
4. Include LETIMER_setup function. This function initializes LETIMER and routes timer output to WSTK led pins. Please keep in mind that if you are not using BG13 and LED output pins are not PF4 and PF5, you need to configure your timer routing location.
E.g. for BGM111, the respective pins are PF6 and PF7 and routing location 30.
static void LETIMER_setup(void)
{
/* Enable necessary clocks */
CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
CMU_ClockEnable(cmuClock_CORELE, true);
CMU_ClockEnable(cmuClock_LETIMER0, true);
CMU_ClockEnable(cmuClock_GPIO, true);
/* WSTK LED pins are configured to Push-pull */
GPIO_PinModeSet(BSP_LED0_PORT, BSP_LED0_PIN, gpioModePushPull, 0);
GPIO_PinModeSet(BSP_LED1_PORT, BSP_LED1_PIN, gpioModePushPull, 0);
/* Repetition values must be nonzero so that the outputs
return switch between idle and active state */
LETIMER_RepeatSet(LETIMER0, 0, 0x01);
LETIMER_RepeatSet(LETIMER0, 1, 0x01);
// This configuration is valid only if PF4 and PF5 are
// used for output signal. Change this "LOC28" value to
// preferred value that can be found from Device datasheet
// section Alternate Functionality Pinout.
/* Set up PF4 pin <- LETIM0_OUT0 */
LETIMER0->ROUTELOC0 = (LETIMER0->ROUTELOC0
& ~_LETIMER_ROUTELOC0_OUT0LOC_MASK)
| LETIMER_ROUTELOC0_OUT0LOC_LOC28;
LETIMER0->ROUTEPEN |= LETIMER_ROUTEPEN_OUT0PEN;
/* Set up PF5 pin <- LETIM0_OUT1 */
LETIMER0->ROUTELOC0 = (LETIMER0->ROUTELOC0
& ~_LETIMER_ROUTELOC0_OUT1LOC_MASK)
| LETIMER_ROUTELOC0_OUT1LOC_LOC28;
LETIMER0->ROUTEPEN |= LETIMER_ROUTEPEN_OUT1PEN;
/* Set configurations for LETIMER 0 */
const LETIMER_Init_TypeDef LETIMER_INITIAL_CONFIG = {
.enable = true, /* Start counting when init completed. */
.debugRun = false, /* Counter shall not keep running during debug halt. */
.comp0Top = true, /* Load COMP0 register into CNT when counter underflows. COMP0 is used as TOP */
.bufTop = false, /* Don't load COMP1 into COMP0 when REP0 reaches 0. */
.out0Pol = 0, /* Idle value for output 0. */
.out1Pol = 0, /* Idle value for output 1. */
.ufoa0 = letimerUFOAPwm, /* PWM output on output 0 */
.ufoa1 = letimerUFOAPwm, /* PWM output on output 1*/
.repMode = letimerRepeatFree /* Count until stopped */
};
/* Initialize LETIMER */
LETIMER_Init(LETIMER0, &LETIMER_INITIAL_CONFIG);
// LETimer is 16-bit down counting timer and oscillator frequency is 32768 Hz.
// PWM duty-cycle and frequency can be determined using these compare values.
// In short, the first one determines frequency and second one determines duty-cycle.
// Frequency = 32768 / COMP0, Duty-cycle [%] = COMP1 / COMP0 * 100
LETIMER_CompareSet(LETIMER0, 0, 32768); // COMP0
LETIMER_CompareSet(LETIMER0, 1, 32768 / 2); // COMP1
}
5. Call LETIMER setup function after stack initialization.
// Initialize LETIMER
LETIMER_setup();
6. Observe the LEDs blinking at roughly 1 Hz, duty-cycle 50 %. You can verify that your device goes to sleep with Energy Profiler. Peaks are sections where LEDs are turned on and lows are sections where LEDs are off. Changing the PWM frequency and duty-cycle is done by adjusting the above compare value registers according to commented formulas.
[Deprecated] KBA_BT_1002: Using Low Energy Timers
Note: This KBA has been marked as deprecated. A more updated KBA can be found here: Using Low-Energy Timers
Introduction
The Low Energy Timer is a 2 channel 16-bit down count timer and is available in energy mode EM2 DeepSleep, EM1 Sleep, and EM0 Active. Because of this, it can be used for timing and output generation when most of the device is powered down, allowing simple tasks to be performed while the power consumption of the system is kept at an absolute minimum. Timer runs from the LFACLK which can be clocked by the LFXO, LFRCO or HFCORECLKLE/2. In my example LFXO is used as an clock source and it has frequency of 32768 Hz. The LETIMER can be used to output a variety of waveforms with minimal software intervention. It can also be connected to the Real-Time Counter (RTC) using the Peripheral Reflex System (PRS), and can be configured to start counting on compare matches from the RTC. Supported waveforms:
LETIMER also support interrupts from the following events:
In this example, I show how to use Low Energy timer with a simple software example. This example is created for the BG13 but is easily portable to other devices as well. More detailed information about the Low Energy Timer can be found from the AN0026 document and EFR32xG1x Wireless Gecko Reference Manual.
Requirements:
Implementation
I have divided the implementation to 6 steps. Example application file, which can be copied on top of SoC - Empty, is also attached below.
Create a new SoC – Empty project for your preferred device, Bluetooth SDK and compiler.
After the project is created, the BLE GATT Configurator is opened automatically and it shows the GATT database of the project. The project template includes a minimal GATT database that includes three services (Generic Access, Device Information and Silicon Labs OTA).
Copy em_letimer.c and em_letimer.h file to the project folder.
emlib
folder.C:\SiliconLabs\SimplicityStudio\vX\developer\sdks\gecko_sdk_suite\vY\platform\emlib\src (and inc))
.Include LE Timer, CMU and GPIO libraries in app.c file. These libraries are required for PWM signal.
Add these lines next to other includes:
4. Include
LETIMER_setup
function. This function initializes LETIMER and routes timer output to WSTK led pins. Please keep in mind that if you are not using BG13 and LED output pins are not PF4 and PF5, you need to configure your timer routing location.E.g. for BGM111, the respective pins are PF6 and PF7 and routing location 30.
5. Call LETIMER setup function after stack initialization.
6. Observe the LEDs blinking at roughly 1 Hz, duty-cycle 50 %. You can verify that your device goes to sleep with Energy Profiler. Peaks are sections where LEDs are turned on and lows are sections where LEDs are off. Changing the PWM frequency and duty-cycle is done by adjusting the above compare value registers according to commented formulas.
Further Reading
AN0026.1: EFM32 and EFR32 Wireless SoC Series 1 Low Energy Timer
EFR32BG13 Blue Gecko Bluetooth® Low Energy SoC Family Datasheet