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.c File Reference

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

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stk_c.h>
#include <stk_c_time.h>
#include "example.h"
Include dependency graph for low_power_c/example.c:

Go to the source code of this file.

Classes

struct  AdcSample
 One ADC reading delivered through the pipe. More...
struct  LedTaskCtx

Macros

#define FLAG_RED   (1U << LED_RED)
#define FLAG_GREEN   (1U << LED_GREEN)
#define FLAG_ORANGE   (1U << LED_ORANGE)
#define RTC_WKUP_COUNTS_PER_TICK   2U
#define RTC_WKUP_GUARD_COUNTS   2U
#define RTC_WKUP_MAX_COUNT   0xFFFFU
#define TICKS_IDLE_SLEEP   8
#define TICKS_DEEP_SLEEP   40
#define TASK_STACK_SIZE   512

Functions

static void LedInit (void)
static void LedSet (int pin, bool on)
static void LedSwitchOnExclusive (int pin)
static void AdcInit (void)
static void AdcSuspend (void)
static void AdcResume (void)
static uint16_t AdcRead (void)
static float TEMP_ValueToDeg (uint32_t sens)
static void RestorePllClock (void)
static void CpuEnterDeepSleepMode (void)
static void CpuEnterSleepMode (void)
static void ButtonInit (void)
static void RtcWakeupInit (void)
static void RtcWakeupArm (int32_t sleep_ticks)
static void RtcWakeupDisarm (void)
void RTC_WKUP_IRQHandler (void)
static bool OnSleep (int32_t sleep_ticks, void *user_data)
static void AdcTimerCallback (stk_timerhost_t *host, stk_timer_t *timer, void *user_data)
static void LedTaskEntry (void *arg)
static void LogTaskEntry (void *arg)
void EXTI0_IRQHandler (void)
void RunExample (void)

Variables

static const uint32_t FLAGS_ALL [3] = { (1U << LED_RED) , (1U << LED_GREEN) , (1U << LED_ORANGE) }
static stk_ef_mem_t s_TaskFlagsMem
static stk_ef_t * g_TaskFlags
static stk_tick_t g_Timeline = 0
static stk_pipe_mem_t s_AdcPipeMem
static uint8_t s_AdcPipeBuf [8 *sizeof(AdcSample)]
static stk_pipe_t * g_AdcPipe
static stk_timer_mem_t s_AdcTimerMem
static volatile bool g_KernelSuspended = false
static stk_kernel_t * g_Kernel
static LedTaskCtx s_LedCtx [3]
static __stk_c_stack stk_word_t s_StackRed [512]
static __stk_c_stack stk_word_t s_StackGrn [512]
static __stk_c_stack stk_word_t s_StackOrg [512]
static __stk_c_stack stk_word_t s_StackLog [512]

Detailed Description

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

Definition in file low_power_c/example.c.

Macro Definition Documentation

◆ FLAG_GREEN

#define FLAG_GREEN   (1U << LED_GREEN)
Examples
low_power_c/example.c.

Definition at line 76 of file low_power_c/example.c.

◆ FLAG_ORANGE

#define FLAG_ORANGE   (1U << LED_ORANGE)
Examples
low_power_c/example.c.

Definition at line 77 of file low_power_c/example.c.

◆ FLAG_RED

#define FLAG_RED   (1U << LED_RED)
Examples
low_power_c/example.c.

Definition at line 75 of file low_power_c/example.c.

Referenced by RunExample().

◆ RTC_WKUP_COUNTS_PER_TICK

#define RTC_WKUP_COUNTS_PER_TICK   2U
Examples
low_power_c/example.c.

Definition at line 226 of file low_power_c/example.c.

Referenced by RtcWakeupArm().

◆ RTC_WKUP_GUARD_COUNTS

#define RTC_WKUP_GUARD_COUNTS   2U
Examples
low_power_c/example.c.

Definition at line 227 of file low_power_c/example.c.

Referenced by RtcWakeupArm().

◆ RTC_WKUP_MAX_COUNT

#define RTC_WKUP_MAX_COUNT   0xFFFFU
Examples
low_power_c/example.c.

