1 /*
2  * Copyright 2021-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 
28 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
29 /* Defines the Hw Timer functions to be implemented in OS-specific environments */
30 #include "OsIf_Timer_System.h"
31 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
32 
33 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
34 /* API functions to be implemented by user */
35 #include "OsIf_Timer_Custom.h"
36 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
37 
38 /*==================================================================================================
39 *                                 SOURCE FILE VERSION INFORMATION
40 ==================================================================================================*/
41 #define OSIF_TIMER_VENDOR_ID_C                    43
42 #define OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C     4
43 #define OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C     7
44 #define OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C  0
45 #define OSIF_TIMER_SW_MAJOR_VERSION_C             1
46 #define OSIF_TIMER_SW_MINOR_VERSION_C             0
47 #define OSIF_TIMER_SW_PATCH_VERSION_C             0
48 
49 /*==================================================================================================
50 *                                       FILE VERSION CHECKS
51 ==================================================================================================*/
52 /* Check if OsIf_Timer.c file and OsIf.h file are of the same vendor */
53 #if (OSIF_TIMER_VENDOR_ID_C != OSIF_VENDOR_ID)
54     #error "OsIf_Timer.c and OsIf.h have different vendor ids"
55 #endif
56 /* Check if OsIf_Timer.c file and OsIf.h file are of the same Autosar version */
57 #if ((OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C    != OSIF_AR_RELEASE_MAJOR_VERSION) || \
58      (OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C    != OSIF_AR_RELEASE_MINOR_VERSION) || \
59      (OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_AR_RELEASE_REVISION_VERSION))
60     #error "AUTOSAR Version Numbers of OsIf_Timer.c and OsIf.h are different"
61 #endif
62 /* Check if OsIf_Timer.c file and OsIf.h file are of the same Software version */
63 #if ((OSIF_TIMER_SW_MAJOR_VERSION_C != OSIF_SW_MAJOR_VERSION) || \
64      (OSIF_TIMER_SW_MINOR_VERSION_C != OSIF_SW_MINOR_VERSION) || \
65      (OSIF_TIMER_SW_PATCH_VERSION_C != OSIF_SW_PATCH_VERSION) \
66     )
67     #error "Software Version Numbers of OsIf_Timer.c and OsIf.h are different"
68 #endif
69 
70 /* Check if OsIf_Timer.c file and OsIf_Cfg.h file are of the same vendor */
71 #if (OSIF_TIMER_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
72     #error "OsIf_Timer.c and OsIf_Cfg.h have different vendor ids"
73 #endif
74 /* Check if OsIf_Timer.c file and OsIf_Cfg.h file are of the same Autosar version */
75 #if ((OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
76      (OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
77      (OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
78     #error "AUTOSAR Version Numbers of OsIf_Timer.c and OsIf_Cfg.h are different"
79 #endif
80 /* Check if OsIf_Timer.c file and OsIf_Cfg.h file are of the same Software version */
81 #if ((OSIF_TIMER_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
82      (OSIF_TIMER_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
83      (OSIF_TIMER_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
84     )
85     #error "Software Version Numbers of OsIf_Timer.c and OsIf_Cfg.h are different"
86 #endif
87 
88 /* Check if OsIf_Timer.c file and OsIf_Cfg_TypesDef.h file are of the same vendor */
89 #if (OSIF_TIMER_VENDOR_ID_C != OSIF_CFG_TYPESDEF_VENDOR_ID)
90     #error "OsIf_Timer.c and OsIf_Cfg_TypesDef.h have different vendor ids"
91 #endif
92 /* Check if OsIf_Timer.c file and OsIf_Cfg_TypesDef.h file are of the same Autosar version */
93 #if ((OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MAJOR_VERSION) || \
94      (OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MINOR_VERSION) || \
95      (OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_TYPESDEF_AR_RELEASE_REVISION_VERSION))
96     #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h are different"
97 #endif
98 /* Check if OsIf_Timer.c file and OsIf_Cfg_TypesDef.h file are of the same Software version */
99 #if ((OSIF_TIMER_SW_MAJOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MAJOR_VERSION) || \
100      (OSIF_TIMER_SW_MINOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MINOR_VERSION) || \
101      (OSIF_TIMER_SW_PATCH_VERSION_C != OSIF_CFG_TYPESDEF_SW_PATCH_VERSION) \
102     )
103     #error "Software Version Numbers of OsIf_Timer.c and OsIf_Cfg_TypesDef.h are different"
104 #endif
105 
106 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
107 /* Check if OsIf_Timer.c file and OsIf_Timer_System.h file are of the same vendor */
108 #if (OSIF_TIMER_VENDOR_ID_C != OSIF_TIMER_SYSTEM_VENDOR_ID)
109     #error "OsIf_Timer.c and OsIf_Timer_System.h have different vendor ids"
110 #endif
111 /* Check if OsIf_Timer.c file and OsIf_Timer_System.h file are of the same Autosar version */
112 #if ((OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION) || \
113      (OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION) || \
114      (OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION))
115     #error "AUTOSAR Version Numbers of OsIf_Timer.c and OsIf_Timer_System.h are different"
116 #endif
117 /* Check if OsIf_Timer.c file and OsIf_Timer_System.h file are of the same Software version */
118 #if ((OSIF_TIMER_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION) || \
119      (OSIF_TIMER_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MINOR_VERSION) || \
120      (OSIF_TIMER_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_SW_PATCH_VERSION) \
121     )
122     #error "Software Version Numbers of OsIf_Timer.c and OsIf_Timer_System.h are different"
123 #endif
124 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
125 
126 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
127 /* Check if OsIf_Timer.c file and OsIf_Timer_Custom.h file are of the same vendor */
128 #if (OSIF_TIMER_VENDOR_ID_C != OSIF_TIMER_CUSTOM_VENDOR_ID)
129     #error "OsIf_Timer.c and OsIf_Timer_Custom.h have different vendor ids"
130 #endif
131 /* Check if OsIf_Timer.c file and OsIf_Timer_Custom.h file are of the same Autosar version */
132 #if ((OSIF_TIMER_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_CUSTOM_AR_RELEASE_MAJOR_VERSION) || \
133      (OSIF_TIMER_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_CUSTOM_AR_RELEASE_MINOR_VERSION) || \
134      (OSIF_TIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_CUSTOM_AR_RELEASE_REVISION_VERSION))
135     #error "AUTOSAR Version Numbers of OsIf_Timer.c and OsIf_Timer_Custom.h are different"
136 #endif
137 /* Check if OsIf_Timer.c file and OsIf_Timer_Custom.h file are of the same Software version */
138 #if ((OSIF_TIMER_SW_MAJOR_VERSION_C != OSIF_TIMER_CUSTOM_SW_MAJOR_VERSION) || \
139      (OSIF_TIMER_SW_MINOR_VERSION_C != OSIF_TIMER_CUSTOM_SW_MINOR_VERSION) || \
140      (OSIF_TIMER_SW_PATCH_VERSION_C != OSIF_TIMER_CUSTOM_SW_PATCH_VERSION) \
141     )
142     #error "Software Version Numbers of OsIf_Timer.c and OsIf_Timer_Custom.h are different"
143 #endif
144 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
145 
146 /*==================================================================================================
147 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
148 ==================================================================================================*/
149 
150 /*==================================================================================================
151 *                                          LOCAL MACROS
152 ==================================================================================================*/
153 
154 /*==================================================================================================
155 *                                         LOCAL CONSTANTS
156 ==================================================================================================*/
157 
158 /*==================================================================================================
159 *                                         LOCAL VARIABLES
160 ==================================================================================================*/
161 
162 /*==================================================================================================
163 *                                        GLOBAL CONSTANTS
164 ==================================================================================================*/
165 
166 /*==================================================================================================
167 *                                        GLOBAL VARIABLES
168 ==================================================================================================*/
169 
170 /*==================================================================================================
171 *                                    LOCAL FUNCTION PROTOTYPES
172 ==================================================================================================*/
173 #define BASENXP_START_SEC_CODE
174 #include "BaseNXP_MemMap.h"
175 
176 static inline uint32 OsIf_Timer_Dummy_GetCounter(void);
177 static inline uint32 OsIf_Timer_Dummy_GetElapsed(const uint32 * const CurrentRef);
178 static inline void OsIf_Timer_Dummy_SetTimerFrequency(uint32 Freq);
179 static inline uint32 OsIf_Timer_Dummy_MicrosToTicks(uint32 Micros);
180 
181 #define BASENXP_STOP_SEC_CODE
182 #include "BaseNXP_MemMap.h"
183 /*==================================================================================================
184 *                                         LOCAL FUNCTIONS
185 ==================================================================================================*/
186 #define BASENXP_START_SEC_CODE
187 #include "BaseNXP_MemMap.h"
188 
189 /*FUNCTION**********************************************************************
190  *
191  * Function Name : OsIf_Timer_Dummy_GetCounter.
192  * Description   : Get counter value.
193  *
194  *END**************************************************************************/
OsIf_Timer_Dummy_GetCounter(void)195 static inline uint32 OsIf_Timer_Dummy_GetCounter(void)
196 {
197     return 0U;
198 }
199 
200 /*FUNCTION**********************************************************************
201  *
202  * Function Name : OsIf_Timer_Dummy_GetElapsed.
203  * Description   : Get elapsed value.
204  *
205  *END**************************************************************************/
OsIf_Timer_Dummy_GetElapsed(const uint32 * const CurrentRef)206 static inline uint32 OsIf_Timer_Dummy_GetElapsed(const uint32 * const CurrentRef)
207 {
208     (void)CurrentRef;
209     return 1U;
210 }
211 
212 /*FUNCTION**********************************************************************
213  *
214  * Function Name : OsIf_Timer_Dummy_SetTimerFrequency.
215  * Description   : Set timer frequency.
216  *
217  *END**************************************************************************/
OsIf_Timer_Dummy_SetTimerFrequency(uint32 Freq)218 static inline void OsIf_Timer_Dummy_SetTimerFrequency(uint32 Freq)
219 {
220     (void)Freq;
221 }
222 
223 /*FUNCTION**********************************************************************
224  *
225  * Function Name : OsIf_Timer_Dummy_MicrosToTicks.
226  * Description   : Convert micro second to ticks.
227  *
228  *END**************************************************************************/
OsIf_Timer_Dummy_MicrosToTicks(uint32 Micros)229 static inline uint32 OsIf_Timer_Dummy_MicrosToTicks(uint32 Micros)
230 {
231     return Micros;
232 }
233 
234 #define BASENXP_STOP_SEC_CODE
235 #include "BaseNXP_MemMap.h"
236 /*==================================================================================================
237 *                                        GLOBAL FUNCTIONS
238 ==================================================================================================*/
239 #define BASENXP_START_SEC_CODE
240 #include "BaseNXP_MemMap.h"
241 
242 /*FUNCTION**********************************************************************
243  *
244  * Function Name : OsIf_Init.
245  * Description   : OsIf initialization.
246  * @implements OsIf_Init_Activity
247  *
248  *END**************************************************************************/
OsIf_Init(const void * Config)249 void OsIf_Init(const void* Config)
250 {
251 #if (STD_ON == OSIF_DEV_ERROR_DETECT)
252     if (Config != NULL_PTR)
253     {
254     #if defined(USING_OS_AUTOSAROS)
255         (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_INIT, OSIF_E_INIT_FAILED);
256     #else
257         OSIF_DEV_ASSERT(FALSE);
258     #endif /* defined(USING_OS_AUTOSAROS) */
259     }
260 #else
261     (void)Config;
262 #endif /* (STD_ON == OSIF_DEV_ERROR_DETECT) */
263 
264 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
265     OsIf_Timer_System_Init();
266 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
267 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
268     OsIf_Timer_Custom_Init();
269 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
270 }
271 
272 /*FUNCTION**********************************************************************
273  *
274  * Function Name : OsIf_GetCounter.
275  * Description   : Get counter value.
276  * @implements OsIf_GetCounter_Activity
277  *
278  *END**************************************************************************/
OsIf_GetCounter(OsIf_CounterType SelectedCounter)279 uint32 OsIf_GetCounter(OsIf_CounterType SelectedCounter)
280 {
281     uint32 Value = 0U;
282 
283     switch (SelectedCounter){
284         case OSIF_COUNTER_DUMMY:
285             Value = OsIf_Timer_Dummy_GetCounter();
286             break;
287 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
288         case OSIF_COUNTER_SYSTEM:
289             Value = OsIf_Timer_System_GetCounter();
290             break;
291 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
292 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
293         case OSIF_COUNTER_CUSTOM:
294             Value = OsIf_Timer_Custom_GetCounter();
295             break;
296 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
297         default:
298             /* impossible */
299             break;
300     }
301 
302     return Value;
303 }
304 
305 
306 /*FUNCTION**********************************************************************
307  *
308  * Function Name : OsIf_GetElapsed.
309  * Description   : Get elapsed value.
310  * @implements OsIf_GetElapsed_Activity
311  *
312  *END**************************************************************************/
OsIf_GetElapsed(uint32 * const CurrentRef,OsIf_CounterType SelectedCounter)313 uint32 OsIf_GetElapsed(uint32 * const CurrentRef, OsIf_CounterType SelectedCounter)
314 {
315     uint32 Value = 0U;
316 
317     switch (SelectedCounter){
318         case OSIF_COUNTER_DUMMY:
319             Value = OsIf_Timer_Dummy_GetElapsed(CurrentRef);
320             break;
321 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
322         case OSIF_COUNTER_SYSTEM:
323             Value = OsIf_Timer_System_GetElapsed(CurrentRef);
324             break;
325 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
326 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
327         case OSIF_COUNTER_CUSTOM:
328             Value = OsIf_Timer_Custom_GetElapsed(CurrentRef);
329             break;
330 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
331         default:
332             /* impossible */
333             break;
334     }
335 
336     return Value;
337 }
338 
339 /*FUNCTION**********************************************************************
340  *
341  * Function Name : OsIf_SetTimerFrequency.
342  * Description   : Set timer frequency.
343  * @implements OsIf_SetTimerFrequency_Activity
344  *
345  *END**************************************************************************/
346 /* @implements OsIf_SetTimerFrequency_Activity */
OsIf_SetTimerFrequency(uint32 Freq,OsIf_CounterType SelectedCounter)347 void OsIf_SetTimerFrequency(uint32 Freq, OsIf_CounterType SelectedCounter)
348 {
349     switch (SelectedCounter){
350         case OSIF_COUNTER_DUMMY:
351             OsIf_Timer_Dummy_SetTimerFrequency(Freq);
352             break;
353 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
354         case OSIF_COUNTER_SYSTEM:
355             OsIf_Timer_System_SetTimerFrequency(Freq);
356             break;
357 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
358 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
359         case OSIF_COUNTER_CUSTOM:
360             OsIf_Timer_Custom_SetTimerFrequency(Freq);
361             break;
362 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
363         default:
364             /* impossible */
365             break;
366     }
367 }
368 
369 /*FUNCTION**********************************************************************
370  *
371  * Function Name : OsIf_MicrosToTicks.
372  * Description   : Convert micro second to ticks.
373  * @implements OsIf_MicrosToTicks_Activity
374  *
375  *END**************************************************************************/
OsIf_MicrosToTicks(uint32 Micros,OsIf_CounterType SelectedCounter)376 uint32 OsIf_MicrosToTicks(uint32 Micros, OsIf_CounterType SelectedCounter)
377 {
378     uint32 Value = 0U;
379 
380     switch (SelectedCounter){
381         case OSIF_COUNTER_DUMMY:
382             Value = OsIf_Timer_Dummy_MicrosToTicks(Micros);
383             break;
384 #if (OSIF_USE_SYSTEM_TIMER == STD_ON)
385         case OSIF_COUNTER_SYSTEM:
386             Value = OsIf_Timer_System_MicrosToTicks(Micros);
387             break;
388 #endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
389 #if (OSIF_USE_CUSTOM_TIMER == STD_ON)
390         case OSIF_COUNTER_CUSTOM:
391             Value = OsIf_Timer_Custom_MicrosToTicks(Micros);
392             break;
393 #endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
394         default:
395             /* impossible */
396             break;
397     }
398 
399     return Value;
400 }
401 
402 #define BASENXP_STOP_SEC_CODE
403 #include "BaseNXP_MemMap.h"
404 
405 #ifdef __cplusplus
406 }
407 #endif
408 
409 /** @} */
410