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

Fix ADC pins not working after deep sleep on RAK3172 #15527

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
9 changes: 6 additions & 3 deletions targets/TARGET_STM/TARGET_STM32WL/analogin_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "cmsis.h"
#include "pinmap.h"
#include "PeripheralPins.h"
#include "platform/mbed_power_mgmt.h" // Pf384


#if STATIC_PINMAP_READY
Expand Down Expand Up @@ -82,6 +83,9 @@ static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
obj->handle.Init.Oversampling.TriggeredMode = 0; // workaround
obj->handle.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;

// Disable deep sleep when ADC is configured
sleep_manager_lock_deep_sleep(); // Pf384

// Enable ADC clock
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
__HAL_RCC_ADC_CLK_ENABLE();
Expand Down Expand Up @@ -181,9 +185,8 @@ uint16_t adc_read(analogin_t *obj)
debug("HAL_ADC_ConfigChannel error\n");
}

if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
debug("HAL_ADC_Start error\n");
}
// Enable ADC only when a measurement is needed
HAL_ADC_Start(&obj->handle); // Pc63c

// Wait end of conversion and get value
uint16_t adcValue = 0;
Expand Down
23 changes: 23 additions & 0 deletions targets/TARGET_STM/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ __WEAK void hal_deepsleep(void)

save_timer_ctx();

// Save ADC registers
uint32_t adc_cr = ADC1->CR;
uint32_t adc_cfgr = ADC1->CFGR;
uint32_t adc_sqr1 = ADC1->SQR1;
uint32_t adc_sqr2 = ADC1->SQR2;
uint32_t adc_sqr3 = ADC1->SQR3;
uint32_t adc_sqr4 = ADC1->SQR4;
uint32_t adc_smpr1 = ADC1->SMPR1;
uint32_t adc_smpr2 = ADC1->SMPR2;
uint32_t adc_difsel = ADC1->DIFSEL;
uint32_t adc_calfact = ADC1->CALFACT;

// Request to enter STOP mode with regulator in low power mode
//PWR_CR1_LPMS_STOP2 -> STM32L4 ; PWR_LOWPOWERMODE_STOP2 -> STM32WL
#if defined (PWR_CR1_LPMS_STOP2) || defined(PWR_LOWPOWERMODE_STOP2)
Expand Down Expand Up @@ -278,6 +290,17 @@ __WEAK void hal_deepsleep(void)
* deep sleep */
wait_loop(500);

// Restore ADC registers
ADC1->CR = adc_cr;
ADC1->CFGR = adc_cfgr;
ADC1->SQR1 = adc_sqr1;
ADC1->SQR2 = adc_sqr2;
ADC1->SQR3 = adc_sqr3;
ADC1->SQR4 = adc_sqr4;
ADC1->SMPR1 = adc_smpr1;
ADC1->SMPR2 = adc_smpr2;
ADC1->DIFSEL = adc_difsel;
ADC1->CALFACT = adc_calfact;

restore_timer_ctx();

Expand Down