1 /*
2 * Copyright 2018-2019, 2023 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_common.h"
10 #include "fsl_device_registers.h"
11 #include "fsl_adapter_timer.h"
12 #include "fsl_ctimer.h"
13
14 #define gStackTimerChannel_c (kCTIMER_Match_0)
15 typedef struct _hal_timer_handle_struct_t
16 {
17 uint32_t timeout;
18 uint32_t timerClock_Hz;
19 hal_timer_callback_t callback;
20 void *callbackParam;
21 uint8_t instance;
22 } hal_timer_handle_struct_t;
23
24 /*******************************************************************************
25 * Variables
26 ******************************************************************************/
27
28 static CTIMER_Type *const s_CtimerBase[] = CTIMER_BASE_PTRS;
29
30 static hal_timer_handle_t s_timerHandle[sizeof(s_CtimerBase) / sizeof(CTIMER_Type *)];
31 /************************************************************************************
32 *************************************************************************************
33 * Private prototypes
34 *************************************************************************************
35 ************************************************************************************/
36
37 /************************************************************************************
38 *************************************************************************************
39 * Private memory declarations
40 *************************************************************************************
41 ************************************************************************************/
HAL_TimerInterruptHandle(uint8_t instance)42 static void HAL_TimerInterruptHandle(uint8_t instance)
43 {
44 hal_timer_handle_struct_t *halTimerState = (hal_timer_handle_struct_t *)s_timerHandle[instance];
45
46 if (halTimerState->callback != NULL)
47 {
48 halTimerState->callback(halTimerState->callbackParam);
49 }
50 }
51
52 void ctimer0_match0_callback(uint32_t flags);
ctimer0_match0_callback(uint32_t flags)53 void ctimer0_match0_callback(uint32_t flags)
54 {
55 HAL_TimerInterruptHandle(0);
56 }
57
58 void ctimer1_match0_callback(uint32_t flags);
ctimer1_match0_callback(uint32_t flags)59 void ctimer1_match0_callback(uint32_t flags)
60 {
61 HAL_TimerInterruptHandle(1);
62 }
63 static ctimer_callback_t ctimer_callback_table[] = {ctimer0_match0_callback, ctimer1_match0_callback};
64
HAL_CTimerConfigTimeout(hal_timer_handle_t halTimerHandle,uint32_t timeout)65 static hal_timer_status_t HAL_CTimerConfigTimeout(hal_timer_handle_t halTimerHandle, uint32_t timeout)
66 {
67 ctimer_match_config_t mCtimerMatchConfig;
68 ctimer_config_t config;
69 assert(halTimerHandle);
70 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
71 halTimerState->timeout = timeout;
72 CTIMER_GetDefaultConfig(&config);
73 CTIMER_Init(s_CtimerBase[halTimerState->instance], &config);
74 CTIMER_StopTimer(s_CtimerBase[halTimerState->instance]);
75
76 /* Configuration 0 */
77 mCtimerMatchConfig.enableCounterReset = true;
78 mCtimerMatchConfig.enableCounterStop = false;
79 mCtimerMatchConfig.outControl = kCTIMER_Output_NoAction;
80 mCtimerMatchConfig.outPinInitState = false;
81 mCtimerMatchConfig.enableInterrupt = true;
82 mCtimerMatchConfig.matchValue = (uint32_t)USEC_TO_COUNT(halTimerState->timeout, halTimerState->timerClock_Hz);
83 if ((mCtimerMatchConfig.matchValue < 1U) || (mCtimerMatchConfig.matchValue > 0xFFFFFFF0U))
84 {
85 return kStatus_HAL_TimerOutOfRanger;
86 }
87 /* Configure channel to Software compare; output pin not used */
88 CTIMER_RegisterCallBack(s_CtimerBase[halTimerState->instance], &ctimer_callback_table[halTimerState->instance],
89 kCTIMER_SingleCallback);
90 CTIMER_SetupMatch(s_CtimerBase[halTimerState->instance], (ctimer_match_t)gStackTimerChannel_c, &mCtimerMatchConfig);
91 return kStatus_HAL_TimerSuccess;
92 }
93
94 /************************************************************************************
95 *************************************************************************************
96 * Public functions
97 *************************************************************************************
98 ************************************************************************************/
HAL_TimerInit(hal_timer_handle_t halTimerHandle,hal_timer_config_t * halTimerConfig)99 hal_timer_status_t HAL_TimerInit(hal_timer_handle_t halTimerHandle, hal_timer_config_t *halTimerConfig)
100 {
101 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
102 ctimer_config_t config;
103 assert(sizeof(hal_timer_handle_struct_t) == HAL_TIMER_HANDLE_SIZE);
104 assert(halTimerConfig);
105 assert(halTimerHandle);
106 assert(halTimerConfig->instance < (sizeof(s_CtimerBase) / sizeof(CTIMER_Type *)));
107
108 CTIMER_GetDefaultConfig(&config);
109 halTimerState->timeout = halTimerConfig->timeout;
110 halTimerState->instance = halTimerConfig->instance;
111 halTimerState->timerClock_Hz = (uint32_t)halTimerConfig->srcClock_Hz / (uint32_t)(config.prescale + 1U);
112 s_timerHandle[halTimerState->instance] = halTimerHandle;
113 return HAL_CTimerConfigTimeout(halTimerHandle, halTimerState->timeout);
114 }
115 /*************************************************************************************/
HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)116 void HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)
117 {
118 assert(halTimerHandle);
119 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
120 s_timerHandle[halTimerState->instance] = NULL;
121 CTIMER_Deinit(s_CtimerBase[halTimerState->instance]);
122 }
123 /*************************************************************************************/
HAL_TimerEnable(hal_timer_handle_t halTimerHandle)124 void HAL_TimerEnable(hal_timer_handle_t halTimerHandle)
125 {
126 assert(halTimerHandle);
127 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
128 CTIMER_StartTimer(s_CtimerBase[halTimerState->instance]);
129 }
130
131 /*************************************************************************************/
HAL_TimerDisable(hal_timer_handle_t halTimerHandle)132 void HAL_TimerDisable(hal_timer_handle_t halTimerHandle)
133 {
134 assert(halTimerHandle);
135 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
136 CTIMER_StopTimer(s_CtimerBase[halTimerState->instance]);
137 }
138
139 /*************************************************************************************/
HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle,hal_timer_callback_t callback,void * callbackParam)140 void HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle, hal_timer_callback_t callback, void *callbackParam)
141 {
142 assert(halTimerHandle);
143 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
144 halTimerState->callback = callback;
145 halTimerState->callbackParam = callbackParam;
146 }
147
HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)148 uint32_t HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)
149 {
150 uint32_t reserveCount;
151 uint64_t retValue;
152 uint32_t reserveMs = 4U;
153 assert(halTimerHandle);
154 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
155 reserveCount = (uint32_t)MSEC_TO_COUNT((reserveMs), (halTimerState->timerClock_Hz));
156
157 retValue = COUNT_TO_USEC(((uint64_t)0xFFFFFFFF - (uint64_t)reserveCount), (uint64_t)halTimerState->timerClock_Hz);
158 return (uint32_t)((retValue > 0xFFFFFFFFU) ? (0xFFFFFFFFU - reserveMs * 1000U) : (uint32_t)retValue);
159 }
160 /* return micro us */
HAL_TimerGetCurrentTimerCount(hal_timer_handle_t halTimerHandle)161 uint32_t HAL_TimerGetCurrentTimerCount(hal_timer_handle_t halTimerHandle)
162 {
163 assert(halTimerHandle);
164 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
165 return (uint32_t)COUNT_TO_USEC(s_CtimerBase[halTimerState->instance]->TC, halTimerState->timerClock_Hz);
166 }
167
HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle,uint32_t timeout)168 hal_timer_status_t HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle, uint32_t timeout)
169 {
170 return HAL_CTimerConfigTimeout(halTimerHandle, timeout);
171 }
172
HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)173 void HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)
174 {
175 assert(halTimerHandle);
176 }
177
HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)178 void HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)
179 {
180 assert(halTimerHandle);
181 }
182