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