1 /*
2  * Copyright 2018-2019 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_gpt.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 
27 static GPT_Type *const s_GptBase[] = GPT_BASE_PTRS;
28 
29 static hal_timer_handle_t s_timerHandle[sizeof(s_GptBase) / sizeof(GPT_Type *)];
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     GPT_ClearStatusFlags(s_GptBase[halTimerState->instance], kGPT_OutputCompare1Flag);
46     if (halTimerState->callback != NULL)
47     {
48         halTimerState->callback(halTimerState->callbackParam);
49     }
50 }
51 
52 void GPT0_IRQHandler(void);
GPT0_IRQHandler(void)53 void GPT0_IRQHandler(void)
54 {
55     HAL_TimerInterruptHandle(0);
56     SDK_ISR_EXIT_BARRIER;
57 }
58 
59 void GPT1_IRQHandler(void);
GPT1_IRQHandler(void)60 void GPT1_IRQHandler(void)
61 {
62     HAL_TimerInterruptHandle(1);
63     SDK_ISR_EXIT_BARRIER;
64 }
65 void GPT2_IRQHandler(void);
GPT2_IRQHandler(void)66 void GPT2_IRQHandler(void)
67 {
68     HAL_TimerInterruptHandle(2);
69     SDK_ISR_EXIT_BARRIER;
70 }
71 /************************************************************************************
72 *************************************************************************************
73 * Public functions
74 *************************************************************************************
75 ************************************************************************************/
HAL_TimerInit(hal_timer_handle_t halTimerHandle,hal_timer_config_t * halTimerConfig)76 hal_timer_status_t HAL_TimerInit(hal_timer_handle_t halTimerHandle, hal_timer_config_t *halTimerConfig)
77 {
78     IRQn_Type instanceIrq[] = GPT_IRQS;
79     IRQn_Type irqId;
80     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
81     /* Structure of initialize GPT */
82     gpt_config_t gptConfig;
83 
84     assert(sizeof(hal_timer_handle_struct_t) == HAL_TIMER_HANDLE_SIZE);
85     assert(halTimerConfig);
86     assert(halTimerHandle);
87     assert(halTimerConfig->instance < (sizeof(s_GptBase) / sizeof(GPT_Type *)));
88 
89     halTimerState->timeout       = halTimerConfig->timeout;
90     halTimerState->instance      = halTimerConfig->instance;
91     halTimerState->timerClock_Hz = halTimerConfig->srcClock_Hz;
92     irqId                        = instanceIrq[halTimerState->instance];
93     GPT_GetDefaultConfig(&gptConfig);
94 
95     /* Initialize GPT module */
96     GPT_Init(s_GptBase[halTimerState->instance], &gptConfig);
97     /* Set both GPT modules to 1 second duration */
98     GPT_SetOutputCompareValue(s_GptBase[halTimerState->instance], kGPT_OutputCompare_Channel1,
99                               (uint32_t)USEC_TO_COUNT(halTimerState->timeout, halTimerState->timerClock_Hz));
100     /* Enable GPT Output Compare1 interrupt */
101     GPT_EnableInterrupts(s_GptBase[halTimerState->instance], (uint32_t)kGPT_OutputCompare1InterruptEnable);
102 
103     s_timerHandle[halTimerState->instance] = halTimerHandle;
104     NVIC_SetPriority((IRQn_Type)irqId, HAL_TIMER_ISR_PRIORITY);
105     (void)EnableIRQ(irqId);
106     return kStatus_HAL_TimerSuccess;
107 }
108 
109 /*************************************************************************************/
HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)110 void HAL_TimerDeinit(hal_timer_handle_t halTimerHandle)
111 {
112     assert(halTimerHandle);
113     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
114     s_timerHandle[halTimerState->instance]   = NULL;
115     GPT_Deinit(s_GptBase[halTimerState->instance]);
116 }
117 
118 /*************************************************************************************/
HAL_TimerEnable(hal_timer_handle_t halTimerHandle)119 void HAL_TimerEnable(hal_timer_handle_t halTimerHandle)
120 {
121     assert(halTimerHandle);
122     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
123     GPT_StartTimer(s_GptBase[halTimerState->instance]);
124 }
125 
126 /*************************************************************************************/
HAL_TimerDisable(hal_timer_handle_t halTimerHandle)127 void HAL_TimerDisable(hal_timer_handle_t halTimerHandle)
128 {
129     assert(halTimerHandle);
130     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
131     GPT_StopTimer(s_GptBase[halTimerState->instance]);
132 }
133 
134 /*************************************************************************************/
HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle,hal_timer_callback_t callback,void * callbackParam)135 void HAL_TimerInstallCallback(hal_timer_handle_t halTimerHandle, hal_timer_callback_t callback, void *callbackParam)
136 {
137     assert(halTimerHandle);
138     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
139     halTimerState->callback                  = callback;
140     halTimerState->callbackParam             = callbackParam;
141 }
142 
HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)143 uint32_t HAL_TimerGetMaxTimeout(hal_timer_handle_t halTimerHandle)
144 {
145     uint32_t reserveCount;
146     assert(halTimerHandle);
147     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
148     reserveCount                             = (uint32_t)MSEC_TO_COUNT((4), (halTimerState->timerClock_Hz));
149     if ((reserveCount < MSEC_TO_COUNT((1), (halTimerState->timerClock_Hz))) ||
150         (reserveCount > (GPT_CNT_COUNT_MASK - 0xfU)))
151     {
152         /* make sure 1ms timeout doesn't overflow */
153         assert(USEC_TO_COUNT(1000, (halTimerState->timerClock_Hz)) < GPT_CNT_COUNT_MASK);
154         return 1000;
155     }
156 
157     return (uint32_t)COUNT_TO_USEC(((uint64_t)GPT_CNT_COUNT_MASK - (uint64_t)reserveCount),
158                                    halTimerState->timerClock_Hz);
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((uint64_t)GPT_GetCurrentTimerCount(s_GptBase[halTimerState->instance]),
166                                    halTimerState->timerClock_Hz);
167 }
168 
HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle,uint32_t timeout)169 hal_timer_status_t HAL_TimerUpdateTimeout(hal_timer_handle_t halTimerHandle, uint32_t timeout)
170 {
171     uint32_t tickCount;
172     assert(halTimerHandle);
173     hal_timer_handle_struct_t *halTimerState = halTimerHandle;
174     halTimerState->timeout                   = timeout;
175     tickCount = (uint32_t)USEC_TO_COUNT(halTimerState->timeout, halTimerState->timerClock_Hz);
176 
177     if ((tickCount < 1U) || (tickCount > (GPT_CNT_COUNT_MASK - 0xfU)))
178     {
179         return kStatus_HAL_TimerOutOfRanger;
180     }
181     GPT_SetOutputCompareValue(s_GptBase[halTimerState->instance], kGPT_OutputCompare_Channel1, tickCount);
182     return kStatus_HAL_TimerSuccess;
183 }
184 
HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)185 void HAL_TimerExitLowpower(hal_timer_handle_t halTimerHandle)
186 {
187     assert(halTimerHandle);
188 }
189 
HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)190 void HAL_TimerEnterLowpower(hal_timer_handle_t halTimerHandle)
191 {
192     assert(halTimerHandle);
193 }
194