Skip to content

Commit 4fdf54c

Browse files
committed
Add optional conversion complete callback to AdcHandle::Start()
1 parent 8d106a2 commit 4fdf54c

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/per/adc.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct dsy_adc
147147
ADC_HandleTypeDef hadc1;
148148
DMA_HandleTypeDef hdma_adc1;
149149
bool mux_used; // flag set when mux is configured
150+
AdcHandle::ConversionCompleteCallbackFunctionPtr complete_callback;
151+
void* complete_callback_context;
150152
};
151153

152154
// Static Functions
@@ -350,6 +352,10 @@ void AdcHandle::Init(AdcChannelConfig* cfg,
350352
{
351353
adc.hadc1.Init.OversamplingMode = DISABLE;
352354
}
355+
356+
adc.complete_callback = nullptr;
357+
adc.complete_callback_context = nullptr;
358+
353359
// Init ADC
354360
if(HAL_ADC_Init(&adc.hadc1) != HAL_OK)
355361
{
@@ -421,8 +427,12 @@ void AdcHandle::Init(AdcChannelConfig* cfg,
421427
}
422428
}
423429

424-
void AdcHandle::Start()
430+
void AdcHandle::Start(ConversionCompleteCallbackFunctionPtr callback,
431+
void* callback_context)
425432
{
433+
adc.complete_callback = callback;
434+
adc.complete_callback_context = callback_context;
435+
426436
HAL_ADCEx_Calibration_Start(
427437
&adc.hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
428438
HAL_ADC_Start_DMA(&adc.hadc1, (uint32_t*)adc.dma_buffer, adc.channels);
@@ -580,9 +590,17 @@ extern "C"
580590

581591
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
582592
{
583-
if(hadc->Instance == ADC1 && adc.mux_used)
593+
if(hadc->Instance == ADC1)
584594
{
585-
adc_internal_callback();
595+
if(adc.mux_used)
596+
{
597+
adc_internal_callback();
598+
}
599+
600+
if(adc.complete_callback)
601+
{
602+
adc.complete_callback(adc.complete_callback_context);
603+
}
586604
}
587605
}
588606

src/per/adc.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class AdcHandle
106106
OVS_LAST, /**< & */
107107
};
108108

109+
/** A callback to be executed after a conversion is completed. */
110+
typedef void (*ConversionCompleteCallbackFunctionPtr)(void *context);
111+
109112
AdcHandle() {}
110113
~AdcHandle() {}
111114
/**
@@ -117,8 +120,15 @@ class AdcHandle
117120
void
118121
Init(AdcChannelConfig *cfg, size_t num_channels, OverSampling ovs = OVS_32);
119122

120-
/** Starts reading from the ADC */
121-
void Start();
123+
/** Starts reading from the ADC.
124+
\param callback an optional callback to be called when a conversion is
125+
complete on all ADC channels. Note that if a mux is used on
126+
a ADC channel, only one of the muxed inputs will have a new
127+
value when this callback is called.
128+
\param callback_context a context pointer that will be handed to the callback
129+
*/
130+
void Start(ConversionCompleteCallbackFunctionPtr callback = nullptr,
131+
void *callback_context = nullptr);
122132

123133
/** Stops reading from the ADC */
124134
void Stop();

0 commit comments

Comments
 (0)