dw_timer.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file dw_timer.c
  18. * @brief CSI Source File for timer Driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #include "csi_core.h"
  23. #include "drv_timer.h"
  24. #include "dw_timer.h"
  25. #include "soc.h"
  26. #define ERR_TIMER(errno) (CSI_DRV_ERRNO_TIMER_BASE | errno)
  27. #define TIMER_NULL_PARAM_CHK(para) \
  28. do { \
  29. if (para == NULL) { \
  30. return ERR_TIMER(EDRV_PARAMETER); \
  31. } \
  32. } while (0)
  33. typedef struct {
  34. uint32_t base;
  35. uint32_t irq;
  36. timer_event_cb_t cb_event;
  37. uint32_t timeout; ///< the set time (us)
  38. uint32_t timeout_flag;
  39. void *arg;
  40. } dw_timer_priv_t;
  41. static dw_timer_priv_t timer_instance[CONFIG_TIMER_NUM];
  42. static const timer_capabilities_t timer_capabilities = {
  43. .interrupt_mode = 1 ///< supports Interrupt mode
  44. };
  45. /**
  46. \brief Make all the timers in the idle state.
  47. \param[in] pointer to timer register base
  48. */
  49. static void timer_deactive_control(dw_timer_reg_t *addr)
  50. {
  51. /* stop the corresponding timer */
  52. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;
  53. /* Disable interrupt. */
  54. addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK;
  55. }
  56. void dw_timer_irqhandler(int idx)
  57. {
  58. dw_timer_priv_t *timer_priv = &timer_instance[idx];
  59. timer_priv->timeout_flag = 1;
  60. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  61. addr->TxEOI;
  62. if (timer_priv->cb_event) {
  63. return timer_priv->cb_event(TIMER_EVENT_TIMEOUT, timer_priv->arg);
  64. }
  65. }
  66. int32_t __attribute__((weak)) target_get_timer_count(void)
  67. {
  68. return 0;
  69. }
  70. int32_t __attribute__((weak)) target_get_timer(uint32_t idx, uint32_t *base, uint32_t *irq)
  71. {
  72. return NULL;
  73. }
  74. /**
  75. \brief get timer instance count.
  76. \return timer instance count
  77. */
  78. int32_t csi_timer_get_instance_count(void)
  79. {
  80. return target_get_timer_count();
  81. }
  82. /**
  83. \brief Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface 2.registers event callback function
  84. \param[in] idx instance timer index
  85. \param[in] cb_event Pointer to \ref timer_event_cb_t
  86. \return pointer to timer instance
  87. */
  88. timer_handle_t csi_timer_initialize(int32_t idx, timer_event_cb_t cb_event, void *arg)
  89. {
  90. if (idx < 0 || idx >= CONFIG_TIMER_NUM) {
  91. return NULL;
  92. }
  93. uint32_t base = 0u;
  94. uint32_t irq = 0u;
  95. int32_t real_idx = target_get_timer(idx, &base, &irq);
  96. if (real_idx != idx) {
  97. return NULL;
  98. }
  99. dw_timer_priv_t *timer_priv = &timer_instance[idx];
  100. timer_priv->base = base;
  101. timer_priv->irq = irq;
  102. timer_priv->arg = arg;
  103. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  104. timer_priv->timeout = DW_TIMER_INIT_DEFAULT_VALUE;
  105. timer_deactive_control(addr);
  106. timer_priv->cb_event = cb_event;
  107. drv_nvic_enable_irq(timer_priv->irq);
  108. return (timer_handle_t)timer_priv;
  109. }
  110. /**
  111. \brief De-initialize TIMER Interface. stops operation and releases the software resources used by the interface
  112. \param[in] handle timer handle to operate.
  113. \return error code
  114. */
  115. int32_t csi_timer_uninitialize(timer_handle_t handle)
  116. {
  117. TIMER_NULL_PARAM_CHK(handle);
  118. dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
  119. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  120. timer_deactive_control(addr);
  121. timer_priv->cb_event = NULL;
  122. drv_nvic_disable_irq(timer_priv->irq);
  123. return 0;
  124. }
  125. /**
  126. \brief Get driver capabilities.
  127. \param[in] handle timer handle to operate.
  128. \return \ref timer_capabilities_t
  129. */
  130. timer_capabilities_t csi_timer_get_capabilities(timer_handle_t handle)
  131. {
  132. return timer_capabilities;
  133. }
  134. /**
  135. \brief config timer mode.
  136. \param[in] handle timer handle to operate.
  137. \param[in] mode \ref timer_mode_e
  138. \return error code
  139. */
  140. int32_t csi_timer_config(timer_handle_t handle, timer_mode_e mode)
  141. {
  142. TIMER_NULL_PARAM_CHK(handle);
  143. dw_timer_priv_t *timer_priv = handle;
  144. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  145. switch (mode) {
  146. case TIMER_MODE_FREE_RUNNING:
  147. addr->TxControl &= ~DW_TIMER_TXCONTROL_MODE;
  148. break;
  149. case TIMER_MODE_RELOAD:
  150. addr->TxControl |= DW_TIMER_TXCONTROL_MODE;
  151. break;
  152. default:
  153. return ERR_TIMER(EDRV_PARAMETER);
  154. }
  155. return 0;
  156. }
  157. /**
  158. \brief Set timer.
  159. \param[in] instance timer instance to operate.
  160. \param[in] timeout the timeout value in microseconds(us).
  161. \return error code
  162. */
  163. int32_t csi_timer_set_timeout(timer_handle_t handle, uint32_t timeout)
  164. {
  165. TIMER_NULL_PARAM_CHK(handle);
  166. dw_timer_priv_t *timer_priv = handle;
  167. timer_priv->timeout = timeout;
  168. return 0;
  169. }
  170. /**
  171. \brief Start timer.
  172. \param[in] handle timer handle to operate.
  173. \return error code
  174. */
  175. int32_t csi_timer_start(timer_handle_t handle, uint32_t apbfreq)
  176. {
  177. TIMER_NULL_PARAM_CHK(handle);
  178. dw_timer_priv_t *timer_priv = handle;
  179. timer_priv->timeout_flag = 0;
  180. uint32_t min_us = apbfreq / 1000000;
  181. if ((timer_priv->timeout < min_us) || (timer_priv->timeout > 0xffffffff / min_us)) {
  182. return ERR_TIMER(EDRV_PARAMETER);
  183. }
  184. uint32_t load = (uint32_t)(timer_priv->timeout * min_us);
  185. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  186. addr->TxLoadCount = load; /* load time(us) */
  187. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
  188. addr->TxControl |= DW_TIMER_TXCONTROL_ENABLE; /* enable the corresponding timer */
  189. addr->TxControl &= ~DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
  190. return 0;
  191. }
  192. /**
  193. \brief Stop timer.
  194. \param[in] handle timer handle to operate.
  195. \return error code
  196. */
  197. int32_t csi_timer_stop(timer_handle_t handle)
  198. {
  199. TIMER_NULL_PARAM_CHK(handle);
  200. dw_timer_priv_t *timer_priv = handle;
  201. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  202. addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
  203. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
  204. return 0;
  205. }
  206. /**
  207. \brief suspend timer.
  208. \param[in] instance timer instance to operate.
  209. \return error code
  210. */
  211. int32_t csi_timer_suspend(timer_handle_t handle)
  212. {
  213. TIMER_NULL_PARAM_CHK(handle);
  214. return ERR_TIMER(EDRV_UNSUPPORTED);
  215. }
  216. /**
  217. \brief resume timer.
  218. \param[in] handle timer handle to operate.
  219. \return error code
  220. */
  221. int32_t csi_timer_resume(timer_handle_t handle)
  222. {
  223. TIMER_NULL_PARAM_CHK(handle);
  224. dw_timer_priv_t *timer_priv = handle;
  225. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  226. addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* stop the corresponding timer */
  227. addr->TxControl &= DW_TIMER_TXCONTROL_ENABLE; /* restart the corresponding timer */
  228. return 0;
  229. }
  230. /**
  231. \brief get timer current value
  232. \param[in] handle timer handle to operate.
  233. \param[in] value timer current value
  234. \return error code
  235. */
  236. int32_t csi_timer_get_current_value(timer_handle_t handle, uint32_t *value)
  237. {
  238. TIMER_NULL_PARAM_CHK(handle);
  239. dw_timer_priv_t *timer_priv = handle;
  240. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  241. *value = addr->TxCurrentValue;
  242. return 0;
  243. }
  244. /**
  245. \brief Get TIMER status.
  246. \param[in] handle timer handle to operate.
  247. \return TIMER status \ref timer_status_t
  248. */
  249. timer_status_t csi_timer_get_status(timer_handle_t handle)
  250. {
  251. timer_status_t timer_status = {0};
  252. if (handle == NULL) {
  253. return timer_status;
  254. }
  255. dw_timer_priv_t *timer_priv = handle;
  256. dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
  257. if (addr->TxControl & DW_TIMER_TXCONTROL_ENABLE) {
  258. timer_status.active = 1;
  259. }
  260. if (timer_priv->timeout_flag == 1) {
  261. timer_status.timeout = 1;
  262. }
  263. return timer_status;
  264. }