rt-thread-official/bsp/nuclei/libraries/gd32vf103/HAL_Drivers/drv_hwtimer.c

316 lines
7.4 KiB
C
Raw Permalink Normal View History

bsp/nuclei: Add more drivers support for gd32vf103_rvstar board Squashed commit of the following: commit 32dd349ccff85cdcad81021b5185f8f7347786db Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 16:08:08 2020 +0800 bsp/nuclei: Add more documentation about how to use RT-Thread in RV-STAR Signed-off-by: Huaqi Fang <578567190@qq.com> commit c9474c20a558e9d7934a3d72cc9bb17a98c20871 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 14:06:42 2020 +0800 bsp/nuclei: remove comments in rt_hw_*_drvinit Signed-off-by: Huaqi Fang <578567190@qq.com> commit 89b5caa2c3e55dc352b13f8e33229f03351b7049 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 13:52:12 2020 +0800 bsp/nuclei: Add a entry README.md for nuclei bsp Signed-off-by: Huaqi Fang <578567190@qq.com> commit ae50d6e9bdc92ca4767c5ac7d0ded7252314a42e Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 11:52:52 2020 +0800 bsp/nuclei: update README.md cover more pinmux section Signed-off-by: Huaqi Fang <578567190@qq.com> commit 1a42d85dfe5bc7b009f057784249d54d0c82de0f Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 08:52:06 2020 +0800 bsp/nuclei: clean up unused code in drv_spi.c Signed-off-by: Huaqi Fang <578567190@qq.com> commit b40edcdf59c96ef261f65a0c023a24ac78cabb39 Author: Huaqi Fang <578567190@qq.com> Date: Wed Jun 10 14:09:15 2020 +0800 bsp/nuclei: Format the code comments for hw pinmux init Signed-off-by: Huaqi Fang <578567190@qq.com> commit c8ae9fdfdb989bbff85d911fee3124abd9d145db Author: Huaqi Fang <578567190@qq.com> Date: Mon Jun 8 17:12:43 2020 +0800 bsp/nuclei: Add more drivers support for gd32vf103 * More drivers are supported for rvstar board, see README.md * If user want to use these supported drivers, menuconfig is required * User also need to setup pinmux for its coresponding driver in board/board.c Signed-off-by: Huaqi Fang <578567190@qq.com> Signed-off-by: Huaqi Fang <578567190@qq.com>
2020-06-16 09:35:58 +08:00
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-05-12 hqfang first version
*/
#include "drv_hwtimer.h"
#ifdef BSP_USING_HWTIMER
#if !defined(BSP_USING_HWTIMER0) && !defined(BSP_USING_HWTIMER1) && !defined(BSP_USING_HWTIMER2) \
&& !defined(BSP_USING_HWTIMER3) && !defined(BSP_USING_HWTIMER4)
#error "Please define at least one BSP_USING_HWTIMERx"
/* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable HWTIMER */
#endif
static struct gd32_hwtimer_config hwtimer_config[] =
{
#ifdef BSP_USING_HWTIMER0
{
"timer0",
TIMER0,
TIMER0_UP_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER1
{
"timer1",
TIMER1,
TIMER1_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER2
{
"timer2",
TIMER2,
TIMER2_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER3
{
"timer3",
TIMER3,
TIMER3_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER4
{
"timer4",
TIMER4,
TIMER4_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER5
{
"timer5",
TIMER5,
TIMER5_IRQn,
},
#endif
#ifdef BSP_USING_HWTIMER6
{
"timer6",
TIMER6,
TIMER6_IRQn,
},
#endif
};
static struct gd32_hwtimer hwtimer_obj[sizeof(hwtimer_config) / sizeof(hwtimer_config[0])] = {0};
static rt_err_t gd32_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
{
rt_err_t err = RT_EOK;
struct gd32_hwtimer_config *config;
RT_ASSERT(timer != RT_NULL);
config = (struct gd32_hwtimer_config *)timer->parent.user_data;
switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
{
uint32_t clk;
uint8_t clkpre;
uint32_t pre;
if (config->timer_periph != TIMER0)
{
clk = rcu_clock_freq_get(CK_APB1);
clkpre = GET_BITS(RCU_CFG0, 8, 10);
}
else
{
clk = rcu_clock_freq_get(CK_APB2);
clkpre = GET_BITS(RCU_CFG0, 11, 13);
}
if (clkpre >= 4)
{
clk = clk * 2;
}
pre = (clk / * ((uint32_t *)args)) - 1;
TIMER_PSC(config->timer_periph) = (uint32_t)pre;
}
break;
case HWTIMER_CTRL_STOP:
timer_disable(config->timer_periph);
break;
default:
err = -RT_ENOSYS;
break;
}
return err;
}
static rt_uint32_t gd32_hwtimer_count_get(rt_hwtimer_t *timer)
{
rt_uint32_t CurrentTimer_Count;
struct gd32_hwtimer_config *config;
RT_ASSERT(timer != RT_NULL);
config = (struct gd32_hwtimer_config *)timer->parent.user_data;
CurrentTimer_Count = timer_counter_read(config->timer_periph);
return CurrentTimer_Count;
}
static void gd32_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
struct gd32_hwtimer_config *config;
timer_parameter_struct initpara;
RT_ASSERT(timer != RT_NULL);
config = (struct gd32_hwtimer_config *)timer->parent.user_data;
if (state == 1)
{
timer_deinit(config->timer_periph);
timer_struct_para_init(&initpara);
timer_init(config->timer_periph, &initpara);
}
else
{
timer_disable(config->timer_periph);
timer_interrupt_enable(config->timer_periph, TIMER_INT_FLAG_UP);
ECLIC_DisableIRQ(config->irqn);
}
}
static rt_err_t gd32_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
{
struct gd32_hwtimer_config *config;
RT_ASSERT(timer != RT_NULL);
config = (struct gd32_hwtimer_config *)timer->parent.user_data;
if (mode == HWTIMER_MODE_ONESHOT)
{
timer_single_pulse_mode_config(config->timer_periph, TIMER_SP_MODE_SINGLE);
}
else
{
timer_single_pulse_mode_config(config->timer_periph, TIMER_SP_MODE_REPETITIVE);
}
timer_counter_value_config(config->timer_periph, 0);
timer_autoreload_value_config(config->timer_periph, cnt);
timer_interrupt_enable(config->timer_periph, TIMER_INT_FLAG_UP);
timer_enable(config->timer_periph);
ECLIC_EnableIRQ(config->irqn);
return RT_EOK;
}
static void gd32_hwtimer_stop(rt_hwtimer_t *timer)
{
struct gd32_hwtimer_config *config;
RT_ASSERT(timer != RT_NULL);
config = (struct gd32_hwtimer_config *)timer->parent.user_data;
timer_disable(config->timer_periph);
ECLIC_DisableIRQ(config->irqn);
}
static const struct rt_hwtimer_ops gd32_hwtimer_ops =
{
.init = gd32_hwtimer_init,
.start = gd32_hwtimer_start,
.stop = gd32_hwtimer_stop,
.count_get = gd32_hwtimer_count_get,
.control = gd32_hwtimer_control,
};
static const struct rt_hwtimer_info gd32_hwtimer_info =
{
54000000, /* the maximum count frequency can be set */
1000, /* the minimum count frequency can be set */
0xFFFF,
HWTIMER_CNTMODE_UP,
};
#ifdef BSP_USING_HWTIMER0
void TIMER0_UP_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[0].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[0].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER1
void TIMER1_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[1].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[1].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER2
void TIMER2_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[2].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[2].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER3
void TIMER3_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[3].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[3].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER4
void TIMER4_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[4].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[4].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER5
void TIMER5_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[5].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[5].time_device);
}
#endif
#ifdef BSP_USING_HWTIMER6
void TIMER6_IRQHandler(void)
{
timer_interrupt_flag_clear(hwtimer_obj[6].config->timer_periph, TIMER_INT_FLAG_UP);
rt_device_hwtimer_isr(&hwtimer_obj[6].time_device);
}
#endif
static int rt_hwtimer_init(void)
{
int i = 0;
int result = RT_EOK;
#ifdef BSP_USING_HWTIMER0
rcu_periph_clock_enable(RCU_TIMER0);
#endif
#ifdef BSP_USING_HWTIMER1
rcu_periph_clock_enable(RCU_TIMER1);
#endif
#ifdef BSP_USING_HWTIMER2
rcu_periph_clock_enable(RCU_TIMER2);
#endif
#ifdef BSP_USING_HWTIMER3
rcu_periph_clock_enable(RCU_TIMER3);
#endif
#ifdef BSP_USING_HWTIMER4
rcu_periph_clock_enable(RCU_TIMER4);
#endif
#ifdef BSP_USING_HWTIMER5
rcu_periph_clock_enable(RCU_TIMER5);
#endif
#ifdef BSP_USING_HWTIMER6
rcu_periph_clock_enable(RCU_TIMER6);
#endif
for (i = 0; i < sizeof(hwtimer_obj) / sizeof(hwtimer_obj[0]); i++)
{
hwtimer_obj[i].time_device.info = &gd32_hwtimer_info;
hwtimer_obj[i].time_device.ops = &gd32_hwtimer_ops;
hwtimer_obj[i].config = &hwtimer_config[i];
rt_device_hwtimer_register(&hwtimer_obj[i].time_device, \
hwtimer_obj[i].config->name, hwtimer_obj[i].config);
}
return result;
}
INIT_DEVICE_EXPORT(rt_hwtimer_init);
#endif /* RT_USING_HWTIMER */