drv_hwtimer.c 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-08-20     breo.com     first version
 */

#include <board.h>
#include "drv_hwtimer.h"

#define DRV_DEBUG
#define LOG_TAG             "drv.hwtimer"
#include <drv_log.h>

#ifdef BSP_USING_HWTIMER
enum
{
#ifdef BSP_USING_HWTIM1
    TIM1_INDEX,
#endif

#ifdef BSP_USING_HWTIM2
    TIM2_INDEX,
#endif

#ifdef BSP_USING_HWTIM3
    TIM3_INDEX,
#endif

#ifdef BSP_USING_HWTIM4
    TIM4_INDEX,
#endif

#ifdef BSP_USING_HWTIM5
    TIM5_INDEX,
#endif

#ifdef BSP_USING_HWTIM6
    TIM6_INDEX,
#endif

#ifdef BSP_USING_HWTIM7
    TIM7_INDEX,
#endif

#ifdef BSP_USING_HW_TIM8
    TIM8_INDEX,
#endif

#ifdef BSP_USING_HWTIM9
    TIM9_INDEX,
#endif

#ifdef BSP_USING_HWTIM10
    TIM10_INDEX,
#endif

#ifdef BSP_USING_HWTIM11
    TIM11_INDEX,
#endif

#ifdef BSP_USING_HWTIM12
    TIM12_INDEX,
#endif

#ifdef BSP_USING_HWTIM13
    TIM13_INDEX,
#endif

#ifdef BSP_USING_HWTIM14
    TIM14_INDEX,
#endif

#ifdef BSP_USING_HWTIM15
    TIM15_INDEX,
#endif
};

struct n32_hwtimer
{
    rt_hwtimer_t    time_device;
85
    TIM_Module     *tim_handle;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    IRQn_Type       tim_irqn;
    char            *name;
};

static struct n32_hwtimer n32_hwtimer_obj[] =
{
#ifdef BSP_USING_HWTIM1
    TIM1_CONFIG,
#endif

#ifdef BSP_USING_HWTIM2
    TIM2_CONFIG,
#endif

#ifdef BSP_USING_HWTIM3
    TIM3_CONFIG,
#endif

#ifdef BSP_USING_HWTIM4
    TIM4_CONFIG,
#endif

#ifdef BSP_USING_HWTIM5
    TIM5_CONFIG,
#endif

#ifdef BSP_USING_HWTIM6
    TIM6_CONFIG,
#endif

#ifdef BSP_USING_HWTIM7
    TIM7_CONFIG,
#endif

#ifdef BSP_USING_HWTIM8
    TIM8_CONFIG,
#endif

#ifdef BSP_USING_HWTIM9
    TIM9_CONFIG,
#endif

#ifdef BSP_USING_HWTIM10
    TIM10_CONFIG,
#endif

#ifdef BSP_USING_HWTIM11
    TIM11_CONFIG,
#endif

#ifdef BSP_USING_HWTIM12
    TIM12_CONFIG,
#endif

#ifdef BSP_USING_HWTIM13
    TIM13_CONFIG,
#endif

#ifdef BSP_USING_HWTIM14
    TIM14_CONFIG,
#endif

#ifdef BSP_USING_HWTIM15
    TIM15_CONFIG,
#endif
};

