lib_rtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /**
  2. ******************************************************************************
  3. * @file lib_rtc.c
  4. * @author Application Team
  5. * @version V1.1.0
  6. * @date 2019-10-28
  7. * @brief RTC library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #include "lib_rtc.h"
  14. #define RTCPWD_KEY 0x5AA55AA5
  15. #define RTCCE_SETKEY 0xA55AA55B
  16. #define RTCCE_CLRKEY 0xA55AA55A
  17. /**
  18. * @brief Enables or disables RTC registers write protection.
  19. * @param NewState:
  20. * ENABLE
  21. * DISABLE
  22. * @retval None
  23. */
  24. void RTC_WriteProtection(uint32_t NewState)
  25. {
  26. /* Check parameters */
  27. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  28. /* Enable RTC Write-Protection */
  29. if (NewState != DISABLE)
  30. {
  31. RTC->PWD = RTCPWD_KEY;
  32. RTC->CE = RTCCE_CLRKEY;
  33. }
  34. /* Disable RTC Write-Protection */
  35. else
  36. {
  37. RTC->PWD = RTCPWD_KEY;
  38. RTC->CE = RTCCE_SETKEY;
  39. }
  40. }
  41. /**
  42. * @brief Waits until the RTC registers (be W/R protected) are synchronized
  43. * with RTC APB clock.
  44. * @note The RTC Resynchronization mode is write protected, use the
  45. * RTC_WriteProtection(DISABLE) before calling this function.
  46. * Write-Operation process as follows:
  47. * 1. RTC_WriteProtection(DISABLE);
  48. * 2. RTC Registers write operation(only first write-operation be
  49. * valid on the same register).
  50. * 3. RTC_WriteProtection(ENABLE);
  51. * 4. RTC_WaitForSynchro(); Wait until the RTC registers be
  52. * synchronized by calling this function.
  53. * @retval None
  54. */
  55. void RTC_WaitForSynchro(void)
  56. {
  57. while (RTC->CE & RTC_CE_BSY)
  58. {
  59. }
  60. }
  61. /**
  62. * @brief Writes RTC registers(continuous/be write-protected).
  63. * @param[in] StartAddr the start address of registers be written
  64. * @param[in] wBuffer pointer to write
  65. * @param[in] Len number of registers be written
  66. * @retval None
  67. */
  68. void RTC_WriteRegisters(uint32_t StartAddr, const uint32_t *wBuffer, uint8_t Len)
  69. {
  70. uint8_t cnt;
  71. /* Parameter check */
  72. assert_parameters(IS_RTC_REGOP_STARTADDR(StartAddr));
  73. /* Wait until the RTC registers be synchronized */
  74. RTC_WaitForSynchro();
  75. /* Disable RTC Registers write-protection */
  76. RTC_WriteProtection(DISABLE);
  77. /* Write registers */
  78. for (cnt=0; cnt<Len; cnt++)
  79. {
  80. *(volatile uint32_t *)(StartAddr) = *(wBuffer++);
  81. StartAddr += 4;
  82. }
  83. /* Enable RTC Registers write-protection */
  84. RTC_WriteProtection(ENABLE);
  85. /* Wait until the RTC registers be synchronized */
  86. RTC_WaitForSynchro();
  87. }
  88. /**
  89. * @brief Reads RTC registers(continuous/be read-protected).
  90. * @param[in] StartAddr the start address of registers be read
  91. * @param[out] rBuffer pointer to read
  92. * @param[in] Len number of registers be read
  93. * @retval None
  94. */
  95. void RTC_ReadRegisters(uint32_t StartAddr, uint32_t *rBuffer, uint8_t Len)
  96. {
  97. __IO uint32_t tmp;
  98. uint8_t cnt;
  99. /* Parameter check */
  100. assert_parameters(IS_RTC_REGOP_STARTADDR(StartAddr));
  101. /* Wait until the RTC registers be synchronized */
  102. RTC_WaitForSynchro();
  103. /* Dummy read-operation to RTC->LOAD */
  104. tmp = RTC->LOAD;
  105. tmp += 1;
  106. /* Wait until the RTC registers be synchronized */
  107. RTC_WaitForSynchro();
  108. /* Read registers */
  109. for (cnt=0; cnt<Len; cnt++)
  110. {
  111. *(rBuffer++) = *(volatile uint32_t *)(StartAddr);
  112. StartAddr += 4;
  113. }
  114. }
  115. /**
  116. * @brief Sets RTC current time.
  117. * @param sTime: Pointer to Time structure
  118. * AccurateSel:
  119. * RTC_ACCURATE
  120. * RTC_INACCURATE
  121. * @retval None
  122. */
  123. void RTC_SetTime(RTC_TimeTypeDef *sTime, uint32_t AccurateSel)
  124. {
  125. uint32_t subsec,sec,alarmctl;
  126. /* Parameter check */
  127. assert_parameters(IS_RTC_TIME_YEAR(sTime->Year));
  128. assert_parameters(IS_RTC_TIME_MONTH(sTime->Month));
  129. assert_parameters(IS_RTC_TIME_DATE(sTime->Date));
  130. assert_parameters(IS_RTC_TIME_WEEKDAY(sTime->WeekDay));
  131. assert_parameters(IS_RTC_TIME_HOURS(sTime->Hours));
  132. assert_parameters(IS_RTC_TIME_MINS(sTime->Minutes));
  133. assert_parameters(IS_RTC_TIME_SECS(sTime->Seconds));
  134. if (AccurateSel == RTC_ACCURATE)
  135. assert_parameters(IS_RTC_TIME_SubSECS(sTime->SubSeconds));
  136. assert_parameters(IS_RTC_ACCURATESEL(AccurateSel));
  137. subsec = sTime->SubSeconds;
  138. subsec = subsec -(subsec>>8)*156 -((subsec&0xFF)>>4)*6;
  139. sec = sTime->Seconds;
  140. sec = sec - (sec>>4)*6;
  141. subsec = sec * 32768 + subsec * 32768 / 1000;
  142. alarmctl = RTC->ALARMCTL;
  143. if (AccurateSel == RTC_ACCURATE)
  144. alarmctl |= RTC_ALARMCTL_TIME_CNT_EN;
  145. else
  146. alarmctl &= ~RTC_ALARMCTL_TIME_CNT_EN;
  147. /* Wait until the RTC registers be synchronized */
  148. RTC_WaitForSynchro();
  149. /* Disable RTC Registers write-protection */
  150. RTC_WriteProtection(DISABLE);
  151. /* Write RTC time registers */
  152. RTC->TIME = subsec;
  153. RTC->SEC = sTime->Seconds;
  154. RTC->MIN = sTime->Minutes;
  155. RTC->HOUR = sTime->Hours;
  156. RTC->DAY = sTime->Date;
  157. RTC->WEEK = sTime->WeekDay;
  158. RTC->MON = sTime->Month;
  159. RTC->YEAR = sTime->Year;
  160. RTC->ALARMCTL = alarmctl;
  161. /* Enable RTC Registers write-protection */
  162. RTC_WriteProtection(ENABLE);
  163. /* Wait until the RTC registers be synchronized */
  164. RTC_WaitForSynchro();
  165. }
  166. /**
  167. * @brief Gets RTC current time.
  168. * @param[out] gTime: Pointer to Time structure
  169. * @param[in] AccurateSel:
  170. * RTC_ACCURATE
  171. * RTC_INACCURATE
  172. * @retval None
  173. */
  174. void RTC_GetTime(RTC_TimeTypeDef *gTime, uint32_t AccurateSel)
  175. {
  176. __IO uint32_t dummy_data = 0;
  177. uint32_t subsec,sec;
  178. /* Parameter check */
  179. assert_parameters(IS_RTC_ACCURATESEL(AccurateSel));
  180. /* Wait until the RTC registers be synchronized */
  181. RTC_WaitForSynchro();
  182. /* Dummy read-operation to RTC->LOAD register */
  183. dummy_data = RTC->LOAD;
  184. dummy_data += 1;
  185. /* Wait until the RTC registers be synchronized */
  186. RTC_WaitForSynchro();
  187. /* Read RTC time registers */
  188. gTime->Seconds = RTC->SEC;
  189. gTime->Minutes = RTC->MIN;
  190. gTime->Hours = RTC->HOUR;
  191. gTime->Date = RTC->DAY;
  192. gTime->WeekDay = RTC->WEEK;
  193. gTime->Month = RTC->MON;
  194. gTime->Year = RTC->YEAR;
  195. subsec = RTC->TIME;
  196. if (AccurateSel == RTC_ACCURATE)
  197. {
  198. sec = subsec/32768;
  199. sec = sec + (sec/10)*6;
  200. gTime->Seconds = sec;
  201. subsec = (subsec%32768)*1000/32768;
  202. subsec = subsec + ((subsec%100)/10)*6 + (subsec/100)*156;
  203. gTime->SubSeconds = subsec;
  204. }
  205. else
  206. {
  207. gTime->SubSeconds = 0;
  208. }
  209. }
  210. /**
  211. * @brief Enables or disables the RTC Sub Seconds.
  212. * @param NewState:
  213. * ENABLE
  214. * DISABLE
  215. * @retval None
  216. */
  217. void RTC_SubSecondCmd(uint32_t NewState)
  218. {
  219. uint32_t tmp;
  220. /* Parameter check */
  221. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  222. tmp = RTC->ALARMCTL;
  223. if (NewState == ENABLE)
  224. {
  225. tmp |= RTC_ALARMCTL_TIME_CNT_EN;
  226. }
  227. else
  228. {
  229. tmp &= ~RTC_ALARMCTL_TIME_CNT_EN;
  230. }
  231. /* Wait until the RTC registers be synchronized */
  232. RTC_WaitForSynchro();
  233. /* Disable RTC Registers write-protection */
  234. RTC_WriteProtection(DISABLE);
  235. RTC->ALARMCTL = tmp;
  236. /* Enable RTC Registers write-protection */
  237. RTC_WriteProtection(ENABLE);
  238. /* Wait until the RTC registers be synchronized */
  239. RTC_WaitForSynchro();
  240. }
  241. /**
  242. * @brief Sets the RTC Alarm.
  243. * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
  244. * contains the alarm configuration parameters.
  245. * AccurateSel:
  246. * RTC_ACCURATE
  247. * RTC_INACCURATE
  248. * @retval None
  249. */
  250. void RTC_SetAlarm(RTC_AlarmTypeDef *RTC_AlarmStruct, uint32_t AccurateSel)
  251. {
  252. uint32_t subsec,sec,alarmctl;
  253. /* Parameter check */
  254. assert_parameters(IS_RTC_TIME_HOURS(RTC_AlarmStruct->AlarmHours));
  255. assert_parameters(IS_RTC_TIME_MINS(RTC_AlarmStruct->AlarmMinutes));
  256. assert_parameters(IS_RTC_TIME_SECS(RTC_AlarmStruct->AlarmSeconds));
  257. if (AccurateSel == RTC_ACCURATE)
  258. assert_parameters(IS_RTC_TIME_SubSECS(RTC_AlarmStruct->AlarmSubSeconds));
  259. assert_parameters(IS_RTC_ACCURATESEL(AccurateSel));
  260. subsec = RTC_AlarmStruct->AlarmSubSeconds;
  261. subsec = subsec -(subsec>>8)*156 -((subsec&0xFF)>>4)*6;
  262. sec = RTC_AlarmStruct->AlarmSeconds;
  263. sec = sec - (sec>>4)*6;
  264. subsec = sec * 32768 + subsec * 32768 / 1000;
  265. alarmctl = RTC->ALARMCTL;
  266. if (AccurateSel == RTC_ACCURATE)
  267. alarmctl &= ~RTC_ALARMCTL_ALARM_INACCURATE;
  268. else
  269. alarmctl |= RTC_ALARMCTL_ALARM_INACCURATE;
  270. /* Wait until the RTC registers be synchronized */
  271. RTC_WaitForSynchro();
  272. /* Disable RTC Registers write-protection */
  273. RTC_WriteProtection(DISABLE);
  274. RTC->ALARMHOUR = RTC_AlarmStruct->AlarmHours;
  275. RTC->ALARMMIN = RTC_AlarmStruct->AlarmMinutes;
  276. RTC->ALARMSEC = RTC_AlarmStruct->AlarmSeconds;
  277. RTC->ALARMTIME = subsec;
  278. RTC->ALARMCTL = alarmctl;
  279. /* Write RTC time registers */
  280. /* Enable RTC Registers write-protection */
  281. RTC_WriteProtection(ENABLE);
  282. /* Wait until the RTC registers be synchronized */
  283. RTC_WaitForSynchro();
  284. }
  285. /**
  286. * @brief Gets the RTC Alarm.
  287. * @param[out] RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
  288. * contains the output alarm configuration values.
  289. * @param[in] AccurateSel:
  290. * RTC_ACCURATE
  291. * RTC_INACCURATE
  292. * @retval None
  293. */
  294. void RTC_GetAlarm(RTC_AlarmTypeDef *RTC_AlarmStruct, uint32_t AccurateSel)
  295. {
  296. uint32_t sec,subsec;
  297. /* Parameter check */
  298. assert_parameters(IS_RTC_ACCURATESEL(AccurateSel));
  299. /* Wait until the RTC registers be synchronized */
  300. RTC_WaitForSynchro();
  301. /* Read RTC time registers */
  302. RTC_AlarmStruct->AlarmHours = RTC->ALARMHOUR;
  303. RTC_AlarmStruct->AlarmMinutes = RTC->ALARMMIN;
  304. RTC_AlarmStruct->AlarmSeconds = RTC->ALARMSEC;
  305. subsec = RTC->ALARMTIME;
  306. if (AccurateSel == RTC_ACCURATE)
  307. {
  308. sec = subsec/32768;
  309. sec = sec + (sec/10)*6;
  310. RTC_AlarmStruct->AlarmSeconds = sec;
  311. subsec = (subsec%32768)*1000/32768;
  312. subsec = subsec + ((subsec%100)/10)*6 + (subsec/100)*156;
  313. RTC_AlarmStruct->AlarmSubSeconds = subsec;
  314. }
  315. else
  316. {
  317. RTC_AlarmStruct->AlarmSubSeconds = 0;
  318. }
  319. }
  320. /**
  321. * @brief Enables or disables the RTC Alarm.
  322. * @param NewState:
  323. * ENABLE
  324. * DISABLE
  325. * @retval None
  326. */
  327. void RTC_AlarmCmd(uint32_t NewState)
  328. {
  329. uint32_t tmp;
  330. /* Parameter check */
  331. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  332. tmp = RTC->ALARMCTL;
  333. if (NewState == ENABLE)
  334. {
  335. tmp |= (RTC_ALARMCTL_ALARM_EN);
  336. }
  337. else
  338. {
  339. tmp &= ~(RTC_ALARMCTL_ALARM_EN);
  340. }
  341. /* Wait until the RTC registers be synchronized */
  342. RTC_WaitForSynchro();
  343. /* Disable RTC Registers write-protection */
  344. RTC_WriteProtection(DISABLE);
  345. RTC->ALARMCTL = tmp;
  346. /* Enable RTC Registers write-protection */
  347. RTC_WriteProtection(ENABLE);
  348. /* Wait until the RTC registers be synchronized */
  349. RTC_WaitForSynchro();
  350. }
  351. /**
  352. * @brief Enables or disables the RTC alarm accurate.
  353. * @param NewState:
  354. * ENABLE
  355. * DISABLE
  356. * @retval None
  357. */
  358. void RTC_AlarmAccurateCmd(uint32_t NewState)
  359. {
  360. uint32_t tmp = 0;
  361. /* Parameter check */
  362. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  363. tmp = RTC->ALARMCTL;
  364. if (NewState == ENABLE)
  365. {
  366. tmp &= ~RTC_ALARMCTL_ALARM_INACCURATE;
  367. }
  368. else
  369. {
  370. tmp |= RTC_ALARMCTL_ALARM_INACCURATE;
  371. }
  372. /* Wait until the RTC registers be synchronized */
  373. RTC_WaitForSynchro();
  374. /* Disable RTC Registers write-protection */
  375. RTC_WriteProtection(DISABLE);
  376. RTC->ALARMCTL = tmp;
  377. /* Enable RTC Registers write-protection */
  378. RTC_WriteProtection(ENABLE);
  379. /* Wait until the RTC registers be synchronized */
  380. RTC_WaitForSynchro();
  381. }
  382. /**
  383. * @brief Enables or disables RTC interrupt.
  384. * @param INTMask: can use the '|' operator
  385. RTC_INT_ALARM
  386. RTC_INT_CEILLE
  387. RTC_INT_ACDONE
  388. RTC_INT_WKUCNT
  389. RTC_INT_MIDNIGHT
  390. RTC_INT_WKUHOUR
  391. RTC_INT_WKUMIN
  392. RTC_INT_WKUSEC
  393. RTC_INT_TIMEILLE
  394. RTC_INT_ITVSITV
  395. NewState:
  396. ENABLE
  397. DISABLE
  398. * @retval None
  399. */
  400. void RTC_INTConfig(uint32_t INTMask, uint32_t NewState)
  401. {
  402. /* Parameter check */
  403. assert_parameters(IS_RTC_INT(INTMask));
  404. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  405. if (NewState == ENABLE)
  406. RTC->INTEN |= INTMask;
  407. else
  408. RTC->INTEN &= ~INTMask;
  409. }
  410. /**
  411. * @brief Gets RTC interrupt status.
  412. * @param INTMask:
  413. RTC_INTSTS_ALARM
  414. RTC_INTSTS_CEILLE
  415. RTC_INTSTS_WKUCNT
  416. RTC_INTSTS_MIDNIGHT
  417. RTC_INTSTS_WKUHOUR
  418. RTC_INTSTS_WKUMIN
  419. RTC_INTSTS_WKUSEC
  420. RTC_INTSTS_TIMEILLE
  421. RTC_INTSTS_ITVSITV
  422. * @retval 1: status set
  423. 0: status reset.
  424. */
  425. uint8_t RTC_GetINTStatus(uint32_t FlagMask)
  426. {
  427. /* Parameter check */
  428. assert_parameters(IS_RTC_INTFLAGR(FlagMask));
  429. if (RTC->INTSTS&FlagMask)
  430. {
  431. return 1;
  432. }
  433. else
  434. {
  435. return 0;
  436. }
  437. }
  438. /**
  439. * @brief Clears RTC interrupt status.
  440. * @param INTMask: can use the '|' operator
  441. RTC_INTSTS_ALARM
  442. RTC_INTSTS_CEILLE
  443. RTC_INTSTS_WKUCNT
  444. RTC_INTSTS_MIDNIGHT
  445. RTC_INTSTS_WKUHOUR
  446. RTC_INTSTS_WKUMIN
  447. RTC_INTSTS_WKUSEC
  448. RTC_INTSTS_TIMEILLE
  449. RTC_INTSTS_ITVSITV
  450. * @retval None
  451. */
  452. void RTC_ClearINTStatus(uint32_t FlagMask)
  453. {
  454. /* Parameter check */
  455. assert_parameters(IS_RTC_INTFLAGC(FlagMask));
  456. RTC->INTSTS = FlagMask;
  457. }
  458. /*
  459. * @brief Configures Multi-second wake up function.
  460. * @param nPeriod£ºN seconds interval.
  461. * @note For the first interrupt generated by calling this function, it may
  462. * have < 1 sec error if the new WKUSEC number(parameter) is not equal
  463. * to current WKUSEC number. If the new WKUSEC is equal to current WKUSEC,
  464. * the first interrupt time may have 0~(WKUSEC +1) variation.
  465. * To avoid this problem, set an alternative parameter (like 1) by calling
  466. * this function, then set the correct parameter to it.
  467. * @retval None
  468. */
  469. void RTC_WKUSecondsConfig(uint8_t nPeriod)
  470. {
  471. /* Parameter check */
  472. assert_parameters(IS_RTC_WKUSEC_PERIOD(nPeriod));
  473. /* Wait until the RTC registers be synchronized */
  474. RTC_WaitForSynchro();
  475. /* Disable RTC Registers write-protection */
  476. RTC_WriteProtection(DISABLE);
  477. /* Write registers */
  478. RTC->WKUSEC = nPeriod - 1;
  479. /* Enable RTC Registers write-protection */
  480. RTC_WriteProtection(ENABLE);
  481. /* Wait until the RTC registers be synchronized */
  482. RTC_WaitForSynchro();
  483. }
  484. /*
  485. * @brief Configures Multi-minute wake up function.
  486. * @param nPeriod£ºN minute interval.
  487. * @note For the first interrupt generated by calling this function, it may
  488. * have < 1 min error if the new WKUMIN number(parameter) is not equal
  489. * to current WKUMIN number. If the new WKUMIN is equal to current WKUMIN,
  490. * the first interrupt time may have 0~(WKUMIN +1) variation.
  491. * To avoid this problem, set an alternative parameter (like 1) by calling
  492. * this function, then set the correct parameter to it.
  493. * @retval None
  494. */
  495. void RTC_WKUMinutesConfig(uint8_t nPeriod)
  496. {
  497. /* Parameter check */
  498. assert_parameters(IS_RTC_WKUMIN_PERIOD(nPeriod));
  499. /* Wait until the RTC registers be synchronized */
  500. RTC_WaitForSynchro();
  501. /* Disable RTC Registers write-protection */
  502. RTC_WriteProtection(DISABLE);
  503. /* Write registers */
  504. RTC->WKUMIN = nPeriod - 1;
  505. /* Enable RTC Registers write-protection */
  506. RTC_WriteProtection(ENABLE);
  507. /* Wait until the RTC registers be synchronized */
  508. RTC_WaitForSynchro();
  509. }
  510. /*
  511. * @brief Configures Multi-hour wake up function.
  512. * @param nPeriod£ºN hour interval.
  513. * @note For the first interrupt generated by calling this function, it may
  514. * have < 1 hour error if the new WKUHOUR number(parameter) is not equal
  515. * to current WKUHOUR number. If the new WKUHOUR is equal to current WKUHOUR,
  516. * the first interrupt time may have 0~(WKUHOUR +1) variation.
  517. * To avoid this problem, set an alternative parameter (like 1) by calling
  518. * this function, then set the correct parameter to it.
  519. * @retval None
  520. */
  521. void RTC_WKUHoursConfig(uint8_t nPeriod)
  522. {
  523. /* Parameter check */
  524. assert_parameters(IS_RTC_WKUHOUR_PERIOD(nPeriod));
  525. /* Wait until the RTC registers be synchronized */
  526. RTC_WaitForSynchro();
  527. /* Disable RTC Registers write-protection */
  528. RTC_WriteProtection(DISABLE);
  529. /* Write registers */
  530. RTC->WKUHOUR = nPeriod - 1;
  531. /* Enable RTC Registers write-protection */
  532. RTC_WriteProtection(ENABLE);
  533. /* Wait until the RTC registers be synchronized */
  534. RTC_WaitForSynchro();
  535. }
  536. /**
  537. * @brief Configures RTC counter wake up function.
  538. * @param nClock:
  539. CNTCLK:
  540. RTC_WKUCNT_RTCCLK
  541. RTC_WKUCNT_2048
  542. RTC_WKUCNT_512
  543. RTC_WKUCNT_128
  544. * @retval None
  545. */
  546. void RTC_WKUCounterConfig(uint32_t nClock,uint32_t CNTCLK)
  547. {
  548. /* Parameter check */
  549. assert_parameters(IS_RTC_WKUCNT_PERIOD(nClock));
  550. assert_parameters(IS_RTC_WKUCNT_CNTSEL(CNTCLK));
  551. /* Wait until the RTC registers be synchronized */
  552. RTC_WaitForSynchro();
  553. /* Disable RTC Registers write-protection */
  554. RTC_WriteProtection(DISABLE);
  555. /* Write registers */
  556. RTC->WKUCNT = (CNTCLK & RTC_WKUCNT_CNTSEL) | ((nClock & RTC_WKUCNT_WKUCNT) -1 );
  557. /* Enable RTC Registers write-protection */
  558. RTC_WriteProtection(ENABLE);
  559. /* Wait until the RTC registers be synchronized */
  560. RTC_WaitForSynchro();
  561. }
  562. /**
  563. * @brief Configures RTC ITV wake up function.
  564. * @param nType:
  565. RTC_ITV_SEC
  566. RTC_ITV_MIN
  567. RTC_ITV_HOUR
  568. RTC_ITV_DAY
  569. RTC_ITV_500MS
  570. RTC_ITV_250MS
  571. RTC_ITV_125MS
  572. RTC_ITV_62MS
  573. * @retval None
  574. */
  575. void RTC_WAKE_ITV(uint8_t nType)
  576. {
  577. /* Parameter check */
  578. assert_parameters(IS_RTC_ITV(nType));
  579. /* Wait until the RTC registers be synchronized */
  580. RTC_WaitForSynchro();
  581. /* Disable RTC Registers write-protection */
  582. RTC_WriteProtection(DISABLE);
  583. RTC->ITV = nType;
  584. RTC->SITV = 0;
  585. /* Enable RTC Registers write-protection */
  586. RTC_WriteProtection(ENABLE);
  587. /* Wait until the RTC registers be synchronized */
  588. RTC_WaitForSynchro();
  589. }
  590. /**
  591. * @brief Configures RTC SITV wake up function.
  592. * @param nPeriod:1~63
  593. * @retval None
  594. */
  595. void RTC_WAKE_SITV(uint8_t nPeriod)
  596. {
  597. /* Parameter check */
  598. assert_parameters(IS_RTC_SITV(nPeriod));
  599. /* Wait until the RTC registers be synchronized */
  600. RTC_WaitForSynchro();
  601. /* Disable RTC Registers write-protection */
  602. RTC_WriteProtection(DISABLE);
  603. RTC->ITV = RTC_ITV_SITVSEC;
  604. RTC->SITV = RTC_SITV_SITVEN | (nPeriod - 1);
  605. /* Enable RTC Registers write-protection */
  606. RTC_WriteProtection(ENABLE);
  607. /* Wait until the RTC registers be synchronized */
  608. RTC_WaitForSynchro();
  609. }
  610. /**
  611. * @brief Gets RTC wake-up counter value.
  612. * @retval RTC wake-up counter value
  613. */
  614. uint32_t RTC_GetWKUCounterValue(void)
  615. {
  616. return RTC->WKUCNTR;
  617. }
  618. /**
  619. * @brief Configures RTC clock prescaler.
  620. * @param[in] Prescaler:
  621. * RTC_CLKDIV_1
  622. * RTC_CLKDIV_4
  623. * @retval None
  624. */
  625. void RTC_PrescalerConfig(uint32_t Prescaler)
  626. {
  627. uint32_t tmp;
  628. /* Parameter check */
  629. assert_parameters(IS_RTC_CLKDIV(Prescaler));
  630. tmp = RTC->PSCA;
  631. tmp &= ~RTC_PSCA_PSCA;
  632. tmp |= Prescaler;
  633. /* Wait until the RTC registers be synchronized */
  634. RTC_WaitForSynchro();
  635. /* Disable RTC Registers write-protection */
  636. RTC_WriteProtection(DISABLE);
  637. RTC->PSCA = tmp;
  638. /* Enable RTC Registers write-protection */
  639. RTC_WriteProtection(ENABLE);
  640. /* Wait until the RTC registers be synchronized */
  641. RTC_WaitForSynchro();
  642. }
  643. /**
  644. * @brief Configures RTC PLLDIV clock-source and frequency.
  645. * @param Source:
  646. RTC_PLLDIVSOURCE_PLLL
  647. RTC_PLLDIVSOURCE_PCLK
  648. nfrequency(HZ): the frequency of RTC PLLDIV output configuration.
  649. * @note Ensure clocks be configured by calling function CLK_ClockConfig(),
  650. * get correct PCLK frequency by calling function CLK_GetPCLKFreq().
  651. * @retval None
  652. */
  653. void RTC_PLLDIVConfig(uint32_t DIVSource,uint32_t nfrequency)
  654. {
  655. /* Parameter check */
  656. assert_parameters(IS_RTC_PLLDIVSOURCE(DIVSource));
  657. if (DIVSource == RTC_PLLDIVSOURCE_PLLL)
  658. {
  659. RTC->CTL |= RTC_CTL_RTCPLLCLKSEL;
  660. if (nfrequency == 0)
  661. {
  662. RTC->DIV = RTC_DIV_RTCDIV;
  663. }
  664. else
  665. {
  666. RTC->DIV = CLK_GetPLLLFreq()/2/nfrequency - 1;
  667. }
  668. }
  669. else
  670. {
  671. RTC->CTL &= ~RTC_CTL_RTCPLLCLKSEL;
  672. if (nfrequency == 0)
  673. {
  674. RTC->DIV = RTC_DIV_RTCDIV;
  675. }
  676. else
  677. {
  678. RTC->DIV = CLK_GetPLLLFreq()/2/nfrequency - 1;
  679. }
  680. }
  681. }
  682. /**
  683. * @brief Enables or disables RTC PLLDIV output function.
  684. * @param NewState:
  685. * ENABLE
  686. * DISABLE
  687. * @retval None
  688. */
  689. void RTC_PLLDIVOutputCmd(uint8_t NewState)
  690. {
  691. /* Parameter check */
  692. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  693. if (NewState == ENABLE) RTC->CTL |= RTC_CTL_RTCPLLOE;
  694. else RTC->CTL &= ~RTC_CTL_RTCPLLOE;
  695. }
  696. /*********************************** END OF FILE ******************************/