1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /**
8 *   @file
9 *
10 *   @addtogroup Swt
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 "Swt_Ip.h"
26 #include "Devassert.h"
27 #include "SchM_Wdg.h"
28 
29 #include "OsIf.h"
30 #include "Swt_Ip_Irq.h"
31 /*==================================================================================================
32 *                                 SOURCE FILE VERSION INFORMATION
33 ==================================================================================================*/
34 #define SWT_IP_VENDOR_ID_C                      43
35 #define SWT_IP_AR_RELEASE_MAJOR_VERSION_C       4
36 #define SWT_IP_AR_RELEASE_MINOR_VERSION_C       7
37 #define SWT_IP_AR_RELEASE_REVISION_VERSION_C    0
38 #define SWT_IP_SW_MAJOR_VERSION_C               0
39 #define SWT_IP_SW_MINOR_VERSION_C               9
40 #define SWT_IP_SW_PATCH_VERSION_C               0
41 
42 /*==================================================================================================
43 *                                       FILE VERSION CHECKS
44 ==================================================================================================*/
45 
46 /* Check if current file and Swt_Ip header file are of the same vendor */
47 #if (SWT_IP_VENDOR_ID_C != SWT_IP_VENDOR_ID)
48 #error "Swt_Ip.c and Swt_Ip.h have different vendor ids"
49 #endif
50 
51 /* Check if current file and Swt_Ip header file are of the same Autosar version */
52 #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C     != SWT_IP_AR_RELEASE_MAJOR_VERSION) || \
53      (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != SWT_IP_AR_RELEASE_MINOR_VERSION) || \
54      (SWT_IP_AR_RELEASE_REVISION_VERSION_C  != SWT_IP_AR_RELEASE_REVISION_VERSION))
55 #error "AutoSar Version Numbers of Swt_Ip.c and Swt_Ip.h are different"
56 #endif
57 
58 /* Check if current file and Swt_Ip header file are of the same software version */
59 #if ((SWT_IP_SW_MAJOR_VERSION_C != SWT_IP_SW_MAJOR_VERSION) || \
60      (SWT_IP_SW_MINOR_VERSION_C != SWT_IP_SW_MINOR_VERSION) || \
61      (SWT_IP_SW_PATCH_VERSION_C != SWT_IP_SW_PATCH_VERSION))
62 #error "Software Version Numbers of Swt_Ip.c and Swt_Ip.h are different"
63 #endif
64 
65 /* Check if current file and Swt_Ip_Irq header file are of the same vendor */
66 #if (SWT_IP_VENDOR_ID_C != SWT_IP_IRQ_VENDOR_ID)
67 #error "Swt_Ip.c and Swt_Ip_Irq.h have different vendor ids"
68 #endif
69 
70 /* Check if current file and Swt_Ip_Irq header file are of the same Autosar version */
71 #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C     != SWT_IP_IRQ_AR_RELEASE_MAJOR_VERSION) || \
72      (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != SWT_IP_IRQ_AR_RELEASE_MINOR_VERSION) || \
73      (SWT_IP_AR_RELEASE_REVISION_VERSION_C  != SWT_IP_IRQ_AR_RELEASE_REVISION_VERSION))
74 #error "AutoSar Version Numbers of Swt_Ip.c and Swt_Ip_Irq.h are different"
75 #endif
76 
77 /* Check if current file and Swt_Ip_Irq header file are of the same software version */
78 #if ((SWT_IP_SW_MAJOR_VERSION_C != SWT_IP_IRQ_SW_MAJOR_VERSION) || \
79      (SWT_IP_SW_MINOR_VERSION_C != SWT_IP_IRQ_SW_MINOR_VERSION) || \
80      (SWT_IP_SW_PATCH_VERSION_C != SWT_IP_IRQ_SW_PATCH_VERSION))
81 #error "Software Version Numbers of Swt_Ip.c and Swt_Ip_Irq.h are different"
82 #endif
83 
84 #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
85     /* Check if current file and Devassert header file are of the same Autosar version */
86     #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C    != DEVASSERT_AR_RELEASE_MAJOR_VERSION) || \
87         (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != DEVASSERT_AR_RELEASE_MINOR_VERSION))
88     #error "AutoSar Version Numbers of Swt_Ip.c and Devassert.h are different"
89     #endif
90 
91     /* Check if current file and StandardTypes header file are of the same Autosar version */
92     #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C    != STD_AR_RELEASE_MAJOR_VERSION) || \
93         (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != STD_AR_RELEASE_MINOR_VERSION))
94     #error "AutoSar Version Numbers of Swt_Ip.c and StandardTypes.h are different"
95     #endif
96 
97     /* Check if current file and StandardTypes header file are of the same Autosar version */
98     #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C    != SCHM_WDG_AR_RELEASE_MAJOR_VERSION) || \
99         (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != SCHM_WDG_AR_RELEASE_MINOR_VERSION))
100     #error "AutoSar Version Numbers of Swt_Ip.c and SchM_Wdg.h are different"
101     #endif
102 #endif /* DISABLE_MCAL_INTERMODULE_ASR_CHECK */
103 
104 /* Check if current file and Osif header file are of the same Autosar version */
105 #if ((SWT_IP_AR_RELEASE_MAJOR_VERSION_C    != OSIF_AR_RELEASE_MAJOR_VERSION) || \
106     (SWT_IP_AR_RELEASE_MINOR_VERSION_C     != OSIF_AR_RELEASE_MINOR_VERSION))
107 #error "AutoSar Version Numbers of Swt_Ip.c and Osif.h are different"
108 #endif
109 
110 /*==================================================================================================
111 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
112 ==================================================================================================*/
113 
114 /*==================================================================================================
115 *                                          LOCAL MACROS
116 ==================================================================================================*/
117 #define SWT_IP_MAP(x)  (((uint32)(((uint32)(x)) << SWT_IP_MAP_SHIFT)) & SWT_IP_MAP_MASK_U32)
118 
119 /*==================================================================================================
120 *                                         LOCAL CONSTANTS
121 ==================================================================================================*/
122 
123 /*==================================================================================================
124 *                                         LOCAL VARIABLES
125 ==================================================================================================*/
126 
127 #define WDG_START_SEC_CONST_UNSPECIFIED
128 
129 #include "Wdg_MemMap.h"
130 
131 #if SWT_IP_DISCOUNTINUOUS_INSTANCE_IDS == STD_OFF
132 static SWT_Type * const Swt_Ip_sBase[SWT_INSTANCE_COUNT] = IP_SWT_BASE_PTRS;
133 #else
134 static SWT_Type * const Swt_Ip_sBase[SWT_INSTANCE_COUNT] = {IP_SWT_0, IP_SWT_0, IP_SWT_2, IP_SWT_3};
135 #endif
136 
137 #define WDG_STOP_SEC_CONST_UNSPECIFIED
138 
139 #include "Wdg_MemMap.h"
140 
141 #define WDG_START_SEC_VAR_CLEARED_UNSPECIFIED
142 
143 #include "Wdg_MemMap.h"
144 
145 static Swt_CallbackPtrType Swt_Ip_apCallbackPtr[SWT_INSTANCE_COUNT];
146 
147 static boolean Wdg_Ip_abStatus[SWT_INSTANCE_COUNT];
148 
149 #define WDG_STOP_SEC_VAR_CLEARED_UNSPECIFIED
150 
151 #include "Wdg_MemMap.h"
152 
153 /*==================================================================================================
154 *                                    LOCAL FUNCTION PROTOTYPES
155 ==================================================================================================*/
156 
157 /*==================================================================================================
158 *                                         LOCAL FUNCTIONS
159 ==================================================================================================*/
160 
161 #ifdef WDG_ROM
162     #if (WDG_ROM == 1U)
163         #define WDG_START_SEC_CODE
164     #endif
165 #else
166     #if (WDG_RAM == 0U)
167         #define WDG_START_SEC_RAMCODE
168     #endif
169 #endif
170 
171 #include "Wdg_MemMap.h"
172 
Swt_Ip_IsEnable(const SWT_Type * const Base)173 static inline boolean Swt_Ip_IsEnable(const SWT_Type * const Base)
174 {
175     /* Checks whether the SWT is enabled */
176     return ((Base->CR & SWT_CR_WEN_MASK) != 0U);
177 }
178 
Swt_Ip_Enable(SWT_Type * const Base)179 static inline void Swt_Ip_Enable(SWT_Type * const Base)
180 {
181     /* Enables the SWT */
182     Base->CR |= SWT_CR_WEN_MASK;
183 }
184 
Swt_Ip_Disable(SWT_Type * const Base)185 static inline void Swt_Ip_Disable(SWT_Type * const Base)
186 {
187     /* Enables the SWT */
188     Base->CR &= ~SWT_CR_WEN_MASK;
189 }
190 
Swt_Ip_GetServiceMode(const SWT_Type * const Base)191 static  Swt_Ip_ServiceModeType Swt_Ip_GetServiceMode(const SWT_Type * const Base)
192 {
193     Swt_Ip_ServiceModeType Mode;
194 
195     /* Gets current service mode */
196     switch ((Base->CR & SWT_CR_SMD_MASK) >> SWT_CR_SMD_SHIFT)
197     {
198         /* Keyed Service Sequence */
199         case 1U:
200             Mode = SWT_IP_KS_SEQ_MODE;
201             break;
202         /* Fixed Service Sequence */
203         default:
204             Mode = SWT_IP_FS_SEQ_MODE;
205             break;
206     }
207 
208     return Mode;
209 }
210 
Swt_Ip_GetLock(const SWT_Type * const Base)211 static Swt_Ip_LockType Swt_Ip_GetLock(const SWT_Type * const Base)
212 {
213     Swt_Ip_LockType LockMode = SWT_IP_UNLOCK;
214 
215     /* Get lock mode */
216     switch ((Base->CR & (SWT_CR_SLK_MASK | SWT_CR_HLK_MASK)) >> SWT_CR_SLK_SHIFT)
217     {
218         case 0U:
219             LockMode = SWT_IP_UNLOCK;
220             break;
221         case 1U:
222             LockMode = SWT_IP_SOFTLOCK;
223             break;
224         case 2U:
225             LockMode = SWT_IP_HARDLOCK;
226             break;
227         default:
228             LockMode = SWT_IP_HARDLOCK;
229             break;
230     }
231 
232     return LockMode;
233 }
234 
Swt_Ip_Lock(SWT_Type * const Base,Swt_Ip_LockType LockConfig)235 static void Swt_Ip_Lock(SWT_Type * const Base,
236                  Swt_Ip_LockType LockConfig)
237 {
238     /* Configures lock bits */
239     switch (LockConfig)
240     {
241         /* Hard lock */
242         case SWT_IP_HARDLOCK:
243             Base->CR |= SWT_CR_HLK(1U);
244             break;
245         /* Soft lock */
246         case SWT_IP_SOFTLOCK:
247             Base->CR |= SWT_CR_SLK(1U);
248             break;
249         /* Unlock */
250         default:
251             /* Do nothing */
252             break;
253     }
254 }
255 
Swt_Ip_Unlock(SWT_Type * const Base)256 static Swt_Ip_StatusType Swt_Ip_Unlock(SWT_Type * const Base)
257 {
258     Swt_Ip_StatusType Ret = SWT_IP_STATUS_SUCCESS;
259 
260     uint32 TimeoutTicks = OsIf_MicrosToTicks(SWT_IP_OSIF_TIMEOUT_VAL, SWT_IP_TIMEOUT_METHOD);
261     uint32 CurrentTicks = 0u; /* initialize current counter */
262     uint32 ElapsedTicks = 0u; /* elapsed will give timeout */
263 
264     /* Hard lock */
265     if ((Base->CR & SWT_CR_HLK_MASK) != 0U)
266     {
267         Ret = SWT_IP_STATUS_ERROR;
268     }
269     else
270     {
271         /* Soft lock */
272         if ((Base->CR & SWT_CR_SLK_MASK) != 0U)
273         {
274             /* Unlocks sequence */
275             Base->SR = SWT_SR_WSC(SWT_IP_UNLOCK_VALUE1_U16);
276             Base->SR = SWT_SR_WSC(SWT_IP_UNLOCK_VALUE2_U16);
277 
278             CurrentTicks = OsIf_GetCounter(SWT_IP_TIMEOUT_METHOD);
279 
280             /* Waits unlock complete */
281             while (((Base->CR & SWT_CR_SLK_MASK) != 0U) && (ElapsedTicks < TimeoutTicks))
282             {
283                 ElapsedTicks += OsIf_GetElapsed(&CurrentTicks, SWT_IP_TIMEOUT_METHOD);
284             }
285 
286             if (ElapsedTicks >= TimeoutTicks)
287             {
288                 /* Soft lock is enabled again to avoid unlock sequence successfully after osif timeout expired */
289                 Swt_Ip_Lock(Base, SWT_IP_SOFTLOCK);
290                 Ret = SWT_IP_STATUS_ERROR;
291             }
292         }
293         /* Unlock */
294         else
295         {
296             /* Do nothing */
297         }
298     }
299 
300     return Ret;
301 }
302 
303 
Swt_Ip_RegisterConfig(SWT_Type * const Base,const Swt_Ip_ConfigType * const ConfigPtr)304 static void Swt_Ip_RegisterConfig(SWT_Type * const Base,
305                                      const Swt_Ip_ConfigType * const ConfigPtr)
306 {
307     /* Temporary storage for control register value */
308     uint32 TempCr = 0x00UL;
309     Swt_Ip_ServiceModeType ServiceMode = ConfigPtr->eServiceMode;
310 
311     /* Sets control configuration */
312     TempCr = (SWT_IP_MAP(ConfigPtr->u8MapEnBitmask)
313         | SWT_CR_SMD(ServiceMode)
314         | SWT_CR_RIA(ConfigPtr->bEnResetOnInvalidAccess ? 1UL : 0UL)
315         | SWT_CR_WND(ConfigPtr->bEnWindow ? 1UL : 0UL)
316         | SWT_CR_ITR(ConfigPtr->bEnInterrupt ? 1UL : 0UL)
317 #if (defined(SWT_IP_HAS_STOP_MODE) && (SWT_IP_HAS_STOP_MODE == 1U))
318         | SWT_CR_STP(ConfigPtr->bEnRunInStopMode? 0UL : 1UL)
319 #endif
320         | SWT_CR_FRZ(ConfigPtr->bEnRunInDebugMode ? 0UL : 1UL));
321 
322     /* Write configuration to the SWT CR register*/
323     Base->CR = TempCr;
324 
325     /* Clears interrupt flags */
326 
327     Base->IR = SWT_IR_TIF_MASK;
328 
329     /* Sets timeout value */
330     Base->TO = ConfigPtr->u32TimeoutValue;
331 
332     /* Sets window value */
333     if (ConfigPtr->bEnWindow)
334     {
335         Base->WN = ConfigPtr->u32WindowValue;
336     }
337     /* Sets initial service key value */
338     if (SWT_IP_KS_SEQ_MODE == ServiceMode)
339     {
340         Base->SK = SWT_SK_SK(ConfigPtr->u16InitialKey);
341     }
342 }
343 
Swt_Ip_CheckTimeout(uint32 u32TimeoutValue,uint32 u32WindowValue)344 static Swt_Ip_StatusType Swt_Ip_CheckTimeout(uint32 u32TimeoutValue, uint32 u32WindowValue)
345 {
346     /* Check timeout period is valid */
347     Swt_Ip_StatusType Ret = SWT_IP_STATUS_SUCCESS;
348 
349     if (u32TimeoutValue < SWT_MIN_VALUE_TIMEOUT_U32)
350     {
351         Ret = SWT_IP_STATUS_ERROR;
352     }
353 
354     if ((u32WindowValue != 0U) && (u32TimeoutValue <= u32WindowValue))
355     {
356         Ret = SWT_IP_STATUS_ERROR;
357     }
358 
359     return Ret;
360 }
361 
Swt_Ip_ServiceCmd(SWT_Type * const Base,uint16 ServiceKey)362 static inline void Swt_Ip_ServiceCmd(SWT_Type * const Base,
363                                         uint16 ServiceKey)
364 {
365     /* Services SWT instance */
366     Base->SR = (Base->SR & ~SWT_SR_WSC_MASK) | SWT_SR_WSC(ServiceKey);
367 }
368 
Swt_Ip_ServiceKeyGen(const SWT_Type * const Base)369 static inline uint16 Swt_Ip_ServiceKeyGen(const SWT_Type * const Base)
370 {
371     /* Generates the next key used to service the SWT */
372     return ((uint16)((((Base->SK & SWT_SK_SK_MASK) >> SWT_SK_SK_SHIFT) * 17U) + 3U));
373 }
374 
375 #if (SWT_IP_DEINIT == STD_ON)
376 #if (SWT_IP_CLEAR_RESET_REQUEST == STD_ON)
Swt_Ip_RequestedReset(const SWT_Type * const Base)377 static inline boolean Swt_Ip_RequestedReset(const SWT_Type * const Base)
378 {
379     /* Get reset request flag */
380     return (((Base->RRR & SWT_RRR_RRF_MASK)>> SWT_RRR_RRF_SHIFT) == 1U);
381 }
382 #endif
383 #endif
384 
385 #if (SWT_IP_HAS_CLEAR_RESET == 1)
386 #if (SWT_IP_DEINIT == STD_ON)
Swt_Ip_ClearRequest(SWT_Type * const Base)387 static inline void Swt_Ip_ClearRequest(SWT_Type * const Base)
388 {
389     /* Clear reset request flag */
390     Base->RRR = SWT_RRR_RRF(1U);
391 }
392 #endif
393 #endif
394 
Swt_Ip_GetIntFlag(const SWT_Type * const Base)395 static inline boolean Swt_Ip_GetIntFlag(const SWT_Type * const Base)
396 {
397     /* Get interrupt flag */
398     return ((Base->IR & SWT_IR_TIF_MASK) != 0U);
399 }
400 
Swt_Ip_IsIntEnabled(const SWT_Type * const Base)401 static inline boolean Swt_Ip_IsIntEnabled(const SWT_Type * const Base)
402 {
403     /* Interrupt then reset request */
404     return ((Base->CR & SWT_CR_ITR_MASK ) != 0U);
405 }
406 
Swt_Ip_ClearIntFlag(SWT_Type * const Base)407 static inline void Swt_Ip_ClearIntFlag(SWT_Type * const Base)
408 {
409     /* Clear interrupt flag */
410     Base->IR &= SWT_IR_TIF_MASK;
411 }
412 
413 /*==================================================================================================
414 *                                 GLOBAL FUNCTIONS PROTOTYPES
415 ==================================================================================================*/
416 
417 
418 /*==================================================================================================
419 *                                      GLOBAL FUNCTIONS
420 ==================================================================================================*/
421 
422 /**
423 * @Description    This function initializes SWT instance.
424 *
425 * @implements     Swt_Ip_Init_Activity
426 */
Swt_Ip_Init(const uint32 Instance,const Swt_Ip_ConfigType * const ConfigPtr)427 Swt_Ip_StatusType Swt_Ip_Init(const uint32 Instance,
428                             const Swt_Ip_ConfigType * const ConfigPtr)
429 {
430     SWT_Type * const Base = Swt_Ip_sBase[Instance];
431     Swt_Ip_StatusType Ret = SWT_IP_STATUS_SUCCESS;
432 
433 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
434     DevAssert(Instance < SWT_INSTANCE_COUNT);
435     DevAssert(ConfigPtr != NULL_PTR);
436 #endif
437 
438     if (Swt_Ip_Unlock(Base) == SWT_IP_STATUS_ERROR)
439     {
440         Ret = SWT_IP_STATUS_ERROR;
441     }
442     else
443     {
444         boolean bSwtEnabled = Swt_Ip_IsEnable(Base);
445 
446         if (bSwtEnabled)
447         {
448             Swt_Ip_Disable(Base);
449         }
450 
451         /* Verify if the timeout and window values are within accepted range */
452         Ret = Swt_Ip_CheckTimeout(ConfigPtr->u32TimeoutValue, ConfigPtr->u32WindowValue);
453 
454         if (SWT_IP_STATUS_SUCCESS == Ret)
455         {
456             /* Configures the SWT instance */
457             Swt_Ip_RegisterConfig(Base, ConfigPtr);
458 
459 
460             if (ConfigPtr->bEnInterrupt)
461             {
462                 Swt_Ip_apCallbackPtr[Instance] = ConfigPtr->pfSwtCallback;
463             }
464 
465             /* Enable the SWT timer */
466             Swt_Ip_Enable(Base);
467 
468             /* Lock the SWT */
469             Swt_Ip_Lock(Base, ConfigPtr->lockConfig);
470 
471             /* Set driver status is initialized */
472             Wdg_Ip_abStatus[Instance] = TRUE;
473         }
474     }
475 
476     return Ret;
477 }
478 
479 #if (SWT_IP_DEINIT == STD_ON)
480 /**
481 * @Description    This function de-initializes SWT instance.
482 *
483 * @implements     Swt_Ip_Deinit_Activity
484 */
Swt_Ip_Deinit(const uint32 Instance)485     Swt_Ip_StatusType Swt_Ip_Deinit(const uint32 Instance)
486     {
487         SWT_Type * const Base = Swt_Ip_sBase[Instance];
488         Swt_Ip_StatusType Ret;
489 
490     #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
491         DevAssert(Instance < SWT_INSTANCE_COUNT);
492     #endif
493 
494         /* Unlocks SWT instance */
495         Ret = Swt_Ip_Unlock(Base);
496 
497         if (Ret == SWT_IP_STATUS_SUCCESS)
498         {
499             /* Disable SWT timer */
500             Swt_Ip_Disable(Base);
501 
502             /* Clears interrupt flag */
503             Base->IR = SWT_IR_TIF_MASK;
504             /* Resets timeout value */
505             Base->TO = SWT_IP_TO_RESET_VALUE_U32;
506             /* Resets window value */
507             Base->WN = SWT_IP_WN_RESET_VALUE_U32;
508             /* Resets service key value */
509             Base->SK = SWT_IP_SK_RESET_VALUE_U16;
510             /* Resets control register */
511             Base->CR = SWT_IP_CR_RESET_VALUE_U32 & (~SWT_CR_WEN_MASK); /* make sure the SWT is disabled after reset */
512 
513 
514             /* Clear RRF bit in RRR register */
515             #if (SWT_IP_HAS_CLEAR_RESET == 1)
516             Swt_Ip_ClearRequest(Base);
517             #endif
518 
519             /* Set driver status is uninit */
520             Wdg_Ip_abStatus[Instance] = FALSE;
521         }
522 
523         return Ret;
524     }
525 #endif
526 
527 /**
528 * @Description    This function services SWT instance.
529 *
530 * @implements     Swt_Ip_Service_Activity
531 */
Swt_Ip_Service(const uint32 Instance)532 void Swt_Ip_Service(const uint32 Instance)
533 {
534     SWT_Type * const Base = Swt_Ip_sBase[Instance];
535     boolean BUnlockSequence = FALSE;
536 
537 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
538     DevAssert(Instance < SWT_INSTANCE_COUNT);
539 #endif
540 
541     SchM_Enter_Wdg_WDG_EXCLUSIVE_AREA_09();
542 
543     Swt_Ip_ServiceModeType ServiceMode = Swt_Ip_GetServiceMode(Base);
544 
545     switch (ServiceMode)
546     {
547         /* Keyed Service Sequence Mode */
548         case SWT_IP_KS_SEQ_MODE:
549             if (Swt_Ip_ServiceKeyGen(Base) == SWT_IP_UNLOCK_VALUE1_U16)
550             {
551                 BUnlockSequence = TRUE;
552             }
553 
554             Swt_Ip_ServiceCmd(Base, Swt_Ip_ServiceKeyGen(Base));
555 
556             if (Swt_Ip_ServiceKeyGen(Base) == SWT_IP_UNLOCK_VALUE1_U16)
557             {
558                 BUnlockSequence = TRUE;
559             }
560 
561             Swt_Ip_ServiceCmd(Base, Swt_Ip_ServiceKeyGen(Base));
562 
563             if ((SWT_IP_SOFTLOCK == Swt_Ip_GetLock(Base)) && (TRUE == BUnlockSequence))
564             {
565                 /* Unlock key generated. Complete the unlock sequence and lock it again. */
566                 Swt_Ip_ServiceCmd(Base, SWT_IP_UNLOCK_VALUE2_U16);
567                 Swt_Ip_Lock(Base, SWT_IP_SOFTLOCK);
568             }
569             break;
570         /* Fixed Service Sequence Mode */
571         case SWT_IP_FS_SEQ_MODE:
572             Swt_Ip_ServiceCmd(Base, SWT_IP_FIXED_SERVICE_VALUE1_U16);
573             Swt_Ip_ServiceCmd(Base, SWT_IP_FIXED_SERVICE_VALUE2_U16);
574             break;
575         default:
576             /* No register write required for servicing the watchdog.
577             The watchdog is serviced when executing code at the address loaded into the designated IAC register */
578             break;
579     }
580 
581     SchM_Exit_Wdg_WDG_EXCLUSIVE_AREA_09();
582 }
583 
584 /**
585 * @Description    This function configure SWT instance.
586 *
587 * @implements     Swt_Ip_Config_Activity
588 */
Swt_Ip_Config(const uint32 Instance,const Swt_Ip_ConfigType * const ConfigPtr)589 Swt_Ip_StatusType Swt_Ip_Config(const uint32 Instance,
590                               const Swt_Ip_ConfigType * const ConfigPtr)
591 {
592     SWT_Type * const Base = Swt_Ip_sBase[Instance];
593     Swt_Ip_StatusType Ret;
594 
595 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
596     DevAssert(Instance < SWT_INSTANCE_COUNT);
597     DevAssert(ConfigPtr != NULL_PTR);
598 #endif
599 
600     Ret = Swt_Ip_CheckTimeout(ConfigPtr->u32TimeoutValue, ConfigPtr->u32WindowValue);
601 
602     if (SWT_IP_STATUS_SUCCESS == Ret)
603     {
604         if (SWT_IP_STATUS_ERROR == Swt_Ip_Unlock(Base))
605         {
606             Ret = SWT_IP_STATUS_ERROR;
607         }
608         else
609         {
610             boolean bSwtEnabled = Swt_Ip_IsEnable(Base);
611 
612             if (bSwtEnabled)
613             {
614                 Swt_Ip_Disable(Base);
615             }
616 
617             /* Configures the SWT instance */
618             Swt_Ip_RegisterConfig(Base, ConfigPtr);
619 
620             /* If the timer was enebled, enable it after configuration is done */
621             if (bSwtEnabled)
622             {
623                 Swt_Ip_Enable(Base);
624             }
625             /* Lock the SWT */
626             Swt_Ip_Lock(Base, ConfigPtr->lockConfig);
627         }
628     }
629 
630     return Ret;
631 }
632 
633 /**
634 * @Description    This function sets the timeout value for the SWT instance.
635 *
636 * @implements     Swt_Ip_SetTimeout_Activity
637 */
Swt_Ip_SetTimeout(const uint32 Instance,const uint32 TimeoutValue,const uint32 WindowValue)638 Swt_Ip_StatusType Swt_Ip_SetTimeout(const uint32 Instance,
639                                     const uint32 TimeoutValue, const uint32 WindowValue)
640 {
641     SWT_Type * const Base = Swt_Ip_sBase[Instance];
642     Swt_Ip_LockType LockConfig;
643     Swt_Ip_StatusType Ret;
644 
645 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
646     DevAssert(Instance < SWT_INSTANCE_COUNT);
647 #endif
648 
649     LockConfig = Swt_Ip_GetLock(Base);
650     Ret = Swt_Ip_CheckTimeout(TimeoutValue, WindowValue);
651 
652     if (SWT_IP_STATUS_SUCCESS == Ret)
653     {
654         if (SWT_IP_STATUS_ERROR == Swt_Ip_Unlock(Base))
655         {
656             Ret = SWT_IP_STATUS_ERROR;
657         }
658         else
659         {
660             /* Stop the SWT timer */
661             Swt_Ip_Disable(Base);
662 
663             /* Sets timeout value */
664             Base->TO = TimeoutValue;
665 
666             /* Sets window value */
667             if (WindowValue > 0U)
668             {
669                 Base->WN = WindowValue;
670                 /* Enable Windowed Mode */
671                 Base->CR |= SWT_CR_WND_MASK;
672             }
673             else
674             {
675                 Base->CR &= ~SWT_CR_WND_MASK;
676             }
677 
678             /* Start the SWT timer */
679             Swt_Ip_Enable(Base);
680 
681             /* Lock the SWT */
682             Swt_Ip_Lock(Base, LockConfig);
683         }
684     }
685 
686     return Ret;
687 }
688 
689 /**
690 * @Description    This function starts the timer for the SWT instance.
691 *
692 * @implements     Swt_Ip_StartTimer_Activity
693 */
Swt_Ip_StartTimer(const uint32 Instance)694 Swt_Ip_StatusType Swt_Ip_StartTimer(const uint32 Instance)
695 {
696     SWT_Type * const Base = Swt_Ip_sBase[Instance];
697     Swt_Ip_LockType LockConfig;
698     Swt_Ip_StatusType Ret;
699 
700 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
701     DevAssert(Instance < SWT_INSTANCE_COUNT);
702 #endif
703 
704     LockConfig = Swt_Ip_GetLock(Base);
705     /* Unlocks the SWT */
706     SchM_Enter_Wdg_WDG_EXCLUSIVE_AREA_10();
707     Ret = Swt_Ip_Unlock(Base);
708 
709     if (SWT_IP_STATUS_SUCCESS == Ret)
710     {
711         /* Start the SWT timer */
712         Swt_Ip_Enable(Base);
713 
714         Swt_Ip_Lock(Base, LockConfig);
715     }
716     SchM_Exit_Wdg_WDG_EXCLUSIVE_AREA_10();
717 
718     return Ret;
719 }
720 
721 /**
722 * @Description    This function stops the timer for the SWT instance.
723 *
724 * @implements     Swt_Ip_StopTimer_Activity
725 */
Swt_Ip_StopTimer(const uint32 Instance)726 Swt_Ip_StatusType Swt_Ip_StopTimer(const uint32 Instance)
727 {
728     SWT_Type * const Base = Swt_Ip_sBase[Instance];
729     Swt_Ip_LockType LockConfig;
730     Swt_Ip_StatusType Ret;
731 
732 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
733     DevAssert(Instance < SWT_INSTANCE_COUNT);
734 #endif
735 
736     LockConfig = Swt_Ip_GetLock(Base);
737     /* Unlocks the SWT */
738     SchM_Enter_Wdg_WDG_EXCLUSIVE_AREA_11();
739     Ret = Swt_Ip_Unlock(Base);
740 
741     if (SWT_IP_STATUS_SUCCESS == Ret)
742     {
743         /* Start the SWT timer */
744         Swt_Ip_Disable(Base);
745 
746         Swt_Ip_Lock(Base, LockConfig);
747     }
748     SchM_Exit_Wdg_WDG_EXCLUSIVE_AREA_11();
749 
750     return Ret;
751 }
752 
753 #if (SWT_IP_HAS_CLEAR_RESET == 1)
754 #if (SWT_IP_DEINIT == STD_ON)
755 #if (SWT_IP_CLEAR_RESET_REQUEST == STD_ON)
756 /**
757 * @Description    This function clears reset request.
758 *
759 * @return                             Swt_Ip_StatusType.
760 * @retval  SWT_IP_STATUS_ERROR        returned if SWT has not requested a reset.
761 * @retval  SWT_IP_STATUS_SUCCESS      Clear reset request successfully.
762 * @implements     Swt_Ip_ClearResetRequest_Activity
763 */
Swt_Ip_ClearResetRequest(const uint32 Instance)764     Swt_Ip_StatusType Swt_Ip_ClearResetRequest(const uint32 Instance)
765     {
766         SWT_Type * const Base = Swt_Ip_sBase[Instance];
767         Swt_Ip_StatusType Ret = SWT_IP_STATUS_SUCCESS;
768 
769         uint32 TimeoutTicks = OsIf_MicrosToTicks(SWT_IP_OSIF_TIMEOUT_VAL, SWT_IP_TIMEOUT_METHOD);
770         uint32 CurrentTicks = 0u; /* initialize current counter */
771         uint32 ElapsedTicks = 0u; /* elapsed will give timeout */
772 
773     #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
774         DevAssert(Instance < SWT_INSTANCE_COUNT);
775 
776         /* Can the SWT instance be reset without system reset */
777         DevAssert((SWT_IP_RRR_SUPPORT & (1UL << Instance)) != 0UL);
778     #endif
779 
780         if (Swt_Ip_RequestedReset(Base))
781         {   /* Reset the SWT instance */
782             Swt_Ip_ClearRequest(Base);
783 
784             CurrentTicks = OsIf_GetCounter(SWT_IP_TIMEOUT_METHOD);
785 
786             /* Wait for the SWT instance to finish reseting */
787             while ((Swt_Ip_RequestedReset(Base)) && (ElapsedTicks < TimeoutTicks))
788             {
789                 ElapsedTicks += OsIf_GetElapsed(&CurrentTicks, SWT_IP_TIMEOUT_METHOD);
790             }
791 
792             if((ElapsedTicks >= TimeoutTicks) && (Swt_Ip_RequestedReset(Base)))
793             {
794                 Ret = SWT_IP_STATUS_TIMEOUT;
795             }
796 
797         }
798         else
799         {   /* SWT has not requested a reset */
800             Ret = SWT_IP_STATUS_ERROR;
801         }
802 
803         return Ret;
804     }
805 #endif /* SWT_IP_DEINIT == STD_ON */
806 #endif /* SWT_IP_DEINIT == STD_ON */
807 #endif /* SWT_IP_HAS_CLEAR_RESET == 1 */
808 
809 /**
810 * @Description    This function handles the swt interrupt request.
811 *
812 * @implements     Swt_Ip_IrqHandler_Activity
813 */
Swt_Ip_IrqHandler(uint32 Instance)814 void Swt_Ip_IrqHandler(uint32 Instance)
815 {
816     SWT_Type * const Base = Swt_Ip_sBase[Instance];
817 
818 #if (SWT_IP_DEV_ERROR_DETECT == STD_ON)
819     DevAssert(Instance < SWT_INSTANCE_COUNT);
820 #endif
821     if(Wdg_Ip_abStatus[Instance] == TRUE)
822     {
823         if  (Swt_Ip_GetIntFlag(Base) && Swt_Ip_IsIntEnabled(Base))
824         {
825             Swt_Ip_ClearIntFlag(Base);
826 
827             if (Swt_Ip_apCallbackPtr[Instance] != ((void *)0))
828             {
829                 Swt_Ip_apCallbackPtr[Instance]();
830             }
831         }
832     }
833     else
834     {
835         Swt_Ip_ClearIntFlag(Base);
836     }
837 }
838 
839 
840 #ifdef WDG_ROM
841     #if (WDG_ROM == 1U)
842         #define WDG_STOP_SEC_CODE
843     #endif
844 #else
845     #if (WDG_RAM == 0U)
846         #define WDG_STOP_SEC_RAMCODE
847     #endif
848 #endif
849 
850 #include "Wdg_MemMap.h"
851 
852 #ifdef __cplusplus
853 }
854 #endif
855 
856 /** @} */
857