nu_rtc.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /**************************************************************************//**
  2. * @file rtc.c
  3. * @version V3.00
  4. * @brief M480 series RTC driver source file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "NuMicro.h"
  10. /** @cond HIDDEN_SYMBOLS */
  11. /*---------------------------------------------------------------------------------------------------------*/
  12. /* Macro, type and constant definitions */
  13. /*---------------------------------------------------------------------------------------------------------*/
  14. #define RTC_GLOBALS
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global file scope (static) variables */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. static volatile uint32_t g_u32hiYear, g_u32loYear, g_u32hiMonth, g_u32loMonth, g_u32hiDay, g_u32loDay;
  19. static volatile uint32_t g_u32hiHour, g_u32loHour, g_u32hiMin, g_u32loMin, g_u32hiSec, g_u32loSec;
  20. /** @endcond HIDDEN_SYMBOLS */
  21. /** @addtogroup Standard_Driver Standard Driver
  22. @{
  23. */
  24. /** @addtogroup RTC_Driver RTC Driver
  25. @{
  26. */
  27. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  28. @{
  29. */
  30. /**
  31. * @brief Initialize RTC module and start counting
  32. *
  33. * @param[in] sPt Specify the time property and current date and time. It includes: \n
  34. * u32Year: Year value, range between 2000 ~ 2099. \n
  35. * u32Month: Month value, range between 1 ~ 12. \n
  36. * u32Day: Day value, range between 1 ~ 31. \n
  37. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  38. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  39. * RTC_SATURDAY] \n
  40. * u32Hour: Hour value, range between 0 ~ 23. \n
  41. * u32Minute: Minute value, range between 0 ~ 59. \n
  42. * u32Second: Second value, range between 0 ~ 59. \n
  43. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  44. * u8AmPm: [RTC_AM / RTC_PM] \n
  45. *
  46. * @return None
  47. *
  48. * @details This function is used to: \n
  49. * 1. Write initial key to let RTC start count. \n
  50. * 2. Input parameter indicates start date/time. \n
  51. * 3. User has to make sure that parameters of RTC date/time are reasonable. \n
  52. * @note Null pointer for using default starting date/time.
  53. */
  54. void RTC_Open(S_RTC_TIME_DATA_T *sPt)
  55. {
  56. RTC->INIT = RTC_INIT_KEY;
  57. if (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  58. {
  59. RTC->INIT = RTC_INIT_KEY;
  60. while (RTC->INIT != RTC_INIT_ACTIVE_Msk)
  61. {
  62. }
  63. }
  64. if (sPt == 0)
  65. {
  66. }
  67. else
  68. {
  69. /* Set RTC date and time */
  70. RTC_SetDateAndTime(sPt);
  71. }
  72. }
  73. /**
  74. * @brief Disable RTC Clock
  75. *
  76. * @param None
  77. *
  78. * @return None
  79. *
  80. * @details This API will disable RTC peripheral clock and stops RTC counting.
  81. */
  82. void RTC_Close(void)
  83. {
  84. CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
  85. }
  86. /**
  87. * @brief Set Frequency Compensation Data
  88. *
  89. * @param[in] i32FrequencyX10000 Specify the RTC clock X10000, ex: 327736512 means 32773.6512.
  90. *
  91. * @return None
  92. *
  93. */
  94. void RTC_32KCalibration(int32_t i32FrequencyX10000)
  95. {
  96. uint64_t u64Compensate;
  97. int32_t i32RegInt, i32RegFra ;
  98. if (!(SYS->CSERVER & 0x1))
  99. {
  100. u64Compensate = (uint64_t)(0x2710000000000);
  101. u64Compensate = (uint64_t)(u64Compensate / (uint64_t)i32FrequencyX10000);
  102. if (u64Compensate >= (uint64_t)0x400000)
  103. {
  104. u64Compensate = (uint64_t)0x3FFFFF;
  105. }
  106. RTC_WaitAccessEnable();
  107. RTC->FREQADJ = (uint32_t)u64Compensate;
  108. }
  109. else
  110. {
  111. /* Compute Integer and Fraction for RTC register*/
  112. i32RegInt = (i32FrequencyX10000 / 10000) - 32752;
  113. i32RegFra = ((((i32FrequencyX10000 % 10000)) * 64) + 5000) / 10000;
  114. if (i32RegFra >= 0x40)
  115. {
  116. i32RegFra = 0x0;
  117. i32RegInt++;
  118. }
  119. /* Judge Integer part is reasonable */
  120. if ((i32RegInt < 0) | (i32RegInt > 31))
  121. {
  122. return;
  123. }
  124. RTC_WaitAccessEnable();
  125. RTC->FREQADJ = (uint32_t)((i32RegInt << 8) | i32RegFra);
  126. }
  127. }
  128. /**
  129. * @brief Get Current RTC Date and Time
  130. *
  131. * @param[out] sPt The returned pointer is specified the current RTC value. It includes: \n
  132. * u32Year: Year value \n
  133. * u32Month: Month value \n
  134. * u32Day: Day value \n
  135. * u32DayOfWeek: Day of week \n
  136. * u32Hour: Hour value \n
  137. * u32Minute: Minute value \n
  138. * u32Second: Second value \n
  139. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  140. * u8AmPm: [RTC_AM / RTC_PM] \n
  141. *
  142. * @return None
  143. *
  144. * @details This API is used to get the current RTC date and time value.
  145. */
  146. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
  147. {
  148. uint32_t u32Tmp;
  149. sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  150. sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  151. /* Get [Date digit] data */
  152. g_u32hiYear = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
  153. g_u32loYear = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
  154. g_u32hiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
  155. g_u32loMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
  156. g_u32hiDay = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
  157. g_u32loDay = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
  158. /* Get [Time digit] data */
  159. g_u32hiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
  160. g_u32loHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
  161. g_u32hiMin = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
  162. g_u32loMin = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
  163. g_u32hiSec = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
  164. g_u32loSec = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
  165. /* Compute to 20XX year */
  166. u32Tmp = (g_u32hiYear * 10ul);
  167. u32Tmp += g_u32loYear;
  168. sPt->u32Year = u32Tmp + RTC_YEAR2000;
  169. /* Compute 0~12 month */
  170. u32Tmp = (g_u32hiMonth * 10ul);
  171. sPt->u32Month = u32Tmp + g_u32loMonth;
  172. /* Compute 0~31 day */
  173. u32Tmp = (g_u32hiDay * 10ul);
  174. sPt->u32Day = u32Tmp + g_u32loDay;
  175. /* Compute 12/24 hour */
  176. if (sPt->u32TimeScale == RTC_CLOCK_12)
  177. {
  178. u32Tmp = (g_u32hiHour * 10ul);
  179. u32Tmp += g_u32loHour;
  180. sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  181. if (sPt->u32Hour >= 21ul)
  182. {
  183. sPt->u32AmPm = RTC_PM;
  184. sPt->u32Hour -= 20ul;
  185. }
  186. else
  187. {
  188. sPt->u32AmPm = RTC_AM;
  189. }
  190. u32Tmp = (g_u32hiMin * 10ul);
  191. u32Tmp += g_u32loMin;
  192. sPt->u32Minute = u32Tmp;
  193. u32Tmp = (g_u32hiSec * 10ul);
  194. u32Tmp += g_u32loSec;
  195. sPt->u32Second = u32Tmp;
  196. }
  197. else
  198. {
  199. u32Tmp = (g_u32hiHour * 10ul);
  200. u32Tmp += g_u32loHour;
  201. sPt->u32Hour = u32Tmp;
  202. u32Tmp = (g_u32hiMin * 10ul);
  203. u32Tmp += g_u32loMin;
  204. sPt->u32Minute = u32Tmp;
  205. u32Tmp = (g_u32hiSec * 10ul);
  206. u32Tmp += g_u32loSec;
  207. sPt->u32Second = u32Tmp;
  208. }
  209. }
  210. /**
  211. * @brief Get RTC Alarm Date and Time
  212. *
  213. * @param[out] sPt The returned pointer is specified the RTC alarm value. It includes: \n
  214. * u32Year: Year value \n
  215. * u32Month: Month value \n
  216. * u32Day: Day value \n
  217. * u32DayOfWeek: Day of week \n
  218. * u32Hour: Hour value \n
  219. * u32Minute: Minute value \n
  220. * u32Second: Second value \n
  221. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  222. * u8AmPm: [RTC_AM / RTC_PM] \n
  223. *
  224. * @return None
  225. *
  226. * @details This API is used to get the RTC alarm date and time setting.
  227. */
  228. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
  229. {
  230. uint32_t u32Tmp;
  231. sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
  232. sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
  233. /* Get alarm [Date digit] data */
  234. RTC_WaitAccessEnable();
  235. g_u32hiYear = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
  236. g_u32loYear = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
  237. g_u32hiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
  238. g_u32loMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
  239. g_u32hiDay = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
  240. g_u32loDay = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
  241. /* Get alarm [Time digit] data */
  242. RTC_WaitAccessEnable();
  243. g_u32hiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
  244. g_u32loHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
  245. g_u32hiMin = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
  246. g_u32loMin = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
  247. g_u32hiSec = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
  248. g_u32loSec = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
  249. /* Compute to 20XX year */
  250. u32Tmp = (g_u32hiYear * 10ul);
  251. u32Tmp += g_u32loYear;
  252. sPt->u32Year = u32Tmp + RTC_YEAR2000;
  253. /* Compute 0~12 month */
  254. u32Tmp = (g_u32hiMonth * 10ul);
  255. sPt->u32Month = u32Tmp + g_u32loMonth;
  256. /* Compute 0~31 day */
  257. u32Tmp = (g_u32hiDay * 10ul);
  258. sPt->u32Day = u32Tmp + g_u32loDay;
  259. /* Compute 12/24 hour */
  260. if (sPt->u32TimeScale == RTC_CLOCK_12)
  261. {
  262. u32Tmp = (g_u32hiHour * 10ul);
  263. u32Tmp += g_u32loHour;
  264. sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
  265. if (sPt->u32Hour >= 21ul)
  266. {
  267. sPt->u32AmPm = RTC_PM;
  268. sPt->u32Hour -= 20ul;
  269. }
  270. else
  271. {
  272. sPt->u32AmPm = RTC_AM;
  273. }
  274. u32Tmp = (g_u32hiMin * 10ul);
  275. u32Tmp += g_u32loMin;
  276. sPt->u32Minute = u32Tmp;
  277. u32Tmp = (g_u32hiSec * 10ul);
  278. u32Tmp += g_u32loSec;
  279. sPt->u32Second = u32Tmp;
  280. }
  281. else
  282. {
  283. u32Tmp = (g_u32hiHour * 10ul);
  284. u32Tmp += g_u32loHour;
  285. sPt->u32Hour = u32Tmp;
  286. u32Tmp = (g_u32hiMin * 10ul);
  287. u32Tmp += g_u32loMin;
  288. sPt->u32Minute = u32Tmp;
  289. u32Tmp = (g_u32hiSec * 10ul);
  290. u32Tmp += g_u32loSec;
  291. sPt->u32Second = u32Tmp;
  292. }
  293. }
  294. /**
  295. * @brief Update Current RTC Date and Time
  296. *
  297. * @param[in] sPt Specify the time property and current date and time. It includes: \n
  298. * u32Year: Year value, range between 2000 ~ 2099. \n
  299. * u32Month: Month value, range between 1 ~ 12. \n
  300. * u32Day: Day value, range between 1 ~ 31. \n
  301. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  302. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  303. * RTC_SATURDAY] \n
  304. * u32Hour: Hour value, range between 0 ~ 23. \n
  305. * u32Minute: Minute value, range between 0 ~ 59. \n
  306. * u32Second: Second value, range between 0 ~ 59. \n
  307. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  308. * u8AmPm: [RTC_AM / RTC_PM] \n
  309. *
  310. * @return None
  311. *
  312. * @details This API is used to update current date and time to RTC.
  313. */
  314. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
  315. {
  316. uint32_t u32RegCAL, u32RegTIME;
  317. if (sPt == NULL)
  318. {
  319. }
  320. else
  321. {
  322. /*-----------------------------------------------------------------------------------------------------*/
  323. /* Set RTC 24/12 hour setting and Day of the Week */
  324. /*-----------------------------------------------------------------------------------------------------*/
  325. RTC_WaitAccessEnable();
  326. if (sPt->u32TimeScale == RTC_CLOCK_12)
  327. {
  328. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  329. /*-------------------------------------------------------------------------------------------------*/
  330. /* Important, range of 12-hour PM mode is 21 up to 32 */
  331. /*-------------------------------------------------------------------------------------------------*/
  332. if (sPt->u32AmPm == RTC_PM)
  333. {
  334. sPt->u32Hour += 20ul;
  335. }
  336. }
  337. else
  338. {
  339. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  340. }
  341. /* Set Day of the Week */
  342. RTC_WaitAccessEnable();
  343. RTC->WEEKDAY = sPt->u32DayOfWeek;
  344. /*-----------------------------------------------------------------------------------------------------*/
  345. /* Set RTC Current Date and Time */
  346. /*-----------------------------------------------------------------------------------------------------*/
  347. u32RegCAL = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  348. u32RegCAL |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  349. u32RegCAL |= ((sPt->u32Month / 10ul) << 12);
  350. u32RegCAL |= ((sPt->u32Month % 10ul) << 8);
  351. u32RegCAL |= ((sPt->u32Day / 10ul) << 4);
  352. u32RegCAL |= (sPt->u32Day % 10ul);
  353. u32RegTIME = ((sPt->u32Hour / 10ul) << 20);
  354. u32RegTIME |= ((sPt->u32Hour % 10ul) << 16);
  355. u32RegTIME |= ((sPt->u32Minute / 10ul) << 12);
  356. u32RegTIME |= ((sPt->u32Minute % 10ul) << 8);
  357. u32RegTIME |= ((sPt->u32Second / 10ul) << 4);
  358. u32RegTIME |= (sPt->u32Second % 10ul);
  359. /*-----------------------------------------------------------------------------------------------------*/
  360. /* Set RTC Calender and Time Loading */
  361. /*-----------------------------------------------------------------------------------------------------*/
  362. RTC_WaitAccessEnable();
  363. RTC->CAL = (uint32_t)u32RegCAL;
  364. RTC_WaitAccessEnable();
  365. RTC->TIME = (uint32_t)u32RegTIME;
  366. }
  367. }
  368. /**
  369. * @brief Update RTC Alarm Date and Time
  370. *
  371. * @param[in] sPt Specify the time property and alarm date and time. It includes: \n
  372. * u32Year: Year value, range between 2000 ~ 2099. \n
  373. * u32Month: Month value, range between 1 ~ 12. \n
  374. * u32Day: Day value, range between 1 ~ 31. \n
  375. * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  376. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  377. * RTC_SATURDAY] \n
  378. * u32Hour: Hour value, range between 0 ~ 23. \n
  379. * u32Minute: Minute value, range between 0 ~ 59. \n
  380. * u32Second: Second value, range between 0 ~ 59. \n
  381. * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
  382. * u8AmPm: [RTC_AM / RTC_PM] \n
  383. *
  384. * @return None
  385. *
  386. * @details This API is used to update alarm date and time setting to RTC.
  387. */
  388. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
  389. {
  390. uint32_t u32RegCALM, u32RegTALM;
  391. if (sPt == NULL)
  392. {
  393. }
  394. else
  395. {
  396. /*-----------------------------------------------------------------------------------------------------*/
  397. /* Set RTC 24/12 hour setting and Day of the Week */
  398. /*-----------------------------------------------------------------------------------------------------*/
  399. RTC_WaitAccessEnable();
  400. if (sPt->u32TimeScale == RTC_CLOCK_12)
  401. {
  402. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  403. /*-------------------------------------------------------------------------------------------------*/
  404. /* Important, range of 12-hour PM mode is 21 up to 32 */
  405. /*-------------------------------------------------------------------------------------------------*/
  406. if (sPt->u32AmPm == RTC_PM)
  407. {
  408. sPt->u32Hour += 20ul;
  409. }
  410. }
  411. else
  412. {
  413. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  414. }
  415. /*-----------------------------------------------------------------------------------------------------*/
  416. /* Set RTC Alarm Date and Time */
  417. /*-----------------------------------------------------------------------------------------------------*/
  418. u32RegCALM = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
  419. u32RegCALM |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
  420. u32RegCALM |= ((sPt->u32Month / 10ul) << 12);
  421. u32RegCALM |= ((sPt->u32Month % 10ul) << 8);
  422. u32RegCALM |= ((sPt->u32Day / 10ul) << 4);
  423. u32RegCALM |= (sPt->u32Day % 10ul);
  424. u32RegTALM = ((sPt->u32Hour / 10ul) << 20);
  425. u32RegTALM |= ((sPt->u32Hour % 10ul) << 16);
  426. u32RegTALM |= ((sPt->u32Minute / 10ul) << 12);
  427. u32RegTALM |= ((sPt->u32Minute % 10ul) << 8);
  428. u32RegTALM |= ((sPt->u32Second / 10ul) << 4);
  429. u32RegTALM |= (sPt->u32Second % 10ul);
  430. RTC_WaitAccessEnable();
  431. RTC->CALM = (uint32_t)u32RegCALM;
  432. RTC_WaitAccessEnable();
  433. RTC->TALM = (uint32_t)u32RegTALM;
  434. }
  435. }
  436. /**
  437. * @brief Update RTC Current Date
  438. *
  439. * @param[in] u32Year The year calendar digit of current RTC setting.
  440. * @param[in] u32Month The month calendar digit of current RTC setting.
  441. * @param[in] u32Day The day calendar digit of current RTC setting.
  442. * @param[in] u32DayOfWeek The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
  443. * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
  444. * RTC_SATURDAY]
  445. *
  446. * @return None
  447. *
  448. * @details This API is used to update current date to RTC.
  449. */
  450. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
  451. {
  452. uint32_t u32RegCAL;
  453. u32RegCAL = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  454. u32RegCAL |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  455. u32RegCAL |= ((u32Month / 10ul) << 12);
  456. u32RegCAL |= ((u32Month % 10ul) << 8);
  457. u32RegCAL |= ((u32Day / 10ul) << 4);
  458. u32RegCAL |= (u32Day % 10ul);
  459. /* Set Day of the Week */
  460. RTC_WaitAccessEnable();
  461. RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
  462. /* Set RTC Calender Loading */
  463. RTC_WaitAccessEnable();
  464. RTC->CAL = (uint32_t)u32RegCAL;
  465. }
  466. /**
  467. * @brief Update RTC Current Time
  468. *
  469. * @param[in] u32Hour The hour time digit of current RTC setting.
  470. * @param[in] u32Minute The minute time digit of current RTC setting.
  471. * @param[in] u32Second The second time digit of current RTC setting.
  472. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  473. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  474. *
  475. * @return None
  476. *
  477. * @details This API is used to update current time to RTC.
  478. */
  479. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  480. {
  481. uint32_t u32RegTIME;
  482. /* Important, range of 12-hour PM mode is 21 up to 32 */
  483. if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
  484. {
  485. u32Hour += 20ul;
  486. }
  487. u32RegTIME = ((u32Hour / 10ul) << 20);
  488. u32RegTIME |= ((u32Hour % 10ul) << 16);
  489. u32RegTIME |= ((u32Minute / 10ul) << 12);
  490. u32RegTIME |= ((u32Minute % 10ul) << 8);
  491. u32RegTIME |= ((u32Second / 10ul) << 4);
  492. u32RegTIME |= (u32Second % 10ul);
  493. /*-----------------------------------------------------------------------------------------------------*/
  494. /* Set RTC 24/12 hour setting and Day of the Week */
  495. /*-----------------------------------------------------------------------------------------------------*/
  496. RTC_WaitAccessEnable();
  497. if (u32TimeMode == RTC_CLOCK_12)
  498. {
  499. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  500. }
  501. else
  502. {
  503. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  504. }
  505. RTC_WaitAccessEnable();
  506. RTC->TIME = (uint32_t)u32RegTIME;
  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. *
  515. * @return None
  516. *
  517. * @details This API is used to update alarm date setting to RTC.
  518. */
  519. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
  520. {
  521. uint32_t u32RegCALM;
  522. u32RegCALM = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
  523. u32RegCALM |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
  524. u32RegCALM |= ((u32Month / 10ul) << 12);
  525. u32RegCALM |= ((u32Month % 10ul) << 8);
  526. u32RegCALM |= ((u32Day / 10ul) << 4);
  527. u32RegCALM |= (u32Day % 10ul);
  528. RTC_WaitAccessEnable();
  529. /* Set RTC Alarm Date */
  530. RTC->CALM = (uint32_t)u32RegCALM;
  531. }
  532. /**
  533. * @brief Update RTC Alarm Time
  534. *
  535. * @param[in] u32Hour The hour time digit of RTC alarm setting.
  536. * @param[in] u32Minute The minute time digit of RTC alarm setting.
  537. * @param[in] u32Second The second time digit of RTC alarm setting.
  538. * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
  539. * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
  540. *
  541. * @return None
  542. *
  543. * @details This API is used to update alarm time setting to RTC.
  544. */
  545. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
  546. {
  547. uint32_t u32RegTALM;
  548. /* Important, range of 12-hour PM mode is 21 up to 32 */
  549. if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
  550. {
  551. u32Hour += 20ul;
  552. }
  553. u32RegTALM = ((u32Hour / 10ul) << 20);
  554. u32RegTALM |= ((u32Hour % 10ul) << 16);
  555. u32RegTALM |= ((u32Minute / 10ul) << 12);
  556. u32RegTALM |= ((u32Minute % 10ul) << 8);
  557. u32RegTALM |= ((u32Second / 10ul) << 4);
  558. u32RegTALM |= (u32Second % 10ul);
  559. /*-----------------------------------------------------------------------------------------------------*/
  560. /* Set RTC 24/12 hour setting and Day of the Week */
  561. /*-----------------------------------------------------------------------------------------------------*/
  562. RTC_WaitAccessEnable();
  563. if (u32TimeMode == RTC_CLOCK_12)
  564. {
  565. RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
  566. }
  567. else
  568. {
  569. RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
  570. }
  571. /* Set RTC Alarm Time */
  572. RTC_WaitAccessEnable();
  573. RTC->TALM = (uint32_t)u32RegTALM;
  574. }
  575. /**
  576. * @brief Set RTC Alarm Date Mask Function
  577. *
  578. * @param[in] u8IsTenYMsk 1: enable 10-Year digit alarm mask; 0: disabled.
  579. * @param[in] u8IsYMsk 1: enable 1-Year digit alarm mask; 0: disabled.
  580. * @param[in] u8IsTenMMsk 1: enable 10-Mon digit alarm mask; 0: disabled.
  581. * @param[in] u8IsMMsk 1: enable 1-Mon digit alarm mask; 0: disabled.
  582. * @param[in] u8IsTenDMsk 1: enable 10-Day digit alarm mask; 0: disabled.
  583. * @param[in] u8IsDMsk 1: enable 1-Day digit alarm mask; 0: disabled.
  584. *
  585. * @return None
  586. *
  587. * @details This API is used to enable or disable RTC alarm date mask function.
  588. */
  589. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk)
  590. {
  591. RTC_WaitAccessEnable();
  592. RTC->CAMSK = ((uint32_t)u8IsTenYMsk << RTC_CAMSK_MTENYEAR_Pos) |
  593. ((uint32_t)u8IsYMsk << RTC_CAMSK_MYEAR_Pos) |
  594. ((uint32_t)u8IsTenMMsk << RTC_CAMSK_MTENMON_Pos) |
  595. ((uint32_t)u8IsMMsk << RTC_CAMSK_MMON_Pos) |
  596. ((uint32_t)u8IsTenDMsk << RTC_CAMSK_MTENDAY_Pos) |
  597. ((uint32_t)u8IsDMsk << RTC_CAMSK_MDAY_Pos);
  598. }
  599. /**
  600. * @brief Set RTC Alarm Time Mask Function
  601. *
  602. * @param[in] u8IsTenHMsk 1: enable 10-Hour digit alarm mask; 0: disabled.
  603. * @param[in] u8IsHMsk 1: enable 1-Hour digit alarm mask; 0: disabled.
  604. * @param[in] u8IsTenMMsk 1: enable 10-Min digit alarm mask; 0: disabled.
  605. * @param[in] u8IsMMsk 1: enable 1-Min digit alarm mask; 0: disabled.
  606. * @param[in] u8IsTenSMsk 1: enable 10-Sec digit alarm mask; 0: disabled.
  607. * @param[in] u8IsSMsk 1: enable 1-Sec digit alarm mask; 0: disabled.
  608. *
  609. * @return None
  610. *
  611. * @details This API is used to enable or disable RTC alarm time mask function.
  612. */
  613. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk)
  614. {
  615. RTC_WaitAccessEnable();
  616. RTC->TAMSK = ((uint32_t)u8IsTenHMsk << RTC_TAMSK_MTENHR_Pos) |
  617. ((uint32_t)u8IsHMsk << RTC_TAMSK_MHR_Pos) |
  618. ((uint32_t)u8IsTenMMsk << RTC_TAMSK_MTENMIN_Pos) |
  619. ((uint32_t)u8IsMMsk << RTC_TAMSK_MMIN_Pos) |
  620. ((uint32_t)u8IsTenSMsk << RTC_TAMSK_MTENSEC_Pos) |
  621. ((uint32_t)u8IsSMsk << RTC_TAMSK_MSEC_Pos);
  622. }
  623. /**
  624. * @brief Get Day of the Week
  625. *
  626. * @param None
  627. *
  628. * @retval 0 Sunday
  629. * @retval 1 Monday
  630. * @retval 2 Tuesday
  631. * @retval 3 Wednesday
  632. * @retval 4 Thursday
  633. * @retval 5 Friday
  634. * @retval 6 Saturday
  635. *
  636. * @details This API is used to get day of the week of current RTC date.
  637. */
  638. uint32_t RTC_GetDayOfWeek(void)
  639. {
  640. return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
  641. }
  642. /**
  643. * @brief Set RTC Tick Period Time
  644. *
  645. * @param[in] u32TickSelection It is used to set the RTC tick period time for Periodic Time Tick request. \n
  646. * It consists of:
  647. * - \ref RTC_TICK_1_SEC : Time tick is 1 second
  648. * - \ref RTC_TICK_1_2_SEC : Time tick is 1/2 second
  649. * - \ref RTC_TICK_1_4_SEC : Time tick is 1/4 second
  650. * - \ref RTC_TICK_1_8_SEC : Time tick is 1/8 second
  651. * - \ref RTC_TICK_1_16_SEC : Time tick is 1/16 second
  652. * - \ref RTC_TICK_1_32_SEC : Time tick is 1/32 second
  653. * - \ref RTC_TICK_1_64_SEC : Time tick is 1/64 second
  654. * - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
  655. *
  656. * @return None
  657. *
  658. * @details This API is used to set RTC tick period time for each tick interrupt.
  659. */
  660. void RTC_SetTickPeriod(uint32_t u32TickSelection)
  661. {
  662. RTC_WaitAccessEnable();
  663. RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
  664. }
  665. /**
  666. * @brief Enable RTC Interrupt
  667. *
  668. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  669. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  670. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  671. * - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
  672. * - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
  673. * - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
  674. * - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
  675. * - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
  676. * - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
  677. *
  678. * @return None
  679. *
  680. * @details This API is used to enable the specify RTC interrupt function.
  681. */
  682. void RTC_EnableInt(uint32_t u32IntFlagMask)
  683. {
  684. RTC_WaitAccessEnable();
  685. RTC->INTEN |= u32IntFlagMask;
  686. }
  687. /**
  688. * @brief Disable RTC Interrupt
  689. *
  690. * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
  691. * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
  692. * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
  693. * - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
  694. * - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
  695. * - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
  696. * - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
  697. * - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
  698. * - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
  699. *
  700. * @return None
  701. *
  702. * @details This API is used to disable the specify RTC interrupt function.
  703. */
  704. void RTC_DisableInt(uint32_t u32IntFlagMask)
  705. {
  706. RTC_WaitAccessEnable();
  707. RTC->INTEN &= ~u32IntFlagMask;
  708. RTC_WaitAccessEnable();
  709. RTC->INTSTS = u32IntFlagMask;
  710. }
  711. /**
  712. * @brief Enable Spare Registers Access
  713. *
  714. * @param None
  715. *
  716. * @return None
  717. *
  718. * @details This API is used to enable the spare registers 0~19 can be accessed.
  719. */
  720. void RTC_EnableSpareAccess(void)
  721. {
  722. RTC_WaitAccessEnable();
  723. RTC->SPRCTL |= RTC_SPRCTL_SPRRWEN_Msk;
  724. }
  725. /**
  726. * @brief Disable Spare Register
  727. *
  728. * @param None
  729. *
  730. * @return None
  731. *
  732. * @details This API is used to disable the spare register 0~19 cannot be accessed.
  733. */
  734. void RTC_DisableSpareRegister(void)
  735. {
  736. RTC_WaitAccessEnable();
  737. RTC->SPRCTL &= ~RTC_SPRCTL_SPRRWEN_Msk;
  738. }
  739. /**
  740. * @brief Static Tamper Detect
  741. *
  742. * @param[in] u32TamperSelect Tamper pin select. Possible options are
  743. * - \ref RTC_TAMPER5_SELECT
  744. * - \ref RTC_TAMPER4_SELECT
  745. * - \ref RTC_TAMPER3_SELECT
  746. * - \ref RTC_TAMPER2_SELECT
  747. * - \ref RTC_TAMPER1_SELECT
  748. * - \ref RTC_TAMPER0_SELECT
  749. *
  750. * @param[in] u32DetecLevel Tamper pin detection level select. Possible options are
  751. * - \ref RTC_TAMPER_HIGH_LEVEL_DETECT
  752. * - \ref RTC_TAMPER_LOW_LEVEL_DETECT
  753. *
  754. * @param[in] u32DebounceEn Tamper pin de-bounce enable
  755. * - \ref RTC_TAMPER_DEBOUNCE_ENABLE
  756. * - \ref RTC_TAMPER_DEBOUNCE_DISABLE
  757. *
  758. * @return None
  759. *
  760. * @details This API is used to enable the tamper pin detect function with specify trigger condition.
  761. */
  762. void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn)
  763. {
  764. uint32_t i;
  765. uint32_t u32Reg;
  766. uint32_t u32TmpReg;
  767. RTC_WaitAccessEnable();
  768. u32Reg = RTC->TAMPCTL;
  769. u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | (u32DetecLevel << RTC_TAMPCTL_TAMP0LV_Pos) |
  770. (u32DebounceEn << RTC_TAMPCTL_TAMP0DBEN_Pos));
  771. for (i = 0ul; i < MAX_TAMPER_PIN_NUM; i++)
  772. {
  773. if (u32TamperSelect & (0x1ul << i))
  774. {
  775. u32Reg &= ~((RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP0LV_Msk | RTC_TAMPCTL_TAMP0DBEN_Msk) << (i * 4ul));
  776. u32Reg |= (u32TmpReg << (i * 4ul));
  777. }
  778. }
  779. RTC_WaitAccessEnable();
  780. RTC->TAMPCTL = u32Reg;
  781. }
  782. /**
  783. * @brief Static Tamper Disable
  784. *
  785. * @param[in] u32TamperSelect Tamper pin select. Possible options are
  786. * - \ref RTC_TAMPER5_SELECT
  787. * - \ref RTC_TAMPER4_SELECT
  788. * - \ref RTC_TAMPER3_SELECT
  789. * - \ref RTC_TAMPER2_SELECT
  790. * - \ref RTC_TAMPER1_SELECT
  791. * - \ref RTC_TAMPER0_SELECT
  792. *
  793. * @return None
  794. *
  795. * @details This API is used to disable the static tamper pin detect.
  796. */
  797. void RTC_StaticTamperDisable(uint32_t u32TamperSelect)
  798. {
  799. uint32_t i;
  800. uint32_t u32Reg;
  801. uint32_t u32TmpReg;
  802. RTC_WaitAccessEnable();
  803. u32Reg = RTC->TAMPCTL;
  804. u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk);
  805. for (i = 0ul; i < MAX_TAMPER_PIN_NUM; i++)
  806. {
  807. if (u32TamperSelect & (0x1ul << i))
  808. {
  809. u32Reg &= ~(u32TmpReg << (i * 4ul));
  810. }
  811. }
  812. RTC_WaitAccessEnable();
  813. RTC->TAMPCTL = u32Reg;
  814. }
  815. /**
  816. * @brief Dynamic Tamper Detect
  817. *
  818. * @param[in] u32PairSel Tamper pin detection enable. Possible options are
  819. * - \ref RTC_PAIR0_SELECT
  820. * - \ref RTC_PAIR1_SELECT
  821. * - \ref RTC_PAIR2_SELECT
  822. *
  823. * @param[in] u32DebounceEn Tamper pin de-bounce enable
  824. * - \ref RTC_TAMPER_DEBOUNCE_ENABLE
  825. * - \ref RTC_TAMPER_DEBOUNCE_DISABLE
  826. *
  827. * @param[in] u32Pair1Source Dynamic Pair 1 Input Source Select
  828. * 0: Pair 1 source select tamper 2
  829. * 1: Pair 1 source select tamper 0
  830. *
  831. * @param[in] u32Pair2Source Dynamic Pair 2 Input Source Select
  832. * 0: Pair 2 source select tamper 4
  833. * 1: Pair 2 source select tamper 0
  834. *
  835. * @return None
  836. *
  837. * @details This API is used to enable the dynamic tamper.
  838. */
  839. void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source)
  840. {
  841. uint32_t i;
  842. uint32_t u32Reg;
  843. uint32_t u32TmpReg;
  844. uint32_t u32Tamper2Debounce, u32Tamper4Debounce;
  845. RTC_WaitAccessEnable();
  846. u32Reg = RTC->TAMPCTL;
  847. u32Tamper2Debounce = u32Reg & RTC_TAMPCTL_TAMP2DBEN_Msk;
  848. u32Tamper4Debounce = u32Reg & RTC_TAMPCTL_TAMP4DBEN_Msk;
  849. u32Reg &= ~(RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_TAMP2EN_Msk |
  850. RTC_TAMPCTL_TAMP3EN_Msk | RTC_TAMPCTL_TAMP4EN_Msk | RTC_TAMPCTL_TAMP5EN_Msk);
  851. u32Reg &= ~(RTC_TAMPCTL_DYN1ISS_Msk | RTC_TAMPCTL_DYN2ISS_Msk);
  852. u32Reg |= ((u32Pair1Source & 0x1ul) << RTC_TAMPCTL_DYN1ISS_Pos) | ((u32Pair2Source & 0x1ul) << RTC_TAMPCTL_DYN2ISS_Pos);
  853. if (u32DebounceEn)
  854. {
  855. u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk |
  856. RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
  857. }
  858. else
  859. {
  860. u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
  861. }
  862. for (i = 0ul; i < MAX_PAIR_NUM; i++)
  863. {
  864. if (u32PairSel & (0x1ul << i))
  865. {
  866. u32Reg &= ~((RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk) << (i * 8ul));
  867. u32Reg |= (u32TmpReg << (i * 8ul));
  868. }
  869. }
  870. if ((u32Pair1Source) && (u32PairSel & RTC_PAIR1_SELECT))
  871. {
  872. u32Reg &= ~RTC_TAMPCTL_TAMP2EN_Msk;
  873. u32Reg |= u32Tamper2Debounce;
  874. }
  875. if ((u32Pair2Source) && (u32PairSel & RTC_PAIR2_SELECT))
  876. {
  877. u32Reg &= ~RTC_TAMPCTL_TAMP4EN_Msk;
  878. u32Reg |= u32Tamper4Debounce;
  879. }
  880. RTC_WaitAccessEnable();
  881. RTC->TAMPCTL = u32Reg;
  882. }
  883. /**
  884. * @brief Dynamic Tamper Disable
  885. *
  886. * @param[in] u32PairSel Tamper pin detection enable. Possible options are
  887. * - \ref RTC_PAIR0_SELECT
  888. * - \ref RTC_PAIR1_SELECT
  889. * - \ref RTC_PAIR2_SELECT
  890. *
  891. * @return None
  892. *
  893. * @details This API is used to disable the dynamic tamper.
  894. */
  895. void RTC_DynamicTamperDisable(uint32_t u32PairSel)
  896. {
  897. uint32_t i;
  898. uint32_t u32Reg;
  899. uint32_t u32TmpReg;
  900. uint32_t u32Tamper2En = 0ul, u32Tamper4En = 0ul;
  901. RTC_WaitAccessEnable();
  902. u32Reg = RTC->TAMPCTL;
  903. if ((u32Reg & RTC_TAMPCTL_DYN1ISS_Msk) && (u32PairSel & RTC_PAIR1_SELECT))
  904. {
  905. u32Tamper2En = u32Reg & RTC_TAMPCTL_TAMP2EN_Msk;
  906. }
  907. if ((u32Reg & RTC_TAMPCTL_DYN2ISS_Msk) && (u32PairSel & RTC_PAIR2_SELECT))
  908. {
  909. u32Tamper4En = u32Reg & RTC_TAMPCTL_TAMP4EN_Msk;
  910. }
  911. u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
  912. for (i = 0ul; i < MAX_PAIR_NUM; i++)
  913. {
  914. if (u32PairSel & (0x1ul << i))
  915. {
  916. u32Reg &= ~(u32TmpReg << ((i * 8ul)));
  917. }
  918. }
  919. u32Reg |= (u32Tamper2En | u32Tamper4En);
  920. RTC_WaitAccessEnable();
  921. RTC->TAMPCTL = u32Reg;
  922. }
  923. /**
  924. * @brief Config dynamic tamper
  925. *
  926. * @param[in] u32ChangeRate The dynamic tamper output change rate
  927. * - \ref RTC_2POW10_CLK
  928. * - \ref RTC_2POW11_CLK
  929. * - \ref RTC_2POW12_CLK
  930. * - \ref RTC_2POW13_CLK
  931. * - \ref RTC_2POW14_CLK
  932. * - \ref RTC_2POW15_CLK
  933. * - \ref RTC_2POW16_CLK
  934. * - \ref RTC_2POW17_CLK
  935. *
  936. * @param[in] u32SeedReload Reload new seed or not
  937. * 0: not reload new seed
  938. * 1: reload new seed
  939. *
  940. * @param[in] u32RefPattern Reference pattern
  941. * - \ref REF_RANDOM_PATTERN
  942. * - \ref REF_PREVIOUS_PATTERN
  943. * - \ref REF_SEED
  944. *
  945. * @param[in] u32Seed Seed Value (0x0 ~ 0xFFFFFFFF)
  946. *
  947. * @return None
  948. *
  949. * @details This API is used to config dynamic tamper setting.
  950. */
  951. void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed)
  952. {
  953. uint32_t u32Reg;
  954. RTC_WaitAccessEnable();
  955. u32Reg = RTC->TAMPCTL;
  956. u32Reg &= ~(RTC_TAMPCTL_DYNSRC_Msk | RTC_TAMPCTL_SEEDRLD_Msk | RTC_TAMPCTL_DYNRATE_Msk);
  957. u32Reg |= (u32ChangeRate) | ((u32SeedReload & 0x1ul) << RTC_TAMPCTL_SEEDRLD_Pos) |
  958. ((u32RefPattern & 0x3ul) << RTC_TAMPCTL_DYNSRC_Pos);
  959. RTC_WaitAccessEnable();
  960. RTC->TAMPSEED = u32Seed; /* need set seed value before re-load seed */
  961. RTC_WaitAccessEnable();
  962. RTC->TAMPCTL = u32Reg;
  963. }
  964. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  965. /*@}*/ /* end of group RTC_Driver */
  966. /*@}*/ /* end of group Standard_Driver */
  967. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/