124 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
128 GPIOA->MODER &= ~(3U << 0);
129 GPIOA->PUPDR &= ~(3U << 0);
132 RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
134 SYSCFG->EXTICR[0] = (SYSCFG->EXTICR[0] & ~SYSCFG_EXTICR1_EXTI0) | SYSCFG_EXTICR1_EXTI0_PA;
137 EXTI->RTSR |= (1U << 0);
138 EXTI->FTSR &= ~(1U << 0);
139 EXTI->IMR |= (1U << 0);
143 NVIC_SetPriority(EXTI0_IRQn, 8);
144 NVIC_EnableIRQ(EXTI0_IRQn);
154 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
158 ADC->CCR |= ADC_CCR_TSVREFE;
163 ADC1->SMPR1 = (7U << 18);
168 ADC1->CR2 |= ADC_CR2_ADON;
169 for (
volatile uint32_t i = 0; i < 10000; ++i) {}
175 ADC1->CR2 &= ~ADC_CR2_ADON;
181 if ((ADC1->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
183 ADC1->CR2 |= ADC_CR2_ADON;
185 for (
volatile uint32_t i = 0; i < 500; ++i) {}
194 ADC1->SR &= ~ADC_SR_EOC;
195 ADC1->CR2 |= ADC_CR2_SWSTART;
196 while (!(ADC1->SR & ADC_SR_EOC)) {}
197 return static_cast<uint16_t
>(ADC1->DR);
202 static const float to_V = 3.3f / 4095;
203 static const float slope = 2.5f / 1000;
204 static const float v_cal = 1060.0f * (3.3f / 4095);
205 static const float t_cal = 26;
207 float temp = (sens * to_V - v_cal) / slope + t_cal;
243 RCC->CR |= RCC_CR_HSEON;
248 while (!(RCC->CR & RCC_CR_HSERDY)) {}
254 RCC->CR |= RCC_CR_PLLON;
255 while (!(RCC->CR & RCC_CR_PLLRDY)) {}
261 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_PLL;
265 while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
274 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
279 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
288 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
325 RCC->CSR |= RCC_CSR_LSION;
326 while (!(RCC->CSR & RCC_CSR_LSIRDY)) {}
331 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
333 PWR->CR |= PWR_CR_DBP;
335 if ((RCC->BDCR & RCC_BDCR_RTCSEL) != RCC_BDCR_RTCSEL_1)
338 RCC->BDCR |= RCC_BDCR_BDRST;
339 RCC->BDCR &= ~RCC_BDCR_BDRST;
340 RCC->BDCR |= RCC_BDCR_RTCSEL_1;
344 RCC->BDCR |= RCC_BDCR_RTCEN;
352 RTC->ISR |= RTC_ISR_INIT;
353 while (!(RTC->ISR & RTC_ISR_INITF)) {}
358 RTC->PRER = (127U << RTC_PRER_PREDIV_A_Pos) | (249U << RTC_PRER_PREDIV_S_Pos);
361 RTC->ISR &= ~RTC_ISR_INIT;
368 EXTI->IMR |= (1U << 22);
369 EXTI->RTSR |= (1U << 22);
370 EXTI->FTSR &= ~(1U << 22);
372 NVIC_SetPriority(RTC_WKUP_IRQn, 12);
373 NVIC_EnableIRQ(RTC_WKUP_IRQn);
395 RTC->CR &= ~RTC_CR_WUTE;
396 while (!(RTC->ISR & RTC_ISR_WUTWF)) {}
399 RTC->ISR &= ~RTC_ISR_WUTF;
400 EXTI->PR = (1U << 22);
403 RTC->CR = (RTC->CR & ~RTC_CR_WUCKSEL) | 0x0U;
407 RTC->WUTR = (counts > 0U) ? (counts - 1U) : 0U;
410 RTC->CR |= RTC_CR_WUTIE | RTC_CR_WUTE;
424 RTC->CR &= ~(RTC_CR_WUTE | RTC_CR_WUTIE);
425 RTC->ISR &= ~RTC_ISR_WUTF;
431 EXTI->PR = (1U << 22);
451 RTC->ISR &= ~RTC_ISR_WUTF;
455 EXTI->PR = (1U << 22);
549 kernel->
Resume(sleep_ticks);
599template <stk::EAccessMode _AccessMode>
706template <stk::EAccessMode _AccessMode>
725 printf(
"[log] t=%d s warning: no ADC sample for 10 s\n",
726 static_cast<int>(sample.
timestamp / 1000));
734 int temp_scaled = (int)(deg_c * 100.0f);
735 int temp_int = temp_scaled / 100;
736 int temp_frac = temp_scaled % 100;
739 temp_frac = -temp_frac;
743 printf(
"[log] t=%d s raw=%-4d temp=%d.%02d C\n",
795 for (
volatile int i = 0; i < 1000000; ++i);
798 EXTI->PR = (1U << 0);
848 timer_host.
Start(adc_timer, adc_period, adc_period);
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define STK_SLEEP_TRAP_STACK_SIZE
Stack size for the sleep trap in elements of Word (default: STK_STACK_SIZE_MIN).
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Collection of synchronization primitives (stk::sync namespace).
Collection of time-related primitives (stk::time namespace).
Namespace of STK package.
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
static bool SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
@ KERNEL_SYNC
Synchronization support (see Event).
@ KERNEL_STATIC
All tasks are static and can not exit.
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
const uint32_t RTC_WKUP_COUNTS_PER_TICK
static __stk_forceinline void LedSwitchOnExclusive(bsp::Led::Id pin)
static __stk_forceinline void AdcResume()
static __stk_forceinline void RestorePllClock()
static __stk_forceinline void CpuEnterSleepMode()
static __stk_forceinline void RtcWakeupArm(stk::Timeout sleep_ticks)
static __stk_forceinline float TEMP_ValueToDeg(uint32_t sens)
const uint32_t RTC_WKUP_GUARD_COUNTS
static __stk_forceinline uint16_t AdcRead()
const uint32_t RTC_WKUP_MAX_COUNT
static void RtcWakeupInit()
static __stk_forceinline void CpuEnterDeepSleepMode()
static __stk_forceinline void RtcWakeupDisarm()
static __stk_forceinline void AdcSuspend()
static __stk_forceinline void LedSet(bsp::Led::Id pin, bool on)
Concrete implementation of IKernel.
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
IPlatform * GetPlatform() override
Get platform driver instance owned by this kernel.
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
RAII instance that enters the critical section on construction and exits it on destruction.
virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure=false)=0
Set platform event overrider.
Interface for a platform event overrider.
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.
Partial implementation of the user task.
static constexpr uint32_t OPT_WAIT_ANY
static bool IsError(uint32_t result)
Thread-safe, type-safe FIFO communication pipe with internal storage.
Abstract base class for a timer managed by TimerHost.
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.
LedTask(uint8_t task_id, const char *name)
const char * GetTraceName() const override
Get task trace name set by application.
void Run() override
Entry point of the user task.
static void SwitchOnLED(uint8_t id)
static constexpr Id GREEN
static constexpr Id ORANGE
static void InitAll(bool init_state)
static void SwitchOnExclusive(Id led)
static void Set(Id led, bool state)
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
bool OnSleep(stk::Timeout sleep_ticks) override
Called by the kernel when it enters the idle/sleep state.
void OnExpired(stk::time::TimerHost *) override
Callback invoked when this timer expires.
void Run() override
Entry point of the user task.
const char * GetTraceName() const override
Get task trace name set by application.
static stk::Ticks g_Timeline
static const uint32_t FLAGS_ALL[]
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static stk::sync::PipeT< AdcSample, 8 > g_AdcPipe
static volatile bool g_KernelSuspended
void RTC_WKUP_IRQHandler()
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static void SwitchOnLED()