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             2
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 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_DeviceRegisters.h file are of the same vendor */
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 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_DeviceRegisters.h file are of the same Autosar version */
47 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_DEVICE_REGISTERS_AR_RELEASE_MAJOR_VERSION) || \
48      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_DEVICE_REGISTERS_AR_RELEASE_MINOR_VERSION) || \
49      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_DEVICE_REGISTERS_AR_RELEASE_REVISION_VERSION))
50     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_DeviceRegisters.h are different"
51 #endif
52 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_DeviceRegisters.h file are of the same Software version */
53 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MAJOR_VERSION) || \
54      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MINOR_VERSION) || \
55      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_DEVICE_REGISTERS_SW_PATCH_VERSION) \
56     )
57     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_DeviceRegisters.h are different"
58 #endif
59 
60 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Cfg.h file are of the same vendor */
61 #if (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
62     #error "OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h have different vendor ids"
63 #endif
64 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Cfg.h file are of the same Autosar version */
65 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
66      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
67      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
68     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h are different"
69 #endif
70 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Cfg.h file are of the same Software version */
71 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
72      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
73      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
74     )
75     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Cfg.h are different"
76 #endif
77 
78 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Timer_System_Internal_Systick.h file are of the same vendor */
79 #if (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID)
80     #error "OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h have different vendor ids"
81 #endif
82 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Timer_System_Internal_Systick.h file are of the same Autosar version */
83 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION) || \
84      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION) || \
85      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION))
86     #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h are different"
87 #endif
88 /* Check if OsIf_Timer_System_Internal_Systick.c file and OsIf_Timer_System_Internal_Systick.h file are of the same Software version */
89 #if ((OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION) || \
90      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION) || \
91      (OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION) \
92     )
93     #error "Software Version Numbers of OsIf_Timer_System_Internal_Systick.c and OsIf_Timer_System_Internal_Systick.h are different"
94 #endif
95 /*==================================================================================================
96 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
97 ==================================================================================================*/
98 
99 /*==================================================================================================
100 *                                          LOCAL MACROS
101 ==================================================================================================*/
102 #if defined(OSIF_USE_SYSTICK)
103 #if (OSIF_USE_SYSTICK == STD_ON)
104 
105 #define SYSTICK_GET_COUNTER() ((S32_SysTick->CVR) & S32_SysTick_CVR_CURRENT_MASK)
106 #define SYSTICK_DELTA_OUTER(high, low, max) ((max) - ((high) - (low)))
107 #define SYSTICK_DELTA_INNER(high, low) ((high) - (low))
108 #define SYSTICK_MAX (0xFFFFFFU)
109 
110 #define SYSTICK_OVERFLOWED(curr, ref) (curr > ref)
111 /*==================================================================================================
112 *                                         LOCAL CONSTANTS
113 ==================================================================================================*/
114 
115 /*==================================================================================================
116 *                                         LOCAL VARIABLES
117 ==================================================================================================*/
118 
119 /*==================================================================================================
120 *                                        GLOBAL CONSTANTS
121 ==================================================================================================*/
122 
123 /*==================================================================================================
124 *                                        GLOBAL VARIABLES
125 ==================================================================================================*/
126 
127 /*==================================================================================================
128 *                                    LOCAL FUNCTION PROTOTYPES
129 ==================================================================================================*/
130 
131 /*==================================================================================================
132 *                                         LOCAL FUNCTIONS
133 ==================================================================================================*/
134 
135 /*==================================================================================================
136 *                                        GLOBAL FUNCTIONS
137 ==================================================================================================*/
138 #define BASENXP_START_SEC_CODE
139 #include "BaseNXP_MemMap.h"
140 
141 /*FUNCTION**********************************************************************
142  *
143  * Function Name : OsIf_Timer_System_Internal_Init.
144  * Description   : Initialize systick timer.
145  *
146  *END**************************************************************************/
OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)147 void OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)
148 {
149     (void)SystemCounterFreq;
150 
151     /* For Cortex-M0 devices the systick counter is initialized with an undefined
152      value, so make sure to initialize it to 0 before starting */
153     S32_SysTick->CSRr = S32_SysTick_CSR_ENABLE(0u);
154     S32_SysTick->RVR = S32_SysTick_RVR_RELOAD(SYSTICK_MAX);
155     S32_SysTick->CVR = S32_SysTick_CVR_CURRENT(0U);
156     S32_SysTick->CSRr = S32_SysTick_CSR_ENABLE(1u) | S32_SysTick_CSR_TICKINT(0u) | S32_SysTick_CSR_CLKSOURCE(1u);
157 }
158 
159 /*FUNCTION**********************************************************************
160  *
161  * Function Name : OsIf_Timer_System_Internal_GetCounter.
162  * Description   : Get systick counter value.
163  *
164  *END**************************************************************************/
OsIf_Timer_System_Internal_GetCounter(void)165 uint32 OsIf_Timer_System_Internal_GetCounter(void)
166 {
167     return SYSTICK_GET_COUNTER();
168 }
169 
170 /*FUNCTION**********************************************************************
171  *
172  * Function Name : OsIf_Timer_System_Internal_GetElapsed.
173  * Description   : Get systick elapsed value.
174  *
175  *END**************************************************************************/
OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)176 uint32 OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)
177 {
178     uint32 CurrentVal = SYSTICK_GET_COUNTER();
179     uint32 dif = 0U;
180 
181     if (SYSTICK_OVERFLOWED((CurrentVal), (*CurrentRef)))
182     {
183         /* overflow occurred */
184         dif = SYSTICK_DELTA_OUTER(CurrentVal, *CurrentRef, SYSTICK_MAX);
185     }
186     else
187     {
188         /* overflow did not occur */
189         dif = SYSTICK_DELTA_INNER(*CurrentRef, CurrentVal);
190     }
191     *CurrentRef = CurrentVal;
192 
193     return dif;
194 }
195 
196 #define BASENXP_STOP_SEC_CODE
197 #include "BaseNXP_MemMap.h"
198 
199 #endif /* (OSIF_USE_SYSTICK == STD_ON) */
200 #endif /* defined(OSIF_USE_SYSTICK) */
201 
202 #ifdef __cplusplus
203 }
204 #endif
205 
206 /** @} */
207