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