static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
{
    RCC_ClocksType RCC_ClockStruct;
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    NVIC_InitType NVIC_InitStructure;
    uint32_t prescaler_value = 0;
    TIM_Module *tim = RT_NULL;
    struct n32_hwtimer *tim_device = RT_NULL;

    RT_ASSERT(timer != RT_NULL);
    if (state)
    {
        tim = (TIM_Module *)timer->parent.user_data;
        tim_device = (struct n32_hwtimer *)timer;

        /* timer clock enable */
        n32_msp_hwtim_init(tim);

        /* timer init */
        RCC_GetClocksFreqValue(&RCC_ClockStruct);
        /* Set timer clock is 1Mhz */
        prescaler_value = (uint32_t)(RCC_ClockStruct.SysclkFreq / 10000) - 1;

        TIM_TimeBaseStructure.Period = 10000 - 1;
        rt_kprintf("Period=[%d]", TIM_TimeBaseStructure.Period);
        TIM_TimeBaseStructure.Prescaler = prescaler_value;
        rt_kprintf("Prescaler=[%d]", TIM_TimeBaseStructure.Prescaler);
        TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
        TIM_TimeBaseStructure.RepetCnt = 0;

        if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
        {
            TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
        }
        else
        {
            TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_DOWN;
        }

        TIM_InitTimeBase(tim, &TIM_TimeBaseStructure);

        /* Enable the TIMx global Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = tim_device->tim_irqn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

201
        TIM_ConfigInt(tim, TIM_INT_UPDATE, ENABLE);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        TIM_ClrIntPendingBit(tim, TIM_INT_UPDATE);

        LOG_D("%s init success", tim_device->name);
    }
}

static rt_err_t n32_timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
    rt_err_t result = RT_EOK;
    TIM_Module *tim = RT_NULL;

    RT_ASSERT(timer != RT_NULL);

    tim = (TIM_Module *)timer->parent.user_data;

    /* set tim cnt */
    TIM_SetCnt(tim, 0);
    /* set tim arr */
    TIM_SetAutoReload(tim, t - 1);
    if (opmode == HWTIMER_MODE_ONESHOT)
    {
        /* set timer to single mode */
        TIM_SelectOnePulseMode(tim, TIM_OPMODE_SINGLE);
    }
    else
    {
        TIM_SelectOnePulseMode(tim, TIM_OPMODE_REPET);
    }

    /* start timer */
    TIM_Enable(tim, ENABLE);

    return result;
}

static void n32_timer_stop(rt_hwtimer_t *timer)
{
    TIM_Module *tim = RT_NULL;

    RT_ASSERT(timer != RT_NULL);

    tim = (TIM_Module *)timer->parent.user_data;

    /* stop timer */
    TIM_Enable(tim, DISABLE);
    /* set tim cnt */
    TIM_SetCnt(tim, 0);
}

static rt_uint32_t n32_timer_counter_get(rt_hwtimer_t *timer)
{
    TIM_Module *tim = RT_NULL;

    RT_ASSERT(timer != RT_NULL);

    tim = (TIM_Module *)timer->parent.user_data;

    return tim->CNT;
}

static rt_err_t n32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
{
    RCC_ClocksType RCC_ClockStruct;
    TIM_Module *tim = RT_NULL;
    rt_err_t result = RT_EOK;

    RT_ASSERT(timer != RT_NULL);
    RT_ASSERT(arg != RT_NULL);

    tim = (TIM_Module *)timer->parent.user_data;

273
    switch (cmd)
274
    {
275 276 277 278
    case HWTIMER_CTRL_FREQ_SET:
    {
        rt_uint32_t freq;
        rt_uint16_t val;
279

280 281
        /* set timer frequence */
        freq = *((rt_uint32_t *)arg);
282

283 284
        /* time init */
        RCC_GetClocksFreqValue(&RCC_ClockStruct);
285

286
        val = RCC_ClockStruct.SysclkFreq / freq;
287

288 289 290 291 292 293 294 295
        TIM_ConfigPrescaler(tim, val - 1, TIM_PSC_RELOAD_MODE_IMMEDIATE);
    }
    break;
    default:
    {
        result = -RT_ENOSYS;
    }
    break;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    }

    return result;
}

static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
static const struct rt_hwtimer_ops _ops =
{
    .init      = n32_timer_init,
    .start     = n32_timer_start,
    .stop      = n32_timer_stop,
    .count_get = n32_timer_counter_get,
    .control   = n32_timer_ctrl,
};

#ifdef BSP_USING_HWTIM2
void TIM2_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

317
    if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) == SET)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM2_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIM3
void TIM3_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

335
    if (TIM_GetIntStatus(TIM3, TIM_INT_UPDATE) == SET)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM3_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM3, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIM4
void TIM4_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

353
    if (TIM_GetIntStatus(TIM4, TIM_INT_UPDATE) == SET)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM4_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM4, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIM5
void TIM5_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

371
    if (TIM_GetIntStatus(TIM5, TIM_INT_UPDATE) == SET)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM5_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM5, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIM6
void TIM6_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

389
    if (TIM_GetIntStatus(TIM6, TIM_INT_UPDATE) == SET)
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM6_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIM7
void TIM7_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

407
    if (TIM_GetIntStatus(TIM7, TIM_INT_UPDATE) == SET)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    {

        rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM7_INDEX].time_device);
        TIM_ClrIntPendingBit(TIM7, TIM_INT_UPDATE);

    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

static int rt_hw_hwtimer_init(void)
{
    int i = 0;
    int result = RT_EOK;

    for (i = 0; i < sizeof(n32_hwtimer_obj) / sizeof(n32_hwtimer_obj[0]); i++)
    {
        n32_hwtimer_obj[i].time_device.info = &_info;
        n32_hwtimer_obj[i].time_device.ops  = &_ops;
        if (rt_device_hwtimer_register(&n32_hwtimer_obj[i].time_device, n32_hwtimer_obj[i].name, n32_hwtimer_obj[i].tim_handle) == RT_EOK)
        {
            LOG_D("%s register success", n32_hwtimer_obj[i].name);
        }
        else
        {
            LOG_E("%s register failed", n32_hwtimer_obj[i].name);
            result = -RT_ERROR;
        }
    }

    return result;
}
INIT_BOARD_EXPORT(rt_hw_hwtimer_init);

#endif /* BSP_USING_HWTIMER */