rtc_calendar.h 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /**
  2. * \file
  3. *
  4. * \brief SAM RTC Driver (Calendar 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_CALENDAR_H_INCLUDED
  47. #define RTC_CALENDAR_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_rtc_calendar_group SAM RTC Calendar (RTC CAL) 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 Calendar operating mode, for the configuration and
  54. * retrieval of the current time and date as maintained by the RTC module.
  55. The following driver API modes are covered by this manual:
  56. *
  57. * - Polled APIs
  58. * \if RTC_CALENDAR_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_calendar_prerequisites
  77. * - \ref asfdoc_sam0_rtc_calendar_module_overview
  78. * - \ref asfdoc_sam0_rtc_calendar_special_considerations
  79. * - \ref asfdoc_sam0_rtc_calendar_extra_info
  80. * - \ref asfdoc_sam0_rtc_calendar_examples
  81. * - \ref asfdoc_sam0_rtc_calendar_api_overview
  82. *
  83. *
  84. * \section asfdoc_sam0_rtc_calendar_prerequisites Prerequisites
  85. *
  86. * There are no prerequisites for this module.
  87. *
  88. *
  89. * \section asfdoc_sam0_rtc_calendar_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 Calendar mode. This allows for an
  98. * easy integration of a real time clock and calendar into a user application
  99. * to track the passing of time and/or perform scheduled tasks.
  100. *
  101. * Whilst operating in Calendar mode, the RTC features:
  102. * - Time tracking in seconds, minutes, and hours
  103. * - 12 or 24 hour mode
  104. * - Date tracking in day, month, and year
  105. * - Automatic leap year correction
  106. *
  107. * \subsection asfdoc_sam0_rtc_calendar_features Driver Feature Macro Definition
  108. * <table>
  109. * <tr>
  110. * <th>Driver Feature Macro</th>
  111. * <th>Supported devices</th>
  112. * </tr>
  113. * <tr>
  114. * <td>FEATURE_RTC_PERIODIC_INT</td>
  115. * <td>SAM L21/L22/C20/C21/R30</td>
  116. * </tr>
  117. * <tr>
  118. * <td>FEATURE_RTC_PRESCALER_OFF</td>
  119. * <td>SAM L21/L22/C20/C21/R30</td>
  120. * </tr>
  121. * <tr>
  122. * <td>FEATURE_RTC_CLOCK_SELECTION</td>
  123. * <td>SAM L21/L22/C20/C21/R30</td>
  124. * </tr>
  125. * <tr>
  126. * <td>FEATURE_RTC_GENERAL_PURPOSE_REG</td>
  127. * <td>SAM L21/L22/R30</td>
  128. * </tr>
  129. * <tr>
  130. * <td>FEATURE_RTC_CONTINUOUSLY_UPDATED</td>
  131. * <td>SAM D20, SAM D21, SAM R21, SAM D10, SAM D11, SAM DA1, SAM HA1</td>
  132. * </tr>
  133. * <tr>
  134. * <td>FEATURE_RTC_TAMPER_DETECTION</td>
  135. * <td>SAM L22</td>
  136. * </tr>
  137. * </table>
  138. * \note The specific features are only available in the driver when the
  139. * selected device supports those features.
  140. *
  141. * \subsection asfdoc_sam0_rtc_calendar_module_overview_alarms Alarms and Overflow
  142. * The RTC has up to four independent hardware alarms that can be configured by the user
  143. * application. These alarms will be triggered on match with the current
  144. * clock value, and can be set up to trigger an interrupt, event, or both. The
  145. * RTC can also be configured to clear the clock value on alarm match, resetting
  146. * the clock to the original start time.
  147. *
  148. * If the RTC is operated in clock-only mode (i.e. with calendar disabled), the
  149. * RTC counter value will instead be cleared on overflow once the maximum count
  150. * value has been reached:
  151. *
  152. * \f[ COUNT_{MAX} = 2^{32}-1 \f]
  153. *
  154. * When the RTC is operated with the calendar enabled and run using a nominal
  155. * 1Hz input clock frequency, a register overflow will occur after 64 years.
  156. *
  157. * \subsection asfdoc_sam0_rtc_calendar_module_overview_periodic Periodic Events
  158. * The RTC can generate events at periodic intervals, allowing for direct
  159. * peripheral actions without CPU intervention. The periodic events can be
  160. * generated on the upper eight bits of the RTC prescaler, and will be generated on
  161. * the rising edge transition of the specified bit. The resulting periodic
  162. * frequency can be calculated by the following formula:
  163. *
  164. * \f[ f_{PERIODIC}=\frac{f_{ASY}}{2^{n+3}} \f]
  165. *
  166. * Where \f$f_{ASY}\f$ refers to the \e asynchronous clock set up in the RTC
  167. * module configuration. For the RTC to operate correctly in calendar mode, this
  168. * frequency must be 1KHz, while the RTC's internal prescaler should be set to
  169. * divide by 1024. The \b n parameter is the event source generator index of the
  170. * RTC module. If the asynchronous clock is operated at the recommended 1KHz,
  171. * the formula results in the values shown in
  172. * \ref asfdoc_sam0_rtc_calendar_module_rtc_hz "the table below".
  173. *
  174. * \anchor asfdoc_sam0_rtc_calendar_module_rtc_hz
  175. * <table>
  176. * <caption>RTC Event Frequencies for Each Prescaler Bit Using a 1KHz Clock</caption>
  177. * <tr>
  178. * <th>n</th> <th>Periodic event</th>
  179. * </tr>
  180. * <tr>
  181. * <td>7</td> <td>1Hz</td>
  182. * </tr>
  183. * <tr>
  184. * <td>6</td> <td>2Hz</td>
  185. * </tr>
  186. * <tr>
  187. * <td>5</td> <td>4Hz</td>
  188. * </tr>
  189. * <tr>
  190. * <td>4</td> <td>8Hz</td>
  191. * </tr>
  192. * <tr>
  193. * <td>3</td> <td>16Hz</td>
  194. * </tr>
  195. * <tr>
  196. * <td>2</td> <td>32Hz</td>
  197. * </tr>
  198. * <tr>
  199. * <td>1</td> <td>64Hz</td>
  200. * </tr>
  201. * <tr>
  202. * <td>0</td> <td>128Hz</td>
  203. * </tr>
  204. * </table>
  205. *
  206. * \note The connection of events between modules requires the use of the
  207. * \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
  208. * to route output event of one module to the input event of another.
  209. * For more information on event routing, refer to the event driver
  210. * documentation.
  211. *
  212. * \subsection asfdoc_sam0_rtc_calendar_module_overview_correction Digital Frequency Correction
  213. * The RTC module contains Digital Frequency Correction logic to compensate for
  214. * inaccurate source clock frequencies which would otherwise result in skewed
  215. * time measurements. The correction scheme requires that at least two bits
  216. * in the RTC module prescaler are reserved by the correction logic. As a
  217. * result of this implementation, frequency correction is only available when
  218. * the RTC is running from a 1Hz reference clock.
  219. *
  220. * The correction procedure is implemented by subtracting or adding a single
  221. * cycle from the RTC prescaler every 1024 RTC Generic Clock (GCLK) cycles. The adjustment is
  222. * applied the specified number of time (maximum 127) over 976 of these periods. The
  223. * corresponding correction in parts per million (PPM) will be given by:
  224. *
  225. * \f[ Correction(PPM) = \frac{VALUE}{999424}10^6 \f]
  226. *
  227. * The RTC clock will tick faster if provided with a positive correction value,
  228. * and slower when given a negative correction value.
  229. *
  230. *
  231. * \subsection asfdoc_sam0_rtc_calendar_module_overview_tamper_detect RTC Tamper Detect
  232. * See \ref asfdoc_sam0_rtc_tamper_detect.
  233. *
  234. * \section asfdoc_sam0_rtc_calendar_special_considerations Special Considerations
  235. *
  236. * \subsection asfdoc_sam0_rtc_calendar_special_considerations_year Year Limit
  237. * The RTC module has a year range of 63 years from the starting year configured
  238. * when the module is initialized. Dates outside the start to end year range
  239. * described below will need software adjustment:
  240. *
  241. * \f[ [YEAR_{START}, YEAR_{START}+64] \f]
  242. *
  243. * \subsection asfdoc_sam0_rtc_calendar_special_considerations_clock Clock Setup
  244. * \subsubsection asfdoc_sam0_rtc_calendar_clock_samd_r SAM D20/D21/R21/D10/D11/DA1/HA1 Clock Setup.
  245. * The RTC is typically clocked by a specialized GCLK generator that has a
  246. * smaller prescaler than the others. By default the RTC clock is on, selected
  247. * to use the internal 32KHz Resistor/Capacitor (RC)-oscillator with a prescaler
  248. * of 32, giving a resulting clock frequency of 1024Hz to the RTC. When the
  249. * internal RTC prescaler is set to 1024, this yields an end-frequency of 1Hz
  250. * for correct time keeping operations.
  251. *
  252. * The implementer also has the option to set other end-frequencies.
  253. * \ref asfdoc_sam0_rtc_calendar_rtc_out_freq "The table below" lists the
  254. * available RTC frequencies for each possible GCLK and RTC input prescaler
  255. * options.
  256. *
  257. * \anchor asfdoc_sam0_rtc_calendar_rtc_out_freq
  258. * <table>
  259. * <caption>RTC Output Frequencies from Allowable Input Clocks</caption>
  260. * <tr>
  261. * <th>End-frequency</th>
  262. * <th>GCLK prescaler</th>
  263. * <th>RTC prescaler</th>
  264. * </tr>
  265. * <tr>
  266. * <td>32KHz</td>
  267. * <td>1</td>
  268. * <td>1</td>
  269. * </tr>
  270. * <tr>
  271. * <td>1KHz</td>
  272. * <td>32</td>
  273. * <td>1</td>
  274. * </tr>
  275. * <tr>
  276. * <td>1Hz</td>
  277. * <td>32</td>
  278. * <td>1024</td>
  279. * </tr>
  280. * </table>
  281. *
  282. * The overall RTC module clocking scheme is shown in
  283. * \ref asfdoc_sam0_rtc_calendar_rtc_clock_fig "the figure below".
  284. *
  285. * \anchor asfdoc_sam0_rtc_calendar_rtc_clock_fig
  286. * \dot
  287. * digraph clocking_scheme {
  288. * rankdir=LR;
  289. * GCLK [shape="record", label="<f0> GCLK | <f1> RTC_GCLK",
  290. * bgcolor="lightgray", style="filled"];
  291. * RTCPRE [shape="record" label="<f0> RTC | <f1> RTC PRESCALER"];
  292. * RTC [shape="record", label="<f0> RTC | <f1> RTC CLOCK"];
  293. *
  294. * GCLK:f1 -> RTCPRE:f1;
  295. * RTCPRE:f1 -> RTC:f1;
  296. * }
  297. * \enddot
  298. *
  299. * \note For the calendar to operate correctly, an asynchronous clock of 1Hz
  300. * should be used.
  301. *
  302. * \subsubsection asfdoc_sam0_rtc_calendar_clock_saml SAM L21/C20/C21/R30 Clock Setup
  303. * The RTC clock can be selected from OSC32K, XOSC32K, or OSCULP32K. A 32KHz
  304. * or 1KHz oscillator clock frequency is required. This clock must be
  305. * configured and enabled in the 32KHz oscillator controller before using the RTC.
  306. *
  307. * \ref asfdoc_sam0_rtc_calendar_rtc_clk lists the available RTC clock.
  308. *
  309. * \anchor asfdoc_sam0_rtc_calendar_rtc_clk
  310. * <table>
  311. * <caption>RTC Clocks Source</caption>
  312. * <tr>
  313. * <th>RTC clock frequency</th>
  314. * <th>Clock source</th>
  315. * <th>Description</th>
  316. * </tr>
  317. * <tr>
  318. * <td>1.024kHz</td>
  319. * <td>ULP1K</td>
  320. * <td>1.024kHz from 32KHz internal ULP oscillator</td>
  321. * </tr>
  322. * <tr>
  323. * <td>32.768kHz</td>
  324. * <td>ULP32K</td>
  325. * <td>32.768kHz from 32KHz internal ULP oscillator</td>
  326. * </tr>
  327. * <tr>
  328. * <td>1.024kHz</td>
  329. * <td>OSC1K</td>
  330. * <td>1.024kHz from 32KHz internal oscillator</td>
  331. * </tr>
  332. * <tr>
  333. * <td>32.768kHz</td>
  334. * <td>OSC32K</td>
  335. * <td>32.768kHz from 3KkHz internal oscillator</td>
  336. * </tr>
  337. * <tr>
  338. * <td>1.024kHz</td>
  339. * <td>XOSC1K</td>
  340. * <td>1.024kHz from 32KHz internal oscillator</td>
  341. * </tr>
  342. * <tr>
  343. * <td>32.768kHz</td>
  344. * <td>XOSC32K</td>
  345. * <td>32.768kHz from 32KHz external crystal oscillator</td>
  346. * </tr>
  347. * </table>
  348. *
  349. * \note For the calendar to operate correctly, an asynchronous clock of 1Hz
  350. * should be used.
  351. *
  352. * \section asfdoc_sam0_rtc_calendar_extra_info Extra Information
  353. *
  354. * For extra information, see \ref asfdoc_sam0_rtc_calendar_extra. This includes:
  355. * - \ref asfdoc_sam0_rtc_calendar_extra_acronyms
  356. * - \ref asfdoc_sam0_rtc_calendar_extra_dependencies
  357. * - \ref asfdoc_sam0_rtc_calendar_extra_errata
  358. * - \ref asfdoc_sam0_rtc_calendar_extra_history
  359. *
  360. *
  361. * \section asfdoc_sam0_rtc_calendar_examples Examples
  362. *
  363. * For a list of examples related to this driver, see
  364. * \ref asfdoc_sam0_rtc_calendar_exqsg.
  365. *
  366. *
  367. * \section asfdoc_sam0_rtc_calendar_api_overview API Overview
  368. * @{
  369. */
  370. #include <conf_clocks.h>
  371. #if RTC_CALENDAR_ASYNC == true
  372. # include <system_interrupt.h>
  373. #endif
  374. #ifdef __cplusplus
  375. extern "C" {
  376. #endif
  377. /**
  378. * Define port features set according to different device family
  379. * @{
  380. */
  381. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30) || defined(__DOXYGEN__)
  382. /** RTC periodic interval interrupt. */
  383. # define FEATURE_RTC_PERIODIC_INT
  384. /** RTC prescaler is off. */
  385. # define FEATURE_RTC_PRESCALER_OFF
  386. /** RTC clock selection. */
  387. # define FEATURE_RTC_CLOCK_SELECTION
  388. # if !(SAMC20) && !(SAMC21)
  389. /** General purpose registers. */
  390. # define FEATURE_RTC_GENERAL_PURPOSE_REG
  391. # endif
  392. #else
  393. /** RTC continuously updated. */
  394. # define FEATURE_RTC_CONTINUOUSLY_UPDATED
  395. #endif
  396. #if (SAML22) || defined(__DOXYGEN__)
  397. /** RTC tamper detection. */
  398. # define FEATURE_RTC_TAMPER_DETECTION
  399. #endif
  400. /*@}*/
  401. #ifdef FEATURE_RTC_CLOCK_SELECTION
  402. /**
  403. * \brief Available clock source for RTC.
  404. * RTC clock source.
  405. */
  406. enum rtc_clock_sel {
  407. /** 1.024kHz from 32KHz internal ULP oscillator */
  408. RTC_CLOCK_SELECTION_ULP1K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val,
  409. /** 32.768kHz from 32KHz internal ULP oscillator */
  410. RTC_CLOCK_SELECTION_ULP32K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP32K_Val,
  411. #if !(SAML22)
  412. /** 1.024kHz from 32KHz internal oscillator */
  413. RTC_CLOCK_SELECTION_OSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC1K_Val,
  414. /** 32.768kHz from 32KHz internal oscillator */
  415. RTC_CLOCK_SELECTION_OSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC32K_Val,
  416. #endif
  417. /** 1.024kHz from 32KHz internal oscillator */
  418. RTC_CLOCK_SELECTION_XOSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val,
  419. /** 32.768kHz from 32.768kHz external crystal oscillator */
  420. RTC_CLOCK_SELECTION_XOSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC32K_Val,
  421. };
  422. #endif
  423. #if !defined (RTC_NUM_OF_ALARMS) && defined(RTC_ALARM_NUM)
  424. #define RTC_NUM_OF_ALARMS RTC_ALARM_NUM
  425. #endif
  426. /**
  427. * \brief Available alarm channels.
  428. *
  429. * Available alarm channels.
  430. *
  431. * \note Not all alarm channels are available on all devices.
  432. */
  433. enum rtc_calendar_alarm {
  434. /** Alarm channel 0 */
  435. RTC_CALENDAR_ALARM_0 = 0,
  436. #if (RTC_NUM_OF_ALARMS > 1) || defined(__DOXYGEN__)
  437. /** Alarm channel 1 */
  438. RTC_CALENDAR_ALARM_1 = 1,
  439. #endif
  440. #if (RTC_NUM_OF_ALARMS > 2) || defined(__DOXYGEN__)
  441. /** Alarm channel 2 */
  442. RTC_CALENDAR_ALARM_2 = 2,
  443. #endif
  444. #if (RTC_NUM_OF_ALARMS > 3) || defined(__DOXYGEN__)
  445. /** Alarm channel 3 */
  446. RTC_CALENDAR_ALARM_3 = 3,
  447. #endif
  448. };
  449. #ifdef FEATURE_RTC_PERIODIC_INT
  450. /**
  451. * \brief Available periodic interval source.
  452. */
  453. enum rtc_calendar_periodic_interval{
  454. /** Periodic interval 0 */
  455. RTC_CALENDAR_PERIODIC_INTERVAL_0 = 0,
  456. /** Periodic interval 1 */
  457. RTC_CALENDAR_PERIODIC_INTERVAL_1 = 1,
  458. /** Periodic interval 2 */
  459. RTC_CALENDAR_PERIODIC_INTERVAL_2 = 2,
  460. /** Periodic interval 3 */
  461. RTC_CALENDAR_PERIODIC_INTERVAL_3 = 3,
  462. /** Periodic interval 4 */
  463. RTC_CALENDAR_PERIODIC_INTERVAL_4 = 4,
  464. /** Periodic interval 5 */
  465. RTC_CALENDAR_PERIODIC_INTERVAL_5 = 5,
  466. /** Periodic interval 6 */
  467. RTC_CALENDAR_PERIODIC_INTERVAL_6 = 6,
  468. /** Periodic interval 7 */
  469. RTC_CALENDAR_PERIODIC_INTERVAL_7 = 7,
  470. };
  471. #endif
  472. #if RTC_CALENDAR_ASYNC == true
  473. #ifdef FEATURE_RTC_PERIODIC_INT
  474. /**
  475. * \brief Callback types.
  476. *
  477. * The available callback types for the RTC calendar module.
  478. */
  479. enum rtc_calendar_callback {
  480. /** Callback for Periodic Interval 0 Interrupt */
  481. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_0 = 0,
  482. /** Callback for Periodic Interval 1 Interrupt */
  483. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_1,
  484. /** Callback for Periodic Interval 2 Interrupt */
  485. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_2,
  486. /** Callback for Periodic Interval 3 Interrupt */
  487. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_3,
  488. /** Callback for Periodic Interval 4 Interrupt */
  489. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_4,
  490. /** Callback for Periodic Interval 5 Interrupt */
  491. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_5,
  492. /** Callback for Periodic Interval 6 Interrupt */
  493. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_6,
  494. /** Callback for Periodic Interval 7 Interrupt */
  495. RTC_CALENDAR_CALLBACK_PERIODIC_INTERVAL_7,
  496. /** Callback for alarm 0 */
  497. RTC_CALENDAR_CALLBACK_ALARM_0,
  498. # if (RTC_NUM_OF_ALARMS > 1) || defined(__DOXYGEN__)
  499. /** Callback for alarm 1 */
  500. RTC_CALENDAR_CALLBACK_ALARM_1,
  501. # endif
  502. # if (RTC_NUM_OF_ALARMS > 2) || defined(__DOXYGEN__)
  503. /** Callback for alarm 2 */
  504. RTC_CALENDAR_CALLBACK_ALARM_2,
  505. # endif
  506. # if (RTC_NUM_OF_ALARMS > 3) || defined(__DOXYGEN__)
  507. /** Callback for alarm 3 */
  508. RTC_CALENDAR_CALLBACK_ALARM_3,
  509. # endif
  510. #ifdef FEATURE_RTC_TAMPER_DETECTION
  511. /** Callback for tamper */
  512. RTC_CALENDAR_CALLBACK_TAMPER,
  513. #endif
  514. /** Callback for overflow */
  515. RTC_CALENDAR_CALLBACK_OVERFLOW,
  516. # if !defined(__DOXYGEN__)
  517. /** Total number of callbacks */
  518. _RTC_CALENDAR_CALLBACK_N
  519. # endif
  520. };
  521. #else
  522. /**
  523. * \brief Callback types.
  524. *
  525. * The available callback types for the RTC calendar module.
  526. */
  527. enum rtc_calendar_callback {
  528. /** Callback for alarm 0 */
  529. RTC_CALENDAR_CALLBACK_ALARM_0 = 0,
  530. # if (RTC_NUM_OF_ALARMS > 1) || defined(__DOXYGEN__)
  531. /** Callback for alarm 1 */
  532. RTC_CALENDAR_CALLBACK_ALARM_1,
  533. # endif
  534. # if (RTC_NUM_OF_ALARMS > 2) || defined(__DOXYGEN__)
  535. /** Callback for alarm 2 */
  536. RTC_CALENDAR_CALLBACK_ALARM_2,
  537. # endif
  538. # if (RTC_NUM_OF_ALARMS > 3) || defined(__DOXYGEN__)
  539. /** Callback for alarm 3 */
  540. RTC_CALENDAR_CALLBACK_ALARM_3,
  541. # endif
  542. #ifdef FEATURE_RTC_TAMPER_DETECTION
  543. /** Callback for tamper */
  544. RTC_CALENDAR_CALLBACK_TAMPER,
  545. #endif
  546. /** Callback for overflow */
  547. RTC_CALENDAR_CALLBACK_OVERFLOW,
  548. # if !defined(__DOXYGEN__)
  549. /** Total number of callbacks */
  550. _RTC_CALENDAR_CALLBACK_N
  551. # endif
  552. };
  553. #endif
  554. # if !defined(__DOXYGEN__)
  555. typedef void (*rtc_calendar_callback_t)(void);
  556. # endif
  557. #endif
  558. #ifdef FEATURE_RTC_PRESCALER_OFF
  559. /**
  560. * \brief RTC input clock prescaler settings.
  561. *
  562. * The available input clock prescaler values for the RTC calendar module.
  563. */
  564. enum rtc_calendar_prescaler {
  565. /** RTC prescaler is off, and the input clock frequency is
  566. prescaled by a factor of 1 */
  567. RTC_CALENDAR_PRESCALER_OFF = RTC_MODE2_CTRLA_PRESCALER_OFF,
  568. /** RTC input clock frequency is prescaled by a factor of 1 */
  569. RTC_CALENDAR_PRESCALER_DIV_1 = RTC_MODE2_CTRLA_PRESCALER_DIV1,
  570. /** RTC input clock frequency is prescaled by a factor of 2 */
  571. RTC_CALENDAR_PRESCALER_DIV_2 = RTC_MODE2_CTRLA_PRESCALER_DIV2,
  572. /** RTC input clock frequency is prescaled by a factor of 4 */
  573. RTC_CALENDAR_PRESCALER_DIV_4 = RTC_MODE2_CTRLA_PRESCALER_DIV4,
  574. /** RTC input clock frequency is prescaled by a factor of 8 */
  575. RTC_CALENDAR_PRESCALER_DIV_8 = RTC_MODE2_CTRLA_PRESCALER_DIV8,
  576. /** RTC input clock frequency is prescaled by a factor of 16 */
  577. RTC_CALENDAR_PRESCALER_DIV_16 = RTC_MODE2_CTRLA_PRESCALER_DIV16,
  578. /** RTC input clock frequency is prescaled by a factor of 32 */
  579. RTC_CALENDAR_PRESCALER_DIV_32 = RTC_MODE2_CTRLA_PRESCALER_DIV32,
  580. /** RTC input clock frequency is prescaled by a factor of 64 */
  581. RTC_CALENDAR_PRESCALER_DIV_64 = RTC_MODE2_CTRLA_PRESCALER_DIV64,
  582. /** RTC input clock frequency is prescaled by a factor of 128 */
  583. RTC_CALENDAR_PRESCALER_DIV_128 = RTC_MODE2_CTRLA_PRESCALER_DIV128,
  584. /** RTC input clock frequency is prescaled by a factor of 256 */
  585. RTC_CALENDAR_PRESCALER_DIV_256 = RTC_MODE2_CTRLA_PRESCALER_DIV256,
  586. /** RTC input clock frequency is prescaled by a factor of 512 */
  587. RTC_CALENDAR_PRESCALER_DIV_512 = RTC_MODE2_CTRLA_PRESCALER_DIV512,
  588. /** RTC input clock frequency is prescaled by a factor of 1024 */
  589. RTC_CALENDAR_PRESCALER_DIV_1024 = RTC_MODE2_CTRLA_PRESCALER_DIV1024,
  590. };
  591. #else
  592. /**
  593. * \brief RTC input clock prescaler settings.
  594. *
  595. * The available input clock prescaler values for the RTC calendar module.
  596. */
  597. enum rtc_calendar_prescaler {
  598. /** RTC input clock frequency is prescaled by a factor of 1 */
  599. RTC_CALENDAR_PRESCALER_DIV_1 = RTC_MODE2_CTRL_PRESCALER_DIV1,
  600. /** RTC input clock frequency is prescaled by a factor of 2 */
  601. RTC_CALENDAR_PRESCALER_DIV_2 = RTC_MODE2_CTRL_PRESCALER_DIV2,
  602. /** RTC input clock frequency is prescaled by a factor of 4 */
  603. RTC_CALENDAR_PRESCALER_DIV_4 = RTC_MODE2_CTRL_PRESCALER_DIV4,
  604. /** RTC input clock frequency is prescaled by a factor of 8 */
  605. RTC_CALENDAR_PRESCALER_DIV_8 = RTC_MODE2_CTRL_PRESCALER_DIV8,
  606. /** RTC input clock frequency is prescaled by a factor of 16 */
  607. RTC_CALENDAR_PRESCALER_DIV_16 = RTC_MODE2_CTRL_PRESCALER_DIV16,
  608. /** RTC input clock frequency is prescaled by a factor of 32 */
  609. RTC_CALENDAR_PRESCALER_DIV_32 = RTC_MODE2_CTRL_PRESCALER_DIV32,
  610. /** RTC input clock frequency is prescaled by a factor of 64 */
  611. RTC_CALENDAR_PRESCALER_DIV_64 = RTC_MODE2_CTRL_PRESCALER_DIV64,
  612. /** RTC input clock frequency is prescaled by a factor of 128 */
  613. RTC_CALENDAR_PRESCALER_DIV_128 = RTC_MODE2_CTRL_PRESCALER_DIV128,
  614. /** RTC input clock frequency is prescaled by a factor of 256 */
  615. RTC_CALENDAR_PRESCALER_DIV_256 = RTC_MODE2_CTRL_PRESCALER_DIV256,
  616. /** RTC input clock frequency is prescaled by a factor of 512 */
  617. RTC_CALENDAR_PRESCALER_DIV_512 = RTC_MODE2_CTRL_PRESCALER_DIV512,
  618. /** RTC input clock frequency is prescaled by a factor of 1024 */
  619. RTC_CALENDAR_PRESCALER_DIV_1024 = RTC_MODE2_CTRL_PRESCALER_DIV1024,
  620. };
  621. #endif
  622. #if !defined(__DOXYGEN__)
  623. /**
  624. * \brief Device structure.
  625. */
  626. struct rtc_module {
  627. /** RTC hardware module */
  628. Rtc *hw;
  629. /** If clock mode 24h */
  630. bool clock_24h;
  631. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  632. /** If continuously update clock register */
  633. bool continuously_update;
  634. #endif
  635. /** Initial year for counter value 0 */
  636. uint16_t year_init_value;
  637. # if RTC_CALENDAR_ASYNC == true
  638. /** Pointers to callback functions */
  639. volatile rtc_calendar_callback_t callbacks[_RTC_CALENDAR_CALLBACK_N];
  640. /** Mask for registered callbacks */
  641. volatile uint16_t registered_callback;
  642. /** Mask for enabled callbacks */
  643. volatile uint16_t enabled_callback;
  644. # endif
  645. };
  646. #endif
  647. /**
  648. * \brief Available mask options for alarms.
  649. *
  650. * Available mask options for alarms.
  651. */
  652. enum rtc_calendar_alarm_mask {
  653. /** Alarm disabled */
  654. RTC_CALENDAR_ALARM_MASK_DISABLED = RTC_MODE2_MASK_SEL_OFF,
  655. /** Alarm match on second */
  656. RTC_CALENDAR_ALARM_MASK_SEC = RTC_MODE2_MASK_SEL_SS,
  657. /** Alarm match on second and minute */
  658. RTC_CALENDAR_ALARM_MASK_MIN = RTC_MODE2_MASK_SEL_MMSS,
  659. /** Alarm match on second, minute, and hour */
  660. RTC_CALENDAR_ALARM_MASK_HOUR = RTC_MODE2_MASK_SEL_HHMMSS,
  661. /** Alarm match on second, minute, hour, and day */
  662. RTC_CALENDAR_ALARM_MASK_DAY = RTC_MODE2_MASK_SEL_DDHHMMSS,
  663. /** Alarm match on second, minute, hour, day, and month */
  664. RTC_CALENDAR_ALARM_MASK_MONTH = RTC_MODE2_MASK_SEL_MMDDHHMMSS,
  665. /** Alarm match on second, minute, hour, day, month, and year */
  666. RTC_CALENDAR_ALARM_MASK_YEAR = RTC_MODE2_MASK_SEL_YYMMDDHHMMSS,
  667. };
  668. /**
  669. * \brief RTC Calendar event enable/disable structure.
  670. *
  671. * Event flags for the \ref rtc_calendar_enable_events() and
  672. * \ref rtc_calendar_disable_events().
  673. */
  674. struct rtc_calendar_events {
  675. /** Generate an output event on each overflow of the RTC count */
  676. bool generate_event_on_overflow;
  677. /** Generate an output event on an alarm channel match against the RTC
  678. * count */
  679. bool generate_event_on_alarm[RTC_NUM_OF_ALARMS];
  680. /** Generate an output event periodically at a binary division of the RTC
  681. * counter frequency
  682. */
  683. bool generate_event_on_periodic[8];
  684. #ifdef FEATURE_RTC_TAMPER_DETECTION
  685. /** Generate an output event on every tamper input */
  686. bool generate_event_on_tamper;
  687. /** Tamper input event and capture the CLOCK value */
  688. bool on_event_to_tamper;
  689. #endif
  690. };
  691. /**
  692. * \brief Time structure.
  693. *
  694. * Time structure containing the time given by or set to the RTC calendar.
  695. * The structure uses seven values to give second, minute, hour, PM/AM, day,
  696. * month, and year. It should be initialized via the
  697. * \ref rtc_calendar_get_time_defaults() function before use.
  698. */
  699. struct rtc_calendar_time {
  700. /** Second value */
  701. uint8_t second;
  702. /** Minute value */
  703. uint8_t minute;
  704. /** Hour value */
  705. uint8_t hour;
  706. /** PM/AM value, \c true for PM, or \c false for AM */
  707. bool pm;
  708. /** Day value, where day 1 is the first day of the month */
  709. uint8_t day;
  710. /** Month value, where month 1 is January */
  711. uint8_t month;
  712. /** Year value */
  713. uint16_t year;
  714. };
  715. /**
  716. * \brief Alarm structure.
  717. *
  718. * Alarm structure containing time of the alarm and a mask to determine when
  719. * the alarm will trigger.
  720. */
  721. struct rtc_calendar_alarm_time {
  722. /** Alarm time */
  723. struct rtc_calendar_time time;
  724. /** Alarm mask to determine on what precision the alarm will match */
  725. enum rtc_calendar_alarm_mask mask;
  726. };
  727. /**
  728. * \brief RTC configuration structure.
  729. *
  730. * Configuration structure for the RTC instance. This structure should
  731. * be initialized using the \ref rtc_calendar_get_config_defaults() before any
  732. * user configurations are set.
  733. */
  734. struct rtc_calendar_config {
  735. /** Input clock prescaler for the RTC module */
  736. enum rtc_calendar_prescaler prescaler;
  737. /** If \c true, clears the clock on alarm match */
  738. bool clear_on_match;
  739. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  740. /** If \c true, the digital counter registers will be continuously updated
  741. * so that internal synchronization is not needed when reading the current
  742. * count */
  743. bool continuously_update;
  744. #endif
  745. /** If \c true, time is represented in 24 hour mode */
  746. bool clock_24h;
  747. /** Initial year for counter value 0 */
  748. uint16_t year_init_value;
  749. #if (SAML21XXXB) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  750. /** Enable count read synchronization. The CLOCK value requires
  751. * synchronization when reading. Disabling the synchronization
  752. * will prevent the CLOCK value from displaying the current value. */
  753. bool enable_read_sync;
  754. #endif
  755. /** Alarm values */
  756. struct rtc_calendar_alarm_time alarm[RTC_NUM_OF_ALARMS];
  757. };
  758. /**
  759. * \name Configuration and Initialization
  760. * @{
  761. */
  762. /**
  763. * \brief Initialize a \c time structure.
  764. *
  765. * This will initialize a given time structure to the time 00:00:00 (hh:mm:ss)
  766. * and date 2000-01-01 (YYYY-MM-DD).
  767. *
  768. * \param[out] time Time structure to initialize
  769. */
  770. static inline void rtc_calendar_get_time_defaults(
  771. struct rtc_calendar_time *const time)
  772. {
  773. time->second = 0;
  774. time->minute = 0;
  775. time->hour = 0;
  776. time->pm = 0;
  777. time->day = 1;
  778. time->month = 1;
  779. time->year = 2000;
  780. }
  781. /**
  782. * \brief Gets the RTC default settings.
  783. *
  784. * Initializes the configuration structure to the known default values. This
  785. * function should be called at the start of any RTC initiation.
  786. *
  787. * The default configuration is as follows:
  788. * - Input clock divided by a factor of 1024
  789. * - Clear on alarm match off
  790. * - Continuously sync clock off
  791. * - 12 hour calendar
  792. * - Start year 2000 (Year 0 in the counter will be year 2000)
  793. * - Events off
  794. * - Alarms set to January 1. 2000, 00:00:00
  795. * - Alarm will match on second, minute, hour, day, month, and year
  796. * - Clock read synchronization is enabled for SAM L22
  797. *
  798. * \param[out] config Configuration structure to be initialized to default
  799. * values
  800. */
  801. static inline void rtc_calendar_get_config_defaults(
  802. struct rtc_calendar_config *const config)
  803. {
  804. /* Sanity check argument */
  805. Assert(config);
  806. /* Initialize and set time structure to default */
  807. struct rtc_calendar_time time;
  808. rtc_calendar_get_time_defaults(&time);
  809. /* Set defaults into configuration structure */
  810. config->prescaler = RTC_CALENDAR_PRESCALER_DIV_1024;
  811. config->clear_on_match = false;
  812. #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
  813. config->continuously_update = false;
  814. #endif
  815. config->clock_24h = false;
  816. config->year_init_value = 2000;
  817. #if (SAML21XXXB) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  818. config->enable_read_sync = true;
  819. #endif
  820. for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) {
  821. config->alarm[i].time = time;
  822. config->alarm[i].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
  823. }
  824. }
  825. void rtc_calendar_reset(struct rtc_module *const module);
  826. void rtc_calendar_enable(struct rtc_module *const module);
  827. void rtc_calendar_disable(struct rtc_module *const module);
  828. #if (RTC_INST_NUM > 1) && !defined(__DOXYGEN__)
  829. /**
  830. * \internal Find the index of given RTC module instance.
  831. *
  832. * \param[in] hw RTC module instance pointer
  833. *
  834. * \return Index of the given RTC module instance.
  835. */
  836. uint8_t _rtc_get_inst_index(
  837. Rtc *const hw)
  838. {
  839. /* List of available RTC modules */
  840. static Rtc *const rtc_modules[RTC_INST_NUM] = RTC_INSTS;
  841. /* Find index for RTC instance */
  842. for (uint32_t i = 0; i < RTC_INST_NUM; i++) {
  843. if (hw == rtc_modules[i]) {
  844. return i;
  845. }
  846. }
  847. /* Invalid data given */
  848. Assert(false);
  849. return 0;
  850. }
  851. #endif /* (RTC_INST_NUM > 1) && !defined(__DOXYGEN__) */
  852. void rtc_calendar_init(
  853. struct rtc_module *const module,
  854. Rtc *const hw,
  855. const struct rtc_calendar_config *const config);
  856. void rtc_calendar_swap_time_mode(struct rtc_module *const module);
  857. enum status_code rtc_calendar_frequency_correction(
  858. struct rtc_module *const module,
  859. const int8_t value);
  860. /** @} */
  861. /** \name Time and Alarm Management
  862. * @{
  863. */
  864. uint32_t rtc_calendar_time_to_register_value(
  865. struct rtc_module *const module,
  866. const struct rtc_calendar_time *const time);
  867. void rtc_calendar_register_value_to_time(
  868. struct rtc_module *const module,
  869. const uint32_t register_value,
  870. struct rtc_calendar_time *const time);
  871. void rtc_calendar_set_time(
  872. struct rtc_module *const module,
  873. const struct rtc_calendar_time *const time);
  874. void rtc_calendar_get_time(
  875. struct rtc_module *const module,
  876. struct rtc_calendar_time *const time);
  877. enum status_code rtc_calendar_set_alarm(
  878. struct rtc_module *const module,
  879. const struct rtc_calendar_alarm_time *const alarm,
  880. const enum rtc_calendar_alarm alarm_index);
  881. enum status_code rtc_calendar_get_alarm(
  882. struct rtc_module *const module,
  883. struct rtc_calendar_alarm_time *const alarm,
  884. const enum rtc_calendar_alarm alarm_index);
  885. /** @} */
  886. /** \name Status Flag Management
  887. * @{
  888. */
  889. /**
  890. * \brief Check if an RTC overflow has occurred.
  891. *
  892. * Checks the overflow flag in the RTC. The flag is set when there
  893. * is an overflow in the clock.
  894. *
  895. * \param[in,out] module Pointer to the software instance struct
  896. *
  897. * \return Overflow state of the RTC module.
  898. *
  899. * \retval true If the RTC count value has overflowed
  900. * \retval false If the RTC count value has not overflowed
  901. */
  902. static inline bool rtc_calendar_is_overflow(struct rtc_module *const module)
  903. {
  904. /* Sanity check arguments */
  905. Assert(module);
  906. Assert(module->hw);
  907. Rtc *const rtc_module = module->hw;
  908. /* Return status of flag */
  909. return (rtc_module->MODE2.INTFLAG.reg & RTC_MODE2_INTFLAG_OVF);
  910. }
  911. /**
  912. * \brief Clears the RTC overflow flag.
  913. *
  914. * \param[in,out] module Pointer to the software instance struct
  915. *
  916. * Clears the RTC module counter overflow flag, so that new overflow conditions
  917. * can be detected.
  918. */
  919. static inline void rtc_calendar_clear_overflow(struct rtc_module *const module)
  920. {
  921. /* Sanity check arguments */
  922. Assert(module);
  923. Assert(module->hw);
  924. Rtc *const rtc_module = module->hw;
  925. /* Clear flag */
  926. rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_OVF;
  927. }
  928. #ifdef FEATURE_RTC_PERIODIC_INT
  929. /**
  930. * \brief Check if an RTC periodic interval interrupt has occurred.
  931. *
  932. * Checks the periodic interval flag in the RTC.
  933. *
  934. * \param[in,out] module RTC hardware module
  935. * \param[in] n RTC periodic interval interrupt
  936. *
  937. * \return Periodic interval interrupt state of the RTC module.
  938. *
  939. * \retval true RTC periodic interval interrupt occur
  940. * \retval false RTC periodic interval interrupt doesn't occur
  941. */
  942. static inline bool rtc_calendar_is_periodic_interval(struct rtc_module *const module,
  943. enum rtc_calendar_periodic_interval n)
  944. {
  945. /* Sanity check arguments */
  946. Assert(module);
  947. Assert(module->hw);
  948. Rtc *const rtc_module = module->hw;
  949. /* Return status of flag */
  950. return (rtc_module->MODE2.INTFLAG.reg & RTC_MODE2_INTFLAG_PER(1 << n));
  951. }
  952. /**
  953. * \brief Clears the RTC periodic interval flag.
  954. *
  955. * Clears the RTC module counter periodic interval flag, so that new periodic
  956. * interval conditions can be detected.
  957. *
  958. * \param[in,out] module RTC hardware module
  959. * \param[in] n RTC periodic interval interrupt
  960. */
  961. static inline void rtc_calendar_clear_periodic_interval(struct rtc_module *const module,
  962. enum rtc_calendar_periodic_interval n)
  963. {
  964. /* Sanity check arguments */
  965. Assert(module);
  966. Assert(module->hw);
  967. Rtc *const rtc_module = module->hw;
  968. /* Clear periodic interval flag */
  969. rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_PER(1 << n);
  970. }
  971. #endif
  972. /**
  973. * \brief Check the RTC alarm flag.
  974. *
  975. * Check if the specified alarm flag is set. The flag is set when there
  976. * is a compare match between the alarm value and the clock.
  977. *
  978. * \param[in,out] module Pointer to the software instance struct
  979. * \param[in] alarm_index Index of the alarm to check
  980. *
  981. * \returns Match status of the specified alarm.
  982. *
  983. * \retval true If the specified alarm has matched the current time
  984. * \retval false If the specified alarm has not matched the current time
  985. */
  986. static inline bool rtc_calendar_is_alarm_match(
  987. struct rtc_module *const module,
  988. const enum rtc_calendar_alarm alarm_index)
  989. {
  990. /* Sanity check arguments */
  991. Assert(module);
  992. Assert(module->hw);
  993. Rtc *const rtc_module = module->hw;
  994. /* Sanity check */
  995. if ((uint32_t)alarm_index > RTC_NUM_OF_ALARMS) {
  996. Assert(false);
  997. return false;
  998. }
  999. /* Return int flag status */
  1000. return (rtc_module->MODE2.INTFLAG.reg & RTC_MODE2_INTFLAG_ALARM(1 << alarm_index));
  1001. }
  1002. /**
  1003. * \brief Clears the RTC alarm match flag.
  1004. *
  1005. * Clear the requested alarm match flag, so that future alarm matches can be
  1006. * determined.
  1007. *
  1008. * \param[in,out] module Pointer to the software instance struct
  1009. * \param[in] alarm_index The index of the alarm match to clear
  1010. *
  1011. * \return Status of the alarm match clear operation.
  1012. *
  1013. * \retval STATUS_OK If flag was cleared correctly
  1014. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  1015. */
  1016. static inline enum status_code rtc_calendar_clear_alarm_match(
  1017. struct rtc_module *const module,
  1018. const enum rtc_calendar_alarm alarm_index)
  1019. {
  1020. /* Sanity check arguments */
  1021. Assert(module);
  1022. Assert(module->hw);
  1023. Rtc *const rtc_module = module->hw;
  1024. /* Sanity check */
  1025. if ((uint32_t)alarm_index > RTC_NUM_OF_ALARMS) {
  1026. Assert(false);
  1027. return STATUS_ERR_INVALID_ARG;
  1028. }
  1029. /* Clear flag */
  1030. rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM(1 << alarm_index);
  1031. return STATUS_OK;
  1032. }
  1033. /** @} */
  1034. /**
  1035. * \name Event Management
  1036. * @{
  1037. */
  1038. /**
  1039. * \brief Enables an RTC event output.
  1040. *
  1041. * Enables one or more output events from the RTC module. See
  1042. * \ref rtc_calendar_events for a list of events this module supports.
  1043. *
  1044. * \note Events cannot be altered while the module is enabled.
  1045. *
  1046. * \param[in,out] module Pointer to the software instance struct
  1047. * \param[in] events Struct containing flags of events to enable
  1048. */
  1049. static inline void rtc_calendar_enable_events(
  1050. struct rtc_module *const module,
  1051. struct rtc_calendar_events *const events)
  1052. {
  1053. /* Sanity check arguments */
  1054. Assert(module);
  1055. Assert(module->hw);
  1056. Rtc *const rtc_module = module->hw;
  1057. uint32_t event_mask = 0;
  1058. /* Check if the user has requested an overflow event */
  1059. if (events->generate_event_on_overflow) {
  1060. event_mask |= RTC_MODE2_EVCTRL_OVFEO;
  1061. }
  1062. /* Check if the user has requested any alarm events */
  1063. for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) {
  1064. if (events->generate_event_on_alarm[i]) {
  1065. event_mask |= RTC_MODE2_EVCTRL_ALARMEO(1 << i);
  1066. }
  1067. }
  1068. /* Check if the user has requested any periodic events */
  1069. for (uint8_t i = 0; i < 8; i++) {
  1070. if (events->generate_event_on_periodic[i]) {
  1071. event_mask |= RTC_MODE2_EVCTRL_PEREO(1 << i);
  1072. }
  1073. }
  1074. #ifdef FEATURE_RTC_TAMPER_DETECTION
  1075. /* Check if the user has requested a tamper event output */
  1076. if (events->generate_event_on_tamper) {
  1077. event_mask |= RTC_MODE2_EVCTRL_TAMPEREO;
  1078. }
  1079. /* Check if the user has requested a tamper event input */
  1080. if (events->on_event_to_tamper) {
  1081. event_mask |= RTC_MODE2_EVCTRL_TAMPEVEI;
  1082. }
  1083. #endif
  1084. /* Enable given event(s) */
  1085. rtc_module->MODE2.EVCTRL.reg |= event_mask;
  1086. }
  1087. /**
  1088. * \brief Disables an RTC event output.
  1089. *
  1090. * Disabled one or more output events from the RTC module. See
  1091. * \ref rtc_calendar_events for a list of events this module supports.
  1092. *
  1093. * \note Events cannot be altered while the module is enabled.
  1094. *
  1095. * \param[in,out] module Pointer to the software instance struct
  1096. * \param[in] events Struct containing flags of events to disable
  1097. */
  1098. static inline void rtc_calendar_disable_events(
  1099. struct rtc_module *const module,
  1100. struct rtc_calendar_events *const events)
  1101. {
  1102. /* Sanity check arguments */
  1103. Assert(module);
  1104. Assert(module->hw);
  1105. Rtc *const rtc_module = module->hw;
  1106. uint32_t event_mask = 0;
  1107. /* Check if the user has requested an overflow event */
  1108. if (events->generate_event_on_overflow) {
  1109. event_mask |= RTC_MODE2_EVCTRL_OVFEO;
  1110. }
  1111. /* Check if the user has requested any alarm events */
  1112. for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) {
  1113. if (events->generate_event_on_alarm[i]) {
  1114. event_mask |= RTC_MODE2_EVCTRL_ALARMEO(1 << i);
  1115. }
  1116. }
  1117. /* Check if the user has requested any periodic events */
  1118. for (uint8_t i = 0; i < 8; i++) {
  1119. if (events->generate_event_on_periodic[i]) {
  1120. event_mask |= RTC_MODE2_EVCTRL_PEREO(1 << i);
  1121. }
  1122. }
  1123. #ifdef FEATURE_RTC_TAMPER_DETECTION
  1124. /* Check if the user has requested a tamper event output */
  1125. if (events->generate_event_on_tamper) {
  1126. event_mask |= RTC_MODE2_EVCTRL_TAMPEREO;
  1127. }
  1128. /* Check if the user has requested a tamper event input */
  1129. if (events->on_event_to_tamper) {
  1130. event_mask |= RTC_MODE2_EVCTRL_TAMPEVEI;
  1131. }
  1132. #endif
  1133. /* Disable given event(s) */
  1134. rtc_module->MODE2.EVCTRL.reg &= ~event_mask;
  1135. }
  1136. /** @} */
  1137. #ifdef FEATURE_RTC_GENERAL_PURPOSE_REG
  1138. /**
  1139. * \name RTC General Purpose Registers
  1140. * @{
  1141. */
  1142. /**
  1143. * \brief Write a value into general purpose register.
  1144. *
  1145. * \param[in] module Pointer to the software instance struct
  1146. * \param[in] n General purpose type
  1147. * \param[in] index General purpose register index (0..3)
  1148. *
  1149. */
  1150. static inline void rtc_write_general_purpose_reg(
  1151. struct rtc_module *const module,
  1152. const uint8_t index,
  1153. uint32_t value)
  1154. {
  1155. /* Sanity check arguments */
  1156. Assert(module);
  1157. Assert(module->hw);
  1158. Assert(index <= 3);
  1159. Rtc *const rtc_module = module->hw;
  1160. rtc_module->MODE0.GP[index].reg = value;
  1161. }
  1162. /**
  1163. * \brief Read the value from general purpose register.
  1164. *
  1165. * \param[in] module Pointer to the software instance struct
  1166. * \param[in] index General purpose register index (0..3)
  1167. *
  1168. * \return Value of general purpose register.
  1169. */
  1170. static inline uint32_t rtc_read_general_purpose_reg(
  1171. struct rtc_module *const module,
  1172. const uint8_t index)
  1173. {
  1174. /* Sanity check arguments */
  1175. Assert(module);
  1176. Assert(module->hw);
  1177. Assert(index <= 3);
  1178. Rtc *const rtc_module = module->hw;
  1179. return rtc_module->MODE0.GP[index].reg;
  1180. }
  1181. /** @} */
  1182. #endif
  1183. #ifdef FEATURE_RTC_TAMPER_DETECTION
  1184. #include "rtc_tamper.h"
  1185. /**
  1186. * \brief Get the tamper stamp value.
  1187. *
  1188. * \param[in,out] module Pointer to the software instance struct
  1189. * \param[out] time Pointer to value that filled with tamper stamp time
  1190. */
  1191. void rtc_tamper_get_stamp (struct rtc_module *const module,
  1192. struct rtc_calendar_time *const time);
  1193. #endif
  1194. /** @} */
  1195. #ifdef __cplusplus
  1196. }
  1197. #endif
  1198. /**
  1199. * \page asfdoc_sam0_rtc_calendar_extra Extra Information for RTC (CAL) Driver
  1200. *
  1201. * \section asfdoc_sam0_rtc_calendar_extra_acronyms Acronyms
  1202. * Below is a table listing the acronyms used in this module, along with their
  1203. * intended meanings.
  1204. *
  1205. * <table>
  1206. * <tr>
  1207. * <th>Acronym</td>
  1208. * <th>Description</td>
  1209. * </tr>
  1210. * <tr>
  1211. * <td>RTC</td>
  1212. * <td>Real Time Counter</td>
  1213. * </tr>
  1214. * <tr>
  1215. * <td>PPM</td>
  1216. * <td>Part Per Million</td>
  1217. * </tr>
  1218. * <tr>
  1219. * <td>RC</td>
  1220. * <td>Resistor/Capacitor</td>
  1221. * </tr>
  1222. * </table>
  1223. *
  1224. *
  1225. * \section asfdoc_sam0_rtc_calendar_extra_dependencies Dependencies
  1226. * This driver has the following dependencies:
  1227. *
  1228. * - None
  1229. *
  1230. *
  1231. * \section asfdoc_sam0_rtc_calendar_extra_errata Errata
  1232. * There are no errata related to this driver.
  1233. *
  1234. *
  1235. * \section asfdoc_sam0_rtc_calendar_extra_history Module History
  1236. * An overview of the module history is presented in the table below, with
  1237. * details on the enhancements and fixes made to the module since its first
  1238. * release. The current version of this corresponds to the newest version in
  1239. * the table.
  1240. *
  1241. * <table>
  1242. * <tr>
  1243. * <th>Changelog</th>
  1244. * </tr>
  1245. * <tr>
  1246. * <td>Added support for RTC tamper feature</td>
  1247. * </tr>
  1248. * <tr>
  1249. * <td>Added driver instance parameter to all API function calls, except
  1250. * get_config_defaults</td>
  1251. * </tr>
  1252. * <tr>
  1253. * <td>Updated initialization function to also enable the digital interface
  1254. * clock to the module if it is disabled</td>
  1255. * </tr>
  1256. * <tr>
  1257. * <td>Initial release</td>
  1258. * </tr>
  1259. * </table>
  1260. */
  1261. /**
  1262. * \page asfdoc_sam0_rtc_calendar_exqsg Examples for RTC CAL Driver
  1263. *
  1264. * This is a list of the available Quick Start guides (QSGs) and example
  1265. * applications for \ref asfdoc_sam0_rtc_calendar_group. QSGs are simple
  1266. * examples with step-by-step instructions to configure and use this driver in a
  1267. * selection of use cases. Note that a QSG can be compiled as a standalone
  1268. * application or be added to the user application.
  1269. *
  1270. * - \subpage asfdoc_sam0_rtc_calendar_basic_use_case
  1271. * \if RTC_CALENDAR_CALLBACK_MODE
  1272. * - \subpage asfdoc_sam0_rtc_calendar_callback_use_case
  1273. * \endif
  1274. *
  1275. * \page asfdoc_sam0_rtc_calendar_document_revision_history Document Revision History
  1276. *
  1277. * <table>
  1278. * <tr>
  1279. * <th>Doc. Rev.</td>
  1280. * <th>Date</td>
  1281. * <th>Comments</td>
  1282. * </tr>
  1283. * <tr>
  1284. * <td>42126E</td>
  1285. * <td>12/2015</td>
  1286. * <td>Added support for SAM L21/L22, SAMR30, SAM C21, SAM D09, and SAM DA1</td>
  1287. * </tr>
  1288. * <tr>
  1289. * <td>42126D</td>
  1290. * <td>12/2014</td>
  1291. * <td>Added support for SAM R21 and SAM D10/D11</td>
  1292. * </tr>
  1293. * <tr>
  1294. * <td>42126C</td>
  1295. * <td>01/2014</td>
  1296. * <td>Added support for SAM D21</td>
  1297. * </tr>
  1298. * <tr>
  1299. * <td>42126B</td>
  1300. * <td>06/2013</td>
  1301. * <td>Added additional documentation on the event system. Corrected
  1302. * documentation typos</td>
  1303. * </tr>
  1304. * <tr>
  1305. * <td>42126A</td>
  1306. * <td>06/2013</td>
  1307. * <td>Initial document release</td>
  1308. * </tr>
  1309. * </table>
  1310. */
  1311. #endif /* RTC_CALENDAR_H_INCLUDED */