ft32f0xx_rtc.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_rtc.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Real-Time Clock (RTC) peripheral:
  7. * + Initialization
  8. * + Calendar (Time and Date) configuration
  9. * + Alarms (Alarm A) configuration
  10. * + Daylight Saving configuration
  11. * + Output pin Configuration
  12. * + Digital Calibration configuration
  13. * + TimeStamp configuration
  14. * + Tampers configuration
  15. * + Backup Data Registers configuration
  16. * + Output Type Config configuration
  17. * + Shift control synchronisation
  18. * + Interrupts and flags management
  19. * @version V1.0.0
  20. * @data 2021-07-01
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "ft32f0xx_rtc.h"
  25. /* Masks Definition */
  26. #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
  27. #define RTC_DR_RESERVED_MASK ((uint32_t)0x00FFFF3F)
  28. #define RTC_INIT_MASK ((uint32_t)0xFFFFFFFF)
  29. #define RTC_RSF_MASK ((uint32_t)0xFFFFFF5F)
  30. #define RTC_FLAGS_MASK ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_ALRAF | \
  31. RTC_FLAG_RSF | RTC_FLAG_INITS |RTC_FLAG_INITF | \
  32. RTC_FLAG_TAMP1F | RTC_FLAG_TAMP2F | RTC_FLAG_RECALPF | \
  33. RTC_FLAG_SHPF))
  34. #define INITMODE_TIMEOUT ((uint32_t) 0x00004000)
  35. #define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
  36. #define RECALPF_TIMEOUT ((uint32_t) 0x00001000)
  37. #define SHPF_TIMEOUT ((uint32_t) 0x00001000)
  38. static uint8_t RTC_ByteToBcd2(uint8_t Value);
  39. static uint8_t RTC_Bcd2ToByte(uint8_t Value);
  40. /**
  41. * @brief Deinitializes the RTC registers to their default reset values.
  42. * @note This function doesn't reset the RTC Clock source and RTC Backup Data
  43. * registers.
  44. * @param None
  45. * @retval An ErrorStatus enumeration value:
  46. * - SUCCESS: RTC registers are deinitialized
  47. * - ERROR: RTC registers are not deinitialized
  48. */
  49. ErrorStatus RTC_DeInit(void)
  50. {
  51. ErrorStatus status = ERROR;
  52. /* Disable the write protection for RTC registers */
  53. RTC->WPR = 0xCA;
  54. RTC->WPR = 0x53;
  55. /* Set Initialization mode */
  56. if (RTC_EnterInitMode() == ERROR)
  57. {
  58. status = ERROR;
  59. }
  60. else
  61. {
  62. /* Reset TR, DR and CR registers */
  63. RTC->TR = (uint32_t)0x00000000;
  64. RTC->DR = (uint32_t)0x00002101;
  65. RTC->CR &= (uint32_t)0x00000000;
  66. RTC->PRER = (uint32_t)0x007F00FF;
  67. RTC->ALRMAR = (uint32_t)0x00000000;
  68. RTC->SHIFTR = (uint32_t)0x00000000;
  69. RTC->CALR = (uint32_t)0x00000000;
  70. RTC->ALRMASSR = (uint32_t)0x00000000;
  71. /* Reset ISR register and exit initialization mode */
  72. RTC->ISR = (uint32_t)0x00000000;
  73. /* Reset Tamper and alternate functions configuration register */
  74. RTC->TAFCR = 0x00000000;
  75. /* Wait till the RTC RSF flag is set */
  76. if (RTC_WaitForSynchro() == ERROR)
  77. {
  78. status = ERROR;
  79. }
  80. else
  81. {
  82. status = SUCCESS;
  83. }
  84. }
  85. /* Enable the write protection for RTC registers */
  86. RTC->WPR = 0xFF;
  87. return status;
  88. }
  89. /**
  90. * @brief Initializes the RTC registers according to the specified parameters
  91. * in RTC_InitStruct.
  92. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
  93. * the configuration information for the RTC peripheral.
  94. * @note The RTC Prescaler register is write protected and can be written in
  95. * initialization mode only.
  96. * @retval An ErrorStatus enumeration value:
  97. * - SUCCESS: RTC registers are initialized
  98. * - ERROR: RTC registers are not initialized
  99. */
  100. ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
  101. {
  102. ErrorStatus status = ERROR;
  103. /* Check the parameters */
  104. assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
  105. assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
  106. assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
  107. /* Disable the write protection for RTC registers */
  108. RTC->WPR = 0xCA;
  109. RTC->WPR = 0x53;
  110. /* Set Initialization mode */
  111. if (RTC_EnterInitMode() == ERROR)
  112. {
  113. status = ERROR;
  114. }
  115. else
  116. {
  117. /* Clear RTC CR FMT Bit */
  118. RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
  119. /* Set RTC_CR register */
  120. RTC->CR |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
  121. /* Configure the RTC PRER */
  122. RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
  123. RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
  124. /* Exit Initialization mode */
  125. RTC_ExitInitMode();
  126. status = SUCCESS;
  127. }
  128. /* Enable the write protection for RTC registers */
  129. RTC->WPR = 0xFF;
  130. return status;
  131. }
  132. /**
  133. * @brief Fills each RTC_InitStruct member with its default value.
  134. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
  135. * initialized.
  136. * @retval None
  137. */
  138. void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
  139. {
  140. /* Initialize the RTC_HourFormat member */
  141. RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
  142. /* Initialize the RTC_AsynchPrediv member */
  143. RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
  144. /* Initialize the RTC_SynchPrediv member */
  145. RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
  146. }
  147. /**
  148. * @brief Enables or disables the RTC registers write protection.
  149. * @note All the RTC registers are write protected except for RTC_ISR[13:8],
  150. * RTC_TAFCR and RTC_BKPxR.
  151. * @note Writing a wrong key reactivates the write protection.
  152. * @note The protection mechanism is not affected by system reset.
  153. * @param NewState: new state of the write protection.
  154. * This parameter can be: ENABLE or DISABLE.
  155. * @retval None
  156. */
  157. void RTC_WriteProtectionCmd(FunctionalState NewState)
  158. {
  159. /* Check the parameters */
  160. assert_param(IS_FUNCTIONAL_STATE(NewState));
  161. if (NewState != DISABLE)
  162. {
  163. /* Enable the write protection for RTC registers */
  164. RTC->WPR = 0xFF;
  165. }
  166. else
  167. {
  168. /* Disable the write protection for RTC registers */
  169. RTC->WPR = 0xCA;
  170. RTC->WPR = 0x53;
  171. }
  172. }
  173. /**
  174. * @brief Enters the RTC Initialization mode.
  175. * @note The RTC Initialization mode is write protected, use the
  176. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  177. * @param None
  178. * @retval An ErrorStatus enumeration value:
  179. * - SUCCESS: RTC is in Init mode
  180. * - ERROR: RTC is not in Init mode
  181. */
  182. ErrorStatus RTC_EnterInitMode(void)
  183. {
  184. __IO uint32_t initcounter = 0x00;
  185. ErrorStatus status = ERROR;
  186. uint32_t initstatus = 0x00;
  187. /* Check if the Initialization mode is set */
  188. if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  189. {
  190. /* Set the Initialization mode */
  191. RTC->ISR = (uint32_t)RTC_INIT_MASK;
  192. /* Wait till RTC is in INIT state and if Time out is reached exit */
  193. do
  194. {
  195. initstatus = RTC->ISR & RTC_ISR_INITF;
  196. initcounter++;
  197. } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  198. if ((RTC->ISR & RTC_ISR_INITF) != RESET)
  199. {
  200. status = SUCCESS;
  201. }
  202. else
  203. {
  204. status = ERROR;
  205. }
  206. }
  207. else
  208. {
  209. status = SUCCESS;
  210. }
  211. return (status);
  212. }
  213. /**
  214. * @brief Exits the RTC Initialization mode.
  215. * @note When the initialization sequence is complete, the calendar restarts
  216. * counting after 4 RTCCLK cycles.
  217. * @note The RTC Initialization mode is write protected, use the
  218. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  219. * @param None
  220. * @retval None
  221. */
  222. void RTC_ExitInitMode(void)
  223. {
  224. /* Exit Initialization mode */
  225. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  226. /*when BypassShadow is enable,this bit should wait to clear zero.edit:2020.5.23*/
  227. while((RTC->ISR & RTC_ISR_INITF) != RESET)
  228. {
  229. ;
  230. }
  231. }
  232. /**
  233. * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  234. * synchronized with RTC APB clock.
  235. * @note The RTC Resynchronization mode is write protected, use the
  236. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  237. * @note To read the calendar through the shadow registers after Calendar
  238. * initialization, calendar update or after wakeup from low power modes
  239. * the software must first clear the RSF flag.
  240. * The software must then wait until it is set again before reading
  241. * the calendar, which means that the calendar registers have been
  242. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  243. * @param None
  244. * @retval An ErrorStatus enumeration value:
  245. * - SUCCESS: RTC registers are synchronised
  246. * - ERROR: RTC registers are not synchronised
  247. */
  248. ErrorStatus RTC_WaitForSynchro(void)
  249. {
  250. __IO uint32_t synchrocounter = 0;
  251. ErrorStatus status = ERROR;
  252. uint32_t synchrostatus = 0x00;
  253. if ((RTC->CR & RTC_CR_BYPSHAD) != RESET)
  254. {
  255. /* Bypass shadow mode */
  256. status = SUCCESS;
  257. }
  258. else
  259. {
  260. /* Disable the write protection for RTC registers */
  261. RTC->WPR = 0xCA;
  262. RTC->WPR = 0x53;
  263. /* Clear RSF flag */
  264. RTC->ISR &= (uint32_t)RTC_RSF_MASK;
  265. /* Wait the registers to be synchronised */
  266. do
  267. {
  268. synchrostatus = RTC->ISR & RTC_ISR_RSF;
  269. synchrocounter++;
  270. } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
  271. if ((RTC->ISR & RTC_ISR_RSF) != RESET)
  272. {
  273. status = SUCCESS;
  274. }
  275. else
  276. {
  277. status = ERROR;
  278. }
  279. /* Disable the write protection for RTC registers */
  280. RTC->WPR = 0xFF;
  281. }
  282. return (status);
  283. }
  284. /**
  285. * @brief Enables or disables the RTC reference clock detection.
  286. * @param NewState: new state of the RTC reference clock.
  287. * This parameter can be: ENABLE or DISABLE.
  288. * @retval An ErrorStatus enumeration value:
  289. * - SUCCESS: RTC reference clock detection is enabled
  290. * - ERROR: RTC reference clock detection is disabled
  291. */
  292. ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
  293. {
  294. ErrorStatus status = ERROR;
  295. /* Check the parameters */
  296. assert_param(IS_FUNCTIONAL_STATE(NewState));
  297. /* Disable the write protection for RTC registers */
  298. RTC->WPR = 0xCA;
  299. RTC->WPR = 0x53;
  300. /* Set Initialization mode */
  301. if (RTC_EnterInitMode() == ERROR)
  302. {
  303. status = ERROR;
  304. }
  305. else
  306. {
  307. if (NewState != DISABLE)
  308. {
  309. /* Enable the RTC reference clock detection */
  310. RTC->CR |= RTC_CR_REFCKON;
  311. }
  312. else
  313. {
  314. /* Disable the RTC reference clock detection */
  315. RTC->CR &= ~RTC_CR_REFCKON;
  316. }
  317. /* Exit Initialization mode */
  318. RTC_ExitInitMode();
  319. status = SUCCESS;
  320. }
  321. /* Enable the write protection for RTC registers */
  322. RTC->WPR = 0xFF;
  323. return status;
  324. }
  325. /**
  326. * @brief Enables or Disables the Bypass Shadow feature.
  327. * @note When the Bypass Shadow is enabled the calendar value are taken
  328. * directly from the Calendar counter.
  329. * @param NewState: new state of the Bypass Shadow feature.
  330. * This parameter can be: ENABLE or DISABLE.
  331. * @retval None
  332. */
  333. void RTC_BypassShadowCmd(FunctionalState NewState)
  334. {
  335. /* Check the parameters */
  336. assert_param(IS_FUNCTIONAL_STATE(NewState));
  337. /* Disable the write protection for RTC registers */
  338. RTC->WPR = 0xCA;
  339. RTC->WPR = 0x53;
  340. if (NewState != DISABLE)
  341. {
  342. /* Set the BYPSHAD bit */
  343. RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
  344. }
  345. else
  346. {
  347. /* Reset the BYPSHAD bit */
  348. RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  349. }
  350. /* Enable the write protection for RTC registers */
  351. RTC->WPR = 0xFF;
  352. }
  353. /**
  354. * @}
  355. */
  356. /**
  357. * @brief Set the RTC current time.
  358. * @param RTC_Format: specifies the format of the entered parameters.
  359. * This parameter can be one of the following values:
  360. * @arg RTC_Format_BIN: Binary data format
  361. * @arg RTC_Format_BCD: BCD data format
  362. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains
  363. * the time configuration information for the RTC.
  364. * @retval An ErrorStatus enumeration value:
  365. * - SUCCESS: RTC Time register is configured
  366. * - ERROR: RTC Time register is not configured
  367. */
  368. ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
  369. {
  370. uint32_t tmpreg = 0;
  371. ErrorStatus status = ERROR;
  372. /* Check the parameters */
  373. assert_param(IS_RTC_FORMAT(RTC_Format));
  374. if (RTC_Format == RTC_Format_BIN)
  375. {
  376. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  377. {
  378. assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
  379. assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
  380. }
  381. else
  382. {
  383. RTC_TimeStruct->RTC_H12 = 0x00;
  384. assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
  385. }
  386. assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
  387. assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
  388. }
  389. else
  390. {
  391. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  392. {
  393. tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
  394. assert_param(IS_RTC_HOUR12(tmpreg));
  395. assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
  396. }
  397. else
  398. {
  399. RTC_TimeStruct->RTC_H12 = 0x00;
  400. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
  401. }
  402. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
  403. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
  404. }
  405. /* Check the input parameters format */
  406. if (RTC_Format != RTC_Format_BIN)
  407. {
  408. tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
  409. ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
  410. ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
  411. ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));
  412. }
  413. else
  414. {
  415. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
  416. ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
  417. ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
  418. (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
  419. }
  420. /* Disable the write protection for RTC registers */
  421. RTC->WPR = 0xCA;
  422. RTC->WPR = 0x53;
  423. /* Set Initialization mode */
  424. if (RTC_EnterInitMode() == ERROR)
  425. {
  426. status = ERROR;
  427. }
  428. else
  429. {
  430. /* Set the RTC_TR register */
  431. RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  432. /* Exit Initialization mode */
  433. RTC_ExitInitMode();
  434. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  435. if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
  436. {
  437. if (RTC_WaitForSynchro() == ERROR)
  438. {
  439. status = ERROR;
  440. }
  441. else
  442. {
  443. status = SUCCESS;
  444. }
  445. }
  446. else
  447. {
  448. status = SUCCESS;
  449. }
  450. }
  451. /* Enable the write protection for RTC registers */
  452. RTC->WPR = 0xFF;
  453. return status;
  454. }
  455. /**
  456. * @brief Fills each RTC_TimeStruct member with its default value
  457. * (Time = 00h:00min:00sec).
  458. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be
  459. * initialized.
  460. * @retval None
  461. */
  462. void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
  463. {
  464. /* Time = 00h:00min:00sec */
  465. RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
  466. RTC_TimeStruct->RTC_Hours = 0;
  467. RTC_TimeStruct->RTC_Minutes = 0;
  468. RTC_TimeStruct->RTC_Seconds = 0;
  469. }
  470. /**
  471. * @brief Get the RTC current Time.
  472. * @param RTC_Format: specifies the format of the returned parameters.
  473. * This parameter can be one of the following values:
  474. * @arg RTC_Format_BIN: Binary data format
  475. * @arg RTC_Format_BCD: BCD data format
  476. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will
  477. * contain the returned current time configuration.
  478. * @retval None
  479. */
  480. void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
  481. {
  482. uint32_t tmpreg = 0;
  483. /* Check the parameters */
  484. assert_param(IS_RTC_FORMAT(RTC_Format));
  485. /* Get the RTC_TR register */
  486. tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
  487. /* Fill the structure fields with the read parameters */
  488. RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  489. RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
  490. RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  491. RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
  492. /* Check the input parameters format */
  493. if (RTC_Format == RTC_Format_BIN)
  494. {
  495. /* Convert the structure parameters to Binary format */
  496. RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
  497. RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
  498. RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
  499. }
  500. }
  501. /**
  502. * @brief Gets the RTC current Calendar Subseconds value.
  503. * @note This function freeze the Time and Date registers after reading the
  504. * SSR register.
  505. * @param None
  506. * @retval RTC current Calendar Subseconds value.
  507. */
  508. uint32_t RTC_GetSubSecond(void)
  509. {
  510. uint32_t tmpreg = 0;
  511. /* Get subseconds values from the correspondent registers*/
  512. tmpreg = (uint32_t)(RTC->SSR);
  513. /* Read DR register to unfroze calendar registers */
  514. (void) (RTC->DR);
  515. return (tmpreg);
  516. }
  517. /**
  518. * @brief Set the RTC current date.
  519. * @param RTC_Format: specifies the format of the entered parameters.
  520. * This parameter can be one of the following values:
  521. * @arg RTC_Format_BIN: Binary data format
  522. * @arg RTC_Format_BCD: BCD data format
  523. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
  524. * the date configuration information for the RTC.
  525. * @retval An ErrorStatus enumeration value:
  526. * - SUCCESS: RTC Date register is configured
  527. * - ERROR: RTC Date register is not configured
  528. */
  529. ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
  530. {
  531. uint32_t tmpreg = 0;
  532. ErrorStatus status = ERROR;
  533. /* Check the parameters */
  534. assert_param(IS_RTC_FORMAT(RTC_Format));
  535. if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
  536. {
  537. RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
  538. }
  539. if (RTC_Format == RTC_Format_BIN)
  540. {
  541. assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
  542. assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
  543. assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
  544. }
  545. else
  546. {
  547. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
  548. tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
  549. assert_param(IS_RTC_MONTH(tmpreg));
  550. tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
  551. assert_param(IS_RTC_DATE(tmpreg));
  552. }
  553. assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
  554. /* Check the input parameters format */
  555. if (RTC_Format != RTC_Format_BIN)
  556. {
  557. tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
  558. (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
  559. ((uint32_t)RTC_DateStruct->RTC_Date) | \
  560. (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
  561. }
  562. else
  563. {
  564. tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
  565. ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
  566. ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
  567. ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
  568. }
  569. /* Disable the write protection for RTC registers */
  570. RTC->WPR = 0xCA;
  571. RTC->WPR = 0x53;
  572. /* Set Initialization mode */
  573. if (RTC_EnterInitMode() == ERROR)
  574. {
  575. status = ERROR;
  576. }
  577. else
  578. {
  579. /* Set the RTC_DR register */
  580. RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
  581. /* Exit Initialization mode */
  582. RTC_ExitInitMode();
  583. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  584. if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
  585. {
  586. if (RTC_WaitForSynchro() == ERROR)
  587. {
  588. status = ERROR;
  589. }
  590. else
  591. {
  592. status = SUCCESS;
  593. }
  594. }
  595. else
  596. {
  597. status = SUCCESS;
  598. }
  599. }
  600. /* Enable the write protection for RTC registers */
  601. RTC->WPR = 0xFF;
  602. return status;
  603. }
  604. /**
  605. * @brief Fills each RTC_DateStruct member with its default value
  606. * (Monday, January 01 xx00).
  607. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be
  608. * initialized.
  609. * @retval None
  610. */
  611. void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
  612. {
  613. /* Monday, January 01 xx00 */
  614. RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
  615. RTC_DateStruct->RTC_Date = 1;
  616. RTC_DateStruct->RTC_Month = RTC_Month_January;
  617. RTC_DateStruct->RTC_Year = 0;
  618. }
  619. /**
  620. * @brief Get the RTC current date.
  621. * @param RTC_Format: specifies the format of the returned parameters.
  622. * This parameter can be one of the following values:
  623. * @arg RTC_Format_BIN: Binary data format
  624. * @arg RTC_Format_BCD: BCD data format
  625. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will
  626. * contain the returned current date configuration.
  627. * @retval None
  628. */
  629. void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
  630. {
  631. uint32_t tmpreg = 0;
  632. /* Check the parameters */
  633. assert_param(IS_RTC_FORMAT(RTC_Format));
  634. /* Get the RTC_TR register */
  635. tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
  636. /* Fill the structure fields with the read parameters */
  637. RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
  638. RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  639. RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
  640. RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);
  641. /* Check the input parameters format */
  642. if (RTC_Format == RTC_Format_BIN)
  643. {
  644. /* Convert the structure parameters to Binary format */
  645. RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
  646. RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
  647. RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
  648. RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);
  649. }
  650. }
  651. /**
  652. * @}
  653. */
  654. /**
  655. * @brief Set the specified RTC Alarm.
  656. * @note The Alarm register can only be written when the corresponding Alarm
  657. * is disabled (Use the RTC_AlarmCmd(DISABLE)).
  658. * @param RTC_Format: specifies the format of the returned parameters.
  659. * This parameter can be one of the following values:
  660. * @arg RTC_Format_BIN: Binary data format
  661. * @arg RTC_Format_BCD: BCD data format
  662. * @param RTC_Alarm: specifies the alarm to be configured.
  663. * This parameter can be one of the following values:
  664. * @arg RTC_Alarm_A: to select Alarm A
  665. * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
  666. * contains the alarm configuration parameters.
  667. * @retval None
  668. */
  669. void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
  670. {
  671. uint32_t tmpreg = 0;
  672. /* Check the parameters */
  673. assert_param(IS_RTC_FORMAT(RTC_Format));
  674. assert_param(IS_RTC_ALARM(RTC_Alarm));
  675. assert_param(IS_RTC_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
  676. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
  677. if (RTC_Format == RTC_Format_BIN)
  678. {
  679. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  680. {
  681. assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  682. assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
  683. }
  684. else
  685. {
  686. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
  687. assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  688. }
  689. assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
  690. assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
  691. if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
  692. {
  693. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  694. }
  695. else
  696. {
  697. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  698. }
  699. }
  700. else
  701. {
  702. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  703. {
  704. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
  705. assert_param(IS_RTC_HOUR12(tmpreg));
  706. assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
  707. }
  708. else
  709. {
  710. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
  711. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
  712. }
  713. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
  714. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
  715. if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
  716. {
  717. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  718. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  719. }
  720. else
  721. {
  722. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  723. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  724. }
  725. }
  726. /* Check the input parameters format */
  727. if (RTC_Format != RTC_Format_BIN)
  728. {
  729. tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
  730. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
  731. ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
  732. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
  733. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
  734. ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
  735. ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
  736. }
  737. else
  738. {
  739. tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
  740. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
  741. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
  742. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
  743. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
  744. ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
  745. ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
  746. }
  747. /* Disable the write protection for RTC registers */
  748. RTC->WPR = 0xCA;
  749. RTC->WPR = 0x53;
  750. /* Configure the Alarm register */
  751. RTC->ALRMAR = (uint32_t)tmpreg;
  752. /* Enable the write protection for RTC registers */
  753. RTC->WPR = 0xFF;
  754. }
  755. /**
  756. * @brief Fills each RTC_AlarmStruct member with its default value
  757. * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
  758. * all fields are masked).
  759. * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
  760. * will be initialized.
  761. * @retval None
  762. */
  763. void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
  764. {
  765. /* Alarm Time Settings : Time = 00h:00mn:00sec */
  766. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  767. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
  768. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
  769. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
  770. /* Alarm Date Settings : Date = 1st day of the month */
  771. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  772. RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
  773. /* Alarm Masks Settings : Mask = all fields are not masked */
  774. RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
  775. }
  776. /**
  777. * @brief Get the RTC Alarm value and masks.
  778. * @param RTC_Format: specifies the format of the output parameters.
  779. * This parameter can be one of the following values:
  780. * @arg RTC_Format_BIN: Binary data format
  781. * @arg RTC_Format_BCD: BCD data format
  782. * @param RTC_Alarm: specifies the alarm to be read.
  783. * This parameter can be one of the following values:
  784. * @arg RTC_Alarm_A: to select Alarm A
  785. * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
  786. * contains the output alarm configuration values.
  787. * @retval None
  788. */
  789. void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
  790. {
  791. uint32_t tmpreg = 0;
  792. /* Check the parameters */
  793. assert_param(IS_RTC_FORMAT(RTC_Format));
  794. assert_param(IS_RTC_ALARM(RTC_Alarm));
  795. /* Get the RTC_ALRMAR register */
  796. tmpreg = (uint32_t)(RTC->ALRMAR);
  797. /* Fill the structure with the read parameters */
  798. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
  799. RTC_ALRMAR_HU)) >> 16);
  800. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
  801. RTC_ALRMAR_MNU)) >> 8);
  802. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
  803. RTC_ALRMAR_SU));
  804. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
  805. RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
  806. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  807. RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
  808. if (RTC_Format == RTC_Format_BIN)
  809. {
  810. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  811. RTC_AlarmTime.RTC_Hours);
  812. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  813. RTC_AlarmTime.RTC_Minutes);
  814. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  815. RTC_AlarmTime.RTC_Seconds);
  816. RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  817. }
  818. }
  819. /**
  820. * @brief Enables or disables the specified RTC Alarm.
  821. * @param RTC_Alarm: specifies the alarm to be configured.
  822. * This parameter can be any combination of the following values:
  823. * @arg RTC_Alarm_A: to select Alarm A
  824. * @param NewState: new state of the specified alarm.
  825. * This parameter can be: ENABLE or DISABLE.
  826. * @retval An ErrorStatus enumeration value:
  827. * - SUCCESS: RTC Alarm is enabled/disabled
  828. * - ERROR: RTC Alarm is not enabled/disabled
  829. */
  830. ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
  831. {
  832. __IO uint32_t alarmcounter = 0x00;
  833. uint32_t alarmstatus = 0x00;
  834. ErrorStatus status = ERROR;
  835. /* Check the parameters */
  836. assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
  837. assert_param(IS_FUNCTIONAL_STATE(NewState));
  838. /* Disable the write protection for RTC registers */
  839. RTC->WPR = 0xCA;
  840. RTC->WPR = 0x53;
  841. /* Configure the Alarm state */
  842. if (NewState != DISABLE)
  843. {
  844. RTC->CR |= (uint32_t)RTC_Alarm;
  845. status = SUCCESS;
  846. }
  847. else
  848. {
  849. /* Disable the Alarm in RTC_CR register */
  850. RTC->CR &= (uint32_t)~RTC_Alarm;
  851. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  852. do
  853. {
  854. alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
  855. alarmcounter++;
  856. } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
  857. if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
  858. {
  859. status = ERROR;
  860. }
  861. else
  862. {
  863. status = SUCCESS;
  864. }
  865. }
  866. /* Enable the write protection for RTC registers */
  867. RTC->WPR = 0xFF;
  868. return status;
  869. }
  870. /**
  871. * @brief Configure the RTC AlarmA/B Subseconds value and mask.
  872. * @note This function is performed only when the Alarm is disabled.
  873. * @param RTC_Alarm: specifies the alarm to be configured.
  874. * This parameter can be one of the following values:
  875. * @arg RTC_Alarm_A: to select Alarm A
  876. * @param RTC_AlarmSubSecondValue: specifies the Subseconds value.
  877. * This parameter can be a value from 0 to 0x00007FFF.
  878. * @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask.
  879. * This parameter can be any combination of the following values:
  880. * @arg RTC_AlarmSubSecondMask_All: All Alarm SS fields are masked.
  881. * There is no comparison on sub seconds for Alarm.
  882. * @arg RTC_AlarmSubSecondMask_SS14_1: SS[14:1] are don't care in Alarm comparison.
  883. * Only SS[0] is compared
  884. * @arg RTC_AlarmSubSecondMask_SS14_2: SS[14:2] are don't care in Alarm comparison.
  885. * Only SS[1:0] are compared
  886. * @arg RTC_AlarmSubSecondMask_SS14_3: SS[14:3] are don't care in Alarm comparison.
  887. * Only SS[2:0] are compared
  888. * @arg RTC_AlarmSubSecondMask_SS14_4: SS[14:4] are don't care in Alarm comparison.
  889. * Only SS[3:0] are compared
  890. * @arg RTC_AlarmSubSecondMask_SS14_5: SS[14:5] are don't care in Alarm comparison.
  891. * Only SS[4:0] are compared
  892. * @arg RTC_AlarmSubSecondMask_SS14_6: SS[14:6] are don't care in Alarm comparison.
  893. * Only SS[5:0] are compared
  894. * @arg RTC_AlarmSubSecondMask_SS14_7: SS[14:7] are don't care in Alarm comparison.
  895. * Only SS[6:0] are compared
  896. * @arg RTC_AlarmSubSecondMask_SS14_8: SS[14:8] are don't care in Alarm comparison.
  897. * Only SS[7:0] are compared
  898. * @arg RTC_AlarmSubSecondMask_SS14_9: SS[14:9] are don't care in Alarm comparison.
  899. * Only SS[8:0] are compared
  900. * @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
  901. * Only SS[9:0] are compared
  902. * @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
  903. * Only SS[10:0] are compared
  904. * @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
  905. * Only SS[11:0] are compared
  906. * @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
  907. * Only SS[12:0] are compared
  908. * @arg RTC_AlarmSubSecondMask_SS14: SS[14] is don't care in Alarm comparison.
  909. * Only SS[13:0] are compared
  910. * @arg RTC_AlarmSubSecondMask_None: SS[14:0] are compared and must match to activate alarm
  911. * @retval None
  912. */
  913. void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint8_t RTC_AlarmSubSecondMask)
  914. {
  915. uint32_t tmpreg = 0;
  916. /* Check the parameters */
  917. assert_param(IS_RTC_ALARM(RTC_Alarm));
  918. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
  919. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
  920. /* Disable the write protection for RTC registers */
  921. RTC->WPR = 0xCA;
  922. RTC->WPR = 0x53;
  923. /* Configure the Alarm A or Alarm B SubSecond registers */
  924. tmpreg = (uint32_t) (((uint32_t)(RTC_AlarmSubSecondValue)) | ((uint32_t)(RTC_AlarmSubSecondMask) << 24));
  925. /* Configure the AlarmA SubSecond register */
  926. RTC->ALRMASSR = tmpreg;
  927. /* Enable the write protection for RTC registers */
  928. RTC->WPR = 0xFF;
  929. }
  930. /**
  931. * @brief Gets the RTC Alarm Subseconds value.
  932. * @param RTC_Alarm: specifies the alarm to be read.
  933. * This parameter can be one of the following values:
  934. * @arg RTC_Alarm_A: to select Alarm A
  935. * @param None
  936. * @retval RTC Alarm Subseconds value.
  937. */
  938. uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
  939. {
  940. uint32_t tmpreg = 0;
  941. /* Get the RTC_ALRMAR register */
  942. tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
  943. return (tmpreg);
  944. }
  945. /**
  946. * @}
  947. */
  948. /**
  949. * @brief Adds or substract one hour from the current time.
  950. * @param RTC_DayLightSaveOperation: the value of hour adjustment.
  951. * This parameter can be one of the following values:
  952. * @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
  953. * @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
  954. * @param RTC_StoreOperation: Specifies the value to be written in the BCK bit
  955. * in CR register to store the operation.
  956. * This parameter can be one of the following values:
  957. * @arg RTC_StoreOperation_Reset: BCK Bit Reset
  958. * @arg RTC_StoreOperation_Set: BCK Bit Set
  959. * @retval None
  960. */
  961. void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
  962. {
  963. /* Check the parameters */
  964. assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  965. assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
  966. /* Disable the write protection for RTC registers */
  967. RTC->WPR = 0xCA;
  968. RTC->WPR = 0x53;
  969. /* Clear the bits to be configured */
  970. RTC->CR &= (uint32_t)~(RTC_CR_BCK);
  971. /* Configure the RTC_CR register */
  972. RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
  973. /* Enable the write protection for RTC registers */
  974. RTC->WPR = 0xFF;
  975. }
  976. /**
  977. * @brief Returns the RTC Day Light Saving stored operation.
  978. * @param None
  979. * @retval RTC Day Light Saving stored operation.
  980. * - RTC_StoreOperation_Reset
  981. * - RTC_StoreOperation_Set
  982. */
  983. uint32_t RTC_GetStoreOperation(void)
  984. {
  985. return (RTC->CR & RTC_CR_BCK);
  986. }
  987. /**
  988. * @}
  989. */
  990. /**
  991. * @brief Configures the RTC output source (AFO_ALARM).
  992. * @param RTC_Output: Specifies which signal will be routed to the RTC output.
  993. * This parameter can be one of the following values:
  994. * @arg RTC_Output_Disable: No output selected
  995. * @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
  996. * @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
  997. * @param RTC_OutputPolarity: Specifies the polarity of the output signal.
  998. * This parameter can be one of the following:
  999. * @arg RTC_OutputPolarity_High: The output pin is high when the
  1000. * ALRAF is high (depending on OSEL)
  1001. * @arg RTC_OutputPolarity_Low: The output pin is low when the
  1002. * ALRAF is high (depending on OSEL)
  1003. * @retval None
  1004. */
  1005. void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
  1006. {
  1007. /* Check the parameters */
  1008. assert_param(IS_RTC_OUTPUT(RTC_Output));
  1009. assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
  1010. /* Disable the write protection for RTC registers */
  1011. RTC->WPR = 0xCA;
  1012. RTC->WPR = 0x53;
  1013. /* Clear the bits to be configured */
  1014. RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
  1015. /* Configure the output selection and polarity */
  1016. RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
  1017. /* Enable the write protection for RTC registers */
  1018. RTC->WPR = 0xFF;
  1019. }
  1020. /**
  1021. * @}
  1022. */
  1023. /**
  1024. * @brief Enables or disables the RTC clock to be output through the relative pin.
  1025. * @param NewState: new state of the digital calibration Output.
  1026. * This parameter can be: ENABLE or DISABLE.
  1027. * @retval None
  1028. */
  1029. void RTC_CalibOutputCmd(FunctionalState NewState)
  1030. {
  1031. /* Check the parameters */
  1032. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1033. /* Disable the write protection for RTC registers */
  1034. RTC->WPR = 0xCA;
  1035. RTC->WPR = 0x53;
  1036. if (NewState != DISABLE)
  1037. {
  1038. /* Enable the RTC clock output */
  1039. RTC->CR |= (uint32_t)RTC_CR_COE;
  1040. }
  1041. else
  1042. {
  1043. /* Disable the RTC clock output */
  1044. RTC->CR &= (uint32_t)~RTC_CR_COE;
  1045. }
  1046. /* Enable the write protection for RTC registers */
  1047. RTC->WPR = 0xFF;
  1048. }
  1049. /**
  1050. * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1051. * @param RTC_CalibOutput: Select the Calibration output Selection .
  1052. * This parameter can be one of the following values:
  1053. * @arg RTC_CalibOutput_512Hz: A signal has a regular waveform at 512Hz.
  1054. * @arg RTC_CalibOutput_1Hz: A signal has a regular waveform at 1Hz.
  1055. * @retval None
  1056. */
  1057. void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
  1058. {
  1059. /* Check the parameters */
  1060. assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
  1061. /* Disable the write protection for RTC registers */
  1062. RTC->WPR = 0xCA;
  1063. RTC->WPR = 0x53;
  1064. /*clear flags before config*/
  1065. RTC->CR &= (uint32_t)~(RTC_CR_CALSEL);
  1066. /* Configure the RTC_CR register */
  1067. RTC->CR |= (uint32_t)RTC_CalibOutput;
  1068. /* Enable the write protection for RTC registers */
  1069. RTC->WPR = 0xFF;
  1070. }
  1071. /**
  1072. * @brief Configures the Smooth Calibration Settings.
  1073. * @param RTC_SmoothCalibPeriod: Select the Smooth Calibration Period.
  1074. * This parameter can be can be one of the following values:
  1075. * @arg RTC_SmoothCalibPeriod_32sec: The smooth calibration periode is 32s.
  1076. * @arg RTC_SmoothCalibPeriod_16sec: The smooth calibration periode is 16s.
  1077. * @arg RTC_SmoothCalibPeriod_8sec: The smooth calibartion periode is 8s.
  1078. * @param RTC_SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
  1079. * This parameter can be one of the following values:
  1080. * @arg RTC_SmoothCalibPlusPulses_Set: Add one RTCCLK puls every 2**11 pulses.
  1081. * @arg RTC_SmoothCalibPlusPulses_Reset: No RTCCLK pulses are added.
  1082. * @param RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  1083. * This parameter can be one any value from 0 to 0x000001FF.
  1084. * @retval An ErrorStatus enumeration value:
  1085. * - SUCCESS: RTC Calib registers are configured
  1086. * - ERROR: RTC Calib registers are not configured
  1087. */
  1088. ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
  1089. uint32_t RTC_SmoothCalibPlusPulses,
  1090. uint32_t RTC_SmouthCalibMinusPulsesValue)
  1091. {
  1092. ErrorStatus status = ERROR;
  1093. uint32_t recalpfcount = 0;
  1094. /* Check the parameters */
  1095. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
  1096. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
  1097. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
  1098. /* Disable the write protection for RTC registers */
  1099. RTC->WPR = 0xCA;
  1100. RTC->WPR = 0x53;
  1101. /* check if a calibration is pending*/
  1102. if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
  1103. {
  1104. /* wait until the Calibration is completed*/
  1105. while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
  1106. {
  1107. recalpfcount++;
  1108. }
  1109. }
  1110. /* check if the calibration pending is completed or if there is no calibration operation at all*/
  1111. if ((RTC->ISR & RTC_ISR_RECALPF) == RESET)
  1112. {
  1113. /* Configure the Smooth calibration settings */
  1114. RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
  1115. status = SUCCESS;
  1116. }
  1117. else
  1118. {
  1119. status = ERROR;
  1120. }
  1121. /* Enable the write protection for RTC registers */
  1122. RTC->WPR = 0xFF;
  1123. return (ErrorStatus)(status);
  1124. }
  1125. /**
  1126. * @}
  1127. */
  1128. /**
  1129. * @brief Enables or Disables the RTC TimeStamp functionality with the
  1130. * specified time stamp pin stimulating edge.
  1131. * @param RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  1132. * activated.
  1133. * This parameter can be one of the following:
  1134. * @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising
  1135. * edge of the related pin.
  1136. * @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the
  1137. * falling edge of the related pin.
  1138. * @param NewState: new state of the TimeStamp.
  1139. * This parameter can be: ENABLE or DISABLE.
  1140. * @retval None
  1141. */
  1142. void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
  1143. {
  1144. uint32_t tmpreg = 0;
  1145. /*��Edge�ĸ��û�д�λ�Ķ���*/
  1146. /* Check the parameters */
  1147. assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  1148. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1149. /* Get the RTC_CR register and clear the bits to be configured */
  1150. tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  1151. /* Get the new configuration */
  1152. if (NewState != DISABLE)
  1153. {
  1154. tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
  1155. }
  1156. else
  1157. {
  1158. tmpreg |= (uint32_t)(RTC_TimeStampEdge);
  1159. }
  1160. /* Disable the write protection for RTC registers */
  1161. RTC->WPR = 0xCA;
  1162. RTC->WPR = 0x53;
  1163. /* Configure the Time Stamp TSEDGE and Enable bits */
  1164. RTC->CR = (uint32_t)tmpreg;
  1165. /* Enable the write protection for RTC registers */
  1166. RTC->WPR = 0xFF;
  1167. }
  1168. /**
  1169. * @brief Get the RTC TimeStamp value and masks.
  1170. * @param RTC_Format: specifies the format of the output parameters.
  1171. * This parameter can be one of the following values:
  1172. * @arg RTC_Format_BIN: Binary data format
  1173. * @arg RTC_Format_BCD: BCD data format
  1174. * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will
  1175. * contains the TimeStamp time values.
  1176. * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will
  1177. * contains the TimeStamp date values.
  1178. * @retval None
  1179. */
  1180. void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
  1181. RTC_DateTypeDef* RTC_StampDateStruct)
  1182. {
  1183. uint32_t tmptime = 0, tmpdate = 0;
  1184. /* Check the parameters */
  1185. assert_param(IS_RTC_FORMAT(RTC_Format));
  1186. /* Get the TimeStamp time and date registers values */
  1187. tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
  1188. tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
  1189. /* Fill the Time structure fields with the read parameters */
  1190. RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  1191. RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
  1192. RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  1193. RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
  1194. /* Fill the Date structure fields with the read parameters */
  1195. RTC_StampDateStruct->RTC_Year = 0;
  1196. RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  1197. RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  1198. RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
  1199. /* Check the input parameters format */
  1200. if (RTC_Format == RTC_Format_BIN)
  1201. {
  1202. /* Convert the Time structure parameters to Binary format */
  1203. RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
  1204. RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
  1205. RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
  1206. /* Convert the Date structure parameters to Binary format */
  1207. RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
  1208. RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
  1209. RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
  1210. }
  1211. }
  1212. /**
  1213. * @brief Get the RTC timestamp Subseconds value.
  1214. * @param None
  1215. * @retval RTC current timestamp Subseconds value.
  1216. */
  1217. uint32_t RTC_GetTimeStampSubSecond(void)
  1218. {
  1219. /* Get timestamp subseconds values from the correspondent registers */
  1220. return (uint32_t)(RTC->TSSSR);
  1221. }
  1222. /**
  1223. * @}
  1224. */
  1225. /**
  1226. * @brief Configures the select Tamper pin edge.
  1227. * @param RTC_Tamper: Selected tamper pin.
  1228. * This parameter can be any combination of the following values:
  1229. * @arg RTC_Tamper_1: Select Tamper 1.
  1230. * @arg RTC_Tamper_2: Select Tamper 2.
  1231. * @param RTC_TamperTrigger: Specifies the trigger on the tamper pin that
  1232. * stimulates tamper event.
  1233. * This parameter can be one of the following values:
  1234. * @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
  1235. * @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
  1236. * @arg RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
  1237. * @arg RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
  1238. * @retval None
  1239. */
  1240. void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
  1241. {
  1242. /* Check the parameters */
  1243. assert_param(IS_RTC_TAMPER(RTC_Tamper));
  1244. assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
  1245. if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
  1246. {
  1247. /* Configure the RTC_TAFCR register */
  1248. RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
  1249. }
  1250. else
  1251. {
  1252. /* Configure the RTC_TAFCR register */
  1253. RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
  1254. }
  1255. }
  1256. /**
  1257. * @brief Enables or Disables the Tamper detection.
  1258. * @param RTC_Tamper: Selected tamper pin.
  1259. * This parameter can be any combination of the following values:
  1260. * @arg RTC_Tamper_1: Select Tamper 1.
  1261. * @arg RTC_Tamper_2: Select Tamper 2.
  1262. * @param NewState: new state of the tamper pin.
  1263. * This parameter can be: ENABLE or DISABLE.
  1264. * @retval None
  1265. */
  1266. void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
  1267. {
  1268. /* Check the parameters */
  1269. assert_param(IS_RTC_TAMPER(RTC_Tamper));
  1270. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1271. if (NewState != DISABLE)
  1272. {
  1273. /* Enable the selected Tamper pin */
  1274. RTC->TAFCR |= (uint32_t)RTC_Tamper;
  1275. }
  1276. else
  1277. {
  1278. /* Disable the selected Tamper pin */
  1279. RTC->TAFCR &= (uint32_t)~RTC_Tamper;
  1280. }
  1281. }
  1282. /**
  1283. * @brief Configures the Tampers Filter.
  1284. * @param RTC_TamperFilter: Specifies the tampers filter.
  1285. * This parameter can be one of the following values:
  1286. * @arg RTC_TamperFilter_Disable: Tamper filter is disabled.
  1287. * @arg RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive
  1288. * samples at the active level
  1289. * @arg RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive
  1290. * samples at the active level
  1291. * @arg RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive
  1292. * samples at the active level
  1293. * @retval None
  1294. */
  1295. void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
  1296. {
  1297. /* Check the parameters */
  1298. assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
  1299. /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
  1300. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
  1301. /* Configure the RTC_TAFCR register */
  1302. RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
  1303. }
  1304. /**
  1305. * @brief Configures the Tampers Sampling Frequency.
  1306. * @param RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
  1307. * This parameter can be one of the following values:
  1308. * @arg RTC_TamperSamplingFreq_RTCCLK_Div32768: Each of the tamper inputs are sampled
  1309. * with a frequency = RTCCLK / 32768
  1310. * @arg RTC_TamperSamplingFreq_RTCCLK_Div16384: Each of the tamper inputs are sampled
  1311. * with a frequency = RTCCLK / 16384
  1312. * @arg RTC_TamperSamplingFreq_RTCCLK_Div8192: Each of the tamper inputs are sampled
  1313. * with a frequency = RTCCLK / 8192
  1314. * @arg RTC_TamperSamplingFreq_RTCCLK_Div4096: Each of the tamper inputs are sampled
  1315. * with a frequency = RTCCLK / 4096
  1316. * @arg RTC_TamperSamplingFreq_RTCCLK_Div2048: Each of the tamper inputs are sampled
  1317. * with a frequency = RTCCLK / 2048
  1318. * @arg RTC_TamperSamplingFreq_RTCCLK_Div1024: Each of the tamper inputs are sampled
  1319. * with a frequency = RTCCLK / 1024
  1320. * @arg RTC_TamperSamplingFreq_RTCCLK_Div512: Each of the tamper inputs are sampled
  1321. * with a frequency = RTCCLK / 512
  1322. * @arg RTC_TamperSamplingFreq_RTCCLK_Div256: Each of the tamper inputs are sampled
  1323. * with a frequency = RTCCLK / 256
  1324. * @retval None
  1325. */
  1326. void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
  1327. {
  1328. /* Check the parameters */
  1329. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
  1330. /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
  1331. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
  1332. /* Configure the RTC_TAFCR register */
  1333. RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
  1334. }
  1335. /**
  1336. * @brief Configures the Tampers Pins input Precharge Duration.
  1337. * @param RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
  1338. * Precharge Duration.
  1339. * This parameter can be one of the following values:
  1340. * @arg RTC_TamperPrechargeDuration_1RTCCLK: Tamper pins are pre-charged before sampling during 1 RTCCLK cycle
  1341. * @arg RTC_TamperPrechargeDuration_2RTCCLK: Tamper pins are pre-charged before sampling during 2 RTCCLK cycle
  1342. * @arg RTC_TamperPrechargeDuration_4RTCCLK: Tamper pins are pre-charged before sampling during 4 RTCCLK cycle
  1343. * @arg RTC_TamperPrechargeDuration_8RTCCLK: Tamper pins are pre-charged before sampling during 8 RTCCLK cycle
  1344. * @retval None
  1345. */
  1346. void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
  1347. {
  1348. /* Check the parameters */
  1349. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
  1350. /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
  1351. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
  1352. /* Configure the RTC_TAFCR register */
  1353. RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
  1354. }
  1355. /**
  1356. * @brief Enables or Disables the TimeStamp on Tamper Detection Event.
  1357. * @note The timestamp is valid even the TSE bit in tamper control register
  1358. * is reset.
  1359. * @param NewState: new state of the timestamp on tamper event.
  1360. * This parameter can be: ENABLE or DISABLE.
  1361. * @retval None
  1362. */
  1363. void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
  1364. {
  1365. /* Check the parameters */
  1366. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1367. if (NewState != DISABLE)
  1368. {
  1369. /* Save timestamp on tamper detection event */
  1370. RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
  1371. }
  1372. else
  1373. {
  1374. /* Tamper detection does not cause a timestamp to be saved */
  1375. RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;
  1376. }
  1377. }
  1378. /**
  1379. * @brief Enables or Disables the Precharge of Tamper pin.
  1380. * @param NewState: new state of tamper pull up.
  1381. * This parameter can be: ENABLE or DISABLE.
  1382. * @retval None
  1383. */
  1384. void RTC_TamperPullUpCmd(FunctionalState NewState)
  1385. {
  1386. /* Check the parameters */
  1387. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1388. if (NewState != DISABLE)
  1389. {
  1390. /* Enable precharge of the selected Tamper pin */
  1391. RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS;
  1392. }
  1393. else
  1394. {
  1395. /* Disable precharge of the selected Tamper pin */
  1396. RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;
  1397. }
  1398. }
  1399. /**
  1400. * @}
  1401. */
  1402. /**
  1403. * @brief Configures the RTC Output Pin mode.
  1404. * @param RTC_OutputType: specifies the RTC Output (PC13) pin mode.
  1405. * This parameter can be one of the following values:
  1406. * @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in
  1407. * Open Drain mode.
  1408. * @arg RTC_OutputType_PushPull: RTC Output (PC13) is configured in
  1409. * Push Pull mode.
  1410. * @retval None
  1411. */
  1412. void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
  1413. {
  1414. /* Check the parameters */
  1415. assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
  1416. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
  1417. RTC->TAFCR |= (uint32_t)(RTC_OutputType);
  1418. }
  1419. /**
  1420. * @}
  1421. */
  1422. /**
  1423. * @brief Configures the Synchronization Shift Control Settings.
  1424. * @note When REFCKON is set, firmware must not write to Shift control register
  1425. * @param RTC_ShiftAdd1S: Select to add or not 1 second to the time Calendar.
  1426. * This parameter can be one of the following values :
  1427. * @arg RTC_ShiftAdd1S_Set: Add one second to the clock calendar.
  1428. * @arg RTC_ShiftAdd1S_Reset: No effect.
  1429. * @param RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
  1430. * This parameter can be one any value from 0 to 0x7FFF.
  1431. * @retval An ErrorStatus enumeration value:
  1432. * - SUCCESS: RTC Shift registers are configured
  1433. * - ERROR: RTC Shift registers are not configured
  1434. */
  1435. ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
  1436. {
  1437. ErrorStatus status = ERROR;
  1438. uint32_t shpfcount = 0;
  1439. /* Check the parameters */
  1440. assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
  1441. assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
  1442. /* Disable the write protection for RTC registers */
  1443. RTC->WPR = 0xCA;
  1444. RTC->WPR = 0x53;
  1445. /* Check if a Shift is pending*/
  1446. if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
  1447. {
  1448. /* Wait until the shift is completed*/
  1449. while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
  1450. {
  1451. shpfcount++;
  1452. }
  1453. }
  1454. /* Check if the Shift pending is completed or if there is no Shift operation at all*/
  1455. if ((RTC->ISR & RTC_ISR_SHPF) == RESET)
  1456. {
  1457. /* check if the reference clock detection is disabled */
  1458. if((RTC->CR & RTC_CR_REFCKON) == RESET)
  1459. {
  1460. /* Configure the Shift settings */
  1461. RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
  1462. if(RTC_WaitForSynchro() == ERROR)
  1463. {
  1464. status = ERROR;
  1465. }
  1466. else
  1467. {
  1468. status = SUCCESS;
  1469. }
  1470. }
  1471. else
  1472. {
  1473. status = ERROR;
  1474. }
  1475. }
  1476. else
  1477. {
  1478. status = ERROR;
  1479. }
  1480. /* Enable the write protection for RTC registers */
  1481. RTC->WPR = 0xFF;
  1482. return (ErrorStatus)(status);
  1483. }
  1484. /**
  1485. * @}
  1486. */
  1487. /**
  1488. * @brief Enables or disables the specified RTC interrupts.
  1489. * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled.
  1490. * This parameter can be any combination of the following values:
  1491. * @arg RTC_IT_TS: Time Stamp interrupt mask
  1492. * @arg RTC_IT_WUT: WakeUp Timer interrupt mask
  1493. * @arg RTC_IT_ALRA: Alarm A interrupt mask
  1494. * @arg RTC_IT_TAMP: Tamper event interrupt mask
  1495. * @param NewState: new state of the specified RTC interrupts.
  1496. * This parameter can be: ENABLE or DISABLE.
  1497. * @retval None
  1498. */
  1499. void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
  1500. {
  1501. /* Check the parameters */
  1502. assert_param(IS_RTC_CONFIG_IT(RTC_IT));
  1503. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1504. /* Disable the write protection for RTC registers */
  1505. RTC->WPR = 0xCA;
  1506. RTC->WPR = 0x53;
  1507. if (NewState != DISABLE)
  1508. {
  1509. /* Configure the Interrupts in the RTC_CR register */
  1510. RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
  1511. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  1512. RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
  1513. }
  1514. else
  1515. {
  1516. /* Configure the Interrupts in the RTC_CR register */
  1517. RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
  1518. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  1519. RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
  1520. }
  1521. /* Enable the write protection for RTC registers */
  1522. RTC->WPR = 0xFF;
  1523. }
  1524. /**
  1525. * @brief Checks whether the specified RTC flag is set or not.
  1526. * @param RTC_FLAG: specifies the flag to check.
  1527. * This parameter can be one of the following values:
  1528. * @arg RTC_FLAG_RECALPF: RECALPF event flag
  1529. * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
  1530. * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
  1531. * @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
  1532. * @arg RTC_FLAG_TSF: Time Stamp event flag
  1533. * @arg RTC_FLAG_WUTF: WakeUp Timer flag
  1534. * @arg RTC_FLAG_ALRAF: Alarm A flag
  1535. * @arg RTC_FLAG_INITF: Initialization mode flag
  1536. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  1537. * @arg RTC_FLAG_INITS: Registers Configured flag
  1538. * @retval The new state of RTC_FLAG (SET or RESET).
  1539. */
  1540. FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
  1541. {
  1542. FlagStatus bitstatus = RESET;
  1543. uint32_t tmpreg = 0;
  1544. /* Check the parameters */
  1545. assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
  1546. /* Get all the flags */
  1547. tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
  1548. /* Return the status of the flag */
  1549. if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
  1550. {
  1551. bitstatus = SET;
  1552. }
  1553. else
  1554. {
  1555. bitstatus = RESET;
  1556. }
  1557. return bitstatus;
  1558. }
  1559. /**
  1560. * @brief Clears the RTC's pending flags.
  1561. * @param RTC_FLAG: specifies the RTC flag to clear.
  1562. * This parameter can be any combination of the following values:
  1563. * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
  1564. * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
  1565. * @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag
  1566. * @arg RTC_FLAG_TSF: Time Stamp event flag
  1567. * @arg RTC_FLAG_WUTF: WakeUp Timer flag
  1568. * @arg RTC_FLAG_ALRAF: Alarm A flag
  1569. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  1570. * @retval None
  1571. */
  1572. void RTC_ClearFlag(uint32_t RTC_FLAG)
  1573. {
  1574. /* Check the parameters */
  1575. assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
  1576. /* Clear the Flags in the RTC_ISR register */
  1577. RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0001FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
  1578. }
  1579. /**
  1580. * @brief Checks whether the specified RTC interrupt has occurred or not.
  1581. * @param RTC_IT: specifies the RTC interrupt source to check.
  1582. * This parameter can be one of the following values:
  1583. * @arg RTC_IT_TS: Time Stamp interrupt
  1584. * @arg RTC_IT_WUT: WakeUp Timer interrupt
  1585. * @arg RTC_IT_ALRA: Alarm A interrupt
  1586. * @arg RTC_IT_TAMP1: Tamper1 event interrupt
  1587. * @arg RTC_IT_TAMP2: Tamper2 event interrupt
  1588. * @retval The new state of RTC_IT (SET or RESET).
  1589. */
  1590. ITStatus RTC_GetITStatus(uint32_t RTC_IT)
  1591. {
  1592. ITStatus bitstatus = RESET;
  1593. uint32_t tmpreg = 0, enablestatus = 0;
  1594. /* Check the parameters */
  1595. assert_param(IS_RTC_GET_IT(RTC_IT));
  1596. /* Get the TAMPER Interrupt enable bit and pending bit */
  1597. tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
  1598. /* Get the Interrupt enable Status */
  1599. enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & ((RTC_IT >> (RTC_IT >> 18)) >> 15)));
  1600. /* Get the Interrupt pending bit */
  1601. tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
  1602. /* Get the status of the Interrupt */
  1603. if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
  1604. {
  1605. bitstatus = SET;
  1606. }
  1607. else
  1608. {
  1609. bitstatus = RESET;
  1610. }
  1611. return bitstatus;
  1612. }
  1613. /**
  1614. * @brief Clears the RTC's interrupt pending bits.
  1615. * @param RTC_IT: specifies the RTC interrupt pending bit to clear.
  1616. * This parameter can be any combination of the following values:
  1617. * @arg RTC_IT_TS: Time Stamp interrupt
  1618. * @arg RTC_IT_WUT: WakeUp Timer interrupt
  1619. * @arg RTC_IT_ALRA: Alarm A interrupt
  1620. * @arg RTC_IT_TAMP1: Tamper1 event interrupt
  1621. * @arg RTC_IT_TAMP2: Tamper2 event interrupt
  1622. * @retval None
  1623. */
  1624. void RTC_ClearITPendingBit(uint32_t RTC_IT)
  1625. {
  1626. uint32_t tmpreg = 0;
  1627. /* Check the parameters */
  1628. assert_param(IS_RTC_CLEAR_IT(RTC_IT));
  1629. /* Get the RTC_ISR Interrupt pending bits mask */
  1630. tmpreg = (uint32_t)(RTC_IT >> 4);
  1631. /* Clear the interrupt pending bits in the RTC_ISR register */
  1632. RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
  1633. }
  1634. /**
  1635. * @}
  1636. */
  1637. /**
  1638. * @brief Converts a 2 digit decimal to BCD format.
  1639. * @param Value: Byte to be converted.
  1640. * @retval Converted byte
  1641. */
  1642. static uint8_t RTC_ByteToBcd2(uint8_t Value)
  1643. {
  1644. uint8_t bcdhigh = 0;
  1645. while (Value >= 10)
  1646. {
  1647. bcdhigh++;
  1648. Value -= 10;
  1649. }
  1650. return ((uint8_t)(bcdhigh << 4) | Value);
  1651. }
  1652. /**
  1653. * @brief Convert from 2 digit BCD to Binary.
  1654. * @param Value: BCD value to be converted.
  1655. * @retval Converted word
  1656. */
  1657. static uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1658. {
  1659. uint8_t tmp = 0;
  1660. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1661. return (tmp + (Value & (uint8_t)0x0F));
  1662. }
  1663. /**
  1664. * @}
  1665. */
  1666. /**
  1667. * @}
  1668. */
  1669. /**
  1670. * @}
  1671. */
  1672. /************************ (C) COPYRIGHT FMD *****END OF FILE****/