The Ambiq Micro Apollo 2 SystemTimer (SysTimer)

A new feature of the new Apollo 2 MCU series is the so called SystemTimer. The SystemTimer or SysTimer is not the SysTick timer of the ARM Cortex M core, but adds new possibilities for real time behaviour scheduling in hardware with a little part of software. In addition it runs also in deepsleep modes!

In the datasheet it is explained: “The Apollo2 MCU System Timer (STIMER), shown above, tracks the global synchronized counter. It can be used for RTOS scheduling and real-time system tracking. This timer is provided in addition to the other timer peripherals to enable software/firmware to have a simple, globally synchronized timer source. The System Timer (STIMER) Module provides real time measurement for all task scheduling, sensor sample rate calibration, and tracking of real time and calendar maintenance.”

The timer itself integrates typical operation system schedulers in hardware. An 32-bit binary counter can be used for RTOS scheduling decisions and 8x 32-bit compare and interrupt registers can be used to facilitate light weight scheduling without RTOS.

Software

The following lines will use the compare slot 0 or compare register A. In the IRQ routine the timer is fired again with the next wakeup in WAKE_INTERVAL_IN_MS.

Defines

#define WAKE_INTERVAL_IN_MS 100
#define XT_PERIOD 3000000
#define WAKE_INTERVAL XT_PERIOD * WAKE_INTERVAL_IN_MS * 1e-3

IRQ Handler

void STIMER_CMPR0_IRQHandler(void)
{
     CTIMER->STMINTCLR_b.COMPAREA = 1;
     CTIMER->SCMPR0 = WAKE_INTERVAL;
     //update your task here…
}

Initialization

The initialization is using the

CTIMER->STCFG_b.CLEAR = 1;
CTIMER->STCFG_b.FREEZE = 1;

CTIMER->STCFG_b.CLKSEL = 1;
CTIMER->STCFG_b.COMPARE_A_EN = 1;
CTIMER->SCMPR0 = WAKE_INTERVAL;

CTIMER->STMINTEN_b.COMPAREA = 1;
NVIC_ClearPendingIRQ(STIMER_CMPR0_IRQn); //clear pending flag
NVIC_EnableIRQ(STIMER_CMPR0_IRQn); //enable IRQ
NVIC_SetPriority(STIMER_CMPR0_IRQn,1); //set priority, smaller value means higher priority

CTIMER->STCFG_b.CLEAR = 0;
CTIMER->STCFG_b.FREEZE = 0;

Initialize Low-Power

Following code lines can be added to enable the buck converters and switching off not required features:

//
// Enable buck converters
//
PWRCTRL->SUPPLYSRC_b.COREBUCKEN = 1;
PWRCTRL->SUPPLYSRC_b.MEMBUCKEN = 1;
while((PWRCTRL->POWERSTATUS_b.COREBUCKON != 1) && (PWRCTRL->POWERSTATUS_b.MEMBUCKON != 1)) __NOP();
PWRCTRL->SUPPLYSRC_b.SWITCH_LDO_IN_SLEEP = 0; // For lowest deep sleep power, make sure we stay in BUCK mode.

//
// For lowest power, we enable clock gating for all SRAM configuration.
//
PWRCTRL->SRAMCTRL_b.SRAM_MASTER_CLKGATE = 1;
PWRCTRL->SRAMCTRL_b.SRAM_CLKGATE = 1;
PWRCTRL->SRAMCTRL_b.SRAM_LIGHT_SLEEP = 1;

//
// Disable voltage comperator
//
VCOMP->PWDKEY = 0x37;

//
// Run the RTC off the LFRC.
//
CLKGEN->OCTRL_b.OSEL = 1;

//
// Stop the XT and LFRC
//
CLKGEN->OCTRL_b.STOPXT = 1;
CLKGEN->OCTRL_b.STOPRC = 1;

//
// Disable the RTC.
//
CLKGEN->RTCCTL_b.RSTOP = 1;

PWRCTRL->MEMEN_b.FLASH1 = 0; //enable just 512KB FLASH
PWRCTRL->MEMEN_b.SRAMEN = 0x001; //enable just 8KB RAM
PWRCTRL->SRAMPWDINSLEEP_b.SRAMSLEEPPOWERDOWN = 0x7FE; //All banks but lower 8k powered down

Go to deep sleep

In the main loop the whole application can go to deep-sleep while having the SystemTimer still active.

SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; //set goto deepsleep (only possible if TPIU or SWO is not enabled)
__WFI(); //wait for interrupt

Complete main.c

The complete example can be downloaded here and is workable with MCU Templates for Apollo 2 from FEEU http://www.fujitsu.com/feeu or by contacting  FEEU directly info.feeu@de.fujitsu.com

See also MCU Templates

Download: apollo2_systimer_example_main

Hardware Availability

The Apollo 2 evaluation board called AMAPHEVK is available for 55€ at the webshop of Fujitsu Electronics Europe GmbH (FEEU):

http://shop.feeu.com/Shops/es966226/Products/AMAPHEVK

Apollo 2 MCUs (AMAPH1KK-KBR and AMAPH1KK-KBR) are available as well: http://shop.feeu.com/Shops/es966226/Categories/Ultralow_power_MCUs_and_RTCs/Page__Category

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.