am_hal_rtc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. //*****************************************************************************
  2. //
  3. // am_hal_rtc.c
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with the Real-Time Clock (RTC).
  7. //!
  8. //! @addtogroup rtc2 Real-Time Clock (RTC)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. //*****************************************************************************
  51. //
  52. // Converts a Binary Coded Decimal (BCD) byte to its Decimal form.
  53. //
  54. //*****************************************************************************
  55. static uint8_t
  56. bcd_to_dec(uint8_t ui8BCDByte)
  57. {
  58. return (((ui8BCDByte & 0xF0) >> 4) * 10) + (ui8BCDByte & 0x0F);
  59. }
  60. //*****************************************************************************
  61. //
  62. // Converts a Decimal byte to its Binary Coded Decimal (BCD) form.
  63. //
  64. //*****************************************************************************
  65. static uint8_t
  66. dec_to_bcd(uint8_t ui8DecimalByte)
  67. {
  68. return (((ui8DecimalByte / 10) << 4) | (ui8DecimalByte % 10));
  69. }
  70. //*****************************************************************************
  71. //
  72. //! @brief Selects the clock source for the RTC.
  73. //!
  74. //! @param ui32OSC the clock source for the RTC.
  75. //!
  76. //! This function selects the clock source for the RTC.
  77. //!
  78. //! Valid values for ui32OSC are:
  79. //!
  80. //! AM_HAL_RTC_OSC_LFRC
  81. //! AM_HAL_RTC_OSC_XT
  82. //!
  83. //! @return None
  84. //
  85. //*****************************************************************************
  86. void
  87. am_hal_rtc_osc_select(uint32_t ui32OSC)
  88. {
  89. //
  90. // Set XT if flag is set.
  91. // Otherwise configure for LFRC.
  92. //
  93. if (ui32OSC)
  94. {
  95. AM_REG(CLKGEN, OCTRL) |= AM_REG_CLKGEN_OCTRL_OSEL_M;
  96. }
  97. else
  98. {
  99. AM_REG(CLKGEN, OCTRL) &= ~AM_REG_CLKGEN_OCTRL_OSEL_M;
  100. }
  101. }
  102. //*****************************************************************************
  103. //
  104. //! @brief Enable/Start the RTC oscillator.
  105. //!
  106. //! Starts the RTC oscillator.
  107. //!
  108. //! @return None.
  109. //
  110. //*****************************************************************************
  111. void
  112. am_hal_rtc_osc_enable(void)
  113. {
  114. //
  115. // Start the RTC Oscillator.
  116. //
  117. AM_REG(RTC, RTCCTL) &= ~AM_REG_RTC_RTCCTL_RSTOP(1);
  118. }
  119. //*****************************************************************************
  120. //
  121. //! @brief Disable/Stop the RTC oscillator.
  122. //!
  123. //! Stops the RTC oscillator.
  124. //!
  125. //! @return None.
  126. //
  127. //*****************************************************************************
  128. void
  129. am_hal_rtc_osc_disable(void)
  130. {
  131. //
  132. // Stop the RTC Oscillator.
  133. //
  134. AM_REG(RTC, RTCCTL) |= AM_REG_RTC_RTCCTL_RSTOP(1);
  135. }
  136. //*****************************************************************************
  137. //
  138. //! @brief Configures the RTC for 12 or 24 hour time keeping.
  139. //!
  140. //! @param b12Hour - A 'true' configures the RTC for 12 hour time keeping.
  141. //!
  142. //! Configures the RTC for 12 (true) or 24 (false) hour time keeping.
  143. //!
  144. //! @return None.
  145. //
  146. //*****************************************************************************
  147. void
  148. am_hal_rtc_time_12hour(bool b12Hour)
  149. {
  150. //
  151. // Set the 12/24 hour bit.
  152. //
  153. AM_REG(RTC, RTCCTL) |= AM_REG_RTC_RTCCTL_HR1224(b12Hour);
  154. }
  155. //*****************************************************************************
  156. //
  157. //! @brief Enable selected RTC interrupts.
  158. //!
  159. //! @param ui32Interrupt - desired interrupts
  160. //!
  161. //! Enables the RTC interrupts.
  162. //!
  163. //! ui32Interrupt should be an OR of the following:
  164. //!
  165. //! AM_HAL_RTC_INT_ALM
  166. //! AM_HAL_RTC_INT_OF
  167. //! AM_HAL_RTC_INT_ACC
  168. //! AM_HAL_RTC_INT_ACF
  169. //!
  170. //! @return None.
  171. //
  172. //*****************************************************************************
  173. void
  174. am_hal_rtc_int_enable(uint32_t ui32Interrupt)
  175. {
  176. //
  177. // Enable the interrupts.
  178. //
  179. AM_REG(RTC, INTEN) |= ui32Interrupt;
  180. }
  181. //*****************************************************************************
  182. //
  183. //! @brief Return the enabled RTC interrupts.
  184. //!
  185. //! Returns the enabled RTC interrupts.
  186. //!
  187. //! @return enabled RTC interrupts. Return is a logical or of:
  188. //!
  189. //! AM_HAL_RTC_INT_ALM
  190. //! AM_HAL_RTC_INT_OF
  191. //! AM_HAL_RTC_INT_ACC
  192. //! AM_HAL_RTC_INT_ACF
  193. //
  194. //*****************************************************************************
  195. uint32_t
  196. am_hal_rtc_int_enable_get(void)
  197. {
  198. //
  199. // Read the RTC interrupt enable register, and return its contents.
  200. //
  201. return AM_REG(RTC, INTEN);
  202. }
  203. //*****************************************************************************
  204. //
  205. //! @brief Disable selected RTC interrupts.
  206. //!
  207. //! @param ui32Interrupt - desired interrupts
  208. //!
  209. //! Disables the RTC interrupts.
  210. //!
  211. //! ui32Interrupt should be an OR of the following:
  212. //!
  213. //! AM_HAL_RTC_INT_ALM
  214. //! AM_HAL_RTC_INT_OF
  215. //! AM_HAL_RTC_INT_ACC
  216. //! AM_HAL_RTC_INT_ACF
  217. //!
  218. //! @return None.
  219. //
  220. //*****************************************************************************
  221. void
  222. am_hal_rtc_int_disable(uint32_t ui32Interrupt)
  223. {
  224. //
  225. // Disable the interrupts.
  226. //
  227. AM_REG(RTC, INTEN) &= ~ui32Interrupt;
  228. }
  229. //*****************************************************************************
  230. //
  231. //! @brief Sets the selected RTC interrupts.
  232. //!
  233. //! @param ui32Interrupt - desired interrupts
  234. //!
  235. //! Sets the RTC interrupts causing them to immediately trigger.
  236. //!
  237. //! ui32Interrupt should be an OR of the following:
  238. //!
  239. //! AM_HAL_RTC_INT_ALM
  240. //! AM_HAL_RTC_INT_OF
  241. //! AM_HAL_RTC_INT_ACC
  242. //! AM_HAL_RTC_INT_ACF
  243. //!
  244. //! @return None.
  245. //
  246. //*****************************************************************************
  247. void
  248. am_hal_rtc_int_set(uint32_t ui32Interrupt)
  249. {
  250. //
  251. // Set the interrupts.
  252. //
  253. AM_REG(RTC, INTSET) = ui32Interrupt;
  254. }
  255. //*****************************************************************************
  256. //
  257. //! @brief Clear selected RTC interrupts.
  258. //!
  259. //! @param ui32Interrupt - desired interrupts
  260. //!
  261. //! Clears the RTC interrupts.
  262. //!
  263. //! ui32Interrupt should be an OR of the following:
  264. //!
  265. //! AM_HAL_RTC_INT_ALM
  266. //! AM_HAL_RTC_INT_OF
  267. //! AM_HAL_RTC_INT_ACC
  268. //! AM_HAL_RTC_INT_ACF
  269. //!
  270. //! @return None.
  271. //
  272. //*****************************************************************************
  273. void
  274. am_hal_rtc_int_clear(uint32_t ui32Interrupt)
  275. {
  276. //
  277. // Clear the interrupts.
  278. //
  279. AM_REG(RTC, INTCLR) = ui32Interrupt;
  280. }
  281. //*****************************************************************************
  282. //
  283. //! @brief Returns the RTC interrupt status.
  284. //!
  285. //! @param bEnabledOnly - return the status of only the enabled interrupts.
  286. //!
  287. //! Returns the RTC interrupt status.
  288. //!
  289. //! @return Bitwise representation of the current interrupt status.
  290. //!
  291. //! The return value will be the logical OR of one or more of the following
  292. //! values:
  293. //!
  294. //! AM_HAL_RTC_INT_ALM
  295. //! AM_HAL_RTC_INT_OF
  296. //! AM_HAL_RTC_INT_ACC
  297. //! AM_HAL_RTC_INT_ACF
  298. //
  299. //*****************************************************************************
  300. uint32_t
  301. am_hal_rtc_int_status_get(bool bEnabledOnly)
  302. {
  303. //
  304. // Get the interrupt status.
  305. //
  306. if (bEnabledOnly)
  307. {
  308. uint32_t u32RetVal;
  309. u32RetVal = AM_REG(RTC, INTSTAT);
  310. u32RetVal &= AM_REG(RTC, INTEN);
  311. return u32RetVal &
  312. (AM_HAL_RTC_INT_ALM | AM_HAL_RTC_INT_OF |
  313. AM_HAL_RTC_INT_ACC | AM_HAL_RTC_INT_ACF);
  314. }
  315. else
  316. {
  317. return (AM_REG(RTC, INTSTAT) & (AM_HAL_RTC_INT_ALM |
  318. AM_HAL_RTC_INT_OF |
  319. AM_HAL_RTC_INT_ACC |
  320. AM_HAL_RTC_INT_ACF));
  321. }
  322. }
  323. //*****************************************************************************
  324. //
  325. //! @brief Set the Real Time Clock counter registers.
  326. //!
  327. //! @param *pTime - A pointer to the time structure.
  328. //!
  329. //! Sets the RTC counter registers to the supplied values.
  330. //!
  331. //! @return None.
  332. //
  333. //*****************************************************************************
  334. void
  335. am_hal_rtc_time_set(am_hal_rtc_time_t *pTime)
  336. {
  337. //
  338. // Enable writing to the counters.
  339. //
  340. AM_REG(RTC, RTCCTL) |= AM_REG_RTC_RTCCTL_WRTC(1);
  341. //
  342. // Write the RTCLOW register.
  343. //
  344. AM_REG(RTC, CTRLOW) =
  345. AM_REG_RTC_CTRLOW_CTRHR(dec_to_bcd(pTime->ui32Hour)) |
  346. AM_REG_RTC_CTRLOW_CTRMIN(dec_to_bcd(pTime->ui32Minute)) |
  347. AM_REG_RTC_CTRLOW_CTRSEC(dec_to_bcd(pTime->ui32Second)) |
  348. AM_REG_RTC_CTRLOW_CTR100(dec_to_bcd(pTime->ui32Hundredths));
  349. //
  350. // Write the RTCUP register.
  351. //
  352. AM_REG(RTC, CTRUP) =
  353. AM_REG_RTC_CTRUP_CEB((pTime->ui32CenturyEnable)) |
  354. AM_REG_RTC_CTRUP_CB((pTime->ui32Century)) |
  355. AM_REG_RTC_CTRUP_CTRWKDY((pTime->ui32Weekday)) |
  356. AM_REG_RTC_CTRUP_CTRYR(dec_to_bcd((pTime->ui32Year))) |
  357. AM_REG_RTC_CTRUP_CTRMO(dec_to_bcd((pTime->ui32Month))) |
  358. AM_REG_RTC_CTRUP_CTRDATE(dec_to_bcd((pTime->ui32DayOfMonth)));
  359. //
  360. // Disable writing to the counters.
  361. //
  362. AM_REG(RTC, RTCCTL) |= AM_REG_RTC_RTCCTL_WRTC(0);
  363. }
  364. //*****************************************************************************
  365. //
  366. //! @brief Get the Real Time Clock current time.
  367. //!
  368. //! @param *pTime - A pointer to the time structure to store the current time.
  369. //!
  370. //! Gets the RTC's current time
  371. //!
  372. //! @return 0 for success and 1 for error.
  373. //
  374. //*****************************************************************************
  375. uint32_t
  376. am_hal_rtc_time_get(am_hal_rtc_time_t *pTime)
  377. {
  378. uint32_t ui32RTCLow, ui32RTCUp, ui32Value;
  379. //
  380. // Read the upper and lower RTC registers.
  381. //
  382. ui32RTCLow = AM_REG(RTC, CTRLOW);
  383. ui32RTCUp = AM_REG(RTC, CTRUP);
  384. //
  385. // Break out the lower word.
  386. //
  387. ui32Value =
  388. ((ui32RTCLow & AM_REG_RTC_CTRLOW_CTRHR_M) >> AM_REG_RTC_CTRLOW_CTRHR_S);
  389. pTime->ui32Hour = bcd_to_dec(ui32Value);
  390. ui32Value =
  391. ((ui32RTCLow & AM_REG_RTC_CTRLOW_CTRMIN_M) >> AM_REG_RTC_CTRLOW_CTRMIN_S);
  392. pTime->ui32Minute = bcd_to_dec(ui32Value);
  393. ui32Value =
  394. ((ui32RTCLow & AM_REG_RTC_CTRLOW_CTRSEC_M) >> AM_REG_RTC_CTRLOW_CTRSEC_S);
  395. pTime->ui32Second = bcd_to_dec(ui32Value);
  396. ui32Value =
  397. ((ui32RTCLow & AM_REG_RTC_CTRLOW_CTR100_M) >> AM_REG_RTC_CTRLOW_CTR100_S);
  398. pTime->ui32Hundredths = bcd_to_dec(ui32Value);
  399. //
  400. // Break out the upper word.
  401. //
  402. pTime->ui32ReadError =
  403. ((ui32RTCUp & AM_REG_RTC_CTRUP_CTERR_M) >> AM_REG_RTC_CTRUP_CTERR_S);
  404. pTime->ui32CenturyEnable =
  405. ((ui32RTCUp & AM_REG_RTC_CTRUP_CEB_M) >> AM_REG_RTC_CTRUP_CEB_S);
  406. pTime->ui32Century =
  407. ((ui32RTCUp & AM_REG_RTC_CTRUP_CB_M) >> AM_REG_RTC_CTRUP_CB_S);
  408. ui32Value =
  409. ((ui32RTCUp & AM_REG_RTC_CTRUP_CTRWKDY_M) >> AM_REG_RTC_CTRUP_CTRWKDY_S);
  410. pTime->ui32Weekday = bcd_to_dec(ui32Value);
  411. ui32Value =
  412. ((ui32RTCUp & AM_REG_RTC_CTRUP_CTRYR_M) >> AM_REG_RTC_CTRUP_CTRYR_S);
  413. pTime->ui32Year = bcd_to_dec(ui32Value);
  414. ui32Value =
  415. ((ui32RTCUp & AM_REG_RTC_CTRUP_CTRMO_M) >> AM_REG_RTC_CTRUP_CTRMO_S);
  416. pTime->ui32Month = bcd_to_dec(ui32Value);
  417. ui32Value =
  418. ((ui32RTCUp & AM_REG_RTC_CTRUP_CTRDATE_M) >> AM_REG_RTC_CTRUP_CTRDATE_S);
  419. pTime->ui32DayOfMonth = bcd_to_dec(ui32Value);
  420. //
  421. // Was there a read error?
  422. //
  423. if (pTime->ui32ReadError)
  424. {
  425. return 1;
  426. }
  427. else
  428. {
  429. return 0;
  430. }
  431. }
  432. //*****************************************************************************
  433. //
  434. //! @brief Sets the alarm repeat interval.
  435. //!
  436. //! @param ui32RepeatInterval the desired repeat interval.
  437. //!
  438. //! Sets the alarm repeat interval.
  439. //!
  440. //! Valid values for ui32RepeatInterval:
  441. //!
  442. //! AM_HAL_RTC_ALM_RPT_DIS
  443. //! AM_HAL_RTC_ALM_RPT_YR
  444. //! AM_HAL_RTC_ALM_RPT_MTH
  445. //! AM_HAL_RTC_ALM_RPT_WK
  446. //! AM_HAL_RTC_ALM_RPT_DAY
  447. //! AM_HAL_RTC_ALM_RPT_HR
  448. //! AM_HAL_RTC_ALM_RPT_MIN
  449. //! AM_HAL_RTC_ALM_RPT_SEC
  450. //! AM_HAL_RTC_ALM_RPT_10TH
  451. //! AM_HAL_RTC_ALM_RPT_100TH
  452. //!
  453. //! @return None.
  454. //
  455. //*****************************************************************************
  456. void
  457. am_hal_rtc_alarm_interval_set(uint32_t ui32RepeatInterval)
  458. {
  459. uint32_t ui32RptInt, ui32Alm100, ui32Value;
  460. switch(ui32RepeatInterval)
  461. {
  462. //
  463. // If repeat every 10th set RPT and ALM100 field accordinly
  464. //
  465. case AM_HAL_RTC_ALM_RPT_10TH:
  466. ui32RptInt = AM_HAL_RTC_ALM_RPT_SEC;
  467. ui32Alm100 = AM_HAL_RTC_ALM100_10TH;
  468. break;
  469. //
  470. // If repeat every 100th set RPT and ALM100 field accordinly
  471. //
  472. case AM_HAL_RTC_ALM_RPT_100TH:
  473. ui32RptInt = AM_HAL_RTC_ALM_RPT_SEC;
  474. ui32Alm100 = AM_HAL_RTC_ALM100_100TH;
  475. break;
  476. //
  477. // Otherwise set RPT as value passed. ALM100 values need to be 0xnn
  478. // in this setting where n = 0-9.
  479. //
  480. default:
  481. //
  482. // Get the current value of the ALM100 field.
  483. //
  484. ui32Value = AM_BFR(RTC, ALMLOW, ALM100);
  485. //
  486. // If ALM100 was previous EVERY_10TH or EVERY_100TH reset to zero
  487. // otherwise keep previous setting.
  488. //
  489. ui32Alm100 = ui32Value >= 0xF0 ? 0 : ui32Value;
  490. //
  491. // Set RPT value to value passed.
  492. //
  493. ui32RptInt = ui32RepeatInterval;
  494. break;
  495. }
  496. //
  497. // Write the interval to the register.
  498. //
  499. AM_BFW(RTC, RTCCTL, RPT, ui32RptInt);
  500. //
  501. // Write the Alarm 100 bits in the ALM100 register.
  502. //
  503. AM_BFW(RTC, ALMLOW, ALM100, ui32Alm100);
  504. }
  505. //*****************************************************************************
  506. //
  507. //! @brief Sets the RTC's Alarm.
  508. //!
  509. //! @param *pTime - A pointer to the time structure.
  510. //! @param ui32RepeatInterval - the desired alarm repeat interval.
  511. //!
  512. //! Set the Real Time Clock Alarm Parameters.
  513. //!
  514. //! Valid values for ui32RepeatInterval:
  515. //!
  516. //! AM_HAL_RTC_ALM_RPT_DIS
  517. //! AM_HAL_RTC_ALM_RPT_YR
  518. //! AM_HAL_RTC_ALM_RPT_MTH
  519. //! AM_HAL_RTC_ALM_RPT_WK
  520. //! AM_HAL_RTC_ALM_RPT_DAY
  521. //! AM_HAL_RTC_ALM_RPT_HR
  522. //! AM_HAL_RTC_ALM_RPT_MIN
  523. //! AM_HAL_RTC_ALM_RPT_SEC
  524. //! AM_HAL_RTC_ALM_RPT_10TH
  525. //! AM_HAL_RTC_ALM_RPT_EVERY_100TH
  526. //!
  527. //! @return None.
  528. //
  529. //*****************************************************************************
  530. void
  531. am_hal_rtc_alarm_set(am_hal_rtc_time_t *pTime, uint32_t ui32RepeatInterval)
  532. {
  533. uint8_t ui8Value = 0;
  534. //
  535. // Write the interval to the register.
  536. //
  537. AM_REG(RTC, RTCCTL) |=
  538. AM_REG_RTC_RTCCTL_RPT(ui32RepeatInterval > 0x7 ? 0x7 : ui32RepeatInterval);
  539. //
  540. // Check if the interval is 10th or every 100th and track it in ui8Value.
  541. //
  542. if (ui32RepeatInterval == AM_HAL_RTC_ALM_RPT_10TH)
  543. {
  544. ui8Value = 0xF0;
  545. }
  546. else if (ui32RepeatInterval == AM_HAL_RTC_ALM_RPT_100TH)
  547. {
  548. ui8Value = 0xFF;
  549. }
  550. //
  551. // Write the ALMUP register.
  552. //
  553. AM_REG(RTC, ALMUP) =
  554. AM_REG_RTC_ALMUP_ALMWKDY((pTime->ui32Weekday)) |
  555. AM_REG_RTC_ALMUP_ALMMO(dec_to_bcd((pTime->ui32Month))) |
  556. AM_REG_RTC_ALMUP_ALMDATE(dec_to_bcd((pTime->ui32DayOfMonth)));
  557. //
  558. // Write the ALMLOW register.
  559. //
  560. AM_REG(RTC, ALMLOW) =
  561. AM_REG_RTC_ALMLOW_ALMHR(dec_to_bcd(pTime->ui32Hour)) |
  562. AM_REG_RTC_ALMLOW_ALMMIN(dec_to_bcd(pTime->ui32Minute)) |
  563. AM_REG_RTC_ALMLOW_ALMSEC(dec_to_bcd(pTime->ui32Second)) |
  564. AM_REG_RTC_ALMLOW_ALM100(dec_to_bcd(pTime->ui32Hundredths) | ui8Value);
  565. }
  566. //*****************************************************************************
  567. //
  568. //! @brief Get the Real Time Clock Alarm Parameters
  569. //!
  570. //! @param *pTime - A pointer to the time structure to store the current alarm.
  571. //!
  572. //! Gets the RTC's Alarm time
  573. //!
  574. //! @return None.
  575. //
  576. //*****************************************************************************
  577. void
  578. am_hal_rtc_alarm_get(am_hal_rtc_time_t *pTime)
  579. {
  580. uint32_t ui32ALMLow, ui32ALMUp, ui32Value;
  581. //
  582. // Read the upper and lower RTC registers.
  583. //
  584. ui32ALMLow = AM_REG(RTC, ALMLOW);
  585. ui32ALMUp = AM_REG(RTC, ALMUP);
  586. //
  587. // Break out the lower word.
  588. //
  589. ui32Value =
  590. ((ui32ALMLow & AM_REG_RTC_ALMLOW_ALMHR_M) >> AM_REG_RTC_ALMLOW_ALMHR_S);
  591. pTime->ui32Hour = bcd_to_dec(ui32Value);
  592. ui32Value =
  593. ((ui32ALMLow & AM_REG_RTC_ALMLOW_ALMMIN_M) >> AM_REG_RTC_ALMLOW_ALMMIN_S);
  594. pTime->ui32Minute = bcd_to_dec(ui32Value);
  595. ui32Value =
  596. ((ui32ALMLow & AM_REG_RTC_ALMLOW_ALMSEC_M) >> AM_REG_RTC_ALMLOW_ALMSEC_S);
  597. pTime->ui32Second = bcd_to_dec(ui32Value);
  598. ui32Value =
  599. ((ui32ALMLow & AM_REG_RTC_ALMLOW_ALM100_M) >> AM_REG_RTC_ALMLOW_ALM100_S);
  600. pTime->ui32Hundredths = bcd_to_dec(ui32Value);
  601. //
  602. // Break out the upper word.
  603. //
  604. pTime->ui32ReadError = 0;
  605. pTime->ui32CenturyEnable = 0;
  606. pTime->ui32Century = 0;
  607. ui32Value =
  608. ((ui32ALMUp & AM_REG_RTC_ALMUP_ALMWKDY_M) >> AM_REG_RTC_ALMUP_ALMWKDY_S);
  609. pTime->ui32Weekday = bcd_to_dec(ui32Value);
  610. pTime->ui32Year = 0;
  611. ui32Value =
  612. ((ui32ALMUp & AM_REG_RTC_ALMUP_ALMMO_M) >> AM_REG_RTC_ALMUP_ALMMO_S);
  613. pTime->ui32Month = bcd_to_dec(ui32Value);
  614. ui32Value =
  615. ((ui32ALMUp & AM_REG_RTC_ALMUP_ALMDATE_M) >> AM_REG_RTC_ALMUP_ALMDATE_S);
  616. pTime->ui32DayOfMonth = bcd_to_dec(ui32Value);
  617. }
  618. //*****************************************************************************
  619. //
  620. // End Doxygen group.
  621. //! @}
  622. //
  623. //*****************************************************************************