semf
stm32timer.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_TIM_MODULE_ENABLED)
14#ifndef SEMF_TIMER_USE_CUSTOM_CALLBACK
15extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
16{
18}
19#endif
20
21namespace semf
22{
23LinkedQueue<Stm32Timer> Stm32Timer::m_queue;
24Stm32Timer::Stm32Timer(TIM_HandleTypeDef& hwHandle)
25: m_hwHandle(&hwHandle)
26{
27 queue()->push(*this);
28}
29
31{
32 auto errorCode = HAL_TIM_Base_Start_IT(m_hwHandle);
33 if (errorCode != HAL_OK)
34 {
35 if (errorCode == HAL_ERROR)
36 {
37 SEMF_ERROR("hal error");
38 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Start_HalError)));
39 }
40 else if (errorCode == HAL_BUSY)
41 {
42 SEMF_ERROR("hal busy");
43 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Start_HalBusy)));
44 }
45 else if (errorCode == HAL_TIMEOUT)
46 {
47 SEMF_ERROR("hal timeout");
48 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Start_HalTimeout)));
49 }
50 return;
51 }
52}
53
55{
56 auto errorCode = HAL_TIM_Base_Stop_IT(m_hwHandle);
57 if (errorCode != HAL_OK)
58 {
59 if (errorCode == HAL_ERROR)
60 {
61 SEMF_ERROR("hal error");
62 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Stop_HalError)));
63 }
64 else if (errorCode == HAL_BUSY)
65 {
66 SEMF_ERROR("hal busy");
67 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Stop_HalBusy)));
68 }
69 else if (errorCode == HAL_TIMEOUT)
70 {
71 SEMF_ERROR("hal timeout");
72 error(Error(kSemfClassId, static_cast<uint32_t>(ErrorCode::Stop_HalTimeout)));
73 }
74 return;
75 }
76}
77
79{
80 m_hwHandle->Instance->CNT = m_hwHandle->Instance->ARR;
81}
82
84{
86 return &queue;
87}
88
89void Stm32Timer::systemIsr(TIM_HandleTypeDef& hwHandle)
90{
91 for (Stm32Timer& i : *(queue()))
92 i.isr(hwHandle);
93}
94
95void Stm32Timer::isr(TIM_HandleTypeDef& hwHandle)
96{
97 if (&hwHandle == m_hwHandle)
98 {
99 SEMF_INFO("timeout");
100 timeout();
101 }
102}
103} /* namespace semf */
104#endif
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
LinkedQueue is an managed single linked queue implementation.
Definition: linkedqueue.h:38
Timer implementation for STM32 as peripheral timer.
Definition: stm32timer.h:27
Stm32Timer(TIM_HandleTypeDef &hwHandle)
Constructor.
Definition: stm32timer.cpp:24
void isr(TIM_HandleTypeDef &hwHandle)
Interrupt service routine for adc conversion finished.
Definition: stm32timer.cpp:95
static void systemIsr(TIM_HandleTypeDef &hwHandle)
System-wide interrupt service routine for adc conversion finished.
Definition: stm32timer.cpp:89
void start() override
Definition: stm32timer.cpp:30
void reset() override
Definition: stm32timer.cpp:78
static LinkedQueue< Stm32Timer > * queue()
Get the list with all timers.
Definition: stm32timer.cpp:83
void stop() override
Definition: stm32timer.cpp:54
Signal< Error > error
Definition: timer.h:38
Signal timeout
Definition: timer.h:36
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Definition: stm32timer.cpp:15