Skip to content

Draft Catch and raise deadlock in adc config. #16808

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 37 additions & 13 deletions ports/stm32/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.

#include "py/mphal.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "adc.h"

#if defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL)
Expand Down Expand Up @@ -87,6 +89,8 @@
#define ADC_SAMPLETIME_DEFAULT_INT ADC_SAMPLETIME_247CYCLES_5
#endif

#define ADC_RESOLUTION (12)

// Timeout for waiting for end-of-conversion
#define ADC_EOC_TIMEOUT_MS (10)

Expand Down Expand Up @@ -327,14 +331,31 @@ static int adc_get_bits(ADC_TypeDef *adc) {
static void adc_config_channel(ADC_TypeDef *adc, uint32_t channel, uint32_t sample_time) {
#if ADC_V2
if (!(adc->CR & ADC_CR_ADEN)) {
if (adc->CR & 0x3f) {
// Cannot enable ADC with CR!=0
return;
for (uint8_t retry = 0; retry < 3; retry++) {
if (adc->CR & 0x3f) {
// Cannot enable ADC with CR!=0, reset and try again.
adc_config(adc, ADC_RESOLUTION);
if (adc->CR & 0x3f) {
mp_raise_OSError(MP_EPERM);
}
}
adc->ISR = ADC_ISR_ADRDY; // clear ADRDY
adc->CR |= ADC_CR_ADEN;
adc_stabilisation_delay_us(ADC_STAB_DELAY_US);
uint32_t t0 = mp_hal_ticks_ms();
while (!(adc->ISR & ADC_ISR_ADRDY)) {
if (mp_hal_ticks_ms() - t0 > 10) {
// The ADC hasn't enabled, reconfigure it
adc_config(adc, ADC_RESOLUTION);
break;
}
}
if (adc->ISR & ADC_ISR_ADRDY) {
break;
}
}
adc->ISR = ADC_ISR_ADRDY; // clear ADRDY
adc->CR |= ADC_CR_ADEN;
adc_stabilisation_delay_us(ADC_STAB_DELAY_US);
while (!(adc->ISR & ADC_ISR_ADRDY)) {
if (!(adc->ISR & ADC_ISR_ADRDY)) {
mp_raise_OSError(MP_ETIMEDOUT);
}
}
#else
Expand Down Expand Up @@ -445,11 +466,14 @@ static void adc_config_channel(ADC_TypeDef *adc, uint32_t channel, uint32_t samp

static uint32_t adc_read_channel(ADC_TypeDef *adc) {
uint32_t value;
#if defined(STM32G4)
// For STM32G4 there is errata 2.7.7, "Wrong ADC result if conversion done late after
// calibration or previous conversion". According to the errata, this can be avoided
// by performing two consecutive ADC conversions and keeping the second result.
for (uint8_t i = 0; i < 2; i++)
#if defined(STM32G4) || defined(STM32WB)
// For STM32G4 errata 2.7.7 / STM32WB errata 2.7.1:
// "Wrong ADC result if conversion done late after calibration or previous conversion"
// states an incorrect reading is returned if more than 1ms has elapsed since the last
// reading or calibration. According to the errata, this can be avoided by performing
// two consecutive ADC conversions and keeping the second result.
// Note: On STM32WB55 @ 64Mhz each ADC read takes ~ 3us.
for (int8_t i = 0; i < 2; i++)
#endif
{
#if ADC_V2
Expand Down Expand Up @@ -596,7 +620,7 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
mp_hal_pin_config(pin, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
}

adc_config(adc, 12);
adc_config(adc, ADC_RESOLUTION);

machine_adc_obj_t *o = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type);
o->adc = adc;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy