semf
stm32analogindma.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_ADC_MODULE_ENABLED)
14namespace semf
15{
16LinkedQueue<Stm32AnalogInDma> Stm32AnalogInDma::m_queue;
17Stm32AnalogInDma::Stm32AnalogInDma(ADC_HandleTypeDef& hwHandle)
18: m_hwHandle(&hwHandle)
19{
20 queue()->push(*this);
21}
22
23void Stm32AnalogInDma::start(uint8_t buffer[], size_t bufferSize)
24{
25 SEMF_INFO("start data: %p, size: %u", buffer, bufferSize);
26
27#if !defined(STM32F4) && !defined STM32F7
28 if (m_hwHandle->DMA_Handle && m_hwHandle->DMA_Handle->Init.MemDataAlignment)
29 bufferSize /= (m_hwHandle->DMA_Handle->Init.MemDataAlignment >> (DMA_CCR_MSIZE_Pos - 1));
30#endif
31
32 HAL_StatusTypeDef state = HAL_ADC_Start_DMA(m_hwHandle, reinterpret_cast<uint32_t*>(buffer), static_cast<uint32_t>(bufferSize));
33 if (state != HAL_OK)
34 {
35 if (state == HAL_ERROR)
36 {
37 SEMF_ERROR("start hal error");
38 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalError)));
39 }
40 else if (state == HAL_BUSY)
41 {
42 SEMF_ERROR("start hal busy");
43 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalBusy)));
44 }
45 else if (state == HAL_TIMEOUT)
46 {
47 SEMF_ERROR("start hal timeout");
48 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalTimeout)));
49 }
50 return;
51 }
52}
53
55{
56 SEMF_INFO("stop");
57 HAL_StatusTypeDef state = HAL_ADC_Stop_DMA(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 Stm32AnalogInDma::systemIsr(ADC_HandleTypeDef& adc)
86{
87 for (Stm32AnalogInDma& i : *(queue()))
88 i.isr(adc);
89}
90
91void Stm32AnalogInDma::systemIsrError(ADC_HandleTypeDef& adc)
92{
93 for (Stm32AnalogInDma& i : *(queue()))
94 i.isrError(adc);
95}
96
97void Stm32AnalogInDma::isr(ADC_HandleTypeDef& adc)
98{
99 if (&adc == m_hwHandle)
100 {
101 SEMF_INFO("dataAvailable");
103 }
104}
105
106void Stm32AnalogInDma::isrError(ADC_HandleTypeDef& adc)
107{
108 if (&adc == m_hwHandle)
109 {
110 SEMF_INFO("error");
111 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::IsrError_Isr)));
112 }
113}
114} /* namespace semf */
115#endif
Signal< Error > error
Definition: analogindma.h:42
Signal dataAvailable
Definition: analogindma.h:40
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
void isrError(ADC_HandleTypeDef &adc)
Sending a error signal.
static void systemIsr(ADC_HandleTypeDef &adc)
System-wide interrupt service routine for adc conversion finished.
void isr(ADC_HandleTypeDef &adc)
Interrupt service routine for adc conversion finished.
Stm32AnalogInDma(ADC_HandleTypeDef &hwHandle)
Constructor.
static void systemIsrError(ADC_HandleTypeDef &adc)
System-wide interrupt service routine for adc error.
void start(uint8_t buffer[], size_t bufferSize) override
Starts the hardware modules (ADC with DMA) for reading ADC values.
static LinkedQueue< Stm32AnalogInDma > * queue()
Get the list with all analog ins.
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41