2024-11-19 07:29 PM
Hi,
I would like to use system wake-up pin 1 (PA0) as an external interrupt to wake up my MCU from standby mode. Understand that a rising edge on PA0 (wake-up pin 1) should trigger the MCU to exit standby mode. However, I am encountering difficulties in waking up the MCU using this pin. The pin configuration is shown in the attached picture.
Circuit: Supply high voltage to VIN (5V)to wakeup MCU.
Code:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET); //turn ON led
/* Check and handle if the system was resumed from Standby mode */
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
{
/* Clear Standby flag */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
}
/* Insert 5 seconds delay */
HAL_Delay(5000);
/* The Following Wakeup sequence is highly recommended prior to each Standby mode entry
mainly when using more than one wakeup source this is to not miss any wakeup event.
- Disable all used wakeup sources,
- Clear all related wakeup flags,
- Re-enable all used wakeup sources,
- Enter the Standby mode.
*/
/* Disable all used wakeup sources: PWR_WAKEUP_PIN1 */
HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
/* Clear all related wakeup flags*/
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* Enable WakeUp Pin PWR_WAKEUP_PIN1 connected to PA.00 */
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
/* Enter the Standby mode */
HAL_PWR_EnterSTANDBYMode();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @PAram None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
/*Configure GPIO pin : PB13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
Solved! Go to Solution.
2024-11-22 12:51 AM - edited 2024-11-22 04:59 AM
I may suggest to decrease the values of the resistors.
Your divider generates this voltage level: 5 V x (549/(549+976)) = 1.8V
According to the datasheet:
VIL max = 0.3 x 3.3V = 0,99V
VIH min = 0.7 x 3.3V = 2,31V
Your generated output voltage is neither seen as Low level nor high level voltage as it is seen as an undefined state.
Need to recalculate the resistors value to fit VOUT < 0,99V for low level and VOUT > 2,31V for high level
Also I recommend to decrease the values of the resistors: 549K and 976K are very high.
2024-11-19 08:20 PM
Hi,
The second time for this problem today!
You'll find it's because the compiler puts the enable IRQ instruction behind the while(1)...
Try
/* Enter the Standby mode */
__enable_irq();
HAL_PWR_EnterSTANDBYMode();
Or, put your code after the while(1).
Kind regards
Pedro
2024-11-19 09:32 PM
Hi Pedron,
Cannot either.
The MCU can only wake up once if VIN source is connected before it enters standby mode. After that, removing VIN and plug in again will not wake up the MCU.
2024-11-19 09:57 PM
Hi,
Perhaps I didn't explain enough. Let me try again -
void main(void)
{
THE CODE IN THIS REGION RUNS WITH INTERRPUTS DISABLED
while(1)
{
THE CODE IN THIS REGION RUNS WITH INTERRUPTS ENABLED
}
}
Functions like HAL_Delay(), and many others, DO NOT work when interrupts are disabled...
Kind regards
Pedro
2024-11-21 06:45 PM
I assume that when the board is up and running, the LED turns on, then 5 seconds later it turns off?
Did you check your circuitry to be sure you're getting a high enough voltage to the wakeup pin? What is your Vin voltage?
2024-11-21 07:01 PM
@PGump.1 wrote:Hi,
Perhaps I didn't explain enough. Let me try again -
void main(void)
{
THE CODE IN THIS REGION RUNS WITH INTERRPUTS DISABLED
while(1)
{
THE CODE IN THIS REGION RUNS WITH INTERRUPTS ENABLED
}
}
Functions like HAL_Delay(), and many others, DO NOT work when interrupts are disabled...
Kind regards
Pedro
HAL_Delay() does work before the while loop but after HAL_Init() and SystemClock_Config() are called first.
The OP was correct to write his code after /* USER CODE BEGIN 2 */ because all peripherals have been configured.
2024-11-21 08:53 PM
Hi @Karl Yamashita ,
Are you saying that the compiler puts the __enable_irq() instruction behind the /* USER CODE BEGIN 2 */ line?
If not, where do you think the __enable_irq() occurs?
Kind regards
Pedro
2024-11-21 09:23 PM
Vin is 5v, after volatage divider will be around1.8V. I have did GPIO read from PA0, is detected high.
I also noticed that if I configured PA0 as input pull down, it cannot detect PA0 input as high or low. It only can when I configure PA0 pin as input (GPIO_NOPULL).
2024-11-22 12:51 AM - edited 2024-11-22 04:59 AM
I may suggest to decrease the values of the resistors.
Your divider generates this voltage level: 5 V x (549/(549+976)) = 1.8V
According to the datasheet:
VIL max = 0.3 x 3.3V = 0,99V
VIH min = 0.7 x 3.3V = 2,31V
Your generated output voltage is neither seen as Low level nor high level voltage as it is seen as an undefined state.
Need to recalculate the resistors value to fit VOUT < 0,99V for low level and VOUT > 2,31V for high level
Also I recommend to decrease the values of the resistors: 549K and 976K are very high.
2024-11-22 12:57 AM
Hi SofLit,
My VDD will be 1.8V as well as it goes through a regulator.
VIL max = 0.3 x 1.8V = 0.54V
VIH min = 0.7 x 1.8V = 1.26V
The only thing I can do is reduce the values of the resistors.
Thanks