nu_rtc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /**************************************************************************//**
  2. * @file rtc.c
  3. * @version V1.00
  4. * $Revision: 4 $
  5. * $Date: 18/04/25 11:43a $
  6. * @brief M031 series Real Time Clock(RTC) driver source file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #include "M031Series.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] psPt 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 *psPt)
  58. {
  59. RTC->INIT = RTC_INIT_KEY;
  60. if (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  61. {
  62. RTC->INIT = RTC_INIT_KEY;
  63. while (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  64. {
  65. }
  66. }
  67. if (psPt == NULL)
  68. {
  69. /* No RTC date/time data */
  70. }
  71. else
  72. {
  73. /* Set RTC date and time */
  74. RTC_SetDateAndTime(psPt);
  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. CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
  89. }
  90. /**
  91. * @brief Set Frequency Compensation Data
  92. *
  93. * @param[in] i32FrequencyX10000 Specify the RTC clock X 10000, ex: 327736512 means 32773.6512.
  94. *
  95. * @return None
  96. *
  97. */
  98. void RTC_32KCalibration(int32_t i32FrequencyX10000)
  99. {
  100. /*
  101. Frequency counter measurement : 32773.6512 Hz
  102. */
  103. uint32_t u32Index;
  104. uint32_t u32Compensate;
  105. /* 327736512 %10000 = 6512 */
  106. u32Compensate = (uint32_t)(i32FrequencyX10000 % 10000);
  107. /*Fraction Part: (6512 X 64)/10000 = 41.6768(0x2A) => RTC_FREQADJ[5:0]=0x2A*/
  108. u32Compensate = ((u32Compensate * 64) / 10000);
  109. u32Compensate &= 0x3F;
  110. /*
  111. Formula for 32K compensation is
  112. FREQADJ = 0~0x00001F00 (Frequency range : 32752Hz ~ 32783Hz)
  113. */
  114. if (i32FrequencyX10000 >= (uint32_t)327840000)
  115. {
  116. u32Compensate = 0x1F3F;
  117. }
  118. else if (i32FrequencyX10000 < (uint32_t)327520000)
  119. {
  120. u32Compensate = 0x0;
  121. }
  122. else
  123. {
  124. /* Integer Part: 32773 => RTC_FREQADJ[12:8] = 0x15 */
  125. for (u32Index = 0; u32Index < 0x20 ; u32Index++)
  126. {
  127. if ((i32FrequencyX10000 >= 327520000 + (u32Index * 10000)) && (i32FrequencyX10000 < 327520000 + ((u32Index + 1) * 10000)))
  128. {
  129. u32Compensate += (u32Index << RTC_FREQADJ_INTEGER_Pos);
  130. break;
  131. }
  132. }
  133. }
  134. RTC->FREQADJ = (uint32_t)u32Compensate;
  135. }
  136. /**
  137. * @brief Get Current RTC Date and Time
  138. *
  139. * @param[out] psPt The returned pointer is specified the current RTC value. It includes: \n
  140. * u32Year: Year value \n
  141. * u32Month: Month value \n
  142. * u32Day: Day value \n
  143. * u32DayOfWeek: Day of week \n
  144. * u32Hour: Hour value \n
  145. * u32Minute: Minute value \n
  146. * u32Second: Second value \n
  147. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  148. * u8AmPm: [RTC_AM / RTC_PM] \n
  149. *
  150. * @return None
  151. *
  152. * @details This API is used to get the current RTC date and time value.
  153. */
  154. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *psPt)
  155. {
  156. uint32_t u32Tmp;
  157. psPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  158. psPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  159. /* Get [Date digit] data */
  160. g_u32HiYear = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
  161. g_u32LoYear = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
  162. g_u32HiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
  163. g_u32LoMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
  164. g_u32HiDay = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
  165. g_u32LoDay = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
  166. /* Get [Time digit] data */
  167. g_u32HiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
  168. g_u32LoHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
  169. g_u32HiMin = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
  170. g_u32LoMin = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
  171. g_u32HiSec = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
  172. g_u32LoSec = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
  173. /* Compute to 20XX year */
  174. u32Tmp = (g_u32HiYear * 10ul);
  175. u32Tmp += g_u32LoYear;
  176. psPt->u32Year = u32Tmp + RTC_YEAR2000;
  177. /* Compute 0~12 month */
  178. u32Tmp = (g_u32HiMonth * 10ul);
  179. psPt->u32Month = u32Tmp + g_u32LoMonth;
  180. /* Compute 0~31 day */
  181. u32Tmp = (g_u32HiDay * 10ul);
  182. psPt->u32Day = u32Tmp + g_u32LoDay;
  183. /* Compute 12/24 hour */
  184. if (psPt->u32TimeScale == RTC_CLOCK_12)
  185. {
  186. u32Tmp = (g_u32HiHour * 10ul);
  187. u32Tmp += g_u32LoHour;
  188. psPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  189. if (psPt->u32Hour >= 21ul)
  190. {
  191. psPt->u32AmPm = RTC_PM;
  192. psPt->u32Hour -= 20ul;
  193. }
  194. else
  195. {
  196. psPt->u32AmPm = RTC_AM;
  197. }
  198. u32Tmp = (g_u32HiMin * 10ul);
  199. u32Tmp += g_u32LoMin;
  200. psPt->u32Minute = u32Tmp;
  201. u32Tmp = (g_u32HiSec * 10ul);
  202. u32Tmp += g_u32LoSec;
  203. psPt->u32Second = u32Tmp;
  204. }
  205. else
  206. {
  207. u32Tmp = (g_u32HiHour * 10ul);
  208. u32Tmp += g_u32LoHour;
  209. psPt->u32Hour = u32Tmp;
  210. u32Tmp = (g_u32HiMin * 10ul);
  211. u32Tmp += g_u32LoMin;
  212. psPt->u32Minute = u32Tmp;
  213. u32Tmp = (g_u32HiSec * 10ul);
  214. u32Tmp += g_u32LoSec;
  215. psPt->u32Second = u32Tmp;
  216. }
  217. }
  218. /**
  219. * @brief Get RTC Alarm Date and Time
  220. *
  221. * @param[out] psPt The returned pointer is specified the RTC alarm value. It includes: \n
  222. * u32Year: Year value \n
  223. * u32Month: Month value \n
  224. * u32Day: Day value \n
  225. * u32DayOfWeek: Day of week \n
  226. * u32Hour: Hour value \n
  227. * u32Minute: Minute value \n
  228. * u32Second: Second value \n
  229. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  230. * u8AmPm: [RTC_AM / RTC_PM] \n
  231. *
  232. * @return None
  233. *
  234. * @details This API is used to get the RTC alarm date and time setting.
  235. */
  236. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *psPt)
  237. {
  238. uint32_t u32Tmp;
  239. psPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  240. psPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  241. /* Get alarm [Date digit] data */
  242. g_u32HiYear = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
  243. g_u32LoYear = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
  244. g_u32HiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
  245. g_u32LoMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
  246. g_u32HiDay = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
  247. g_u32LoDay = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
  248. /* Get alarm [Time digit] data */
  249. g_u32HiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
  250. g_u32LoHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
  251. g_u32HiMin = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
  252. g_u32LoMin = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
  253. g_u32HiSec = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
  254. g_u32LoSec = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
  255. /* Compute to 20XX year */
  256. u32Tmp = (g_u32HiYear * 10ul);
  257. u32Tmp += g_u32LoYear;
  258. psPt->u32Year = u32Tmp + RTC_YEAR2000;
  259. /* Compute 0~12 month */
  260. u32Tmp = (g_u32HiMonth * 10ul);
  261. psPt->u32Month = u32Tmp + g_u32LoMonth;
  262. /* Compute 0~31 day */
  263. u32Tmp = (g_u32HiDay * 10ul);
  264. psPt->u32Day = u32Tmp + g_u32LoDay;
  265. /* Compute 12/24 hour */
  266. if (psPt->u32TimeScale == RTC_CLOCK_12)
  267. {
  268. u32Tmp = (g_u32HiHour * 10ul);
  269. u32Tmp += g_u32LoHour;
  270. psPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  271. if (psPt->u32Hour >= 21ul)
  272. {
  273. psPt->u32AmPm = RTC_PM;
  274. psPt->u32Hour -= 20ul;
  275. }
  276. else
  277. {
  278. psPt->u32AmPm = RTC_AM;
  279. }
  280. u32Tmp = (g_u32HiMin * 10ul);
  281. u32Tmp += g_u32LoMin;
  282. psPt->u32Minute = u32Tmp;
  283. u32Tmp = (g_u32HiSec * 10ul);
  284. u32Tmp += g_u32LoSec;
  285. psPt->u32Second = u32Tmp;
  286. }
  287. else
  288. {
  289. u32Tmp = (g_u32HiHour * 10ul);
  290. u32Tmp += g_u32LoHour;
  291. psPt->u32Hour = u32Tmp;
  292. u32Tmp = (g_u32HiMin * 10ul);
  293. u32Tmp += g_u32LoMin;
  294. psPt->u32Minute = u32Tmp;
  295. u32Tmp = (g_u32HiSec * 10ul);
  296. u32Tmp += g_u32LoSec;
  297. psPt->u32Second = u32Tmp;
  298. }
  299. }
  300. /**
  301. * @brief Update Current RTC Date and Time
  302. *
  303. * @param[in] psPt Specify the time property and current date and time. It includes: \n
  304. * u32Year: Year value, range between 2000 ~ 2099. \n
  305. * u32Month: Month value, range between 1 ~ 12. \n
  306. * u32Day: Day value, range between 1 ~ 31. \n
  307. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  308. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  309. * RTC_SATURDAY] \n
  310. * u32Hour: Hour value, range between 0 ~ 23. \n
  311. * u32Minute: Minute value, range between 0 ~ 59. \n
  312. * u32Second: Second value, range between 0 ~ 59. \n
  313. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  314. * u8AmPm: [RTC_AM / RTC_PM] \n
  315. *
  316. * @return None
  317. *
  318. * @details This API is used to update current date and time to RTC.
  319. */
  320. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *psPt)
  321. {
  322. uint32_t u32RegCAL, u32RegTIME;
  323. if (psPt == NULL)
  324. {
  325. /* No RTC date/time data */
  326. }
  327. else
  328. {
  329. /*-----------------------------------------------------------------------------------------------------*/
  330. /* Set RTC 24/12 hour setting and Day of the Week */
  331. /*-----------------------------------------------------------------------------------------------------*/
  332. if (psPt->u32TimeScale == RTC_CLOCK_12)
  333. {
  334. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  335. /*-------------------------------------------------------------------------------------------------*/
  336. /* Important, range of 12-hour PM mode is 21 up to 32 */
  337. /*-------------------------------------------------------------------------------------------------*/
  338. if (psPt->u32AmPm == RTC_PM)
  339. {
  340. psPt->u32Hour += 20ul;
  341. }
  342. }
  343. else
  344. {
  345. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  346. }
  347. /* Set Day of the Week */
  348. RTC->WEEKDAY = psPt->u32DayOfWeek;
  349. /*-----------------------------------------------------------------------------------------------------*/
  350. /* Set RTC Current Date and Time */
  351. /*-----------------------------------------------------------------------------------------------------*/
  352. u32RegCAL = ((psPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  353. u32RegCAL |= (((psPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  354. u32RegCAL |= ((psPt->u32Month / 10ul) << 12);
  355. u32RegCAL |= ((psPt->u32Month % 10ul) << 8);
  356. u32RegCAL |= ((psPt->u32Day / 10ul) << 4);
  357. u32RegCAL |= (psPt->u32Day % 10ul);
  358. u32RegTIME = ((psPt->u32Hour / 10ul) << 20);
  359. u32RegTIME |= ((psPt->u32Hour % 10ul) << 16);
  360. u32RegTIME |= ((psPt->u32Minute / 10ul) << 12);
  361. u32RegTIME |= ((psPt->u32Minute % 10ul) << 8);
  362. u32RegTIME |= ((psPt->u32Second / 10ul) << 4);
  363. u32RegTIME |= (psPt->u32Second % 10ul);
  364. /*-----------------------------------------------------------------------------------------------------*/
  365. /* Set RTC Calender and Time Loading */
  366. /*-----------------------------------------------------------------------------------------------------*/
  367. RTC->CAL = (uint32_t)u32RegCAL;
  368. RTC->TIME = (uint32_t)u32RegTIME;
  369. }
  370. }
  371. /**
  372. * @brief Update RTC Alarm Date and Time
  373. *
  374. * @param[in] psPt Specify the time property and alarm date and time. It includes: \n
  375. * u32Year: Year value, range between 2000 ~ 2099. \n
  376. * u32Month: Month value, range between 1 ~ 12. \n
  377. * u32Day: Day value, range between 1 ~ 31. \n
  378. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  379. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  380. * RTC_SATURDAY] \n
  381. * u32Hour: Hour value, range between 0 ~ 23. \n
  382. * u32Minute: Minute value, range between 0 ~ 59. \n
  383. * u32Second: Second value, range between 0 ~ 59. \n
  384. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  385. * u8AmPm: [RTC_AM / RTC_PM] \n
  386. *
  387. * @return None
  388. *
  389. * @details This API is used to update alarm date and time setting to RTC.
  390. */
  391. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *psPt)
  392. {
  393. uint32_t u32RegCALM, u32RegTALM;
  394. if (psPt == NULL)
  395. {
  396. /* No RTC date/time data */
  397. }
  398. else
  399. {
  400. /*-----------------------------------------------------------------------------------------------------*/
  401. /* Set RTC 24/12 hour setting and Day of the Week */
  402. /*-----------------------------------------------------------------------------------------------------*/
  403. if (psPt->u32TimeScale == RTC_CLOCK_12)
  404. {
  405. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  406. /*-------------------------------------------------------------------------------------------------*/
  407. /* Important, range of 12-hour PM mode is 21 up to 32 */
  408. /*-------------------------------------------------------------------------------------------------*/
  409. if (psPt->u32AmPm == RTC_PM)
  410. {
  411. psPt->u32Hour += 20ul;
  412. }
  413. }
  414. else
  415. {
  416. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  417. }
  418. /*-----------------------------------------------------------------------------------------------------*/
  419. /* Set RTC Alarm Date and Time */
  420. /*-----------------------------------------------------------------------------------------------------*/
  421. u32RegCALM = ((psPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  422. u32RegCALM |= (((psPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  423. u32RegCALM |= ((psPt->u32Month / 10ul) << 12);
  424. u32RegCALM |= ((psPt->u32Month % 10ul) << 8);
  425. u32RegCALM |= ((psPt->u32Day / 10ul) << 4);
  426. u32RegCALM |= (psPt->u32Day % 10ul);
  427. u32RegTALM = ((psPt->u32Hour / 10ul) << 20);
  428. u32RegTALM |= ((psPt->u32Hour % 10ul) << 16);
  429. u32RegTALM |= ((psPt->u32Minute / 10ul) << 12);
  430. u32RegTALM |= ((psPt->u32Minute % 10ul) << 8);
  431. u32RegTALM |= ((psPt->u32Second / 10ul) << 4);
  432. u32RegTALM |= (psPt->u32Second % 10ul);
  433. RTC->CALM = (uint32_t)u32RegCALM;
  434. RTC->TALM = (uint32_t)u32RegTALM;
  435. }
  436. }
  437. /**
  438. * @brief Update RTC Current Date
  439. *
  440. * @param[in] u32Year The year calendar digit of current RTC setting.
  441. * @param[in] u32Month The month calendar digit of current RTC setting.
  442. * @param[in] u32Day The day calendar digit of current RTC setting.
  443. * @param[in] u32DayOfWeek The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  444. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  445. * RTC_SATURDAY]
  446. *
  447. * @return None
  448. *
  449. * @details This API is used to update current date to RTC.
  450. */
  451. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
  452. {
  453. uint32_t u32RegCAL;
  454. u32RegCAL = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  455. u32RegCAL |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  456. u32RegCAL |= ((u32Month / 10ul) << 12);
  457. u32RegCAL |= ((u32Month % 10ul) << 8);
  458. u32RegCAL |= ((u32Day / 10ul) << 4);
  459. u32RegCAL |= (u32Day % 10ul);
  460. /* Set Day of the Week */
  461. RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
  462. /* Set RTC Calender Loading */
  463. RTC->CAL = (uint32_t)u32RegCAL;
  464. }
  465. /**
  466. * @brief Update RTC Current Time
  467. *
  468. * @param[in] u32Hour The hour time digit of current RTC setting.
  469. * @param[in] u32Minute The minute time digit of current RTC setting.
  470. * @param[in] u32Second The second time digit of current RTC setting.
  471. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  472. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  473. *
  474. * @return None
  475. *
  476. * @details This API is used to update current time to RTC.
  477. */
  478. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  479. {
  480. uint32_t u32RegTIME;
  481. /* Important, range of 12-hour PM mode is 21 up to 32 */
  482. if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
  483. {
  484. u32Hour += 20ul;
  485. }
  486. u32RegTIME = ((u32Hour / 10ul) << 20);
  487. u32RegTIME |= ((u32Hour % 10ul) << 16);
  488. u32RegTIME |= ((u32Minute / 10ul) << 12);
  489. u32RegTIME |= ((u32Minute % 10ul) << 8);
  490. u32RegTIME |= ((u32Second / 10ul) << 4);
  491. u32RegTIME |= (u32Second % 10ul);
  492. /*-----------------------------------------------------------------------------------------------------*/
  493. /* Set RTC 24/12 hour setting and Day of the Week */
  494. /*-----------------------------------------------------------------------------------------------------*/
  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->TIME = (uint32_t)u32RegTIME;
  504. }
  505. /**
  506. * @brief Update RTC Alarm Date
  507. *
  508. * @param[in] u32Year The year calendar digit of RTC alarm setting.
  509. * @param[in] u32Month The month calendar digit of RTC alarm setting.
  510. * @param[in] u32Day The day calendar digit of RTC alarm setting.
  511. *
  512. * @return None
  513. *
  514. * @details This API is used to update alarm date setting to RTC.
  515. */
  516. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
  517. {
  518. uint32_t u32RegCALM;
  519. u32RegCALM = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  520. u32RegCALM |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  521. u32RegCALM |= ((u32Month / 10ul) << 12);
  522. u32RegCALM |= ((u32Month % 10ul) << 8);
  523. u32RegCALM |= ((u32Day / 10ul) << 4);
  524. u32RegCALM |= (u32Day % 10ul);
  525. /* Set RTC Alarm Date */
  526. RTC->CALM = (uint32_t)u32RegCALM;
  527. }
  528. /**
  529. * @brief Update RTC Alarm Time
  530. *
  531. * @param[in] u32Hour The hour time digit of RTC alarm setting.
  532. * @param[in] u32Minute The minute time digit of RTC alarm setting.
  533. * @param[in] u32Second The second time digit of RTC alarm setting.
  534. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  535. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  536. *
  537. * @return None
  538. *
  539. * @details This API is used to update alarm time setting to RTC.
  540. */
  541. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  542. {
  543. uint32_t u32RegTALM;
  544. /* Important, range of 12-hour PM mode is 21 up to 32 */
  545. if((u32TimeMode == (uint32_t)RTC_CLOCK_12) && (u32AmPm == (uint32_t)RTC_PM))
  546. {
  547. u32Hour += 20ul;
  548. }
  549. u32RegTALM = ((u32Hour / 10ul) << 20);
  550. u32RegTALM |= ((u32Hour % 10ul) << 16);
  551. u32RegTALM |= ((u32Minute / 10ul) << 12);
  552. u32RegTALM |= ((u32Minute % 10ul) << 8);
  553. u32RegTALM |= ((u32Second / 10ul) << 4);
  554. u32RegTALM |= (u32Second % 10ul);
  555. /*-----------------------------------------------------------------------------------------------------*/
  556. /* Set RTC 24/12 hour setting and Day of the Week */
  557. /*-----------------------------------------------------------------------------------------------------*/
  558. if(u32TimeMode == (uint32_t)RTC_CLOCK_12)
  559. {
  560. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  561. }
  562. else
  563. {
  564. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  565. }
  566. /* Set RTC Alarm Time */
  567. RTC->TALM = (uint32_t)u32RegTALM;
  568. }
  569. /**
  570. * @brief Set RTC Alarm Date Mask Function
  571. *
  572. * @param[in] u8IsTenYMsk 1: enable 10-Year digit alarm mask; 0: disabled.
  573. * @param[in] u8IsYMsk 1: enable 1-Year digit alarm mask; 0: disabled.
  574. * @param[in] u8IsTenMMsk 1: enable 10-Mon digit alarm mask; 0: disabled.
  575. * @param[in] u8IsMMsk 1: enable 1-Mon digit alarm mask; 0: disabled.
  576. * @param[in] u8IsTenDMsk 1: enable 10-Day digit alarm mask; 0: disabled.
  577. * @param[in] u8IsDMsk 1: enable 1-Day digit alarm mask; 0: disabled.
  578. *
  579. * @return None
  580. *
  581. * @details This API is used to enable or disable RTC alarm date mask function.
  582. */
  583. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk)
  584. {
  585. RTC->CAMSK = ((uint32_t)u8IsTenYMsk << RTC_CAMSK_MTENYEAR_Pos) |
  586. ((uint32_t)u8IsYMsk << RTC_CAMSK_MYEAR_Pos) |
  587. ((uint32_t)u8IsTenMMsk << RTC_CAMSK_MTENMON_Pos) |
  588. ((uint32_t)u8IsMMsk << RTC_CAMSK_MMON_Pos) |
  589. ((uint32_t)u8IsTenDMsk << RTC_CAMSK_MTENDAY_Pos) |
  590. ((uint32_t)u8IsDMsk << RTC_CAMSK_MDAY_Pos);
  591. }
  592. /**
  593. * @brief Set RTC Alarm Time Mask Function
  594. *
  595. * @param[in] u8IsTenHMsk 1: enable 10-Hour digit alarm mask; 0: disabled.
  596. * @param[in] u8IsHMsk 1: enable 1-Hour digit alarm mask; 0: disabled.
  597. * @param[in] u8IsTenMMsk 1: enable 10-Min digit alarm mask; 0: disabled.
  598. * @param[in] u8IsMMsk 1: enable 1-Min digit alarm mask; 0: disabled.
  599. * @param[in] u8IsTenSMsk 1: enable 10-Sec digit alarm mask; 0: disabled.
  600. * @param[in] u8IsSMsk 1: enable 1-Sec digit alarm mask; 0: disabled.
  601. *
  602. * @return None
  603. *
  604. * @details This API is used to enable or disable RTC alarm time mask function.
  605. */
  606. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk)
  607. {
  608. RTC->TAMSK = ((uint32_t)u8IsTenHMsk << RTC_TAMSK_MTENHR_Pos) |
  609. ((uint32_t)u8IsHMsk << RTC_TAMSK_MHR_Pos) |
  610. ((uint32_t)u8IsTenMMsk << RTC_TAMSK_MTENMIN_Pos) |
  611. ((uint32_t)u8IsMMsk << RTC_TAMSK_MMIN_Pos) |
  612. ((uint32_t)u8IsTenSMsk << RTC_TAMSK_MTENSEC_Pos) |
  613. ((uint32_t)u8IsSMsk << RTC_TAMSK_MSEC_Pos);
  614. }
  615. /**
  616. * @brief Get Day of the Week
  617. *
  618. * @param None
  619. *
  620. * @retval 0 Sunday
  621. * @retval 1 Monday
  622. * @retval 2 Tuesday
  623. * @retval 3 Wednesday
  624. * @retval 4 Thursday
  625. * @retval 5 Friday
  626. * @retval 6 Saturday
  627. *
  628. * @details This API is used to get day of the week of current RTC date.
  629. */
  630. uint32_t RTC_GetDayOfWeek(void)
  631. {
  632. return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
  633. }
  634. /**
  635. * @brief Set RTC Tick Period Time
  636. *
  637. * @param[in] u32TickSelection It is used to set the RTC tick period time for Periodic Time Tick request. \n
  638. * It consists of:
  639. * - \ref RTC_TICK_1_SEC : Time tick is 1 second
  640. * - \ref RTC_TICK_1_2_SEC : Time tick is 1/2 second
  641. * - \ref RTC_TICK_1_4_SEC : Time tick is 1/4 second
  642. * - \ref RTC_TICK_1_8_SEC : Time tick is 1/8 second
  643. * - \ref RTC_TICK_1_16_SEC : Time tick is 1/16 second
  644. * - \ref RTC_TICK_1_32_SEC : Time tick is 1/32 second
  645. * - \ref RTC_TICK_1_64_SEC : Time tick is 1/64 second
  646. * - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
  647. *
  648. * @return None
  649. *
  650. * @details This API is used to set RTC tick period time for each tick interrupt.
  651. */
  652. void RTC_SetTickPeriod(uint32_t u32TickSelection)
  653. {
  654. RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
  655. }
  656. /**
  657. * @brief Enable RTC Interrupt
  658. *
  659. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  660. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  661. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  662. *
  663. * @return None
  664. *
  665. * @details This API is used to enable the specify RTC interrupt function.
  666. */
  667. void RTC_EnableInt(uint32_t u32IntFlagMask)
  668. {
  669. RTC->INTEN |= u32IntFlagMask;
  670. }
  671. /**
  672. * @brief Disable RTC Interrupt
  673. *
  674. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  675. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  676. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  677. *
  678. * @return None
  679. *
  680. * @details This API is used to disable the specify RTC interrupt function.
  681. */
  682. void RTC_DisableInt(uint32_t u32IntFlagMask)
  683. {
  684. RTC->INTEN &= ~u32IntFlagMask;
  685. RTC->INTSTS = u32IntFlagMask;
  686. }
  687. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  688. /*@}*/ /* end of group RTC_Driver */
  689. /*@}*/ /* end of group Standard_Driver */
  690. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/