drv_hwtimer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-23 yuzrain the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <drv_hwtimer.h>
  14. #include <board.h>
  15. #include "md_ad16c4t.h"
  16. #include "md_rcu.h"
  17. #ifdef RT_USING_HWTIMER
  18. /* Defien the hardware timer control struct */
  19. struct es32f0_hwtimer_dev
  20. {
  21. rt_hwtimer_t parent;
  22. AD16C4T_TypeDef *hwtimer_periph;
  23. IRQn_Type IRQn;
  24. };
  25. #ifdef BSP_USING_HWTIMER1
  26. static struct es32f0_hwtimer_dev hwtimer1;
  27. void BS16T1_IRQHandler(void)
  28. {
  29. /* if BS16T1 IT */
  30. if (BS16T1->IFM & AD16C4T_RIF_UI_MSK)
  31. {
  32. SET_BIT(BS16T1->ICR, AD16C4T_ICR_UI_MSK);
  33. rt_device_hwtimer_isr(&hwtimer1.parent);
  34. if (HWTIMER_MODE_ONESHOT == hwtimer1.parent.mode)
  35. {
  36. CLEAR_BIT(BS16T1->CON1, AD16C4T_CON1_CNTEN_MSK);
  37. }
  38. }
  39. }
  40. #endif
  41. static struct rt_hwtimer_info es32f0_hwtimer_info =
  42. {
  43. 48000000, /* maximum count frequency */
  44. 1, /* minimum count frequency */
  45. 65535, /* counter maximum value */
  46. HWTIMER_CNTMODE_UP
  47. };
  48. static void es32f0_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  49. {
  50. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  51. RT_ASSERT(hwtimer != RT_NULL);
  52. if (1 == state)
  53. {
  54. /* Set frequency */
  55. WRITE_REG(hwtimer->hwtimer_periph->PRES, (SystemCoreClock/hwtimer->parent.freq - 1));
  56. /* Enable timer IT */
  57. SET_BIT(hwtimer->hwtimer_periph->IER, AD16C4T_IER_UI_MSK);
  58. NVIC_EnableIRQ(hwtimer->IRQn);
  59. }
  60. else
  61. {
  62. /* Dsiable timer IT */
  63. SET_BIT(hwtimer->hwtimer_periph->IDR, AD16C4T_IER_UI_MSK);
  64. }
  65. }
  66. static rt_err_t es32f0_hwtimer_start(rt_hwtimer_t *timer,
  67. rt_uint32_t cnt,
  68. rt_hwtimer_mode_t mode)
  69. {
  70. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  71. RT_ASSERT(hwtimer != RT_NULL);
  72. WRITE_REG(hwtimer->hwtimer_periph->AR, cnt);
  73. SET_BIT(hwtimer->hwtimer_periph->CON1, AD16C4T_CON1_CNTEN_MSK);
  74. return RT_EOK;
  75. }
  76. static void es32f0_hwtimer_stop(rt_hwtimer_t *timer)
  77. {
  78. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  79. RT_ASSERT(hwtimer != RT_NULL);
  80. CLEAR_BIT(hwtimer->hwtimer_periph->CON1, AD16C4T_CON1_CNTEN_MSK);
  81. }
  82. static rt_uint32_t es32f0_hwtimer_count_get(rt_hwtimer_t *timer)
  83. {
  84. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  85. uint32_t hwtimer_count = 0;
  86. RT_ASSERT(hwtimer != RT_NULL);
  87. hwtimer_count = READ_REG(hwtimer->hwtimer_periph->COUNT);
  88. return hwtimer_count;
  89. }
  90. static rt_err_t es32f0_hwtimer_control(rt_hwtimer_t *timer,
  91. rt_uint32_t cmd,
  92. void *args)
  93. {
  94. rt_err_t ret = RT_EOK;
  95. rt_uint32_t freq = 0;
  96. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  97. RT_ASSERT(hwtimer != RT_NULL);
  98. switch (cmd)
  99. {
  100. case HWTIMER_CTRL_FREQ_SET:
  101. freq = *(rt_uint32_t *)args;
  102. if ((freq < hwtimer->parent.info->minfreq) || (freq > hwtimer->parent.info->maxfreq))
  103. {
  104. ret = RT_EINVAL;
  105. }
  106. /* Set frequency */
  107. WRITE_REG(hwtimer->hwtimer_periph->PRES, (SystemCoreClock/freq - 1));
  108. break;
  109. case HWTIMER_CTRL_STOP:
  110. CLEAR_BIT(hwtimer->hwtimer_periph->CON1, AD16C4T_CON1_CNTEN_MSK);
  111. break;
  112. default:
  113. ret = RT_EINVAL;
  114. break;
  115. }
  116. return ret;
  117. }
  118. static struct rt_hwtimer_ops es32f0_hwtimer_ops =
  119. {
  120. es32f0_hwtimer_init,
  121. es32f0_hwtimer_start,
  122. es32f0_hwtimer_stop,
  123. es32f0_hwtimer_count_get,
  124. es32f0_hwtimer_control
  125. };
  126. int rt_hw_hwtimer_init(void)
  127. {
  128. rt_err_t ret = RT_EOK;
  129. #ifdef BSP_USING_HWTIMER1
  130. /*Open clock*/
  131. SET_BIT(RCU->APB1EN, RCU_APB1EN_BS16T1EN_MSK);
  132. hwtimer1.hwtimer_periph = BS16T1;
  133. hwtimer1.IRQn = BS16T1_IRQn;
  134. hwtimer1.parent.info = &es32f0_hwtimer_info;
  135. hwtimer1.parent.ops = &es32f0_hwtimer_ops;
  136. ret = rt_device_hwtimer_register(&hwtimer1.parent, "timer1", &hwtimer1);
  137. #endif
  138. return ret;
  139. }
  140. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  141. #endif