apm32f4xx_rtc.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. /*!
  2. * @file apm32f4xx_rtc.c
  3. *
  4. * @brief This file provides all the RTC firmware functions
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-06-23
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. #include "apm32f4xx.h"
  26. #include "apm32f4xx_rtc.h"
  27. #include "apm32f4xx_rcm.h"
  28. /** @addtogroup APM32F4xx_StdPeriphDriver
  29. @{
  30. */
  31. /** @defgroup RTC_Driver
  32. * @brief RTC driver modules
  33. @{
  34. */
  35. /** @defgroup RTC_Macros Macros
  36. @{
  37. */
  38. /* RTC timeout definition */
  39. #define RTC_TIMEOUT_WAKE_UP ((uint32_t)0x00010000)
  40. #define RTC_TIMEOUT_INIT ((uint32_t)0x00010000)
  41. #define RTC_TIMEOUT_ALARM ((uint32_t)0x00010000)
  42. #define RTC_TIMEOUT_CALIB ((uint32_t)0x00020000)
  43. #define RTC_TIMEOUT_SYNCHRO ((uint32_t)0x00020000)
  44. #define RTC_TIMEOUT_SHIFT ((uint32_t)0x00001000)
  45. /**@} end of group RTC_Macros*/
  46. /** @defgroup RTC_Functions
  47. @{
  48. */
  49. static uint8_t RTC_ByteConBcd2(uint8_t val);
  50. static uint8_t RTC_Bcd2ConByte(uint8_t val);
  51. /*!
  52. * @brief Reset the RTC registers to their default values.
  53. *
  54. * @param None
  55. *
  56. * @retval SUCCESS or ERROR
  57. */
  58. uint8_t RTC_Reset(void)
  59. {
  60. __IO uint32_t timeout = 0x00;
  61. RTC_DisableWriteProtection();
  62. if (RTC_EnableInit() == ERROR)
  63. {
  64. RTC_EnableWriteProtection();
  65. return ERROR;
  66. }
  67. else
  68. {
  69. /* Reset register */
  70. RTC->TIME = (uint32_t)0x00000000;
  71. RTC->DATE = (uint32_t)0x00002101;
  72. RTC->CTRL &= (uint32_t)0x00000007;
  73. /* Wait for wakeup timer write flag */
  74. while ((RTC->STS_B.WUTWFLG != BIT_SET) && (timeout != RTC_TIMEOUT_WAKE_UP))
  75. {
  76. timeout++;
  77. }
  78. if (RTC->STS_B.WUTWFLG == BIT_RESET)
  79. {
  80. RTC_EnableWriteProtection();
  81. return ERROR;
  82. }
  83. else
  84. {
  85. /* Reset register */
  86. RTC->CTRL = (uint32_t)0x00000000;
  87. RTC->AUTORLD = (uint32_t)0x0000FFFF;
  88. RTC->PSC = (uint32_t)0x007F00FF;
  89. RTC->DCAL = (uint32_t)0x00000000;
  90. RTC->ALRMA = (uint32_t)0x00000000;
  91. RTC->ALRMB = (uint32_t)0x00000000;
  92. RTC->SHIFT = (uint32_t)0x00000000;
  93. RTC->CAL = (uint32_t)0x00000000;
  94. RTC->ALRMASS = (uint32_t)0x00000000;
  95. RTC->ALRMBSS = (uint32_t)0x00000000;
  96. RTC->STS = (uint32_t)0x00000000;
  97. RTC->TACFG = (uint32_t)0x00000000;
  98. if (RTC_WaitForSynchro() == ERROR)
  99. {
  100. RTC_EnableWriteProtection();
  101. return ERROR;
  102. }
  103. else
  104. {
  105. RTC_EnableWriteProtection();
  106. return SUCCESS;
  107. }
  108. }
  109. }
  110. }
  111. /*!
  112. * @brief Deinitializes the RTC registers to their default reset values
  113. *
  114. * @param rtcConfig : pointer to a RTC_Config_T structure which will be initialized
  115. *
  116. * @retval SUCCESS or ERROR
  117. */
  118. uint8_t RTC_Config(RTC_Config_T *rtcConfig)
  119. {
  120. RTC_DisableWriteProtection();
  121. if (RTC_EnableInit() == ERROR)
  122. {
  123. RTC_EnableWriteProtection();
  124. return ERROR;
  125. }
  126. else
  127. {
  128. RTC->CTRL_B.TIMEFCFG = (rtcConfig->format);
  129. RTC->PSC_B.SPSC = (rtcConfig->synchPrediv);
  130. RTC->PSC_B.APSC = (rtcConfig->asynchPrediv);
  131. RTC_DisableInit();
  132. RTC_EnableWriteProtection();
  133. return SUCCESS;
  134. }
  135. }
  136. /*!
  137. * @brief Fills each RTC_ConfigStruct member with its default value
  138. *
  139. * @param rtcConfig : pointer to a RTC_Config_T structure which will be initialized
  140. *
  141. * @retval None
  142. */
  143. void RTC_ConfigStructInit(RTC_Config_T *rtcConfig)
  144. {
  145. rtcConfig->format = RTC_HOURFORMAT_24;
  146. rtcConfig->asynchPrediv = (uint32_t)0x7F;
  147. rtcConfig->synchPrediv = (uint32_t)0xFF;
  148. }
  149. /*!
  150. * @brief Enable the write protection for RTC registers
  151. *
  152. * @param None
  153. *
  154. * @retval None
  155. */
  156. void RTC_EnableWriteProtection(void)
  157. {
  158. RTC->WRPROT = 0xFF;
  159. }
  160. /*!
  161. * @brief Disable the write protection for RTC registers
  162. *
  163. * @param None
  164. *
  165. * @retval None
  166. */
  167. void RTC_DisableWriteProtection(void)
  168. {
  169. RTC->WRPROT = 0xCA;
  170. RTC->WRPROT = 0x53;
  171. }
  172. /*!
  173. * @brief Enable the RTC Initialization mode.
  174. *
  175. * @param None
  176. *
  177. * @retval SUCCESS or ERROR
  178. */
  179. uint8_t RTC_EnableInit(void)
  180. {
  181. __IO uint32_t timeout = 0x00;
  182. if (RTC->STS_B.RINITFLG == BIT_RESET)
  183. {
  184. /* Enable initialization mode */
  185. RTC->STS_B.INITEN = BIT_SET;
  186. /* Wait for initialization flag */
  187. while ((RTC->STS_B.RINITFLG != BIT_SET) && (timeout != RTC_TIMEOUT_INIT))
  188. {
  189. timeout++;
  190. }
  191. if (RTC->STS_B.RINITFLG != BIT_SET)
  192. {
  193. return ERROR;
  194. }
  195. }
  196. return SUCCESS;
  197. }
  198. /*!
  199. * @brief Disable the RTC Initialization mode.
  200. *
  201. * @param None
  202. *
  203. * @retval None
  204. */
  205. void RTC_DisableInit(void)
  206. {
  207. RTC->STS_B.INITEN = BIT_RESET;
  208. }
  209. /*!
  210. * @brief Waits until the RTC Time and Date registers are synchronized
  211. * with RTC APB clock
  212. *
  213. * @param None
  214. *
  215. * @retval SUCCESS or ERROR
  216. */
  217. uint8_t RTC_WaitForSynchro(void)
  218. {
  219. __IO uint32_t timeout = 0;
  220. RTC_DisableWriteProtection();
  221. RTC->STS &= (uint32_t)0xFFFFFF5F;
  222. /* Wait for synchrocnt flag */
  223. while ((RTC->STS_B.RSFLG != BIT_SET) && (timeout != RTC_TIMEOUT_SYNCHRO))
  224. {
  225. timeout++;
  226. }
  227. if (RTC->STS_B.RSFLG != BIT_RESET)
  228. {
  229. RTC_EnableWriteProtection();
  230. return SUCCESS;
  231. }
  232. else
  233. {
  234. RTC_EnableWriteProtection();
  235. return ERROR;
  236. }
  237. }
  238. /*!
  239. * @brief Enables the RTC reference clock detection.
  240. *
  241. * @param None
  242. *
  243. * @retval SUCCESS or ERROR
  244. */
  245. uint8_t RTC_EnableRefClock(void)
  246. {
  247. RTC_DisableWriteProtection();
  248. if (RTC_EnableInit() == ERROR)
  249. {
  250. RTC_EnableWriteProtection();
  251. return ERROR;
  252. }
  253. else
  254. {
  255. RTC->CTRL_B.RCLKDEN = BIT_SET;
  256. RTC_DisableInit();
  257. RTC_EnableWriteProtection();
  258. return SUCCESS;
  259. }
  260. }
  261. /*!
  262. * @brief Disable the RTC reference clock detection
  263. *
  264. * @param None
  265. *
  266. * @retval SUCCESS or ERROR
  267. */
  268. uint8_t RTC_DisableRefClock(void)
  269. {
  270. RTC_DisableWriteProtection();
  271. if (RTC_EnableInit() == ERROR)
  272. {
  273. RTC_EnableWriteProtection();
  274. return ERROR;
  275. }
  276. else
  277. {
  278. RTC->CTRL_B.RCLKDEN = BIT_RESET;
  279. RTC_DisableInit();
  280. RTC_EnableWriteProtection();
  281. return SUCCESS;
  282. }
  283. }
  284. /*!
  285. * @brief Enable the Bypass Shadow feature.
  286. *
  287. * @param None
  288. *
  289. * @retval None
  290. */
  291. void RTC_EnableBypassShadow(void)
  292. {
  293. RTC_DisableWriteProtection();
  294. RTC->CTRL_B.RCMCFG = BIT_SET;
  295. RTC_EnableWriteProtection();
  296. }
  297. /*!
  298. * @brief Disable the Bypass Shadow feature.
  299. *
  300. * @param None
  301. *
  302. * @retval None
  303. */
  304. void RTC_DisableBypassShadow(void)
  305. {
  306. RTC_DisableWriteProtection();
  307. RTC->CTRL_B.RCMCFG = BIT_RESET;
  308. RTC_EnableWriteProtection();
  309. }
  310. /*!
  311. * @brief Config the RTC current time
  312. *
  313. * @param format: specifies the format to write
  314. * This parameter can be one of the following values:
  315. * @arg RTC_FORMAT_BIN : Format in Bin
  316. * @arg RTC_FORMAT_BCD : Format in BCD
  317. *
  318. * @param timeConfig: Pointer to a RTC_TimeConfig_T structure that
  319. * contains the configuration information for the RTC peripheral
  320. *
  321. * @retval SUCCESS or ERROR
  322. */
  323. uint8_t RTC_ConfigTime(RTC_FORMAT_T format, RTC_TimeConfig_T *timeConfig)
  324. {
  325. uint8_t state = ERROR;
  326. uint32_t temp = 0;
  327. if (RTC->CTRL_B.TIMEFCFG == BIT_RESET)
  328. {
  329. timeConfig->h12 = RTC_H12_AM;
  330. }
  331. /* Combine parameters of time */
  332. if (format != RTC_FORMAT_BIN)
  333. {
  334. temp = (((uint32_t)(timeConfig->hours) << 16) | \
  335. ((uint32_t)(timeConfig->minutes) << 8) | \
  336. ((uint32_t)(timeConfig->seconds)) | \
  337. ((uint32_t)(timeConfig->h12) << 22));
  338. }
  339. else
  340. {
  341. temp = (uint32_t)(((uint32_t)RTC_ByteConBcd2(timeConfig->hours) << 16) | \
  342. ((uint32_t)RTC_ByteConBcd2(timeConfig->minutes) << 8) | \
  343. ((uint32_t)RTC_ByteConBcd2(timeConfig->seconds)) | \
  344. (((uint32_t)(timeConfig->h12) << 22)));
  345. }
  346. RTC_DisableWriteProtection();
  347. if (RTC_EnableInit() == ERROR)
  348. {
  349. state = ERROR;
  350. }
  351. else
  352. {
  353. RTC->TIME = (uint32_t)(temp & 0x007F7F7F);
  354. RTC_DisableInit();
  355. if (RTC->CTRL_B.RCMCFG == RESET)
  356. {
  357. if (RTC_WaitForSynchro() == ERROR)
  358. {
  359. state = ERROR;
  360. }
  361. else
  362. {
  363. state = SUCCESS;
  364. }
  365. }
  366. else
  367. {
  368. state = SUCCESS;
  369. }
  370. }
  371. RTC_EnableWriteProtection();
  372. return state;
  373. }
  374. /*!
  375. * @brief Fills each timeConfig member with its default value
  376. *
  377. * @param timeConfig: Pointer to a RTC_TimeConfig_T structure that
  378. * contains the configuration information for the RTC peripheral
  379. *
  380. * @retval None
  381. */
  382. void RTC_ConfigTimeStructInit(RTC_TimeConfig_T *timeConfig)
  383. {
  384. timeConfig->hours = 0;
  385. timeConfig->minutes = 0;
  386. timeConfig->seconds = 0;
  387. timeConfig->h12 = RTC_H12_AM;
  388. }
  389. /*!
  390. * @brief Read the RTC current Time
  391. *
  392. * @param format: specifies the format to write
  393. * This parameter can be one of the following values:
  394. * @arg RTC_FORMAT_BIN : format in Bin
  395. * @arg RTC_FORMAT_BCD : format in BCD
  396. *
  397. * @param time: Pointer to a RTC_TimeConfig_T structure that
  398. * contains the configuration information for the RTC peripheral
  399. *
  400. * @retval None
  401. */
  402. void RTC_ReadTime(RTC_FORMAT_T format, RTC_TimeConfig_T *time)
  403. {
  404. uint32_t temp = 0;
  405. temp = (uint32_t)((RTC->TIME) & 0x007F7F7F);
  406. time->h12 = (RTC_H12_T)((temp & 0x00400000) >> 22);
  407. time->hours = (uint8_t)((temp & 0x003F0000) >> 16);
  408. time->minutes = (uint8_t)((temp & 0x00007F00) >> 8);
  409. time->seconds = (uint8_t)(temp & 0x0000007F);
  410. if (format == RTC_FORMAT_BIN)
  411. {
  412. time->hours = (uint8_t)RTC_Bcd2ConByte(time->hours);
  413. time->minutes = (uint8_t)RTC_Bcd2ConByte(time->minutes);
  414. time->seconds = (uint8_t)RTC_Bcd2ConByte(time->seconds);
  415. }
  416. }
  417. /*!
  418. * @brief Read the RTC current Calendar Subseconds value
  419. *
  420. * @param None
  421. *
  422. * @retval RTC current Calendar Subseconds value
  423. */
  424. uint32_t RTC_ReadSubSecond(void)
  425. {
  426. uint32_t temp = 0;
  427. temp = (uint32_t)(RTC->SUBSEC);
  428. (void)(RTC->DATE);
  429. return temp;
  430. }
  431. /*!
  432. * @brief Config the RTC current time
  433. *
  434. * @param format: specifies the format to write
  435. * This parameter can be one of the following values:
  436. * @arg RTC_FORMAT_BIN : format in Bin
  437. * @arg RTC_FORMAT_BCD : format in BCD
  438. *
  439. * @param dateConfig: Pointer to a RTC_DateConfig_T structure that
  440. * contains the configuration DATE information for the RTC peripheral
  441. *
  442. * @retval None
  443. */
  444. uint8_t RTC_ConfigDate(RTC_FORMAT_T format, RTC_DateConfig_T *dateConfig)
  445. {
  446. uint8_t state = ERROR;
  447. uint32_t temp = 0;
  448. if ((format == RTC_FORMAT_BIN) && ((dateConfig->month & 0x10) == 0x10))
  449. {
  450. dateConfig->month = (RTC_MONTH_T)((dateConfig->month & (uint32_t)~(0x10)) + 0x0A);
  451. }
  452. if (format != RTC_FORMAT_BIN)
  453. {
  454. temp = (((uint32_t)(dateConfig->year) << 16) | \
  455. ((uint32_t)(dateConfig->month) << 8) | \
  456. ((uint32_t)(dateConfig->date)) | \
  457. ((uint32_t)(dateConfig->weekday) << 13));
  458. }
  459. else
  460. {
  461. temp = (((uint32_t)RTC_ByteConBcd2(dateConfig->year) << 16) | \
  462. ((uint32_t)RTC_ByteConBcd2(dateConfig->month) << 8) | \
  463. ((uint32_t)RTC_ByteConBcd2(dateConfig->date)) | \
  464. ((uint32_t)(dateConfig->weekday) << 13));
  465. }
  466. RTC_DisableWriteProtection();
  467. if (RTC_EnableInit() == ERROR)
  468. {
  469. state = ERROR;
  470. }
  471. else
  472. {
  473. RTC->DATE = (uint32_t)(temp & 0x00FFFF3F);
  474. RTC_DisableInit();
  475. if (RTC->CTRL_B.RCMCFG == RESET)
  476. {
  477. if (RTC_WaitForSynchro() == ERROR)
  478. {
  479. state = ERROR;
  480. }
  481. else
  482. {
  483. state = SUCCESS;
  484. }
  485. }
  486. else
  487. {
  488. state = SUCCESS;
  489. }
  490. }
  491. RTC_EnableWriteProtection();
  492. return state;
  493. }
  494. /*!
  495. * @brief Fills each dateConfig member with its default value
  496. *
  497. * @param dateConfig: Pointer to a RTC_DateConfig_T structure that contains
  498. * the configuration DATE information for the RTC peripheral
  499. *
  500. * @retval None
  501. */
  502. void RTC_ConfigDateStructInit(RTC_DateConfig_T *dateConfig)
  503. {
  504. dateConfig->weekday = RTC_WEEKDAY_MONDAY;
  505. dateConfig->month = RTC_MONTH_JANUARY;
  506. dateConfig->date = 0x01;
  507. dateConfig->year = 0x00;
  508. }
  509. /*!
  510. * @brief Read the RTC current date
  511. *
  512. * @param format: specifies the format to write
  513. * This parameter can be one of the following values:
  514. * @arg RTC_FORMAT_BIN : format in Bin
  515. * @arg RTC_FORMAT_BCD : format in BCD
  516. *
  517. * @param date: Pointer to a RTC_DateConfig_T structure that contains the
  518. * configuration DATE information for the RTC peripheral.
  519. *
  520. * @retval None
  521. */
  522. void RTC_ReadDate(RTC_FORMAT_T format, RTC_DateConfig_T *date)
  523. {
  524. uint32_t temp = 0;
  525. temp = (uint32_t)((RTC->DATE) & 0x00FFFF3F);
  526. date->year = (uint8_t)((temp & 0x00FF0000) >> 16);
  527. date->month = (RTC_MONTH_T)((temp & 0x00001F00) >> 8);
  528. date->date = (uint8_t)(temp & 0x0000003F);
  529. date->weekday = (RTC_WEEKDAY_T)((temp & 0x0000E000) >> 13);
  530. if (format == RTC_FORMAT_BIN)
  531. {
  532. date->year = (uint8_t)RTC_Bcd2ConByte(date->year);
  533. date->month = (RTC_MONTH_T)RTC_Bcd2ConByte(date->month);
  534. date->date = (uint8_t)RTC_Bcd2ConByte(date->date);
  535. date->weekday = (RTC_WEEKDAY_T)(date->weekday);
  536. }
  537. }
  538. /*!
  539. * @brief Config the specified RTC alarm
  540. *
  541. * @param format: specifies the format to write
  542. * This parameter can be one of the following values:
  543. * @arg RTC_FORMAT_BIN : format in Bin
  544. * @arg RTC_FORMAT_BCD : format in BCD
  545. *
  546. * @param alarm: specifies the format to write
  547. * This parameter can be one of the following values:
  548. * @arg RTC_ALARM_A : to select Alarm A
  549. * @arg RTC_ALARM_B : to select Alarm B
  550. *
  551. * @param alarmConfig: Pointer to a RTC_AlarmConfig_T structure that
  552. * contains the configuration ALRMA information for the RTC peripheral
  553. *
  554. * @retva None
  555. */
  556. void RTC_ConfigAlarm(RTC_FORMAT_T format, RTC_ALARM_T alarm, RTC_AlarmConfig_T *alarmConfig)
  557. {
  558. uint32_t temp = 0;
  559. if (RTC->CTRL_B.TIMEFCFG == BIT_RESET)
  560. {
  561. alarmConfig->time.h12 = RTC_H12_AM;
  562. }
  563. if (format == RTC_FORMAT_BCD)
  564. {
  565. temp = (((uint32_t)(alarmConfig->time.hours) << 16) | \
  566. ((uint32_t)(alarmConfig->time.minutes) << 8) | \
  567. ((uint32_t)alarmConfig->time.seconds) | \
  568. ((uint32_t)(alarmConfig->time.h12) << 22) | \
  569. ((uint32_t)(alarmConfig->alarmDateWeekDay) << 24) | \
  570. ((uint32_t)alarmConfig->alarmDateWeekDaySel << 30) | \
  571. ((uint32_t)alarmConfig->alarmMask));
  572. }
  573. else
  574. {
  575. temp = (((uint32_t)RTC_ByteConBcd2(alarmConfig->time.hours) << 16) | \
  576. ((uint32_t)RTC_ByteConBcd2(alarmConfig->time.minutes) << 8) | \
  577. ((uint32_t)RTC_ByteConBcd2(alarmConfig->time.seconds)) | \
  578. ((uint32_t)(alarmConfig->time.h12) << 22) | \
  579. ((uint32_t)RTC_ByteConBcd2(alarmConfig->alarmDateWeekDay) << 24) | \
  580. ((uint32_t)alarmConfig->alarmDateWeekDaySel << 30) | \
  581. ((uint32_t)alarmConfig->alarmMask));
  582. }
  583. RTC_DisableWriteProtection();
  584. if (alarm == RTC_ALARM_A)
  585. {
  586. RTC->ALRMA = temp;
  587. }
  588. else
  589. {
  590. RTC->ALRMB = temp;
  591. }
  592. RTC_EnableWriteProtection();
  593. }
  594. /*!
  595. * @brief Fills each alarmConfig member with its default value.
  596. *
  597. * @param alarmConfig: Pointer to a RTC_AlarmConfig_T structure that
  598. * contains the configuration ALRMA information for the RTC peripheral
  599. *
  600. * @retval None
  601. */
  602. void RTC_ConfigAlarmStructInit(RTC_AlarmConfig_T *alarmConfig)
  603. {
  604. alarmConfig->time.hours = 0;
  605. alarmConfig->time.minutes = 0;
  606. alarmConfig->time.seconds = 0;
  607. alarmConfig->time.h12 = RTC_H12_AM;
  608. alarmConfig->alarmDateWeekDay = 1;
  609. alarmConfig->alarmDateWeekDaySel = RTC_WEEKDAY_SEL_DATE;
  610. alarmConfig->alarmMask = RTC_MASK_NONE;
  611. }
  612. /*!
  613. * @brief Read the RTC alarm value.
  614. *
  615. * @param format: specifies the format to write
  616. * This parameter can be one of the following values:
  617. * @arg RTC_FORMAT_BIN : format in Bin
  618. * @arg RTC_FORMAT_BCD : format in BCD
  619. *
  620. * @param alarm: specifies the format to write
  621. * This parameter can be one of the following values:
  622. * @arg RTC_ALARM_A : to select Alarm A
  623. * @arg RTC_ALARM_B : to select Alarm B
  624. *
  625. * @param alarmConfig: Pointer to a RTC_AlarmConfig_T structure that contains
  626. * the configuration ALRMA information for the RTC peripheral
  627. *
  628. * @retval None
  629. */
  630. void RTC_ReadAlarm(RTC_FORMAT_T format, RTC_ALARM_T alarm, RTC_AlarmConfig_T *alarmConfig)
  631. {
  632. uint8_t day_d, day_u, hours_d, hours_u, minutes_d, minutes_u, seconds_d, seconds_u;
  633. uint32_t day_mask, hours_mask, minutes_mask, seconds_mask;
  634. if (alarm == RTC_ALARM_A)
  635. {
  636. day_d = RTC->ALRMA_B.DAYT << 0x04;
  637. day_u = RTC->ALRMA_B.DAYU;
  638. hours_d = RTC->ALRMA_B.HRT << 0x04;
  639. hours_u = RTC->ALRMA_B.HRU;
  640. minutes_d = RTC->ALRMA_B.MINT << 0x04;
  641. minutes_u = RTC->ALRMA_B.MINU;
  642. seconds_d = RTC->ALRMA_B.SECT << 0x04;
  643. seconds_u = RTC->ALRMA_B.SECU;
  644. day_mask = RTC->ALRMA_B.DATEMEN << 8;
  645. hours_mask = RTC->ALRMA_B.HRMEN << 8;
  646. minutes_mask = RTC->ALRMA_B.MINMEN << 8;
  647. seconds_mask = RTC->ALRMA_B.SECMEN << 7;
  648. alarmConfig->time.hours = (uint8_t)(hours_d | hours_u);
  649. alarmConfig->time.minutes = (uint8_t)(minutes_d | minutes_u);
  650. alarmConfig->time.seconds = (uint8_t)(seconds_d | seconds_u);
  651. alarmConfig->time.h12 = (RTC_H12_T)(RTC->ALRMA_B.TIMEFCFG);
  652. alarmConfig->alarmDateWeekDay = (uint8_t)(day_d | day_u);
  653. alarmConfig->alarmDateWeekDaySel = (RTC_WEEKDAY_SEL_T)(RTC->ALRMA_B.WEEKSEL);
  654. alarmConfig->alarmMask = (uint32_t)(day_mask | hours_mask | minutes_mask | seconds_mask);
  655. }
  656. else
  657. {
  658. day_d = RTC->ALRMB_B.DAYT << 0x04;
  659. day_u = RTC->ALRMB_B.DAYU;
  660. hours_d = RTC->ALRMB_B.HRT << 0x04;
  661. hours_u = RTC->ALRMB_B.HRU;
  662. minutes_d = RTC->ALRMB_B.MINT << 0x04;
  663. minutes_u = RTC->ALRMB_B.MINU;
  664. seconds_d = RTC->ALRMB_B.SECT << 0x04;
  665. seconds_u = RTC->ALRMB_B.SECU;
  666. day_mask = RTC->ALRMB_B.DATEMEN << 8;
  667. hours_mask = RTC->ALRMB_B.HRMEN << 8;
  668. minutes_mask = RTC->ALRMB_B.MINMEN << 8;
  669. seconds_mask = RTC->ALRMB_B.SECMEN << 7;
  670. alarmConfig->time.hours = (uint8_t)(hours_d | hours_u);
  671. alarmConfig->time.minutes = (uint8_t)(minutes_d | minutes_u);
  672. alarmConfig->time.seconds = (uint8_t)(seconds_d | seconds_u);
  673. alarmConfig->time.h12 = (RTC_H12_T)(RTC->ALRMB_B.TIMEFCFG);
  674. alarmConfig->alarmDateWeekDay = (uint8_t)(day_d | day_u);
  675. alarmConfig->alarmDateWeekDaySel = (RTC_WEEKDAY_SEL_T)(RTC->ALRMB_B.WEEKSEL);
  676. alarmConfig->alarmMask = (uint32_t)(day_mask | hours_mask | minutes_mask | seconds_mask);
  677. }
  678. if (format == RTC_FORMAT_BIN)
  679. {
  680. alarmConfig->time.hours = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.hours);
  681. alarmConfig->time.minutes = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.minutes);
  682. alarmConfig->time.seconds = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.seconds);
  683. alarmConfig->alarmDateWeekDay = (uint8_t)RTC_Bcd2ConByte(alarmConfig->alarmDateWeekDay);
  684. }
  685. }
  686. /*!
  687. * @brief Enable the RTC Alarm A.
  688. *
  689. * @param None
  690. *
  691. * @retval None
  692. */
  693. void RTC_EnableAlarmA(void)
  694. {
  695. RTC_DisableWriteProtection();
  696. RTC->CTRL_B.ALRAEN = BIT_SET;
  697. RTC_EnableWriteProtection();
  698. }
  699. /*!
  700. * @brief Disable the the RTC Alarm A.
  701. *
  702. * @param None
  703. *
  704. * @retval None
  705. */
  706. uint8_t RTC_DisableAlarmA(void)
  707. {
  708. __IO uint32_t timeout = 0x00;
  709. RTC_DisableWriteProtection();
  710. RTC->CTRL_B.ALRAEN = BIT_RESET;
  711. /* wait for Alarm A write flag */
  712. while (((RTC->STS_B.ALRAWFLG) != BIT_SET) && (timeout != RTC_TIMEOUT_ALARM))
  713. {
  714. timeout++;
  715. }
  716. if ((RTC->STS_B.ALRAWFLG) == BIT_RESET)
  717. {
  718. RTC_EnableWriteProtection();
  719. return ERROR;
  720. }
  721. else
  722. {
  723. RTC_EnableWriteProtection();
  724. return SUCCESS;
  725. }
  726. }
  727. /*!
  728. * @brief Enable the RTC Alarm B.
  729. *
  730. * @param None
  731. *
  732. * @retval None
  733. */
  734. void RTC_EnableAlarmB(void)
  735. {
  736. RTC_DisableWriteProtection();
  737. RTC->CTRL_B.ALRBEN = BIT_SET;
  738. RTC_EnableWriteProtection();
  739. }
  740. /*!
  741. * @brief Disable the the RTC Alarm B.
  742. *
  743. * @param None
  744. *
  745. * @retval None
  746. */
  747. uint8_t RTC_DisableAlarmB(void)
  748. {
  749. __IO uint32_t timeout = 0x00;
  750. RTC_DisableWriteProtection();
  751. RTC->CTRL_B.ALRBEN = BIT_RESET;
  752. /* wait for Alarm B write flag */
  753. while (((RTC->STS_B.ALRBWFLG) != BIT_SET) && (timeout != RTC_TIMEOUT_ALARM))
  754. {
  755. timeout++;
  756. }
  757. if ((RTC->STS_B.ALRBWFLG) == BIT_RESET)
  758. {
  759. RTC_EnableWriteProtection();
  760. return ERROR;
  761. }
  762. else
  763. {
  764. RTC_EnableWriteProtection();
  765. return SUCCESS;
  766. }
  767. }
  768. /*!
  769. * @brief Read the RTC ALRMA Subseconds value
  770. *
  771. * @param val: specifies the value for ALRMA Sub Second
  772. * this value must less than 0x00007FFF
  773. *
  774. * @param format: specifies the format to write
  775. * This parameter can be one of the following values:
  776. * @arg RTC_ALARM_A : to select Alarm A
  777. * @arg RTC_ALARM_B : to select Alarm B
  778. *
  779. * @param mask: specifies the mask for ALRMA Sub Second.
  780. * This parameter can be one of the following values:
  781. * @arg RTC_ALARM_SUBSEC_MASK_ALL : All Alarm SUBSEC fields are masked.
  782. * @arg RTC_ALARM_SUBSEC_MASK_14_1 : Mask SUBSEC[14:1]
  783. * @arg RTC_ALARM_SUBSEC_MASK_14_2 : Mask SUBSEC[14:2]
  784. * @arg RTC_ALARM_SUBSEC_MASK_14_3 : Mask SUBSEC[14:3]
  785. * @arg RTC_ALARM_SUBSEC_MASK_14_4 : Mask SUBSEC[14:4]
  786. * @arg RTC_ALARM_SUBSEC_MASK_14_5 : Mask SUBSEC[14:5]
  787. * @arg RTC_ALARM_SUBSEC_MASK_14_6 : Mask SUBSEC[14:6]
  788. * @arg RTC_ALARM_SUBSEC_MASK_14_7 : Mask SUBSEC[14:7]
  789. * @arg RTC_ALARM_SUBSEC_MASK_14_8 : Mask SUBSEC[14:8]
  790. * @arg RTC_ALARM_SUBSEC_MASK_14_9 : Mask SUBSEC[14:9]
  791. * @arg RTC_ALARM_SUBSEC_MASK_14_10 : Mask SUBSEC[14:10]
  792. * @arg RTC_ALARM_SUBSEC_MASK_14_11 : Mask SUBSEC[14:11]
  793. * @arg RTC_ALARM_SUBSEC_MASK_14_12 : Mask SUBSEC[14:12]
  794. * @arg RTC_ALARM_SUBSEC_MASK_14_13 : Mask SUBSEC[14:13]
  795. * @arg RTC_ALARM_SUBSEC_MASK_14 : Mask SUBSEC[14]
  796. * @arg RTC_ALARM_SUBSEC_MASK_NONE : Alarm comparison is all the SUBSEC bit.
  797. *
  798. * this value must less than 0x0f
  799. *
  800. * @retval None
  801. */
  802. void RTC_ConfigAlarmSubSecond(RTC_ALARM_T alarm, uint32_t val, RTC_ALARM_SUBSEC_MASK_T mask)
  803. {
  804. RTC_DisableWriteProtection();
  805. if (alarm == RTC_ALARM_A)
  806. {
  807. RTC->ALRMASS_B.SUBSEC = val;
  808. RTC->ALRMASS_B.MASKSEL = mask;
  809. }
  810. else
  811. {
  812. RTC->ALRMBSS_B.SUBSEC = val;
  813. RTC->ALRMBSS_B.MASKSEL = mask;
  814. }
  815. RTC_EnableWriteProtection();
  816. }
  817. /*!
  818. * @brief Read the RTC Alarm Subseconds value
  819. *
  820. * @param format: specifies the format to write
  821. * This parameter can be one of the following values:
  822. * @arg RTC_ALARM_A : select Alarm A
  823. * @arg RTC_ALARM_B : select Alarm B
  824. *
  825. * @retval RTC ALRM Subseconds value
  826. */
  827. uint16_t RTC_ReadAlarmSubSecond(RTC_ALARM_T alarm)
  828. {
  829. if (alarm == RTC_ALARM_A)
  830. {
  831. return (uint16_t)(RTC->ALRMASS_B.SUBSEC);
  832. }
  833. else
  834. {
  835. return (uint16_t)(RTC->ALRMBSS_B.SUBSEC);
  836. }
  837. }
  838. /*!
  839. * @brief Configure the RTC Wakeup clock source.
  840. *
  841. * @param wakeUpClock: specifies the Wakeup Clock source.
  842. * This parameter can be one of the following values:
  843. * @arg RTC_WAKEUP_CLOCK_RTC_DIV16 : Wakeup Clock Select to RTC/16
  844. * @arg RTC_WAKEUP_CLOCK_RTC_DIV8 : Wakeup Clock Select to RTC/8
  845. * @arg RTC_WAKEUP_CLOCK_RTC_DIV4 : Wakeup Clock Select to RTC/4
  846. * @arg RTC_WAKEUP_CLOCK_RTC_DIV2 : Wakeup Clock Select to RTC/2
  847. * @arg RTC_WAKEUP_CLOCK_CK_SPRE_16B : Wakeup Clock Select to clk_spre
  848. * @arg RTC_WAKEUP_CLOCK_CK_SPRE_17B : Wakeup Clock Select to clk_spre
  849. *
  850. * @retval None
  851. */
  852. void RTC_ConfigWakeUpClock(RTC_WAKEUP_CLOCK_T wakeUpClock)
  853. {
  854. RTC_DisableWriteProtection();
  855. RTC->CTRL_B.WUCLKSEL = wakeUpClock;
  856. RTC_EnableWriteProtection();
  857. }
  858. /*!
  859. * @brief Configure the RTC Wakeup counter value.
  860. *
  861. * @param wakeUpValue: a 16-bit Wakeup auto-reload value.
  862. *
  863. * @retval None
  864. */
  865. void RTC_ConfigWakeUpValue(uint16_t wakeUpValue)
  866. {
  867. RTC_DisableWriteProtection();
  868. RTC->AUTORLD = (uint32_t)wakeUpValue;
  869. RTC_EnableWriteProtection();
  870. }
  871. /*!
  872. * @brief Read the RTC Wakeup auto-reload value.
  873. *
  874. * @param None
  875. *
  876. * @retval The RTC WakeUp auto-reload value.
  877. */
  878. uint16_t RTC_ReadWakeUpValue(void)
  879. {
  880. return (uint16_t)RTC->AUTORLD;
  881. }
  882. /*!
  883. * @brief Enable the RTC WakeUp timer.
  884. *
  885. * @param None
  886. *
  887. * @retval None
  888. */
  889. void RTC_EnableWakeUp(void)
  890. {
  891. RTC_DisableWriteProtection();
  892. RTC->CTRL_B.WUTEN = BIT_SET;
  893. RTC_EnableWriteProtection();
  894. }
  895. /*!
  896. * @brief Disable the RTC WakeUp timer.
  897. *
  898. * @param None
  899. *
  900. * @retval None
  901. */
  902. uint8_t RTC_DisableWakeUp(void)
  903. {
  904. __IO uint32_t timeout = 0x00;
  905. RTC_DisableWriteProtection();
  906. RTC->CTRL_B.WUTEN = BIT_RESET;
  907. /* wait for wakeup timer write flag */
  908. while (((RTC->STS_B.WUTWFLG) == BIT_RESET) && (timeout != RTC_TIMEOUT_WAKE_UP))
  909. {
  910. timeout++;
  911. }
  912. if ((RTC->STS_B.WUTWFLG) == BIT_RESET)
  913. {
  914. RTC_EnableWriteProtection();
  915. return ERROR;
  916. }
  917. else
  918. {
  919. RTC_EnableWriteProtection();
  920. return SUCCESS;
  921. }
  922. }
  923. /*!
  924. * @brief Adds or substract one hour from the current time
  925. *
  926. * @param saving: specifies the DayLight Saving.
  927. * This parameter can be one of the following values:
  928. * @arg RTC_DLS_SUB_1H : Winter time change
  929. * @arg RTC_DLS_ADD_1H : Summer time change
  930. *
  931. * @param bit: set the Backup Value.
  932. * This parameter can be one of the following values:
  933. * @arg RTC_BACKUP_RESET : Reset backup value
  934. * @arg RTC_BACKUP_SET : Set backup value
  935. *
  936. * @retval None
  937. */
  938. void RTC_ConfigDayLightSaving(RTC_DLS_T saving, RTC_BACKUP_T bit)
  939. {
  940. RTC_DisableWriteProtection();
  941. if (saving == RTC_DLS_ADD_1H)
  942. {
  943. RTC->CTRL_B.STCCFG = BIT_SET;
  944. }
  945. else
  946. {
  947. RTC->CTRL_B.WTCCFG = BIT_SET;
  948. }
  949. /* Backup Value Setup */
  950. RTC->CTRL_B.BAKP = bit;
  951. RTC_EnableWriteProtection();
  952. }
  953. /*!
  954. * @brief Returns the RTC Day Light Saving stored operation
  955. *
  956. * @param None
  957. *
  958. * @retval RTC Day Light Saving backup stored operation
  959. */
  960. uint8_t RTC_ReadStoreOperation(void)
  961. {
  962. return (uint8_t)RTC->CTRL_B.BAKP;
  963. }
  964. /*!
  965. * @brief Config the RTC alarm Output Way and output polarity.
  966. *
  967. * @param outputSel: specifies RTC alarm Output Way.
  968. * This parameter can be one of the following values:
  969. * @arg RTC_OUT_SEL_DISABLE : Disable RTC output
  970. * @arg RTC_OUT_SEL_ALARM_A : Select alarm A as RTC output
  971. * @arg RTC_OUT_SEL_ALARM_B : Select alarm B as RTC output
  972. * @arg RTC_OUT_SEL_WAKEUP: : Select wake up as RTC output
  973. *
  974. * @param polarity: specified the output polarity.
  975. * This parameter can be one of the following values:
  976. * @arg RTC_OUT_POLARITY_HIGH : The output polarity is high
  977. * @arg RTC_OUT_POLARITY_LOW : The output polarity is low
  978. *
  979. * @retval None
  980. */
  981. void RTC_ConfigOutput(RTC_OUT_SEL_T outputSel, RTC_OUT_POLARITY_T polarity)
  982. {
  983. RTC_DisableWriteProtection();
  984. RTC->CTRL_B.OUTSEL = outputSel;
  985. RTC->CTRL_B.POLCFG = polarity;
  986. RTC_EnableWriteProtection();
  987. }
  988. /*!
  989. * @brief Config RTC Coarse calibration.
  990. *
  991. * @param calibSign: specifies the sign of the coarse calibration value.
  992. * This parameter can be one of the following values:
  993. * @arg RTC_CALIB_SIGN_POSITIVE : The Calibration sign is positive
  994. * @arg RTC_CALIB_SIGN_NEGATIVE : The Calibration sign is negative
  995. *
  996. * @param value: a 5-bit value of coarse calibration expressed in ppm.
  997. *
  998. * @note For positive sign, the Calibration value could be between 0 and 126
  999. * with a 4-ppm step.
  1000. *
  1001. * @note For negative sign, the Calibration value could be between 0 and 63
  1002. * with a 2-ppm step.
  1003. *
  1004. * @retval SUCCESS or ERROR
  1005. */
  1006. uint8_t RTC_ConfigCoarseCalib(RTC_CALIB_SIGN_T calibSign, uint32_t value)
  1007. {
  1008. RTC_DisableWriteProtection();
  1009. if (RTC_EnableInit() == ERROR)
  1010. {
  1011. RTC_EnableWriteProtection();
  1012. return ERROR;
  1013. }
  1014. else
  1015. {
  1016. RTC->DCAL_B.DCALCFG = calibSign;
  1017. RTC->DCAL_B.DCAL = value;
  1018. RTC_DisableInit();
  1019. RTC_EnableWriteProtection();
  1020. return SUCCESS;
  1021. }
  1022. }
  1023. /*!
  1024. * @brief Enables the Coarse calibration.
  1025. *
  1026. * @param None
  1027. *
  1028. * @retval SUCCESS or ERROR
  1029. */
  1030. uint8_t RTC_EnableCoarseCalib(void)
  1031. {
  1032. RTC_DisableWriteProtection();
  1033. if (RTC_EnableInit() == ERROR)
  1034. {
  1035. RTC_EnableWriteProtection();
  1036. return ERROR;
  1037. }
  1038. else
  1039. {
  1040. RTC->CTRL_B.DCALEN = BIT_SET;
  1041. RTC_DisableInit();
  1042. RTC_EnableWriteProtection();
  1043. return SUCCESS;
  1044. }
  1045. }
  1046. /*!
  1047. * @brief Disables the Coarse calibration.
  1048. *
  1049. * @param None
  1050. *
  1051. * @retval SUCCESS or ERROR
  1052. */
  1053. uint8_t RTC_DisableCoarseCalib(void)
  1054. {
  1055. RTC_DisableWriteProtection();
  1056. if (RTC_EnableInit() == ERROR)
  1057. {
  1058. RTC_EnableWriteProtection();
  1059. return ERROR;
  1060. }
  1061. else
  1062. {
  1063. RTC->CTRL_B.DCALEN = BIT_RESET;
  1064. RTC_DisableInit();
  1065. RTC_EnableWriteProtection();
  1066. return SUCCESS;
  1067. }
  1068. }
  1069. /*!
  1070. * @brief Enable the RTC clock Calibration Output.
  1071. *
  1072. * @param None
  1073. *
  1074. * @retval None
  1075. */
  1076. void RTC_EnableCalibOutput(void)
  1077. {
  1078. RTC_DisableWriteProtection();
  1079. RTC->CTRL_B.CALOEN = BIT_SET;
  1080. RTC_EnableWriteProtection();
  1081. }
  1082. /*!
  1083. * @brief Disable the RTC clock Calibration Output.
  1084. *
  1085. * @param None
  1086. *
  1087. * @retval None
  1088. */
  1089. void RTC_DisableCalibOutput(void)
  1090. {
  1091. RTC_DisableWriteProtection();
  1092. RTC->CTRL_B.CALOEN = BIT_RESET;
  1093. RTC_EnableWriteProtection();
  1094. }
  1095. /*!
  1096. * @brief Config the Calibration output.
  1097. *
  1098. * @param calib: Select the Calibration output.
  1099. * This parameter can be one of the following values:
  1100. * @arg RTC_CALIB_OUTPUT_512HZ : Calibration output is 512 Hz
  1101. * @arg RTC_CALIB_OUTPUT_1HZ : Calibration output is 1 Hz
  1102. *
  1103. * @retval None
  1104. */
  1105. void RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_T calib)
  1106. {
  1107. RTC_DisableWriteProtection();
  1108. RTC->CTRL_B.CALOSEL = calib;
  1109. RTC_EnableWriteProtection();
  1110. }
  1111. /*!
  1112. * @brief Config the Synchronization Shift Control Settings.
  1113. *
  1114. * @param period: Select the Smooth Calibration period.
  1115. * This parameter can be can be one of the following values:
  1116. * @arg RTC_SCP_32SEC : The smooth calibration periode is 32s.
  1117. * @arg RTC_SCP_16SEC : The smooth calibration periode is 16s.
  1118. * @arg RTC_SCP_8SEC : The smooth calibartion periode is 8s.
  1119. *
  1120. * @param calibPulse: Select to Set or reset the CALP bit.
  1121. * This parameter can be one of the following values:
  1122. * @arg RTC_SCPP_RESET : Add one RTCCLK puls every 2**11 pulses.
  1123. * @arg RTC_SCPP_SET : No RTCCLK pulses are added.
  1124. *
  1125. * @param value: a 9-bits value to Config RECALF[8:0].
  1126. *
  1127. * @retval SUCCESS or ERROR
  1128. */
  1129. uint8_t RTC_ConfigSmoothCalib(RTC_SCP_T period, RTC_SCPP_T calibPulse, uint16_t value)
  1130. {
  1131. uint8_t state = ERROR;
  1132. uint32_t timeout = 0;
  1133. RTC_DisableWriteProtection();
  1134. /* wait for Recalibration flag to be cleared */
  1135. while ((RTC->STS_B.RCALPFLG != BIT_RESET) && (timeout != RTC_TIMEOUT_CALIB))
  1136. {
  1137. timeout++;
  1138. }
  1139. if (RTC->STS_B.RCALPFLG != BIT_SET)
  1140. {
  1141. RTC->CAL = ((uint32_t)calibPulse << 15) | ((uint32_t)period << 13) |
  1142. (uint32_t)(value & 0x01FF);
  1143. state = SUCCESS;
  1144. }
  1145. else
  1146. {
  1147. state = ERROR;
  1148. }
  1149. RTC_EnableWriteProtection();
  1150. return state;
  1151. }
  1152. /*!
  1153. * @brief Enables the RTC TimeStamp functionality and config the Timestamp event
  1154. * active edge.
  1155. *
  1156. * @param edge: Specifies the Timestamp event active edge.
  1157. * This paramete can be one of the following:
  1158. * @arg RTC_TIMESTAMP_EDGE_RISING : Rising edge generates a timestamp event.
  1159. * @arg RTC_TIMESTAMP_EDGE_FALLING : Falling edge generates a timestamp event
  1160. *
  1161. * @retval None
  1162. */
  1163. void RTC_EnableTimeStamp(RTC_TIMESTAMP_EDGE_T edge)
  1164. {
  1165. RTC_DisableWriteProtection();
  1166. /* Disable Timestamp when change the edge */
  1167. RTC->CTRL_B.TSEN = BIT_RESET;
  1168. RTC->CTRL_B.TSETECFG = edge;
  1169. RTC->CTRL_B.TSEN = BIT_SET;
  1170. RTC_EnableWriteProtection();
  1171. }
  1172. /*!
  1173. * @brief Disables the RTC TimeStamp functionality.
  1174. *
  1175. * @param None
  1176. *
  1177. * @retval None
  1178. */
  1179. void RTC_DisableTimeStamp(void)
  1180. {
  1181. RTC_DisableWriteProtection();
  1182. RTC->CTRL_B.TSEN = BIT_RESET;
  1183. RTC_EnableWriteProtection();
  1184. }
  1185. /*!
  1186. * @brief Read the RTC TimeStamp value and masks
  1187. *
  1188. * @param format: specifies the format of the output parameters.
  1189. * This parameter can be one of the following values:
  1190. * @arg RTC_FORMAT_BIN : data in Binary format
  1191. * @arg RTC_FORMAT_BCD : data in BCD format
  1192. *
  1193. * @param time: pointer to a RTC_TimeConfig_T structure that will
  1194. * contains the TimeStamp time values.
  1195. *
  1196. * @param date: pointer to a RTC_DateConfig_T structure that will
  1197. * contains the TimeStamp date values.
  1198. *
  1199. * @retval None
  1200. */
  1201. void RTC_ReadTimeDate(RTC_FORMAT_T format, RTC_TimeConfig_T *time, RTC_DateConfig_T *date)
  1202. {
  1203. uint32_t temptime = 0, tempdate = 0;
  1204. temptime = (uint32_t)((RTC->TSTIME) & 0x007F7F7F);
  1205. tempdate = (uint32_t)((RTC->TSDATE) & 0x00FFFF3F);
  1206. /* Read the time in BCD format */
  1207. time->hours = (uint8_t)((temptime & 0x003F0000) >> 16);
  1208. time->minutes = (uint8_t)((temptime & 0x00007F00) >> 8);
  1209. time->seconds = (uint8_t)(temptime & 0x0000007F);
  1210. time->h12 = (RTC_H12_T)((temptime & 0x00400000) >> 22);
  1211. /* Read the date in BCD format */
  1212. date->year = 0;
  1213. date->month = (RTC_MONTH_T)((tempdate & 0x00001F00) >> 8);
  1214. date->date = (uint8_t)(tempdate & 0x0000003F);
  1215. date->weekday = (RTC_WEEKDAY_T)((tempdate & 0x0000E000) >> 13);
  1216. /* Binary format */
  1217. if (format == RTC_FORMAT_BIN)
  1218. {
  1219. time->hours = (uint8_t)RTC_Bcd2ConByte(time->hours);
  1220. time->minutes = (uint8_t)RTC_Bcd2ConByte(time->minutes);
  1221. time->seconds = (uint8_t)RTC_Bcd2ConByte(time->seconds);
  1222. date->month = (RTC_MONTH_T)RTC_Bcd2ConByte(date->month);
  1223. date->date = (uint8_t)RTC_Bcd2ConByte(date->date);
  1224. date->weekday = (RTC_WEEKDAY_T)(date->weekday);
  1225. }
  1226. }
  1227. /*!
  1228. * @brief Get the RTC timestamp Subseconds value
  1229. *
  1230. * @param None
  1231. *
  1232. * @retval RTC current timestamp Subseconds value
  1233. */
  1234. uint16_t RTC_ReadTimeStampSubSecond(void)
  1235. {
  1236. return RTC->TSSUBSEC_B.SUBSEC;
  1237. }
  1238. /*!
  1239. * @brief Config the select Tamper pin edge
  1240. *
  1241. * @param tamper: Selected tamper pin.
  1242. * This parameter can be one of the following values:
  1243. * @arg RTC_TAMPER_1 : Select Tamper 1.
  1244. * @arg RTC_TAMPER_2 : Select Tamper 2.
  1245. *
  1246. * @param trigger: Specifies the trigger on the tamper pin that stimulates tamper event.
  1247. * This parameter can be one of the following values:
  1248. * @arg RTC_TAMPER_TRIGGER_EDGE_RISING : Rising Edge of the tamper pin causes tamper event.
  1249. * @arg RTC_TAMPER_TRIGGER_EDGE_FALLING : Falling Edge of the tamper pin causes tamper event.
  1250. * @arg RTC_TAMPER_TRIGGER_LEVEL_LOW : Low Level of the tamper pin causes tamper event.
  1251. * @arg RTC_TAMPER_TRIGGER_LEVEL_HIGH : High Level of the tamper pin causes tamper event.
  1252. *
  1253. * @retval None
  1254. */
  1255. void RTC_ConfigTamperTrigger(RTC_TAMPER_T tamper, RTC_TAMPER_TRIGGER_T trigger)
  1256. {
  1257. if (tamper == RTC_TAMPER_1)
  1258. {
  1259. RTC->TACFG_B.TP1ALCFG = trigger;
  1260. }
  1261. else
  1262. {
  1263. RTC->TACFG_B.TP2ALCFG = trigger;
  1264. }
  1265. }
  1266. /*!
  1267. * @brief Enables the Tamper detection
  1268. *
  1269. * @param tamper: Selected tamper pin.
  1270. * This parameter can be one of the following values:
  1271. * @arg RTC_TAMPER_1 : Select Tamper 1.
  1272. * @arg RTC_TAMPER_2 : Select Tamper 2.
  1273. *
  1274. * @retval None
  1275. */
  1276. void RTC_EnableTamper(RTC_TAMPER_T tamper)
  1277. {
  1278. if (tamper == RTC_TAMPER_1)
  1279. {
  1280. RTC->TACFG_B.TP1EN = BIT_SET;
  1281. }
  1282. else
  1283. {
  1284. RTC->TACFG_B.TP2EN = BIT_SET;
  1285. }
  1286. }
  1287. /*!
  1288. * @brief Disables the Tamper detection
  1289. *
  1290. * @param tamper: Selected tamper pin.
  1291. * This parameter can be any combination of the following values:
  1292. * @arg RTC_TAMPER_1 : Select Tamper 1.
  1293. * @arg RTC_TAMPER_2 : Select Tamper 2.
  1294. *
  1295. * @retval None
  1296. */
  1297. void RTC_DisableTamper(RTC_TAMPER_T tamper)
  1298. {
  1299. if (tamper == RTC_TAMPER_1)
  1300. {
  1301. RTC->TACFG_B.TP1EN = BIT_RESET;
  1302. }
  1303. else
  1304. {
  1305. RTC->TACFG_B.TP2EN = BIT_RESET;
  1306. }
  1307. }
  1308. /*!
  1309. * @brief Config the Tampers Filter
  1310. *
  1311. * @param filter: Specifies the tampers filter.
  1312. * This parameter can be one of the following values:
  1313. * @arg RTC_TAMPER_FILTER_DISABLE : Tamper filter is disabled.
  1314. * @arg RTC_TAMPER_FILTER_2SAMPLE : Tamper is activated after 2 consecutive samples at the active level
  1315. * @arg RTC_TAMPER_FILTER_4SAMPLE : Tamper is activated after 4 consecutive samples at the active level
  1316. * @arg RTC_TAMPER_FILTER_8SAMPLE : Tamper is activated after 8 consecutive samples at the active level
  1317. *
  1318. * @retval None
  1319. */
  1320. void RTC_ConfigFilter(RTC_TAMPER_FILTER_T filter)
  1321. {
  1322. RTC->TACFG_B.TPFCSEL = filter;
  1323. }
  1324. /*!
  1325. * @brief Config the Tampers Sampling Frequency
  1326. *
  1327. * @param freq: Specifies the tampers Sampling Frequency.
  1328. * This parameter can be one of the following values:
  1329. * @arg RTC_TSF_DIV_32768 : Tampers Sampling Frequency = RTC_CLK/32768
  1330. * @arg RTC_TSF_DIV_16384 : Tampers Sampling Frequency = RTC_CLK/16384
  1331. * @arg RTC_TSF_DIV_8192 : Tampers Sampling Frequency = RTC_CLK/8192
  1332. * @arg RTC_TSF_DIV_4096 : Tampers Sampling Frequency = RTC_CLK/4096
  1333. * @arg RTC_TSF_DIV_2048 : Tampers Sampling Frequency = RTC_CLK/2048
  1334. * @arg RTC_TSF_DIV_1024 : Tampers Sampling Frequency = RTC_CLK/1024
  1335. * @arg RTC_TSF_DIV_512 : Tampers Sampling Frequency = RTC_CLK/512
  1336. * @arg RTC_TSF_DIV_256 : Tampers Sampling Frequency = RTC_CLK/256
  1337. *
  1338. * @retval None
  1339. */
  1340. void RTC_ConfigSamplingFreq(RTC_TSF_DIV_T freq)
  1341. {
  1342. RTC->TACFG_B.TPSFSEL = freq;
  1343. }
  1344. /*!
  1345. * @brief Config the Precharge Duration.
  1346. *
  1347. * @param duration: Specifies the Tampers Pins input Precharge Duration.
  1348. * This parameter can be one of the following values:
  1349. * @arg RTC_TPD_RTCCLK_1 : Tamper pins are pre-charged before sampling during 1 RTCCLK cycle.
  1350. * @arg RTC_TPD_RTCCLK_2 : Tamper pins are pre-charged before sampling during 2 RTCCLK cycle.
  1351. * @arg RTC_TPD_RTCCLK_4 : Tamper pins are pre-charged before sampling during 4 RTCCLK cycle.
  1352. * @arg RTC_TPD_RTCCLK_8 : Tamper pins are pre-charged before sampling during 8 RTCCLK cycle.
  1353. *
  1354. * @retval None
  1355. */
  1356. void RTC_PinsPrechargeDuration(RTC_TPD_RTCCLK_T duration)
  1357. {
  1358. RTC->TACFG_B.TPPRDUSEL = duration;
  1359. }
  1360. /*!
  1361. * @brief Enables the TimeStamp on Tamper Detection Event
  1362. *
  1363. * @param None
  1364. *
  1365. * @retval None
  1366. */
  1367. void RTC_EnableTDE(void)
  1368. {
  1369. RTC->TACFG_B.TPTSEN = BIT_SET;
  1370. }
  1371. /*!
  1372. * @brief Disable the TimeStamp on Tamper Detection Event
  1373. *
  1374. * @param None
  1375. *
  1376. * @retval None
  1377. */
  1378. void RTC_DisableTDE(void)
  1379. {
  1380. RTC->TACFG_B.TPTSEN = BIT_RESET;
  1381. }
  1382. /*!
  1383. * @brief Enable pull-up resistors to precharge of the selected Tamper pin.
  1384. *
  1385. * @param None
  1386. *
  1387. * @retval None
  1388. */
  1389. void RTC_EnablePullUp(void)
  1390. {
  1391. /* Enable precharge */
  1392. RTC->TACFG_B.TPPUDIS = BIT_RESET;
  1393. }
  1394. /*!
  1395. * @brief Disable pull-up resistors to precharge of the selected Tamper pin.
  1396. *
  1397. * @param None
  1398. *
  1399. * @retval None
  1400. */
  1401. void RTC_DisablePullUp(void)
  1402. {
  1403. /* Disable precharge */
  1404. RTC->TACFG_B.TPPUDIS = BIT_SET;
  1405. }
  1406. /*!
  1407. * @brief Writes a data in RTC Backup data.
  1408. *
  1409. * @param backup: RTC Backup data Register number.
  1410. * This parameter can be RTC_BAKP_REG_x where x can be from 0 to 19.
  1411. *
  1412. * @param data: Data to be written in the specified RTC Backup data register.
  1413. *
  1414. * @retval None
  1415. */
  1416. void RTC_WriteBackup(RTC_BAKP_REG_T backupReg, uint32_t data)
  1417. {
  1418. RTC->BAKP[backupReg] = (uint32_t)data;
  1419. }
  1420. /*!
  1421. * @brief Reads a data in RTC Backup data.
  1422. *
  1423. * @param backup: RTC Backup data Register number.
  1424. * This parameter can be RTC_BAKP_REG_x where x can be from 0 to 19.
  1425. *
  1426. * @retval None
  1427. */
  1428. uint32_t RTC_ReadBackup(RTC_BAKP_REG_T backupReg)
  1429. {
  1430. return RTC->BAKP[backupReg];
  1431. }
  1432. /*!
  1433. * @brief Config Tamper Pin.
  1434. *
  1435. * @param tamperPin: specifies the RTC Tamper Pin.
  1436. * This parameter can be one of the following values:
  1437. * @arg RTC_TAMPER_PIN_AF1 : RTC_AF1(PC13) is used as RTC Tamper Pin.
  1438. * @arg RTC_TAMPER_PIN_AF2 : RTC_AF2(PI8) is used as RTC Tamper Pin.
  1439. *
  1440. * @retval None
  1441. */
  1442. void RTC_ConfigTamperPin(RTC_TAMPER_PIN_T tamperPin)
  1443. {
  1444. RTC->TACFG_B.TP1MSEL = tamperPin;
  1445. }
  1446. /*!
  1447. * @brief Config the RTC TimeStamp Pin.
  1448. *
  1449. * @param timeStampPin: specifies the RTC TimeStamp Pin.
  1450. * This parameter can be one of the following values:
  1451. * @arg RTC_TIMESTAMP_PIN_AF1 : RTC_AF1(PC13) is used as RTC TimeStamp Pin.
  1452. * @arg RTC_TIMESTAMP_PIN_AF2 : RTC_AF2(PI8) is used as RTC TimeStamp Pin.
  1453. *
  1454. * @retval None
  1455. */
  1456. void RTC_ConfigTimeStampPin(RTC_TIMESTAMP_PIN_T timeStampPin)
  1457. {
  1458. RTC->TACFG_B.TSMSEL = timeStampPin;
  1459. }
  1460. /*!
  1461. * @brief Config the RTC Output Pin mode
  1462. *
  1463. * @param outputType: specifies the RTC Output (PC13) pin mode.
  1464. * This parameter can be one of the following values:
  1465. * @arg RTC_OUTPUT_OD : RTC_ALARM(PC13) output Open-drain
  1466. * @arg RTC_OUTPUT_PP : RTC_ALARM(PC13) output Push-pull
  1467. *
  1468. * @retval None
  1469. */
  1470. void RTC_ConfigOutputType(RTC_OUTPUT_T outputType)
  1471. {
  1472. RTC->TACFG_B.ALRMOT = outputType;
  1473. }
  1474. /*!
  1475. * @brief Config the Synchronization Shift Control Settings.
  1476. *
  1477. * @param add1S: Select to add or not 1 second to the time Calendar.
  1478. * This parameter can be one of the following values :
  1479. * @arg RTC_SHIFT_ADD1S_RESET: No effect.
  1480. * @arg RTC_SHIFT_ADD1S_SET : Add one second to the clock calendar.
  1481. *
  1482. * @param subFS: Select the number of Second Fractions to Substitute.
  1483. * This parameter can be one any value from 0 to 0x7FFF.
  1484. *
  1485. * @retval SUCCESS or ERROR
  1486. */
  1487. uint8_t RTC_ConfigSynchroShift(RTC_SHIFT_ADD1S_T add1S, uint16_t subFS)
  1488. {
  1489. uint32_t timeout = 0;
  1490. RTC_DisableWriteProtection();
  1491. /* wait for Shift operation flag to be cleared */
  1492. while ((RTC->STS_B.SOPFLG != BIT_RESET) && (timeout != RTC_TIMEOUT_SHIFT))
  1493. {
  1494. timeout++;
  1495. }
  1496. /* check if Shift Operation flag is cleared and Reference Clock Detection is disabled */
  1497. if ((RTC->STS_B.SOPFLG != BIT_SET) && (RTC->CTRL_B.RCLKDEN != BIT_SET))
  1498. {
  1499. RTC->SHIFT = ((uint32_t)add1S << 31) | (uint32_t)subFS;
  1500. if (RTC_WaitForSynchro() != ERROR)
  1501. {
  1502. RTC_EnableWriteProtection();
  1503. return SUCCESS;
  1504. }
  1505. }
  1506. RTC_EnableWriteProtection();
  1507. return ERROR;
  1508. }
  1509. /*!
  1510. * @brief Enable RTC interrupts.
  1511. *
  1512. * @param interrupt: specifies the RTC interrupt sources to be enabled
  1513. * This parameter can be any combination of the following values:
  1514. * @arg RTC_INT_ALRA : Enable ALRMA A interrupt
  1515. * @arg RTC_INT_ALRB : Enable ALRMA B interrupt
  1516. * @arg RTC_INT_WT : Enable WakeUp Timer interrupt
  1517. * @arg RTC_INT_TS : Enable Time Stamp interrupt
  1518. * @arg RTC_INT_TAMP : Enable Tamper event interrupt
  1519. *
  1520. * @retval None
  1521. */
  1522. void RTC_EnableInterrupt(uint32_t interrupt)
  1523. {
  1524. RTC_DisableWriteProtection();
  1525. RTC->CTRL |= (uint32_t)(interrupt & ~RTC_INT_TAMP);
  1526. RTC->TACFG |= (uint32_t)(interrupt & RTC_INT_TAMP);
  1527. RTC_EnableWriteProtection();
  1528. }
  1529. /*!
  1530. * @brief Disable RTC interrupts.
  1531. *
  1532. * @param interrupt: specifies the RTC interrupt sources to be disable
  1533. * This parameter can be any combination of the following values:
  1534. * @arg RTC_INT_ALRA : Disable ALRMA A interrupt
  1535. * @arg RTC_INT_ALRB : Disable ALRMA B interrupt
  1536. * @arg RTC_INT_WT : Disable WakeUp Timer interrupt
  1537. * @arg RTC_INT_TS : Disable Time Stamp interrupt
  1538. * @arg RTC_INT_TAMP : Disable Tamper event interrupt
  1539. *
  1540. * @retval None
  1541. */
  1542. void RTC_DisableInterrupt(uint32_t interrupt)
  1543. {
  1544. RTC_DisableWriteProtection();
  1545. RTC->CTRL &= (uint32_t)~(interrupt & ~RTC_INT_TAMP);
  1546. RTC->TACFG &= (uint32_t)~(interrupt & RTC_INT_TAMP);
  1547. RTC_EnableWriteProtection();
  1548. }
  1549. /*!
  1550. * @brief Read the specified RTC flag.
  1551. *
  1552. * @param flag: specifies the flag to check.
  1553. * This parameter can be one of the following values:
  1554. * @arg RTC_FLAG_AAWF : Alarm A Write Flag
  1555. * @arg RTC_FLAG_ABWF : Alarm B Write Flag
  1556. * @arg RTC_FLAG_WTWF : Wakeup Timer Write Flag
  1557. * @arg RTC_FLAG_SOPF : Shift Operation Pending Flag
  1558. * @arg RTC_FLAG_ISF : Initialization State Flag
  1559. * @arg RTC_FLAG_RSF : Registers Synchronization Flag
  1560. * @arg RTC_FLAG_INTF : Register Initialization Flag
  1561. * @arg RTC_FLAG_ALRAF: Alarm A Match Flag
  1562. * @arg RTC_FLAG_ALRBF: Alarm B Match Flag
  1563. * @arg RTC_FLAG_WTF : Wakeup Timer Flag
  1564. * @arg RTC_FLAG_TSF : Time Stamp Flag
  1565. * @arg RTC_FLAG_TSOF : Time Stamp Overflow Flag
  1566. * @arg RTC_FLAG_TP1F : Tamper 1 event Detection Flag
  1567. * @arg RTC_FLAG_TP2F : Tamper 2 event Detection Flag
  1568. * @arg RTC_FLAG_RPF : Recalibration Pending Flag
  1569. *
  1570. * @retval SET or RESET.
  1571. */
  1572. uint8_t RTC_ReadStatusFlag(RTC_FLAG_T flag)
  1573. {
  1574. return (RTC->STS & flag) ? SET : RESET;
  1575. }
  1576. /*!
  1577. * @brief Clears the RTC's status flags.
  1578. * @param flag: specifies the RTC flag to clear.
  1579. * This parameter can be any combination of the following values:
  1580. * @arg RTC_FLAG_RSF : Registers Synchronization Flag
  1581. * @arg RTC_FLAG_ALRAF : Alarm A Match Flag
  1582. * @arg RTC_FLAG_ALRBF : Alarm B Match Flag
  1583. * @arg RTC_FLAG_WTF : Wakeup Timer Flag
  1584. * @arg RTC_FLAG_TSF : Time Stamp Flag
  1585. * @arg RTC_FLAG_TSOF : Time Stamp Overflow Flag
  1586. * @arg RTC_FLAG_TP1F : Tamper 1 event Detection Flag
  1587. * @arg RTC_FLAG_TP2F : Tamper 2 event Detection Flag
  1588. */
  1589. void RTC_ClearStatusFlag(uint32_t flag)
  1590. {
  1591. RTC->STS &= (uint32_t)~flag;
  1592. }
  1593. /*!
  1594. * @brief Read interrupt flag bit is set
  1595. *
  1596. * @param flag: specifies the flag to read.
  1597. * This parameter can be one of the following values:
  1598. * @arg RTC_INT_FLAG_ALRA : Alarm A interrupt
  1599. * @arg RTC_INT_FLAG_ALRB : Alarm B interrupt
  1600. * @arg RTC_INT_FLAG_WT : WakeUp Timer interrupt
  1601. * @arg RTC_INT_FLAG_TS : Time Stamp interrupt
  1602. * @arg RTC_INT_FLAG_TAMP1 : Tamper1 event interrupt
  1603. * @arg RTC_INT_FLAG_TAMP2 : Tamper2 event interrupt
  1604. *
  1605. * @retval The new state of flag (SET or RESET).
  1606. */
  1607. uint8_t RTC_ReadIntFlag(RTC_INT_FLAG_T flag)
  1608. {
  1609. uint8_t intEnable;
  1610. uint32_t intStatus;
  1611. if (flag & 0x01)
  1612. {
  1613. intEnable = RTC->TACFG_B.TPIEN;
  1614. intStatus = (uint32_t)(RTC->STS & (flag >> 4));
  1615. }
  1616. else
  1617. {
  1618. intEnable = (RTC->CTRL & flag) ? SET : RESET;
  1619. intStatus = (uint32_t)(RTC->STS & (flag >> 4));
  1620. }
  1621. if (intEnable && intStatus)
  1622. {
  1623. return SET;
  1624. }
  1625. return RESET;
  1626. }
  1627. /*!
  1628. * @brief Clear RTC interrupt flag bit
  1629. *
  1630. * @param flag: specifies the flag to clear.
  1631. * This parameter can be any combination the following values:
  1632. * @arg RTC_INT_FLAG_ALRA : ALRMA interrupt
  1633. * @arg RTC_INT_FLAG_ALRB : ALRMB interrupt
  1634. * @arg RTC_INT_FLAG_TS : Time Stamp interrupt
  1635. * @arg RTC_INT_FLAG_WT : WakeUp Timer interrupt
  1636. * @arg RTC_INT_FLAG_TAMP1 : Tamper1 event interrupt
  1637. * @arg RTC_INT_FLAG_TAMP2 : Tamper2 event interrupt
  1638. *
  1639. * @retval SET or RESET
  1640. */
  1641. void RTC_ClearIntFlag(uint32_t flag)
  1642. {
  1643. RTC->STS &= (uint32_t) ~(flag >> 4);
  1644. }
  1645. /*!
  1646. * @brief Converts a 2 digit decimal to BCD format
  1647. *
  1648. * @param val: Byte to be converted
  1649. *
  1650. * @retval Converted byte
  1651. */
  1652. static uint8_t RTC_ByteConBcd2(uint8_t val)
  1653. {
  1654. uint8_t bcdhigh = 0;
  1655. while (val >= 10)
  1656. {
  1657. bcdhigh++;
  1658. val -= 10;
  1659. }
  1660. return ((uint8_t)(bcdhigh << 4) | val);
  1661. }
  1662. /*!
  1663. * @brief Convert from 2 digit BCD to Binary
  1664. *
  1665. * @param val: BCD value to be converted
  1666. *
  1667. * @retval Converted word
  1668. */
  1669. static uint8_t RTC_Bcd2ConByte(uint8_t val)
  1670. {
  1671. uint8_t tmp = 0;
  1672. tmp = ((uint8_t)(val & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1673. return (tmp + (val & (uint8_t)0x0F));
  1674. }
  1675. /**@} end of group RTC_Functions */
  1676. /**@} end of group RTC_Driver */
  1677. /**@} end of group APM32F4xx_StdPeriphDriver */