timer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_TIMER_H
  16. #define _DRIVER_TIMER_H
  17. #include <stdint.h>
  18. #include <stddef.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* clang-format off */
  23. struct timer_channel_t
  24. {
  25. /* TIMER_N Load Count Register (0x00+(N-1)*0x14) */
  26. volatile uint32_t load_count;
  27. /* TIMER_N Current Value Register (0x04+(N-1)*0x14) */
  28. volatile uint32_t current_value;
  29. /* TIMER_N Control Register (0x08+(N-1)*0x14) */
  30. volatile uint32_t control;
  31. /* TIMER_N Interrupt Clear Register (0x0c+(N-1)*0x14) */
  32. volatile uint32_t eoi;
  33. /* TIMER_N Interrupt Status Register (0x10+(N-1)*0x14) */
  34. volatile uint32_t intr_stat;
  35. } __attribute__((packed, aligned(4)));
  36. struct timer_t
  37. {
  38. /* TIMER_N Register (0x00-0x4c) */
  39. volatile struct timer_channel_t channel[4];
  40. /* reserverd (0x50-0x9c) */
  41. volatile uint32_t resv1[20];
  42. /* TIMER Interrupt Status Register (0xa0) */
  43. volatile uint32_t intr_stat;
  44. /* TIMER Interrupt Clear Register (0xa4) */
  45. volatile uint32_t eoi;
  46. /* TIMER Raw Interrupt Status Register (0xa8) */
  47. volatile uint32_t raw_intr_stat;
  48. /* TIMER Component Version Register (0xac) */
  49. volatile uint32_t comp_version;
  50. /* TIMER_N Load Count2 Register (0xb0-0xbc) */
  51. volatile uint32_t load_count2[4];
  52. } __attribute__((packed, aligned(4)));
  53. /* TIMER Control Register */
  54. #define TIMER_CR_ENABLE 0x00000001
  55. #define TIMER_CR_MODE_MASK 0x00000002
  56. #define TIMER_CR_FREE_MODE 0x00000000
  57. #define TIMER_CR_USER_MODE 0x00000002
  58. #define TIMER_CR_INTERRUPT_MASK 0x00000004
  59. #define TIMER_CR_PWM_ENABLE 0x00000008
  60. /* clang-format on */
  61. extern volatile struct timer_t *const timer[3];
  62. /**
  63. * @brief Set timer clock frequency
  64. *
  65. * @param[in] timer timer
  66. * @param[in] div clock divide value
  67. */
  68. void timer_set_clock_div(uint32_t timer, uint32_t div);
  69. /**
  70. * @brief Enable timer channel
  71. *
  72. * @param[in] timer timer
  73. * @param[in] channel channel
  74. */
  75. void timer_enable(uint32_t timer, uint32_t channel);
  76. /**
  77. * @brief Disable timer channel
  78. *
  79. * @param[in] timer timer
  80. * @param[in] channel channel
  81. */
  82. void timer_disable(uint32_t timer, uint32_t channel);
  83. /**
  84. * @brief Enable timer channel PWM
  85. *
  86. * @param[in] timer timer
  87. * @param[in] channel channel
  88. */
  89. void timer_enable_pwm(uint32_t timer, uint32_t channel);
  90. /**
  91. * @brief Disable timer channel PWM
  92. *
  93. * @param[in] timer timer
  94. * @param[in] channel channel
  95. */
  96. void timer_disable_pwm(uint32_t timer, uint32_t channel);
  97. /**
  98. * @brief Enable timer channel interrupt
  99. *
  100. * @param[in] timer timer
  101. * @param[in] channel channel
  102. */
  103. void timer_enable_interrupt(uint32_t timer, uint32_t channel);
  104. /**
  105. * @brief Disable timer channel interrupt
  106. *
  107. * @param[in] timer timer
  108. * @param[in] channel channel
  109. */
  110. void timer_disable_interrupt(uint32_t timer, uint32_t channel);
  111. /**
  112. * @brief Set timer channel mode
  113. *
  114. * @param[in] timer timer
  115. * @param[in] channel channel
  116. * @param[in] mode mode
  117. */
  118. void timer_set_mode(uint32_t timer, uint32_t channel, uint32_t mode);
  119. /**
  120. * @brief Set timer channel reload value
  121. *
  122. * @param[in] timer timer
  123. * @param[in] channel channel
  124. * @param[in] count count
  125. */
  126. void timer_set_reload(uint32_t timer, uint32_t channel, uint32_t count);
  127. /**
  128. * @brief Set timer channel reload value2
  129. *
  130. * @param[in] timer timer
  131. * @param[in] channel channel
  132. * @param[in] count count
  133. */
  134. void timer_set_reload2(uint32_t timer, uint32_t channel, uint32_t count);
  135. /**
  136. * @brief Get timer channel count
  137. *
  138. * @param[in] timer timer
  139. * @param[in] channel channel
  140. *
  141. * @return current value
  142. */
  143. uint32_t timer_get_count(uint32_t timer, uint32_t channel);
  144. /**
  145. * @brief Get timer channel reload value
  146. *
  147. * @param[in] timer timer
  148. * @param[in] channel channel
  149. *
  150. * @return reload value
  151. */
  152. uint32_t timer_get_reload(uint32_t timer, uint32_t channel);
  153. /**
  154. * @brief Get timer channel reload value2
  155. *
  156. * @param[in] timer timer
  157. * @param[in] channel channel
  158. *
  159. * @return reload value2
  160. */
  161. uint32_t timer_get_reload2(uint32_t timer, uint32_t channel);
  162. /**
  163. * @brief Get timer interrupt status
  164. *
  165. * @param[in] timer timer
  166. *
  167. * @return interrupt status
  168. */
  169. uint32_t timer_get_interrupt_status(uint32_t timer);
  170. /**
  171. * @brief Get timer raw interrupt status
  172. *
  173. * @param[in] timer timer
  174. *
  175. * @return raw interrupt status
  176. */
  177. uint32_t timer_get_raw_interrupt_status(uint32_t timer);
  178. /**
  179. * @brief Get timer interrupt status
  180. *
  181. * @param[in] timer timer
  182. * @param[in] channel channel
  183. *
  184. * @return interrupt status
  185. */
  186. uint32_t timer_channel_get_interrupt_status(uint32_t timer, uint32_t channel);
  187. /**
  188. * @brief Clear interrupt
  189. *
  190. * @param[in] timer timer
  191. */
  192. void timer_clear_interrupt(uint32_t timer);
  193. /**
  194. * @brief Clear interrupt
  195. *
  196. * @param[in] timer timer
  197. * @param[in] channel channel
  198. */
  199. void timer_channel_clear_interrupt(uint32_t timer, uint32_t channel);
  200. /**
  201. * @brief Set timer timeout
  202. *
  203. * @param[in] timer timer
  204. * @param[in] channel channel
  205. * @param[in] nanoseconds timeout
  206. *
  207. * @return the real timeout
  208. */
  209. size_t timer_set_interval(uint32_t timer, uint32_t channel, size_t nanoseconds);
  210. /**
  211. * @brief Init timer
  212. *
  213. * @param[in] timer timer
  214. */
  215. void timer_init(uint32_t timer);
  216. /**
  217. * @brief Set timer timeout function
  218. *
  219. * @param[in] timer timer
  220. * @param[in] channel channel
  221. * @param[in] func timeout function
  222. * @param[in] priority interrupt priority
  223. *
  224. */
  225. void timer_set_irq(uint32_t timer, uint32_t channel, void(*func)(), uint32_t priority);
  226. /**
  227. * @brief Enable timer
  228. *
  229. * @param[in] timer timer
  230. * @param[in] channel channel
  231. * @param[in] enable Enable or disable
  232. *
  233. */
  234. void timer_set_enable(uint32_t timer, uint32_t channel, uint32_t enable);
  235. /**
  236. * @brief Enable timer
  237. *
  238. * @param[in] timer timer
  239. * @param[in] channel channel
  240. * @param[in] enable Enable or disable
  241. *
  242. */
  243. void pwm_set_enable(uint32_t timer, uint32_t channel, int enable);
  244. /**
  245. * @brief Set pwm duty
  246. *
  247. * @param[in] timer timer
  248. * @param[in] channel channel
  249. * @param[in] frequency pwm frequency
  250. * @param[in] duty duty
  251. *
  252. */
  253. double pwm_set_frequency(uint32_t timer, uint32_t channel, double frequency, double duty);
  254. #ifdef __cplusplus
  255. }
  256. #endif
  257. #endif /* _DRIVER_TIMER_H */