SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
example.cpp File Reference

Low-power demo with sleep and stop modes (STM32F407G-DISC1). More...

#include <stdio.h>
#include <stk.h>
#include <sync/stk_sync.h>
#include <time/stk_time.h>
#include "example.h"
Include dependency graph for low_power/example.cpp:

Go to the source code of this file.

Classes

struct  AdcSample
 One ADC reading delivered through the pipe. More...
class  LowPowerOverrider
class  LedTask< _Priority, _AccessMode >
class  AdcTimer
class  LogTask< _AccessMode >

Namespaces

namespace  Board

Enumerations

enum  { TASK_STACK_SIZE = 256 }

Functions

static void Board::LedInit ()
static __stk_forceinline void Board::LedSet (bsp::Led::Id pin, bool on)
static __stk_forceinline void Board::LedSwitchOnExclusive (bsp::Led::Id pin)
static void Board::ButtonInit ()
static void Board::AdcInit ()
static __stk_forceinline void Board::AdcSuspend ()
static __stk_forceinline void Board::AdcResume ()
static __stk_forceinline uint16_t Board::AdcRead ()
static __stk_forceinline float Board::TEMP_ValueToDeg (uint32_t sens)
static __stk_forceinline void Board::RestorePllClock ()
static __stk_forceinline void Board::CpuEnterDeepSleepMode ()
static __stk_forceinline void Board::CpuEnterSleepMode ()
static void Board::RtcWakeupInit ()
static __stk_forceinline void Board::RtcWakeupArm (stk::Timeout sleep_ticks)
static __stk_forceinline void Board::RtcWakeupDisarm ()
void RTC_WKUP_IRQHandler ()
void EXTI0_IRQHandler ()
void RunExample ()

Variables

static const uint32_t FLAGS_ALL []
static stk::sync::EventFlags g_TaskFlags (FLAGS_ALL[LED_RED])
static stk::Ticks g_Timeline = 0
static stk::sync::PipeT< AdcSample, 8 > g_AdcPipe
static volatile bool g_KernelSuspended = false
const uint32_t Board::RTC_WKUP_COUNTS_PER_TICK = 2U
const uint32_t Board::RTC_WKUP_GUARD_COUNTS = 2U
const uint32_t Board::RTC_WKUP_MAX_COUNT = 0xFFFFU

Detailed Description

Low-power demo with sleep and stop modes (STM32F407G-DISC1).

Definition in file low_power/example.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
TASK_STACK_SIZE 

Definition at line 592 of file low_power/example.cpp.

592{ TASK_STACK_SIZE = 256 };
#define TASK_STACK_SIZE

Function Documentation

◆ EXTI0_IRQHandler()

void EXTI0_IRQHandler ( void )

Definition at line 766 of file low_power/example.cpp.

767{
769
771 {
772 // Suspend() is ISR-safe. It returns the number of ticks until the
773 // nearest scheduled wake-up (useful for tickless operation), but we
774 // do not need that value here since we resume on the next button press.
775 kernel->Suspend();
776
777 g_KernelSuspended = true;
778
779 // Light the BLUE LED to signal the suspended state, all other - off.
781 }
782 else
783 {
784 // Resume() with 0 ticks because we could sleep indefinitely long, so it
785 // is similar to restore from hibernation.
786 kernel->Resume(0);
787
788 g_KernelSuspended = false;
789
790 // Extinguish the suspended-state indicator.
792 }
793
794 // Debounce button press
795 for (volatile int i = 0; i < 1000000; ++i);
796
797 // Clear the EXTI pending bit first to acknowledge the interrupt.
798 EXTI->PR = (1U << 0);
799}
static __stk_forceinline void LedSwitchOnExclusive(bsp::Led::Id pin)
static void LedInit()
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual void Resume(Timeout elapsed_ticks)=0
Resume scheduling after a prior Suspend() call.
virtual Timeout Suspend()=0
Suspend scheduling.
static constexpr Id BLUE
Definition led.h:37
static volatile bool g_KernelSuspended

References bsp::Led::BLUE, g_KernelSuspended, stk::IKernelService::GetInstance(), Board::LedInit(), Board::LedSwitchOnExclusive(), stk::IKernelService::Resume(), and stk::IKernelService::Suspend().

Here is the call graph for this function:

◆ RTC_WKUP_IRQHandler()

void RTC_WKUP_IRQHandler ( void )
Examples
low_power/example.cpp, and low_power_c/example.c.

Definition at line 446 of file low_power/example.cpp.

447{
448 // Clear the RTC wakeup flag (inside write-protected domain)
449 RTC->WPR = 0xCA;
450 RTC->WPR = 0x53;
451 RTC->ISR &= ~RTC_ISR_WUTF;
452 RTC->WPR = 0xFF;
453
454 // Clear EXTI line 22 pending bit
455 EXTI->PR = (1U << 22);
456}

◆ RunExample()

void RunExample ( void )

Definition at line 805 of file low_power/example.cpp.

