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_DeviceRegisters.h"
25 #include "OsIf_Cfg.h"
26 #include "OsIf_Timer_System_Internal_Systick.h"
27 
28 /*==================================================================================================
29 *                                 SOURCE FILE VERSION INFORMATION
30 ==================================================================================================*/
31 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C                    43
32 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C     4
33 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C     7
34 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C  0
35 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C             3
36 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C             0
37 #define OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C             0
38 
39 /*==================================================================================================
40 *                                       FILE VERSION CHECKS
41 ==================================================================================================*/
42 /* Checks against OsIf_DeviceRegisters.h */
43 #if (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C != OSIF_DEVICE_REGISTERS_VENDOR_ID)
44     #error "OsIf_Timer_System_Internal_Systick.c and OsIf_DeviceRegisters.h have different vendor ids"
45 #endif
46 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_DEVICE_REGISTERS_AR_RELEASE_MAJOR_VERSION) || \
47      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_DEVICE_REGISTERS_AR_RELEASE_MINOR_VERSION) || \
48      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_DEVICE_REGISTERS_AR_RELEASE_REVISION_VERSION))
49     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_DeviceRegisters.h are different"
50 #endif
51 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MAJOR_VERSION) || \
52      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MINOR_VERSION) || \
53      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_DEVICE_REGISTERS_SW_PATCH_VERSION) \
54     )
55     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_DeviceRegisters.h are different"
56 #endif
57 
58 /* Checks against OsIf_Cfg.h */
59 #if (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
60     #error "OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h have different vendor ids"
61 #endif
62 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
63      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
64      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
65     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h are different"
66 #endif
67 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
68      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
69      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
70     )
71     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h are different"
72 #endif
73 
74 /* Checks against OsIf_Timer_System_Internal_Systick.h */
75 #if (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID)
76     #error "OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h have different vendor ids"
77 #endif
78 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION) || \
79      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION) || \
80      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION))
81     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h are different"
82 #endif
83 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION) || \
84      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION) || \
85      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION) \
86     )
87     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h are different"
88 #endif
89 /*==================================================================================================
90 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
91 ==================================================================================================*/
92 
93 /*==================================================================================================
94 *                                          LOCAL MACROS
95 ==================================================================================================*/
96 #if defined(OSIF_USE_SYSTICK)
97 #if (OSIF_USE_SYSTICK == STD_ON)
98 
99 #define SYSTICK_GET_COUNTER() ((S32_SysTick->CVR) & S32_SysTick_CVR_CURRENT_MASK)
100 #define SYSTICK_DELTA_OUTER(high, low, max) ((max) - ((high) - (low)))
101 #define SYSTICK_DELTA_INNER(high, low) ((high) - (low))
102 #define SYSTICK_MAX (0xFFFFFFu)
103 
104 #define SYSTICK_OVERFLOWED(curr, ref) (curr > ref)
105 /*==================================================================================================
106 *                                         LOCAL CONSTANTS
107 ==================================================================================================*/
108 
109 /*==================================================================================================
110 *                                         LOCAL VARIABLES
111 ==================================================================================================*/
112 
113 /*==================================================================================================
114 *                                        GLOBAL CONSTANTS
115 ==================================================================================================*/
116 
117 /*==================================================================================================
118 *                                        GLOBAL VARIABLES
119 ==================================================================================================*/
120 
121 /*==================================================================================================
122 *                                    LOCAL FUNCTION PROTOTYPES
123 ==================================================================================================*/
124 
125 /*==================================================================================================
126 *                                         LOCAL FUNCTIONS
127 ==================================================================================================*/
128 
129 /*==================================================================================================
130 *                                        GLOBAL FUNCTIONS
131 ==================================================================================================*/
132 #define BASENXP_START_SEC_CODE
133 #include "BaseNXP_MemMap.h"
134 
OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)135 void OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)
136 {
137     (void)SystemCounterFreq;
138 
139     /* For Cortex-M0 devices the systick counter is initialized with an undefined
140      value, so make sure to initialize it to 0 before starting */
141     S32_SysTick->CSRr = S32_SysTick_CSR_ENABLE(0u);
142     S32_SysTick->RVR = S32_SysTick_RVR_RELOAD(SYSTICK_MAX);
143     S32_SysTick->CVR = S32_SysTick_CVR_CURRENT(0U);
144     S32_SysTick->CSRr = S32_SysTick_CSR_ENABLE(1u) | S32_SysTick_CSR_TICKINT(0u) | S32_SysTick_CSR_CLKSOURCE(1u);
145 }
146 
147 /* OsIf_Timer_System_Internal_GetCounter_Activity */
OsIf_Timer_System_Internal_GetCounter(void)148 uint32 OsIf_Timer_System_Internal_GetCounter(void)
149 {
150     return SYSTICK_GET_COUNTER();
151 }
152 
OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)153 uint32 OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)
154 {
155     uint32 CurrentVal = SYSTICK_GET_COUNTER();
156     uint32 dif = 0u;
157     if (SYSTICK_OVERFLOWED((CurrentVal), (*CurrentRef)))
158     {
159         /* overflow occurred */
160         dif = SYSTICK_DELTA_OUTER(CurrentVal, *CurrentRef, S32_SysTick->RVR);
161     }
162     else
163     {
164         /* overflow did not occur */
165         dif = SYSTICK_DELTA_INNER(*CurrentRef, CurrentVal);
166     }
167     *CurrentRef = CurrentVal;
168     return dif;
169 }
170 
171 #define BASENXP_STOP_SEC_CODE
172 #include "BaseNXP_MemMap.h"
173 
174 #endif /* OSIF_USE_SYSTICK == STD_ON */
175 #endif /* defined(OSIF_USE_SYSTICK) */
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 /** @} */
182