1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /**
8 *   @file
9 *
10 *   @addtogroup osif_drv
11 *   @{
12 */
13 
14 #ifdef __cplusplus
15 extern "C"{
16 #endif
17 
18 /*==================================================================================================
19 *                                          INCLUDE FILES
20 * 1) system and project includes
21 * 2) needed interfaces from external units
22 * 3) internal and external interfaces from this unit
23 ==================================================================================================*/
24 #include "OsIf.h"
25 #include "OsIf_Cfg.h"
26 #include "OsIf_Cfg_TypesDef.h"
27 #include "OsIf_Timer_System.h"
28 
29 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
30 
31 #if defined(USING_OS_AUTOSAROS)
32 #include "Os.h"
33 #elif defined(USING_OS_FREERTOS)
34 #include "FreeRTOSConfig.h"
35 #elif defined(USING_OS_ZEPHYR)
36 #include <zephyr/kernel.h>
37 #else
38     /* Baremetal, make sure USING_OS_BAREMETAL is defined */
39 #ifndef USING_OS_BAREMETAL
40 #define USING_OS_BAREMETAL
41 #endif /* ifndef USING_OS_BAREMETAL */
42 #endif /* defined(USING_OS_AUTOSAROS) */
43 
44 /* Timer specific includes to define:
45  - OsIf_Timer_System_Internal_Init
46  - OsIf_Timer_System_Internal_GetCounter
47  - OsIf_Timer_System_Internal_GetElapsed
48  */
49 #if defined(OSIF_USE_SYSTICK)
50 #if (OSIF_USE_SYSTICK == STD_ON)
51 #include "OsIf_Timer_System_Internal_Systick.h"
52 #endif /* OSIF_USE_SYSTICK == STD_ON */
53 #endif /* defined(OSIF_USE_SYSTICK) */
54 
55 #if defined(OSIF_USE_GENERICTIMER)
56 #if (OSIF_USE_GENERICTIMER == STD_ON)
57 #include "OsIf_Timer_System_Internal_GenericTimer.h"
58 #endif /* OSIF_USE_GENERICTIMER == STD_ON */
59 #endif /* defined(OSIF_USE_GENERICTIMER) */
60 
61 /*==================================================================================================
62 *                                 SOURCE FILE VERSION INFORMATION
63 ==================================================================================================*/
64 #define OSIF_TIMER_SYSTEM_VENDOR_ID_C                    43
65 #define OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C     4
66 #define OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C     7
67 #define OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C  0
68 #define OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C             0
69 #define OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C             9
70 #define OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C             0
71 
72 /*==================================================================================================
73 *                                       FILE VERSION CHECKS
74 ==================================================================================================*/
75 /* Checks against OsIf.h */
76 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_VENDOR_ID)
77     #error "OsIf_Timer_System.c and OsIf.h have different vendor ids"
78 #endif
79 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_AR_RELEASE_MAJOR_VERSION) || \
80      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_AR_RELEASE_MINOR_VERSION) || \
81      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_AR_RELEASE_REVISION_VERSION))
82     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf.h are different"
83 #endif
84 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_SW_MAJOR_VERSION) || \
85      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_SW_MINOR_VERSION) || \
86      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_SW_PATCH_VERSION) \
87     )
88     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf.h are different"
89 #endif
90 
91 /* Checks against OsIf_Cfg.h */
92 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
93     #error "OsIf_Timer_System.c and OsIf_Cfg.h have different vendor ids"
94 #endif
95 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
96      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
97      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
98     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Cfg.h are different"
99 #endif
100 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
101      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
102      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
103     )
104     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Cfg.h are different"
105 #endif
106 
107 /* Checks against OsIf_Cfg_TypesDef.h */
108 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_CFG_TYPESDEF_VENDOR_ID)
109     #error "OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h have different vendor ids"
110 #endif
111 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MAJOR_VERSION) || \
112      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MINOR_VERSION) || \
113      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_TYPESDEF_AR_RELEASE_REVISION_VERSION))
114     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h are different"
115 #endif
116 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MAJOR_VERSION) || \
117      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MINOR_VERSION) || \
118      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_CFG_TYPESDEF_SW_PATCH_VERSION) \
119     )
120     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h are different"
121 #endif
122 
123 /* Checks against OsIf_Timer_System.h */
124 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_TIMER_SYSTEM_VENDOR_ID)
125     #error "OsIf_Timer_System.c and OsIf_Timer_System.h have different vendor ids"
126 #endif
127 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION) || \
128      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION) || \
129      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION))
130     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System.h are different"
131 #endif
132 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION) || \
133      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MINOR_VERSION) || \
134      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_SW_PATCH_VERSION) \
135     )
136     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System.h are different"
137 #endif
138 
139 #if defined(OSIF_USE_SYSTICK)
140 #if (OSIF_USE_SYSTICK == STD_ON)
141 /* Checks against OsIf_Timer_System_Internal_Systick.h */
142 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID)
143     #error "OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h have different vendor ids"
144 #endif
145 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION) || \
146      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION) || \
147      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION))
148     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h are different"
149 #endif
150 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION) || \
151      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION) || \
152      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION) \
153     )
154     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h are different"
155 #endif
156 #endif /* OSIF_USE_SYSTICK == STD_ON */
157 #endif /* defined(OSIF_USE_SYSTICK) */
158 
159 #if defined(OSIF_USE_GENERICTIMER)
160 #if OSIF_USE_GENERICTIMER == STD_ON
161 /* Checks against OsIf_Timer_System_Internal_GenericTimer.h */
162 #if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID)
163     #error "OsIf_Timer_System.c and OsIf_Timer_System_Internal_GenericTimer.h have different vendor ids"
164 #endif
165 #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION) || \
166      (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION) || \
167      (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION))
168     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_GenericTimer.h are different"
169 #endif
170 #if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION) || \
171      (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION) || \
172      (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION) \
173     )
174     #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_GenericTimer.h are different"
175 #endif
176 #endif /* OSIF_USE_GENERICTIMER == STD_ON */
177 #endif /* defined(OSIF_USE_GENERICTIMER) */
178 
179 #if defined(USING_OS_AUTOSAROS)
180 /* Checks against Os.h */
181 #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
182     #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C != OS_AR_RELEASE_MAJOR_VERSION) || \
183          (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C != OS_AR_RELEASE_MINOR_VERSION))
184         #error "AutoSar Version Numbers of OsIf_Timer_System.c and Os.h are different"
185     #endif
186 #endif
187 #endif /* defined(USING_OS_AUTOSAROS) */
188 
189 /*==================================================================================================
190 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
191 ==================================================================================================*/
192 
193 /*==================================================================================================
194 *                                          LOCAL MACROS
195 ==================================================================================================*/
196 #if (STD_ON == OSIF_ENABLE_USER_MODE_SUPPORT)
197     #define Trusted_OsIf_Timer_System_Internal_Init(Freq)               OsIf_Trusted_Call1param(OsIf_Timer_System_Internal_Init, (Freq))
198     #define Trusted_OsIf_Timer_System_Internal_GetCounter()             OsIf_Trusted_Call_Return(OsIf_Timer_System_Internal_GetCounter)
199     #define Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef)   OsIf_Trusted_Call_Return1param(OsIf_Timer_System_Internal_GetElapsed, (CurrentRef))
200     #define Trusted_k_cycle_get_32()                                    OsIf_Trusted_Call_Return(k_cycle_get_32)
201 #else
202     #define Trusted_OsIf_Timer_System_Internal_Init(Freq)               OsIf_Timer_System_Internal_Init(Freq)
203     #define Trusted_OsIf_Timer_System_Internal_GetCounter()             OsIf_Timer_System_Internal_GetCounter()
204     #define Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef)   OsIf_Timer_System_Internal_GetElapsed(CurrentRef)
205     #define Trusted_k_cycle_get_32()                                    k_cycle_get_32()
206 #endif
207 
208 #if STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT
209     #define OsIfCoreID()        (OsIf_GetCoreID())
210 #else
211     #define OsIfCoreID()        (0U)
212 #endif
213 /*==================================================================================================
214 *                                         LOCAL CONSTANTS
215 ==================================================================================================*/
216 
217 /*==================================================================================================
218 *                                         LOCAL VARIABLES
219 ==================================================================================================*/
220 #if STD_ON == OSIF_DEV_ERROR_DETECT
221 #define BASENXP_START_SEC_VAR_CLEARED_BOOLEAN
222 #include "BaseNXP_MemMap.h"
223 
224 static boolean OsIf_abMdlInit[OSIF_MAX_COREIDX_SUPPORTED];
225 
226 #define BASENXP_STOP_SEC_VAR_CLEARED_BOOLEAN
227 #include "BaseNXP_MemMap.h"
228 #endif /* STD_ON == OSIF_DEV_ERROR_DETECT */
229 
230 #if (defined(USING_OS_AUTOSAROS) || (STD_ON == OSIF_DEV_ERROR_DETECT))
231 #define BASENXP_START_SEC_VAR_CLEARED_UNSPECIFIED
232 #include "BaseNXP_MemMap.h"
233 
234 static const OsIf_ConfigType *OsIf_apxInternalCfg[OSIF_MAX_COREIDX_SUPPORTED];
235 
236 #define BASENXP_STOP_SEC_VAR_CLEARED_UNSPECIFIED
237 #include "BaseNXP_MemMap.h"
238 #endif /* (defined(USING_OS_AUTOSAROS) || (STD_ON == OSIF_DEV_ERROR_DETECT)) */
239 
240 #define BASENXP_START_SEC_VAR_CLEARED_32
241 #include "BaseNXP_MemMap.h"
242 
243 static uint32 OsIf_au32InternalFrequencies[OSIF_MAX_COREIDX_SUPPORTED];
244 
245 #define BASENXP_STOP_SEC_VAR_CLEARED_32
246 #include "BaseNXP_MemMap.h"
247 /*==================================================================================================
248 *                                        GLOBAL CONSTANTS
249 ==================================================================================================*/
250 
251 /*==================================================================================================
252 *                                        GLOBAL VARIABLES
253 ==================================================================================================*/
254 #define BASENXP_START_SEC_CONFIG_DATA_UNSPECIFIED
255 #include "BaseNXP_MemMap.h"
256 
257 extern const OsIf_ConfigType *const OsIf_apxPredefinedConfig[OSIF_MAX_COREIDX_SUPPORTED];
258 
259 #define BASENXP_STOP_SEC_CONFIG_DATA_UNSPECIFIED
260 #include "BaseNXP_MemMap.h"
261 /*==================================================================================================
262 *                                    LOCAL FUNCTION PROTOTYPES
263 ==================================================================================================*/
264 
265 /*==================================================================================================
266 *                                         LOCAL FUNCTIONS
267 ==================================================================================================*/
268 
269 /*==================================================================================================
270 *                                        GLOBAL FUNCTIONS
271 ==================================================================================================*/
272 #define BASENXP_START_SEC_CODE
273 #include "BaseNXP_MemMap.h"
274 
OsIf_Timer_System_Init(void)275 void OsIf_Timer_System_Init(void)
276 {
277     uint32 CoreId = OsIfCoreID();
278 
279 #if (STD_ON == OSIF_DEV_ERROR_DETECT)
280 #if (STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT)
281     if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxPredefinedConfig[CoreId]))
282 #else
283     if (NULL_PTR == OsIf_apxPredefinedConfig[CoreId])
284 #endif
285     {
286         #if defined(USING_OS_AUTOSAROS)
287         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_INIT, OSIF_E_INV_CORE_IDX);
288         #else
289         OSIF_DEV_ASSERT(FALSE);
290         #endif
291     }
292     OsIf_abMdlInit[CoreId] = TRUE;
293 #endif
294 
295 #if (defined(USING_OS_AUTOSAROS) || (STD_ON == OSIF_DEV_ERROR_DETECT))
296     OsIf_apxInternalCfg[CoreId] = OsIf_apxPredefinedConfig[CoreId];
297 #endif
298 #if (!defined(USING_OS_FREERTOS) && !defined(USING_OS_ZEPHYR))
299     OsIf_au32InternalFrequencies[CoreId] = OsIf_apxPredefinedConfig[CoreId]->counterFrequency;
300 #endif
301 
302 #if defined(USING_OS_FREERTOS)
303     /* FreeRTOS */
304     OsIf_au32InternalFrequencies[CoreId] = configCPU_CLOCK_HZ;
305 #elif defined(USING_OS_ZEPHYR)
306     /* ZephyrOS */
307     OsIf_au32InternalFrequencies[CoreId] = sys_clock_hw_cycles_per_sec();
308 #elif defined(USING_OS_BAREMETAL)
309     /* Baremetal */
310     Trusted_OsIf_Timer_System_Internal_Init(OsIf_au32InternalFrequencies[CoreId]);
311 #endif
312 }
313 
314 
OsIf_Timer_System_GetCounter(void)315 uint32 OsIf_Timer_System_GetCounter(void)
316 {
317     uint32 Counter = 0u;
318     uint32 CoreId = OsIfCoreID();
319 
320 #if STD_ON == OSIF_DEV_ERROR_DETECT
321     if (TRUE != OsIf_abMdlInit[CoreId])
322     {
323         #if defined(USING_OS_AUTOSAROS)
324         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETCOUNTER, OSIF_E_UNINIT);
325         #else
326         OSIF_DEV_ASSERT(FALSE);
327         #endif
328     }
329 #if (STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT)
330     else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
331 #else
332     else if (NULL_PTR == OsIf_apxInternalCfg[CoreId])
333 #endif
334     {
335         #if defined(USING_OS_AUTOSAROS)
336         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETCOUNTER, OSIF_E_INV_CORE_IDX);
337         #else
338         OSIF_DEV_ASSERT(FALSE);
339         #endif
340     }
341     else
342     {
343 #endif /* OSIF_DEV_ERROR_DETECT  */
344 
345 #if defined(USING_OS_AUTOSAROS)
346     StatusType Status;
347     TickType value;
348     Status = GetCounterValue(OsIf_apxInternalCfg[CoreId]->counterId, &value);
349     OSIF_DEV_ASSERT(Status == E_OK);
350     Counter = (uint32)value;
351 #elif defined(USING_OS_ZEPHYR)
352     /* ZephyrOS */
353     (void)CoreId;
354     Counter = Trusted_k_cycle_get_32();
355 #elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
356     /* FreeRTOS and Baremetal*/
357     (void)CoreId;
358     Counter = Trusted_OsIf_Timer_System_Internal_GetCounter();
359 #endif
360 
361 #if STD_ON == OSIF_DEV_ERROR_DETECT
362     }
363 #endif
364 
365     return Counter;
366 }
367 
368 
OsIf_Timer_System_GetElapsed(uint32 * const CurrentRef)369 uint32 OsIf_Timer_System_GetElapsed(uint32 * const CurrentRef)
370 {
371     uint32 Elapsed = 0u;
372     uint32 CoreId = OsIfCoreID();
373 
374 #if STD_ON == OSIF_DEV_ERROR_DETECT
375     if (TRUE != OsIf_abMdlInit[CoreId])
376     {
377         #if defined(USING_OS_AUTOSAROS)
378         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETELAPSED, OSIF_E_UNINIT);
379         #else
380         OSIF_DEV_ASSERT(FALSE);
381         #endif
382     }
383 #if (STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT)
384     else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
385 #else
386     else if (NULL_PTR == OsIf_apxInternalCfg[CoreId])
387 #endif
388     {
389         #if defined(USING_OS_AUTOSAROS)
390         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETELAPSED, OSIF_E_INV_CORE_IDX);
391         #else
392         OSIF_DEV_ASSERT(FALSE);
393         #endif
394     }
395     else
396     {
397 #endif /* OSIF_DEV_ERROR_DETECT  */
398 
399 #if defined(USING_OS_AUTOSAROS)
400     TickType ElapsedTickType;
401     StatusType Status;
402     Status = GetElapsedValue(OsIf_apxInternalCfg[CoreId]->counterId, (TickType*)CurrentRef, &ElapsedTickType);
403     OSIF_DEV_ASSERT(Status == E_OK);
404     Elapsed = (uint32)ElapsedTickType;
405 #elif defined(USING_OS_ZEPHYR)
406     /* No need for additional logic. The hardware clock is represented as a 32-bit counter */
407     uint32 CurrentVal = Trusted_k_cycle_get_32();
408     Elapsed = CurrentVal - *CurrentRef;
409     *CurrentRef = CurrentVal;
410     (void)CoreId;
411 #elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
412     /* FreeRTOS and Baremetal*/
413     (void)CoreId;
414     Elapsed = Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef);
415 #endif
416 
417 #if STD_ON == OSIF_DEV_ERROR_DETECT
418     }
419 #endif
420 
421     return Elapsed;
422 }
423 
424 
OsIf_Timer_System_SetTimerFrequency(uint32 Freq)425 void OsIf_Timer_System_SetTimerFrequency(uint32 Freq)
426 {
427     uint32 CoreId = OsIfCoreID();
428 #if STD_ON == OSIF_DEV_ERROR_DETECT
429 
430     if (TRUE != OsIf_abMdlInit[CoreId])
431     {
432         #if defined(USING_OS_AUTOSAROS)
433         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_SETTIMERFREQ, OSIF_E_UNINIT);
434         #else
435         OSIF_DEV_ASSERT(FALSE);
436         #endif
437     }
438 #if (STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT)
439     else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
440 #else
441     else if (NULL_PTR == OsIf_apxInternalCfg[CoreId])
442 #endif
443     {
444         #if defined(USING_OS_AUTOSAROS)
445         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_SETTIMERFREQ, OSIF_E_INV_CORE_IDX);
446         #else
447         OSIF_DEV_ASSERT(FALSE);
448         #endif
449     }
450     else
451     {
452 #endif /* OSIF_DEV_ERROR_DETECT  */
453 
454 #if defined(USING_OS_AUTOSAROS)
455     (void)CoreId;
456     (void)Freq;
457 #elif defined(USING_OS_ZEPHYR)
458     (void)CoreId;
459     (void)Freq;
460     /* As of 2.6.0: "The frequency of this counter is required to be steady over time" */
461 #elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
462     /* FreeRTOS and Baremetal*/
463     OsIf_au32InternalFrequencies[CoreId] = Freq;
464 #endif
465 
466 #if STD_ON == OSIF_DEV_ERROR_DETECT
467     }
468 #endif
469 }
470 
471 
OsIf_Timer_System_MicrosToTicks(uint32 Micros)472 uint32 OsIf_Timer_System_MicrosToTicks(uint32 Micros)
473 {
474     uint64 interim;
475     uint32 ticks = 0u;
476     uint32 CoreId = OsIfCoreID();
477 
478 #if STD_ON == OSIF_DEV_ERROR_DETECT
479     if (TRUE != OsIf_abMdlInit[CoreId])
480     {
481         #if defined(USING_OS_AUTOSAROS)
482         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_US2TICKS, OSIF_E_UNINIT);
483         #else
484         OSIF_DEV_ASSERT(FALSE);
485         #endif
486     }
487 #if (STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT)
488     else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
489 #else
490     else if (NULL_PTR == OsIf_apxInternalCfg[CoreId])
491 #endif
492     {
493         #if defined(USING_OS_AUTOSAROS)
494         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_US2TICKS, OSIF_E_INV_CORE_IDX);
495         #else
496         OSIF_DEV_ASSERT(FALSE);
497         #endif
498     }
499     else
500     {
501 #endif /* OSIF_DEV_ERROR_DETECT  */
502 
503     interim = Micros * (uint64)OsIf_au32InternalFrequencies[CoreId];
504     interim /= 1000000u;
505     OSIF_DEV_ASSERT(interim <= 0xFFFFFFFFu); /* check that computed value fits in 32 bits */
506     ticks = (uint32)(interim & 0xFFFFFFFFu);
507 
508 #if STD_ON == OSIF_DEV_ERROR_DETECT
509     }
510 #endif
511 
512     return ticks;
513 }
514 
515 #define BASENXP_STOP_SEC_CODE
516 #include "BaseNXP_MemMap.h"
517 
518 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
519 
520 #ifdef __cplusplus
521 }
522 #endif
523 
524 /** @} */
525