![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
Basic example showing how to use the sleep/deepsleep modes on the DISCO_L476VG board.
Dependencies: BSP_DISCO_L476VG LCD_DISCO_L476VG mbed
Diff: main.cpp
- Revision:
- 0:e99d70686026
- Child:
- 2:3201a7da544e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Oct 07 15:38:11 2015 +0000 @@ -0,0 +1,190 @@ +#include "mbed.h" +#include "LCD_DISCO_L476VG.h" + +LCD_DISCO_L476VG lcd; + +DigitalOut led_green(LED1); +DigitalOut led_red(LED2); + +InterruptIn joy_down(JOYSTICK_DOWN); +InterruptIn joy_up(JOYSTICK_UP); + +Ticker app_ticker; + +int deepsleep_mode; + +void SystemClockConfig(void); +void LowPowerConfiguration(void); + +void wakeup_tasks() +{ + led_green = 1; + lcd.Clear(); + lcd.DisplayString((uint8_t *)"WAKEUP"); + wait(0.5); +} + +// Used to enter in deepsleep mode +void joy_down_pressed() +{ + led_red = 1; + deepsleep_mode = 1; + wait(0.5); +} + +// Used to exit from deepsleep mode +void joy_up_pressed() +{ + led_green = 1; + deepsleep_mode = 0; +} + +int main() +{ + led_green = 1; + led_red = 1; + lcd.Clear(); + lcd.DisplayString((uint8_t *)"HELLO"); + wait(1.0); + + // Configure the buttons + joy_down.rise(&joy_down_pressed); + joy_down.mode(PullDown); + joy_up.rise(&joy_up_pressed); + joy_up.mode(PullDown); + + deepsleep_mode = 0; + + LowPowerConfiguration(); + + while(1) { + + SystemClockConfig(); + + app_ticker.detach(); + app_ticker.attach(&wakeup_tasks, 5.0); // Wake-up every 5 seconds + + led_green = 0; + led_red = 0; + lcd.Clear(); + + if (deepsleep_mode == 0) { + lcd.DisplayString((uint8_t *)"SLEEP"); + sleep(); + } else { + // Ensure that MSI is the wake-up system clock + HAL_RCCEx_WakeUpStopCLKConfig(RCC_STOP_WAKEUPCLOCK_MSI); + __HAL_RCC_PWR_CLK_ENABLE(); + HAL_PWREx_DisableLowPowerRunMode(); + lcd.DisplayString((uint8_t *)"DSLEEP"); + deepsleep(); + } + } +} + +void SystemClockConfig(void) +{ + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + + /* MSI 2 MHz as source clock for Low Power Run mode */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; + RCC_OscInitStruct.MSIState = RCC_MSI_ON; + RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5; // 2 MHz + RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + // Error + } + + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 + clocks dividers */ + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | 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 + } + + /* The voltage scaling allows optimizing the power consumption when the device is + clocked below the maximum system frequency, to update the voltage scaling value + regarding system frequency refer to product datasheet. */ + + /* Enable Power Control clock */ + __HAL_RCC_PWR_CLK_ENABLE(); + + if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK) { + // Error + } + + /* Enter Low Power Run mode */ + HAL_PWREx_EnableLowPowerRunMode(); + + /* Disable Power Control clock */ + __HAL_RCC_PWR_CLK_DISABLE(); + + SystemCoreClockUpdate(); +} + +// This is just an example of configuration !!! +void LowPowerConfiguration(void) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + // Configure all unused GPIOs in Analog Input mode + // Used GPIOs are: + // LED1: PA8 + // LED2: PB2 + // Joystick Up/Down: PA3,5 + // LCD: PA6..10,15 + PB0,1,4,5,9,12..15 + PC3..8 + PD8..15 + // SWD: PA13,14 + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + + __HAL_RCC_GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4| + GPIO_PIN_11|GPIO_PIN_12; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8| + GPIO_PIN_10|GPIO_PIN_11; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_9| + GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13| + GPIO_PIN_14|GPIO_PIN_15; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + __HAL_RCC_GPIOD_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3| + GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); + + __HAL_RCC_GPIOE_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3| + GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7| + GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11| + GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15; + HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); + + __HAL_RCC_GPIOH_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); + + // Disable peripherals clock + __HAL_RCC_GPIOH_CLK_DISABLE(); + + // Disable peripherals clock in Sleep/Stop mode + // Keep TIMER5 used for the ticker + RCC->AHB1SMENR = 0x0; + RCC->AHB2SMENR = 0x0; + RCC->AHB3SMENR = 0x0; + RCC->APB1SMENR1 = RCC_APB1SMENR1_TIM5SMEN; + RCC->APB1SMENR2 = 0x0; + RCC->APB2SMENR = 0x0; +}