|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2024 RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2024-11-26 hywing the first version. |
| 9 | + * |
| 10 | +*/ |
| 11 | +#include <rtthread.h> |
| 12 | + |
| 13 | +#ifdef BSP_USING_HWTIMER |
| 14 | + |
| 15 | +#define LOG_TAG "drv.hwtimer" |
| 16 | +#include <drv_log.h> |
| 17 | +#include <rtdevice.h> |
| 18 | +#include "drv_hwtimer.h" |
| 19 | +#include "fsl_ctimer.h" |
| 20 | + |
| 21 | +enum |
| 22 | +{ |
| 23 | +#ifdef BSP_USING_CTIMER0 |
| 24 | + TIM0_INDEX, |
| 25 | +#endif |
| 26 | +#ifdef BSP_USING_CTIMER1 |
| 27 | + TIM1_INDEX, |
| 28 | +#endif |
| 29 | +#ifdef BSP_USING_CTIMER2 |
| 30 | + TIM2_INDEX, |
| 31 | +#endif |
| 32 | +}; |
| 33 | + |
| 34 | +#ifdef BSP_USING_CTIMER0 |
| 35 | +#define TIM0_CONFIG \ |
| 36 | + { \ |
| 37 | + .tim_handle = CTIMER0, \ |
| 38 | + .tim_irqn = CTIMER0_IRQn, \ |
| 39 | + .name = "timer0", \ |
| 40 | + } |
| 41 | +#endif /* TIM0_CONFIG */ |
| 42 | + |
| 43 | +#ifdef BSP_USING_CTIMER1 |
| 44 | +#define TIM1_CONFIG \ |
| 45 | + { \ |
| 46 | + .tim_handle = CTIMER1, \ |
| 47 | + .tim_irqn = CTIMER1_IRQn, \ |
| 48 | + .name = "timer1", \ |
| 49 | + } |
| 50 | +#endif /* TIM1_CONFIG */ |
| 51 | + |
| 52 | +#ifdef BSP_USING_CTIMER2 |
| 53 | +#define TIM2_CONFIG \ |
| 54 | + { \ |
| 55 | + .tim_handle = CTIMER2, \ |
| 56 | + .tim_irqn = CTIMER2_IRQn, \ |
| 57 | + .name = "timer2", \ |
| 58 | + } |
| 59 | +#endif /* TIM2_CONFIG */ |
| 60 | + |
| 61 | +struct mcxa_hwtimer |
| 62 | +{ |
| 63 | + rt_hwtimer_t time_device; |
| 64 | + CTIMER_Type* tim_handle; |
| 65 | + enum IRQn tim_irqn; |
| 66 | + char* name; |
| 67 | +}; |
| 68 | + |
| 69 | +static struct mcxa_hwtimer mcxa_hwtimer_obj[] = |
| 70 | +{ |
| 71 | +#ifdef BSP_USING_CTIMER0 |
| 72 | + TIM0_CONFIG, |
| 73 | +#endif |
| 74 | + |
| 75 | +#ifdef BSP_USING_CTIMER1 |
| 76 | + TIM1_CONFIG, |
| 77 | +#endif |
| 78 | + |
| 79 | +#ifdef BSP_USING_CTIMER2 |
| 80 | + TIM2_CONFIG, |
| 81 | +#endif |
| 82 | +}; |
| 83 | + |
| 84 | +static void NVIC_Configuration(void) |
| 85 | +{ |
| 86 | +#ifdef BSP_USING_CTIMER0 |
| 87 | + EnableIRQ(CTIMER0_IRQn); |
| 88 | +#endif |
| 89 | + |
| 90 | +#ifdef BSP_USING_CTIMER1 |
| 91 | + EnableIRQ(CTIMER1_IRQn); |
| 92 | +#endif |
| 93 | + |
| 94 | +#ifdef BSP_USING_CTIMER2 |
| 95 | + EnableIRQ(CTIMER2_IRQn); |
| 96 | +#endif |
| 97 | +} |
| 98 | + |
| 99 | +static rt_err_t mcxa_ctimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args) |
| 100 | +{ |
| 101 | + rt_err_t err = RT_EOK; |
| 102 | + CTIMER_Type *hwtimer_dev; |
| 103 | + hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; |
| 104 | + |
| 105 | + RT_ASSERT(timer != RT_NULL); |
| 106 | + |
| 107 | + switch (cmd) |
| 108 | + { |
| 109 | + case HWTIMER_CTRL_FREQ_SET: |
| 110 | + { |
| 111 | + uint32_t clk; |
| 112 | + uint32_t pre; |
| 113 | + if(hwtimer_dev == CTIMER0) clk = CLOCK_GetCTimerClkFreq(0U); |
| 114 | + if(hwtimer_dev == CTIMER1) clk = CLOCK_GetCTimerClkFreq(1U); |
| 115 | + if(hwtimer_dev == CTIMER2) clk = CLOCK_GetCTimerClkFreq(2U); |
| 116 | + |
| 117 | + pre = clk / *((uint32_t *)args) - 1; |
| 118 | + |
| 119 | + hwtimer_dev->PR = pre; |
| 120 | + } |
| 121 | + break; |
| 122 | + default: |
| 123 | + err = -RT_ENOSYS; |
| 124 | + break; |
| 125 | + } |
| 126 | + return err; |
| 127 | +} |
| 128 | + |
| 129 | +static rt_uint32_t mcxa_ctimer_count_get(rt_hwtimer_t *timer) |
| 130 | +{ |
| 131 | + rt_uint32_t CurrentTimer_Count; |
| 132 | + CTIMER_Type *hwtimer_dev; |
| 133 | + hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; |
| 134 | + |
| 135 | + RT_ASSERT(timer != RT_NULL); |
| 136 | + |
| 137 | + CurrentTimer_Count = hwtimer_dev->TC; |
| 138 | + |
| 139 | + return CurrentTimer_Count; |
| 140 | +} |
| 141 | + |
| 142 | +static void mcxa_ctimer_init(rt_hwtimer_t *timer, rt_uint32_t state) |
| 143 | +{ |
| 144 | + CTIMER_Type *hwtimer_dev; |
| 145 | + ctimer_config_t cfg; |
| 146 | + hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; |
| 147 | + |
| 148 | + RT_ASSERT(timer != RT_NULL); |
| 149 | + |
| 150 | + /* Use Main clock for some of the Ctimers */ |
| 151 | + if(hwtimer_dev == CTIMER0) CLOCK_AttachClk(kFRO_HF_to_CTIMER0); |
| 152 | + if(hwtimer_dev == CTIMER1) CLOCK_AttachClk(kFRO_HF_to_CTIMER1); |
| 153 | + if(hwtimer_dev == CTIMER2) CLOCK_AttachClk(kFRO_HF_to_CTIMER2); |
| 154 | + |
| 155 | + CTIMER_Init(hwtimer_dev, &cfg); |
| 156 | + |
| 157 | + if (state == 1) |
| 158 | + { |
| 159 | + NVIC_Configuration(); |
| 160 | + CTIMER_GetDefaultConfig(&cfg); |
| 161 | + CTIMER_Init(hwtimer_dev, &cfg); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +static rt_err_t mcxa_ctimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode) |
| 166 | +{ |
| 167 | + CTIMER_Type *hwtimer_dev; |
| 168 | + hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; |
| 169 | + /* Match Configuration for Channel 0 */ |
| 170 | + ctimer_match_config_t matchCfg; |
| 171 | + |
| 172 | + RT_ASSERT(timer != RT_NULL); |
| 173 | + |
| 174 | + /* Configuration*/ |
| 175 | + matchCfg.enableCounterReset = true; |
| 176 | + matchCfg.enableCounterStop = (mode == HWTIMER_MODE_ONESHOT) ? true : false;; |
| 177 | + matchCfg.matchValue = cnt; |
| 178 | + matchCfg.outControl = kCTIMER_Output_NoAction; |
| 179 | + matchCfg.outPinInitState = false; |
| 180 | + matchCfg.enableInterrupt = true; |
| 181 | + |
| 182 | + CTIMER_SetupMatch(hwtimer_dev, kCTIMER_Match_1, &matchCfg); |
| 183 | + |
| 184 | + NVIC_Configuration(); |
| 185 | + |
| 186 | + CTIMER_StartTimer(hwtimer_dev); |
| 187 | + |
| 188 | + return RT_EOK; |
| 189 | +} |
| 190 | + |
| 191 | +static void mcxa_ctimer_stop(rt_hwtimer_t *timer) |
| 192 | +{ |
| 193 | + CTIMER_Type *hwtimer_dev; |
| 194 | + hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; |
| 195 | + |
| 196 | + RT_ASSERT(timer != RT_NULL); |
| 197 | + |
| 198 | + CTIMER_StopTimer(hwtimer_dev); |
| 199 | +} |
| 200 | + |
| 201 | +static const struct rt_hwtimer_ops mcxa_hwtimer_ops = |
| 202 | +{ |
| 203 | + .init = mcxa_ctimer_init, |
| 204 | + .start = mcxa_ctimer_start, |
| 205 | + .stop = mcxa_ctimer_stop, |
| 206 | + .count_get = mcxa_ctimer_count_get, |
| 207 | + .control = mcxa_ctimer_control, |
| 208 | +}; |
| 209 | + |
| 210 | +static const struct rt_hwtimer_info mcxa_hwtimer_info = |
| 211 | +{ |
| 212 | + 96000000, /* the maximum count frequency can be set */ |
| 213 | + 6103, /* the minimum count frequency can be set */ |
| 214 | + 0xFFFFFFFF, |
| 215 | + HWTIMER_CNTMODE_UP, |
| 216 | +}; |
| 217 | + |
| 218 | +int rt_hw_hwtimer_init(void) |
| 219 | +{ |
| 220 | + int i = 0; |
| 221 | + int result = RT_EOK; |
| 222 | + |
| 223 | + for (i = 0; i < sizeof(mcxa_hwtimer_obj) / sizeof(mcxa_hwtimer_obj[0]); i++) |
| 224 | + { |
| 225 | + mcxa_hwtimer_obj[i].time_device.info = &mcxa_hwtimer_info; |
| 226 | + mcxa_hwtimer_obj[i].time_device.ops = &mcxa_hwtimer_ops; |
| 227 | + if (rt_device_hwtimer_register(&mcxa_hwtimer_obj[i].time_device, |
| 228 | + mcxa_hwtimer_obj[i].name, mcxa_hwtimer_obj[i].tim_handle) == RT_EOK) |
| 229 | + { |
| 230 | + LOG_D("%s register success", mcxa_hwtimer_obj[i].name); |
| 231 | + } |
| 232 | + else |
| 233 | + { |
| 234 | + LOG_E("%s register failed", mcxa_hwtimer_obj[i].name); |
| 235 | + result = -RT_ERROR; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return result; |
| 240 | +} |
| 241 | + |
| 242 | +INIT_DEVICE_EXPORT(rt_hw_hwtimer_init); |
| 243 | + |
| 244 | +#ifdef BSP_USING_CTIMER0 |
| 245 | +void CTIMER0_IRQHandler(void) |
| 246 | +{ |
| 247 | + uint32_t int_stat; |
| 248 | + /* Get Interrupt status flags */ |
| 249 | + int_stat = CTIMER_GetStatusFlags(CTIMER0); |
| 250 | + /* Clear the status flags that were set */ |
| 251 | + CTIMER_ClearStatusFlags(CTIMER0, int_stat); |
| 252 | + rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM0_INDEX].time_device); |
| 253 | + |
| 254 | +} |
| 255 | +#endif /* BSP_USING_HWTIMER0 */ |
| 256 | + |
| 257 | +#ifdef BSP_USING_CTIMER1 |
| 258 | +void CTIMER1_IRQHandler(void) |
| 259 | +{ |
| 260 | + uint32_t int_stat; |
| 261 | + /* Get Interrupt status flags */ |
| 262 | + int_stat = CTIMER_GetStatusFlags(CTIMER1); |
| 263 | + /* Clear the status flags that were set */ |
| 264 | + CTIMER_ClearStatusFlags(CTIMER1, int_stat); |
| 265 | + rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM1_INDEX].time_device); |
| 266 | + |
| 267 | +} |
| 268 | +#endif /* BSP_USING_HWTIMER1 */ |
| 269 | + |
| 270 | +#ifdef BSP_USING_CTIMER2 |
| 271 | +void CTIMER2_IRQHandler(void) |
| 272 | +{ |
| 273 | + uint32_t int_stat; |
| 274 | + /* Get Interrupt status flags */ |
| 275 | + int_stat = CTIMER_GetStatusFlags(CTIMER2); |
| 276 | + /* Clear the status flags that were set */ |
| 277 | + CTIMER_ClearStatusFlags(CTIMER2, int_stat); |
| 278 | + rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM2_INDEX].time_device); |
| 279 | + |
| 280 | +} |
| 281 | +#endif /* BSP_USING_HWTIMER2 */ |
| 282 | + |
| 283 | + |
| 284 | +#endif /* BSP_USING_HWTIMER */ |
0 commit comments