xref: /btstack/port/stm32-wb55xx-nucleo-freertos/Src/hw_lpm.c (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
1*0561b2d8STREFOU Felix /**
2*0561b2d8STREFOU Felix  ******************************************************************************
3*0561b2d8STREFOU Felix   * File Name          : hw_lpm.c
4*0561b2d8STREFOU Felix   * Description        : Hardware Low Power Mode source file for BLE
5*0561b2d8STREFOU Felix   *                      middleWare.
6*0561b2d8STREFOU Felix   ******************************************************************************
7*0561b2d8STREFOU Felix   * This notice applies to any and all portions of this file
8*0561b2d8STREFOU Felix   * that are not between comment pairs USER CODE BEGIN and
9*0561b2d8STREFOU Felix   * USER CODE END. Other portions of this file, whether
10*0561b2d8STREFOU Felix   * inserted by the user or by software development tools
11*0561b2d8STREFOU Felix   * are owned by their respective copyright owners.
12*0561b2d8STREFOU Felix   *
13*0561b2d8STREFOU Felix   * Copyright (c) 2019 STMicroelectronics International N.V.
14*0561b2d8STREFOU Felix   * All rights reserved.
15*0561b2d8STREFOU Felix   *
16*0561b2d8STREFOU Felix   * Redistribution and use in source and binary forms, with or without
17*0561b2d8STREFOU Felix   * modification, are permitted, provided that the following conditions are met:
18*0561b2d8STREFOU Felix   *
19*0561b2d8STREFOU Felix   * 1. Redistribution of source code must retain the above copyright notice,
20*0561b2d8STREFOU Felix   *    this list of conditions and the following disclaimer.
21*0561b2d8STREFOU Felix   * 2. Redistributions in binary form must reproduce the above copyright notice,
22*0561b2d8STREFOU Felix   *    this list of conditions and the following disclaimer in the documentation
23*0561b2d8STREFOU Felix   *    and/or other materials provided with the distribution.
24*0561b2d8STREFOU Felix   * 3. Neither the name of STMicroelectronics nor the names of other
25*0561b2d8STREFOU Felix   *    contributors to this software may be used to endorse or promote products
26*0561b2d8STREFOU Felix   *    derived from this software without specific written permission.
27*0561b2d8STREFOU Felix   * 4. This software, including modifications and/or derivative works of this
28*0561b2d8STREFOU Felix   *    software, must execute solely and exclusively on microcontroller or
29*0561b2d8STREFOU Felix   *    microprocessor devices manufactured by or for STMicroelectronics.
30*0561b2d8STREFOU Felix   * 5. Redistribution and use of this software other than as permitted under
31*0561b2d8STREFOU Felix   *    this license is void and will automatically terminate your rights under
32*0561b2d8STREFOU Felix   *    this license.
33*0561b2d8STREFOU Felix   *
34*0561b2d8STREFOU Felix   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35*0561b2d8STREFOU Felix   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36*0561b2d8STREFOU Felix   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37*0561b2d8STREFOU Felix   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38*0561b2d8STREFOU Felix   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39*0561b2d8STREFOU Felix   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40*0561b2d8STREFOU Felix   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*0561b2d8STREFOU Felix   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42*0561b2d8STREFOU Felix   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43*0561b2d8STREFOU Felix   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44*0561b2d8STREFOU Felix   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45*0561b2d8STREFOU Felix   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46*0561b2d8STREFOU Felix   *
47*0561b2d8STREFOU Felix   ******************************************************************************
48*0561b2d8STREFOU Felix   */
49*0561b2d8STREFOU Felix 
50*0561b2d8STREFOU Felix /* Includes ------------------------------------------------------------------*/
51*0561b2d8STREFOU Felix #include "app_conf.h"
52*0561b2d8STREFOU Felix #include "hw_conf.h"
53*0561b2d8STREFOU Felix 
54*0561b2d8STREFOU Felix /* Private const -----------------------------------------------------------*/
55*0561b2d8STREFOU Felix const uint32_t HW_LPM_STOP_MODE[3] = {LL_PWR_MODE_STOP0, LL_PWR_MODE_STOP1, LL_PWR_MODE_STOP2};
56*0561b2d8STREFOU Felix const uint32_t HW_LPM_OFF_MODE[2] = {LL_PWR_MODE_STANDBY, LL_PWR_MODE_SHUTDOWN};
57*0561b2d8STREFOU Felix 
HW_LPM_SleepMode(void)58*0561b2d8STREFOU Felix void HW_LPM_SleepMode(void)
59*0561b2d8STREFOU Felix {
60*0561b2d8STREFOU Felix   LL_LPM_EnableSleep(); /**< Clear SLEEPDEEP bit of Cortex System Control Register */
61*0561b2d8STREFOU Felix 
62*0561b2d8STREFOU Felix   /**
63*0561b2d8STREFOU Felix    * This option is used to ensure that store operations are completed
64*0561b2d8STREFOU Felix    */
65*0561b2d8STREFOU Felix #if defined ( __CC_ARM)
66*0561b2d8STREFOU Felix   __force_stores();
67*0561b2d8STREFOU Felix #endif
68*0561b2d8STREFOU Felix 
69*0561b2d8STREFOU Felix   __WFI();
70*0561b2d8STREFOU Felix 
71*0561b2d8STREFOU Felix   return;
72*0561b2d8STREFOU Felix }
73*0561b2d8STREFOU Felix 
HW_LPM_StopMode(HW_LPM_StopModeConf_t configuration)74*0561b2d8STREFOU Felix void HW_LPM_StopMode(HW_LPM_StopModeConf_t configuration)
75*0561b2d8STREFOU Felix {
76*0561b2d8STREFOU Felix   LL_PWR_SetPowerMode(HW_LPM_STOP_MODE[configuration]);
77*0561b2d8STREFOU Felix 
78*0561b2d8STREFOU Felix   LL_LPM_EnableDeepSleep(); /**< Set SLEEPDEEP bit of Cortex System Control Register */
79*0561b2d8STREFOU Felix 
80*0561b2d8STREFOU Felix   /**
81*0561b2d8STREFOU Felix    * This option is used to ensure that store operations are completed
82*0561b2d8STREFOU Felix    */
83*0561b2d8STREFOU Felix #if defined ( __CC_ARM)
84*0561b2d8STREFOU Felix   __force_stores();
85*0561b2d8STREFOU Felix #endif
86*0561b2d8STREFOU Felix 
87*0561b2d8STREFOU Felix   __WFI();
88*0561b2d8STREFOU Felix 
89*0561b2d8STREFOU Felix   return;
90*0561b2d8STREFOU Felix }
91*0561b2d8STREFOU Felix 
HW_LPM_OffMode(HW_LPM_OffModeConf_t configuration)92*0561b2d8STREFOU Felix void HW_LPM_OffMode(HW_LPM_OffModeConf_t configuration)
93*0561b2d8STREFOU Felix {
94*0561b2d8STREFOU Felix     /*
95*0561b2d8STREFOU Felix      * There is no risk to clear all the WUF here because in the current implementation, this API is called
96*0561b2d8STREFOU Felix      * in critical section. If an interrupt occurs while in that critical section before that point,
97*0561b2d8STREFOU Felix      * the flag is set and will be cleared here but the system will not enter Off Mode
98*0561b2d8STREFOU Felix      * because an interrupt is pending in the NVIC. The ISR will be executed when moving out
99*0561b2d8STREFOU Felix      * of this critical section
100*0561b2d8STREFOU Felix      */
101*0561b2d8STREFOU Felix     LL_PWR_ClearFlag_WU();
102*0561b2d8STREFOU Felix 
103*0561b2d8STREFOU Felix     LL_PWR_SetPowerMode(HW_LPM_OFF_MODE[configuration]);
104*0561b2d8STREFOU Felix 
105*0561b2d8STREFOU Felix     LL_LPM_EnableDeepSleep(); /**< Set SLEEPDEEP bit of Cortex System Control Register */
106*0561b2d8STREFOU Felix 
107*0561b2d8STREFOU Felix     /**
108*0561b2d8STREFOU Felix      * This option is used to ensure that store operations are completed
109*0561b2d8STREFOU Felix      */
110*0561b2d8STREFOU Felix #if defined ( __CC_ARM)
111*0561b2d8STREFOU Felix     __force_stores();
112*0561b2d8STREFOU Felix #endif
113*0561b2d8STREFOU Felix 
114*0561b2d8STREFOU Felix     __WFI();
115*0561b2d8STREFOU Felix 
116*0561b2d8STREFOU Felix   return;
117*0561b2d8STREFOU Felix }
118*0561b2d8STREFOU Felix 
119*0561b2d8STREFOU Felix /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
120