1 /*
2  * Copyright 2020-2023 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 /**
7 *   @file       Power_Ip_MC_RGM.c
8 *   @version    3.0.0
9 *
10 *   @brief
11 *   @brief   POWER driver implementations.
12 *   @details POWER driver implementations.
13 *
14 *   @addtogroup POWER_DRIVER Power Ip Driver
15 *   @{
16 */
17 
18 #ifdef __cplusplus
19 extern "C"{
20 #endif
21 
22 
23 
24 
25 
26 /*==================================================================================================
27                                          INCLUDE FILES
28  1) system and project includes
29  2) needed interfaces from external units
30  3) internal and external interfaces from this unit
31 ==================================================================================================*/
32 #include "Power_Ip_Private.h"
33 #include "Power_Ip_MC_RGM.h"
34 #include "Power_Ip_MC_ME.h"
35 
36 #if (defined(POWER_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT))
37   #if (defined(MCAL_MC_RGM_REG_PROT_AVAILABLE))
38     #if (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE)
39       #define USER_MODE_REG_PROT_ENABLED      (STD_ON)
40       #include "RegLockMacros.h"
41     #endif /* (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE) */
42   #endif
43 #endif /* (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT) */
44 
45 /*==================================================================================================
46                                SOURCE FILE VERSION INFORMATION
47 ==================================================================================================*/
48 #define POWER_IP_MC_RGM_VENDOR_ID_C                      43
49 #define POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION_C       4
50 #define POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION_C       7
51 #define POWER_IP_MC_RGM_AR_RELEASE_REVISION_VERSION_C    0
52 #define POWER_IP_MC_RGM_SW_MAJOR_VERSION_C               3
53 #define POWER_IP_MC_RGM_SW_MINOR_VERSION_C               0
54 #define POWER_IP_MC_RGM_SW_PATCH_VERSION_C               0
55 
56 /*==================================================================================================
57 *                                     FILE VERSION CHECKS
58 ==================================================================================================*/
59 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_Private.h file are of the same vendor */
60 #if (POWER_IP_MC_RGM_VENDOR_ID_C != POWER_IP_PRIVATE_VENDOR_ID)
61     #error "Power_Ip_MC_RGM.c and Power_Ip_Private.h have different vendor ids"
62 #endif
63 
64 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_Private.h file are of the same Autosar version */
65 #if ((POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION_C != POWER_IP_PRIVATE_AR_RELEASE_MAJOR_VERSION) || \
66      (POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION_C != POWER_IP_PRIVATE_AR_RELEASE_MINOR_VERSION) || \
67      (POWER_IP_MC_RGM_AR_RELEASE_REVISION_VERSION_C != POWER_IP_PRIVATE_AR_RELEASE_REVISION_VERSION) \
68     )
69     #error "AutoSar Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_Private.h are different"
70 #endif
71 
72 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_Private.h file are of the same Software version */
73 #if ((POWER_IP_MC_RGM_SW_MAJOR_VERSION_C != POWER_IP_PRIVATE_SW_MAJOR_VERSION) || \
74      (POWER_IP_MC_RGM_SW_MINOR_VERSION_C != POWER_IP_PRIVATE_SW_MINOR_VERSION) || \
75      (POWER_IP_MC_RGM_SW_PATCH_VERSION_C != POWER_IP_PRIVATE_SW_PATCH_VERSION) \
76     )
77     #error "Software Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_Private.h are different"
78 #endif
79 
80 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_RGM.h file are of the same vendor */
81 #if (POWER_IP_MC_RGM_VENDOR_ID_C != POWER_IP_MC_RGM_VENDOR_ID)
82     #error "Power_Ip_MC_RGM.c and Power_Ip_MC_RGM.h have different vendor ids"
83 #endif
84 
85 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_RGM.h file are of the same Autosar version */
86 #if ((POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION_C != POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION) || \
87      (POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION_C != POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION) || \
88      (POWER_IP_MC_RGM_AR_RELEASE_REVISION_VERSION_C != POWER_IP_MC_RGM_AR_RELEASE_REVISION_VERSION) \
89     )
90     #error "AutoSar Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_MC_RGM.h are different"
91 #endif
92 
93 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_RGM.h file are of the same Software version */
94 #if ((POWER_IP_MC_RGM_SW_MAJOR_VERSION_C != POWER_IP_MC_RGM_SW_MAJOR_VERSION) || \
95      (POWER_IP_MC_RGM_SW_MINOR_VERSION_C != POWER_IP_MC_RGM_SW_MINOR_VERSION) || \
96      (POWER_IP_MC_RGM_SW_PATCH_VERSION_C != POWER_IP_MC_RGM_SW_PATCH_VERSION) \
97     )
98     #error "Software Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_MC_RGM.h are different"
99 #endif
100 
101 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_ME.h file are of the same vendor */
102 #if (POWER_IP_MC_RGM_VENDOR_ID_C != POWER_IP_MC_ME_VENDOR_ID)
103     #error "Power_Ip_MC_RGM.c and Power_Ip_MC_ME.h have different vendor ids"
104 #endif
105 
106 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_ME.h file are of the same Autosar version */
107 #if ((POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION_C != POWER_IP_MC_ME_AR_RELEASE_MAJOR_VERSION) || \
108      (POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION_C != POWER_IP_MC_ME_AR_RELEASE_MINOR_VERSION) || \
109      (POWER_IP_MC_RGM_AR_RELEASE_REVISION_VERSION_C != POWER_IP_MC_ME_AR_RELEASE_REVISION_VERSION) \
110     )
111     #error "AutoSar Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_MC_ME.h are different"
112 #endif
113 
114 /* Check if Power_Ip_MC_RGM.c file and Power_Ip_MC_ME.h file are of the same Software version */
115 #if ((POWER_IP_MC_RGM_SW_MAJOR_VERSION_C != POWER_IP_MC_ME_SW_MAJOR_VERSION) || \
116      (POWER_IP_MC_RGM_SW_MINOR_VERSION_C != POWER_IP_MC_ME_SW_MINOR_VERSION) || \
117      (POWER_IP_MC_RGM_SW_PATCH_VERSION_C != POWER_IP_MC_ME_SW_PATCH_VERSION) \
118     )
119     #error "Software Version Numbers of Power_Ip_MC_RGM.c and Power_Ip_MC_ME.h are different"
120 #endif
121 
122 #if (defined(POWER_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT))
123   #if (defined(MCAL_MC_RGM_REG_PROT_AVAILABLE))
124     #if (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE)
125         #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
126         /* Check if Power_Ip_MC_RGM.c file and RegLockMacros.h file are of the same Autosar version */
127             #if ((POWER_IP_MC_RGM_AR_RELEASE_MAJOR_VERSION_C    != REGLOCKMACROS_AR_RELEASE_MAJOR_VERSION) || \
128                 (POWER_IP_MC_RGM_AR_RELEASE_MINOR_VERSION_C    != REGLOCKMACROS_AR_RELEASE_MINOR_VERSION))
129                 #error "AutoSar Version Numbers of Power_Ip_MC_RGM.c and RegLockMacros.h are different"
130             #endif
131         #endif
132     #endif /* (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE) */
133   #endif
134 #endif /* (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT) */
135 /*==================================================================================================
136                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
137 ==================================================================================================*/
138 
139 
140 /*==================================================================================================
141                                        LOCAL MACROS
142 ==================================================================================================*/
143 
144 
145 /*==================================================================================================
146                                        LOCAL CONSTANTS
147 ==================================================================================================*/
148 
149 
150 /*==================================================================================================
151                                        LOCAL VARIABLES
152 ==================================================================================================*/
153 #define MCU_START_SEC_VAR_CLEARED_32
154 
155 #include "Mcu_MemMap.h"
156 
157 static uint32 DesResetStatus;
158 static uint32 FesResetStatus;
159 
160 #if (defined(POWER_IP_RESET_DURING_STANDBY_SUPPORTED) && (POWER_IP_RESET_DURING_STANDBY_SUPPORTED == STD_ON))
161     #if (defined(POWER_IP_ENTER_LOW_POWER_MODE) && (POWER_IP_ENTER_LOW_POWER_MODE == STD_ON))
162 static uint32 StandbyResetStatus =0U;
163 #endif
164 #endif
165 
166 
167 #define MCU_STOP_SEC_VAR_CLEARED_32
168 
169 #include "Mcu_MemMap.h"
170 
171 
172 #define MCU_START_SEC_VAR_INIT_UNSPECIFIED
173 #include "Mcu_MemMap.h"
174 
175 static Power_Ip_MC_RGM_Type * Power_Ip_pxMC_RGM = (Power_Ip_MC_RGM_Type *)IP_MC_RGM_BASE;
176 #define MCU_STOP_SEC_VAR_INIT_UNSPECIFIED
177 #include "Mcu_MemMap.h"
178 
179 #define MCU_START_SEC_VAR_CLEARED_UNSPECIFIED
180 #include "Mcu_MemMap.h"
181 
182 #if (defined(POWER_IP_RESET_ALTERNATE_ISR_USED) && (POWER_IP_RESET_ALTERNATE_ISR_USED == STD_ON))
183 static volatile Power_MC_RGM_StatusType Power_Ip_eMcRgmStatus = POWER_MC_RGM_UNINIT;
184 #endif
185 
186 #define MCU_STOP_SEC_VAR_CLEARED_UNSPECIFIED
187 #include "Mcu_MemMap.h"
188 /*==================================================================================================
189                                        GLOBAL CONSTANTS
190 ==================================================================================================*/
191 
192 
193 /*==================================================================================================
194                                        GLOBAL VARIABLES
195 ==================================================================================================*/
196 
197 
198 /*==================================================================================================
199                                    LOCAL FUNCTION PROTOTYPES
200 ==================================================================================================*/
201 #define MCU_START_SEC_CODE
202 
203 #include "Mcu_MemMap.h"
204 
205 
206 static void Power_Ip_MC_RGM_ClearFesResetFlags(uint32 RegValue);
207 static void Power_Ip_MC_RGM_ClearDesResetFlags(uint32 RegValue);
208 static Power_Ip_ResetType Power_Ip_MC_RGM_CheckResetReason( uint32 ActiveValue,
209                                                             uint8 ResetMode,
210                                                             uint32 * PositionPtr
211                                                             );
212 
213 /*==================================================================================================
214                                        LOCAL FUNCTIONS
215 ==================================================================================================*/
216 /**
217 * @brief            This function clears all the reset flags from MC_RGM_FES.
218 * @details          This function clears all the reset flags from MC_RGM_FES.
219 *
220 * @param[in]        RegValue     The value of the MC_RGM_FES register.
221 *
222 * @return           void
223 *
224 */
Power_Ip_MC_RGM_ClearFesResetFlags(uint32 RegValue)225 static void Power_Ip_MC_RGM_ClearFesResetFlags(uint32 RegValue)
226 {
227     uint32 StartTime;
228     uint32 ElapsedTime;
229     uint32 TimeoutTicks;
230     boolean TimeoutOccurred = FALSE;
231     uint32 RegValueTemp = RegValue;
232 
233     /* Clear all the reset flags of MC_RGM_FES */
234     if ((uint32)0U != RegValueTemp)
235     {
236         /* Wait until the update has finished. */
237         Power_Ip_StartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, POWER_IP_TIMEOUT_VALUE_US);
238         do
239         {
240             TimeoutOccurred = Power_Ip_TimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
241             Power_Ip_pxMC_RGM->FES = RegValueTemp;
242 
243             RegValueTemp = Power_Ip_pxMC_RGM->FES & MC_RGM_FES_RWBITS_MASK32;
244         }
245         while ( ((uint32)0U != RegValueTemp) && (!TimeoutOccurred) );
246         /* timeout notification */
247         if(TimeoutOccurred)
248         {
249             Power_Ip_ReportPowerErrors(POWER_IP_REPORT_TIMEOUT_ERROR, POWER_IP_ERR_CODE_RESERVED);
250         }
251     }
252 }
253 
254 /**
255 * @brief            This function clears all the reset flags from MC_RGM_DES.
256 * @details          This function clears all the reset flags from MC_RGM_DES.
257 *                   Called by:
258 *                       - Power_Ip_MC_RGM_GetResetReason().
259 *                       - Power_Ip_MC_RGM_GetResetRawValue().
260 *
261 * @param[in]        RegValue     The value of the MC_RGM_DES register.
262 *
263 * @return           void
264 *
265 */
Power_Ip_MC_RGM_ClearDesResetFlags(uint32 RegValue)266 static void Power_Ip_MC_RGM_ClearDesResetFlags(uint32 RegValue)
267 {
268     uint32 StartTime;
269     uint32 ElapsedTime;
270     uint32 TimeoutTicks;
271     boolean TimeoutOccurred = FALSE;
272     uint32 RegValueTemp = RegValue;
273 
274     /* Clear all the reset flags from MC_RGM_DES */
275     if ((uint32)0U != RegValueTemp)
276     {
277         /* Wait until the update has finished. */
278         Power_Ip_StartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, POWER_IP_TIMEOUT_VALUE_US);
279         do
280         {
281             TimeoutOccurred = Power_Ip_TimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
282             Power_Ip_pxMC_RGM->DES = RegValueTemp;
283 
284             RegValueTemp = Power_Ip_pxMC_RGM->DES & MC_RGM_DES_RWBITS_MASK32;
285         }
286         while ( ((uint32)0x00U != RegValueTemp) && (!TimeoutOccurred) );
287         /* Report error when timeout occurred */
288         if (TimeoutOccurred)
289         {
290             Power_Ip_ReportPowerErrors(POWER_IP_REPORT_TIMEOUT_ERROR, POWER_IP_ERR_CODE_RESERVED);
291         }
292     }
293 }
294 
295 /*==================================================================================================
296                                        GLOBAL FUNCTIONS
297 ==================================================================================================*/
298 
299 #if (defined(POWER_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT))
300   #if (defined(MCAL_MC_RGM_REG_PROT_AVAILABLE))
301     #if (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE)
302 /**
303 * @brief            This function will enable writing in User mode by configuring REG_PROT
304 */
Power_Ip_MC_RGM_SetUserAccessAllowed(void)305 void Power_Ip_MC_RGM_SetUserAccessAllowed(void)
306 {
307 #if (defined(IP_MC_RGM_BASE))
308     SET_USER_ACCESS_ALLOWED(IP_MC_RGM_BASE, MC_RGM_PROT_MEM_U32);
309 #endif
310 }
311     #endif /* (STD_ON == MCAL_MC_RGM_REG_PROT_AVAILABLE) */
312   #endif
313 #endif /* (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT) */
314 
315 
316 #if (defined(POWER_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT))
317   #if (defined(MCAL_RDC_REG_PROT_AVAILABLE))
318     #if(STD_ON == MCAL_RDC_REG_PROT_AVAILABLE)
319 /**
320 * @brief            This function will enable writing in User mode by configuring REG_PROT
321 */
Power_Ip_RDC_SetUserAccessAllowed(void)322 void Power_Ip_RDC_SetUserAccessAllowed(void)
323 {
324 #if (defined(IP_RDC_BASE))
325     SET_USER_ACCESS_ALLOWED(IP_RDC_BASE, RDC_PROT_MEM_U32);
326 #endif
327 }
328     #endif
329   #endif
330 #endif /* (STD_ON == POWER_IP_ENABLE_USER_MODE_SUPPORT) */
331 
332 
333 /**
334 * @brief            This function initializes the Reset parameters.
335 * @details          This function configures disabling the functional event reset, disabling the
336 *                   destructive event reset, the functional event alternate request, the
337 *                   destructive event alternate request, the functional event short sequence, the
338 *                   standby reset sequence and enabling the functional bidirectional reset.
339 *
340 * @param[in]        ConfigPtr   Pointer to the MC_RGM configuration structure.
341 *
342 * @return           void
343 *
344 */
Power_Ip_MC_RGM_ResetInit(const Power_Ip_MC_RGM_ConfigType * ConfigPtr)345 void Power_Ip_MC_RGM_ResetInit(const Power_Ip_MC_RGM_ConfigType * ConfigPtr)
346 {
347     /* Temporary variables for FES and DES register values. */
348     uint32 FesResetReason = 0U;
349     uint32 DesResetReason = 0U;
350 
351 
352     /* Warning: It is important to clear the RGM_FES register before setting any of the bits
353      * in the RGM_FERD register to '1'. Otherwise, a redundant interrupt request may occur.
354      * Also, each byte in RGM_FERD can be written only once after a power-on reset. */
355 
356     /* Check "Functional Reset Reason" from FES Status Register. */
357     FesResetReason = Power_Ip_pxMC_RGM->FES;
358 
359     /* Store the contents of FES. */
360     if ((uint32)0U != FesResetReason)
361     {
362         FesResetStatus = FesResetReason;
363         Power_Ip_MC_RGM_ClearFesResetFlags(FesResetReason);
364     }
365 
366 #if (defined(POWER_IP_FUNCTIONAL_RESET_DISABLE_SUPPORT) && (POWER_IP_FUNCTIONAL_RESET_DISABLE_SUPPORT == STD_ON))
367     /* 'Functional' Event Reset Disable Register (RGM_FERD) */
368     Power_Ip_pxMC_RGM->FERD = ConfigPtr->FuncResetOpt;
369 #endif /* POWER_IP_FUNCTIONAL_RESET_DISABLE_SUPPORT */
370 
371     /* Check "Destructive Reset Reason" from DES Status Register. */
372     DesResetReason = Power_Ip_pxMC_RGM->DES;
373 
374     /* Enable (reconfigure) FRET only on "Power On Reset" or "Destructive Reset" event.   */
375     /* Otherwise, the "Functional Reset Escalation" will not work, because the "internal  */
376     /* functional reset counter" will be reset to 0 each time "Mcu_Init" is called.       */
377     /* Therefore, it will never reach the functional threshold value.                     */
378     if ((uint32)0U != DesResetReason)
379     {
380         /* 'Functional' Reset Escalation Threshold Register (MC_RGM_FRET) */
381         Power_Ip_pxMC_RGM->FRET = ConfigPtr->FesThresholdReset;
382     }
383 
384     /* Enable (reconfigure) DRET only on "Power On Reset" event.                          */
385     /* Otherwise, the "Destructive Reset Escalation" will not work, because the "internal */
386     /* destructive reset counter" will be reset to 0 each time "Mcu_Init" is called.      */
387     /* Therefore, it will never reach the destructive threshold value.                    */
388     if (MC_RGM_DES_F_POR_MASK == (DesResetReason & MC_RGM_DES_F_POR_MASK))
389     {
390         /* 'Destructive' Reset Escalation Threshold Register (MC_RGM_DRET) */
391         Power_Ip_pxMC_RGM->DRET = ConfigPtr->DesThresholdReset;
392     }
393 
394     /* Configure the timeout duration of the functional reset entry timer */
395 #if defined(POWER_IP_FUNCTIONAL_RESET_ENTRY_TIMEOUT_SUPPORT)
396   #if (POWER_IP_FUNCTIONAL_RESET_ENTRY_TIMEOUT_SUPPORT == STD_ON)
397     Power_Ip_pxMC_RGM->FRENTC = ConfigPtr->ResetEntryTimeout;
398   #endif
399 #endif
400 
401 #if (defined(POWER_IP_RESET_ALTERNATE_ISR_USED) && (POWER_IP_RESET_ALTERNATE_ISR_USED == STD_ON))
402     Power_Ip_eMcRgmStatus = POWER_MC_RGM_INIT;
403 #endif
404 }
405 
406 
407 /**
408 * @brief            This function check reset reason RGM.
409 * @details          This function check reset reason RGM.
410 *
411 * @param[in]        uint32 ActiveValue, uint8 ResetMode, uint32 * PositionPt
412 *
413 * @return           ResetReason
414 *
415 */
Power_Ip_MC_RGM_CheckResetReason(uint32 ActiveValue,uint8 ResetMode,uint32 * PositionPtr)416 static Power_Ip_ResetType Power_Ip_MC_RGM_CheckResetReason(uint32 ActiveValue, uint8 ResetMode, uint32 * PositionPtr)
417 {
418     /* Temporary variables for DES and FES registers value. */
419     Power_Ip_ResetType ResetReason = MCU_NO_RESET_REASON;
420     uint32 ResetMask;
421     uint32 NumberOfFlags = 0U;
422     uint32 Index;
423     uint32 DynamicMask;
424     uint32 Position = *PositionPtr;
425 
426     /* Type 0 : destructive reset
427        Type 1 : functional reset */
428     if (0U == ResetMode)
429     {
430 #if (defined(POWER_IP_DERIVATIVE_001) || defined(POWER_IP_DERIVATIVE_002) || \
431     defined(POWER_IP_DERIVATIVE_003) || defined(POWER_IP_DERIVATIVE_004) || \
432     defined(POWER_IP_DERIVATIVE_005) || defined(POWER_IP_DERIVATIVE_007) || \
433     defined(POWER_IP_DERIVATIVE_009))
434         /* Combination with CM7_CORE_CLK_FAIL bit mask on the derivatives doesn't have to skip CM7_CORE_CLK_FAIL_RESET reason */
435         /* The ResetMask variable isn't used to filter the value read from register */
436         ResetMask = MC_RGM_DES_RWBITS_MASK32 | MC_RGM_DES_CM7_CORE_CLK_FAIL_MASK32;
437 #else
438         ResetMask = MC_RGM_DES_RWBITS_MASK32;
439 #endif
440     }
441     else
442     {
443 #if (defined(POWER_IP_DERIVATIVE_001) || defined(POWER_IP_DERIVATIVE_002) || \
444      defined(POWER_IP_DERIVATIVE_004) || defined(POWER_IP_DERIVATIVE_005))
445         /* Combination with SWT1/SWT2/SWT3 bit mask on the derivatives doesn't have to skip MCU_SWT1_RST_RESET/MCU_SWT2_RST_RESET/MCU_SWT3_RST_RESET reason */
446         /* The ResetMask variable isn't used to filter the value read from register */
447         ResetMask = (MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT1_RST_MASK32 | MC_RGM_FES_SWT2_RST_MASK32 | MC_RGM_FES_SWT3_RST_MASK32);
448 #elif (defined(POWER_IP_DERIVATIVE_003) || defined(POWER_IP_DERIVATIVE_007))
449         ResetMask = MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT2_RST_MASK32 | MC_RGM_FES_SWT3_RST_MASK32;
450 #elif (defined(POWER_IP_DERIVATIVE_006) || defined(POWER_IP_DERIVATIVE_009))
451         ResetMask = MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT3_RST_MASK32;
452 #else
453         ResetMask = MC_RGM_FES_RWBITS_MASK32;
454 #endif
455 
456 #if (!(defined(POWER_IP_DERIVATIVE_008) || defined(POWER_IP_DERIVATIVE_009)))
457     #if (defined(MC_RGM_FES_PLL_AUX_MASK32))
458         /* Combination with PLL_AUX bit mask on the derivatives doesn't have to skip MCU_PLL_AUX_RESET reason */
459         /* The ResetMask variable isn't used to filter the value read from register */
460         ResetMask |= MC_RGM_FES_PLL_AUX_MASK32;
461     #endif
462 #endif
463     }
464 
465     for (Index = 0U; Index < (uint32)32U; Index++)
466     {
467         DynamicMask = ((uint32)0x01U << Index);
468         if ( (uint32)0U != (DynamicMask & ResetMask) )
469         {
470             if ( (uint32)0U != (DynamicMask & ActiveValue) )
471             {
472                 ResetReason = (Power_Ip_ResetType)Position;
473                 NumberOfFlags++;
474 
475                 /*
476                    MCU_MULTIPLE_RESET_REASON returned if more than one reset reason.
477                    In this case, use function Mcu_GetRawValue to determine.
478                 */
479                 if (2U <= NumberOfFlags)
480                 {
481                     ResetReason = MCU_MULTIPLE_RESET_REASON;
482                     break;
483                 }
484             }
485 
486             Position++;
487         }
488     }
489     /* update value for PositionPtr. It used to calculate in both Des and Fes */
490     *PositionPtr = Position;
491     (void)NumberOfFlags;
492     (void)ActiveValue;
493 
494     return ResetReason;
495 }
496 /**
497 * @brief            This function returns the Reset reason.
498 * @details          This routine returns the Reset reason that is read from the hardware.
499 *                   Called by:
500 *                       - Mcu_Ipw_GetResetReason() from IPW.
501 *
502 * @return           Reason of the Reset event.
503 * @retval           Power_Ip_ResetType   Platform-specific enumeration defined in "".
504 *
505 * @note             The User should ensure that the reset reason is cleared once it has been read
506 *                   out to avoid multiple reset reasons. The function Mcu_GetResetReason shall
507 *                   return MCU_RESET_UNDEFINED if this function is called prior to calling of the
508 *                   function Mcu_Init, and if supported by the hardware.
509 */
Power_Ip_MC_RGM_GetResetReason(void)510 Power_Ip_ResetType Power_Ip_MC_RGM_GetResetReason(void)
511 {
512     /* Code for the Reset event returned by this function. */
513     Power_Ip_ResetType ResetReason;
514 
515     /* Temporary variables for DES and FES registers value. */
516     uint32 RegValue = 0U;
517     uint32 ActiveValue;
518     uint32 Position = POWER_IP_FIRST_RESET_REASON_POS;
519 
520     /* Check "Destructive Reset Reason" from DES Status Register. */
521     RegValue = Power_Ip_pxMC_RGM->DES & MC_RGM_DES_RWBITS_MASK32;
522 
523     /* Store the contents of DES. */
524     if ((uint32)0U != RegValue)
525     {
526         DesResetStatus = RegValue;
527     }
528 
529     ActiveValue = DesResetStatus;
530     /* If at least one bit is set in the RGM_DES register, then it means that some destructive reset event has been logged. */
531     ResetReason = Power_Ip_MC_RGM_CheckResetReason(ActiveValue, 0U, &Position);
532 
533     /* Clear all the reset flags from MC_RGM_DES. */
534     Power_Ip_MC_RGM_ClearDesResetFlags(RegValue);
535 
536     /* If the fields of Destructive Event Status Register (DES) are set then the status of FES register must be ignored */
537     if ((uint32)0U == ActiveValue)
538     {
539         /* Check "Functional Reset Reason" from FES Status Register. */
540         RegValue = Power_Ip_pxMC_RGM->FES & MC_RGM_FES_RWBITS_MASK32;
541 
542         /* Store the contents of FES. */
543         if ((uint32)0U != RegValue)
544         {
545             FesResetStatus = RegValue;
546         }
547 
548         ActiveValue = FesResetStatus;
549         if ((uint32)0U != ActiveValue)
550         {
551             /* If at least one bit is set in the RGM_FES register, then it means that some functional reset event has been logged. */
552             ResetReason = Power_Ip_MC_RGM_CheckResetReason(ActiveValue, 1U, &Position);
553         }
554 
555         /* Clear all the reset flags from MC_RGM_FES */
556         Power_Ip_MC_RGM_ClearFesResetFlags(RegValue);
557     }
558 
559     return ResetReason;
560 }
561 
562 
563 /**
564 * @brief            This function returns the Raw Reset value.
565 * @details          This routine returns the Raw Reset value that is read from the hardware.
566 *
567 * @return           Implementation-specific value with the Reset status.
568 * @retval           Power_Ip_RawResetType   Code of the Raw reset value.
569 *
570 * @note             The User should ensure that the reset reason is cleared once it has been read
571 *                   out to avoid multiple reset reasons.
572 *
573 */
Power_Ip_MC_RGM_GetResetRawValue(void)574 Power_Ip_RawResetType Power_Ip_MC_RGM_GetResetRawValue(void)
575 {
576     /* Temporary variables for DES and FES registers value. */
577     uint32 RawReset = 0U;
578     uint32 RegValue;
579     uint32 ActiveValue;
580     uint32 Index;
581     uint32 DynamicMask;
582     uint32 Position;
583     uint32 ResetMask;
584 #if (defined(POWER_IP_ENTER_LOW_POWER_MODE) && (POWER_IP_ENTER_LOW_POWER_MODE == STD_ON))
585     Power_Ip_PowerModeType PreviousMode;
586 #endif
587 
588     /* Compact the 2x32 registers to 1x32 register. */
589     /* Lower 16 bits will be FES, higher 16 bits will be DES. */
590     RegValue = Power_Ip_pxMC_RGM->FES & MC_RGM_FES_RWBITS_MASK32;
591 
592     /* Store the contents of FES. */
593     if ((uint32)0U != RegValue)
594     {
595         FesResetStatus = RegValue;
596     }
597 #if (defined(POWER_IP_DERIVATIVE_001) || defined(POWER_IP_DERIVATIVE_002) || \
598      defined(POWER_IP_DERIVATIVE_004) || defined(POWER_IP_DERIVATIVE_005))
599     /* Combination with SWT1/SWT2/SWT3 bit mask on the derivatives doesn't have to skip MCU_SWT1_RST_RESET/MCU_SWT2_RST_RESET/MCU_SWT3_RST_RESET reason */
600     /* The ResetMask variable isn't used to filter the value read from register */
601     ResetMask = (MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT1_RST_MASK32 | MC_RGM_FES_SWT2_RST_MASK32 | MC_RGM_FES_SWT3_RST_MASK32);
602 #elif (defined(POWER_IP_DERIVATIVE_003) || defined(POWER_IP_DERIVATIVE_007))
603     ResetMask = MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT2_RST_MASK32 | MC_RGM_FES_SWT3_RST_MASK32;
604 #elif (defined(POWER_IP_DERIVATIVE_006) || defined(POWER_IP_DERIVATIVE_009))
605     ResetMask = MC_RGM_FES_RWBITS_MASK32 | MC_RGM_FES_SWT3_RST_MASK32;
606 #else
607     ResetMask = MC_RGM_FES_RWBITS_MASK32;
608 #endif
609 
610 #if (!(defined(POWER_IP_DERIVATIVE_008) || defined(POWER_IP_DERIVATIVE_009)))
611 #if (defined(MC_RGM_FES_PLL_AUX_MASK32))
612     /* Combination with PLL_AUX bit mask on the derivatives doesn't have to skip MCU_PLL_AUX_RESET reason */
613     /* The ResetMask variable isn't used to filter the value read from register */
614     ResetMask |= MC_RGM_FES_PLL_AUX_MASK32;
615 #endif
616 #endif
617 
618     ActiveValue = FesResetStatus;
619     if ((uint32)0U != ActiveValue)
620     {
621         Position = MC_RGM_RAWRESET_FES_BASE_SHIFT_U32;
622 
623         for (Index = 0U; Index < (uint32)32U; Index++)
624         {
625             DynamicMask = ((uint32)0x01U << Index);
626             if ( (uint32)0U != (DynamicMask & ResetMask) )
627             {
628                 if ( (uint32)0U != (DynamicMask & ActiveValue) )
629                 {
630                     RawReset |= ((uint32)0x01U << Position);
631                 }
632 
633                 Position++;
634             }
635         }
636 
637         /* Clear all the reset flags of MC_RGM_FES. */
638         Power_Ip_MC_RGM_ClearFesResetFlags(RegValue);
639     }
640 
641 #if (defined(POWER_IP_DERIVATIVE_001) || defined(POWER_IP_DERIVATIVE_002) || \
642     defined(POWER_IP_DERIVATIVE_003) || defined(POWER_IP_DERIVATIVE_004) || \
643     defined(POWER_IP_DERIVATIVE_005) || defined(POWER_IP_DERIVATIVE_007) || \
644     defined(POWER_IP_DERIVATIVE_009))
645     ResetMask = MC_RGM_DES_RWBITS_MASK32 | MC_RGM_DES_CM7_CORE_CLK_FAIL_MASK32;
646 #else
647     ResetMask = MC_RGM_DES_RWBITS_MASK32;
648 #endif
649     RegValue = Power_Ip_pxMC_RGM->DES & MC_RGM_DES_RWBITS_MASK32;
650 
651     /* Store the contents of DES. */
652     if ((uint32)0U != RegValue)
653     {
654         DesResetStatus = RegValue;
655     }
656 
657     ActiveValue = DesResetStatus;
658     if ((uint32)0U != ActiveValue)
659     {
660         Position = MC_RGM_RAWRESET_DES_BASE_SHIFT_U32;
661 
662         for (Index = 0x00U; Index < (uint32)32U; Index++)
663         {
664             DynamicMask = ((uint32)0x01U << Index);
665             if ((uint32)0x00U != (DynamicMask & ResetMask))
666             {
667                 if ((uint32)0x00U != (DynamicMask & ActiveValue))
668                 {
669                     RawReset |= ((uint32)0x01U << Position);
670                 }
671 
672                 Position++;
673             }
674         }
675 
676         /* Clear all the reset flags from MC_RGM_DES. */
677         Power_Ip_MC_RGM_ClearDesResetFlags(RegValue);
678     }
679 #if (defined(POWER_IP_ENTER_LOW_POWER_MODE) && (POWER_IP_ENTER_LOW_POWER_MODE == STD_ON))
680     /* If there is no reset reason on the main platform */
681     if ((uint32)0U == RawReset)
682     {
683         PreviousMode = Power_Ip_MC_ME_GetPreviousMode();
684         if ( POWER_IP_SOC_STANDBY_MODE == PreviousMode )
685         {
686             RawReset |= MC_RGM_RAWRESET_WAKEUP_MASK32;
687         }
688     }
689 #endif /*(POWER_IP_ENTER_LOW_POWER_MODE == STD_ON) */
690     return (Power_Ip_RawResetType)RawReset;
691 }
692 
693 #if (defined(POWER_IP_RESET_DURING_STANDBY_SUPPORTED) && (POWER_IP_RESET_DURING_STANDBY_SUPPORTED == STD_ON))
694     #if (defined(POWER_IP_ENTER_LOW_POWER_MODE) && (POWER_IP_ENTER_LOW_POWER_MODE == STD_ON))
695 /**
696 * @brief            This function returns whether a reset occurred during standby.
697 * @details          This function returns whether a reset occurred during standby.
698 *
699 * @return           uint8
700 * @retval           1U        Reset occurred during standby.
701 * @retval           0U       Reset did not occurred during standby.
702 *
703 */
Power_Ip_MC_RGM_ResetDuringStandby(void)704 uint8 Power_Ip_MC_RGM_ResetDuringStandby(void)
705 {
706     uint32 RegValue;
707     uint8 ResetDuringStdby = 0U;
708 
709     RegValue = (uint32)(Power_Ip_pxMC_RGM->RDSS & MC_RGM_RDSS_RWBITS_MASK_U32);
710 
711     /* Store the contents of RDSS. */
712     if ((uint32)0U != RegValue)
713     {
714         StandbyResetStatus = RegValue;
715 
716         /* Clear all the flags from MC_RGM_RDSS */
717         Power_Ip_pxMC_RGM->RDSS = RegValue;
718     }
719 
720 #ifdef MC_RGM_FES_RES_OCCURED_DURING_STANDBY_U32
721     if (
722            (MC_RGM_FES_RES_OCCURED_DURING_STANDBY_U32 == (StandbyResetStatus & MC_RGM_RDSS_FES_RES_MASK_U32)) ||
723            (MC_RGM_DES_RES_OCCURED_DURING_STANDBY_U32 == (StandbyResetStatus & MC_RGM_RDSS_DES_RES_MASK_U32))
724        )
725 #else
726     if (
727            (MC_RGM_DES_RES_OCCURED_DURING_STANDBY_U32 == (StandbyResetStatus & MC_RGM_RDSS_DES_RES_MASK_U32))
728        )
729 #endif
730     {
731         ResetDuringStdby = 1U;
732     }
733 
734     return ResetDuringStdby;
735 }
736     #endif /* (POWER_IP_ENTER_LOW_POWER_MODE == STD_ON) */
737 #endif /* (POWER_IP_RESET_DURING_STANDBY_SUPPORTED == STD_ON) */
738 
739 
740 #if (POWER_IP_PERFORM_RESET_API == STD_ON)
741 /**
742 * @brief            This function performs a microcontroller reset.
743 * @details          This function performs a microcontroller reset by using the hardware feature of
744 *                   the microcontroller.
745 *                   This is a chip-wide virtual mode during which the application is not active.
746 *                   The system remains in this mode until all resources are available for the
747 *                   embedded software to take control of the device.
748 *
749 * @param[in]        ConfigPtr   Pointer to the MC_RGM configuration structure.
750 *
751 * @return           void
752 *
753 */
Power_Ip_MC_RGM_PerformReset(const Power_Ip_MC_RGM_ConfigType * ConfigPtr)754 void Power_Ip_MC_RGM_PerformReset(const Power_Ip_MC_RGM_ConfigType * ConfigPtr)
755 {
756     switch (ConfigPtr->ResetType)
757     {
758         case (MCU_FUNC_RESET):
759         {
760             /* Initiate "Functional Reset" Mode. */
761             Power_Ip_MC_ME_SocTriggerResetEvent(POWER_IP_FUNC_RESET_MODE);
762             break;
763         }
764         case (MCU_DEST_RESET):
765         {
766             /* Initiate "Destructive Reset" Mode. */
767             Power_Ip_MC_ME_SocTriggerResetEvent(POWER_IP_DEST_RESET_MODE);
768             break;
769         }
770         default:
771         {
772             /* Default case - should never enter here. */
773             break;
774         }
775     }
776 }
777 #endif /* (POWER_IP_PERFORM_RESET_API == STD_ON) */
778 
779 #if (defined(POWER_IP_RESET_ALTERNATE_ISR_USED) && (POWER_IP_RESET_ALTERNATE_ISR_USED == STD_ON))
780 /**
781 * @brief            This function handles both the 'Functional' and 'Destructive' Reset
782 *                   Alternate Event Interrupts.
783 * @details          Only the following reset events support interrupt reaction:
784 *                       - DEBUG_FUNC (Functional Debug Reset)
785 *                       - EXR (External Reset)
786 *
787 * @return           void
788 *
789 */
790 
Power_Ip_MC_RGM_ResetAltInt(void)791 void Power_Ip_MC_RGM_ResetAltInt(void)
792 {
793     /* Functional event IRQs */
794     uint32 FesIntResetReason       = 0U;
795     uint32 MaskedFesIntResetReason = 0U;
796 
797     if (POWER_MC_RGM_UNINIT != Power_Ip_eMcRgmStatus)
798     {
799         /* Get only the bits that can generate a functional reset IRQ. */
800         FesIntResetReason = ((uint32)(Power_Ip_pxMC_RGM->FES & MC_RGM_FES_IRQ_BITS_MASK));
801         /* Check for spurious interrupt. */
802         MaskedFesIntResetReason = FesIntResetReason & ( (uint32)(Power_Ip_pxMC_RGM->FERD & MC_RGM_FERD_RWBITS_MASK) );
803 
804         /* Signal the functional alternate reset event (IRQ) to upper layers. */
805         if ((uint32)0U != MaskedFesIntResetReason)
806         {
807             /* Clear active functional IRQs */
808             Power_Ip_pxMC_RGM->FES = ((uint32)(FesIntResetReason & MC_RGM_FES_RWBITS_MASK32));
809 #ifdef POWER_IP_ERROR_ISR_NOTIFICATION
810             POWER_IP_ERROR_ISR_NOTIFICATION(POWER_IP_E_ISR_FUNC_RESET_ALT_FAILURE);
811 #else
812             Power_Ip_ReportPowerErrors(POWER_IP_ISR_ERROR, POWER_IP_E_ISR_FUNC_RESET_ALT_FAILURE);
813 #endif
814         }
815     }
816     else
817     {
818         /* Clear active functional IRQs */
819         Power_Ip_pxMC_RGM->FES = ((uint32)(FesIntResetReason & MC_RGM_FES_RWBITS_MASK32));
820     }
821 }
822 #endif /* ( POWER_IP_RESET_ALTERNATE_ISR_USED == STD_ON ) */
823 
824 
825 #define MCU_STOP_SEC_CODE
826 
827 #include "Mcu_MemMap.h"
828 
829 #ifdef __cplusplus
830 }
831 #endif
832 
833 /** @} */
834 
835