2025-04-07 1:51 AM
I'm using an STM32WB5MMG module.
I was facing a problem with AD not working properly when exiting from stop2 mode.
HAL_ADC_PollForConversion()
returns a value other than HAL_OK
And,
HAL_ADC_GetValue(&hadc1)
returns only 0.
I have confirmed the same problem in the following post, but it remained unresolved, so I would like to know how to resolve it.
https://community.st.com/t5/stm32-mcus-wireless/how-to-get-adc-to-work-after-stop0-mode-stm32wb5/td-p/95710
Similar to this post
Before entering STOP2 mode, tried to do
HAL_ADC_DeInit(&hadc1);
and after exiting from STOP2 mode, tried to do
MX_ADC1_Init()
But, The process in this function below had error.
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
Is there any approach I should try?
2025-04-30 6:50 PM
Hi,
any information on this topic?
2025-05-05 8:10 AM
Hello @Yuki_I
Could you please share the code that you used to show the ADC is working fine before STOP2 mode and it gets an error after.
Best Regards.
STTwo-32
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-05-07 6:31 PM
Hello, @STTwo-32
The code I am trying is below.
I am using cubeMX V6.12 and CubeFW_WB V.1.20 for code generation.
in main.c
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Config code for STM32_WPAN (HSE Tuning must be done before system clock configuration) */
MX_APPE_Config();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the peripherals common clocks */
PeriphCommonClock_Config();
/* IPCC initialisation */
MX_IPCC_Init();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_RTC_Init();
MX_TIM16_Init();
MX_ADC1_Init();
MX_RF_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_DeInit(&hadc1);
/* USER CODE END 2 */
/* Init code for STM32_WPAN */
MX_APPE_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_TIM_Base_Start_IT(&htim16);
while (1)
{
/* USER CODE END WHILE */
MX_APPE_Process();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
uint16_t ADCdata12bit = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
/* ADC conversion start error */
//Error_Handler();
}
/* Wait for ADC conversion completed */
if (HAL_ADC_PollForConversion(&hadc1, 10) != HAL_OK)
{
/* End Of Conversion flag not set on time */
//Error_Handler();
}
ADCdata12bit = HAL_ADC_GetValue(&hadc1);
if(HAL_ADC_Stop(&hadc1) != HAL_OK)
{
//Error_Handler();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
in app_entry.c
void MX_APPE_Init(void)
{
System_Init(); /**< System initialization */
SystemPower_Config(); /**< Configure the system Power Mode */
HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
/* USER CODE BEGIN APPE_Init_1 */
APPD_Init();
UTIL_LPM_SetOffMode(1 << CFG_LPM_APP, UTIL_LPM_DISABLE);
/* USER CODE END APPE_Init_1 */
appe_Tl_Init(); /* Initialize all transport layers */
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return;
}
in app_ble.c
void APP_BLE_Init(void)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Initialize Ble Transport Layer
*/
Ble_Tl_Init();
/**
* Do not allow standby in the application
*/
UTIL_LPM_SetOffMode(1 << CFG_LPM_APP_BLE, UTIL_LPM_DISABLE);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Start to Advertise to be connected by a Client
*/
Adv_Request(APP_BLE_LP_ADV);
}
SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void *p_Pckt)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
switch (p_event_pckt->evt)
{
case HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE:
{
p_connection_complete_event = (hci_le_connection_complete_event_rp0 *) p_meta_evt->data;
/**
* The connection is done, there is no need anymore to schedule the LP ADV
*/
APP_DBG_MSG(">>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x%x\n", p_connection_complete_event->Connection_Handle);
APP_DBG_MSG(" - Connection established with Central: @:%02x:%02x:%02x:%02x:%02x:%02x\n",
p_connection_complete_event->Peer_Address[5],
p_connection_complete_event->Peer_Address[4],
p_connection_complete_event->Peer_Address[3],
p_connection_complete_event->Peer_Address[2],
p_connection_complete_event->Peer_Address[1],
p_connection_complete_event->Peer_Address[0]);
APP_DBG_MSG(" - Connection Interval: %.2f ms\n - Connection latency: %d\n - Supervision Timeout: %d ms\n\r",
p_connection_complete_event->Conn_Interval*1.25,
p_connection_complete_event->Conn_Latency,
p_connection_complete_event->Supervision_Timeout*10
);
if (BleApplicationContext.Device_Connection_Status == APP_BLE_LP_CONNECTING)
{
/* Connection as client */
BleApplicationContext.Device_Connection_Status = APP_BLE_CONNECTED_CLIENT;
}
else
{
/* Connection as server */
BleApplicationContext.Device_Connection_Status = APP_BLE_CONNECTED_SERVER;
}
BleApplicationContext.BleApplicationContext_legacy.connectionHandle = p_connection_complete_event->Connection_Handle;
/**
* SPECIFIC to Custom Template APP
*/
HandleNotification.Custom_Evt_Opcode = CUSTOM_CONN_HANDLE_EVT;
HandleNotification.ConnectionHandle = BleApplicationContext.BleApplicationContext_legacy.connectionHandle;
Custom_APP_Notification(&HandleNotification);
/* USER CODE BEGIN HCI_EVT_LE_CONN_COMPLETE */
//BLEConnectionStatus = BleApplicationContext.Device_Connection_Status;
UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE, UTIL_LPM_DISABLE);
UTIL_LPM_SetStopMode(1 << CFG_LPM_APP, UTIL_LPM_DISABLE);
//To make sure that the device is not in stop mode when connected.
APP_BLE_Reconnection_Status = 1;
/* USER CODE END HCI_EVT_LE_CONN_COMPLETE */
break; /* HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE */
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~
}
On this code, in app_conf.h
#define CFG_LPM_SUPPORTED 0
the ADC will work correctly after BLE connection,
#define CFG_LPM_SUPPORTED 1
in main.c
HAL_ADC_PollForConversion(&hadc1, 10)
an error is generated.