semf
stm32inputcapture.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_TIM_MODULE_ENABLED)
14extern "C" void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
15{
17}
18namespace semf
19{
20LinkedQueue<Stm32InputCapture> Stm32InputCapture::m_queue;
21Stm32InputCapture::Stm32InputCapture(TIM_HandleTypeDef& hwHandle, uint32_t channel, unsigned int ticksPerSecond)
22: m_hwHandle(&hwHandle),
23 m_channel(channel),
24 m_ticksPerSecond{ticksPerSecond}
25{
26 m_queue.push(*this);
27}
28
30{
31 SEMF_INFO("start");
32 auto state = HAL_TIM_IC_Start_IT(m_hwHandle, m_channel);
33 if (state != HAL_OK)
34 {
35 if (state == HAL_ERROR)
36 {
37 SEMF_ERROR("hal error");
38 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_Error)));
39 }
40 else if (state == HAL_BUSY)
41 {
42 SEMF_ERROR("hal busy");
43 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_Busy)));
44 }
45 else if (state == HAL_TIMEOUT)
46 {
47 SEMF_ERROR("hal timeout");
48 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_Timeout)));
49 }
50 return;
51 }
52}
53
55{
56 SEMF_INFO("stop");
57 auto state = HAL_TIM_IC_Stop_IT(m_hwHandle, m_channel);
58 if (state != HAL_OK)
59 {
60 if (state == HAL_ERROR)
61 {
62 SEMF_ERROR("hal error");
63 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_Error)));
64 }
65 else if (state == HAL_BUSY)
66 {
67 SEMF_ERROR("hal busy");
68 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_Busy)));
69 }
70 else if (state == HAL_TIMEOUT)
71 {
72 SEMF_ERROR("hal timeout");
73 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_Timeout)));
74 }
75 return;
76 }
77}
78
80{
81 uint32_t val = HAL_TIM_ReadCapturedValue(m_hwHandle, m_channel);
82 SEMF_INFO("value %u on channel %u", val, m_channel);
83 return val;
84}
85
87{
88 if (m_hwHandle->Lock == HAL_LOCKED)
89 {
90 SEMF_ERROR("handle is locked");
91 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetTrigger_Busy)));
92 return;
93 }
94 m_hwHandle->Lock = HAL_LOCKED;
95
96 // get the capture compare enable register
97 uint32_t ccer = m_hwHandle->Instance->CCER;
98
99 // disable the capture compare mode
100 m_hwHandle->Instance->CCER &= ~(1 << (m_channel / 2));
101
102 switch (trigger)
103 {
104 case Trigger::RisingEdge:
105 ccer &= ~(1 << (m_channel + 1));
106 ccer &= ~(1 << (m_channel + 3));
107 break;
108 case Trigger::FallingEdge:
109 ccer |= 1 << (m_channel + 1);
110 ccer &= ~(1 << (m_channel + 3));
111 break;
112 case Trigger::RisingAndFallingEdge:
113 ccer |= 1 << (m_channel + 1);
114 ccer |= 1 << (m_channel + 3);
115 break;
116 default:
117 SEMF_ERROR("invalid value for trigger: %u", static_cast<uint32_t>(trigger));
118 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetTrigger_TriggerInvalid)));
119 return;
120 }
121
122 m_hwHandle->Instance->CCER = ccer;
123 __HAL_UNLOCK(m_hwHandle);
124}
125
126void Stm32InputCapture::systemIsr(TIM_HandleTypeDef& hwHandle)
127{
128 for (auto& i : m_queue)
129 i.isr(hwHandle);
130}
131
132void Stm32InputCapture::isr(TIM_HandleTypeDef& hwHandle)
133{
134 if (m_hwHandle == &hwHandle && hwHandle.Channel == m_channel)
135 {
136 SEMF_INFO("dataAvailable");
138 }
139}
140
142{
143 return m_ticksPerSecond;
144}
145
146void Stm32InputCapture::setMaxTicks(unsigned int maxTicks)
147{
148 SEMF_INFO("set max ticks %u", maxTicks);
149 m_hwHandle->Instance->ARR = maxTicks;
150}
151
152unsigned int Stm32InputCapture::maxTicks() const
153{
154 return m_hwHandle->Instance->ARR;
155}
156} /* namespace semf */
157#endif
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
Signal< Error > error
Definition: inputcapture.h:69
unsigned int ticks() override
Get the latest number of ticks (at the last change of the input).
void setMaxTicks(unsigned int maxTicks) override
Set the maximum number of ticks at which the tick counter will overflow.
static void systemIsr(TIM_HandleTypeDef &hwHandle)
System-wide interrupt service routine for input capture.
void setTrigger(Trigger trigger) override
unsigned int maxTicks() const override
Get the maximum number of ticks from the timer.
Stm32InputCapture(TIM_HandleTypeDef &hwHandle, uint32_t channel, unsigned int ticksPerSecond)
Constructor.
unsigned int ticksPerSecond() const override
Returns the the number of ticks per second (Hardware timer frequency).
void isr(TIM_HandleTypeDef &hwHandle)
Interrupt service routine for input capture.
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)