inport from local
Dependents: Hobbyking_Cheetah_0511
targets/TARGET_STM/sleep.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | /* mbed Microcontroller Library |
NYX | 0:85b3fd62ea1a | 2 | ******************************************************************************* |
NYX | 0:85b3fd62ea1a | 3 | * Copyright (c) 2016, STMicroelectronics |
NYX | 0:85b3fd62ea1a | 4 | * All rights reserved. |
NYX | 0:85b3fd62ea1a | 5 | * |
NYX | 0:85b3fd62ea1a | 6 | * Redistribution and use in source and binary forms, with or without |
NYX | 0:85b3fd62ea1a | 7 | * modification, are permitted provided that the following conditions are met: |
NYX | 0:85b3fd62ea1a | 8 | * |
NYX | 0:85b3fd62ea1a | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
NYX | 0:85b3fd62ea1a | 10 | * this list of conditions and the following disclaimer. |
NYX | 0:85b3fd62ea1a | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
NYX | 0:85b3fd62ea1a | 12 | * this list of conditions and the following disclaimer in the documentation |
NYX | 0:85b3fd62ea1a | 13 | * and/or other materials provided with the distribution. |
NYX | 0:85b3fd62ea1a | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
NYX | 0:85b3fd62ea1a | 15 | * may be used to endorse or promote products derived from this software |
NYX | 0:85b3fd62ea1a | 16 | * without specific prior written permission. |
NYX | 0:85b3fd62ea1a | 17 | * |
NYX | 0:85b3fd62ea1a | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
NYX | 0:85b3fd62ea1a | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NYX | 0:85b3fd62ea1a | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
NYX | 0:85b3fd62ea1a | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
NYX | 0:85b3fd62ea1a | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NYX | 0:85b3fd62ea1a | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
NYX | 0:85b3fd62ea1a | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
NYX | 0:85b3fd62ea1a | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
NYX | 0:85b3fd62ea1a | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
NYX | 0:85b3fd62ea1a | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
NYX | 0:85b3fd62ea1a | 28 | ******************************************************************************* |
NYX | 0:85b3fd62ea1a | 29 | */ |
NYX | 0:85b3fd62ea1a | 30 | #if DEVICE_SLEEP |
NYX | 0:85b3fd62ea1a | 31 | |
NYX | 0:85b3fd62ea1a | 32 | #include "cmsis.h" |
NYX | 0:85b3fd62ea1a | 33 | #include "us_ticker_api.h" |
NYX | 0:85b3fd62ea1a | 34 | #include "sleep_api.h" |
NYX | 0:85b3fd62ea1a | 35 | #include "rtc_api_hal.h" |
NYX | 0:85b3fd62ea1a | 36 | #include "hal_tick.h" |
NYX | 0:85b3fd62ea1a | 37 | #include "mbed_critical.h" |
NYX | 0:85b3fd62ea1a | 38 | |
NYX | 0:85b3fd62ea1a | 39 | extern void HAL_SuspendTick(void); |
NYX | 0:85b3fd62ea1a | 40 | extern void HAL_ResumeTick(void); |
NYX | 0:85b3fd62ea1a | 41 | |
NYX | 0:85b3fd62ea1a | 42 | void hal_sleep(void) |
NYX | 0:85b3fd62ea1a | 43 | { |
NYX | 0:85b3fd62ea1a | 44 | // Disable IRQs |
NYX | 0:85b3fd62ea1a | 45 | core_util_critical_section_enter(); |
NYX | 0:85b3fd62ea1a | 46 | |
NYX | 0:85b3fd62ea1a | 47 | // Stop HAL tick to avoid to exit sleep in 1ms |
NYX | 0:85b3fd62ea1a | 48 | HAL_SuspendTick(); |
NYX | 0:85b3fd62ea1a | 49 | // Request to enter SLEEP mode |
NYX | 0:85b3fd62ea1a | 50 | HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); |
NYX | 0:85b3fd62ea1a | 51 | // Restart HAL tick |
NYX | 0:85b3fd62ea1a | 52 | HAL_ResumeTick(); |
NYX | 0:85b3fd62ea1a | 53 | |
NYX | 0:85b3fd62ea1a | 54 | // Enable IRQs |
NYX | 0:85b3fd62ea1a | 55 | core_util_critical_section_exit(); |
NYX | 0:85b3fd62ea1a | 56 | } |
NYX | 0:85b3fd62ea1a | 57 | |
NYX | 0:85b3fd62ea1a | 58 | void hal_deepsleep(void) |
NYX | 0:85b3fd62ea1a | 59 | { |
NYX | 0:85b3fd62ea1a | 60 | // Disable IRQs |
NYX | 0:85b3fd62ea1a | 61 | core_util_critical_section_enter(); |
NYX | 0:85b3fd62ea1a | 62 | |
NYX | 0:85b3fd62ea1a | 63 | // Stop HAL tick |
NYX | 0:85b3fd62ea1a | 64 | HAL_SuspendTick(); |
NYX | 0:85b3fd62ea1a | 65 | uint32_t EnterTimeUS = us_ticker_read(); |
NYX | 0:85b3fd62ea1a | 66 | |
NYX | 0:85b3fd62ea1a | 67 | // Request to enter STOP mode with regulator in low power mode |
NYX | 0:85b3fd62ea1a | 68 | #if TARGET_STM32L4 |
NYX | 0:85b3fd62ea1a | 69 | int pwrClockEnabled = __HAL_RCC_PWR_IS_CLK_ENABLED(); |
NYX | 0:85b3fd62ea1a | 70 | int lowPowerModeEnabled = PWR->CR1 & PWR_CR1_LPR; |
NYX | 0:85b3fd62ea1a | 71 | |
NYX | 0:85b3fd62ea1a | 72 | if (!pwrClockEnabled) { |
NYX | 0:85b3fd62ea1a | 73 | __HAL_RCC_PWR_CLK_ENABLE(); |
NYX | 0:85b3fd62ea1a | 74 | } |
NYX | 0:85b3fd62ea1a | 75 | if (lowPowerModeEnabled) { |
NYX | 0:85b3fd62ea1a | 76 | HAL_PWREx_DisableLowPowerRunMode(); |
NYX | 0:85b3fd62ea1a | 77 | } |
NYX | 0:85b3fd62ea1a | 78 | |
NYX | 0:85b3fd62ea1a | 79 | HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); |
NYX | 0:85b3fd62ea1a | 80 | |
NYX | 0:85b3fd62ea1a | 81 | if (lowPowerModeEnabled) { |
NYX | 0:85b3fd62ea1a | 82 | HAL_PWREx_EnableLowPowerRunMode(); |
NYX | 0:85b3fd62ea1a | 83 | } |
NYX | 0:85b3fd62ea1a | 84 | if (!pwrClockEnabled) { |
NYX | 0:85b3fd62ea1a | 85 | __HAL_RCC_PWR_CLK_DISABLE(); |
NYX | 0:85b3fd62ea1a | 86 | } |
NYX | 0:85b3fd62ea1a | 87 | #else /* TARGET_STM32L4 */ |
NYX | 0:85b3fd62ea1a | 88 | HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); |
NYX | 0:85b3fd62ea1a | 89 | #endif /* TARGET_STM32L4 */ |
NYX | 0:85b3fd62ea1a | 90 | |
NYX | 0:85b3fd62ea1a | 91 | // Restart HAL tick |
NYX | 0:85b3fd62ea1a | 92 | HAL_ResumeTick(); |
NYX | 0:85b3fd62ea1a | 93 | |
NYX | 0:85b3fd62ea1a | 94 | // Enable IRQs |
NYX | 0:85b3fd62ea1a | 95 | core_util_critical_section_exit(); |
NYX | 0:85b3fd62ea1a | 96 | |
NYX | 0:85b3fd62ea1a | 97 | // After wake-up from STOP reconfigure the PLL |
NYX | 0:85b3fd62ea1a | 98 | SetSysClock(); |
NYX | 0:85b3fd62ea1a | 99 | |
NYX | 0:85b3fd62ea1a | 100 | TIM_HandleTypeDef TimMasterHandle; |
NYX | 0:85b3fd62ea1a | 101 | TimMasterHandle.Instance = TIM_MST; |
NYX | 0:85b3fd62ea1a | 102 | __HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS); |
NYX | 0:85b3fd62ea1a | 103 | |
NYX | 0:85b3fd62ea1a | 104 | #if DEVICE_LOWPOWERTIMER |
NYX | 0:85b3fd62ea1a | 105 | rtc_synchronize(); |
NYX | 0:85b3fd62ea1a | 106 | #endif |
NYX | 0:85b3fd62ea1a | 107 | } |
NYX | 0:85b3fd62ea1a | 108 | |
NYX | 0:85b3fd62ea1a | 109 | #endif |