semf
stm32analogin.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_ADC_MODULE_ENABLED)
14namespace semf
15{
16Stm32AnalogIn::Stm32AnalogIn(ADC_HandleTypeDef& hwHandle)
17: m_hwHandle(&hwHandle)
18{
19 queue()->push(*this);
20}
21
23{
24 SEMF_INFO("start");
25 HAL_StatusTypeDef state = HAL_ADC_Start_IT(m_hwHandle);
26 if (state != HAL_OK)
27 {
28 if (state == HAL_ERROR)
29 {
30 SEMF_ERROR("start hal error");
31 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalError)));
32 }
33 else if (state == HAL_BUSY)
34 {
35 SEMF_ERROR("start hal busy");
36 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalBusy)));
37 }
38 else if (state == HAL_TIMEOUT)
39 {
40 SEMF_ERROR("start hal timeout");
41 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalTimeout)));
42 }
43 return;
44 }
45}
46
48{
49 uint32_t val = HAL_ADC_GetValue(m_hwHandle);
50 SEMF_INFO("value %u", val);
51 return val;
52}
53
55{
56 SEMF_INFO("stop");
57 HAL_StatusTypeDef state = HAL_ADC_Stop_IT(m_hwHandle);
58 if (state != HAL_OK)
59 {
60 if (state == HAL_ERROR)
61 {
62 SEMF_ERROR("stop hal error");
63 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalError)));
64 }
65 else if (state == HAL_BUSY)
66 {
67 SEMF_ERROR("stop hal busy");
68 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalBusy)));
69 }
70 else if (state == HAL_TIMEOUT)
71 {
72 SEMF_ERROR("stop hal timeout");
73 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalTimeout)));
74 }
75 return;
76 }
77}
78
80{
82 return &queue;
83}
84
85void Stm32AnalogIn::systemIsr(ADC_HandleTypeDef& adc)
86{
87 for (Stm32AnalogIn& i : *(queue()))
88 i.isr(adc);
89}
90
91void Stm32AnalogIn::systemIsrError(ADC_HandleTypeDef& adc)
92{
93 for (Stm32AnalogIn& i : *(queue()))
94 i.isrError(adc);
95}
96
97void Stm32AnalogIn::isr(ADC_HandleTypeDef& adc)
98{
99 if (&adc == m_hwHandle)
100 {
101 SEMF_INFO("dataAvailable");
103 }
104}
105
106void Stm32AnalogIn::isrError(ADC_HandleTypeDef& adc)
107{
108 if (&adc == m_hwHandle)
109 {
110 SEMF_ERROR("error");
111 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::IsrError_Isr)));
112 }
113}
114} /* namespace semf */
115#endif
Signal< Error > error
Definition: analogin.h:48
Signal dataAvailable
Definition: analogin.h:46
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
Stm32 driver for using a anologIn channel.
Definition: stm32analogin.h:26
static void systemIsrError(ADC_HandleTypeDef &adc)
System-wide interrupt service routine for adc error.
void stop() override
static LinkedQueue< Stm32AnalogIn > * queue()
Get the list with all analog ins.
Stm32AnalogIn(ADC_HandleTypeDef &hwHandle)
Constructor.
void isrError(ADC_HandleTypeDef &adc)
Sending a error signal.
void isr(ADC_HandleTypeDef &adc)
Interrupt service routine for adc conversion finished.
virtual void start()
Starts the hardware module (ADC) for reading ADC values. After finishing analog to digital conversion...
virtual uint32_t value()
Returns the last read value.
static void systemIsr(ADC_HandleTypeDef &adc)
System-wide interrupt service routine for adc conversion finished.
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41