Definition at line 228 of file low_power_c/example.c.

Referenced by RtcWakeupArm().

◆ TASK_STACK_SIZE

◆ TICKS_DEEP_SLEEP

#define TICKS_DEEP_SLEEP   40
Examples
low_power/example.cpp, and low_power_c/example.c.

Definition at line 318 of file low_power_c/example.c.

Referenced by LowPowerOverrider::OnSleep(), and OnSleep().

◆ TICKS_IDLE_SLEEP

#define TICKS_IDLE_SLEEP   8
Examples
low_power/example.cpp, and low_power_c/example.c.

Definition at line 317 of file low_power_c/example.c.

Referenced by LowPowerOverrider::OnSleep(), and OnSleep().

Function Documentation

◆ AdcInit()

void AdcInit ( void )
static
Examples
low_power_c/example.c.

Definition at line 123 of file low_power_c/example.c.

124{
125 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
126 __DSB();
127
128 ADC->CCR |= ADC_CCR_TSVREFE;
129 ADC1->CR1 = 0;
130 ADC1->CR2 = 0;
131 ADC1->SMPR1 = (7U << 18);
132 ADC1->SQR3 = 16U;
133 ADC1->SQR1 = 0;
134
135 ADC1->CR2 |= ADC_CR2_ADON;
136 for (volatile uint32_t i = 0; i < 10000; ++i) {}
137}

Referenced by RunExample().

Here is the caller graph for this function:

◆ AdcRead()

uint16_t AdcRead ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 153 of file low_power_c/example.c.

154{
155 AdcResume();
156 ADC1->SR &= ~ADC_SR_EOC;
157 ADC1->CR2 |= ADC_CR2_SWSTART;
158 while (!(ADC1->SR & ADC_SR_EOC)) {}
159 return (uint16_t)ADC1->DR;
160}
static void AdcResume(void)

References AdcResume().

Referenced by AdcTimerCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AdcResume()

void AdcResume ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 144 of file low_power_c/example.c.

