Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional conversion complete callback to AdcHandle::Start() #632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Features
- Add an optional conversion complete callback to `AdcHandle::Start()`

### Bugfixes

Expand Down
24 changes: 21 additions & 3 deletions src/per/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ struct dsy_adc
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
bool mux_used; // flag set when mux is configured
AdcHandle::ConversionCompleteCallbackFunctionPtr complete_callback;
void* complete_callback_context;
};

// Static Functions
Expand Down Expand Up @@ -350,6 +352,10 @@ void AdcHandle::Init(AdcChannelConfig* cfg,
{
adc.hadc1.Init.OversamplingMode = DISABLE;
}

adc.complete_callback = nullptr;
adc.complete_callback_context = nullptr;

// Init ADC
if(HAL_ADC_Init(&adc.hadc1) != HAL_OK)
{
Expand Down Expand Up @@ -421,8 +427,12 @@ void AdcHandle::Init(AdcChannelConfig* cfg,
}
}

void AdcHandle::Start()
void AdcHandle::Start(ConversionCompleteCallbackFunctionPtr callback,
void* callback_context)
{
adc.complete_callback = callback;
adc.complete_callback_context = callback_context;

HAL_ADCEx_Calibration_Start(
&adc.hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&adc.hadc1, (uint32_t*)adc.dma_buffer, adc.channels);
Expand Down Expand Up @@ -580,9 +590,17 @@ extern "C"

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance == ADC1 && adc.mux_used)
if(hadc->Instance == ADC1)
{
adc_internal_callback();
if(adc.mux_used)
{
adc_internal_callback();
}

if(adc.complete_callback)
{
adc.complete_callback(adc.complete_callback_context);
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/per/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class AdcHandle
OVS_LAST, /**< & */
};

/** A callback to be executed after a conversion is completed. */
typedef void (*ConversionCompleteCallbackFunctionPtr)(void *context);

AdcHandle() {}
~AdcHandle() {}
/**
Expand All @@ -117,8 +120,15 @@ class AdcHandle
void
Init(AdcChannelConfig *cfg, size_t num_channels, OverSampling ovs = OVS_32);

/** Starts reading from the ADC */
void Start();
/** Starts reading from the ADC.
\param callback an optional callback to be called when a conversion is
complete on all ADC channels. Note that if a mux is used on
a ADC channel, only one of the muxed inputs will have a new
value when this callback is called.
\param callback_context a context pointer that will be handed to the callback
*/
void Start(ConversionCompleteCallbackFunctionPtr callback = nullptr,
void *callback_context = nullptr);

/** Stops reading from the ADC */
void Stop();
Expand Down
Loading