semf
stm32analogininjected.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_ADC_MODULE_ENABLED)
14#if defined(STM32F1) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7)
15
16namespace semf
17{
18Stm32AnalogInInjected::Stm32AnalogInInjected(ADC_HandleTypeDef& hwHandle, uint32_t injectedRank)
19: m_hwHandle(&hwHandle),
20 m_injectedRank(injectedRank)
21{
22 queue()->push(*this);
23}
24
25void Stm32AnalogInInjected::start()
26{
27 SEMF_INFO("start");
28
29 HAL_StatusTypeDef state = HAL_ADCEx_InjectedStart_IT(m_hwHandle);
30 if (state != HAL_OK)
31 {
32 if (state == HAL_ERROR)
33 {
34 SEMF_ERROR("start hal error");
35 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalError)));
36 }
37 else if (state == HAL_BUSY)
38 {
39 SEMF_ERROR("start hal busy");
40 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalBusy)));
41 }
42 else if (state == HAL_TIMEOUT)
43 {
44 SEMF_ERROR("start hal timeout");
45 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalTimeout)));
46 }
47 return;
48 }
49}
50
51uint32_t Stm32AnalogInInjected::value()
52{
53 uint32_t val = HAL_ADCEx_InjectedGetValue(m_hwHandle, m_injectedRank);
54 SEMF_INFO("value %u", val);
55 return val;
56}
57
58void Stm32AnalogInInjected::stop()
59{
60 SEMF_INFO("stop");
61 HAL_StatusTypeDef state = HAL_ADC_Stop_IT(m_hwHandle);
62 if (state != HAL_OK)
63 {
64 if (state == HAL_ERROR)
65 {
66 SEMF_ERROR("stop hal error");
67 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalError)));
68 }
69 else if (state == HAL_BUSY)
70 {
71 SEMF_ERROR("stop hal busy");
72 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalBusy)));
73 }
74 else if (state == HAL_TIMEOUT)
75 {
76 SEMF_ERROR("stop hal timeout");
77 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalTimeout)));
78 }
79 return;
80 }
81}
82
83LinkedQueue<Stm32AnalogInInjected>* Stm32AnalogInInjected::queue()
84{
85 static LinkedQueue<Stm32AnalogInInjected> queue;
86 return &queue;
87}
88
89void Stm32AnalogInInjected::systemIsr(ADC_HandleTypeDef& adc)
90{
91 for (Stm32AnalogInInjected& i : *(queue()))
92 i.isr(adc);
93}
94
95void Stm32AnalogInInjected::systemIsrError(ADC_HandleTypeDef& adc)
96{
97 for (Stm32AnalogInInjected& i : *(queue()))
98 i.isrError(adc);
99}
100
101void Stm32AnalogInInjected::isr(ADC_HandleTypeDef& adc)
102{
103 if (&adc == m_hwHandle)
104 {
105 SEMF_INFO("dataAvailable");
106 dataAvailable();
107 }
108}
109
110void Stm32AnalogInInjected::isrError(ADC_HandleTypeDef& adc)
111{
112 if (&adc == m_hwHandle)
113 {
114 SEMF_INFO("error");
115 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::IsrError_Isr)));
116 }
117}
118} /* namespace semf */
119#endif
120#endif
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41