diff options
Diffstat (limited to 'firmware/memory_chip_gone/Utilities/lpm')
| -rw-r--r-- | firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.c | 258 | ||||
| -rw-r--r-- | firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.h | 167 |
2 files changed, 425 insertions, 0 deletions
diff --git a/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.c b/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.c new file mode 100644 index 0000000..8f4e7cd --- /dev/null +++ b/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.c @@ -0,0 +1,258 @@ +/** + ****************************************************************************** + * @file stm32_lpm.c + * @author MCD Application Team + * @brief Low Power Manager + ****************************************************************************** + * @attention + * + * <h2><center>© Copyright (c) 2019 STMicroelectronics. + * All rights reserved.</center></h2> + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_lpm.h" +#include "utilities_conf.h" + +/** @addtogroup TINY_LPM + * @{ + */ + +/* Private typedef -----------------------------------------------------------*/ +/* Private macros ------------------------------------------------------------*/ +/** @defgroup TINY_LPM_Private_macros TINY LPM private macros + * @{ + */ + +/** + * @brief macro used to initialized the critical section + */ +#ifndef UTIL_LPM_INIT_CRITICAL_SECTION + #define UTIL_LPM_INIT_CRITICAL_SECTION( ) +#endif + +/** + * @brief macro used to enter the critical section + */ +#ifndef UTIL_LPM_ENTER_CRITICAL_SECTION + #define UTIL_LPM_ENTER_CRITICAL_SECTION( ) UTILS_ENTER_CRITICAL_SECTION( ) +#endif + +/** + * @brief macro used to exit the critical section + */ +#ifndef UTIL_LPM_EXIT_CRITICAL_SECTION + #define UTIL_LPM_EXIT_CRITICAL_SECTION( ) UTILS_EXIT_CRITICAL_SECTION( ) +#endif + +/** + * @brief macro used to enter the critical section when Entering Low Power + * @note this macro is only called inside the function UTIL_LPM_EnterLowPower + * and in a basic configuration shall be identcal to the macro + * UTIL_LPM_EXIT_CRITICAL_SECTION. In general, the request to enter the + * low power mode is already done under a critical section and + * nesting it is useless (in specific implementations not even possible). + * So the users could define their own macro) + */ +#ifndef UTIL_LPM_ENTER_CRITICAL_SECTION_ELP + #define UTIL_LPM_ENTER_CRITICAL_SECTION_ELP( ) UTIL_LPM_ENTER_CRITICAL_SECTION( ) +#endif + +/** + * @brief macro used to exit the critical section when exiting Low Power mode + * @note the behavior of the macro shall be symmetrical with the macro + * UTIL_LPM_ENTER_CRITICAL_SECTION_ELP + */ +#ifndef UTIL_LPM_EXIT_CRITICAL_SECTION_ELP + #define UTIL_LPM_EXIT_CRITICAL_SECTION_ELP( ) UTIL_LPM_EXIT_CRITICAL_SECTION( ) +#endif + +/** + * @} + */ +/* Private function prototypes -----------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private typedef -----------------------------------------------------------*/ +/* Private defines -----------------------------------------------------------*/ +/** @defgroup TINY_LPM_Private_define TINY LPM private defines + * @{ + */ + +/** + * @brief value used to reset the LPM mode + */ +#define UTIL_LPM_NO_BIT_SET (0UL) + +/** + * @} + */ +/* Private macros ------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/** @defgroup TINY_LPM_Private_variables TINY LPM private variables + * @{ + */ + +/** + * @brief value used to represent the LPM state of stop mode + */ +static UTIL_LPM_bm_t StopModeDisable = UTIL_LPM_NO_BIT_SET; + +/** + * @brief value used to represent the LPM state of off mode + */ +static UTIL_LPM_bm_t OffModeDisable = UTIL_LPM_NO_BIT_SET; + +/** + * @} + */ +/* Global variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Functions Definition ------------------------------------------------------*/ + +/** @addtogroup TINY_LPM_Exported_function + * @{ + */ +void UTIL_LPM_Init( void ) +{ + StopModeDisable = UTIL_LPM_NO_BIT_SET; + OffModeDisable = UTIL_LPM_NO_BIT_SET; + UTIL_LPM_INIT_CRITICAL_SECTION( ); +} + +void UTIL_LPM_DeInit( void ) +{ +} + +void UTIL_LPM_SetStopMode( UTIL_LPM_bm_t lpm_id_bm, UTIL_LPM_State_t state ) +{ + UTIL_LPM_ENTER_CRITICAL_SECTION( ); + + switch( state ) + { + case UTIL_LPM_DISABLE: + { + StopModeDisable |= lpm_id_bm; + break; + } + case UTIL_LPM_ENABLE: + { + StopModeDisable &= ( ~lpm_id_bm ); + break; + } + default : + { + break; + } + } + + UTIL_LPM_EXIT_CRITICAL_SECTION( ); +} + +void UTIL_LPM_SetOffMode( UTIL_LPM_bm_t lpm_id_bm, UTIL_LPM_State_t state ) +{ + UTIL_LPM_ENTER_CRITICAL_SECTION( ); + + switch(state) + { + case UTIL_LPM_DISABLE: + { + OffModeDisable |= lpm_id_bm; + break; + } + case UTIL_LPM_ENABLE: + { + OffModeDisable &= ( ~lpm_id_bm ); + break; + } + default : + { + break; + } + } + + UTIL_LPM_EXIT_CRITICAL_SECTION( ); +} + +UTIL_LPM_Mode_t UTIL_LPM_GetMode( void ) +{ + UTIL_LPM_Mode_t mode_selected; + + UTIL_LPM_ENTER_CRITICAL_SECTION( ); + + if( StopModeDisable != UTIL_LPM_NO_BIT_SET ) + { + /** + * At least one user disallows Stop Mode + */ + mode_selected = UTIL_LPM_SLEEPMODE; + } + else + { + if( OffModeDisable != UTIL_LPM_NO_BIT_SET ) + { + /** + * At least one user disallows Off Mode + */ + mode_selected = UTIL_LPM_STOPMODE; + } + else + { + mode_selected = UTIL_LPM_OFFMODE; + } + } + + UTIL_LPM_EXIT_CRITICAL_SECTION( ); + + return mode_selected; +} + +void UTIL_LPM_EnterLowPower( void ) +{ + UTIL_LPM_ENTER_CRITICAL_SECTION_ELP( ); + + if( StopModeDisable != UTIL_LPM_NO_BIT_SET ) + { + /** + * At least one user disallows Stop Mode + * SLEEP mode is required + */ + UTIL_PowerDriver.EnterSleepMode( ); + UTIL_PowerDriver.ExitSleepMode( ); + } + else + { + if( OffModeDisable != UTIL_LPM_NO_BIT_SET ) + { + /** + * At least one user disallows Off Mode + * STOP mode is required + */ + UTIL_PowerDriver.EnterStopMode( ); + UTIL_PowerDriver.ExitStopMode( ); + } + else + { + /** + * OFF mode is required + */ + UTIL_PowerDriver.EnterOffMode( ); + UTIL_PowerDriver.ExitOffMode( ); + } + } + + UTIL_LPM_EXIT_CRITICAL_SECTION_ELP( ); +} + +/** + * @} + */ + +/** + * @} + */ diff --git a/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.h b/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.h new file mode 100644 index 0000000..a262f9c --- /dev/null +++ b/firmware/memory_chip_gone/Utilities/lpm/tiny_lpm/stm32_lpm.h @@ -0,0 +1,167 @@ +/** + ****************************************************************************** + * @file stm32_lpm.h + * @author MCD Application Team + * @brief Header for stm32_lpm.c module + ****************************************************************************** + * @attention + * + * <h2><center>© Copyright (c) 2019 STMicroelectronics. + * All rights reserved.</center></h2> + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** +*/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef STM32_TINY_LPM_H +#define STM32_TINY_LPM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stdint.h" + +/** @defgroup TINY_LPM TINY LPM + * @{ + */ + +/* Exported typedef ---------------------------------------------------------*/ +/** @defgroup TINY_LPM_Exported_typedef TINY LPM exported typedef + * @{ + */ + +/** + * @brief type definition to represent the bit mask of an LPM mode + */ +typedef uint32_t UTIL_LPM_bm_t; + +/** + * @brief type definition to represent value of an LPM mode + */ +typedef enum +{ + UTIL_LPM_ENABLE=0, + UTIL_LPM_DISABLE, +} UTIL_LPM_State_t; + +/** + * @brief type definition to represent the different type of LPM mode + */ + +typedef enum +{ + UTIL_LPM_SLEEPMODE, + UTIL_LPM_STOPMODE, + UTIL_LPM_OFFMODE, +} UTIL_LPM_Mode_t; + +/** + * @} + */ + +/** @defgroup TINY_LPM_Exported_struct TINY LPM exported struct + * @{ + */ + +/** + * @brief LPM driver definition + */ +struct UTIL_LPM_Driver_s +{ + void (*EnterSleepMode) ( void ); /*!<function to enter the sleep mode */ + void (*ExitSleepMode) ( void ); /*!<function to exit the sleep mode */ + void (*EnterStopMode) ( void ); /*!<function to enter the stop mode */ + void (*ExitStopMode) ( void ); /*!<function to exit the stop mode */ + void (*EnterOffMode) ( void ); /*!<function to enter the off mode */ + void (*ExitOffMode) ( void ); /*!<function to exit the off mode */ +}; + +/** + * @} + */ + +/* External variables --------------------------------------------------------*/ + +/** @defgroup TINY_LPM_Exported_struct TINY LPM exported struct + * @{ + */ + +/** + * @brief LPM driver + * + * @note This structure is defined and initialized in the specific platform + * power implementation + */ +extern const struct UTIL_LPM_Driver_s UTIL_PowerDriver; + +/** + * @} + */ + +/* Exported macros -----------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +/** @defgroup TINY_LPM_Exported_function TINY LPM exported functions + * @{ + */ + +/** + * @brief This API Initializes the LPM resources. + */ +void UTIL_LPM_Init( void ); + +/** + * @brief This API Un-Initializes the LPM resources. + */ +void UTIL_LPM_DeInit( void ); + +/** + * @brief This API returns the Low Power Mode selected that will be applied when the system will enter low power mode + * if there is no update between the time the mode is read with this API and the time the system enters + * low power mode. + * @retval the LPM mode based on @ref UTIL_LPM_Mode_t + */ +UTIL_LPM_Mode_t UTIL_LPM_GetMode( void ); + +/** + * @brief This API notifies the low power manager if the specified user allows the Stop mode or not. + * The default mode selection for all users is Stop Mode enabled + * @param lpm_id_bm: identifier of the user ( 1 bit per user ) + * @param state: Specify whether StopMode is allowed or not by this user + */ +void UTIL_LPM_SetStopMode( UTIL_LPM_bm_t lpm_id_bm, UTIL_LPM_State_t state ); + +/** + * @brief This API notifies the low power manager if the specified user allows the Off mode or not. + * The default mode selection for all users is Off mode enabled + * @param lpm_id_bm: identifier of the user ( 1 bit per user ) + * @param state: Specify whether OffMode is allowed or not by this user + */ +void UTIL_LPM_SetOffMode( UTIL_LPM_bm_t lpm_id_bm, UTIL_LPM_State_t state ); + +/** + * @brief This API is called by the low power manager in a critical section (PRIMASK bit set) to allow the + * application to implement dedicated code before entering Low Power Mode + */ +void UTIL_LPM_EnterLowPower( void ); + +/** + *@} + */ + +/** + *@} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* STM32_TINY_LPM_H */ + |
