Skip to content

Commit 065318b

Browse files
fixes according to @iabdalkader review
Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Update src/HALConfig.cpp Update src/AdvancedADC.cpp Update src/AdvancedADC.cpp Co-Authored-By: Ibrahim Abdelkader <[email protected]>
1 parent 0bf0e0a commit 065318b

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

Diff for: src/AdvancedADC.cpp

+34-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "AdvancedADC.h"
2323

2424
#define ADC_NP ((ADCName) NC)
25+
#define ADC_PIN_ALT_MASK (uint32_t) (ALT0 | ALT1 )
2526

2627
struct adc_descr_t {
2728
ADC_HandleTypeDef adc;
@@ -33,7 +34,7 @@ struct adc_descr_t {
3334
DMABuffer<Sample> *dmabuf[2];
3435
};
3536

36-
static uint32_t pin_alt[3] = {0, ALT0, ALT1};
37+
static uint32_t adc_pin_alt[3] = {0, ALT0, ALT1};
3738

3839
static adc_descr_t adc_descr_all[3] = {
3940
{{ADC1}, {DMA1_Stream1, {DMA_REQUEST_ADC1}}, DMA1_Stream1_IRQn, {TIM1}, ADC_EXTERNALTRIG_T1_TRGO,
@@ -124,25 +125,28 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
124125

125126
// Clear ALTx pin.
126127
for (size_t i=0; i<n_channels; i++) {
127-
for (size_t j=0; j<AN_ARRAY_SIZE(pin_alt); j++) {
128-
adc_pins[i] = (PinName) (adc_pins[i] & ~(uint32_t)pin_alt[j]);
129-
}
128+
adc_pins[i] = (PinName) (adc_pins[i] & ~(ADC_PIN_ALT_MASK));
130129
}
131130

132131
// Find an ADC that can be used with these set of pins/channels.
133-
for (size_t i=0; instance == ADC_NP && i<AN_ARRAY_SIZE(pin_alt); i++) {
132+
for (size_t i=0; instance == ADC_NP && i<AN_ARRAY_SIZE(adc_pin_alt); i++) {
134133
// Calculate alternate function pin.
135-
PinName pin = (PinName) (adc_pins[0] | pin_alt[i]); // First pin decides the ADC.
134+
PinName pin = (PinName) (adc_pins[0] | adc_pin_alt[i]); // First pin decides the ADC.
136135

137136
// Check if pin is mapped.
138-
if (pinmap_find_peripheral(pin, PinMap_ADC) == NC) break;
137+
if (pinmap_find_peripheral(pin, PinMap_ADC) == NC) {
138+
break;
139+
}
139140

140141
// Find the first free ADC according to the available ADCs on pin.
141142
for (size_t j=0; instance == ADC_NP && j<AN_ARRAY_SIZE(adc_descr_all); j++) {
142143
descr = &adc_descr_all[j];
143-
if (descr->pool == nullptr && ((ADC_TypeDef*)pinmap_peripheral(pin, PinMap_ADC) == descr->adc.Instance)) {
144-
instance = (ADCName) pinmap_peripheral(pin, PinMap_ADC);
145-
adc_pins[0] = pin;
144+
if (descr->pool == nullptr) {
145+
ADCName tmp_instance = (ADCName) pinmap_peripheral(pin, PinMap_ADC);
146+
if (descr->adc.Instance == ((ADC_TypeDef*) tmp_instance)) {
147+
instance = tmp_instance;
148+
adc_pins[0] = pin;
149+
}
146150
}
147151
}
148152
}
@@ -157,11 +161,13 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
157161
pinmap_pinout(adc_pins[0], PinMap_ADC);
158162
uint8_t ch_init = 1;
159163
for (size_t i=1; i<n_channels; i++) {
160-
for (size_t j=0; j<AN_ARRAY_SIZE(pin_alt); j++) {
164+
for (size_t j=0; j<AN_ARRAY_SIZE(adc_pin_alt); j++) {
161165
// Calculate alternate function pin.
162-
PinName pin = (PinName) (adc_pins[i] | pin_alt[j]);
166+
PinName pin = (PinName) (adc_pins[i] | adc_pin_alt[j]);
163167
// Check if pin is mapped.
164-
if (pinmap_find_peripheral(pin, PinMap_ADC) == NC) break;
168+
if (pinmap_find_peripheral(pin, PinMap_ADC) == NC) {
169+
break;
170+
}
165171
// Check if pin is connected to the selected ADC.
166172
if (instance == pinmap_peripheral(pin, PinMap_ADC)) {
167173
pinmap_pinout(pin, PinMap_ADC);
@@ -173,7 +179,9 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
173179
}
174180

175181
// All channels must share the same instance; if not, bail out.
176-
if (ch_init < n_channels) return 0;
182+
if (ch_init < n_channels) {
183+
return 0;
184+
}
177185

178186
// Allocate DMA buffer pool.
179187
descr->pool = new DMABufferPool<Sample>(n_samples, n_channels, n_buffers);
@@ -184,21 +192,29 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
184192
descr->dmabuf[1] = descr->pool->allocate();
185193

186194
// Init and config DMA.
187-
if (hal_dma_config(&descr->dma, descr->dma_irqn, DMA_PERIPH_TO_MEMORY) < 0) return 0;
195+
if (hal_dma_config(&descr->dma, descr->dma_irqn, DMA_PERIPH_TO_MEMORY) < 0) {
196+
return 0;
197+
}
188198

189199
// Init and config ADC.
190-
if (hal_adc_config(&descr->adc, ADC_RES_LUT[resolution], descr->tim_trig, adc_pins, n_channels) < 0) return 0;
200+
if (hal_adc_config(&descr->adc, ADC_RES_LUT[resolution], descr->tim_trig, adc_pins, n_channels) < 0) {
201+
return 0;
202+
}
191203

192204
// Link DMA handle to ADC handle, and start the ADC.
193205
__HAL_LINKDMA(&descr->adc, DMA_Handle, descr->dma);
194-
if (HAL_ADC_Start_DMA(&descr->adc, (uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size()) != HAL_OK) return 0;
206+
if (HAL_ADC_Start_DMA(&descr->adc, (uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size()) != HAL_OK) {
207+
return 0;
208+
}
195209

196210
// Re/enable DMA double buffer mode.
197211
hal_dma_enable_dbm(&descr->dma, descr->dmabuf[0]->data(), descr->dmabuf[1]->data());
198212

199213
// Init, config and start the ADC timer.
200214
hal_tim_config(&descr->tim, sample_rate);
201-
if (HAL_TIM_Base_Start(&descr->tim) != HAL_OK) return 0;
215+
if (HAL_TIM_Base_Start(&descr->tim) != HAL_OK) {
216+
return 0;
217+
}
202218

203219
return 1;
204220
}

Diff for: src/HALConfig.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger
211211
uint32_t channel = STM_PIN_CHANNEL(function);
212212
sConfig.Rank = ADC_RANK_LUT[rank];
213213
sConfig.Channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel);
214-
if (HAL_ADC_ConfigChannel(adc, &sConfig) != HAL_OK) return -1;
214+
if (HAL_ADC_ConfigChannel(adc, &sConfig) != HAL_OK) {
215+
return -1;
216+
}
215217
}
216218

217219
return 0;

0 commit comments

Comments
 (0)