nu_rtc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /**************************************************************************//**
  2. * @file rtc.c
  3. * @version V3.00
  4. * $Revision: 5 $
  5. * $Date: 14/06/10 5:49p $
  6. * @brief NUC980 series RTC driver source file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #include "nu_rtc.h"
  13. /** @cond HIDDEN_SYMBOLS */
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Macro, type and constant definitions */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. #define RTC_GLOBALS
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. /* Global file scope (static) variables */
  20. /*---------------------------------------------------------------------------------------------------------*/
  21. static volatile uint32_t g_u32hiYear, g_u32loYear, g_u32hiMonth, g_u32loMonth, g_u32hiDay, g_u32loDay;
  22. static volatile uint32_t g_u32hiHour, g_u32loHour, g_u32hiMin, g_u32loMin, g_u32hiSec, g_u32loSec;
  23. /** @endcond HIDDEN_SYMBOLS */
  24. /** @addtogroup Standard_Driver Standard Driver
  25. @{
  26. */
  27. /** @addtogroup RTC_Driver RTC Driver
  28. @{
  29. */
  30. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  31. @{
  32. */
  33. /**
  34. * @brief Initialize RTC module and start counting
  35. *
  36. * @param[in] sPt Specify the time property and current date and time. It includes: \n
  37. * u32Year: Year value, range between 2000 ~ 2099. \n
  38. * u32Month: Month value, range between 1 ~ 12. \n
  39. * u32Day: Day value, range between 1 ~ 31. \n
  40. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  41. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  42. * RTC_SATURDAY] \n
  43. * u32Hour: Hour value, range between 0 ~ 23. \n
  44. * u32Minute: Minute value, range between 0 ~ 59. \n
  45. * u32Second: Second value, range between 0 ~ 59. \n
  46. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  47. * u8AmPm: [RTC_AM / RTC_PM] \n
  48. *
  49. * @return None
  50. *
  51. * @details This function is used to: \n
  52. * 1. Write initial key to let RTC start count. \n
  53. * 2. Input parameter indicates start date/time. \n
  54. * 3. User has to make sure that parameters of RTC date/time are reasonable. \n
  55. * @note Null pointer for using default starting date/time.
  56. */
  57. void RTC_Open(S_RTC_TIME_DATA_T *sPt)
  58. {
  59. RTC->INIT = RTC_INIT_KEY;
  60. RTC_Check();
  61. if (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  62. {
  63. RTC->INIT = RTC_INIT_KEY;
  64. while (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  65. {
  66. }
  67. }
  68. if (sPt == 0)
  69. {
  70. }
  71. else
  72. {
  73. /* Set RTC date and time */
  74. RTC_SetDateAndTime(sPt);
  75. }
  76. }
  77. /**
  78. * @brief Disable RTC Clock
  79. *
  80. * @param None
  81. *
  82. * @return None
  83. *
  84. * @details This API will disable RTC peripheral clock and stops RTC counting.
  85. */
  86. void RTC_Close(void)
  87. {
  88. outp32(REG_CLK_PCLKEN0, inp32(REG_CLK_PCLKEN0 & ~(0x1 << 1)));
  89. }
  90. /**
  91. * @brief Set Frequency Compensation Data
  92. *
  93. * @param[in] i32FrequencyX100 Specify the RTC clock X100, ex: 3277365 means 32773.65.
  94. *
  95. * @return None
  96. *
  97. */
  98. void RTC_32KCalibration(int32_t i32FrequencyX100)
  99. {
  100. INT32 i32RegInt, i32RegFra;
  101. UINT32 u32Reg;
  102. /* Compute integer and fraction for RTC FCR register */
  103. i32RegInt = (i32FrequencyX100 / 100) - RTC_FCR_REFERENCE;
  104. i32RegFra = (((i32FrequencyX100 % 100)) * 60) / 100;
  105. /* Judge Integer part is reasonable */
  106. if (i32RegInt < 0) i32RegInt = 0;
  107. if (i32RegInt > 15) i32RegInt = 15;
  108. u32Reg = (uint32_t)((i32RegInt << 8) | i32RegFra);
  109. RTC_WaitAccessEnable();
  110. outp32(REG_RTC_FREQADJ, u32Reg);
  111. RTC_Check();
  112. }
  113. /**
  114. * @brief Get Current RTC Date and Time
  115. *
  116. * @param[out] sPt The returned pointer is specified the current RTC value. It includes: \n
  117. * u32Year: Year value \n
  118. * u32Month: Month value \n
  119. * u32Day: Day value \n
  120. * u32DayOfWeek: Day of week \n
  121. * u32Hour: Hour value \n
  122. * u32Minute: Minute value \n
  123. * u32Second: Second value \n
  124. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  125. * u8AmPm: [RTC_AM / RTC_PM] \n
  126. *
  127. * @return None
  128. *
  129. * @details This API is used to get the current RTC date and time value.
  130. */
  131. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
  132. {
  133. uint32_t u32Tmp;
  134. sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  135. sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  136. /* Get [Date digit] data */
  137. g_u32hiYear = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
  138. g_u32loYear = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
  139. g_u32hiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
  140. g_u32loMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
  141. g_u32hiDay = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
  142. g_u32loDay = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
  143. /* Get [Time digit] data */
  144. g_u32hiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
  145. g_u32loHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
  146. g_u32hiMin = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
  147. g_u32loMin = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
  148. g_u32hiSec = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
  149. g_u32loSec = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
  150. /* Compute to 20XX year */
  151. u32Tmp = (g_u32hiYear * 10ul);
  152. u32Tmp += g_u32loYear;
  153. sPt->u32Year = u32Tmp + RTC_YEAR2000;
  154. /* Compute 0~12 month */
  155. u32Tmp = (g_u32hiMonth * 10ul);
  156. sPt->u32Month = u32Tmp + g_u32loMonth;
  157. /* Compute 0~31 day */
  158. u32Tmp = (g_u32hiDay * 10ul);
  159. sPt->u32Day = u32Tmp + g_u32loDay;
  160. /* Compute 12/24 hour */
  161. if (sPt->u32TimeScale == RTC_CLOCK_12)
  162. {
  163. u32Tmp = (g_u32hiHour * 10ul);
  164. u32Tmp += g_u32loHour;
  165. sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  166. if (sPt->u32Hour >= 21ul)
  167. {
  168. sPt->u32AmPm = RTC_PM;
  169. sPt->u32Hour -= 20ul;
  170. }
  171. else
  172. {
  173. sPt->u32AmPm = RTC_AM;
  174. }
  175. u32Tmp = (g_u32hiMin * 10ul);
  176. u32Tmp += g_u32loMin;
  177. sPt->u32Minute = u32Tmp;
  178. u32Tmp = (g_u32hiSec * 10ul);
  179. u32Tmp += g_u32loSec;
  180. sPt->u32Second = u32Tmp;
  181. }
  182. else
  183. {
  184. u32Tmp = (g_u32hiHour * 10ul);
  185. u32Tmp += g_u32loHour;
  186. sPt->u32Hour = u32Tmp;
  187. u32Tmp = (g_u32hiMin * 10ul);
  188. u32Tmp += g_u32loMin;
  189. sPt->u32Minute = u32Tmp;
  190. u32Tmp = (g_u32hiSec * 10ul);
  191. u32Tmp += g_u32loSec;
  192. sPt->u32Second = u32Tmp;
  193. }
  194. }
  195. /**
  196. * @brief Get RTC Alarm Date and Time
  197. *
  198. * @param[out] sPt The returned pointer is specified the RTC alarm value. It includes: \n
  199. * u32Year: Year value \n
  200. * u32Month: Month value \n
  201. * u32Day: Day value \n
  202. * u32DayOfWeek: Day of week \n
  203. * u32Hour: Hour value \n
  204. * u32Minute: Minute value \n
  205. * u32Second: Second value \n
  206. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  207. * u8AmPm: [RTC_AM / RTC_PM] \n
  208. *
  209. * @return None
  210. *
  211. * @details This API is used to get the RTC alarm date and time setting.
  212. */
  213. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
  214. {
  215. uint32_t u32Tmp;
  216. sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  217. sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  218. /* Get alarm [Date digit] data */
  219. RTC_WaitAccessEnable();
  220. g_u32hiYear = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
  221. g_u32loYear = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
  222. g_u32hiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
  223. g_u32loMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
  224. g_u32hiDay = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
  225. g_u32loDay = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
  226. /* Get alarm [Time digit] data */
  227. RTC_WaitAccessEnable();
  228. g_u32hiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
  229. g_u32loHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
  230. g_u32hiMin = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
  231. g_u32loMin = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
  232. g_u32hiSec = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
  233. g_u32loSec = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
  234. /* Compute to 20XX year */
  235. u32Tmp = (g_u32hiYear * 10ul);
  236. u32Tmp += g_u32loYear;
  237. sPt->u32Year = u32Tmp + RTC_YEAR2000;
  238. /* Compute 0~12 month */
  239. u32Tmp = (g_u32hiMonth * 10ul);
  240. sPt->u32Month = u32Tmp + g_u32loMonth;
  241. /* Compute 0~31 day */
  242. u32Tmp = (g_u32hiDay * 10ul);
  243. sPt->u32Day = u32Tmp + g_u32loDay;
  244. /* Compute 12/24 hour */
  245. if (sPt->u32TimeScale == RTC_CLOCK_12)
  246. {
  247. u32Tmp = (g_u32hiHour * 10ul);
  248. u32Tmp += g_u32loHour;
  249. sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  250. if (sPt->u32Hour >= 21ul)
  251. {
  252. sPt->u32AmPm = RTC_PM;
  253. sPt->u32Hour -= 20ul;
  254. }
  255. else
  256. {
  257. sPt->u32AmPm = RTC_AM;
  258. }
  259. u32Tmp = (g_u32hiMin * 10ul);
  260. u32Tmp += g_u32loMin;
  261. sPt->u32Minute = u32Tmp;
  262. u32Tmp = (g_u32hiSec * 10ul);
  263. u32Tmp += g_u32loSec;
  264. sPt->u32Second = u32Tmp;
  265. }
  266. else
  267. {
  268. u32Tmp = (g_u32hiHour * 10ul);
  269. u32Tmp += g_u32loHour;
  270. sPt->u32Hour = u32Tmp;
  271. u32Tmp = (g_u32hiMin * 10ul);
  272. u32Tmp += g_u32loMin;
  273. sPt->u32Minute = u32Tmp;
  274. u32Tmp = (g_u32hiSec * 10ul);
  275. u32Tmp += g_u32loSec;
  276. sPt->u32Second = u32Tmp;
  277. }
  278. }
  279. /**
  280. * @brief Update Current RTC Date and Time
  281. *
  282. * @param[in] sPt Specify the time property and current date and time. It includes: \n
  283. * u32Year: Year value, range between 2000 ~ 2099. \n
  284. * u32Month: Month value, range between 1 ~ 12. \n
  285. * u32Day: Day value, range between 1 ~ 31. \n
  286. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  287. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  288. * RTC_SATURDAY] \n
  289. * u32Hour: Hour value, range between 0 ~ 23. \n
  290. * u32Minute: Minute value, range between 0 ~ 59. \n
  291. * u32Second: Second value, range between 0 ~ 59. \n
  292. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  293. * u8AmPm: [RTC_AM / RTC_PM] \n
  294. *
  295. * @return None
  296. *
  297. * @details This API is used to update current date and time to RTC.
  298. */
  299. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
  300. {
  301. uint32_t u32RegCAL, u32RegTIME;
  302. if (sPt == NULL)
  303. {
  304. }
  305. else
  306. {
  307. /*-----------------------------------------------------------------------------------------------------*/
  308. /* Set RTC 24/12 hour setting and Day of the Week */
  309. /*-----------------------------------------------------------------------------------------------------*/
  310. RTC_WaitAccessEnable();
  311. if (sPt->u32TimeScale == RTC_CLOCK_12)
  312. {
  313. RTC_WaitAccessEnable();
  314. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  315. RTC_Check();
  316. /*-------------------------------------------------------------------------------------------------*/
  317. /* Important, range of 12-hour PM mode is 21 up to 32 */
  318. /*-------------------------------------------------------------------------------------------------*/
  319. if (sPt->u32AmPm == RTC_PM)
  320. {
  321. sPt->u32Hour += 20ul;
  322. }
  323. }
  324. else
  325. {
  326. RTC_WaitAccessEnable();
  327. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  328. RTC_Check();
  329. }
  330. /* Set Day of the Week */
  331. RTC_WaitAccessEnable();
  332. RTC->WEEKDAY = sPt->u32DayOfWeek;
  333. RTC_Check();
  334. /*-----------------------------------------------------------------------------------------------------*/
  335. /* Set RTC Current Date and Time */
  336. /*-----------------------------------------------------------------------------------------------------*/
  337. u32RegCAL = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  338. u32RegCAL |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  339. u32RegCAL |= ((sPt->u32Month / 10ul) << 12);
  340. u32RegCAL |= ((sPt->u32Month % 10ul) << 8);
  341. u32RegCAL |= ((sPt->u32Day / 10ul) << 4);
  342. u32RegCAL |= (sPt->u32Day % 10ul);
  343. u32RegTIME = ((sPt->u32Hour / 10ul) << 20);
  344. u32RegTIME |= ((sPt->u32Hour % 10ul) << 16);
  345. u32RegTIME |= ((sPt->u32Minute / 10ul) << 12);
  346. u32RegTIME |= ((sPt->u32Minute % 10ul) << 8);
  347. u32RegTIME |= ((sPt->u32Second / 10ul) << 4);
  348. u32RegTIME |= (sPt->u32Second % 10ul);
  349. /*-----------------------------------------------------------------------------------------------------*/
  350. /* Set RTC Calender and Time Loading */
  351. /*-----------------------------------------------------------------------------------------------------*/
  352. RTC_WaitAccessEnable();
  353. RTC->CAL = (uint32_t)u32RegCAL;
  354. RTC_Check();
  355. RTC_WaitAccessEnable();
  356. RTC->TIME = (uint32_t)u32RegTIME;
  357. RTC_Check();
  358. }
  359. }
  360. /**
  361. * @brief Update RTC Alarm Date and Time
  362. *
  363. * @param[in] sPt Specify the time property and alarm date and time. It includes: \n
  364. * u32Year: Year value, range between 2000 ~ 2099. \n
  365. * u32Month: Month value, range between 1 ~ 12. \n
  366. * u32Day: Day value, range between 1 ~ 31. \n
  367. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  368. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  369. * RTC_SATURDAY] \n
  370. * u32Hour: Hour value, range between 0 ~ 23. \n
  371. * u32Minute: Minute value, range between 0 ~ 59. \n
  372. * u32Second: Second value, range between 0 ~ 59. \n
  373. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  374. * u8AmPm: [RTC_AM / RTC_PM] \n
  375. *
  376. * @return None
  377. *
  378. * @details This API is used to update alarm date and time setting to RTC.
  379. */
  380. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
  381. {
  382. uint32_t u32RegCALM, u32RegTALM;
  383. if (sPt == 0)
  384. {
  385. }
  386. else
  387. {
  388. /*-----------------------------------------------------------------------------------------------------*/
  389. /* Set RTC 24/12 hour setting and Day of the Week */
  390. /*-----------------------------------------------------------------------------------------------------*/
  391. RTC_WaitAccessEnable();
  392. if (sPt->u32TimeScale == RTC_CLOCK_12)
  393. {
  394. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  395. /*-------------------------------------------------------------------------------------------------*/
  396. /* Important, range of 12-hour PM mode is 21 up to 32 */
  397. /*-------------------------------------------------------------------------------------------------*/
  398. if (sPt->u32AmPm == RTC_PM)
  399. {
  400. sPt->u32Hour += 20ul;
  401. }
  402. }
  403. else
  404. {
  405. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  406. }
  407. RTC_Check();
  408. /*-----------------------------------------------------------------------------------------------------*/
  409. /* Set RTC Alarm Date and Time */
  410. /*-----------------------------------------------------------------------------------------------------*/
  411. u32RegCALM = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  412. u32RegCALM |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  413. u32RegCALM |= ((sPt->u32Month / 10ul) << 12);
  414. u32RegCALM |= ((sPt->u32Month % 10ul) << 8);
  415. u32RegCALM |= ((sPt->u32Day / 10ul) << 4);
  416. u32RegCALM |= (sPt->u32Day % 10ul);
  417. u32RegCALM |= (sPt->u32DayOfWeek << 24);
  418. u32RegTALM = ((sPt->u32Hour / 10ul) << 20);
  419. u32RegTALM |= ((sPt->u32Hour % 10ul) << 16);
  420. u32RegTALM |= ((sPt->u32Minute / 10ul) << 12);
  421. u32RegTALM |= ((sPt->u32Minute % 10ul) << 8);
  422. u32RegTALM |= ((sPt->u32Second / 10ul) << 4);
  423. u32RegTALM |= (sPt->u32Second % 10ul);
  424. RTC_WaitAccessEnable();
  425. RTC->CALM = (uint32_t)u32RegCALM;
  426. RTC_Check();
  427. RTC_WaitAccessEnable();
  428. RTC->TALM = (uint32_t)u32RegTALM;
  429. RTC_Check();
  430. }
  431. }
  432. /**
  433. * @brief Update RTC Current Date
  434. *
  435. * @param[in] u32Year The year calendar digit of current RTC setting.
  436. * @param[in] u32Month The month calendar digit of current RTC setting.
  437. * @param[in] u32Day The day calendar digit of current RTC setting.
  438. * @param[in] u32DayOfWeek The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  439. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  440. * RTC_SATURDAY]
  441. *
  442. * @return None
  443. *
  444. * @details This API is used to update current date to RTC.
  445. */
  446. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
  447. {
  448. uint32_t u32RegCAL;
  449. u32RegCAL = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  450. u32RegCAL |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  451. u32RegCAL |= ((u32Month / 10ul) << 12);
  452. u32RegCAL |= ((u32Month % 10ul) << 8);
  453. u32RegCAL |= ((u32Day / 10ul) << 4);
  454. u32RegCAL |= (u32Day % 10ul);
  455. /* Set Day of the Week */
  456. RTC_WaitAccessEnable();
  457. RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
  458. RTC_Check();
  459. /* Set RTC Calender Loading */
  460. RTC_WaitAccessEnable();
  461. RTC->CAL = (uint32_t)u32RegCAL;
  462. RTC_Check();
  463. }
  464. /**
  465. * @brief Update RTC Current Time
  466. *
  467. * @param[in] u32Hour The hour time digit of current RTC setting.
  468. * @param[in] u32Minute The minute time digit of current RTC setting.
  469. * @param[in] u32Second The second time digit of current RTC setting.
  470. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  471. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  472. *
  473. * @return None
  474. *
  475. * @details This API is used to update current time to RTC.
  476. */
  477. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  478. {
  479. uint32_t u32RegTIME;
  480. /* Important, range of 12-hour PM mode is 21 up to 32 */
  481. if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
  482. {
  483. u32Hour += 20ul;
  484. }
  485. u32RegTIME = ((u32Hour / 10ul) << 20);
  486. u32RegTIME |= ((u32Hour % 10ul) << 16);
  487. u32RegTIME |= ((u32Minute / 10ul) << 12);
  488. u32RegTIME |= ((u32Minute % 10ul) << 8);
  489. u32RegTIME |= ((u32Second / 10ul) << 4);
  490. u32RegTIME |= (u32Second % 10ul);
  491. /*-----------------------------------------------------------------------------------------------------*/
  492. /* Set RTC 24/12 hour setting and Day of the Week */
  493. /*-----------------------------------------------------------------------------------------------------*/
  494. RTC_WaitAccessEnable();
  495. if (u32TimeMode == RTC_CLOCK_12)
  496. {
  497. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  498. }
  499. else
  500. {
  501. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  502. }
  503. RTC_Check();
  504. RTC_WaitAccessEnable();
  505. RTC->TIME = (uint32_t)u32RegTIME;
  506. RTC_Check();
  507. }
  508. /**
  509. * @brief Update RTC Alarm Date
  510. *
  511. * @param[in] u32Year The year calendar digit of RTC alarm setting.
  512. * @param[in] u32Month The month calendar digit of RTC alarm setting.
  513. * @param[in] u32Day The day calendar digit of RTC alarm setting.
  514. * @param[in] u32DayOfWeek The day of week
  515. *
  516. * @return None
  517. *
  518. * @details This API is used to update alarm date setting to RTC.
  519. */
  520. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
  521. {
  522. uint32_t u32RegCALM;
  523. u32RegCALM = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  524. u32RegCALM |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  525. u32RegCALM |= ((u32Month / 10ul) << 12);
  526. u32RegCALM |= ((u32Month % 10ul) << 8);
  527. u32RegCALM |= ((u32Day / 10ul) << 4);
  528. u32RegCALM |= (u32Day % 10ul);
  529. u32RegCALM |= (u32DayOfWeek << 24);
  530. RTC_WaitAccessEnable();
  531. /* Set RTC Alarm Date */
  532. RTC->CALM = (uint32_t)u32RegCALM;
  533. RTC_Check();
  534. }
  535. /**
  536. * @brief Update RTC Alarm Time
  537. *
  538. * @param[in] u32Hour The hour time digit of RTC alarm setting.
  539. * @param[in] u32Minute The minute time digit of RTC alarm setting.
  540. * @param[in] u32Second The second time digit of RTC alarm setting.
  541. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  542. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  543. *
  544. * @return None
  545. *
  546. * @details This API is used to update alarm time setting to RTC.
  547. */
  548. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  549. {
  550. uint32_t u32RegTALM;
  551. /* Important, range of 12-hour PM mode is 21 up to 32 */
  552. if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
  553. {
  554. u32Hour += 20ul;
  555. }
  556. u32RegTALM = ((u32Hour / 10ul) << 20);
  557. u32RegTALM |= ((u32Hour % 10ul) << 16);
  558. u32RegTALM |= ((u32Minute / 10ul) << 12);
  559. u32RegTALM |= ((u32Minute % 10ul) << 8);
  560. u32RegTALM |= ((u32Second / 10ul) << 4);
  561. u32RegTALM |= (u32Second % 10ul);
  562. /*-----------------------------------------------------------------------------------------------------*/
  563. /* Set RTC 24/12 hour setting and Day of the Week */
  564. /*-----------------------------------------------------------------------------------------------------*/
  565. RTC_WaitAccessEnable();
  566. if (u32TimeMode == RTC_CLOCK_12)
  567. {
  568. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  569. }
  570. else
  571. {
  572. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  573. }
  574. RTC_Check();
  575. /* Set RTC Alarm Time */
  576. RTC_WaitAccessEnable();
  577. RTC->TALM = (uint32_t)u32RegTALM;
  578. RTC_Check();
  579. }
  580. /**
  581. * @brief Get Day of the Week
  582. *
  583. * @param None
  584. *
  585. * @retval 0 Sunday
  586. * @retval 1 Monday
  587. * @retval 2 Tuesday
  588. * @retval 3 Wednesday
  589. * @retval 4 Thursday
  590. * @retval 5 Friday
  591. * @retval 6 Saturday
  592. *
  593. * @details This API is used to get day of the week of current RTC date.
  594. */
  595. uint32_t RTC_GetDayOfWeek(void)
  596. {
  597. return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
  598. }
  599. /**
  600. * @brief Set RTC Tick Period Time
  601. *
  602. * @param[in] u32TickSelection It is used to set the RTC tick period time for Periodic Time Tick request. \n
  603. * It consists of:
  604. * - \ref RTC_TICK_1_SEC : Time tick is 1 second
  605. * - \ref RTC_TICK_1_2_SEC : Time tick is 1/2 second
  606. * - \ref RTC_TICK_1_4_SEC : Time tick is 1/4 second
  607. * - \ref RTC_TICK_1_8_SEC : Time tick is 1/8 second
  608. * - \ref RTC_TICK_1_16_SEC : Time tick is 1/16 second
  609. * - \ref RTC_TICK_1_32_SEC : Time tick is 1/32 second
  610. * - \ref RTC_TICK_1_64_SEC : Time tick is 1/64 second
  611. * - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
  612. *
  613. * @return None
  614. *
  615. * @details This API is used to set RTC tick period time for each tick interrupt.
  616. */
  617. void RTC_SetTickPeriod(uint32_t u32TickSelection)
  618. {
  619. RTC_WaitAccessEnable();
  620. RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
  621. RTC_Check();
  622. }
  623. /**
  624. * @brief Enable RTC Interrupt
  625. *
  626. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  627. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  628. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  629. *
  630. * @return None
  631. *
  632. * @details This API is used to enable the specify RTC interrupt function.
  633. */
  634. void RTC_EnableInt(uint32_t u32IntFlagMask)
  635. {
  636. RTC_WaitAccessEnable();
  637. RTC->INTEN |= u32IntFlagMask;
  638. RTC_Check();
  639. if (u32IntFlagMask & RTC_INTEN_ALMIEN_Msk)
  640. {
  641. RTC_WaitAccessEnable();
  642. RTC->PWRCTL |= RTC_PWRCTL_ALARM_EN_Msk;
  643. RTC_Check();
  644. }
  645. if (u32IntFlagMask & RTC_INTEN_RELALMIEN_Msk)
  646. {
  647. RTC_WaitAccessEnable();
  648. RTC->PWRCTL |= RTC_PWRCTL_REL_ALARM_EN_Msk;
  649. RTC_Check();
  650. }
  651. }
  652. /**
  653. * @brief Disable RTC Interrupt
  654. *
  655. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  656. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  657. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  658. *
  659. * @return None
  660. *
  661. * @details This API is used to disable the specify RTC interrupt function.
  662. */
  663. void RTC_DisableInt(uint32_t u32IntFlagMask)
  664. {
  665. RTC_WaitAccessEnable();
  666. RTC->INTEN &= ~u32IntFlagMask;
  667. RTC_Check();
  668. RTC_WaitAccessEnable();
  669. RTC->INTSTS = u32IntFlagMask;
  670. RTC_Check();
  671. }
  672. /**
  673. * @brief Wait RTC Access Enable
  674. *
  675. * @param None
  676. *
  677. * @return None
  678. *
  679. * @details This function is used to enable the maximum RTC read/write accessible time.
  680. */
  681. void RTC_WaitAccessEnable(void)
  682. {
  683. INT32 volatile i32i;
  684. RTC_Check();
  685. outp32(REG_RTC_RWEN, RTC_WRITE_KEY);
  686. RTC_Check();
  687. while (!(inp32(REG_RTC_RWEN) & 0x10000));
  688. }
  689. void RTC_Check(void)
  690. {
  691. uint32_t i = 0;
  692. uint32_t Wait;
  693. Wait = inp32(REG_RTC_INTSTS) & RTC_INTSTS_REGWRBUSY_Msk;
  694. while (Wait == RTC_INTSTS_REGWRBUSY_Msk)
  695. {
  696. Wait = inp32(REG_RTC_INTSTS) & RTC_INTSTS_REGWRBUSY_Msk;
  697. i++;
  698. if (i > RTC_WAIT_COUNT)
  699. {
  700. //printf("Time out\n");
  701. break;
  702. }
  703. }
  704. }
  705. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  706. /*@}*/ /* end of group RTC_Driver */
  707. /*@}*/ /* end of group Standard_Driver */
  708. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/