1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_ASMC_H_
10 #define _FSL_ASMC_H_
11 
12 #include "fsl_common.h"
13 
14 /*! @addtogroup asmc */
15 /*! @{ */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 
21 /*! @name Driver version */
22 /*@{*/
23 /*! @brief ASMC driver version 2.0.1. */
24 #define FSL_ASMC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
25 /*@}*/
26 
27 /*!
28  * @brief System Reset Status
29  */
30 enum _asmc_system_reset_status_flags
31 {
32     kASMC_LowLeakageWakeupResetFlag = ASMC_SRS_WAKEUP_MASK, /*!< Reset caused by LLWU module wakeup source. */
33     kASMC_WatchdogResetFlag         = ASMC_SRS_WDOG1_MASK,  /*!< Reset caused by watchdog timeout.      */
34     kASMC_ChipResetNotPORFlag       = ASMC_SRS_RES_MASK, /*!< Chip Reset caused by a source other than POR occurred. */
35     kASMC_PowerOnResetFlag          = ASMC_SRS_POR_MASK, /*!< Reset caused by POR.        */
36     kASMC_Core1LockupResetFlag      = ASMC_SRS_LOCKUP_MASK, /*!< Reset caused by core LOCKUP event.  */
37     kASMC_SoftwareResetFlag         = ASMC_SRS_SW_MASK, /*!< Reset caused by software setting of SYSRESETREQ bit.    */
38     kASMC_StopModeAcknowledgeErrorResetFlag =
39         ASMC_SRS_SACKERR_MASK /*!< Reset caused by peripheral failure to acknowledge attempt to enter stop mode. */
40 };
41 
42 /*!
43  * @brief Power Modes Protection
44  */
45 typedef enum _asmc_power_mode_protection
46 {
47     kASMC_AllowPowerModeVlls = ASMC_PMPROT_AVLLS_MASK, /*!< Allow Very-Low-Leakage Stop Mode. */
48     kASMC_AllowPowerModeLls  = ASMC_PMPROT_ALLS_MASK,  /*!< Allow Low-Leakage Stop Mode.      */
49     kASMC_AllowPowerModeVlp  = ASMC_PMPROT_AVLP_MASK,  /*!< Allow Very-Low-Power Mode.        */
50 #if (defined(FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE) && FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE)
51     kASMC_AllowPowerModeHsrun = ASMC_PMPROT_AHSRUN_MASK, /*!< Allow High Speed Run mode.        */
52     kASMC_AllowPowerModeAll   = (ASMC_PMPROT_AVLLS_MASK | ASMC_PMPROT_ALLS_MASK | ASMC_PMPROT_AVLP_MASK |
53                                ASMC_PMPROT_AHSRUN_MASK) /*!< Allow all power mode.              */
54 #else
55     kASMC_AllowPowerModeAll =
56         (ASMC_PMPROT_AVLLS_MASK | ASMC_PMPROT_ALLS_MASK | ASMC_PMPROT_AVLP_MASK) /*!< Allow all power mode. */
57 #endif /* FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE */
58 } asmc_power_mode_protection_t;
59 
60 /*!
61  * @brief Power Modes in PMSTAT
62  */
63 typedef enum _asmc_power_state
64 {
65     kASMC_PowerStateRun  = 0x01U << 0U, /*!< 0000_0001 - Current power mode is RUN   */
66     kASMC_PowerStateStop = 0x01U << 1U, /*!< 0000_0010 - Current power mode is STOP  */
67     kASMC_PowerStateVlpr = 0x01U << 2U, /*!< 0000_0100 - Current power mode is VLPR  */
68     kASMC_PowerStateVlpw = 0x01U << 3U, /*!< 0000_1000 - Current power mode is VLPW  */
69     kASMC_PowerStateVlps = 0x01U << 4U, /*!< 0001_0000 - Current power mode is VLPS  */
70     kASMC_PowerStateLls  = 0x01U << 5U, /*!< 0010_0000 - Current power mode is LLS   */
71     kASMC_PowerStateVlls = 0x01U << 6U, /*!< 0100_0000 - Current power mode is VLLS  */
72 #if (defined(FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE) && FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE)
73     kASMC_PowerStateHsrun = 0x01U << 7U /*!< 1000_0000 - Current power mode is HSRUN */
74 #endif                                  /* FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE */
75 } asmc_power_state_t;
76 
77 /*!
78  * @brief Run mode definition
79  */
80 typedef enum _asmc_run_mode
81 {
82     kASMC_RunNormal = 0U, /*!< normal RUN mode.             */
83     kASMC_RunVlpr   = 2U, /*!< Very-Low-Power RUN mode.     */
84 #if (defined(FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE) && FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE)
85     kASMC_Hsrun = 3U /*!< High Speed Run mode (HSRUN). */
86 #endif               /* FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE */
87 } asmc_run_mode_t;
88 
89 /*!
90  * @brief Stop mode definition
91  */
92 typedef enum _asmc_stop_mode
93 {
94     kASMC_StopNormal = 0U, /*!< Normal STOP mode.           */
95     kASMC_StopVlps   = 2U, /*!< Very-Low-Power STOP mode.   */
96     kASMC_StopLls    = 3U, /*!< Low-Leakage Stop mode.      */
97     kASMC_StopVlls   = 4U  /*!< Very-Low-Leakage Stop mode. */
98 } asmc_stop_mode_t;
99 
100 /*!
101  * @brief Partial STOP option
102  */
103 typedef enum _asmc_partial_stop_mode
104 {
105     kASMC_PartialStop  = 0U, /*!< STOP - Normal Stop mode*/
106     kASMC_PartialStop1 = 1U, /*!< Partial Stop with both system and bus clocks disabled*/
107     kASMC_PartialStop2 = 2U, /*!< Partial Stop with system clock disabled and bus clock enabled*/
108 } asmc_partial_stop_option_t;
109 
110 /*******************************************************************************
111  * API
112  ******************************************************************************/
113 
114 #if defined(__cplusplus)
115 extern "C" {
116 #endif /* __cplusplus */
117 
118 /*! @name Auxiliary system mode controller APIs*/
119 /*@{*/
120 
121 /*!
122  * @brief Gets ASMC system reset status flags.
123  *
124  * This function gets all ASMC system reset status flags. The flag indicates the source
125  * of the most recent MCU reset. The reset state of these bits depends on what caused
126  * the MCU to reset. The flags are returned as the logical OR value of the enumerators
127  * @ref _asmc_system_reset_status_flags. To check for a specific status, compare the
128  * return value with enumerators in the @ref _asmc_system_reset_status_flags.
129  * For example, to check whether the reset is caused by POR:
130  * @code
131  *     if (kASMC_PowerOnResetFlag & ASMC_GetSystemResetStatusFlags(ASMC))
132  *     {
133  *         ...
134  *     }
135  * @endcode
136  *
137  * @param base ASMC peripheral base address.
138  * @return ASMC system reset status flags. See "_asmc_system_reset_status_flags".
139  */
ASMC_GetSystemResetStatusFlags(ASMC_Type * base)140 static inline uint32_t ASMC_GetSystemResetStatusFlags(ASMC_Type *base)
141 {
142     return base->SRS;
143 }
144 
145 /*!
146  * @brief Configures all power mode protection settings.
147  *
148  * This function configures the power mode protection settings for
149  * supported power modes in the specified chip family. The available power modes
150  * are defined in the smc_power_mode_protection_t. This should be done at an early
151  * system level initialization stage. See the reference manual for details.
152  * This register can only write once after the power reset.
153  *
154  * The allowed modes are passed as bit map, for example, to allow LLS and VLLS,
155  * use ASMC_SetPowerModeProtection(kASMC_AllowPowerModeVlls | kASMC_AllowPowerModeVlps).
156  * To allow all modes, use ASMC_SetPowerModeProtection(kASMC_AllowPowerModeAll).
157  *
158  * @param base ASMC peripheral base address.
159  * @param allowedModes Bitmap of the allowed power modes. See "asmc_power_mode_protection_t".
160  */
ASMC_SetPowerModeProtection(ASMC_Type * base,uint8_t allowedModes)161 static inline void ASMC_SetPowerModeProtection(ASMC_Type *base, uint8_t allowedModes)
162 {
163     base->PMPROT = (uint32_t)(allowedModes);
164 }
165 
166 /*!
167  * @brief Gets the current power mode status.
168  *
169  * This function returns the current power mode stat. Once application
170  * switches the power mode, it should always check the stat to check whether it
171  * runs into the specified mode or not. An application  should  check
172  * this mode before switching to a different mode. The system  requires that
173  * only certain modes can switch to other specific modes. See the
174  * reference manual for details and the asmc_power_state_t for information about
175  * the power stat.
176  *
177  * @param base ASMC peripheral base address.
178  * @return Current power mode status.
179  */
ASMC_GetPowerModeState(ASMC_Type * base)180 static inline asmc_power_state_t ASMC_GetPowerModeState(ASMC_Type *base)
181 {
182     return (asmc_power_state_t)(base->PMSTAT);
183 }
184 
185 /*!
186  * @brief Prepare to enter stop modes
187  *
188  * This function should be called before entering STOP/VLPS/LLS/VLLS modes.
189  */
ASMC_PreEnterStopModes(void)190 static inline void ASMC_PreEnterStopModes(void)
191 {
192     __disable_irq();
193     __ISB();
194 }
195 
196 /*!
197  * @brief Recovering after wake up from stop modes
198  *
199  * This function should be called after wake up from STOP/VLPS/LLS/VLLS modes.
200  * It is used together with @ref ASMC_PreEnterStopModes.
201  */
ASMC_PostExitStopModes(void)202 static inline void ASMC_PostExitStopModes(void)
203 {
204     __enable_irq();
205     __ISB();
206 }
207 
208 /*!
209  * @brief Prepare to enter wait modes
210  *
211  * This function should be called before entering WAIT/VLPW modes..
212  */
ASMC_PreEnterWaitModes(void)213 static inline void ASMC_PreEnterWaitModes(void)
214 {
215     __disable_irq();
216     __ISB();
217 }
218 
219 /*!
220  * @brief Recovering after wake up from stop modes
221  *
222  * This function should be called after wake up from WAIT/VLPW modes.
223  * It is used together with @ref ASMC_PreEnterWaitModes.
224  */
ASMC_PostExitWaitModes(void)225 static inline void ASMC_PostExitWaitModes(void)
226 {
227     __enable_irq();
228     __ISB();
229 }
230 
231 /*!
232  * @brief Configure the system to RUN power mode.
233  *
234  * @param base ASMC peripheral base address.
235  * @return ASMC configuration error code.
236  */
237 status_t ASMC_SetPowerModeRun(ASMC_Type *base);
238 
239 #if (defined(FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE) && FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE)
240 /*!
241  * @brief Configure the system to HSRUN power mode.
242  *
243  * @param base ASMC peripheral base address.
244  * @return ASMC configuration error code.
245  */
246 status_t ASMC_SetPowerModeHsrun(ASMC_Type *base);
247 #endif /* FSL_FEATURE_ASMC_HAS_HIGH_SPEED_RUN_MODE */
248 
249 /*!
250  * @brief Configure the system to WAIT power mode.
251  *
252  * @param base ASMC peripheral base address.
253  * @return ASMC configuration error code.
254  */
255 status_t ASMC_SetPowerModeWait(ASMC_Type *base);
256 
257 /*!
258  * @brief Configure the system to Stop power mode.
259  *
260  * @param base ASMC peripheral base address.
261  * @param  option Partial Stop mode option.
262  * @return ASMC configuration error code.
263  */
264 status_t ASMC_SetPowerModeStop(ASMC_Type *base, asmc_partial_stop_option_t option);
265 
266 /*!
267  * @brief Configure the system to VLPR power mode.
268  *
269  * @param base ASMC peripheral base address.
270  * @return ASMC configuration error code.
271  */
272 status_t ASMC_SetPowerModeVlpr(ASMC_Type *base);
273 
274 /*!
275  * @brief Configure the system to VLPW power mode.
276  *
277  * @param base ASMC peripheral base address.
278  * @return ASMC configuration error code.
279  */
280 status_t ASMC_SetPowerModeVlpw(ASMC_Type *base);
281 
282 /*!
283  * @brief Configure the system to VLPS power mode.
284  *
285  * @param base ASMC peripheral base address.
286  * @return ASMC configuration error code.
287  */
288 status_t ASMC_SetPowerModeVlps(ASMC_Type *base);
289 
290 /*!
291  * @brief Configure the system to LLS power mode.
292  *
293  * @param base ASMC peripheral base address.
294  * @return ASMC configuration error code.
295  */
296 status_t ASMC_SetPowerModeLls(ASMC_Type *base);
297 
298 /*!
299  * @brief Configure the system to VLLS power mode.
300  *
301  * @param base ASMC peripheral base address.
302  * @return ASMC configuration error code.
303  */
304 status_t ASMC_SetPowerModeVlls(ASMC_Type *base);
305 
306 /*@}*/
307 
308 #if defined(__cplusplus)
309 }
310 #endif /* __cplusplus */
311 
312 /*! @}*/
313 
314 #endif /* _FSL_ASMC_H_ */
315