145{
146 if ((ADC1->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
147 {
148 ADC1->CR2 |= ADC_CR2_ADON;
149 for (volatile uint32_t i = 0; i < 500; ++i) {}
150 }
151}

Referenced by AdcRead(), and OnSleep().

Here is the caller graph for this function:

◆ AdcSuspend()

void AdcSuspend ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 139 of file low_power_c/example.c.

140{
141 ADC1->CR2 &= ~ADC_CR2_ADON;
142}

Referenced by OnSleep().

Here is the caller graph for this function:

◆ AdcTimerCallback()

void AdcTimerCallback ( stk_timerhost_t * host,
stk_timer_t * timer,
void * user_data )
static
Examples
low_power_c/example.c.

Definition at line 381 of file low_power_c/example.c.

384{
385 (void)host;
386 (void)timer;
387 (void)user_data;
388
389 AdcSample sample;
390 sample.raw = AdcRead();
391 sample.timestamp = stk_ticks();
392
394}
stk_tick_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
Definition stk_c.cpp:789
bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data)
Attempt to write a single element to the pipe without blocking.
One ADC reading delivered through the pipe.
uint16_t raw
raw 12-bit ADC value
stk::Ticks timestamp
kernel tick at the time of conversion
static stk::sync::PipeT< AdcSample, 8 > g_AdcPipe
static uint16_t AdcRead(void)

References AdcRead(), g_AdcPipe, AdcSample::raw, stk_pipe_trywrite(), stk_ticks(), and AdcSample::timestamp.

Referenced by RunExample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ButtonInit()

void ButtonInit ( void )
static
Examples
low_power_c/example.c.

Definition at line 203 of file low_power_c/example.c.

204{
205 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
206 __DSB();
207
208 GPIOA->MODER &= ~(3U << 0);
209 GPIOA->PUPDR &= ~(3U << 0);
210
211 RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
212 __DSB();
213 SYSCFG->EXTICR[0] = (SYSCFG->EXTICR[0] & ~SYSCFG_EXTICR1_EXTI0)
214 | SYSCFG_EXTICR1_EXTI0_PA;
215
216 EXTI->RTSR |= (1U << 0);
217 EXTI->FTSR &= ~(1U << 0);
218 EXTI->IMR |= (1U << 0);
219
220 NVIC_SetPriority(EXTI0_IRQn, 8);
221 NVIC_EnableIRQ(EXTI0_IRQn);
222}

Referenced by RunExample().

Here is the caller graph for this function:

◆ CpuEnterDeepSleepMode()

void CpuEnterDeepSleepMode ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 185 of file low_power_c/example.c.

186{
187 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
188 __DSB();
189 __WFI();
190 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
192}
static void RestorePllClock(void)

References RestorePllClock().

Referenced by OnSleep().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CpuEnterSleepMode()

void CpuEnterSleepMode ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 194 of file low_power_c/example.c.

195{
196 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
197 __DSB();
198 __WFI();
199}

Referenced by OnSleep().

Here is the caller graph for this function:

◆ EXTI0_IRQHandler()

void EXTI0_IRQHandler ( void )

Definition at line 498 of file low_power_c/example.c.

499{
501 {
503
504 g_KernelSuspended = true;
506 }
507 else
508 {
510
511 g_KernelSuspended = false;
512 LedInit();
513 }
514
515 /* Debounce button press */
516 for (volatile int i = 0; i < 1000000; ++i);
517
518 /* Clear the EXTI pending bit first to acknowledge the interrupt. */
519 EXTI->PR = (1U << 0);
520}
@ LED_BLUE
Definition led.h:20
void stk_kernel_resume(stk_kernel_t *k, stk_timeout_t elapsed_ticks)
Resume scheduling after a prior stk_kernel_suspend() call.
Definition stk_c.cpp:603
stk_timeout_t stk_kernel_suspend(stk_kernel_t *k)
Suspend scheduling (tickless idle entry point).
Definition stk_c.cpp:596
static volatile bool g_KernelSuspended
static void LedSwitchOnExclusive(int pin)
static void LedInit(void)
static stk_kernel_t * g_Kernel

References g_Kernel, g_KernelSuspended, LED_BLUE, LedInit(), LedSwitchOnExclusive(), stk_kernel_resume(), and stk_kernel_suspend().

Here is the call graph for this function:

◆ LedInit()

void LedInit ( void )
static
Examples
low_power_c/example.c.

Definition at line 106 of file low_power_c/example.c.

107{
108 Led_InitAll(false);
109}
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27

References Led_InitAll().

Referenced by EXTI0_IRQHandler(), and RunExample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LedSet()

void LedSet ( int pin,
bool on )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 111 of file low_power_c/example.c.

112{
113 Led_Set(pin, on);
114}
void Led_Set(LedId led, bool state)
Definition c_wrapper.cpp:22

References Led_Set().

Referenced by LedTaskEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LedSwitchOnExclusive()

void LedSwitchOnExclusive ( int pin)
inlinestatic
Examples
low_power_c/example.c.

Definition at line 116 of file low_power_c/example.c.

117{
119}
void Led_SwitchOnExclusive(LedId led)
Definition c_wrapper.cpp:32

References Led_SwitchOnExclusive().

Referenced by EXTI0_IRQHandler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LedTaskEntry()

void LedTaskEntry ( void * arg)
static
Examples
low_power_c/example.c.

Definition at line 413 of file low_power_c/example.c.

414{
415 LedTaskCtx *ctx = (LedTaskCtx *)arg;
416
418
419 while (true)
420 {
421 uint32_t result = stk_ef_wait(g_TaskFlags,
422 ctx->my_flag,
425 if (stk_ef_is_error(result))
426 continue;
427
428 /* Atomically switch the active LED. */
430 LedSet(LED_RED, ctx->task_id == 0);
431 LedSet(LED_GREEN, ctx->task_id == 1);
432 LedSet(LED_ORANGE, ctx->task_id == 2);
434
435 /* Drift-free 1 s sleep. */
437
438 /* Hand off to the next LED task. */
440 }
441}
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_RED
Definition led.h:19
#define STK_EF_OPT_WAIT_ANY
Options bitmask constants for stk_ef_wait() / stk_ef_trywait().
Definition stk_c.h:1767
#define STK_WAIT_INFINITE
Infinite timeout constant.
Definition stk_c.h:249
stk_tick_t stk_ticks_from_ms(stk_time_t msec)
Get ticks from milliseconds using current kernel tick resolution.
Definition stk_c.cpp:792
static bool stk_ef_is_error(stk_ef_val_t result)
Returns true if a value returned by stk_ef_set(), stk_ef_clear(), stk_ef_wait(), or stk_ef_trywait() ...
Definition stk_c.h:1788
void stk_critical_section_enter()
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:834
bool stk_sleep_until(stk_tick_t ts)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:800
stk_ef_val_t stk_ef_set(stk_ef_t *ef, stk_ef_val_t flags)
Set one or more flags.
stk_ef_val_t stk_ef_wait(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_timeout_t timeout)
Wait for one or more flags to be set.
void stk_critical_section_exit()
Leave global critical section - re-enable context switches.
Definition stk_c.cpp:841
static stk::Ticks g_Timeline
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static void LedSet(int pin, bool on)

References g_TaskFlags, g_Timeline, LED_GREEN, LED_ORANGE, LED_RED, LedSet(), LedTaskCtx::my_flag, LedTaskCtx::next_flag, stk_critical_section_enter(), stk_critical_section_exit(), stk_ef_is_error(), STK_EF_OPT_WAIT_ANY, stk_ef_set(), stk_ef_wait(), stk_sleep_until(), stk_ticks(), stk_ticks_from_ms(), STK_WAIT_INFINITE, and LedTaskCtx::task_id.

Referenced by RunExample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LogTaskEntry()

void LogTaskEntry ( void * arg)
static
Examples
low_power_c/example.c.

Definition at line 450 of file low_power_c/example.c.

451{
452 (void)arg;
453
454 while (true)
455 {
456 AdcSample sample;
457 sample.timestamp = stk_ticks();
458
459 /* Block up to 10 s for a sample (matches the C++ 10000 ms timeout). */
460 bool ok = stk_pipe_read(g_AdcPipe, &sample, stk_ticks_from_ms(10000));
461
462 if (!ok)
463 {
464 printf("[log] t=%d s warning: no ADC sample for 10 s\n",
465 (int)(sample.timestamp / 1000));
466 }
467 else
468 {
469 float deg_c = TEMP_ValueToDeg(sample.raw);
470
471 /* Scale by 100 to preserve 2 decimal places. */
472 int temp_scaled = (int)(deg_c * 100.0f);
473 int temp_int = temp_scaled / 100;
474 int temp_frac = temp_scaled % 100;
475 if (temp_frac < 0)
476 {
477 temp_frac = -temp_frac; /* ensure fractional part is positive */
478 }
479
480 /* Log: tick timestamp, raw counts, and temperature. */
481 printf("[log] t=%d s raw=%-4d temp=%d.%02d C\n",
482 (int)(sample.timestamp / 1000),
483 (int)sample.raw,
484 temp_int,
485 temp_frac);
486 }
487 }
488}
bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe.
static float TEMP_ValueToDeg(uint32_t sens)

References g_AdcPipe, AdcSample::raw, stk_pipe_read(), stk_ticks(), stk_ticks_from_ms(), TEMP_ValueToDeg(), and AdcSample::timestamp.

Referenced by RunExample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnSleep()

bool OnSleep ( int32_t sleep_ticks,
void * user_data )
static
Examples
low_power_c/example.c.

Definition at line 320 of file low_power_c/example.c.

321{
322 (void)user_data;
323
324 // Note: For extended processing here we must increase the stack of
325 // the sleep task using STK_SLEEP_TRAP_STACK_SIZE in stk_config.h.
326 // Based on processing inside OnSleep you would need more or less
327 // stack. 256 looks optimal for this implementation.
328 // You can always inspect Kernel::m_sleep_trap->memory stack array
329 // and check how it is filled (not touched memory is filled with
330 // STK_STACK_MEMORY_FILLER.
331 assert(STK_SLEEP_TRAP_STACK_SIZE >= 256);
332
333 if ((sleep_ticks >= TICKS_DEEP_SLEEP) || g_KernelSuspended)
334 {
336 {
337 /* Arm RTC wakeup timer. */
338 RtcWakeupArm(sleep_ticks);
339
340 /* Suspend kernel ticking. */
342 }
343
344 AdcSuspend();
347 AdcResume();
348
350 {
351 stk_kernel_resume(g_Kernel, sleep_ticks);
352
353 /* Return false: let driver wait in idle sleep until context switch. */
354 return false;
355 }
356 }
357 else
358 if (sleep_ticks > TICKS_IDLE_SLEEP)
359 {
360 AdcSuspend();
362 AdcResume();
363 }
364 else
365 {
367 }
368
369 /* Return true: driver re-enters OnSleep() until an ISR breaks the sleep. */
370 return true;
371}
#define STK_SLEEP_TRAP_STACK_SIZE
Stack size for the sleep trap in elements of Word (default: STK_STACK_SIZE_MIN).
Definition stk_defs.h:659
#define TICKS_IDLE_SLEEP
static void AdcSuspend(void)
#define TICKS_DEEP_SLEEP
static void RtcWakeupArm(int32_t sleep_ticks)
static void CpuEnterSleepMode(void)
static void RtcWakeupDisarm(void)
static void CpuEnterDeepSleepMode(void)

References AdcResume(), AdcSuspend(), CpuEnterDeepSleepMode(), CpuEnterSleepMode(), g_Kernel, g_KernelSuspended, RtcWakeupArm(), RtcWakeupDisarm(), stk_kernel_resume(), stk_kernel_suspend(), STK_SLEEP_TRAP_STACK_SIZE, TICKS_DEEP_SLEEP, and TICKS_IDLE_SLEEP.

Referenced by RunExample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RestorePllClock()

void RestorePllClock ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 173 of file low_power_c/example.c.

174{
175 RCC->CR |= RCC_CR_HSEON;
176 while (!(RCC->CR & RCC_CR_HSERDY)) {}
177
178 RCC->CR |= RCC_CR_PLLON;
179 while (!(RCC->CR & RCC_CR_PLLRDY)) {}
180
181 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_PLL;
182 while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
183}

Referenced by CpuEnterDeepSleepMode().

Here is the caller graph for this function:

◆ RTC_WKUP_IRQHandler()

void RTC_WKUP_IRQHandler ( void )

Definition at line 301 of file low_power_c/example.c.

302{
303 RTC->WPR = 0xCA;
304 RTC->WPR = 0x53;
305 RTC->ISR &= ~RTC_ISR_WUTF;
306 RTC->WPR = 0xFF;
307 EXTI->PR = (1U << 22);
308}

◆ RtcWakeupArm()

void RtcWakeupArm ( int32_t sleep_ticks)
inlinestatic
Examples
low_power_c/example.c.

Definition at line 268 of file low_power_c/example.c.

269{
270 uint32_t counts = (uint32_t)sleep_ticks * RTC_WKUP_COUNTS_PER_TICK;
271 if (counts > RTC_WKUP_MAX_COUNT)
272 counts = RTC_WKUP_MAX_COUNT;
273 counts = (counts > RTC_WKUP_GUARD_COUNTS) ? (counts - RTC_WKUP_GUARD_COUNTS) : 1U;
274
275 RTC->WPR = 0xCA;
276 RTC->WPR = 0x53;
277 RTC->CR &= ~RTC_CR_WUTE;
278 while (!(RTC->ISR & RTC_ISR_WUTWF)) {}
279 RTC->ISR &= ~RTC_ISR_WUTF;
280 EXTI->PR = (1U << 22);
281 RTC->CR = (RTC->CR & ~RTC_CR_WUCKSEL) | 0x0U;
282 RTC->WUTR = (counts > 0U) ? (counts - 1U) : 0U;
283 RTC->CR |= RTC_CR_WUTIE | RTC_CR_WUTE;
284 RTC->WPR = 0xFF;
285}
#define RTC_WKUP_MAX_COUNT
#define RTC_WKUP_GUARD_COUNTS
#define RTC_WKUP_COUNTS_PER_TICK

References RTC_WKUP_COUNTS_PER_TICK, RTC_WKUP_GUARD_COUNTS, and RTC_WKUP_MAX_COUNT.

Referenced by OnSleep().

Here is the caller graph for this function:

◆ RtcWakeupDisarm()

void RtcWakeupDisarm ( void )
inlinestatic
Examples
low_power_c/example.c.

Definition at line 287 of file low_power_c/example.c.

288{
289 RTC->WPR = 0xCA;
290 RTC->WPR = 0x53;
291 RTC->CR &= ~(RTC_CR_WUTE | RTC_CR_WUTIE);
292 RTC->ISR &= ~RTC_ISR_WUTF;
293 RTC->WPR = 0xFF;
294 EXTI->PR = (1U << 22);
295}

Referenced by OnSleep().

Here is the caller graph for this function:

◆ RtcWakeupInit()

void RtcWakeupInit ( void )
static
Examples
low_power_c/example.c.

Definition at line 230 of file low_power_c/example.c.

231{
232 RCC->CSR |= RCC_CSR_LSION;
233 while (!(RCC->CSR & RCC_CSR_LSIRDY)) {}
234
235 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
236 __DSB();
237 PWR->CR |= PWR_CR_DBP;
238
239 if ((RCC->BDCR & RCC_BDCR_RTCSEL) != RCC_BDCR_RTCSEL_1)
240 {
241 RCC->BDCR |= RCC_BDCR_BDRST;
242 RCC->BDCR &= ~RCC_BDCR_BDRST;
243 RCC->BDCR |= RCC_BDCR_RTCSEL_1;
244 }
245
246 RCC->BDCR |= RCC_BDCR_RTCEN;
247 __DSB();
248
249 RTC->WPR = 0xCA;
250 RTC->WPR = 0x53;
251
252 RTC->ISR |= RTC_ISR_INIT;
253 while (!(RTC->ISR & RTC_ISR_INITF)) {}
254
255 RTC->PRER = (127U << RTC_PRER_PREDIV_A_Pos) | (249U << RTC_PRER_PREDIV_S_Pos);
256 RTC->ISR &= ~RTC_ISR_INIT;
257
258 RTC->WPR = 0xFF;
259
260 EXTI->IMR |= (1U << 22);
261 EXTI->RTSR |= (1U << 22);
262 EXTI->FTSR &= ~(1U << 22);
263
264 NVIC_SetPriority(RTC_WKUP_IRQn, 12);
265 NVIC_EnableIRQ(RTC_WKUP_IRQn);
266}

Referenced by RunExample().

Here is the caller graph for this function:

◆ RunExample()

void RunExample ( void )

Definition at line 537 of file low_power_c/example.c.

538{
539 /* --- Board initialization ----------------------------------------------- */
540 LedInit();
541 AdcInit();
542 ButtonInit();
544
545 /* --- Kernel ------------------------------------------------------------- */
546 /* STK_C_KERNEL_TYPE_CPU_0 must be configured in stk_config.h with:
547 * KERNEL_STATIC | KERNEL_SYNC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0)
548 * task count = 4 + TimerHost::TASK_COUNT
549 * strategy = sched::SwitchStrategyRR
550 * platform = PlatformDefault */
553
554 /* --- Sync objects ------------------------------------------------------- */
555 /* EventFlags: start with RED task's flag set so it runs first. */
557
558 /* Pipe for AdcSample structs, capacity 8. */
560 s_AdcPipeBuf, sizeof(s_AdcPipeBuf),
561 8, sizeof(AdcSample));
562
563 /* --- LED task contexts -------------------------------------------------- */
564 for (int i = 0; i < 3; ++i)
565 {
566 s_LedCtx[i].task_id = (uint8_t)i;
567 s_LedCtx[i].my_flag = FLAGS_ALL[i];
568 s_LedCtx[i].next_flag = FLAGS_ALL[(i + 1) % 3];
569 }
570
571 /* --- Tasks -------------------------------------------------------------- */
574 stk_task_set_name(task_red, "LED-red");
575
578 stk_task_set_name(task_grn, "LED-grn");
579
582 stk_task_set_name(task_org, "LED-org");
583
586 stk_task_set_name(task_log, "log");
587
588 /* --- TimerHost ---------------------------------------------------------- */
589 stk_timerhost_t *timer_host = stk_timerhost_get(0);
590 stk_timerhost_init(timer_host, g_Kernel, /*privileged=*/true);
591
593 uint32_t adc_period = (uint32_t)stk_ticks_from_ms(2000);
594 stk_timer_start(timer_host, adc_timer, adc_period, adc_period);
595
596 /* --- Low-power event overrider ------------------------------------------ */
597 /* Replaces kernel.GetPlatform()->SetEventOverrider(&lp_overrider).
598 * Must be called after stk_kernel_init() and before stk_kernel_start(). */
599 static stk_event_overrider_t lp_overrider = {
600 .on_sleep = OnSleep,
601 .on_hard_fault = NULL,
602 .user_data = NULL
603 };
605
606 /* --- Add tasks and start ------------------------------------------------ */
607 stk_kernel_add_task(g_Kernel, task_red);
608 stk_kernel_add_task(g_Kernel, task_grn);
609 stk_kernel_add_task(g_Kernel, task_org);
610 stk_kernel_add_task(g_Kernel, task_log);
611
612 /* Never returns in KERNEL_STATIC mode. */
614}
void stk_task_set_name(stk_task_t *tsk, const char *tname)
Assign human-readable task name (for tracing/debugging).
Definition stk_c.cpp:690
stk_pipe_t * stk_pipe_create(stk_pipe_mem_t *const membuf, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t element_size)
Create a Pipe (using provided memory).
stk_ef_t * stk_ef_create(stk_ef_mem_t *const membuf, stk_ef_val_t initial_flags)
Create an EventFlags object (using provided memory).
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
Definition stk_c.cpp:478
void stk_kernel_set_event_overrider(stk_kernel_t *k, stk_event_overrider_t *overrider)
Install a platform event overrider on the kernel.
Definition stk_c.cpp:624
stk_add_result_t stk_kernel_add_task(stk_kernel_t *k, stk_task_t *tsk)
Add task to non-HRT kernel (static or dynamic).
Definition stk_c.cpp:506
#define STK_PERIODICITY_DEFAULT
Default tick period (1 ms).
Definition stk_c.h:196
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:471
stk_task_t * stk_task_create_privileged(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size)
Create privileged-mode (kernel-mode) task.
Definition stk_c.cpp:650
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:403
stk_timer_result_t stk_timer_start(stk_timerhost_t *host, stk_timer_t *tmr, uint32_t delay, uint32_t period)
Start a timer.
void stk_timerhost_init(stk_timerhost_t *host, stk_kernel_t *kernel, bool privileged)
Initialize the TimerHost and register its internal tasks with the kernel.
stk_timer_t * stk_timer_create(stk_timer_mem_t *const membuf, stk_timer_callback_t callback, void *user_data)
Construct a timer in the supplied memory buffer.
stk_timerhost_t * stk_timerhost_get(uint8_t core_nr)
Obtain the pre-allocated TimerHost for the given CPU core.
C-level callback table that mirrors stk::IPlatform::IEventOverrider.
Definition stk_c.h:701
static const uint32_t FLAGS_ALL[]
static stk_ef_mem_t s_TaskFlagsMem
static void RtcWakeupInit(void)
static LedTaskCtx s_LedCtx[3]
static stk_pipe_mem_t s_AdcPipeMem
static __stk_c_stack stk_word_t s_StackGrn[512]
static stk_timer_mem_t s_AdcTimerMem
static __stk_c_stack stk_word_t s_StackOrg[512]
#define TASK_STACK_SIZE
static void ButtonInit(void)
static void AdcInit(void)
static __stk_c_stack stk_word_t s_StackLog[512]
static uint8_t s_AdcPipeBuf[8 *sizeof(AdcSample)]
static __stk_c_stack stk_word_t s_StackRed[512]
static void LogTaskEntry(void *arg)
#define FLAG_RED
static void AdcTimerCallback(stk_timerhost_t *host, stk_timer_t *timer, void *user_data)
static bool OnSleep(int32_t sleep_ticks, void *user_data)
static void LedTaskEntry(void *arg)

References AdcInit(), AdcTimerCallback(), ButtonInit(), FLAG_RED, FLAGS_ALL, g_AdcPipe, g_Kernel, g_TaskFlags, LedInit(), LedTaskEntry(), LogTaskEntry(), OnSleep(), RtcWakeupInit(), s_AdcPipeBuf, s_AdcPipeMem, s_AdcTimerMem, s_LedCtx, s_StackGrn, s_StackLog, s_StackOrg, s_StackRed, s_TaskFlagsMem, stk_ef_create(), stk_kernel_add_task(), stk_kernel_create(), stk_kernel_init(), stk_kernel_set_event_overrider(), stk_kernel_start(), STK_PERIODICITY_DEFAULT, stk_pipe_create(), stk_task_create_privileged(), stk_task_set_name(), stk_ticks_from_ms(), stk_timer_create(), stk_timer_start(), stk_timerhost_get(), stk_timerhost_init(), and TASK_STACK_SIZE.

Here is the call graph for this function:

◆ TEMP_ValueToDeg()

float TEMP_ValueToDeg ( uint32_t sens)
inlinestatic
Examples
low_power_c/example.c.

Definition at line 162 of file low_power_c/example.c.

163{
164 static const float to_V = 3.3f / 4095.0f;
165 static const float slope = 2.5f / 1000.0f;
166 static const float v_cal = 1060.0f * (3.3f / 4095.0f);
167 static const float t_cal = 26.0f;
168 return (sens * to_V - v_cal) / slope + t_cal;
169}

Referenced by LogTaskEntry().

Here is the caller graph for this function:

Variable Documentation

◆ FLAGS_ALL

const uint32_t FLAGS_ALL[3] = { (1U << LED_RED) , (1U << LED_GREEN) , (1U << LED_ORANGE) }
static

Definition at line 79 of file low_power_c/example.c.

#define FLAG_ORANGE
#define FLAG_GREEN

◆ g_AdcPipe

stk_pipe_t* g_AdcPipe
static

Definition at line 91 of file low_power_c/example.c.

◆ g_Kernel

◆ g_KernelSuspended

volatile bool g_KernelSuspended = false
static

Definition at line 95 of file low_power_c/example.c.

◆ g_TaskFlags

stk_ef_t* g_TaskFlags
static

Definition at line 83 of file low_power_c/example.c.

◆ g_Timeline

stk_tick_t g_Timeline = 0
static

Definition at line 86 of file low_power_c/example.c.

◆ s_AdcPipeBuf

uint8_t s_AdcPipeBuf[8 *sizeof(AdcSample)]
static
Examples
low_power_c/example.c.

Definition at line 90 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_AdcPipeMem

stk_pipe_mem_t s_AdcPipeMem
static
Examples
low_power_c/example.c.

Definition at line 89 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_AdcTimerMem

stk_timer_mem_t s_AdcTimerMem
static
Examples
low_power_c/example.c.

Definition at line 92 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_LedCtx

LedTaskCtx s_LedCtx[3]
static
Examples
low_power_c/example.c.

Definition at line 411 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_StackGrn

__stk_c_stack stk_word_t s_StackGrn[512]
static
Examples
low_power_c/example.c.

Definition at line 529 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_StackLog

__stk_c_stack stk_word_t s_StackLog[512]
static
Examples
low_power_c/example.c.

Definition at line 531 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_StackOrg

__stk_c_stack stk_word_t s_StackOrg[512]
static
Examples
low_power_c/example.c.

Definition at line 530 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_StackRed

__stk_c_stack stk_word_t s_StackRed[512]
static
Examples
low_power_c/example.c.

Definition at line 528 of file low_power_c/example.c.

Referenced by RunExample().

◆ s_TaskFlagsMem

stk_ef_mem_t s_TaskFlagsMem
static
Examples
low_power_c/example.c.

Definition at line 82 of file low_power_c/example.c.

Referenced by RunExample().