dualtimer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * \file
  3. *
  4. * \brief SAM DUALTIMER Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "dualtimer.h"
  47. static dualtimer_callback_t dualtimer_callback_timer1 = NULL;
  48. static dualtimer_callback_t dualtimer_callback_timer2 = NULL;
  49. /**
  50. * \brief Initializes config with predefined default values.
  51. *
  52. * This function will initialize a given Dualtimer configuration structure to
  53. * a set of known default values. This function should be called on
  54. * any new instance of the configuration structures before being
  55. * modified by the user application.
  56. *
  57. * \param[out] config Pointer to a DUALTIMER module configuration structure to set
  58. */
  59. void dualtimer_get_config_defaults(struct dualtimer_config *config)
  60. {
  61. config->timer1.timer_enable = true;
  62. config->timer2.timer_enable = true;
  63. config->timer1.counter_mode = DUALTIMER_PERIODIC_MODE;
  64. config->timer2.counter_mode = DUALTIMER_PERIODIC_MODE;
  65. config->timer1.counter_size = DUALTIMER_COUNTER_SIZE_32BIT;
  66. config->timer2.counter_size = DUALTIMER_COUNTER_SIZE_32BIT;
  67. config->timer1.clock_prescaler = DUALTIMER_CLOCK_PRESCALER_DIV1;
  68. config->timer2.clock_prescaler = DUALTIMER_CLOCK_PRESCALER_DIV1;
  69. config->timer1.interrup_enable = true;
  70. config->timer2.interrup_enable = true;
  71. config->timer1.load_value = 0;
  72. config->timer2.load_value = 0;
  73. config->clock_source = DUALTIMER_CLK_INPUT_0;
  74. }
  75. /**
  76. * \brief Get Dualtimer module timer1/timer2 current value.
  77. *
  78. * \param[in] timer Timer1/Timer2
  79. *
  80. * \retval Timer1/Timer2 current value
  81. */
  82. uint32_t dualtimer_get_value(enum dualtimer_timer timer)
  83. {
  84. if (timer == DUALTIMER_TIMER1) {
  85. return DUALTIMER0->TIMER1VALUE.reg;
  86. } else {
  87. return DUALTIMER0->TIMER2VALUE.reg;
  88. }
  89. }
  90. /**
  91. * \brief Set Dualtimer module timer1/timer2 load value.
  92. *
  93. * \param[in] timer Timer1/Timer2
  94. * \param[in] cur_bg Current/Background
  95. * \param[in] value Load value
  96. */
  97. void dualtimer_set_counter(enum dualtimer_timer timer,
  98. enum dualtimer_set_register cur_bg, uint32_t value)
  99. {
  100. if (timer == DUALTIMER_TIMER1) {
  101. if (cur_bg == DUALTIMER_SET_CURRUNT_REG) {
  102. DUALTIMER0->TIMER1LOAD.reg = value;
  103. } else {
  104. DUALTIMER0->TIMER1BGLOAD.reg = value;
  105. }
  106. } else {
  107. if (cur_bg == DUALTIMER_SET_CURRUNT_REG) {
  108. DUALTIMER0->TIMER2LOAD.reg = value;
  109. } else {
  110. DUALTIMER0->TIMER2BGLOAD.reg = value;
  111. }
  112. }
  113. }
  114. /**
  115. * \brief Get Dualtimer module timer1/timer2 raw interrupt status
  116. *
  117. * \param[in] timer Timer1/Timer2
  118. *
  119. * \retval The raw interrupt status of timer1/timer2
  120. */
  121. uint8_t dualtimer_get_status(enum dualtimer_timer timer)
  122. {
  123. if (timer == DUALTIMER_TIMER1) {
  124. return DUALTIMER0->TIMER1RIS.reg;
  125. } else {
  126. return DUALTIMER0->TIMER2RIS.reg;
  127. }
  128. }
  129. /**
  130. * \brief Get Dualtimer module timer1/timer2 interrupt status
  131. *
  132. * \param[in] timer Timer1/Timer2
  133. *
  134. * \retval The interrupt status of timer1/timer2
  135. */
  136. uint8_t dualtimer_get_interrupt_status(enum dualtimer_timer timer)
  137. {
  138. if (timer == DUALTIMER_TIMER1) {
  139. return DUALTIMER0->TIMER1MIS.reg;
  140. } else {
  141. return DUALTIMER0->TIMER2MIS.reg;
  142. }
  143. }
  144. /**
  145. * \brief Clear Dualtimer module timer1/timer2 interrupt status
  146. *
  147. * Clear the Dualtimer module timer1/timer2 interrupt status
  148. *
  149. * \param[in] timer Timer1/Timer2
  150. */
  151. void dualtimer_clear_interrupt_status(enum dualtimer_timer timer)
  152. {
  153. if (timer == DUALTIMER_TIMER1) {
  154. DUALTIMER0->TIMER1INTCLR.reg = 1;
  155. } else {
  156. DUALTIMER0->TIMER2INTCLR.reg = 1;
  157. }
  158. }
  159. /**
  160. * \brief Set Dualtimer module timer1/timer2 enable
  161. *
  162. * Enable the Dualtimer module timer1/timer2
  163. *
  164. * \param[in] timer Timer1/Timer2
  165. */
  166. void dualtimer_enable(enum dualtimer_timer timer)
  167. {
  168. if (timer == DUALTIMER_TIMER1) {
  169. DUALTIMER0->TIMER1CONTROL.reg |= DUALTIMER_TIMER1CONTROL_TIMER_ENABLE;
  170. } else {
  171. DUALTIMER0->TIMER2CONTROL.reg |= DUALTIMER_TIMER2CONTROL_TIMER_ENABLE;
  172. }
  173. }
  174. /**
  175. * \brief Set Dualtimer module timer1/timer2 disable
  176. *
  177. * Disable the Dualtimer module timer1/timer2
  178. *
  179. * \param[in] timer Timer1/Timer2
  180. */
  181. void dualtimer_disable(enum dualtimer_timer timer)
  182. {
  183. if (timer == DUALTIMER_TIMER1) {
  184. DUALTIMER0->TIMER1CONTROL.reg &= ~DUALTIMER_TIMER1CONTROL_TIMER_ENABLE;
  185. } else {
  186. DUALTIMER0->TIMER2CONTROL.reg &= ~DUALTIMER_TIMER2CONTROL_TIMER_ENABLE;
  187. }
  188. }
  189. /**
  190. * \brief Dualtimer ISR handler.
  191. *
  192. * Dualtimer ISR handler.
  193. *
  194. */
  195. static void dualtimer_isr_handler(void)
  196. {
  197. if (dualtimer_get_interrupt_status(DUALTIMER_TIMER1)) {
  198. dualtimer_clear_interrupt_status(DUALTIMER_TIMER1);
  199. if (dualtimer_callback_timer1)
  200. dualtimer_callback_timer1();
  201. }
  202. if (dualtimer_get_interrupt_status(DUALTIMER_TIMER2)) {
  203. dualtimer_clear_interrupt_status(DUALTIMER_TIMER2);
  204. if (dualtimer_callback_timer2)
  205. dualtimer_callback_timer2();
  206. }
  207. }
  208. /**
  209. * \brief Initializes Dualtimer module instance.
  210. *
  211. * Initializes the Dualtimer module, based on the given
  212. * configuration values.
  213. *
  214. * \param[in] config Pointer to the Dualtimer configuration options struct
  215. *
  216. * \return Status of the initialization procedure.
  217. */
  218. void dualtimer_init(const struct dualtimer_config *config)
  219. {
  220. uint8_t regval = 0;
  221. /* Global reset */
  222. system_peripheral_reset(PERIPHERAL_DUALT_TIMER);
  223. /* Common config */
  224. if (config->timer1.timer_enable || config->timer2.timer_enable) {
  225. LPMCU_MISC_REGS0->LPMCU_CLOCK_ENABLES_0.reg |=
  226. LPMCU_MISC_REGS_LPMCU_CLOCK_ENABLES_0_DUALTIMER0_CLK_EN;
  227. LPMCU_MISC_REGS0->LPMCU_CTRL.bit.DUALTIMER0_CLK_SEL = config->clock_source;
  228. }
  229. /* Timer1 config */
  230. if (config->timer1.timer_enable) {
  231. if (config->timer1.counter_mode == DUALTIMER_ONE_SHOT_MODE) {
  232. regval = DUALTIMER_TIMER1CONTROL_ONE_SHOT_COUNT_1;
  233. } else if (config->timer1.counter_mode == DUALTIMER_FREE_RUNNING_MODE) {
  234. regval = DUALTIMER_TIMER1CONTROL_TIMER_MODE_0;
  235. } else if (config->timer1.counter_mode == DUALTIMER_PERIODIC_MODE) {
  236. regval = DUALTIMER_TIMER1CONTROL_TIMER_MODE_1;
  237. }
  238. regval |= (DUALTIMER_TIMER1CONTROL_TIMER_SIZE &
  239. ((config->timer1.counter_size) << DUALTIMER_TIMER1CONTROL_TIMER_SIZE_Pos)) |
  240. DUALTIMER_TIMER1CONTROL_TIMERPRE(config->timer1.clock_prescaler);
  241. if (config->timer1.interrup_enable) {
  242. regval |= DUALTIMER_TIMER1CONTROL_INTERRUPT_ENABLE;
  243. }
  244. DUALTIMER0->TIMER1LOAD.reg = config->timer1.load_value;
  245. DUALTIMER0->TIMER1CONTROL.reg = regval;
  246. LPMCU_MISC_REGS0->DUALTIMER0_CTRL.reg |= LPMCU_MISC_REGS_DUALTIMER0_CTRL_CNTR_1_ENABLE;
  247. dualtimer_enable(DUALTIMER_TIMER1);
  248. }
  249. /* Timer2 config */
  250. if (config->timer2.timer_enable) {
  251. if (config->timer2.counter_mode == DUALTIMER_ONE_SHOT_MODE) {
  252. regval = DUALTIMER_TIMER2CONTROL_ONE_SHOT_COUNT_1;
  253. } else if (config->timer2.counter_mode == DUALTIMER_FREE_RUNNING_MODE) {
  254. regval = DUALTIMER_TIMER2CONTROL_TIMER_MODE_0;
  255. } else if (config->timer2.counter_mode == DUALTIMER_PERIODIC_MODE) {
  256. regval = DUALTIMER_TIMER2CONTROL_TIMER_MODE_1;
  257. }
  258. regval |= (DUALTIMER_TIMER2CONTROL_TIMER_SIZE &
  259. ((config->timer2.counter_size) << DUALTIMER_TIMER2CONTROL_TIMER_SIZE_Pos)) |
  260. DUALTIMER_TIMER2CONTROL_TIMERPRE(config->timer2.clock_prescaler);
  261. if (config->timer2.interrup_enable) {
  262. regval |= DUALTIMER_TIMER2CONTROL_INTERRUPT_ENABLE;
  263. }
  264. DUALTIMER0->TIMER2LOAD.reg = config->timer2.load_value;
  265. DUALTIMER0->TIMER2CONTROL.reg = regval;
  266. LPMCU_MISC_REGS0->DUALTIMER0_CTRL.reg |= LPMCU_MISC_REGS_DUALTIMER0_CTRL_CNTR_2_ENABLE;
  267. dualtimer_enable(DUALTIMER_TIMER2);
  268. }
  269. system_register_isr(RAM_ISR_TABLE_DUALTIMER_INDEX, (uint32_t)dualtimer_isr_handler);
  270. }
  271. /**
  272. * \brief Registers a callback.
  273. *
  274. * Registers and enable a callback function which is implemented by the user.
  275. *
  276. * \param[in] callback_func Pointer to callback function
  277. */
  278. void dualtimer_register_callback(enum dualtimer_timer timer, dualtimer_callback_t fun)
  279. {
  280. if (timer == DUALTIMER_TIMER1) {
  281. dualtimer_callback_timer1 = fun;
  282. } else {
  283. dualtimer_callback_timer2 = fun;
  284. }
  285. }
  286. /**
  287. * \brief Unregisters a callback.
  288. *
  289. * Unregisters and disable a callback function implemented by the user.
  290. *
  291. */
  292. void dualtimer_unregister_callback(enum dualtimer_timer timer)
  293. {
  294. if (timer == DUALTIMER_TIMER1) {
  295. dualtimer_callback_timer1 = NULL;
  296. } else {
  297. dualtimer_callback_timer2 = NULL;
  298. }
  299. }