1 /*
2 * Copyright 2018-2022, 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_ostimer.h"
13
14 typedef struct _hal_timer_handle_struct_t
15 {
16 uint32_t timeout;
17 uint32_t timerClock_Hz;
18 hal_timer_callback_t callback;
19 void *callbackParam;
20 uint8_t instance;
21 } hal_timer_handle_struct_t;
22
23 /*******************************************************************************
24 * Variables
25 ******************************************************************************/
26 static OSTIMER_Type *const s_ostimerBase[] = OSTIMER_BASE_PTRS;
27
28 static hal_timer_handle_t s_timerHandle[sizeof(s_ostimerBase) / sizeof(OSTIMER_Type *)];
29
30 /************************************************************************************
31 *************************************************************************************
32 * Private prototypes
33 *************************************************************************************
34 ************************************************************************************/
35
36 /************************************************************************************
37 *************************************************************************************
38 * Private memory declarations
39 *************************************************************************************
40 ************************************************************************************/
HAL_TimerInterruptHandle(uint8_t instance)41 static void HAL_TimerInterruptHandle(uint8_t instance)
42 {
43 hal_timer_handle_struct_t *halTimerState = (hal_timer_handle_struct_t *)s_timerHandle[instance];
44
45 if (halTimerState->callback != NULL)
46 {
47 halTimerState->callback(halTimerState->callbackParam);
48 }
49 }
50
51 void ostimer_match_callback(void);
ostimer_match_callback(void)52 void ostimer_match_callback(void)
53 {
54 HAL_TimerInterruptHandle(0);
55 }
56
57 static ostimer_callback_t ostimer_callback_table[] = {ostimer_match_callback};
58
59 /************************************************************************************
60 *************************************************************************************
61 * Public functions
62 *************************************************************************************
63 ************************************************************************************/
HAL_TimerInit(hal_timer_handle_t halTimerHandle,hal_timer_config_t * halTimerConfig)64 hal_timer_status_t HAL_TimerInit(hal_timer_handle_t halTimerHandle, hal_timer_config_t *halTimerConfig)
65 {
66 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
67 assert(sizeof(hal_timer_handle_struct_t) == HAL_TIMER_HANDLE_SIZE);
68 assert(halTimerConfig);
69 assert(halTimerHandle);
70 assert(halTimerConfig->instance < (sizeof(s_ostimerBase) / sizeof(OSTIMER_Type *)));
71
72 halTimerState->instance = halTimerConfig->instance;
73 halTimerState->timerClock_Hz = halTimerConfig->srcClock_Hz;
74 s_timerHandle[halTimerState->instance] = halTimerHandle;
75 OSTIMER_Init(s_ostimerBase[halTimerState->instance]);
76 return kStatus_HAL_TimerSuccess;
77 }
78
79 /*************************************************************************************/
HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)80 void HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)
81 {
82 assert(halTimerHandle);
83 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
84 s_timerHandle[halTimerState->instance] = NULL;
85 OSTIMER_Deinit(s_ostimerBase[halTimerState->instance]);
86 }
87
88 /*************************************************************************************/
HAL_TimerEnable(hal_timer_handle_t halTimerHandle)89 void HAL_TimerEnable(hal_timer_handle_t halTimerHandle)
90 {
91 assert(halTimerHandle);
92 }
93
94 /*************************************************************************************/
HAL_TimerDisable(hal_timer_handle_t halTimerHandle)95 void HAL_TimerDisable(hal_timer_handle_t halTimerHandle)
96 {
97 assert(halTimerHandle);
98 }
99
100 /*************************************************************************************/
HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle,hal_timer_callback_t callback,void * callbackParam)101 void HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle, hal_timer_callback_t callback, void *callbackParam)
102 {
103 assert(halTimerHandle);
104 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
105 halTimerState->callback = callback;
106 halTimerState->callbackParam = callbackParam;
107 }
108
HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)109 uint32_t HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)
110 {
111 uint32_t reserveCount;
112 uint64_t retValue;
113 uint32_t reserveMs = 4U;
114 assert(halTimerHandle);
115 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
116 reserveCount = (uint32_t)MSEC_TO_COUNT((reserveMs), (halTimerState->timerClock_Hz));
117
118 retValue = COUNT_TO_USEC(((uint64_t)0xFFFFFFFF - (uint64_t)reserveCount), (uint64_t)halTimerState->timerClock_Hz);
119 return (uint32_t)((retValue > 0xFFFFFFFFU) ? (0xFFFFFFFFU - reserveMs * 1000U) : (uint32_t)retValue);
120 }
121
122 /* return micro us */
HAL_TimerGetCurrentTimerCount(hal_timer_handle_t halTimerHandle)123 uint32_t HAL_TimerGetCurrentTimerCount(hal_timer_handle_t halTimerHandle)
124 {
125 assert(halTimerHandle);
126 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
127 return (uint32_t)COUNT_TO_USEC(OSTIMER_GetCurrentTimerValue(s_ostimerBase[halTimerState->instance]),
128 halTimerState->timerClock_Hz);
129 }
130
HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle,uint32_t timeout)131 hal_timer_status_t HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle, uint32_t timeout)
132 {
133 hal_timer_handle_struct_t *halTimerState = halTimerHandle;
134 uint64_t timerTicks = OSTIMER_GetCurrentTimerValue(s_ostimerBase[halTimerState->instance]);
135 /* Translate the millisecond to ostimer count value. */
136 timerTicks += USEC_TO_COUNT(timeout, halTimerState->timerClock_Hz);
137
138 /* Set the match value with unit of ticks. */
139 OSTIMER_SetMatchValue(s_ostimerBase[halTimerState->instance], timerTicks,
140 ostimer_callback_table[halTimerState->instance]);
141 return kStatus_HAL_TimerSuccess;
142 }
143
HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)144 void HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)
145 {
146 assert(halTimerHandle);
147 }
148
HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)149 void HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)
150 {
151 assert(halTimerHandle);
152 }
153