1 /*
2  * Copyright 2020-2023 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /**
8  *     @file
9  *
10  *     @addtogroup wkpu_icu_ip WKPU IPL
11  *     @{
12  */
13 
14 #ifdef __cplusplus
15 extern "C"{
16 #endif
17 
18 
19 /*==================================================================================================
20 *                                          INCLUDE FILES
21 *  1) system and project includes
22 *  2) needed interfaces from external units
23 *  3) internal and external interfaces from this unit
24 ==================================================================================================*/
25 #include "Std_Types.h"
26 #include "Wkpu_Ip.h"
27 
28 #if (STD_ON == WKPU_IP_USED)
29     #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
30         #include "Devassert.h"
31     #endif
32 
33 #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
34     #if (STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT)
35         #define USER_MODE_REG_PROT_ENABLED  (WKPU_IP_ENABLE_USER_MODE_SUPPORT)
36         #include "RegLockMacros.h"
37     #endif
38 #endif
39 
40 #endif /* WKPU_IP_USED */
41 
42 #include "SchM_Icu.h"
43 /*==================================================================================================
44 *                                         LOCAL MACROS
45 ==================================================================================================*/
46 #define WKPU_IP_VENDOR_ID_C                    43
47 #define WKPU_IP_AR_RELEASE_MAJOR_VERSION_C     4
48 #define WKPU_IP_AR_RELEASE_MINOR_VERSION_C     7
49 #define WKPU_IP_AR_RELEASE_REVISION_VERSION_C  0
50 
51 #define WKPU_IP_SW_MAJOR_VERSION_C  3
52 #define WKPU_IP_SW_MINOR_VERSION_C  0
53 #define WKPU_IP_SW_PATCH_VERSION_C  0
54 
55 /*==================================================================================================
56 *                                       FILE VERSION CHECKS
57 ==================================================================================================*/
58 #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
59     /* Check if header file and Std_Types.h file are of the same Autosar version */
60     #if ((WKPU_IP_AR_RELEASE_MAJOR_VERSION_C != STD_AR_RELEASE_MAJOR_VERSION) || \
61          (WKPU_IP_AR_RELEASE_MINOR_VERSION_C != STD_AR_RELEASE_MINOR_VERSION))
62         #error "AutoSar Version Numbers of Wkpu_Ip.c and Std_Types.h are different"
63     #endif
64     #if (STD_ON == WKPU_IP_USED)
65         #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
66             /* Check if this header file and Devassert.h file are of the same Autosar version */
67             #if ((WKPU_IP_AR_RELEASE_MAJOR_VERSION_C != DEVASSERT_AR_RELEASE_MAJOR_VERSION) || \
68                 (WKPU_IP_AR_RELEASE_MINOR_VERSION_C != DEVASSERT_AR_RELEASE_MINOR_VERSION))
69                 #error "AutoSar Version Numbers of Wkpu_Ip.c and Devassert.h are different"
70             #endif
71         #endif
72 
73         #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
74         #if (STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT)
75         /* Check if header file and RegLockMacros.h file are of the same Autosar version */
76         #if ((WKPU_IP_AR_RELEASE_MAJOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MAJOR_VERSION) || \
77              (WKPU_IP_AR_RELEASE_MINOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MINOR_VERSION))
78             #error "AutoSar Version Numbers of Wkpu_Ip.c and RegLockMacros.h are different"
79         #endif
80         #endif
81     #endif
82     #endif /* WKPU_IP_USED */
83     /* Check if this header file and SchM_Icu.h file are of the same Autosar version */
84     #if ((WKPU_IP_AR_RELEASE_MAJOR_VERSION_C != SCHM_ICU_AR_RELEASE_MAJOR_VERSION) || \
85         (WKPU_IP_AR_RELEASE_MINOR_VERSION_C != SCHM_ICU_AR_RELEASE_MINOR_VERSION))
86         #error "AutoSar Version Numbers of Wkpu_Ip.c and SchM_Icu.h are different"
87     #endif
88 #endif
89 
90 /* Check if source file and ICU header file are of the same vendor */
91 #if (WKPU_IP_VENDOR_ID_C != WKPU_IP_VENDOR_ID)
92     #error "Wkpu_Ip.c and Wkpu_Ip.h have different vendor IDs"
93 #endif
94 /* Check if source file and ICU header file are of the same AutoSar version */
95 #if ((WKPU_IP_AR_RELEASE_MAJOR_VERSION_C  != WKPU_IP_AR_RELEASE_MAJOR_VERSION) || \
96      (WKPU_IP_AR_RELEASE_MINOR_VERSION_C  != WKPU_IP_AR_RELEASE_MINOR_VERSION) || \
97      (WKPU_IP_AR_RELEASE_REVISION_VERSION_C   != WKPU_IP_AR_RELEASE_REVISION_VERSION))
98     #error "AutoSar Version Numbers of Wkpu_Ip.c and Wkpu_Ip.h are different"
99 #endif
100 /* Check if source file and ICU header file are of the same Software version */
101 #if ((WKPU_IP_SW_MAJOR_VERSION_C  != WKPU_IP_SW_MAJOR_VERSION) || \
102      (WKPU_IP_SW_MINOR_VERSION_C  != WKPU_IP_SW_MINOR_VERSION) || \
103      (WKPU_IP_SW_PATCH_VERSION_C  != WKPU_IP_SW_PATCH_VERSION))
104 #error "Software Version Numbers of Wkpu_Ip.c and Wkpu_Ip.h are different"
105 #endif
106 /*==================================================================================================
107 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
108 ==================================================================================================*/
109 
110 
111 /*==================================================================================================
112 *                                        LOCAL CONSTANTS
113 ==================================================================================================*/
114 
115 /*==================================================================================================
116 *                                        LOCAL VARIABLES
117 ==================================================================================================*/
118 
119 /*==================================================================================================
120 *                                        GLOBAL CONSTANTS
121 ==================================================================================================*/
122 
123 /*==================================================================================================
124 *                                        GLOBAL VARIABLES
125 ==================================================================================================*/
126 #if (STD_ON == WKPU_IP_USED)
127 
128 #define ICU_START_SEC_VAR_CLEARED_UNSPECIFIED
129 #include "Icu_MemMap.h"
130 /* Table of initilized Wkpu channels */
131 #if !defined(WKPU_DERIVATIVE_SUPPORT_ONLY_NMI)
132 /* Table of initilized WKPU channels */
133 Wkpu_Ip_State Wkpu_Ip_u32ChState[WKPU_IP_NUM_OF_CHANNELS_USED];
134 #endif /* WKPU_DERIVATIVE_SUPPORT_ONLY_NMI */
135 #define ICU_STOP_SEC_VAR_CLEARED_UNSPECIFIED
136 #include "Icu_MemMap.h"
137 
138 #define ICU_START_SEC_VAR_INIT_8
139 #include "Icu_MemMap.h"
140 /* This array stores the positions in the Wkpu_Ip_u32ChState array of the configured Wkpu channels. */
141 uint8 Wkpu_Ip_IndexInChState[WKPU_IP_NUM_OF_CHANNELS] = WKPU_IP_INITIAL_INDEX_OF_CHANNELS;
142 #define ICU_STOP_SEC_VAR_INIT_8
143 #include "Icu_MemMap.h"
144 
145 #define ICU_START_SEC_CONST_UNSPECIFIED
146 #include "Icu_MemMap.h"
147 /* Table of base addresses for WKPU instances. */
148 WKPU_Type * const Wkpu_Ip_pBase[] = IP_WKPU_BASE_PTRS;
149 #define ICU_STOP_SEC_CONST_UNSPECIFIED
150 #include "Icu_MemMap.h"
151 
152 /*==================================================================================================
153 *                                    LOCAL FUNCTION PROTOTYPES
154 ==================================================================================================*/
155 
156 /*==================================================================================================
157 *                                        LOCAL FUNCTIONS
158 ==================================================================================================*/
159 #define ICU_START_SEC_CODE
160 #include "Icu_MemMap.h"
161 
162 #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
163 #if (STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT)
164 void Wkpu_SetUserAccessAllowed(uint32 WkpuBaseAddr);
165 #endif
166 #if (defined (MCAL_WKPU_REG_PROT_AVAILABLE) && (STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT))
167     #define Call_Wkpu_SetUserAccessAllowed(BaseAddr) OsIf_Trusted_Call1param(Wkpu_SetUserAccessAllowed,(BaseAddr))
168 #else
169     #define Call_Wkpu_SetUserAccessAllowed(BaseAddr)
170 #endif
171 #endif
172 
173 #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
174 #if (STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT)
175 /**
176  * @brief        Enables WKPU registers writing in User Mode by configuring REG_PROT
177  * @details      Sets the UAA (User Access Allowed) bit of the WKPU IP allowing WKPU registers writing in User Mode
178  *
179  * @param[in]    WkpuBaseAddr
180  *
181  * @return       none
182  *
183  * @pre          Should be executed in supervisor mode
184  */
Wkpu_SetUserAccessAllowed(uint32 WkpuBaseAddr)185 void Wkpu_SetUserAccessAllowed(uint32 WkpuBaseAddr)
186 {
187     SET_USER_ACCESS_ALLOWED(WkpuBaseAddr, WKPU_PROT_MEM_U32);
188 }
189 #endif /* STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT */
190 #endif /* STD_ON == WKPU_IP_ENABLE_USER_MODE_SUPPORT */
191 
192 #if !defined(WKPU_DERIVATIVE_SUPPORT_ONLY_NMI)
193 /*!
194  * @brief Enables or disables wakeup request
195  *
196  * This function enables or disables wakeup request
197  *
198  * @param[in] base The WKPU peripheral base address
199  * @param[in] channelMask The channel mask
200  * @param[in] enable Enables or disables wakeup request
201  */
Wkpu_Ip_WakeupRequest(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)202 static inline void Wkpu_Ip_WakeupRequest(WKPU_Type * const base,
203                                       uint32 channelMaskLo,
204 #ifdef WKPU_IP_64_CH_USED
205                                       uint32 channelMaskHi,
206 #endif
207                                       boolean enable)
208 {
209     /* Enable wake-up request */
210     if (enable)
211     {
212         base->WRER |= channelMaskLo;
213 #ifdef WKPU_IP_64_CH_USED
214         base->WRER_64 |= channelMaskHi;
215 #endif
216     }
217     /* Disable wake-up request */
218     else
219     {
220         base->WRER &= ~channelMaskLo;
221 #ifdef WKPU_IP_64_CH_USED
222         base->WRER_64 &= ~channelMaskHi;
223 #endif
224     }
225 }
226 
Wkpu_Ip_InterruptRequest(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)227 static inline void Wkpu_Ip_InterruptRequest(WKPU_Type * const base,
228                                             uint32 channelMaskLo,
229 #ifdef WKPU_IP_64_CH_USED
230                                             uint32 channelMaskHi,
231 #endif
232                                             boolean enable)
233 {
234     /* Enable interrupt request */
235     if (enable)
236     {
237         base->IRER |= channelMaskLo;
238 #ifdef WKPU_IP_64_CH_USED
239         base->IRER_64 |= channelMaskHi;
240 #endif
241     }
242     /* Disable interrupt request */
243     else
244     {
245         base->IRER &= ~channelMaskLo;
246 #ifdef WKPU_IP_64_CH_USED
247         base->IRER_64 &= ~channelMaskHi;
248 #endif
249     }
250 }
251 
252 /*!
253  * @brief Enables or disables rising edge event
254  *
255  * This function enables or disables rising edge event
256  *
257  * @param[in] base The WKPU peripheral base address
258  * @param[in] channelMask The channel mask
259  * @param[in] enable Enables or disables rising edge event
260  */
Wkpu_Ip_EnableRisingEdge(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)261 static inline void Wkpu_Ip_EnableRisingEdge(WKPU_Type * const base,
262                                      uint32 channelMaskLo,
263 #ifdef WKPU_IP_64_CH_USED
264                                      uint32 channelMaskHi,
265 #endif
266                                      boolean enable)
267 {
268     /* Enables Wakeup/Interrupt Rising edge event enable Register */
269     if (enable)
270     {
271         base->WIREER |= channelMaskLo;
272 #ifdef WKPU_IP_64_CH_USED
273         base->WIREER_64 |= channelMaskHi;
274 #endif
275     }
276     /* Disables Wakeup/Interrupt Rising edge event enable Register */
277     else
278     {
279         base->WIREER &= ~channelMaskLo;
280 #ifdef WKPU_IP_64_CH_USED
281         base->WIREER_64 &= ~channelMaskHi;
282 #endif
283     }
284 }
285 
286 /*!
287  * @brief Enables or disables falling edge event
288  *
289  * This function enables or disables falling edge event
290  *
291  * @param[in] base The WKPU peripheral base address
292  * @param[in] channelMask The channel mask
293  * @param[in] enable Enables or disables falling edge event
294  */
Wkpu_Ip_EnableFallingEdge(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)295 static inline void Wkpu_Ip_EnableFallingEdge(WKPU_Type * const base,
296                                       uint32 channelMaskLo,
297 #ifdef WKPU_IP_64_CH_USED
298                                       uint32 channelMaskHi,
299 #endif
300                                       boolean enable)
301 {
302     /* Enables Wakeup/Interrupt Falling edge event enable Register */
303     if (enable)
304     {
305         base->WIFEER |= channelMaskLo;
306 #ifdef WKPU_IP_64_CH_USED
307         base->WIFEER_64 |= channelMaskHi;
308 #endif
309     }
310     /* Disables Wakeup/Interrupt Falling edge event enable Register */
311     else
312     {
313         base->WIFEER &= ~channelMaskLo;
314 #ifdef WKPU_IP_64_CH_USED
315         base->WIFEER_64 &= ~channelMaskHi;
316 #endif
317     }
318 }
319 
320 /*!
321  * @brief Enables or disables filter
322  *
323  * This function enables or disables filter
324  *
325  * @param[in] base The WKPU peripheral base address
326  * @param[in] channelMask The channel mask
327  * @param[in] enable Enables or disables filter
328  */
Wkpu_Ip_Filter(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)329 static inline void Wkpu_Ip_Filter(WKPU_Type * const base,
330                                     uint32 channelMaskLo,
331 #ifdef WKPU_IP_64_CH_USED
332                                     uint32 channelMaskHi,
333 #endif
334                                     boolean enable)
335 {
336     /* Enables Wakeup/Interrupt Filter Enable Register */
337     if (enable)
338     {
339         base->WIFER |= channelMaskLo;
340 #ifdef WKPU_IP_64_CH_USED
341         base->WIFER_64 |= channelMaskHi;
342 #endif
343     }
344     /* Disables Wakeup/Interrupt Filter Enable Register */
345     else
346     {
347         base->WIFER &= ~channelMaskLo;
348 #ifdef WKPU_IP_64_CH_USED
349         base->WIFER_64 &= ~channelMaskHi;
350 #endif
351     }
352 }
353 
354 #ifdef WKPU_IP_SUPPORT_PULLUP
355 /*!
356  * @brief Enables or disables request enable
357  *
358  * This function enables or disables request enable
359  *
360  * @param[in] base The WKPU peripheral base address
361  * @param[in] channelMask The channel mask
362  * @param[in] enable Enables or disables pull
363  */
Wkpu_Ip_PullUp(WKPU_Type * const base,uint32 channelMaskLo,uint32 channelMaskHi,boolean enable)364 static inline void Wkpu_Ip_PullUp(WKPU_Type * const base,
365                                   uint32 channelMaskLo,
366 #ifdef WKPU_IP_64_CH_USED
367                                   uint32 channelMaskHi,
368 #endif
369                                   boolean enable)
370 {
371     if (enable)
372     {
373         base->WIPER |= channelMaskLo;
374 #ifdef WKPU_IP_64_CH_USED
375         base->WIPER_64 |= channelMaskHi;
376 #endif
377     }
378     else
379     {
380         base->WIPER &= ~channelMaskLo;
381 #ifdef WKPU_IP_64_CH_USED
382         base->WIPER_64 &= ~channelMaskHi;
383 #endif
384     }
385 }
386 #endif
387 
388 /*==================================================================================================
389 *                                        GLOBAL FUNCTIONS
390 ==================================================================================================*/
391 /**
392 * @brief      Icu driver function that enables the interrupt of WKPU channel.
393 * @details    This function enables WKPU Channel Interrupt
394 *
395 * @param[in]  u32ChannelMask - Channel's Bit Mask
396 *
397 * @implements Wkpu_Ip_EnableInterrupt_Activity
398 */
Wkpu_Ip_EnableInterrupt(uint8 instance,uint8 hwChannel)399 void Wkpu_Ip_EnableInterrupt(uint8 instance, uint8 hwChannel)
400 {
401     WKPU_Type * base;
402     uint32 channelMaskLo = (uint32)0U;
403     uint32 channelMaskHi = (uint32)0U;
404 
405 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
406     DevAssert(instance < WKPU_INSTANCE_COUNT);
407     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
408 #endif
409 
410     base = Wkpu_Ip_pBase[instance];
411 
412     SchM_Enter_Icu_ICU_EXCLUSIVE_AREA_57();
413 
414     if(32U > hwChannel)
415     {
416         channelMaskLo = (uint32)1UL << (uint32)hwChannel;
417         channelMaskHi = (uint32)0U;
418     }
419 #ifdef WKPU_IP_64_CH_USED
420     else
421     {
422         channelMaskLo = (uint32)0U;
423         channelMaskHi = (uint32)1UL << ((uint32)hwChannel - (uint32)32U);
424     }
425 #endif
426 
427     /* Write 1 to wakeup/interrupt status flag register to clear the flag. */
428     base->WISR    |= channelMaskLo;
429 #ifdef WKPU_IP_64_CH_USED
430     base->WISR_64 |= channelMaskLo;
431 #endif
432 
433     Wkpu_Ip_InterruptRequest(base,
434                             channelMaskLo,
435 #ifdef WKPU_IP_64_CH_USED
436                             channelMaskHi,
437 #endif
438                             TRUE);
439 
440     Wkpu_Ip_WakeupRequest(base,
441                             channelMaskLo,
442 #ifdef WKPU_IP_64_CH_USED
443                             channelMaskHi,
444 #endif
445                             TRUE);
446 #ifndef WKPU_IP_64_CH_USED
447     /* To avoid compilerwarning*/
448     (void)channelMaskHi;
449 #endif
450     SchM_Exit_Icu_ICU_EXCLUSIVE_AREA_57();
451 }
452 
453 
454 /**
455 * @brief      Icu driver function that disables the interrupt of WKPU channel.
456 * @details    This function disables WKPU Channel Interrupt
457 *
458 * @param[in]  u32ChannelMask - Channel's Bit Mask
459 *
460 * @implements Wkpu_Ip_DisableInterrupt_Activity
461 */
Wkpu_Ip_DisableInterrupt(uint8 instance,uint8 hwChannel)462 void Wkpu_Ip_DisableInterrupt(uint8 instance, uint8 hwChannel)
463 {
464     WKPU_Type * base;
465     uint32 channelMaskLo = (uint32)0U;
466     uint32 channelMaskHi = (uint32)0U;
467 
468 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
469     DevAssert(instance < WKPU_INSTANCE_COUNT);
470     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
471 #endif
472 
473     base = Wkpu_Ip_pBase[instance];
474 
475     SchM_Enter_Icu_ICU_EXCLUSIVE_AREA_58();
476 
477     if(32U > hwChannel)
478     {
479         channelMaskLo = (uint32)1UL << (uint32)hwChannel;
480         channelMaskHi = (uint32)0U;
481     }
482 #ifdef WKPU_IP_64_CH_USED
483     else
484     {
485         channelMaskLo = (uint32)0U;
486         channelMaskHi = (uint32)1UL << ((uint32)hwChannel - (uint32)32U);
487     }
488 #endif
489 
490     Wkpu_Ip_InterruptRequest(base,
491                             channelMaskLo,
492 #ifdef WKPU_IP_64_CH_USED
493                             channelMaskHi,
494 #endif
495                             FALSE);
496 
497     Wkpu_Ip_WakeupRequest(base,
498                             channelMaskLo,
499 #ifdef WKPU_IP_64_CH_USED
500                             channelMaskHi,
501 #endif
502                             FALSE);
503 #ifndef WKPU_IP_64_CH_USED
504     /* To avoid compilerwarning*/
505     (void)channelMaskHi;
506 #endif
507     SchM_Exit_Icu_ICU_EXCLUSIVE_AREA_58();
508 }
509 
510 /**
511 * @brief      Icu driver function that initializes Wkpu.
512 * @details    This function:
513 *              - Sets Interrupt Filter Enable Register
514 *              - Sets Wakeup/Interrupt Pull-up Enable Register
515 *              - Sets Activation Condition
516 *
517 * @param[in]  instance   - Instance number
518 * @param[in]  userConfig - Pointer to channel configuration structure
519 *
520 * @implements Wkpu_Ip_Init_Activity
521 */
Wkpu_Ip_Init(uint8 instance,const Wkpu_Ip_IrqConfigType * userConfig)522 Wkpu_Ip_StatusType Wkpu_Ip_Init (uint8 instance, const Wkpu_Ip_IrqConfigType* userConfig)
523 {
524     WKPU_Type * base;
525     uint8 hwChannel, index;
526     uint32 channelMaskLo = (uint32)0U;
527     uint32 channelMaskHi = (uint32)0U;
528 
529 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
530     DevAssert(instance < WKPU_INSTANCE_COUNT);
531     DevAssert(userConfig != NULL_PTR);
532 #endif
533     base = Wkpu_Ip_pBase[instance];
534 
535 #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
536     Call_Wkpu_SetUserAccessAllowed((uint32)Wkpu_Ip_pBase[instance]);
537 #endif
538 
539     for(index=0; index < userConfig->numChannels; index++)
540     {
541         /* Configure for Interrupt */
542         hwChannel = (*userConfig->pChannelsConfig)[index].hwChannel;
543         /* Save in state structure the callback information */
544         Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].callback = (*userConfig->pChannelsConfig)[index].callback;
545         Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].callbackParam = (*userConfig->pChannelsConfig)[index].callbackParam;
546         Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].WkpuChannelNotification = (*userConfig->pChannelsConfig)[index].WkpuChannelNotification;
547 
548         if(32U > hwChannel)
549         {
550             channelMaskLo = (uint32)1UL << (uint32)hwChannel;
551             channelMaskHi = (uint32)0U;
552         }
553 #ifdef WKPU_IP_64_CH_USED
554         else
555         {
556             channelMaskLo = (uint32)0U;
557             channelMaskHi = (uint32)1UL << ((uint32)hwChannel - (uint32)32U);
558         }
559 #endif
560 
561 #ifdef WKPU_IP_STANDBY_WAKEUP_SUPPORT
562 #if (WKPU_IP_STANDBY_WAKEUP_SUPPORT == STD_OFF)
563         /* Disable interrupt request */
564         Wkpu_Ip_DisableInterrupt(instance, hwChannel);
565 #endif
566 #endif
567 
568         /* Set Wakeup/Interrupt Filter Enable Register */
569         Wkpu_Ip_Filter(base,
570                         channelMaskLo,
571 #ifdef WKPU_IP_64_CH_USED
572                         channelMaskHi,
573 #endif
574                         (*userConfig->pChannelsConfig)[index].filterEn);
575 
576 #ifdef WKPU_IP_SUPPORT_PULLUP
577         /* Set Wakeup/Interrupt Pull-up Enable Register */
578         Wkpu_Ip_PullUp(base,
579                         channelMaskLo,
580 #ifdef WKPU_IP_64_CH_USED
581                         channelMaskHi,
582 #endif
583                         (*userConfig->pChannelsConfig)[index].pullEn);
584 #endif
585 
586         /* Set edge events enable registers */
587         Wkpu_Ip_SetActivationCondition(instance, hwChannel, (*userConfig->pChannelsConfig)[index].edgeEvent);
588         if(WKPU_IP_NUM_OF_CHANNELS > hwChannel)
589         {
590             Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].chInit = TRUE;
591         }
592     }
593     #ifndef WKPU_IP_64_CH_USED
594         /* To avoid compilerwarning*/
595         (void)channelMaskHi;
596     #endif
597     return WKPU_IP_SUCCESS;
598 }
599 
600 /**
601 * @brief      Icu driver function that resets Wkpu configuration.
602 * @details    This function:
603 *              - Disables IRQ Interrupt
604 *              - Clears Wakeup/Interrupt Filter Enable Register
605 *              - Clears Wakeup/Interrupt Pull-up Enable Register
606 *              - Clears edge event enable registers
607 *              - Clear Interrupt Filter Enable Register
608 *
609 * @param[in]  instance - Instance number
610 * @implements Wkpu_Ip_DeInit_Activity
611 */
Wkpu_Ip_DeInit(uint8 instance)612 Wkpu_Ip_StatusType Wkpu_Ip_DeInit(uint8 instance)
613 {
614     uint32 u32ChannelMask;
615     uint8 index;
616 
617 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
618     DevAssert(instance < WKPU_INSTANCE_COUNT);
619 #endif
620     for(index=0U; index < WKPU_IP_NUM_OF_CHANNELS; index++)
621     {
622 #ifdef WKPU_IP_64_CH_USED
623         if((uint8)32U > index)
624 #endif
625         {
626             u32ChannelMask = (uint32)1U << (uint32)(index);
627             /* Disable IRQ Interrupt */
628             Wkpu_Ip_DisableInterrupt(instance, index);
629             /* Clear Wakeup/Interrupt Filter Enable Register  */
630             Wkpu_Ip_pBase[instance]->WIFER &= ~u32ChannelMask;
631             /* Clear edge event enable registers */
632             Wkpu_Ip_pBase[instance]->WIREER &= ~u32ChannelMask;
633             Wkpu_Ip_pBase[instance]->WIFEER &= ~u32ChannelMask;
634             /* Clear Interrupt Filter Enable Register */
635             Wkpu_Ip_pBase[instance]->WIFER &= ~u32ChannelMask;
636         }
637 #ifdef WKPU_IP_64_CH_USED
638         if((uint8)32U <= index)
639         {
640             u32ChannelMask = (uint32)1U << ((uint32)index - (uint32)32U);
641             /* Disable IRQ Interrupt */
642             Wkpu_Ip_DisableInterrupt(instance, index);
643             /* Clear Wakeup/Interrupt Filter Enable Register  */
644             Wkpu_Ip_pBase[instance]->WIFER_64 &= ~u32ChannelMask;
645             /* Clear edge event enable registers */
646             Wkpu_Ip_pBase[instance]->WIREER_64 &= ~u32ChannelMask;
647             Wkpu_Ip_pBase[instance]->WIFEER_64 &= ~u32ChannelMask;
648             /* Clear Interrupt Filter Enable Register */
649             Wkpu_Ip_pBase[instance]->WIFER_64 &= ~u32ChannelMask;
650         }
651 #endif
652     }
653     return WKPU_IP_SUCCESS;
654 }
655 
656 #if (WKPU_IP_SET_MODE_API == STD_ON)
657 /**
658 * @brief      Icu driver function that sets WKPU channel to SLEEP mode
659 * @details    This function enables the interrupt for WKUP channel if wakeup is enabled for the channel
660 *
661 * @param[in]  HwChannel       - IRQ HW Channel
662 */
Wkpu_Ip_SetSleepMode(uint8 instance,uint8 hwChannel)663 void Wkpu_Ip_SetSleepMode(uint8 instance, uint8 hwChannel)
664 {
665     /* Disable IRQ Interrupt */
666     Wkpu_Ip_DisableInterrupt(instance, hwChannel);
667 }
668 
669 /**
670 * @brief      Icu driver function that sets WKPU channel to NORMAL mpde
671 * @details    This function enables the interrupt for WKUP channel if Notifcation is enabled
672 *             for the channel
673 *
674 * @param[in]  HwChannel IRQ HW Channel
675 *
676 */
Wkpu_Ip_SetNormalMode(uint8 instance,uint8 hwChannel)677 void Wkpu_Ip_SetNormalMode(uint8 instance, uint8 hwChannel)
678 {
679     /* Enable IRQ Interrupt */
680     Wkpu_Ip_EnableInterrupt(instance, hwChannel);
681 }
682 #endif  /* WKPU_IP_SET_MODE_API */
683 
684 /**
685 * @brief      Icu driver function that sets activation condition of WKUP channel
686 * @details    This function enables the requested activation condition(rising, falling or both edges)
687 *             for corresponding WKPU channels.
688 *
689 * @param[in]  instance - Instance number used
690 * @param[in]  hwChannel - Wake up hardware channel
691 * @param[in]  edge - edge type for activation
692 * @implements Wkpu_Ip_SetActivationCondition_Activity
693 */
Wkpu_Ip_SetActivationCondition(uint8 instance,uint8 hwChannel,Wkpu_Ip_EdgeType edge)694 void Wkpu_Ip_SetActivationCondition (uint8 instance, uint8 hwChannel, Wkpu_Ip_EdgeType edge)
695 {
696     WKPU_Type * base;
697     uint32 channelMaskLo = (uint32)0U;
698     uint32 channelMaskHi = (uint32)0U;
699 
700 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
701     DevAssert(instance < WKPU_INSTANCE_COUNT);
702     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
703 #endif
704 
705     base = Wkpu_Ip_pBase[instance];
706     if(32U > hwChannel)
707     {
708         channelMaskLo = (uint32)1UL << (uint32)hwChannel;
709         channelMaskHi = (uint32)0U;
710     }
711 #ifdef WKPU_IP_64_CH_USED
712     else
713     {
714         channelMaskLo = (uint32)0U;
715         channelMaskHi = (uint32)1UL << ((uint32)hwChannel - (uint32)32U);
716     }
717 #endif
718 
719     switch (edge)
720     {
721         case WKPU_IP_RISING_EDGE:
722             Wkpu_Ip_EnableRisingEdge(base,
723                             channelMaskLo,
724 #ifdef WKPU_IP_64_CH_USED
725                             channelMaskHi,
726 #endif
727                             TRUE);
728             Wkpu_Ip_EnableFallingEdge(base,
729                             channelMaskLo,
730 #ifdef WKPU_IP_64_CH_USED
731                             channelMaskHi,
732 #endif
733                             FALSE);
734             break;
735         case WKPU_IP_FALLING_EDGE:
736             Wkpu_Ip_EnableRisingEdge(base,
737                             channelMaskLo,
738 #ifdef WKPU_IP_64_CH_USED
739                             channelMaskHi,
740 #endif
741                             FALSE);
742             Wkpu_Ip_EnableFallingEdge(base,
743                             channelMaskLo,
744 #ifdef WKPU_IP_64_CH_USED
745                             channelMaskHi,
746 #endif
747                             TRUE);
748             break;
749         case WKPU_IP_NONE_EDGE:
750             Wkpu_Ip_EnableRisingEdge(base,
751                             channelMaskLo,
752 #ifdef WKPU_IP_64_CH_USED
753                             channelMaskHi,
754 #endif
755                             FALSE);
756             Wkpu_Ip_EnableFallingEdge(base,
757                             channelMaskLo,
758 #ifdef WKPU_IP_64_CH_USED
759                             channelMaskHi,
760 #endif
761                             FALSE);
762             break;
763         case WKPU_IP_BOTH_EDGES:
764         default:
765             /* fall-through */
766             Wkpu_Ip_EnableRisingEdge(base,
767                             channelMaskLo,
768 #ifdef WKPU_IP_64_CH_USED
769                             channelMaskHi,
770 #endif
771                             TRUE);
772             Wkpu_Ip_EnableFallingEdge(base,
773                             channelMaskLo,
774 #ifdef WKPU_IP_64_CH_USED
775                             channelMaskHi,
776 #endif
777                             TRUE);
778             break;
779     }
780 #ifndef WKPU_IP_64_CH_USED
781     /* To avoid compilerwarning*/
782     (void)channelMaskHi;
783 #endif
784 }
785 
786 /**
787 * @brief      Icu driver function that gets the input state of WKPU channel.
788 * @details    This function:
789 *             - Checks if interrupt flags for corresponding WKPU channel is set then
790 *             it clears the interrupt flag and returns the value as TRUE.
791 *
792 * @param[in]  instance - Instance number used
793 * @param[in]  hwChannel - HW Channel used
794 *
795 * @return     boolean
796 *              - TRUE - if channel is active
797 *              - FALSE - If channel is in idle
798 */
799 /** @implements Wkpu_Ip_GetInputState_Activity */
Wkpu_Ip_GetInputState(uint8 instance,uint8 hwChannel)800 boolean Wkpu_Ip_GetInputState(uint8 instance, uint8 hwChannel)
801 {
802     boolean bstate = FALSE;
803     uint32 u32regWkpuWISR = 0;
804     uint32 u32regWkpuIRER = 0;
805     uint32 channelMask;
806 
807     WKPU_Type * base;
808 
809 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
810     DevAssert(instance < WKPU_INSTANCE_COUNT);
811     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
812 #endif
813 
814     base = Wkpu_Ip_pBase[instance];
815 
816     if((uint8)32U > hwChannel)
817     {
818         channelMask = 1UL << hwChannel;
819         u32regWkpuWISR = base->WISR & channelMask;
820         u32regWkpuIRER = base->WRER & channelMask;
821         if ((0x0U != u32regWkpuWISR) && (0x0U == u32regWkpuIRER))
822         {
823             base->WISR = u32regWkpuWISR;
824             bstate = TRUE;
825         }
826     }
827 #ifdef WKPU_IP_64_CH_USED
828     else
829     {
830         channelMask = 1UL << (hwChannel - 32U);
831         u32regWkpuWISR = base->WISR_64 & channelMask;
832         u32regWkpuIRER = base->WRER_64 & channelMask;
833         if ((0x0U != u32regWkpuWISR) && (0x0U == u32regWkpuIRER))
834         {
835             base->WISR_64 = u32regWkpuWISR;
836             bstate = TRUE;
837         }
838     }
839 #endif
840     return bstate;
841 }
842 
843 /**
844  * @brief      Driver function Enable Notification for timestamp.
845  */
846 
847 /** @implements Wkpu_Ip_EnableNotification_Activity */
Wkpu_Ip_EnableNotification(uint8 hwChannel)848 void Wkpu_Ip_EnableNotification(uint8 hwChannel)
849 {
850 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
851     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
852 #endif
853 
854     Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].notificationEnable = TRUE;
855 }
856 
857 /**
858  * @brief      Driver function Disable Notification for timestamp.
859  */
860 /** @implements Wkpu_Ip_DisableNotification_Activity */
Wkpu_Ip_DisableNotification(uint8 hwChannel)861 void Wkpu_Ip_DisableNotification(uint8 hwChannel)
862 {
863 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
864     DevAssert(hwChannel < WKPU_IP_NUM_OF_CHANNELS);
865 #endif
866 
867     Wkpu_Ip_u32ChState[Wkpu_Ip_IndexInChState[hwChannel]].notificationEnable = FALSE;
868 }
869 
870 #endif /* WKPU_DERIVATIVE_SUPPORT_ONLY_NMI */
871 
872 
873 #if (defined (WKPU_IP_NMI_API) && (STD_ON == WKPU_IP_NMI_API))
874 /*!
875  * @brief Gets NMI/Reset configuration lock
876  *
877  * This function gets NMI/Reset configuration lock
878  *
879  * @param[in] base The WKPU peripheral base address
880  * @param[in] position The position of source
881  * @return Status of configuration lock
882  */
Wkpu_Ip_IsNMIConfigLock(const WKPU_Type * base,uint32 position)883 static inline boolean Wkpu_Ip_IsNMIConfigLock(const WKPU_Type * base,
884                                         uint32 position)
885 {
886     return (base->NCR & (WKPU_NCR_NLOCK0_MASK >> position)) != 0U;
887 }
888 
889 /**
890 * @brief      Icu driver function that initializes NMI part of Wkpu.
891 * @details    This function:
892 *              - Configure destination
893 *              - Configure wakeup request
894                - Configure glitch filter if used
895 *              - Sets Activation Condition
896 *
897 * @param[in]  instance   - Instance number
898 * @param[in]  userConfig - Pointer to channel configuration structure
899 */
900 /** @implements Wkpu_Ip_InitNMI_Activity */
Wkpu_Ip_InitNMI(uint8 instance,const Wkpu_Ip_IrqConfigType * userConfig)901 Wkpu_Ip_StatusType Wkpu_Ip_InitNMI(uint8 instance, const Wkpu_Ip_IrqConfigType* userConfig)
902 {
903 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
904     DevAssert(instance < WKPU_INSTANCE_COUNT);
905     DevAssert(userConfig->pNMIChannelsConfig != NULL_PTR);
906 #endif
907 
908     uint8 coreNumber;
909     uint8 i;
910     WKPU_Type * base = Wkpu_Ip_pBase[instance];
911     Wkpu_Ip_StatusType retVal = WKPU_IP_SUCCESS;
912     uint32 coreShift = 0U;
913     const Wkpu_Ip_NmiCfgType * userConfigNMI = (*userConfig->pNMIChannelsConfig);
914 
915 #if (defined (WKPU_IP_ENABLE_USER_MODE_SUPPORT))
916     Call_Wkpu_SetUserAccessAllowed((uint32)Wkpu_Ip_pBase[instance]);
917 #endif
918 
919     retVal = Wkpu_Ip_DeinitNMI(instance);
920 
921     if(WKPU_IP_SUCCESS == retVal)
922     {
923         for (i = 0U; ((i < WKPU_IP_NMI_CORE_CNT) && (i < (userConfig->numNMIChannels))); i++)
924         {
925             coreNumber = (uint8)userConfigNMI[i].core;
926             coreShift = coreNumber * (uint32)WKPU_IP_CORE_OFFSET_SIZE;
927 
928             /* Configure destination source */
929             base->NCR &= ~(WKPU_NCR_NDSS0_MASK >> coreShift);
930             base->NCR |= WKPU_NCR_NDSS0((uint8)userConfigNMI[i].destination) >> coreShift;
931 
932             /* Configure wake-up request */
933             base->NCR &= ~(WKPU_NCR_NWRE0_MASK >> coreShift);
934             base->NCR |= WKPU_NCR_NWRE0(userConfigNMI[i].wkpReqEn ? 1UL : 0UL) >> coreShift;
935 
936         #ifndef WKPU_IP_SUPPORT_FILTER_ON_EACH_CORE
937             /* Only set filter for Core 0 */
938             if ( 0UL == coreNumber)
939         #endif /* #ifndef WKPU_IP_SUPPORT_FILTER_ON_EACH_CORE */
940             {
941                 /* Configure glitch filter */
942                 base->NCR &= ~(WKPU_NCR_NFE0_MASK >> coreShift);
943                 base->NCR |= WKPU_NCR_NFE0(userConfigNMI[i].filterEn ? 1UL : 0UL) >> coreShift;
944             }
945 
946             /* Configure edge events */
947             if (WKPU_IP_RISING_EDGE == userConfigNMI[i].edgeEvent)
948             {
949 #ifdef ERR_IPV_WKPU_E050394
950                 base->NCR |= WKPU_NCR_NFEE0_MASK >> coreShift;
951 #endif
952                 base->NCR |= WKPU_NCR_NREE0_MASK >> coreShift;
953                 base->NCR &= ~(WKPU_NCR_NFEE0_MASK >> coreShift);
954             }
955             else if (WKPU_IP_FALLING_EDGE == userConfigNMI[i].edgeEvent)
956             {
957                 base->NCR &= ~(WKPU_NCR_NREE0_MASK >> coreShift);
958                 base->NCR |= WKPU_NCR_NFEE0_MASK >> coreShift;
959             }
960             else
961             {
962                 base->NCR |= WKPU_NCR_NFEE0_MASK >> coreShift;
963                 base->NCR |= WKPU_NCR_NREE0_MASK >> coreShift;
964             }
965             /* Configure lock */
966             base->NCR |= WKPU_NCR_NLOCK0(userConfigNMI[i].lockEn ? 1UL : 0UL) >> coreShift;
967         }
968     }
969     return retVal;
970 }
971 
972 /** @implements Wkpu_Ip_DeinitNMI_Activity */
Wkpu_Ip_DeinitNMI(uint8 instance)973 Wkpu_Ip_StatusType Wkpu_Ip_DeinitNMI(uint8 instance)
974 {
975 #if(WKPU_IP_DEV_ERROR_DETECT == STD_ON)
976     DevAssert(instance < WKPU_INSTANCE_COUNT);
977 #endif
978     uint8 i;
979     uint32 coreShift = 0U;
980     Wkpu_Ip_StatusType retVal = WKPU_IP_SUCCESS;
981     static const Wkpu_Ip_CoreType coreNumber[WKPU_IP_NMI_NUM_CORES] = WKPU_IP_CORE_ARRAY;
982 
983     WKPU_Type * base = Wkpu_Ip_pBase[instance];
984 
985     for (i = 0U; i < WKPU_IP_NMI_NUM_CORES; i++)
986     {
987         coreShift = (uint32)coreNumber[i] * WKPU_IP_CORE_OFFSET_SIZE;
988 
989         if (Wkpu_Ip_IsNMIConfigLock(base, coreShift) == FALSE)
990         {
991             /* Clear status flag and overrun status flag */
992             base->NSR = ((WKPU_NSR_NIF0_MASK | WKPU_NSR_NOVF0_MASK) >> coreShift);
993             /* Clear edge events */
994             base->NCR &= ~(WKPU_NCR_NREE0_MASK >> coreShift);
995             base->NCR &= ~(WKPU_NCR_NFEE0_MASK >> coreShift);
996 
997         #ifndef WKPU_IP_SUPPORT_FILTER_ON_EACH_CORE
998             /* Only set filter for Core 0 */
999             if (WKPU_CORE0 == coreNumber[i])
1000         #endif /* #ifndef WKPU_IP_SUPPORT_FILTER_ON_EACH_CORE */
1001             {
1002                 /* Disable glitch filter */
1003                 base->NCR &= ~(WKPU_NCR_NFE0_MASK >> coreShift);
1004             }
1005 
1006             /* Disable wake-up request */
1007             base->NCR &= ~(WKPU_NCR_NWRE0_MASK >> coreShift);
1008 
1009         #ifdef WKPU_IP_SUPPORT_NONE_REQUEST
1010             /* Configure destination source */
1011             base->NCR &= ~(WKPU_NCR_NDSS0_MASK >> coreShift);
1012             base->NCR |= WKPU_NCR_NDSS0((uint8)WKPU_IP_NMI_NONE) >> coreShift;
1013         #endif /* #ifndef WKPU_IP_SUPPORT_NONE_REQUEST */
1014         }
1015         else
1016         {
1017             retVal = WKPU_IP_ERROR;
1018             break;
1019         }
1020     }
1021     return retVal;
1022 }
1023 #endif
1024 
1025 #define ICU_STOP_SEC_CODE
1026 #include "Icu_MemMap.h"
1027 
1028 #endif /* WKPU_IP_USED */
1029 
1030 #ifdef __cplusplus
1031 }
1032 #endif
1033 
1034 /** @} */
1035 
1036