|
| 1 | +/* mbed Microcontroller Library |
| 2 | + ******************************************************************************* |
| 3 | + * Copyright (c) 2019, STMicroelectronics |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + ******************************************************************************* |
| 29 | + */ |
| 30 | +#if DEVICE_SLEEP |
| 31 | + |
| 32 | +#include "sleep_api.h" |
| 33 | +#include "us_ticker_api.h" |
| 34 | +#include "us_ticker_data.h" |
| 35 | +#include "mbed_critical.h" |
| 36 | +#include "mbed_error.h" |
| 37 | +#include "stm32wbxx_ll_hsem.h" |
| 38 | +#include "stm32wbxx_ll_cortex.h" |
| 39 | +#include "hw_conf.h" /* Common BLE file where BLE shared resources are defined */ |
| 40 | + |
| 41 | +extern void save_timer_ctx(void); |
| 42 | +extern void restore_timer_ctx(void); |
| 43 | +extern int serial_is_tx_ongoing(void); |
| 44 | +extern int mbed_sdk_inited; |
| 45 | +extern void SetSysClock(void); |
| 46 | + |
| 47 | +static void Switch_On_HSI( void ) |
| 48 | +{ |
| 49 | + LL_RCC_HSI_Enable(); |
| 50 | + while(!LL_RCC_HSI_IsReady()); |
| 51 | + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI); |
| 52 | + while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI); |
| 53 | + |
| 54 | + return; |
| 55 | +} |
| 56 | + |
| 57 | +static void LPM_EnterStopMode(void) |
| 58 | +{ |
| 59 | + /** |
| 60 | + * This function is called from CRITICAL SECTION |
| 61 | + */ |
| 62 | + |
| 63 | + while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) ); |
| 64 | + |
| 65 | + if ( ! LL_HSEM_1StepLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID ) ) |
| 66 | + { |
| 67 | + if( LL_PWR_IsActiveFlag_C2DS() ) |
| 68 | + { |
| 69 | + /* Release ENTRY_STOP_MODE semaphore */ |
| 70 | + LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 ); |
| 71 | + |
| 72 | + Switch_On_HSI(); |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + Switch_On_HSI(); |
| 78 | + } |
| 79 | + |
| 80 | + /* Release RCC semaphore */ |
| 81 | + LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 ); |
| 82 | + |
| 83 | + return; |
| 84 | +} |
| 85 | + |
| 86 | +static void LPM_ExitStopMode(void) |
| 87 | +{ |
| 88 | + /** |
| 89 | + * This function is called from CRITICAL SECTION |
| 90 | + */ |
| 91 | + |
| 92 | + /* Release ENTRY_STOP_MODE semaphore */ |
| 93 | + LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 ); |
| 94 | + |
| 95 | + if( (LL_RCC_GetSysClkSource() == LL_RCC_SYS_CLKSOURCE_STATUS_HSI) || (LL_PWR_IsActiveFlag_C1STOP() != 0) ) |
| 96 | + { |
| 97 | + LL_PWR_ClearFlag_C1STOP_C1STB(); |
| 98 | + |
| 99 | + while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) ); |
| 100 | + |
| 101 | + if(LL_RCC_GetSysClkSource() == LL_RCC_SYS_CLKSOURCE_STATUS_HSI) |
| 102 | + { |
| 103 | + LL_RCC_HSE_Enable(); |
| 104 | + while(!LL_RCC_HSE_IsReady()); |
| 105 | + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSE); |
| 106 | + while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSE); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + /** |
| 111 | + * As long as the current application is fine with HSE as system clock source, |
| 112 | + * there is nothing to do here |
| 113 | + */ |
| 114 | + } |
| 115 | + |
| 116 | + /* Release RCC semaphore */ |
| 117 | + LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 ); |
| 118 | + } |
| 119 | + |
| 120 | + return; |
| 121 | +} |
| 122 | + |
| 123 | +void HW_LPM_StopMode(void) |
| 124 | +{ |
| 125 | + LL_PWR_SetPowerMode(LL_PWR_MODE_STOP2); |
| 126 | + |
| 127 | + LL_LPM_EnableDeepSleep(); /**< Set SLEEPDEEP bit of Cortex System Control Register */ |
| 128 | + |
| 129 | + /** |
| 130 | + * This option is used to ensure that store operations are completed |
| 131 | + */ |
| 132 | +#if defined ( __CC_ARM) |
| 133 | + __force_stores(); |
| 134 | +#endif |
| 135 | + |
| 136 | + __WFI(); |
| 137 | + |
| 138 | + return; |
| 139 | +} |
| 140 | + |
| 141 | +/* STM32WB has very specific needs to handling STOP mode. |
| 142 | + * The target has a cortex-M4 and cortex-M0 and needs to |
| 143 | + * handle shared ressources btw both cores. |
| 144 | + * Each core can sleep independantly, but the first one that |
| 145 | + * wkaes-up needs to restore the clock tree. |
| 146 | + */ |
| 147 | +void hal_deepsleep(void) |
| 148 | +{ |
| 149 | + /* WORKAROUND: |
| 150 | + * MBED serial driver does not handle deepsleep lock |
| 151 | + * to prevent entering deepsleep until HW serial FIFO is empty. |
| 152 | + * This is tracked in mbed issue 4408. |
| 153 | + * For now, we're checking all Serial HW FIFO. If any transfer is ongoing |
| 154 | + * we're not entering deep sleep and returning immediately. */ |
| 155 | + if (serial_is_tx_ongoing()) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Disable IRQs |
| 160 | + core_util_critical_section_enter(); |
| 161 | + |
| 162 | + save_timer_ctx(); |
| 163 | + |
| 164 | + /* Prevent HAL_GetTick() from using ticker_read_us() to read the |
| 165 | + * us_ticker timestamp until the us_ticker context is restored. */ |
| 166 | + mbed_sdk_inited = 0; |
| 167 | + |
| 168 | + /** |
| 169 | + * Enter the STOP mode |
| 170 | + */ |
| 171 | + LPM_EnterStopMode(); |
| 172 | + HW_LPM_StopMode(); |
| 173 | + LPM_ExitStopMode(); |
| 174 | + |
| 175 | + /* After wake-up from STOP reconfigure the whole clock tree */ |
| 176 | + SetSysClock(); |
| 177 | + |
| 178 | + restore_timer_ctx(); |
| 179 | + |
| 180 | + /* us_ticker context restored, allow HAL_GetTick() to read the us_ticker |
| 181 | + * timestamp via ticker_read_us() again. */ |
| 182 | + mbed_sdk_inited = 1; |
| 183 | + |
| 184 | + // Enable IRQs |
| 185 | + core_util_critical_section_exit(); |
| 186 | +} |
| 187 | + |
| 188 | + |
| 189 | +#endif |
0 commit comments