dualtimer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * \file
  3. *
  4. * \brief SAM DUALTIMER Driver for SAMB11
  5. *
  6. * Copyright (C) 2015 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. #ifndef DUALTIMER_H_INCLUDED
  47. #define DUALTIMER_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_samb_dualtimer_group SAM DUALTIMER
  50. *
  51. * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
  52. * configuration and management of the device's DUALTIMER functionality.
  53. *
  54. * The following peripherals are used by this module:
  55. * - DUALTIMER
  56. *
  57. * The following devices can use this module:
  58. * - Atmel | SMART SAM B11
  59. *
  60. * The outline of this documentation is as follows:
  61. * - \ref asfdoc_samb_dualtimer_prerequisites
  62. * - \ref asfdoc_samb_dualtimer_module_overview
  63. * - \ref asfdoc_samb_dualtimer_special_considerations
  64. * - \ref asfdoc_samb_dualtimer_extra_info
  65. * - \ref asfdoc_samb_dualtimer_examples
  66. * - \ref asfdoc_samb_dualtimer_api_overview
  67. *
  68. *
  69. * \section asfdoc_samb_dualtimer_prerequisites Prerequisites
  70. *
  71. * There are no prerequisites for this module.
  72. *
  73. *
  74. * \section asfdoc_samb_dualtimer_module_overview Module Overview
  75. * The dual-input timers module provides a set of timing and counting related functionality,
  76. * and each timer could setup respective. The module can be configured to use an 16-,
  77. * or 32-bit down-counter that can generate interrupts when they reach 0.
  78. *
  79. * \subsection asfdoc_samb_dual_timer_module_overview_counter_mode Counter Mode
  80. * For each timer, the following modes of operation are available:
  81. * - One-shot timer mode
  82. *
  83. * The counter generates an interrupt once. When the counter reaches 0,
  84. * it halts until you reprogram it. You can do this using one of the following:
  85. *
  86. * 1. Clearing the one-shot count bit in the control register, in which case the
  87. * count proceeds according to the selection of Free-running or Periodic mode.
  88. *
  89. * 2. Writing a new value to the Load Value register.
  90. * - Free-running Mode
  91. *
  92. * The counter wraps after reaching its zero value, and continues to count down from
  93. * the maximum value. This is the default mode.
  94. * - Periodic Mode
  95. *
  96. * The counter generates an interrupt at a constant interval, reloading the original
  97. * value after wrapping past zero.
  98. *
  99. * \subsection asfdoc_samb_dual_timer_module_overview_counter_size Counter Size
  100. * Each timer module can be configured in one of two different counter
  101. * sizes; 16-, and 32-bit. The size of the counter determines the maximum
  102. * value it can count to before an overflow occurs and the count is reset back
  103. * to zero. \ref asfdoc_sam0_count_size_vs_top "The table below" shows the
  104. * maximum values for each of the possible counter sizes.
  105. *
  106. * \anchor asfdoc_sam0_count_size_vs_top
  107. * <table>
  108. * <caption>Timer Counter Sizes and Their Maximum Count Values</caption>
  109. * <tr>
  110. * <th>Counter size</th>
  111. * <th>Max. (hexadecimal)</th>
  112. * <th>Max. (decimal)</th>
  113. * </tr>
  114. * <tr>
  115. * <td>16-bit</td>
  116. * <td>0xFFFF</td>
  117. * <td>65,535</td>
  118. * </tr>
  119. * <tr>
  120. * <td>32-bit</td>
  121. * <td>0xFFFFFFFF</td>
  122. * <td>4,294,967,295</td>
  123. * </tr>
  124. * </table>
  125. *
  126. * \subsection asfdoc_samb_dual_timer_module_overview_clock Clock Settings
  127. *
  128. * \subsubsection asfdoc_sam0_dual_timer_module_overview_clock_selection Clock Selection
  129. * The timers contain the PCLK and TIMCLK clock inputs. PCLK is the main APB system
  130. * clock, and is used by the register interface. TIMCLK is the input to the prescale
  131. * units and the decrementing counters.
  132. *
  133. * This provision of two clock inputs enables the counters to continue to run while
  134. * the APB system is in a sleep state when PCLK is disabled. External system control
  135. * logic must handle the changeover periods when PCLK is disabled and enabled to ensure
  136. * that the PCLK and TIMCLK inputs are fed with synchronous signals when any register
  137. * access is to occur.
  138. *
  139. * \subsubsection asfdoc_sam0_dual_timer_module_overview_clock_prescaler Prescaler
  140. * Each timer module in the SAM B11 has its own individual clock prescaler, which can
  141. * be used to divide the input clock frequency used in the counter. This prescaler
  142. * only scales the clock used to provide clock pulses for the counter to count, the clock
  143. * can be divide to 1, 16, or 256.
  144. *
  145. * \section asfdoc_samb_dualtimer_special_considerations Special Considerations
  146. *
  147. * There are no special considerations for this module.
  148. *
  149. * \section asfdoc_samb_dualtimer_extra_info Extra Information
  150. *
  151. * For extra information, see \ref asfdoc_samb_dualtimer_extra. This includes:
  152. * - \ref asfdoc_samb_dualtimer_extra_acronyms
  153. * - \ref asfdoc_samb_dualtimer_extra_dependencies
  154. * - \ref asfdoc_samb_dualtimer_extra_errata
  155. * - \ref asfdoc_samb_dualtimer_extra_history
  156. *
  157. *
  158. * \section asfdoc_samb_dualtimer_examples Examples
  159. *
  160. * For a list of examples related to this driver, see
  161. * \ref asfdoc_samb_dualtimer_exqsg.
  162. *
  163. *
  164. * \section asfdoc_samb_dualtimer_api_overview API Overview
  165. * @{
  166. */
  167. #include <compiler.h>
  168. #include <system_sam_b.h>
  169. #ifdef __cplusplus
  170. extern "C" {
  171. #endif
  172. /** Type definition for a DUALTIMER module callback function. */
  173. typedef void (*dualtimer_callback_t)(void);
  174. /**
  175. * \brief Specifies the timer1 or timer2.
  176. *
  177. * This enum specifies duatimer timer1 or timer2.
  178. */
  179. enum dualtimer_timer {
  180. /** Dualtimer timer1 */
  181. DUALTIMER_TIMER1 = 0,
  182. /** Dualtimer timer2 */
  183. DUALTIMER_TIMER2,
  184. };
  185. /**
  186. * \brief DUALTIMER module clock input.
  187. *
  188. * DUALTIMER module clock.
  189. */
  190. enum dualtimer_clock_input {
  191. /** source from clock input 0: 26MHz */
  192. DUALTIMER_CLK_INPUT_0 = 0,
  193. /** source from clock input 1: 13MHz */
  194. DUALTIMER_CLK_INPUT_1,
  195. /** source from clock input 2: 6.5MHz */
  196. DUALTIMER_CLK_INPUT_2,
  197. /** source from clock input 3: 3MHz*/
  198. DUALTIMER_CLK_INPUT_3,
  199. };
  200. /**
  201. * \brief Specifies if the counter is 16-bit, or 32-bit.
  202. *
  203. * This enum specifies counter with one-shot, free running or periodic counter mode.
  204. */
  205. enum dualtimer_counter_mode {
  206. /** Counter in one-shot mode */
  207. DUALTIMER_ONE_SHOT_MODE = 0,
  208. /** Counter is in free-running mode */
  209. DUALTIMER_FREE_RUNNING_MODE,
  210. /** Counter is in periodic mode */
  211. DUALTIMER_PERIODIC_MODE,
  212. };
  213. /**
  214. * \brief Dualtimer counter mode enum.
  215. *
  216. * This enum specify the maximum value it is possible to count to.
  217. */
  218. enum dualtimer_counter_size {
  219. /** 16-bit counter */
  220. DUALTIMER_COUNTER_SIZE_16BIT = 0,
  221. /** 32-bit counter */
  222. DUALTIMER_COUNTER_SIZE_32BIT,
  223. };
  224. /**
  225. * \brief Dualtimer clock prescaler values.
  226. *
  227. * This enum is used to choose the clock prescaler
  228. * configuration. The prescaler divides the clock frequency of the
  229. * Dual Timer module to make the counter count slower.
  230. */
  231. enum dualtimer_clock_prescaler {
  232. /** Divide clock by 1 */
  233. DUALTIMER_CLOCK_PRESCALER_DIV1 = 0,
  234. /** Divide clock by 16 */
  235. DUALTIMER_CLOCK_PRESCALER_DIV16,
  236. /** Divide clock by 256 */
  237. DUALTIMER_CLOCK_PRESCALER_DIV256,
  238. };
  239. /**
  240. * \brief Dualtimer set counter.
  241. *
  242. * This enum is used to choose set the load register or
  243. * background load register. The difference to set load
  244. * register is that writes to background register do not
  245. * cause the counter to immediately restart from the new value.
  246. */
  247. enum dualtimer_set_register {
  248. /** Set current counter */
  249. DUALTIMER_SET_CURRUNT_REG = 0,
  250. /** Set background counter */
  251. DUALTIMER_SET_BG_REG,
  252. };
  253. /**
  254. * \brief Dualtimer private configuration structure.
  255. *
  256. * Private configuration struct for Dualtimer instance.
  257. */
  258. struct dualtimer_private_config {
  259. /** Enable timer */
  260. bool timer_enable;
  261. /** Selects one-shot or wrapping counter mode */
  262. enum dualtimer_counter_mode counter_mode;
  263. /** Selects 16-bit or 32- bit counter size */
  264. enum dualtimer_counter_size counter_size;
  265. /** Selects the prescaler value */
  266. enum dualtimer_clock_prescaler clock_prescaler;
  267. /** Enable the interrupt */
  268. bool interrup_enable;
  269. /** Counter load value */
  270. uint32_t load_value;
  271. };
  272. /**
  273. * \brief Dualtimer configuration structure.
  274. *
  275. * Configuration struct for Dualtimer instance. This structure should be
  276. * initialized by the \ref dualtimer_get_config_defaults function before being
  277. * modified by the user application.
  278. */
  279. struct dualtimer_config {
  280. /** Timer1 private configuration */
  281. struct dualtimer_private_config timer1;
  282. /** Timer2 private configuration */
  283. struct dualtimer_private_config timer2;
  284. /** Selects Dualtimer clock frequency */
  285. enum dualtimer_clock_input clock_source;
  286. };
  287. /**
  288. * \name Configuration and Initialization
  289. * @{
  290. */
  291. void dualtimer_get_config_defaults(struct dualtimer_config *config);
  292. void dualtimer_init(const struct dualtimer_config *config);
  293. /** @} */
  294. /**
  295. * \name Get and set value
  296. * @{
  297. */
  298. uint32_t dualtimer_get_value(enum dualtimer_timer timer);
  299. void dualtimer_set_counter(enum dualtimer_timer timer,
  300. enum dualtimer_set_register cur_bg, uint32_t value);
  301. /** @} */
  302. /**
  303. * \name Get and Clear status
  304. * @{
  305. */
  306. uint8_t dualtimer_get_status(enum dualtimer_timer timer);
  307. uint8_t dualtimer_get_interrupt_status(enum dualtimer_timer timer);
  308. void dualtimer_clear_interrupt_status(enum dualtimer_timer timer);
  309. /** @} */
  310. /**
  311. * \name Enable and disable module
  312. * @{
  313. */
  314. void dualtimer_enable(enum dualtimer_timer timer);
  315. void dualtimer_disable(enum dualtimer_timer timer);
  316. /** @} */
  317. /**
  318. * \name Callback
  319. * @{
  320. */
  321. void dualtimer_register_callback(enum dualtimer_timer timer, dualtimer_callback_t fun);
  322. void dualtimer_unregister_callback(enum dualtimer_timer timer);
  323. /** @} */
  324. /** @}*/
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. /**
  329. * \page asfdoc_samb_dualtimer_extra Extra Information for DUALTIMER Driver
  330. *
  331. * \section asfdoc_samb_dualtimer_extra_acronyms Acronyms
  332. * Below is a table listing the acronyms used in this module, along with their
  333. * intended meanings.
  334. * <table>
  335. * <tr>
  336. * <th>Acronym</th>
  337. * <th>Description</th>
  338. * </tr>
  339. * <tr>
  340. * <td>DUALTIMER</td>
  341. * <td>Dualtimer</td>
  342. * </tr>
  343. * </table>
  344. *
  345. * \section asfdoc_samb_dualtimer_extra_dependencies Dependencies
  346. * There are no dependencies related to this driver.
  347. *
  348. *
  349. * \section asfdoc_samb_dualtimer_extra_errata Errata
  350. * There are no errata related to this driver.
  351. *
  352. *
  353. * \section asfdoc_samb_dualtimer_extra_history Module History
  354. * An overview of the module history is presented in the table below, with
  355. * details on the enhancements and fixes made to the module since its first
  356. * release. The current version of this corresponds to the newest version in
  357. * the table.
  358. *
  359. * <table>
  360. * <tr>
  361. * <th>Changelog</th>
  362. * </tr>
  363. * <tr>
  364. * <td>Initial Release</td>
  365. * </tr>
  366. * </table>
  367. */
  368. /**
  369. * \page asfdoc_samb_dualtimer_exqsg Examples for DUALTIMER Driver
  370. *
  371. * This is a list of the available Quick Start guides (QSGs) and example
  372. * applications for \ref asfdoc_samb_dualtimer_group. QSGs are simple examples with
  373. * step-by-step instructions to configure and use this driver in a selection of
  374. * use cases. Note that QSGs can be compiled as a standalone application or be
  375. * added to the user application.
  376. *
  377. * - \subpage asfdoc_samb_dualtimer_basic_use_case
  378. *
  379. * \page asfdoc_samb_dualtimer_document_revision_history Document Revision History
  380. *
  381. * <table>
  382. * <tr>
  383. * <th>Doc. Rev.</td>
  384. * <th>Date</td>
  385. * <th>Comments</td>
  386. * </tr>
  387. * <tr>
  388. * <td>A</td>
  389. * <td>09/2015</td>
  390. * <td>Initial release</td>
  391. * </tr>
  392. * </table>
  393. */
  394. #endif