rtc_count.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /**
  2. * \file
  3. *
  4. * \brief SAM RTC Driver (Count Mode)
  5. *
  6. * Copyright (C) 2012-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. #ifndef RTC_COUNT_H_INCLUDED
  47. #define RTC_COUNT_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_rtc_count_group SAM RTC Count (RTC COUNT) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides
  52. * an interface for the configuration and management of the device's Real Time
  53. * Clock functionality in Count operating mode, for the configuration and
  54. * retrieval of the current RTC counter value. The following driver API modes
  55. * are covered by this manual:
  56. *
  57. * - Polled APIs
  58. * \if RTC_COUNT_CALLBACK_MODE
  59. * - Callback APIs
  60. * \endif
  61. *
  62. * The following peripheral is used by this module:
  63. * - RTC (Real Time Clock)
  64. *
  65. * The following devices can use this module:
  66. * - Atmel | SMART SAM D20/D21
  67. * - Atmel | SMART SAM R21
  68. * - Atmel | SMART SAM D09/D10/D11
  69. * - Atmel | SMART SAM L21/L22
  70. * - Atmel | SMART SAM DA1
  71. * - Atmel | SMART SAM C20/C21
  72. * - Atmel | SMART SAM HA1
  73. * - Atmel | SMART SAM R30
  74. *
  75. * The outline of this documentation is as follows:
  76. * - \ref asfdoc_sam0_rtc_count_prerequisites
  77. * - \ref asfdoc_sam0_rtc_count_module_overview
  78. * - \ref asfdoc_sam0_rtc_count_special_considerations
  79. * - \ref asfdoc_sam0_rtc_count_extra_info
  80. * - \ref asfdoc_sam0_rtc_count_examples
  81. * - \ref asfdoc_sam0_rtc_count_api_overview
  82. *
  83. *
  84. * \section asfdoc_sam0_rtc_count_prerequisites Prerequisites
  85. *
  86. * There are no prerequisites for this module.
  87. *
  88. *
  89. * \section asfdoc_sam0_rtc_count_module_overview Module Overview
  90. *
  91. * The RTC module in the SAM devices is a 32-bit counter, with a 10-bit
  92. * programmable prescaler. Typically, the RTC clock is run continuously,
  93. * including in the device's low-power sleep modes, to track the current time
  94. * and date information. The RTC can be used as a source to wake up the system
  95. * at a scheduled time or periodically using the alarm functions.
  96. *
  97. * In this driver, the RTC is operated in Count mode. This allows for an
  98. * easy integration of an asynchronous counter into a user application, which is
  99. * capable of operating while the device is in sleep mode.
  100. *
  101. * Whilst operating in Count mode, the RTC features:
  102. * - 16-bit counter mode
  103. * - Selectable counter period
  104. * - Up to six configurable compare values
  105. * - 32-bit counter mode
  106. * - Clear counter value on match
  107. * - Up to four configurable compare values
  108. *
  109. * \subsection asfdoc_sam0_rtc_count_features Driver Feature Macro Definition
  110. * <table>
  111. * <tr>
  112. * <th>Driver Feature Macro</th>
  113. * <th>Supported devices</th>
  114. * </tr>
  115. * <tr>
  116. * <td>FEATURE_RTC_PERIODIC_INT</td>
  117. * <td>SAM L21/L22/C20/C21/R30</td>
  118. * </tr>
  119. * <tr>
  120. * <td>FEATURE_RTC_PRESCALER_OFF</td>
  121. * <td>SAM L21/L22/C20/C21/R30</td>
  122. * </tr>
  123. * <tr>
  124. * <td>FEATURE_RTC_CLOCK_SELECTION</td>
  125. * <td>SAM L21/L22/C20/C21/R30</td>
  126. * </tr>
  127. * <tr>
  128. * <td>FEATURE_RTC_GENERAL_PURPOSE_REG</td>
  129. * <td>SAM L21/L22/R30</td>
  130. * </tr>
  131. * <tr>
  132. * <td>FEATURE_RTC_CONTINUOUSLY_UPDATED</td>
  133. * <td>SAM D20, SAM D21, SAM R21, SAM D10, SAM D11, SAM DA1, SAM HA1</td>
  134. * </tr>
  135. * <tr>
  136. * <td>FEATURE_RTC_TAMPER_DETECTION</td>
  137. * <td>SAM L22</td>
  138. * </tr>
  139. * </table>
  140. * \note The specific features are only available in the driver when the
  141. * selected device supports those features.
  142. *
  143. * \section asfdoc_sam0_rtc_count_module_overview_compares Compare and Overflow
  144. * The RTC can be used with up to 4/6 compare values (depending on selected
  145. * operation mode). These compare values will trigger on match with the current
  146. * RTC counter value, and can be set up to trigger an interrupt, event, or both.
  147. * The RTC can also be configured to clear the counter value on compare match
  148. * in 32-bit mode, resetting the count value back to zero.
  149. *
  150. * If the RTC is operated without the Clear on Match option enabled, or in
  151. * 16-bit mode, the RTC counter value will instead be cleared on overflow once
  152. * the maximum count value has been reached:
  153. *
  154. * \f[ COUNT_{MAX} = 2^{32}-1 \f] for 32-bit counter mode, and
  155. * \f[ COUNT_{MAX} = 2^{16}-1 \f] for 16-bit counter mode.
  156. *
  157. * When running in 16-bit mode, the overflow value is selectable with a period
  158. * value. The counter overflow will then occur when the counter value reaches
  159. * the specified period value.
  160. *
  161. * \subsection asfdoc_sam0_rtc_count_module_overview_periodic Periodic Events
  162. * The RTC can generate events at periodic intervals, allowing for direct
  163. * peripheral actions without CPU intervention. The periodic events can be
  164. * generated on the upper eight bits of the RTC prescaler, and will be generated on
  165. * the rising edge transition of the specified bit. The resulting periodic
  166. * frequency can be calculated by the following formula:
  167. *
  168. * \f[ f_{PERIODIC}=\frac{f_{ASY}}{2^{n+3}} \f]
  169. *
  170. * Where \f$f_{ASY}\f$ refers to the \e asynchronous clock is set up in the RTC
  171. * module configuration. The \b n parameter is the event source generator index
  172. * of the RTC module. If the asynchronous clock is operated at the recommended
  173. * frequency of 1KHz, the formula results in the values shown in
  174. * \ref asfdoc_sam0_rtc_count_module_rtc_hz "the table below".
  175. *
  176. * \anchor asfdoc_sam0_rtc_count_module_rtc_hz
  177. * <table>
  178. * <caption>RTC Event Frequencies for Each Prescaler Bit Using a 1KHz Clock</caption>
  179. * <tr>
  180. * <th>n</th> <th>Periodic event</th>
  181. * </tr>
  182. * <tr>
  183. * <td>7</td> <td>1Hz</td>
  184. * </tr>
  185. * <tr>
  186. * <td>6</td> <td>2Hz</td>
  187. * </tr>
  188. * <tr>
  189. * <td>5</td> <td>4Hz</td>
  190. * </tr>
  191. * <tr>
  192. * <td>4</td> <td>8Hz</td>
  193. * </tr>
  194. * <tr>
  195. * <td>3</td> <td>16Hz</td>
  196. * </tr>
  197. * <tr>
  198. * <td>2</td> <td>32Hz</td>
  199. * </tr>
  200. * <tr>
  201. * <td>1</td> <td>64Hz</td>
  202. * </tr>
  203. * <tr>
  204. * <td>0</td> <td>128Hz</td>
  205. * </tr>
  206. * </table>
  207. *
  208. * \note The connection of events between modules requires the use of the
  209. * \ref asfdoc_sam0_events_group "SAM Event System (EVENTS) Driver"
  210. * to route output event of one module to the the input event of another.
  211. * For more information on event routing, refer to the event driver
  212. * documentation.
  213. *
  214. * \subsection asfdoc_sam0_rtc_count_module_overview_correction Digital Frequency Correction
  215. * The RTC module contains Digital Frequency Correction logic to compensate for
  216. * inaccurate source clock frequencies which would otherwise result in skewed
  217. * time measurements. The correction scheme requires that at least two bits
  218. * in the RTC module prescaler are reserved by the correction logic. As a
  219. * result of this implementation, frequency correction is only available when
  220. * the RTC is running from a 1Hz reference clock.
  221. *
  222. * The correction procedure is implemented by subtracting or adding a single
  223. * cycle from the RTC prescaler every 1024 RTC GCLK cycles. The adjustment is
  224. * applied the specified number of time (maximum 127) over 976 of these periods. The
  225. * corresponding correction in PPM will be given by:
  226. *
  227. * \f[ Correction(PPM) = \frac{VALUE}{999424}10^6 \f]
  228. *
  229. * The RTC clock will tick faster if provided with a positive correction value,
  230. * and slower when given a negative correction value.
  231. *
  232. *
  233. * \subsection asfdoc_sam0_rtc_count_module_overview_tamper_detect RTC Tamper Detect
  234. * see \ref asfdoc_sam0_rtc_tamper_detect
  235. *
  236. * \section asfdoc_sam0_rtc_count_special_considerations Special Considerations
  237. *
  238. * \subsection asfdoc_sam0_rtc_count_special_considerations_clock Clock Setup
  239. * \subsubsection asfdoc_sam0_rtc_count_clock_samd_r SAM D20/D21/R21/D10/D11/DA1/HA1 Clock Setup
  240. * The RTC is typically clocked by a specialized GCLK generator that has a
  241. * smaller prescaler than the others. By default the RTC clock is on, selected
  242. * to use the internal 32KHz RC-oscillator with a prescaler of 32, giving a
  243. * resulting clock frequency of 1KHz to the RTC. When the internal RTC
  244. * prescaler is set to 1024, this yields an end-frequency of 1Hz.
  245. *
  246. * The implementer also has the option to set other end-frequencies.
  247. * \ref asfdoc_sam0_rtc_count_rtc_out_freq "The table below" lists the
  248. * available RTC frequencies for each possible GCLK and RTC input prescaler
  249. * options.
  250. *
  251. * \anchor asfdoc_sam0_rtc_count_rtc_out_freq
  252. * <table>
  253. * <caption>RTC Output Frequencies from Allowable Input Clocks</caption>
  254. * <tr>
  255. * <th>End-frequency</th>
  256. * <th>GCLK prescaler</th>
  257. * <th>RTC prescaler</th>
  258. * </tr>
  259. * <tr>
  260. * <td>32KHz</td>
  261. * <td>1</td>
  262. * <td>1</td>
  263. * </tr>
  264. * <tr>
  265. * <td>1KHz</td>
  266. * <td>32</td>
  267. * <td>1</td>
  268. * </tr>
  269. * <tr>
  270. * <td>1Hz</td>
  271. * <td>32</td>
  272. * <td>1024</td>
  273. * </tr>
  274. * </table>
  275. *
  276. * The overall RTC module clocking scheme is shown in
  277. * \ref asfdoc_sam0_rtc_count_rtc_clock_fig "the figure below".
  278. *
  279. * \anchor asfdoc_sam0_rtc_count_rtc_clock_fig
  280. * \dot
  281. * digraph clocking_scheme {
  282. * rankdir=LR;
  283. * GCLK [shape="record", label="<f0> GCLK | <f1> RTC_GCLK",
  284. * bgcolor="lightgray", style="filled"];
  285. * RTCPRE [shape="record" label="<f0> RTC | <f1> RTC PRESCALER"];
  286. * RTC [shape="record", label="<f0> RTC | <f1> RTC CLOCK"];
  287. *
  288. * GCLK:f1 -> RTCPRE:f1;
  289. * RTCPRE:f1 -> RTC:f1;
  290. * }
  291. * \enddot
  292. *
  293. * \subsubsection asfdoc_sam0_rtc_count_clock_saml SAM L21/C20/C21/R30 Clock Setup
  294. * The RTC clock can be selected from OSC32K, XOSC32K, or OSCULP32K, and a 32KHz
  295. * or 1KHz oscillator clock frequency is required. This clock must be
  296. * configured and enabled in the 32KHz oscillator controller before using the RTC.
  297. *
  298. * The table below lists the available RTC clock \ref asfdoc_sam0_rtc_count_rtc_clk.
  299. *
  300. * \anchor asfdoc_sam0_rtc_count_rtc_clk
  301. * <table>
  302. * <caption>RTC Clocks Source</caption>
  303. * <tr>
  304. * <th>RTC clock frequency</th>
  305. * <th>Clock source</th>
  306. * <th>Description</th>
  307. * </tr>
  308. * <tr>
  309. * <td>1.024KHz</td>
  310. * <td>ULP1K</td>
  311. * <td>1.024KHz from 32KHz internal ULP oscillator</td>
  312. * </tr>
  313. * <tr>
  314. * <td>32.768KHz</td>
  315. * <td>ULP32K</td>
  316. * <td>32.768KHz from 32KHz internal ULP oscillator</td>
  317. * </tr>
  318. * <tr>
  319. * <td>1.024KHz</td>
  320. * <td>OSC1K</td>
  321. * <td>1.024KHz from 32KHz internal oscillator</td>
  322. * </tr>
  323. * <tr>
  324. * <td>32.768KHz</td>
  325. * <td>OSC32K</td>
  326. * <td>32.768KHz from 32KHz internal oscillator</td>
  327. * </tr>
  328. * <tr>
  329. * <td>1.024KHz</td>
  330. * <td>XOSC1K</td>
  331. * <td>1.024KHz from 32KHz internal oscillator</td>
  332. * </tr>
  333. * <tr>
  334. * <td>32.768KHz</td>
  335. * <td>XOSC32K</td>
  336. * <td>32.768KHz from 32KHz external crystal oscillator</td>
  337. * </tr>
  338. * </table>
  339. *
  340. * \section asfdoc_sam0_rtc_count_extra_info Extra Information
  341. *
  342. * For extra information, see \ref asfdoc_sam0_rtc_count_extra. This includes:
  343. * - \ref asfdoc_sam0_rtc_count_extra_acronyms
  344. * - \ref asfdoc_sam0_rtc_count_extra_dependencies
  345. * - \ref asfdoc_sam0_rtc_count_extra_errata
  346. * - \ref asfdoc_sam0_rtc_count_extra_history
  347. *
  348. *
  349. * \section asfdoc_sam0_rtc_count_examples Examples
  350. *
  351. * For a list of examples related to this driver, see
  352. * \ref asfdoc_sam0_rtc_count_exqsg.
  353. *
  354. *
  355. * \section asfdoc_sam0_rtc_count_api_overview API Overview
  356. * @{
  357. */
  358. #include <compiler.h>
  359. #include <conf_clocks.h>
  360. #if RTC_COUNT_ASYNC == true
  361. # include <system_interrupt.h>
  362. #endif
  363. #ifdef __cplusplus
  364. extern "C" {
  365. #endif
  366. /**
  367. * \name Driver Feature Definition
  368. *
  369. * Define port features set according to different device family.
  370. * @{
  371. */
  372. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30) || defined(__DOXYGEN__)
  373. /** RTC periodic interval interrupt. */
  374. # define FEATURE_RTC_PERIODIC_INT
  375. /** RTC prescaler is off. */
  376. # define FEATURE_RTC_PRESCALER_OFF
  377. /** RTC clock selection. */
  378. # define FEATURE_RTC_CLOCK_SELECTION
  379. # if !(SAMC20) && !(SAMC21)
  380. /** General purpose registers. */
  381. # define FEATURE_RTC_GENERAL_PURPOSE_REG
  382. # endif
  383. #else
  384. /** RTC continuously updated. */
  385. # define FEATURE_RTC_CONTINUOUSLY_UPDATED
  386. #endif
  387. #if (SAML22) || defined(__DOXYGEN__)
  388. /** RTC tamper detection. */
  389. # define FEATURE_RTC_TAMPER_DETECTION
  390. #endif
  391. /*@}*/
  392. #ifdef FEATURE_RTC_CLOCK_SELECTION
  393. /**
  394. * \brief Available clock source for RTC.
  395. * RTC clock source.
  396. */
  397. enum rtc_clock_sel {
  398. /** 1.024KHz from 32KHz internal ULP oscillator */
  399. RTC_CLOCK_SELECTION_ULP1K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val,
  400. /** 32.768KHz from 32KHz internal ULP oscillator */
  401. RTC_CLOCK_SELECTION_ULP32K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP32K_Val,
  402. #if !(SAML22)
  403. /** 1.024KHz from 32KHz internal oscillator */
  404. RTC_CLOCK_SELECTION_OSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC1K_Val,
  405. /** 32.768KHz from 32KHz internal oscillator */
  406. RTC_CLOCK_SELECTION_OSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC32K_Val,
  407. #endif
  408. /** 1.024KHz from 32KHz external oscillator */
  409. RTC_CLOCK_SELECTION_XOSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val,
  410. /** 32.768KHz from 32.768KHz external crystal oscillator */
  411. RTC_CLOCK_SELECTION_XOSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC32K_Val,
  412. };
  413. #endif
  414. /**
  415. * \brief Available operation modes for the RTC.
  416. *
  417. * RTC Count operating modes, to select the counting width and associated module
  418. * operation.
  419. */
  420. enum rtc_count_mode {
  421. /** RTC Count module operates in 16-bit mode */
  422. RTC_COUNT_MODE_16BIT = 0,
  423. /** RTC Count module operates in 32-bit mode */
  424. RTC_COUNT_MODE_32BIT = 1,
  425. };
  426. #if !defined (RTC_NUM_OF_COMP16) && defined(RTC_COMP16_NUM)
  427. #define RTC_NUM_OF_COMP16 RTC_COMP16_NUM
  428. #endif
  429. /**
  430. * \brief Available compare channels.
  431. *
  432. * \note Not all compare channels are available in all devices and modes.
  433. */
  434. enum rtc_count_compare {
  435. /** Compare channel 0 */
  436. RTC_COUNT_COMPARE_0 = 0,
  437. #if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
  438. /** Compare channel 1 */
  439. RTC_COUNT_COMPARE_1 = 1,
  440. #endif
  441. #if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
  442. /** Compare channel 2 */
  443. RTC_COUNT_COMPARE_2 = 2,
  444. #endif
  445. #if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
  446. /** Compare channel 3 */
  447. RTC_COUNT_COMPARE_3 = 3,
  448. #endif
  449. #if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
  450. /** Compare channel 4 */
  451. RTC_COUNT_COMPARE_4 = 4,
  452. #endif
  453. #if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
  454. /** Compare channel 5 */
  455. RTC_COUNT_COMPARE_5 = 5,
  456. #endif
  457. };
  458. #ifdef FEATURE_RTC_PERIODIC_INT
  459. /**
  460. * \brief Available periodic interval source.
  461. */
  462. enum rtc_count_periodic_interval{
  463. /** Periodic interval 0 */
  464. RTC_COUNT_PERIODIC_INTERVAL_0 = 0,
  465. /** Periodic interval 1 */
  466. RTC_COUNT_PERIODIC_INTERVAL_1 = 1,
  467. /** Periodic interval 2 */
  468. RTC_COUNT_PERIODIC_INTERVAL_2 = 2,
  469. /** Periodic interval 3 */
  470. RTC_COUNT_PERIODIC_INTERVAL_3 = 3,
  471. /** Periodic interval 4 */
  472. RTC_COUNT_PERIODIC_INTERVAL_4 = 4,
  473. /** Periodic interval 5 */
  474. RTC_COUNT_PERIODIC_INTERVAL_5 = 5,
  475. /** Periodic interval 6 */
  476. RTC_COUNT_PERIODIC_INTERVAL_6 = 6,
  477. /** Periodic interval 7 */
  478. RTC_COUNT_PERIODIC_INTERVAL_7 = 7,
  479. };
  480. #endif
  481. #if RTC_COUNT_ASYNC == true
  482. #ifdef FEATURE_RTC_PERIODIC_INT
  483. /**
  484. * \brief Callback types.
  485. *
  486. * The available callback types for the RTC count module.
  487. */
  488. enum rtc_count_callback {
  489. /** Callback for Periodic Interval 0 Interrupt */
  490. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_0 = 0,
  491. /** Callback for Periodic Interval 1 Interrupt */
  492. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_1,
  493. /** Callback for Periodic Interval 2 Interrupt */
  494. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_2,
  495. /** Callback for Periodic Interval 3 Interrupt */
  496. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_3,
  497. /** Callback for Periodic Interval 4 Interrupt */
  498. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_4,
  499. /** Callback for Periodic Interval 5 Interrupt */
  500. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_5,
  501. /** Callback for Periodic Interval 6 Interrupt */
  502. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_6,
  503. /** Callback for Periodic Interval 7 Interrupt */
  504. RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_7,
  505. /** Callback for compare channel 0 */
  506. RTC_COUNT_CALLBACK_COMPARE_0,
  507. # if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
  508. /** Callback for compare channel 1 */
  509. RTC_COUNT_CALLBACK_COMPARE_1,
  510. # endif
  511. # if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
  512. /** Callback for compare channel 2 */
  513. RTC_COUNT_CALLBACK_COMPARE_2,
  514. # endif
  515. # if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
  516. /** Callback for compare channel 3 */
  517. RTC_COUNT_CALLBACK_COMPARE_3,
  518. # endif
  519. # if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
  520. /** Callback for compare channel 4 */
  521. RTC_COUNT_CALLBACK_COMPARE_4,
  522. # endif
  523. # if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
  524. /** Callback for compare channel 5 */
  525. RTC_COUNT_CALLBACK_COMPARE_5,
  526. # endif
  527. #ifdef FEATURE_RTC_TAMPER_DETECTION
  528. /** Callback for tamper */
  529. RTC_COUNT_CALLBACK_TAMPER,
  530. #endif
  531. /** Callback for overflow */
  532. RTC_COUNT_CALLBACK_OVERFLOW,
  533. # if !defined(__DOXYGEN__)
  534. /** Total number of callbacks */
  535. _RTC_COUNT_CALLBACK_N
  536. # endif
  537. };
  538. #else
  539. /**
  540. * \brief Callback types.
  541. *
  542. * The available callback types for the RTC count module.
  543. */
  544. enum rtc_count_callback {
  545. /** Callback for compare channel 0 */
  546. RTC_COUNT_CALLBACK_COMPARE_0 = 0,
  547. # if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
  548. /** Callback for compare channel 1 */
  549. RTC_COUNT_CALLBACK_COMPARE_1,
  550. # endif
  551. # if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
  552. /** Callback for compare channel 2 */
  553. RTC_COUNT_CALLBACK_COMPARE_2,
  554. # endif
  555. # if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
  556. /** Callback for compare channel 3 */
  557. RTC_COUNT_CALLBACK_COMPARE_3,
  558. # endif
  559. # if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
  560. /** Callback for compare channel 4 */
  561. RTC_COUNT_CALLBACK_COMPARE_4,
  562. # endif
  563. # if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
  564. /** Callback for compare channel 5 */
  565. RTC_COUNT_CALLBACK_COMPARE_5,
  566. # endif
  567. #ifdef FEATURE_RTC_TAMPER_DETECTION
  568. /** Callback for tamper */
  569. RTC_COUNT_CALLBACK_TAMPER,
  570. #endif
  571. /** Callback for overflow */
  572. RTC_COUNT_CALLBACK_OVERFLOW,
  573. # if !defined(__DOXYGEN__)
  574. /** Total number of callbacks */
  575. _RTC_COUNT_CALLBACK_N
  576. # endif
  577. };
  578. #endif
  579. # if !defined(__DOXYGEN__)
  580. typedef void (*rtc_count_callback_t)(void);
  581. # endif
  582. #endif
  583. #ifdef FEATURE_RTC_PRESCALER_OFF
  584. /**
  585. * \brief RTC input clock prescaler settings.
  586. *
  587. * The available input clock prescaler values for the RTC count module.
  588. */
  589. enum rtc_count_prescaler {
  590. /** RTC prescaler is off, and the input clock frequency is
  591. prescaled by a factor of 1 */
  592. RTC_COUNT_PRESCALER_OFF = RTC_MODE0_CTRLA_PRESCALER_OFF,
  593. /** RTC input clock frequency is prescaled by a factor of 1 */
  594. RTC_COUNT_PRESCALER_DIV_1 = RTC_MODE0_CTRLA_PRESCALER_DIV1,
  595. /** RTC input clock frequency is prescaled by a factor of 2 */
  596. RTC_COUNT_PRESCALER_DIV_2 = RTC_MODE0_CTRLA_PRESCALER_DIV2,
  597. /** RTC input clock frequency is prescaled by a factor of 4 */
  598. RTC_COUNT_PRESCALER_DIV_4 = RTC_MODE0_CTRLA_PRESCALER_DIV4,
  599. /** RTC input clock frequency is prescaled by a factor of 8 */
  600. RTC_COUNT_PRESCALER_DIV_8 = RTC_MODE0_CTRLA_PRESCALER_DIV8,
  601. /** RTC input clock frequency is prescaled by a factor of 16 */
  602. RTC_COUNT_PRESCALER_DIV_16 = RTC_MODE0_CTRLA_PRESCALER_DIV16,
  603. /** RTC input clock frequency is prescaled by a factor of 32 */
  604. RTC_COUNT_PRESCALER_DIV_32 = RTC_MODE0_CTRLA_PRESCALER_DIV32,
  605. /** RTC input clock frequency is prescaled by a factor of 64 */
  606. RTC_COUNT_PRESCALER_DIV_64 = RTC_MODE0_CTRLA_PRESCALER_DIV64,
  607. /** RTC input clock frequency is prescaled by a factor of 128 */
  608. RTC_COUNT_PRESCALER_DIV_128 = RTC_MODE0_CTRLA_PRESCALER_DIV128,
  609. /** RTC input clock frequency is prescaled by a factor of 256 */
  610. RTC_COUNT_PRESCALER_DIV_256 = RTC_MODE0_CTRLA_PRESCALER_DIV256,
  611. /** RTC input clock frequency is prescaled by a factor of 512 */
  612. RTC_COUNT_PRESCALER_DIV_512 = RTC_MODE0_CTRLA_PRESCALER_DIV512,
  613. /** RTC input clock frequency is prescaled by a factor of 1024 */
  614. RTC_COUNT_PRESCALER_DIV_1024 = RTC_MODE0_CTRLA_PRESCALER_DIV1024,
  615. };
  616. #else
  617. /**
  618. * \brief RTC input clock prescaler settings.
  619. *
  620. * The available input clock prescaler values for the RTC count module.
  621. */
  622. enum rtc_count_prescaler {
  623. /** RTC input clock frequency is prescaled by a factor of 1 */
  624. RTC_COUNT_PRESCALER_DIV_1 = RTC_MODE0_CTRL_PRESCALER_DIV1,
  625. /** RTC input clock frequency is prescaled by a factor of 2 */
  626. RTC_COUNT_PRESCALER_DIV_2 = RTC_MODE0_CTRL_PRESCALER_DIV2,
  627. /** RTC input clock frequency is prescaled by a factor of 4 */
  628. RTC_COUNT_PRESCALER_DIV_4 = RTC_MODE0_CTRL_PRESCALER_DIV4,
  629. /** RTC input clock frequency is prescaled by a factor of 8 */
  630. RTC_COUNT_PRESCALER_DIV_8 = RTC_MODE0_CTRL_PRESCALER_DIV8,
  631. /** RTC input clock frequency is prescaled by a factor of 16 */
  632. RTC_COUNT_PRESCALER_DIV_16 = RTC_MODE0_CTRL_PRESCALER_DIV16,
  633. /** RTC input clock frequency is prescaled by a factor of 32 */
  634. RTC_COUNT_PRESCALER_DIV_32 = RTC_MODE0_CTRL_PRESCALER_DIV32,
  635. /** RTC input clock frequency is prescaled by a factor of 64 */
  636. RTC_COUNT_PRESCALER_DIV_64 = RTC_MODE0_CTRL_PRESCALER_DIV64,
  637. /** RTC input clock frequency is prescaled by a factor of 128 */
  638. RTC_COUNT_PRESCALER_DIV_128 = RTC_MODE0_CTRL_PRESCALER_DIV128,
  639. /** RTC input clock frequency is prescaled by a factor of 256 */
  640. RTC_COUNT_PRESCALER_DIV_256 = RTC_MODE0_CTRL_PRESCALER_DIV256,
  641. /** RTC input clock frequency is prescaled by a factor of 512 */
  642. RTC_COUNT_PRESCALER_DIV_512 = RTC_MODE0_CTRL_PRESCALER_DIV512,
  643. /** RTC input clock frequency is prescaled by a factor of 1024 */
  644. RTC_COUNT_PRESCALER_DIV_1024 = RTC_MODE0_CTRL_PRESCALER_DIV1024,
  645. };
  646. #endif
  647. /**
  648. * \brief RTC Count event enable/disable structure.
  649. *
  650. * Event flags for the \ref rtc_count_enable_events() and
  651. * \ref rtc_count_disable_events().
  652. */
  653. struct rtc_count_events {
  654. /** Generate an output event on each overflow of the RTC count */
  655. bool generate_event_on_overflow;
  656. /** Generate an output event on a compare channel match against the RTC
  657. * count */
  658. bool generate_event_on_compare[RTC_NUM_OF_COMP16];
  659. /** Generate an output event periodically at a binary division of the RTC
  660. * counter frequency */
  661. bool generate_event_on_periodic[8];
  662. #ifdef FEATURE_RTC_TAMPER_DETECTION
  663. /** Generate an output event on every tamper input */
  664. bool generate_event_on_tamper;
  665. /** Tamper input event and capture the COUNT value */
  666. bool on_event_to_tamper;
  667. #endif
  668. };
  669. #if !defined(__DOXYGEN__)
  670. /**
  671. * \brief Device structure.
  672. */
  673. struct rtc_module {
  674. /** RTC hardware module */
  675. Rtc *hw;
  676. /** Operation mode of count */
  677. enum rtc_count_mode mode;
  678. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  679. /** Set if counter value should be continuously updated */
  680. bool continuously_update;
  681. #endif
  682. # if RTC_COUNT_ASYNC == true
  683. /** Pointers to callback functions */
  684. volatile rtc_count_callback_t callbacks[_RTC_COUNT_CALLBACK_N];
  685. /** Mask for registered callbacks */
  686. volatile uint16_t registered_callback;
  687. /** Mask for enabled callbacks */
  688. volatile uint16_t enabled_callback;
  689. # endif
  690. };
  691. #endif
  692. /**
  693. * \brief RTC Count configuration structure.
  694. *
  695. * Configuration structure for the RTC instance. This structure should
  696. * be initialized using the \ref rtc_count_get_config_defaults() before any
  697. * user configurations are set.
  698. */
  699. struct rtc_count_config {
  700. /** Input clock prescaler for the RTC module */
  701. enum rtc_count_prescaler prescaler;
  702. /** Select the operation mode of the RTC */
  703. enum rtc_count_mode mode;
  704. /** If true, clears the counter value on compare match. Only available
  705. * whilst running in 32-bit mode */
  706. bool clear_on_match;
  707. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  708. /** Continuously update the counter value so no synchronization is
  709. * needed for reading */
  710. bool continuously_update;
  711. #endif
  712. #if (SAML21XXXB) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  713. /** Enable count read synchronization. The COUNT value requires
  714. * synchronization when reading. Disabling the synchronization
  715. * will prevent the COUNT value from displaying the current value. */
  716. bool enable_read_sync;
  717. #endif
  718. /** Array of Compare values. Not all Compare values are available in 32-bit
  719. * mode */
  720. uint32_t compare_values[RTC_NUM_OF_COMP16];
  721. };
  722. /**
  723. * \name Configuration and Initialization
  724. * @{
  725. */
  726. /**
  727. * \brief Gets the RTC default configurations.
  728. *
  729. * Initializes the configuration structure to default values. This
  730. * function should be called at the start of any RTC initialization.
  731. *
  732. * The default configuration is:
  733. * - Input clock divided by a factor of 1024
  734. * - RTC in 32-bit mode
  735. * - Clear on compare match off
  736. * - Continuously sync count register off
  737. * - No event source on
  738. * - All compare values equal 0
  739. * - Count read synchronization is enabled for SAM L22
  740. *
  741. * \param[out] config Configuration structure to be initialized to default
  742. * values
  743. */
  744. static inline void rtc_count_get_config_defaults(
  745. struct rtc_count_config *const config)
  746. {
  747. /* Sanity check argument */
  748. Assert(config);
  749. /* Set default into configuration structure */
  750. config->prescaler = RTC_COUNT_PRESCALER_DIV_1024;
  751. config->mode = RTC_COUNT_MODE_32BIT;
  752. config->clear_on_match = false;
  753. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  754. config->continuously_update = false;
  755. #endif
  756. #if (SAML21XXXB) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  757. config->enable_read_sync = true;
  758. #endif
  759. for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
  760. config->compare_values[i] = 0;
  761. }
  762. }
  763. void rtc_count_reset(struct rtc_module *const module);
  764. void rtc_count_enable(struct rtc_module *const module);
  765. void rtc_count_disable(struct rtc_module *const module);
  766. #if (RTC_INST_NUM > 1) && !defined(__DOXYGEN__)
  767. /**
  768. * \internal Find the index of given RTC module instance.
  769. *
  770. * \param[in] RTC module instance pointer
  771. *
  772. * \return Index of the given AC module instance.
  773. */
  774. uint8_t _rtc_get_inst_index(
  775. Rtc *const hw)
  776. {
  777. /* List of available RTC modules */
  778. static Rtc *const rtc_modules[RTC_INST_NUM] = RTC_INSTS;
  779. /* Find index for RTC instance */
  780. for (uint32_t i = 0; i < RTC_INST_NUM; i++) {
  781. if (hw == rtc_modules[i]) {
  782. return i;
  783. }
  784. }
  785. /* Invalid data given */
  786. Assert(false);
  787. return 0;
  788. }
  789. #endif /* (RTC_INST_NUM > 1) && !defined(__DOXYGEN__) */
  790. enum status_code rtc_count_init(
  791. struct rtc_module *const module,
  792. Rtc *const hw,
  793. const struct rtc_count_config *const config);
  794. enum status_code rtc_count_frequency_correction(
  795. struct rtc_module *const module,
  796. const int8_t value);
  797. /** @} */
  798. /** \name Count and Compare Value Management
  799. * @{
  800. */
  801. enum status_code rtc_count_set_count(
  802. struct rtc_module *const module,
  803. const uint32_t count_value);
  804. uint32_t rtc_count_get_count(struct rtc_module *const module);
  805. enum status_code rtc_count_set_compare(
  806. struct rtc_module *const module,
  807. const uint32_t comp_value,
  808. const enum rtc_count_compare comp_index);
  809. enum status_code rtc_count_get_compare(
  810. struct rtc_module *const module,
  811. uint32_t *const comp_value,
  812. const enum rtc_count_compare comp_index);
  813. enum status_code rtc_count_set_period(
  814. struct rtc_module *const module,
  815. uint16_t period_value);
  816. enum status_code rtc_count_get_period(
  817. struct rtc_module *const module,
  818. uint16_t *const period_value);
  819. /** @} */
  820. /** \name Status Management
  821. * @{
  822. */
  823. /**
  824. * \brief Check if an RTC overflow has occurred.
  825. *
  826. * Checks the overflow flag in the RTC. The flag is set when there
  827. * is an overflow in the clock.
  828. *
  829. * \param[in,out] module RTC hardware module
  830. *
  831. * \return Overflow state of the RTC module.
  832. *
  833. * \retval true If the RTC count value has overflowed
  834. * \retval false If the RTC count value has not overflowed
  835. */
  836. static inline bool rtc_count_is_overflow(struct rtc_module *const module)
  837. {
  838. /* Sanity check arguments */
  839. Assert(module);
  840. Assert(module->hw);
  841. Rtc *const rtc_module = module->hw;
  842. /* Return status of flag */
  843. return (rtc_module->MODE0.INTFLAG.reg & RTC_MODE0_INTFLAG_OVF);
  844. }
  845. /**
  846. * \brief Clears the RTC overflow flag.
  847. *
  848. * Clears the RTC module counter overflow flag, so that new overflow conditions
  849. * can be detected.
  850. *
  851. * \param[in,out] module RTC hardware module
  852. */
  853. static inline void rtc_count_clear_overflow(struct rtc_module *const module)
  854. {
  855. /* Sanity check arguments */
  856. Assert(module);
  857. Assert(module->hw);
  858. Rtc *const rtc_module = module->hw;
  859. /* Clear OVF flag */
  860. rtc_module->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_OVF;
  861. }
  862. #ifdef FEATURE_RTC_PERIODIC_INT
  863. /**
  864. * \brief Check if an RTC periodic interval interrupt has occurred.
  865. *
  866. * Checks the periodic interval flag in the RTC.
  867. *
  868. * \param[in,out] module RTC hardware module
  869. * \param[in] n RTC periodic interval interrupt
  870. *
  871. * \return Periodic interval interrupt state of the RTC module.
  872. *
  873. * \retval true RTC periodic interval interrupt occurs
  874. * \retval false RTC periodic interval interrupt doesn't occur
  875. */
  876. static inline bool rtc_count_is_periodic_interval(struct rtc_module *const module,
  877. enum rtc_count_periodic_interval n)
  878. {
  879. /* Sanity check arguments */
  880. Assert(module);
  881. Assert(module->hw);
  882. Rtc *const rtc_module = module->hw;
  883. /* Return status of flag */
  884. return (rtc_module->MODE0.INTFLAG.reg & RTC_MODE0_INTFLAG_PER(1 << n));
  885. }
  886. /**
  887. * \brief Clears the RTC periodic interval flag.
  888. *
  889. * Clears the RTC module counter periodic interval flag, so that new periodic
  890. * interval conditions can be detected.
  891. *
  892. * \param[in,out] module RTC hardware module
  893. * \param[in] n RTC periodic interval interrupt
  894. */
  895. static inline void rtc_count_clear_periodic_interval(struct rtc_module *const module,
  896. enum rtc_count_periodic_interval n)
  897. {
  898. /* Sanity check arguments */
  899. Assert(module);
  900. Assert(module->hw);
  901. Rtc *const rtc_module = module->hw;
  902. /* Clear periodic interval flag */
  903. rtc_module->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_PER(1 << n);
  904. }
  905. #endif
  906. bool rtc_count_is_compare_match(
  907. struct rtc_module *const module,
  908. const enum rtc_count_compare comp_index);
  909. enum status_code rtc_count_clear_compare_match(
  910. struct rtc_module *const module,
  911. const enum rtc_count_compare comp_index);
  912. /** @} */
  913. /**
  914. * \name Event Management
  915. * @{
  916. */
  917. /**
  918. * \brief Enables an RTC event output.
  919. *
  920. * Enables one or more output events from the RTC module. See
  921. * \ref rtc_count_events for a list of events this module supports.
  922. *
  923. * \note Events cannot be altered while the module is enabled.
  924. *
  925. * \param[in,out] module RTC hardware module
  926. * \param[in] events Struct containing flags of events to enable
  927. */
  928. static inline void rtc_count_enable_events(
  929. struct rtc_module *const module,
  930. struct rtc_count_events *const events)
  931. {
  932. /* Sanity check arguments */
  933. Assert(module);
  934. Assert(module->hw);
  935. Rtc *const rtc_module = module->hw;
  936. uint32_t event_mask = 0;
  937. /* Check if the user has requested an overflow event */
  938. if (events->generate_event_on_overflow) {
  939. event_mask |= RTC_MODE0_EVCTRL_OVFEO;
  940. }
  941. /* Check if the user has requested any compare events */
  942. for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
  943. if (events->generate_event_on_compare[i]) {
  944. event_mask |= RTC_MODE0_EVCTRL_CMPEO(1 << i);
  945. }
  946. }
  947. /* Check if the user has requested any periodic events */
  948. for (uint8_t i = 0; i < 8; i++) {
  949. if (events->generate_event_on_periodic[i]) {
  950. event_mask |= RTC_MODE0_EVCTRL_PEREO(1 << i);
  951. }
  952. }
  953. #ifdef FEATURE_RTC_TAMPER_DETECTION
  954. /* Check if the user has requested a tamper event output. */
  955. if (events->generate_event_on_tamper) {
  956. event_mask |= RTC_MODE0_EVCTRL_TAMPEREO;
  957. }
  958. /* Check if the user has requested a tamper event input. */
  959. if (events->on_event_to_tamper) {
  960. event_mask |= RTC_MODE0_EVCTRL_TAMPEVEI;
  961. }
  962. #endif
  963. /* Enable given event(s). */
  964. rtc_module->MODE0.EVCTRL.reg |= event_mask;
  965. }
  966. /**
  967. * \brief Disables an RTC event output.
  968. *
  969. * Disabled one or more output events from the RTC module. See
  970. * \ref rtc_count_events for a list of events this module supports.
  971. *
  972. * \note Events cannot be altered while the module is enabled.
  973. *
  974. * \param[in,out] module RTC hardware module
  975. * \param[in] events Struct containing flags of events to disable
  976. */
  977. static inline void rtc_count_disable_events(
  978. struct rtc_module *const module,
  979. struct rtc_count_events *const events)
  980. {
  981. /* Sanity check arguments */
  982. Assert(module);
  983. Assert(module->hw);
  984. Rtc *const rtc_module = module->hw;
  985. uint32_t event_mask = 0;
  986. /* Check if the user has requested an overflow event */
  987. if (events->generate_event_on_overflow) {
  988. event_mask |= RTC_MODE0_EVCTRL_OVFEO;
  989. }
  990. /* Check if the user has requested any compare events */
  991. for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
  992. if (events->generate_event_on_compare[i]) {
  993. event_mask |= RTC_MODE0_EVCTRL_CMPEO(1 << i);
  994. }
  995. }
  996. /* Check if the user has requested any periodic events */
  997. for (uint8_t i = 0; i < 8; i++) {
  998. if (events->generate_event_on_periodic[i]) {
  999. event_mask |= RTC_MODE0_EVCTRL_PEREO(1 << i);
  1000. }
  1001. }
  1002. #ifdef FEATURE_RTC_TAMPER_DETECTION
  1003. /* Check if the user has requested a tamper event output. */
  1004. if (events->generate_event_on_tamper) {
  1005. event_mask |= RTC_MODE0_EVCTRL_TAMPEREO;
  1006. }
  1007. /* Check if the user has requested a tamper event input. */
  1008. if (events->on_event_to_tamper) {
  1009. event_mask |= RTC_MODE0_EVCTRL_TAMPEVEI;
  1010. }
  1011. #endif
  1012. /* Disable given event(s). */
  1013. rtc_module->MODE0.EVCTRL.reg &= ~event_mask;
  1014. }
  1015. /** @} */
  1016. #ifdef FEATURE_RTC_GENERAL_PURPOSE_REG
  1017. /**
  1018. * \name RTC General Purpose Registers
  1019. * @{
  1020. */
  1021. /**
  1022. * \brief Write a value into general purpose register.
  1023. *
  1024. * \param[in] module Pointer to the software instance struct
  1025. * \param[in] n General purpose type
  1026. * \param[in] index General purpose register index (0..3)
  1027. *
  1028. */
  1029. static inline void rtc_write_general_purpose_reg(
  1030. struct rtc_module *const module,
  1031. const uint8_t index,
  1032. uint32_t value)
  1033. {
  1034. /* Sanity check arguments */
  1035. Assert(module);
  1036. Assert(module->hw);
  1037. Assert(index <= 3);
  1038. Rtc *const rtc_module = module->hw;
  1039. rtc_module->MODE0.GP[index].reg = value;
  1040. }
  1041. /**
  1042. * \brief Read the value from general purpose register.
  1043. *
  1044. * \param[in] module Pointer to the software instance struct
  1045. * \param[in] index General purpose register index (0..3)
  1046. *
  1047. * \return Value of general purpose register.
  1048. */
  1049. static inline uint32_t rtc_read_general_purpose_reg(
  1050. struct rtc_module *const module,
  1051. const uint8_t index)
  1052. {
  1053. /* Sanity check arguments */
  1054. Assert(module);
  1055. Assert(module->hw);
  1056. Assert(index <= 3);
  1057. Rtc *const rtc_module = module->hw;
  1058. return rtc_module->MODE0.GP[index].reg;
  1059. }
  1060. /** @} */
  1061. #endif
  1062. #ifdef FEATURE_RTC_TAMPER_DETECTION
  1063. #include "rtc_tamper.h"
  1064. /**
  1065. * \brief Get the tamper stamp value.
  1066. *
  1067. * \param[in,out] module Pointer to the software instance struct
  1068. *
  1069. * \return The current tamper stamp value as a 32-bit unsigned integer.
  1070. */
  1071. uint32_t rtc_tamper_get_stamp (struct rtc_module *const module);
  1072. #endif
  1073. /** @} */
  1074. #ifdef __cplusplus
  1075. }
  1076. #endif
  1077. /**
  1078. * \page asfdoc_sam0_rtc_count_extra Extra Information for RTC COUNT Driver
  1079. *
  1080. * \section asfdoc_sam0_rtc_count_extra_acronyms Acronyms
  1081. * Below is a table listing the acronyms used in this module, along with their
  1082. * intended meanings.
  1083. *
  1084. * <table>
  1085. * <tr>
  1086. * <th>Acronym</td>
  1087. * <th>Description</td>
  1088. * </tr>
  1089. * <tr>
  1090. * <td>RTC</td>
  1091. * <td>Real Time Counter</td>
  1092. * </tr>
  1093. * <tr>
  1094. * <td>PPM</td>
  1095. * <td>Part Per Million</td>
  1096. * </tr>
  1097. * <tr>
  1098. * <td>RC</td>
  1099. * <td>Resistor/Capacitor</td>
  1100. * </tr>
  1101. * </table>
  1102. *
  1103. *
  1104. * \section asfdoc_sam0_rtc_count_extra_dependencies Dependencies
  1105. * This driver has the following dependencies:
  1106. *
  1107. * - None
  1108. *
  1109. *
  1110. * \section asfdoc_sam0_rtc_count_extra_errata Errata
  1111. * There are no errata related to this driver.
  1112. *
  1113. *
  1114. * \section asfdoc_sam0_rtc_count_extra_history Module History
  1115. * An overview of the module history is presented in the table below, with
  1116. * details on the enhancements and fixes made to the module since its first
  1117. * release. The current version of this corresponds to the newest version in
  1118. * the table.
  1119. *
  1120. * <table>
  1121. * <tr>
  1122. * <th>Changelog</th>
  1123. * </tr>
  1124. * <tr>
  1125. * <td>Added support for SAM C21</td>
  1126. * </tr>
  1127. * <tr>
  1128. * <td>Added support for SAM L21/L22</td>
  1129. * </tr>
  1130. * <tr>
  1131. * <td>Added support for SAM R30</td>
  1132. * </tr>
  1133. * <tr>
  1134. * <td>Added support for RTC tamper feature</td>
  1135. * </tr>
  1136. * <tr>
  1137. * <td>
  1138. * Added driver instance parameter to all API function calls, except
  1139. * get_config_defaults
  1140. * </td>
  1141. * </tr>
  1142. * <tr>
  1143. * <td>
  1144. * Updated initialization function to also enable the digital interface
  1145. * clock to the module if it is disabled
  1146. * </td>
  1147. * </tr>
  1148. * <tr>
  1149. * <td>Initial Release</td>
  1150. * </tr>
  1151. * </table>
  1152. */
  1153. /**
  1154. * \page asfdoc_sam0_rtc_count_exqsg Examples for RTC (COUNT) Driver
  1155. *
  1156. * This is a list of the available Quick Start guides (QSGs) and example
  1157. * applications for \ref asfdoc_sam0_rtc_count_group. QSGs are simple
  1158. * examples with step-by-step instructions to configure and use this driver in a
  1159. * selection of use cases. Note that a QSG can be compiled as a standalone
  1160. * application or be added to the user application.
  1161. *
  1162. * - \subpage asfdoc_sam0_rtc_count_basic_use_case
  1163. * \if RTC_COUNT_CALLBACK_MODE
  1164. * - \subpage asfdoc_sam0_rtc_count_callback_use_case
  1165. * \endif
  1166. * - \subpage asfdoc_sam0_rtc_tamper_dma_use_case
  1167. *
  1168. * \page asfdoc_sam0_rtc_count_document_revision_history Document Revision History
  1169. *
  1170. * <table>
  1171. * <tr>
  1172. * <th>Doc. Rev.</th>
  1173. * <th>Date</th>
  1174. * <th>Comments</th>
  1175. * </tr>
  1176. * <tr>
  1177. * <td>42111E</td>
  1178. * <td>12/2015</td>
  1179. * <td>Added support for SAM L21/L22, SAM C21, SAM D09, SAMR30 and SAM DA1</td>
  1180. * </tr>
  1181. * <tr>
  1182. * <td>42111D</td>
  1183. * <td>12/2014</td>
  1184. * <td>Added support for SAM R21 and SAM D10/D11</td>
  1185. * </tr>
  1186. * <tr>
  1187. * <td>42111C</td>
  1188. * <td>01/2014</td>
  1189. * <td>Added support for SAM D21</td>
  1190. * </tr>
  1191. * <tr>
  1192. * <td>42111B</td>
  1193. * <td>06/2013</td>
  1194. * <td>Added additional documentation on the event system. Corrected
  1195. * documentation typos.</td>
  1196. * </tr>
  1197. * <tr>
  1198. * <td>42111A</td>
  1199. * <td>06/2013</td>
  1200. * <td>Initial release</td>
  1201. * </tr>
  1202. * </table>
  1203. */
  1204. #endif /* RTC_COUNT_H_INCLUDED */