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