806{
807 using namespace stk;
808
809 // - Board initialization -------------------------------------------------
810 Board::LedInit(); // LEDs
811 Board::AdcInit(); // CPU temperature reader
812 Board::ButtonInit(); // Standby button
813 Board::RtcWakeupInit(); // RTC for deep-sleep wake-up
814
815 // - Kernel setup ---------------------------------------------------------
816 // KERNEL_STATIC - tasks never exit (required for the while(true) pattern).
817 // KERNEL_TICKLESS - kernel suppresses SysTick firings when all tasks sleep,
818 // amplifying the power benefit of OnSleep()+WFI.
819 //
820 // Task count reduced from 5 to 4: the former AdcTask is replaced by an
821 // AdcTimer managed by TimerHost (which adds its own internal tasks).
822 const uint8_t KernelMode = KERNEL_STATIC | KERNEL_SYNC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0);
823
824 // 4 user tasks: 3 LED + 1 Log.
825 // TimerHost::TASK_COUNT additional internal tasks are registered separately.
827
828 // Tasks run in privileged mode - GPIO and ADC registers require it on
829 // Cortex-M4 when the MPU restricts peripheral access to privileged code.
830 static LedTask<ACCESS_PRIVILEGED> task_red (0, "LED-red");
831 static LedTask<ACCESS_PRIVILEGED> task_grn (1, "LED-grn");
832 static LedTask<ACCESS_PRIVILEGED> task_org (2, "LED-org");
833 static LogTask<ACCESS_PRIVILEGED> task_log;
834
835 kernel.Initialize();
836
837 // - TimerHost setup ------------------------------------------------------
838 // Initialize the host and register it with the kernel. Must be called
839 // after kernel.Initialize() and before kernel.Start().
840 // ACCESS_PRIVILEGED is required so the handler task can call AdcRead()
841 // which accesses peripheral registers.
842 static stk::time::TimerHost timer_host;
843 timer_host.Initialize(&kernel, ACCESS_PRIVILEGED);
844
845 // ADC sampling timer: periodic, fires every 1 s (~1 Hz).
846 static AdcTimer adc_timer;
847 const uint32_t adc_period = stk::GetTicksFromMs(2000);
848 timer_host.Start(adc_timer, adc_period, adc_period);
849
850 // - Low-power event overrider --------------------------------------------
851 // Must be registered before kernel.Start() so the platform driver sees it
852 // from the very first idle period.
853 static LowPowerOverrider lp_overrider;
854 kernel.GetPlatform()->SetEventOverrider(&lp_overrider);
855
856 // - Start scheduling -----------------------------------------------------
857 kernel.AddTask(&task_red);
858 kernel.AddTask(&task_grn);
859 kernel.AddTask(&task_org);
860 kernel.AddTask(&task_log);
861
862 // Start() never returns in KERNEL_STATIC mode.
863 kernel.Start();
864
865 STK_ASSERT(false);
866}
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
Namespace of STK package.
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:38
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:406
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
Definition stk_common.h:51
@ KERNEL_SYNC
Synchronization support (see Event).
Definition stk_common.h:50
@ KERNEL_STATIC
All tasks are static and can not exit.
Definition stk_common.h:47
static void AdcInit()
static void ButtonInit()
static void RtcWakeupInit()
Concrete implementation of IKernel.
Definition stk.h:102
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:1999
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:1465
IPlatform * GetPlatform() override
Get platform driver instance owned by this kernel.
Definition stk.h:2034
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Definition stk.h:1425
virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure=false)=0
Set platform event overrider.
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kerne...
EResult Start(Timer &tmr, uint32_t delay, uint32_t period=0)
Start timer.
EAddResult Initialize(IKernel *kernel, EAccessMode mode, const Config &cfg=Config())
Initialize timer host instance.

References stk::ACCESS_PRIVILEGED, Board::AdcInit(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AddTask(), Board::ButtonInit(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::GetPlatform(), stk::GetTicksFromMs(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Initialize(), stk::time::TimerHost::Initialize(), stk::KERNEL_STATIC, stk::KERNEL_SYNC, stk::KERNEL_TICKLESS, Board::LedInit(), Board::RtcWakeupInit(), stk::IPlatform::SetEventOverrider(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Start(), stk::time::TimerHost::Start(), and STK_ASSERT.

Here is the call graph for this function:

Variable Documentation

◆ FLAGS_ALL

const uint32_t FLAGS_ALL[]
static
Initial value:
= {
(1U << LED_RED),
(1U << LED_GREEN),
(1U << LED_ORANGE)
}
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_RED
Definition led.h:19

Definition at line 71 of file low_power/example.cpp.

71 {
72 (1U << LED_RED),
73 (1U << LED_GREEN),
74 (1U << LED_ORANGE)
75};

◆ g_AdcPipe

◆ g_KernelSuspended

volatile bool g_KernelSuspended = false
static

◆ g_TaskFlags

stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED]) ( FLAGS_ALL [LED_RED])
static

◆ g_Timeline

stk::Ticks g_Timeline = 0
static

Definition at line 81 of file low_power/example.cpp.