2021-03-10 01:56 AM
Hello, I'm trying to set up sleep mode and wake up from it by sending UART message to MCU.
However, MCU does not wake up.
Details:
UART baudrate - 115200
UArt set up in DMA mode, however, before going to sleep I activate RX in IT mode.
clocks setup:
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI
|RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_8;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses 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();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
|RCC_PERIPHCLK_ADC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_HSI;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSI;
PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
PeriphClkInit.PLLSAI1.PLLSAI1N = 8;
PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
}
sleep procedure:
/* Ensure that MSI is wake-up system clock */
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
/* Enable Power Clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* Enable Flash power down mode during Sleep mode */
/* (uncomment this line if power consumption figures */
/* must be measured with Flash still on in Low Power */
/* Sleep mode) */
__HAL_FLASH_SLEEP_POWERDOWN_ENABLE();
/* Reset all RCC Sleep and Stop modes register to */
/* improve power consumption */
RCC->AHB1SMENR = 0x0;
RCC->AHB2SMENR = 0x0;
RCC->AHB3SMENR = 0x0;
RCC->APB1SMENR1 = 0x0;
RCC->APB1SMENR2 = 0x0;
RCC->APB2SMENR = 0x0;
while (1)
{
/* USER CODE END WHILE */
print_debug_msg("Enter sleep mode", DEBUG_MSG);
HAL_Delay(4);
HAL_UART_Abort(&huart1);
/* Activate UART RX in IT mode */
HAL_UART_Receive_IT(&huart1, (uint8_t *)uart1_rx_buf, UART_BUF_SIZE);
/* Reduce the System clock to below 2 MHz */
__HAL_RCC_MSI_RANGE_CONFIG(RCC_MSIRANGE_0);
/* Suspend Tick increment to prevent wakeup by Systick interrupt. */
/* Otherwise the Systick interrupt will wake up the device within 1ms */
/* (HAL time base). */
HAL_SuspendTick();
HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
/* ... Low-power SLEEP mode ... */
/* System is Low Power Run mode when exiting Low Power Sleep mode,
disable low power run mode and reset the clock to initialization configuration */
HAL_PWREx_DisableLowPowerRunMode();
SystemClock_Config();
/* Resume Tick interrupt if disabled prior to Low Power Sleep mode entry */
HAL_ResumeTick();
print_debug_msg("Exit sleep mode", DEBUG_MSG);
}
When MCU goes into sleep mode I send a message over UART and expect it to wake from UART RX ISR, which was activate in UART init fct:
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
Any suggestions?
Solved! Go to Solution.
2021-03-10 02:28 AM
Enable the sleep clocks for the resources you want to function.
2021-03-10 02:28 AM
Enable the sleep clocks for the resources you want to function.
2021-03-10 02:34 AM
Hei, did not get it. If you mean peripheral clock then UART1 is clocked with HSI and it is enabled in SystemClock_Config (added code snippet for Clocks init).