timer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <stddef.h>
  18. #include <stdint.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* clang-format off */
  23. typedef struct _timer_channel
  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))) timer_channel_t;
  36. typedef struct _kendryte_timer
  37. {
  38. /* TIMER_N Register (0x00-0x4c) */
  39. volatile 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))) kendryte_timer_t;
  53. typedef enum _timer_deivce_number
  54. {
  55. TIMER_DEVICE_0,
  56. TIMER_DEVICE_1,
  57. TIMER_DEVICE_2,
  58. TIMER_DEVICE_MAX,
  59. } timer_device_number_t;
  60. typedef enum _timer_channel_number
  61. {
  62. TIMER_CHANNEL_0,
  63. TIMER_CHANNEL_1,
  64. TIMER_CHANNEL_2,
  65. TIMER_CHANNEL_3,
  66. TIMER_CHANNEL_MAX,
  67. } timer_channel_number_t;
  68. /* TIMER Control Register */
  69. #define TIMER_CR_ENABLE 0x00000001
  70. #define TIMER_CR_MODE_MASK 0x00000002
  71. #define TIMER_CR_FREE_MODE 0x00000000
  72. #define TIMER_CR_USER_MODE 0x00000002
  73. #define TIMER_CR_INTERRUPT_MASK 0x00000004
  74. #define TIMER_CR_PWM_ENABLE 0x00000008
  75. /* clang-format on */
  76. extern volatile kendryte_timer_t *const timer[3];
  77. /**
  78. * @brief Definitions for the timer callbacks
  79. */
  80. typedef int (*timer_callback_t)(void *ctx);
  81. /**
  82. * @brief Set timer timeout
  83. *
  84. * @param[in] timer timer
  85. * @param[in] channel channel
  86. * @param[in] nanoseconds timeout
  87. *
  88. * @return the real timeout
  89. */
  90. size_t timer_set_interval(timer_device_number_t timer_number, timer_channel_number_t channel, size_t nanoseconds);
  91. /**
  92. * @brief Init timer
  93. *
  94. * @param[in] timer timer
  95. */
  96. void timer_init(timer_device_number_t timer_number);
  97. /**
  98. * @brief [DEPRECATED] Set timer timeout function
  99. *
  100. * @param[in] timer timer
  101. * @param[in] channel channel
  102. * @param[in] func timeout function
  103. * @param[in] priority interrupt priority
  104. *
  105. */
  106. void timer_set_irq(timer_device_number_t timer_number, timer_channel_number_t channel, void (*func)(), uint32_t priority);
  107. /**
  108. * @brief Register timer interrupt user callback function
  109. *
  110. * @param[in] device The timer device number
  111. * @param[in] channel The channel
  112. * @param[in] is_one_shot Indicates if single shot
  113. * @param[in] priority The priority
  114. * @param[in] callback The callback function
  115. * @param[in] ctx The context
  116. *
  117. * @return result
  118. * - 0 Success
  119. * - Other Fail
  120. */
  121. int timer_irq_register(timer_device_number_t device, timer_channel_number_t channel, int is_single_shot, uint32_t priority, timer_callback_t callback, void *ctx);
  122. /**
  123. * @brief Deregister timer interrupt user callback function
  124. *
  125. * @param[in] device The timer device number
  126. * @param[in] channel The channel
  127. *
  128. * @return result
  129. * - 0 Success
  130. * - Other Fail
  131. */
  132. int timer_irq_unregister(timer_device_number_t device, timer_channel_number_t channel);
  133. /**
  134. * @brief Enable timer
  135. *
  136. * @param[in] timer timer
  137. * @param[in] channel channel
  138. * @param[in] enable Enable or disable
  139. *
  140. */
  141. void timer_set_enable(timer_device_number_t timer_number, timer_channel_number_t channel, uint32_t enable);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* _DRIVER_TIMER_H */