Basic example showing how to use the sleep/deepsleep modes on the DISCO_L476VG board.

Dependencies:   BSP_DISCO_L476VG LCD_DISCO_L476VG mbed

Revision:
2:3201a7da544e
Parent:
0:e99d70686026
--- a/main.cpp	Tue Jun 06 14:56:42 2017 +0200
+++ b/main.cpp	Thu Jul 06 13:57:53 2017 +0000
@@ -8,35 +8,35 @@
 
 InterruptIn joy_down(JOYSTICK_DOWN);
 InterruptIn joy_up(JOYSTICK_UP);
-
-Ticker app_ticker;
+InterruptIn joy_center(JOYSTICK_CENTER);
 
-int deepsleep_mode;
+#define MODE_WAKEUP     0  // IDD=19mA
+#define MODE_SLEEP      1  // IDD=10mA
+#define MODE_DEEP_SLEEP 2  // IDD= 1mA
 
-void SystemClockConfig(void);
-void LowPowerConfiguration(void);
+static int deepsleep_mode = MODE_WAKEUP;
 
+// Used to enter in wake up mode
 void wakeup_tasks()
 {
     led_green = 1;
+    led_red = 1;
     lcd.Clear();
+    printf("WAKEUP\n");
     lcd.DisplayString((uint8_t *)"WAKEUP");
-    wait(0.5);
+    deepsleep_mode = MODE_WAKEUP;
 }
 
 // Used to enter in deepsleep mode
 void joy_down_pressed()
 {
-    led_red = 1;
-    deepsleep_mode = 1;
-    wait(0.5);
+    deepsleep_mode = MODE_DEEP_SLEEP;
 }
 
-// Used to exit from deepsleep mode
+// Used to enter in sleep mode
 void joy_up_pressed()
 {
-    led_green = 1;
-    deepsleep_mode = 0;
+    deepsleep_mode = MODE_SLEEP;
 }
 
 int main()
@@ -44,147 +44,36 @@
     led_green = 1;
     led_red = 1;
     lcd.Clear();
+    printf("HELLO\n");
     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();
+    joy_center.rise(&wakeup_tasks);
+    joy_center.mode(PullDown);
 
     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) {
+        if (deepsleep_mode == MODE_SLEEP) {
+            lcd.Clear();
+            printf("SLEEP\n");
             lcd.DisplayString((uint8_t *)"SLEEP");
+            led_red = 0;
+            wait(1);
             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");
+            wakeup_tasks();
+        }
+        if (deepsleep_mode == MODE_DEEP_SLEEP) {
+            lcd.Clear();
+            printf("DEEP_SLEEP\n");
+            lcd.DisplayString((uint8_t *)"DEEP");
+            led_green = 0;
+            wait(.1);
             deepsleep();
+            wakeup_tasks();
         }
     }
 }
-
-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;
-}