stm8l15x_rtc.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_rtc.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Real-Time Clock (RTC) peripheral:
  9. * - Initialization
  10. * - Calendar (Time and Date) configuration
  11. * - Alarm configuration
  12. * - WakeUp Timer configuration
  13. * - Daylight Saving configuration
  14. * - Output pin Configuration
  15. * - Smooth Calibration configuration
  16. * - Tampers configuration
  17. * - Output Type configuration
  18. * - Interrupts and flags management
  19. *
  20. * @verbatim
  21. *
  22. * ===================================================================
  23. * RTC Domain Reset
  24. * ===================================================================
  25. * After power-on reset, the RTC domain (RTC registers) is reset.
  26. *
  27. * ===================================================================
  28. * RTC Operating Condition
  29. * ===================================================================
  30. * As long as the supply voltage remains in the operating range,
  31. * the RTC never stops, regardless of the device status (Run mode,
  32. * low power modes or under reset).
  33. *
  34. * ===================================================================
  35. * RTC Domain Access
  36. * ===================================================================
  37. * After reset, the RTC domain (RTC registers) is protected against
  38. * possible stray write accesses.
  39. * To enable access to the RTC registers, proceed as follows:
  40. * - Select the RTC clock source using the CLK_RTCClockConfig()
  41. * function : HSE, HSI, LSE or LSI.
  42. * CLK_RTCClockConfig(CLK_RTCCLKSource_HSI, CLK_RTCCLKDiv_1)
  43. * - Enable RTC Clock using the CLK_PeripheralClockConfig() function
  44. * : CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE).
  45. *
  46. * ===================================================================
  47. * RTC Driver: how to use it
  48. * ===================================================================
  49. * - Enable the RTC domain access (see description in the section above)
  50. * - Configure the RTC Prescaler (Asynchronous and Synchronous) and
  51. * RTC hour format using the RTC_Init() function.
  52. *
  53. * Time and Date configuration
  54. * ===========================
  55. * - To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
  56. * and RTC_SetDate() functions.
  57. * - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
  58. * functions.
  59. * - Use the RTC_DayLightSavingConfig() function to add or sub 1hour
  60. * to the RTC Calendar.
  61. *
  62. * Alarm configuration
  63. * ===================
  64. * - To configure the RTC Alarm use the RTC_SetAlarm() function.
  65. * - Enable the selected RTC Alarm using the RTC_AlarmCmd() function
  66. * - To read the RTC Alarm, use the RTC_GetAlarm() function.
  67. *
  68. * RTC Wakeup configuration
  69. * ========================
  70. * - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
  71. * function.
  72. * - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter()
  73. * function
  74. * - Enable the RTC WakeUp using the RTC_WakeUpCmd() function
  75. * - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter()
  76. * function.
  77. *
  78. * Outputs configuration
  79. * =====================
  80. * The RTC has 2 different outputs:
  81. * - AFO_ALARM: this output is used to manage the RTC Alarm and
  82. * WaKeUp signals.
  83. * To output the selected RTC signal on RTC_ALARM pin, use the
  84. * RTC_OutputConfig() function.
  85. * - AFO_CALIB: this output is used to manage the RTC Clock divided
  86. * by 64 (512Hz) signal or divided by 32768 (1Hz) which can be
  87. * configured using RTC_CalibOutputConfig() function.
  88. * To output the RTC Clock on RTC_CALIB pin, use the
  89. * RTC_CalibOutputCmd() function.
  90. *
  91. * Smooth Calibration configuration
  92. * =================================
  93. * - Configure the RTC Smooth Calibration Value and the corresponding
  94. * sign using the RTC_SmoothCalibConfig() function.
  95. *
  96. *
  97. * Tamper configuration
  98. * ====================
  99. * - Configure the RTC Tamper Level using the RTC_TamperConfig()
  100. * function.
  101. * - Enable the RTC Tamper using the RTC_TamperCmd() function.
  102. *
  103. *
  104. * ===================================================================
  105. * RTC and low power modes
  106. * ===================================================================
  107. * The MCU can be woken up from a low power mode by an RTC alternate
  108. * function.
  109. * The RTC alternate functions are the RTC alarms,
  110. * RTC wakeup and RTC tamper event detection.
  111. * These RTC alternate functions can wake up the system from the
  112. * lowpower modes.
  113. * The system can also wake up from low power modes without depending
  114. * on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  115. * or the RTC wakeup events.
  116. * The RTC provides a programmable time base for waking up from the
  117. * halt or wfe or wfi modes at regular intervals.
  118. *
  119. *
  120. * @endverbatim
  121. *
  122. ******************************************************************************
  123. * @attention
  124. *
  125. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  126. *
  127. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  128. * You may not use this file except in compliance with the License.
  129. * You may obtain a copy of the License at:
  130. *
  131. * http://www.st.com/software_license_agreement_liberty_v2
  132. *
  133. * Unless required by applicable law or agreed to in writing, software
  134. * distributed under the License is distributed on an "AS IS" BASIS,
  135. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  136. * See the License for the specific language governing permissions and
  137. * limitations under the License.
  138. *
  139. ******************************************************************************
  140. */
  141. /* Includes ------------------------------------------------------------------*/
  142. #include "stm8l15x_rtc.h"
  143. /** @addtogroup STM8L15x_StdPeriph_Driver
  144. * @{
  145. */
  146. /** @defgroup RTC
  147. * @brief RTC driver modules
  148. * @{
  149. */
  150. /* Private typedef -----------------------------------------------------------*/
  151. /* Private define ------------------------------------------------------------*/
  152. #define INIT_TIMEOUT ((uint16_t)0xFFFF)
  153. #define RSF_TIMEOUT ((uint16_t)0xFFFF)
  154. #define INITF_TIMEOUT ((uint16_t)0xFFFF)
  155. #define WUTWF_TIMEOUT ((uint16_t)0xFFFF)
  156. #define ALRAWF_TIMEOUT ((uint16_t)0xFFFF)
  157. #define RECALPF_TIMEOUT ((uint16_t)0xFFFF)
  158. #define SHPF_TIMEOUT ((uint16_t)0xFFFF)
  159. #define TEN_VALUE_BCD ((uint8_t)0x10)
  160. #define TEN_VALUE_BIN ((uint8_t)0x0A)
  161. /* Private macro -------------------------------------------------------------*/
  162. /* Private variables ---------------------------------------------------------*/
  163. /** @defgroup RTC_Private_Functions_Prototype
  164. * @{
  165. */
  166. static uint8_t ByteToBcd2(uint8_t Value);
  167. static uint8_t Bcd2ToByte(uint8_t Value);
  168. /**
  169. * @}
  170. */
  171. /** @defgroup RTC_Private_Functions
  172. * @{
  173. */
  174. /** @defgroup RTC_Group1 Initialization and Configuration functions
  175. * @brief Initialization and Configuration functions
  176. *
  177. @verbatim
  178. ===============================================================================
  179. Initialization and Configuration functions
  180. ===============================================================================
  181. This section provide functions allowing to initialize and configure the RTC
  182. Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
  183. Write protection, enter and exit the RTC initialization mode, RTC registers
  184. synchronization check and reference clock detection enable.
  185. 1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
  186. split into 2 programmable prescaler to minimize power consumption.
  187. - A 7-bit asynchronous prescaler and A 13-bit or 15-bit synchronous
  188. prescaler depending to density of the mcu package.
  189. - When both prescaler are used, it is recommended to configure the
  190. asynchronous prescaler to a high value to minimize consumption.
  191. 2. All RTC registers are Write protected. Writing to the RTC registers
  192. is enabled by writing a key into the Write Protection register, RTC_WPR.
  193. 3. To Configure the RTC Calendar, user application should enter initialization
  194. mode. In this mode, the Calendar counter is stopped and its value can be
  195. updated. When the initialization sequence is complete, the Calendar restarts
  196. counting after 4 RTCCLK cycles.
  197. 4. To read the Calendar through the shadow registers after Calendar initialization,
  198. Calendar update or after wakeup from low power modes the software must first
  199. clear the RSF flag. The software must then wait until it is set again before
  200. reading the Calendar, which means that the Calendar registers have been
  201. correctly copied into the RTC_TR and RTC_DR shadow registers.
  202. The RTC_WaitForSynchro() function implements the above software sequence
  203. (RSF clear and RSF check).
  204. @endverbatim
  205. * @{
  206. */
  207. /**
  208. * @brief Deinitializes the RTC registers to their default reset values.
  209. * @note This function doesn't reset the RTC Clock source.
  210. * @param None
  211. * @retval An ErrorStatus enumeration value:
  212. * - SUCCESS: RTC registers are deinitialized
  213. * - ERROR: RTC registers are not deinitialized
  214. */
  215. ErrorStatus RTC_DeInit(void)
  216. {
  217. ErrorStatus status = ERROR;
  218. uint16_t wutwfcount = 0;
  219. uint16_t recalpfcount = 0;
  220. /* Disable the write protection for RTC registers */
  221. RTC->WPR = 0xCA;
  222. RTC->WPR = 0x53;
  223. /* Set Initialization mode */
  224. if (RTC_EnterInitMode() == ERROR)
  225. {
  226. status = ERROR;
  227. /* Enable the write protection for RTC registers */
  228. RTC->WPR = 0xFF;
  229. }
  230. else
  231. {
  232. /* Reset TR registers */
  233. RTC->TR1 = RTC_TR1_RESET_VALUE;
  234. RTC->TR2 = RTC_TR2_RESET_VALUE;
  235. RTC->TR3 = RTC_TR3_RESET_VALUE;
  236. /* Reset DR registers */
  237. RTC->DR1 = RTC_DR1_RESET_VALUE;
  238. RTC->DR2 = RTC_DR2_RESET_VALUE;
  239. RTC->DR3 = RTC_DR3_RESET_VALUE;
  240. /* Reset SPER & ARPER registers */
  241. RTC->SPRERH = RTC_SPRERH_RESET_VALUE;
  242. RTC->SPRERL = RTC_SPRERL_RESET_VALUE;
  243. RTC->APRER = RTC_APRER_RESET_VALUE;
  244. RTC->TCR1 = RTC_TCR1_RESET_VALUE;
  245. RTC->TCR2 = RTC_TCR2_RESET_VALUE;
  246. /* Reset All CR1 bits except CR1[2:0] */
  247. RTC->CR1 = RTC_CR1_RESET_VALUE;
  248. RTC->CR2 = RTC_CR2_RESET_VALUE;
  249. RTC->CR3 = RTC_CR3_RESET_VALUE;
  250. /* Wait till RTC WUTWF flag is set or if Time out is reached exit */
  251. while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT))
  252. {
  253. wutwfcount++;
  254. RTC->ISR1 = 0;
  255. }
  256. if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET)
  257. {
  258. status = ERROR;
  259. /* Enable the write protection for RTC registers */
  260. RTC->WPR = 0xFF;
  261. }
  262. else
  263. {
  264. /* Reset All CR1 bits */
  265. RTC->CR1 = RTC_CR1_RESET_VALUE;
  266. /* Reset WUTR registers */
  267. RTC->WUTRH = RTC_WUTRH_RESET_VALUE;
  268. RTC->WUTRL = RTC_WUTRL_RESET_VALUE;
  269. /* Reset ALARM registers */
  270. RTC->ALRMAR1 = RTC_ALRMAR1_RESET_VALUE;
  271. RTC->ALRMAR2 = RTC_ALRMAR2_RESET_VALUE;
  272. RTC->ALRMAR3 = RTC_ALRMAR3_RESET_VALUE;
  273. RTC->ALRMAR4 = RTC_ALRMAR4_RESET_VALUE;
  274. RTC->ALRMASSRH = RTC_ALRMASSRH_RESET_VALUE;
  275. RTC->ALRMASSRL = RTC_ALRMASSRL_RESET_VALUE;
  276. RTC->ALRMASSMSKR = RTC_ALRMASSMSKR_RESET_VALUE;
  277. /* Reset ISR register and exit initialization mode */
  278. RTC->ISR1 = (uint8_t)0x00;
  279. RTC->ISR2 = RTC_ISR2_RESET_VALUE;
  280. if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET)
  281. {
  282. while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
  283. {
  284. recalpfcount++;
  285. }
  286. }
  287. if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET)
  288. {
  289. RTC->CALRH = RTC_CALRH_RESET_VALUE;
  290. RTC->CALRL = RTC_CALRL_RESET_VALUE;
  291. if (RTC_WaitForSynchro() == ERROR)
  292. {
  293. status = ERROR;
  294. }
  295. else
  296. {
  297. status = SUCCESS;
  298. }
  299. }
  300. else
  301. {
  302. status = ERROR;
  303. }
  304. /* Enable the write protection for RTC registers */
  305. RTC->WPR = 0xFF;
  306. }
  307. }
  308. /* return Deinitialize RTC registers status*/
  309. return (ErrorStatus)status;
  310. }
  311. /**
  312. * @brief Initializes the RTC registers according to the specified parameters
  313. * in RTC_InitStruct.
  314. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
  315. * the configuration information for the RTC peripheral.
  316. * @note The RTC Prescaler register is write protected and can be written in
  317. * initialization mode only.
  318. * @retval An ErrorStatus enumeration value:
  319. * - SUCCESS: RTC registers are initialized
  320. * - ERROR: RTC registers are not initialized
  321. */
  322. ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
  323. {
  324. ErrorStatus status = ERROR;
  325. /* Check the parameters */
  326. assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
  327. assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
  328. assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
  329. /* Disable the write protection for RTC registers */
  330. RTC->WPR = 0xCA;
  331. RTC->WPR = 0x53;
  332. /* Set Initialization mode */
  333. if (RTC_EnterInitMode() == ERROR)
  334. {
  335. status = ERROR;
  336. }
  337. else
  338. {
  339. /* Clear the bits to be configured first */
  340. RTC->CR1 &= ((uint8_t)~( RTC_CR1_FMT ));
  341. /* Set RTC_CR1 register */
  342. RTC->CR1 |= ((uint8_t)(RTC_InitStruct->RTC_HourFormat));
  343. /* Set Prescalers registers */
  344. RTC->SPRERH = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv >> 8);
  345. RTC->SPRERL = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv);
  346. RTC->APRER = (uint8_t)(RTC_InitStruct->RTC_AsynchPrediv);
  347. /* Exit Initialization mode */
  348. RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
  349. status = SUCCESS;
  350. }
  351. /* Enable the write protection for RTC registers */
  352. RTC->WPR = 0xFF;
  353. /* return Initialize the RTC registers status*/
  354. return (ErrorStatus)(status);
  355. }
  356. /**
  357. * @brief Fills each RTC_InitStruct member with its default value
  358. * Hour format = 24h / Prescalers configured to their reset values.
  359. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
  360. * initialized.
  361. * @retval None
  362. */
  363. void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
  364. {
  365. /* Initialize the RTC_Hourformat member */
  366. RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
  367. /* Initialize the RTC_AsynchPrediv member */
  368. RTC_InitStruct->RTC_AsynchPrediv = RTC_APRER_RESET_VALUE;
  369. /* Initialize the RTC_SynchPrediv member */
  370. RTC_InitStruct->RTC_SynchPrediv = RTC_SPRERL_RESET_VALUE;
  371. }
  372. /**
  373. * @brief Enables or disables the RTC registers write protection.
  374. * @param NewState: new state of the write protection.
  375. * This parameter can be: ENABLE or DISABLE.
  376. * @note Writing a wrong key reactivates the write protection.
  377. * @note The protection mechanism is not affected by system reset.
  378. * @retval None
  379. */
  380. void RTC_WriteProtectionCmd(FunctionalState NewState)
  381. {
  382. /* Check the parameters */
  383. assert_param(IS_FUNCTIONAL_STATE(NewState));
  384. if (NewState != DISABLE)
  385. {
  386. /* Enable the write protection for RTC registers */
  387. RTC->WPR = RTC_WPR_EnableKey;
  388. }
  389. else
  390. {
  391. /* Disable the write protection for RTC registers */
  392. RTC->WPR = RTC_WPR_DisableKey1;
  393. RTC->WPR = RTC_WPR_DisableKey2;
  394. }
  395. }
  396. /**
  397. * @brief Enters the RTC Initialization mode.
  398. * @note The RTC Initialization mode is write protected, use the
  399. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  400. * @param None
  401. * @retval An ErrorStatus enumeration value:
  402. * - SUCCESS: RTC is in Init mode
  403. * - ERROR: RTC is not in Init mode
  404. */
  405. ErrorStatus RTC_EnterInitMode(void)
  406. {
  407. ErrorStatus status = ERROR;
  408. uint16_t initfcount = 0;
  409. /* Check if the Initialization mode is set */
  410. if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
  411. {
  412. /* Set the Initialization mode */
  413. RTC->ISR1 = (uint8_t)RTC_ISR1_INIT;
  414. /* Wait until INITF flag is set */
  415. while (((RTC->ISR1 & RTC_ISR1_INITF) == RESET) && ( initfcount != INITF_TIMEOUT))
  416. {
  417. initfcount++;
  418. }
  419. }
  420. if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
  421. {
  422. status = ERROR;
  423. }
  424. else
  425. {
  426. status = SUCCESS;
  427. }
  428. return (ErrorStatus)status;
  429. }
  430. /**
  431. * @brief Exits the RTC Initialization mode.
  432. * @note When the initialization sequence is complete, the Calendar restarts
  433. * counting after 4 RTCCLK cycles.
  434. * @note The RTC Initialization mode is write protected, use the
  435. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  436. * @param None
  437. * @retval None
  438. */
  439. void RTC_ExitInitMode(void)
  440. {
  441. /* Exit Initialization mode */
  442. RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
  443. }
  444. /**
  445. * @brief Waits until the RTC Calendar registers (Time and Date)
  446. * are synchronized with RTC clock.
  447. * @note This function is meaningless when BYPASS feature is enabled in RTC_CR1
  448. * register.
  449. * @note To read the Calendar through the shadow registers after Calendar
  450. * initialization, Calendar update or after wakeup from low power modes
  451. * the software must first clear the RSF flag.
  452. * The software must then wait until it is set again before reading
  453. * the Calendar (if not yet done), which means that the Calendar registers
  454. * have been correctly copied into the RTC_TRx and RTC_DRx shadow registers.
  455. * @note RTC_SetTime() and RTC_SetDate() functions call RTC_WaitForSynchro() function
  456. * after updating the Calendar. In Run mode, User can use RTC_GetDate(),
  457. * RTC_GetTime() and/or RTC_GetSubSecond() without need to call
  458. * RTC_WaitForSynchro() function. After waking up from low power mode, this
  459. * function must be called before calling RTC_GetDate(), RTC_GetTime() or
  460. * RTC_GetSubSecond() functions.
  461. * @param None
  462. * @retval An ErrorStatus enumeration value:
  463. * - SUCCESS: RTC registers are synchronized
  464. * - ERROR: RTC registers are not synchronized
  465. */
  466. ErrorStatus RTC_WaitForSynchro(void)
  467. {
  468. uint16_t rsfcount = 0;
  469. ErrorStatus status = ERROR;
  470. /* Disable the write protection for RTC registers */
  471. RTC->WPR = 0xCA;
  472. RTC->WPR = 0x53;
  473. /* Clear RSF flag by writing 0 in RSF bit */
  474. RTC->ISR1 &= (uint8_t)~(RTC_ISR1_RSF | RTC_ISR1_INIT);
  475. /* Wait the registers to be synchronised */
  476. while (((RTC->ISR1 & RTC_ISR1_RSF) == RESET) && ( rsfcount != RSF_TIMEOUT))
  477. {
  478. rsfcount++;
  479. }
  480. /* Check if RSF flag occurs*/
  481. if ((RTC->ISR1 & RTC_ISR1_RSF) != RESET)
  482. {
  483. status = SUCCESS;
  484. }
  485. else
  486. {
  487. status = ERROR;
  488. }
  489. /* Enable the write protection for RTC registers */
  490. RTC->WPR = 0xFF;
  491. return (ErrorStatus)status;
  492. }
  493. /**
  494. * @brief Enables or Disables the RTC Ratio.
  495. * @param NewState: new state of the Ratio feature.
  496. * This parameter can be: ENABLE or DISABLE.
  497. * @retval None
  498. */
  499. void RTC_RatioCmd(FunctionalState NewState)
  500. {
  501. /* Check the parameters */
  502. assert_param(IS_FUNCTIONAL_STATE(NewState));
  503. /* Disable the write protection for RTC registers */
  504. RTC->WPR = 0xCA;
  505. RTC->WPR = 0x53;
  506. if (NewState != DISABLE)
  507. {
  508. /* Set the RATIO bit */
  509. RTC->CR1 |= (uint8_t)RTC_CR1_RATIO;
  510. }
  511. else
  512. {
  513. /* Reset the RATIO bit */
  514. RTC->CR1 &= (uint8_t)~RTC_CR1_RATIO;
  515. }
  516. /* Enable the write protection for RTC registers */
  517. RTC->WPR = 0xFF;
  518. }
  519. /**
  520. * @brief Enables or Disables the Bypass Shadow feature.
  521. * @param NewState: new state of the Bypass Shadow feature.
  522. * This parameter can be: ENABLE or DISABLE.
  523. * @retval None
  524. */
  525. void RTC_BypassShadowCmd(FunctionalState NewState)
  526. {
  527. /* Check the parameters */
  528. assert_param(IS_FUNCTIONAL_STATE(NewState));
  529. /* Disable the write protection for RTC registers */
  530. RTC->WPR = 0xCA;
  531. RTC->WPR = 0x53;
  532. if (NewState != DISABLE)
  533. {
  534. /* Set the BYPSHAD bit */
  535. RTC->CR1 |= (uint8_t)RTC_CR1_BYPSHAD;
  536. }
  537. else
  538. {
  539. /* Reset the BYPSHAD bit */
  540. RTC->CR1 &= (uint8_t)~RTC_CR1_BYPSHAD;
  541. }
  542. /* Enable the write protection for RTC registers */
  543. RTC->WPR = 0xFF;
  544. }
  545. /**
  546. * @}
  547. */
  548. /** @defgroup RTC_Group2 Time and Date configuration functions
  549. * @brief Time and Date configuration functions
  550. *
  551. @verbatim
  552. ===============================================================================
  553. Time and Date configuration functions
  554. ===============================================================================
  555. This section provide functions allowing to program and read the RTC Calendar
  556. (Time and Date).
  557. @endverbatim
  558. * @{
  559. */
  560. /**
  561. * @brief Sets the RTC current time.
  562. * @note After updating the Calendar, this routine clears the RSF flag and waits
  563. * until it is set again (using RTC_WaitForSynchro() function) , which means
  564. * that the Calendar registers have been correctly copied into the RTC_TRx
  565. * and RTC_DRx shadow registers.
  566. * @param RTC_Format: specifies the format of the entered parameters.
  567. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  568. * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure that
  569. * contains the time configuration information for the RTC
  570. * @retval An ErrorStatus enumeration value:
  571. * - SUCCESS: RTC Time register is configured
  572. * - ERROR: RTC Time register is not configured
  573. */
  574. ErrorStatus RTC_SetTime(RTC_Format_TypeDef RTC_Format,
  575. RTC_TimeTypeDef* RTC_TimeStruct)
  576. {
  577. ErrorStatus status = ERROR;
  578. uint8_t temp = 0;
  579. /* Check the parameters */
  580. assert_param(IS_RTC_FORMAT(RTC_Format));
  581. if (RTC_Format == RTC_Format_BIN)
  582. {
  583. /* Check Hour Format (24h or 12h)*/
  584. if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
  585. {
  586. assert_param(IS_RTC_HOUR12_MAX(RTC_TimeStruct->RTC_Hours));
  587. assert_param(IS_RTC_HOUR12_MIN(RTC_TimeStruct->RTC_Hours));
  588. }
  589. else
  590. {
  591. assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
  592. }
  593. assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
  594. assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
  595. }
  596. else
  597. {
  598. /* Check Hour Format (24h or 12h)*/
  599. if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
  600. {
  601. assert_param(IS_RTC_HOUR12_MAX(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
  602. assert_param(IS_RTC_HOUR12_MIN(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
  603. }
  604. else
  605. {
  606. assert_param(IS_RTC_HOUR24(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
  607. }
  608. assert_param(IS_RTC_MINUTES(Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
  609. assert_param(IS_RTC_SECONDS(Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
  610. }
  611. /* Disable the write protection for RTC registers */
  612. RTC->WPR = 0xCA;
  613. RTC->WPR = 0x53;
  614. /* Set Initialization mode */
  615. if (RTC_EnterInitMode() == ERROR)
  616. {
  617. status = ERROR;
  618. /* Enable the write protection for RTC registers */
  619. RTC->WPR = 0xFF;
  620. }
  621. else
  622. {
  623. /* Check Hour Format is 12h)*/
  624. if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
  625. {
  626. assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
  627. temp = RTC_TimeStruct->RTC_H12;
  628. }
  629. else
  630. {
  631. temp = 0;
  632. }
  633. /* Check the input parameters format */
  634. if (RTC_Format != RTC_Format_BIN)
  635. {
  636. RTC->TR1 = (uint8_t)(RTC_TimeStruct->RTC_Seconds);
  637. RTC->TR2 = (uint8_t)(RTC_TimeStruct->RTC_Minutes) ;
  638. RTC->TR3 = (uint8_t)( temp | RTC_TimeStruct->RTC_Hours) ;
  639. }
  640. else
  641. {
  642. RTC->TR1 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Seconds));
  643. RTC->TR2 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Minutes)) ;
  644. RTC->TR3 = (uint8_t)( temp | ByteToBcd2(RTC_TimeStruct->RTC_Hours));
  645. }
  646. /* Read DR3 register to unfreeze calender registers */
  647. (void)(RTC->DR3);
  648. /* Exit Initialization mode */
  649. RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
  650. /* Enable the write protection for RTC registers */
  651. RTC->WPR = 0xFF;
  652. /* if RTC_CR1_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  653. if ((RTC->CR1 & RTC_CR1_BYPSHAD) == RESET)
  654. {
  655. if (RTC_WaitForSynchro() == ERROR)
  656. {
  657. status = ERROR;
  658. }
  659. else
  660. {
  661. status = SUCCESS;
  662. }
  663. }
  664. else
  665. {
  666. status = SUCCESS;
  667. }
  668. }
  669. return (ErrorStatus)status;
  670. }
  671. /**
  672. * @brief Fills each RTC_TimeStruct member with its default value
  673. * (Time = 00h:00min:00sec).
  674. * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure which will be
  675. * initialized.
  676. * @retval None
  677. */
  678. void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
  679. {
  680. /* Time = 00h:00min:00sec*/
  681. RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
  682. RTC_TimeStruct->RTC_Hours = 0;
  683. RTC_TimeStruct->RTC_Minutes = 0;
  684. RTC_TimeStruct->RTC_Seconds = 0;
  685. }
  686. /**
  687. * @brief Gets the RTC current Time.
  688. * @note To read the Calendar after wake up from low power mode, user software
  689. * must first check that the RSF flag is set in RTC_ISR1, using
  690. * RTC_WaitForSynchro() function, which means that the Calendar registers
  691. * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx).
  692. * @param RTC_Format: specifies the format of the returned parameters.
  693. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  694. * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure that
  695. * will contain the returned current time configuration.
  696. * @retval None
  697. */
  698. void RTC_GetTime(RTC_Format_TypeDef RTC_Format,
  699. RTC_TimeTypeDef* RTC_TimeStruct)
  700. {
  701. uint8_t tmpreg = 0;
  702. /* Check the parameters */
  703. assert_param(IS_RTC_FORMAT(RTC_Format));
  704. /* Fill the structure fields with the read parameters */
  705. /* Get RTC seconds */
  706. RTC_TimeStruct->RTC_Seconds = RTC->TR1;
  707. /* Get RTC Minutes */
  708. RTC_TimeStruct->RTC_Minutes = RTC->TR2;
  709. /* Get the RTC_TR3 register */
  710. tmpreg = (uint8_t)RTC->TR3;
  711. /* Read DR3 register to unfreeze calender registers */
  712. (void) (RTC->DR3) ;
  713. /* Get RTC Hours */
  714. RTC_TimeStruct->RTC_Hours = (uint8_t)(tmpreg & (uint8_t)~(RTC_TR3_PM));
  715. /* Get RTC H12 state */
  716. RTC_TimeStruct->RTC_H12 = (RTC_H12_TypeDef)(tmpreg & RTC_TR3_PM);
  717. /* Check the input parameters format */
  718. if (RTC_Format == RTC_Format_BIN)
  719. {
  720. /* Convert the structure parameters to Binary format */
  721. RTC_TimeStruct->RTC_Hours = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
  722. RTC_TimeStruct->RTC_Minutes = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
  723. RTC_TimeStruct->RTC_Seconds = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
  724. }
  725. }
  726. /**
  727. * @brief Gets the RTC current Calendar Subseconds value.
  728. * @note To read the Calendar after wake up from low power mode, user software
  729. * must first check that the RSF flag is set in RTC_ISR1, using
  730. * RTC_WaitForSynchro() function, which means that the Calendar registers
  731. * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx).
  732. * @param None
  733. * @retval RTC current Calendar Subseconds value.
  734. */
  735. uint16_t RTC_GetSubSecond(void)
  736. {
  737. uint8_t ssrhreg = 0, ssrlreg = 0;
  738. uint16_t ssrreg = 0;
  739. /* Get subseconds values from the correspondent registers*/
  740. ssrhreg = RTC->SSRH;
  741. ssrlreg = RTC->SSRL;
  742. /* Read DR3 register to unfreeze calender registers */
  743. (void) (RTC->DR3);
  744. ssrreg = (uint16_t)((uint16_t)((uint16_t)ssrhreg << 8) | (uint16_t)(ssrlreg));
  745. return (uint16_t)(ssrreg);
  746. }
  747. /**
  748. * @brief Set the RTC current date.
  749. * @note After updating the Calendar, this routine clears the RSF flag and waits
  750. * until it is set again (using RTC_WaitForSynchro() function) , which means
  751. * that the Calendar registers have been correctly copied into the RTC_TRx
  752. * and RTC_DRx shadow registers.
  753. * @param RTC_Format: specifies the format of the entered parameters.
  754. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  755. * @param RTC_DateStruct: pointer to a @ref RTC_TimeTypeDef structure that
  756. * contains the date configuration information for the RTC.
  757. * @retval An ErrorStatus enumeration value:
  758. * - SUCCESS: RTC Date register is configured
  759. * - ERROR: RTC Date register is not configured
  760. */
  761. ErrorStatus RTC_SetDate(RTC_Format_TypeDef RTC_Format,
  762. RTC_DateTypeDef* RTC_DateStruct)
  763. {
  764. ErrorStatus status = ERROR;
  765. if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & TEN_VALUE_BCD) == TEN_VALUE_BCD))
  766. {
  767. RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)((RTC_DateStruct->RTC_Month & (uint8_t)~(TEN_VALUE_BCD)) + TEN_VALUE_BIN);
  768. }
  769. /* Check the parameters */
  770. assert_param(IS_RTC_FORMAT(RTC_Format));
  771. if (RTC_Format == RTC_Format_BIN)
  772. {
  773. assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
  774. assert_param(IS_RTC_MONTH_MIN(RTC_DateStruct->RTC_Month));
  775. assert_param(IS_RTC_MONTH_MAX(RTC_DateStruct->RTC_Month));
  776. assert_param(IS_RTC_DATE_MIN(RTC_DateStruct->RTC_Date));
  777. assert_param(IS_RTC_DATE_MAX(RTC_DateStruct->RTC_Date));
  778. }
  779. else
  780. {
  781. assert_param(IS_RTC_YEAR(Bcd2ToByte(RTC_DateStruct->RTC_Year)));
  782. assert_param(IS_RTC_MONTH_MAX(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month)));
  783. assert_param(IS_RTC_MONTH_MIN(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month)));
  784. assert_param(IS_RTC_DATE_MIN(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date)));
  785. assert_param(IS_RTC_DATE_MAX(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date)));
  786. }
  787. assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
  788. /* Disable the write protection for RTC registers */
  789. RTC->WPR = 0xCA;
  790. RTC->WPR = 0x53;
  791. /* Set Initialization mode */
  792. if (RTC_EnterInitMode() == ERROR)
  793. {
  794. status = ERROR;
  795. /* Enable the write protection for RTC registers */
  796. RTC->WPR = 0xFF;
  797. }
  798. else
  799. {
  800. (void)(RTC->TR1);
  801. /* Set the RTC_DR registers */
  802. /* Check the input parameters format */
  803. if (RTC_Format != RTC_Format_BIN)
  804. {
  805. RTC->DR1 = (uint8_t)(RTC_DateStruct->RTC_Date);
  806. RTC->DR2 = (uint8_t)((RTC_DateStruct->RTC_Month) | (uint8_t)((RTC_DateStruct->RTC_WeekDay) << 5));
  807. RTC->DR3 = (uint8_t)((RTC_DateStruct->RTC_Year));
  808. }
  809. else
  810. {
  811. RTC->DR1 = (uint8_t)(ByteToBcd2 ((uint8_t)RTC_DateStruct->RTC_Date));
  812. RTC->DR2 = (uint8_t)((ByteToBcd2((uint8_t)RTC_DateStruct->RTC_Month)) | (uint8_t)((RTC_DateStruct->RTC_WeekDay) << 5));
  813. RTC->DR3 = (uint8_t)(ByteToBcd2((uint8_t)RTC_DateStruct->RTC_Year));
  814. }
  815. /* Exit Initialization mode */
  816. RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
  817. /* Enable the write protection for RTC registers */
  818. RTC->WPR = 0xFF;
  819. /* if RTC_CR1_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  820. if ((RTC->CR1 & RTC_CR1_BYPSHAD) == RESET)
  821. {
  822. if (RTC_WaitForSynchro() == ERROR)
  823. {
  824. status = ERROR;
  825. }
  826. else
  827. {
  828. status = SUCCESS;
  829. }
  830. }
  831. else
  832. {
  833. status = SUCCESS;
  834. }
  835. }
  836. return (ErrorStatus)status;
  837. }
  838. /**
  839. * @brief Fills each RTC_DateStruct member with its default value
  840. * (Monday 01 January xx00).
  841. * @param RTC_DateStruct: pointer to a @ref RTC_DateTypeDef structure which will be
  842. * initialized.
  843. * @retval None
  844. */
  845. void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
  846. {
  847. /* * (Monday 01 January xx00)*/
  848. RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
  849. RTC_DateStruct->RTC_Date = 1;
  850. RTC_DateStruct->RTC_Month = RTC_Month_January;
  851. RTC_DateStruct->RTC_Year = 0;
  852. }
  853. /**
  854. * @brief Get the RTC current date.
  855. * @note To read the Calendar after wake up from low power mode, user software
  856. * must first check that the RSF flag is set in RTC_ISR1, using
  857. * RTC_WaitForSynchro() function, which means that the Calendar registers
  858. * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx).
  859. * @param RTC_Format: specifies the format of the returned parameters.
  860. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  861. * @param RTC_DateStruct: pointer to a @ref RTC_DateTypeDef structure that
  862. * will contain the returned current Date configuration.
  863. * @retval None
  864. */
  865. void RTC_GetDate(RTC_Format_TypeDef RTC_Format,
  866. RTC_DateTypeDef* RTC_DateStruct)
  867. {
  868. uint8_t tmpreg = 0;
  869. /* Check the parameters */
  870. assert_param(IS_RTC_FORMAT(RTC_Format));
  871. /* Fill the structure fields with the read parameters */
  872. (void) (RTC->TR1) ;
  873. RTC_DateStruct->RTC_Date = (uint8_t)(RTC->DR1);
  874. tmpreg = (uint8_t)RTC->DR2;
  875. RTC_DateStruct->RTC_Year = (uint8_t)(RTC->DR3);
  876. RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)(tmpreg & (uint8_t)(RTC_DR2_MT | RTC_DR2_MU));
  877. RTC_DateStruct->RTC_WeekDay = (RTC_Weekday_TypeDef)((uint8_t)((uint8_t)tmpreg & (uint8_t)(RTC_DR2_WDU)) >> (uint8_t)5);
  878. /* Check the input parameters format */
  879. if (RTC_Format == RTC_Format_BIN)
  880. {
  881. /* Convert the structure parameters to Binary format */
  882. RTC_DateStruct->RTC_Year = (uint8_t)Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Year);
  883. RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month);
  884. RTC_DateStruct->RTC_Date = (uint8_t)(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date));
  885. }
  886. }
  887. /**
  888. * @}
  889. */
  890. /** @defgroup RTC_Group3 Alarms configuration functions
  891. * @brief Alarms (Alarm A and Alarm B) configuration functions
  892. *
  893. @verbatim
  894. ===============================================================================
  895. Alarms (Alarm A and Alarm B) configuration functions
  896. ===============================================================================
  897. This section provide functions allowing to program and read the RTC Alarms.
  898. @endverbatim
  899. * @{
  900. */
  901. /**
  902. * @brief Sets the RTC Alarm configuration.
  903. * @note Before configuring the Alarm settings, the Alarm Unit must be disabled
  904. * (if enabled) using RTC_AlarmCmd(DISABLE) function.
  905. * @param RTC_Format: specifies the format of the entered parameters.
  906. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  907. * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure that
  908. * contains the Alarm configuration information for the RTC.
  909. * @retval None.
  910. */
  911. void RTC_SetAlarm(RTC_Format_TypeDef RTC_Format,
  912. RTC_AlarmTypeDef* RTC_AlarmStruct)
  913. {
  914. uint8_t tmpreg1 = 0;
  915. uint8_t tmpreg2 = 0;
  916. uint8_t tmpreg3 = 0;
  917. uint8_t tmpreg4 = 0;
  918. /* Check the parameters */
  919. assert_param(IS_RTC_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
  920. assert_param(IS_RTC_FORMAT(RTC_Format));
  921. assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
  922. assert_param(IS_RTC_ALARM_DATEWEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
  923. if (RTC_Format == RTC_Format_BIN)
  924. {
  925. /* Check Hour Format (24h or 12h)*/
  926. if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
  927. {
  928. assert_param(IS_RTC_HOUR12_MAX(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  929. assert_param(IS_RTC_HOUR12_MIN(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  930. }
  931. else
  932. {
  933. assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  934. }
  935. assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
  936. assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
  937. }
  938. else
  939. {
  940. /* Check Hour Format (24h or 12h)*/
  941. if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
  942. {
  943. assert_param(IS_RTC_HOUR12_MAX(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
  944. assert_param(IS_RTC_HOUR12_MIN(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
  945. }
  946. else
  947. {
  948. assert_param(IS_RTC_HOUR24(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
  949. }
  950. assert_param(IS_RTC_MINUTES(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
  951. assert_param(IS_RTC_SECONDS(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
  952. }
  953. /* if Date/Weekday field is not masked */
  954. if ((RTC_AlarmStruct->RTC_AlarmMask & RTC_AlarmMask_DateWeekDay) == RESET )
  955. {
  956. if (RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_WeekDay)
  957. {
  958. assert_param(IS_RTC_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  959. }
  960. else
  961. {
  962. assert_param(IS_RTC_DATE_MIN(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  963. assert_param(IS_RTC_DATE_MAX(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  964. }
  965. }
  966. /* Disable the write protection for RTC registers */
  967. RTC->WPR = 0xCA;
  968. RTC->WPR = 0x53;
  969. /* Check the input parameters format & Configure the Alarm register */
  970. if (RTC_Format != RTC_Format_BIN)
  971. {
  972. tmpreg1 = (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
  973. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask) & (uint8_t)RTC_ALRMAR1_MSK1));
  974. tmpreg2 = (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) | \
  975. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 1) & (uint8_t)RTC_ALRMAR2_MSK2));
  976. tmpreg3 = (uint8_t)((uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) | \
  977. (uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12)) | \
  978. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 2) & (uint8_t)RTC_ALRMAR3_MSK3));
  979. tmpreg4 = (uint8_t)((uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) | \
  980. (uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel)) | \
  981. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 3) & (uint8_t)RTC_ALRMAR4_MSK4));
  982. }
  983. else
  984. {
  985. tmpreg1 = (uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
  986. (uint8_t)(RTC_AlarmStruct->RTC_AlarmMask & RTC_ALRMAR1_MSK1));
  987. tmpreg2 = (uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)) | \
  988. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 1) & (uint8_t)RTC_ALRMAR2_MSK2));
  989. tmpreg3 = (uint8_t)((uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)) | \
  990. (uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12)) | \
  991. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 2) & (uint8_t)RTC_ALRMAR3_MSK3));
  992. tmpreg4 = (uint8_t)((uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay)) | \
  993. (uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel)) | \
  994. (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 3) & (uint8_t)(RTC_ALRMAR4_MSK4)));
  995. }
  996. /* Configure the Alarm register */
  997. RTC->ALRMAR1 = tmpreg1;
  998. RTC->ALRMAR2 = tmpreg2;
  999. RTC->ALRMAR3 = tmpreg3;
  1000. RTC->ALRMAR4 = tmpreg4;
  1001. /* Enable the write protection for RTC registers */
  1002. RTC->WPR = 0xFF;
  1003. }
  1004. /**
  1005. * @brief Fills each RTC_AlarmStruct member with its default value
  1006. * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
  1007. * all fields are masked).
  1008. * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
  1009. * will be initialized.
  1010. * @retval None
  1011. */
  1012. void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
  1013. {
  1014. /* Alarm Time Settings : Time = 00h:00mn:00sec */
  1015. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  1016. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
  1017. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
  1018. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
  1019. /* Alarm Date Settings : Date = 1st day of the month*/
  1020. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  1021. RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
  1022. /* Alarm Masks Settings : Mask = all fields are masked*/
  1023. RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_All;
  1024. }
  1025. /**
  1026. * @brief Gets the RTC Alarm configuration.
  1027. * @param RTC_Format: specifies the format of the entered parameters.
  1028. * This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  1029. * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure that
  1030. * will contain the Alarm configuration information of the RTC.
  1031. * @retval None
  1032. */
  1033. void RTC_GetAlarm(RTC_Format_TypeDef RTC_Format,
  1034. RTC_AlarmTypeDef* RTC_AlarmStruct)
  1035. {
  1036. uint8_t tmpreg1 = 0;
  1037. uint8_t tmpreg2 = 0;
  1038. uint8_t tmpreg3 = 0;
  1039. uint8_t tmpreg4 = 0;
  1040. uint8_t alarmmask = 0;
  1041. /* Check the parameters */
  1042. assert_param(IS_RTC_FORMAT(RTC_Format));
  1043. /* Get Alarm registers data */
  1044. tmpreg1 = (uint8_t)RTC->ALRMAR1;
  1045. tmpreg2 = (uint8_t)RTC->ALRMAR2;
  1046. tmpreg3 = (uint8_t)RTC->ALRMAR3;
  1047. tmpreg4 = (uint8_t)RTC->ALRMAR4;
  1048. /* Fill the structure with the read parameters */
  1049. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint8_t)((uint8_t)tmpreg1 & (uint8_t)((uint8_t)RTC_ALRMAR1_ST | (uint8_t)RTC_ALRMAR1_SU));
  1050. alarmmask = (uint8_t)(tmpreg1 & RTC_ALRMAR1_MSK1);
  1051. /* Fill the structure with the read parameters */
  1052. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint8_t)((uint8_t)tmpreg2 & (uint8_t)((uint8_t)RTC_ALRMAR2_MNT | (uint8_t)RTC_ALRMAR2_MNU));
  1053. alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)(tmpreg2 & RTC_ALRMAR2_MSK2) >> 1));
  1054. /* Fill the structure with the read parameters */
  1055. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint8_t)((uint8_t)tmpreg3 & (uint8_t)((uint8_t)RTC_ALRMAR3_HT | (uint8_t)RTC_ALRMAR3_HU));
  1056. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (RTC_H12_TypeDef)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_PM);
  1057. alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_MSK3) >> 2));
  1058. /* Fill the structure with the read parameters */
  1059. RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint8_t)((uint8_t)tmpreg4 & (uint8_t)((uint8_t)RTC_ALRMAR4_DT | (uint8_t)RTC_ALRMAR4_DU));
  1060. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (RTC_AlarmDateWeekDaySel_TypeDef)((uint8_t)tmpreg4 & (uint8_t)RTC_ALRMAR4_WDSEL);
  1061. alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg4 & RTC_ALRMAR4_MSK4) >> 3));
  1062. RTC_AlarmStruct->RTC_AlarmMask = alarmmask;
  1063. if (RTC_Format == RTC_Format_BIN)
  1064. {
  1065. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
  1066. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes);
  1067. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds);
  1068. RTC_AlarmStruct->RTC_AlarmDateWeekDay = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  1069. }
  1070. }
  1071. /**
  1072. * @brief Enables or disables the RTC Alarm.
  1073. * @param NewState: new state of the alarm. This parameter can be: ENABLE or DISABLE.
  1074. * @retval An ErrorStatus enumeration value:
  1075. * - SUCCESS: RTC Alarm is enabled/disabled
  1076. * - ERROR: RTC Alarm is not enabled/disabled
  1077. */
  1078. ErrorStatus RTC_AlarmCmd(FunctionalState NewState)
  1079. {
  1080. __IO uint16_t alrawfcount = 0;
  1081. ErrorStatus status = ERROR;
  1082. uint8_t temp1 = 0;
  1083. /* Check the parameters */
  1084. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1085. /* Disable the write protection for RTC registers */
  1086. RTC->WPR = 0xCA;
  1087. RTC->WPR = 0x53;
  1088. /* Configure the Alarm state */
  1089. if (NewState != DISABLE)
  1090. { /*Enable the Alarm*/
  1091. RTC->CR2 |= (uint8_t)(RTC_CR2_ALRAE);
  1092. status = SUCCESS;
  1093. }
  1094. else
  1095. { /* Disable the Alarm */
  1096. RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE) ;
  1097. /* Wait until ALRxWF flag is set */
  1098. temp1 = (uint8_t)(RTC->ISR1 & RTC_ISR1_ALRAWF);
  1099. while ((alrawfcount != ALRAWF_TIMEOUT) && (temp1 == RESET))
  1100. {
  1101. alrawfcount++;
  1102. }
  1103. if ((RTC->ISR1 & RTC_ISR1_ALRAWF) == RESET)
  1104. {
  1105. status = ERROR;
  1106. }
  1107. else
  1108. {
  1109. status = SUCCESS;
  1110. }
  1111. }
  1112. /* Enable the write protection for RTC registers */
  1113. RTC->WPR = 0xFF;
  1114. /* Return the status*/
  1115. return (ErrorStatus)status;
  1116. }
  1117. /**
  1118. * @brief Configure the RTC Alarm Subseconds value and mask.
  1119. * @param RTC_AlarmSubSecondValue: specifies the Subseconds value.
  1120. * This parameter can be a value from 0 to 0x7FFF.
  1121. * @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask.
  1122. * This parameter can be one of the @ref RTC_AlarmSubSecondMask_TypeDef enumeration.
  1123. * @retval An ErrorStatus enumeration value:
  1124. * - SUCCESS: Alarm Subseconds value and mask are configured
  1125. * - ERROR: Alarm Subseconds value and mask are not configured
  1126. */
  1127. ErrorStatus RTC_AlarmSubSecondConfig(uint16_t RTC_AlarmSubSecondValue,
  1128. RTC_AlarmSubSecondMask_TypeDef RTC_AlarmSubSecondMask)
  1129. {
  1130. uint8_t alarmstatus = 0;
  1131. ErrorStatus status = ERROR;
  1132. /* Check the parameters */
  1133. assert_param(IS_RTC_ALARM_SS_VALUE(RTC_AlarmSubSecondValue));
  1134. assert_param(IS_RTC_ALARM_SS_MASK(RTC_AlarmSubSecondMask));
  1135. /* Disable the write protection for RTC registers */
  1136. RTC->WPR = 0xCA;
  1137. RTC->WPR = 0x53;
  1138. /* Check if the initialization mode is not set */
  1139. if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
  1140. {
  1141. /* save Alarm status */
  1142. alarmstatus = (uint8_t)(RTC->CR2 | RTC_CR2_ALRAE);
  1143. /* Disable the Alarm */
  1144. RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE);
  1145. /* Configure the Alarm register */
  1146. RTC->ALRMASSRH = (uint8_t)(RTC_AlarmSubSecondValue >> 8);
  1147. RTC->ALRMASSRL = (uint8_t)(RTC_AlarmSubSecondValue);
  1148. RTC->ALRMASSMSKR = (uint8_t)RTC_AlarmSubSecondMask;
  1149. /* restore the saved Alarm status */
  1150. RTC->CR2 |= alarmstatus;
  1151. status = SUCCESS;
  1152. }
  1153. else
  1154. {
  1155. status = ERROR;
  1156. }
  1157. /* Enable the write protection for RTC registers */
  1158. RTC->WPR = 0xFF;
  1159. return (ErrorStatus)status;
  1160. }
  1161. /**
  1162. * @}
  1163. */
  1164. /** @defgroup RTC_Group4 WakeUp Timer configuration functions
  1165. * @brief WakeUp Timer configuration functions
  1166. *
  1167. @verbatim
  1168. ===============================================================================
  1169. WakeUp Timer configuration functions
  1170. ===============================================================================
  1171. This section provide functions allowing to program and read the RTC WakeUp.
  1172. @endverbatim
  1173. * @{
  1174. */
  1175. /**
  1176. * @brief Configures the RTC Wakeup clock source.
  1177. * @pre Before configuring the wakeup unit Clock source, the wake up Unit must
  1178. * be disabled (if enabled) using RTC_WakeUpCmd(Disable) .
  1179. * @param RTC_WakeupClockSrc: specifies the Wakeup clock source,
  1180. * this parameter can be one of the @ref RTC_WakeupClockSrc_TypeDef enumeration.
  1181. * @retval None
  1182. */
  1183. void RTC_WakeUpClockConfig(RTC_WakeUpClock_TypeDef RTC_WakeUpClock)
  1184. {
  1185. /* Check the parameters */
  1186. assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
  1187. /* Disable the write protection for RTC registers */
  1188. RTC->WPR = 0xCA;
  1189. RTC->WPR = 0x53;
  1190. /* Disable the Wakeup timer in RTC_CR2 register */
  1191. RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE;
  1192. /* Clear the Wakeup Timer clock source bits in CR1 register */
  1193. RTC->CR1 &= (uint8_t)~RTC_CR1_WUCKSEL;
  1194. /* Configure the clock source */
  1195. RTC->CR1 |= (uint8_t)RTC_WakeUpClock;
  1196. /* Enable the write protection for RTC registers */
  1197. RTC->WPR = 0xFF;
  1198. }
  1199. /**
  1200. * @brief Sets the RTC Wakeup counter.
  1201. * @note Before configuring the wakeup unit counter, the wake up Unit must be
  1202. * disabled (if enabled) using RTC_WakeUpCmd(Disable).
  1203. * @param RTC_WakeupCounter: specifies the Wake up counter,
  1204. * This parameter can be a value from 0x0000 to 0xFFFF.
  1205. * @retval None.
  1206. */
  1207. void RTC_SetWakeUpCounter(uint16_t RTC_WakeupCounter)
  1208. {
  1209. /* Disable the write protection for RTC registers */
  1210. RTC->WPR = 0xCA;
  1211. RTC->WPR = 0x53;
  1212. /* Configure the Wakeup Timer counter */
  1213. RTC->WUTRH = (uint8_t)(RTC_WakeupCounter >> 8);
  1214. RTC->WUTRL = (uint8_t)(RTC_WakeupCounter);
  1215. /* Enable the write protection for RTC registers */
  1216. RTC->WPR = 0xFF;
  1217. }
  1218. /**
  1219. * @brief Returns the RTC Wakeup timer counter value.
  1220. * @param None.
  1221. * @retval RTC Wakeup Counter value.
  1222. */
  1223. uint16_t RTC_GetWakeUpCounter(void)
  1224. {
  1225. uint16_t tmpreg = 0;
  1226. /* Get the counter value */
  1227. tmpreg = ((uint16_t)RTC->WUTRH) << 8;
  1228. tmpreg |= RTC->WUTRL;
  1229. /* return RTC Wakeup Counter value*/
  1230. return (uint16_t)tmpreg;
  1231. }
  1232. /**
  1233. * @brief Enables or Disables the RTC Wakeup Unit.
  1234. * @param NewState: new state of the Wakeup Unit. This parameter can
  1235. * be: ENABLE or DISABLE.
  1236. * @retval An ErrorStatus enumeration value:
  1237. * - SUCCESS : RTC Wakeup Unit is enabled/disabled
  1238. * - ERROR : RTC Wakeup Unit is not enabled/disabled
  1239. */
  1240. ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
  1241. {
  1242. ErrorStatus status = ERROR;
  1243. uint16_t wutwfcount = 0;
  1244. /* Check the parameters */
  1245. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1246. /* Disable the write protection for RTC registers */
  1247. RTC->WPR = 0xCA;
  1248. RTC->WPR = 0x53;
  1249. if (NewState != DISABLE)
  1250. {
  1251. /* Enable the Wakeup Timer */
  1252. RTC->CR2 |= (uint8_t)RTC_CR2_WUTE;
  1253. status = SUCCESS;
  1254. }
  1255. else
  1256. {
  1257. /* Disable the Wakeup Timer */
  1258. RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE;
  1259. /* Wait until WUTWF flag is set */
  1260. while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT))
  1261. {
  1262. wutwfcount++;
  1263. }
  1264. /* Check WUTWF flag is set or not */
  1265. if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET)
  1266. {
  1267. status = ERROR;
  1268. }
  1269. else
  1270. {
  1271. status = SUCCESS;
  1272. }
  1273. }
  1274. /* Enable the write protection for RTC registers */
  1275. RTC->WPR = 0xFF;
  1276. /* Return the status*/
  1277. return (ErrorStatus)status;
  1278. }
  1279. /**
  1280. * @}
  1281. */
  1282. /** @defgroup RTC_Group5 Daylight Saving configuration functions
  1283. * @brief Daylight Saving configuration functions
  1284. *
  1285. @verbatim
  1286. ===============================================================================
  1287. Daylight Saving configuration functions
  1288. ===============================================================================
  1289. This section provide functions allowing to configure the RTC DayLight Saving.
  1290. @endverbatim
  1291. * @{
  1292. */
  1293. /**
  1294. * @brief Adds or subtracts one hour from the current time depending on
  1295. * the daylight saving parameter.
  1296. * @param RTC_DayLightSaving: the day light saving Mode
  1297. * This parameter can be one of the @ref RTC_DayLightSaving_TypeDef
  1298. * enumeration.
  1299. * @param RTC_StoreOperation: the day light saving store operation
  1300. * This parameter can be one of the @ref RTC_StoreOperation_TypeDef
  1301. * enumeration.
  1302. * @retval None
  1303. */
  1304. void RTC_DayLightSavingConfig(RTC_DayLightSaving_TypeDef RTC_DayLightSaving,
  1305. RTC_StoreOperation_TypeDef RTC_StoreOperation)
  1306. {
  1307. /* Check the parameters */
  1308. assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  1309. assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
  1310. /* Disable the write protection for RTC registers */
  1311. RTC->WPR = 0xCA;
  1312. RTC->WPR = 0x53;
  1313. /* Clear the bits to be configured */
  1314. RTC->CR3 &= (uint8_t)~(RTC_CR3_BCK);
  1315. /* Configure the RTC_CR3 register */
  1316. RTC->CR3 |= (uint8_t)((uint8_t)RTC_DayLightSaving | (uint8_t)RTC_StoreOperation);
  1317. /* Enable the write protection for RTC registers */
  1318. RTC->WPR = 0xFF;
  1319. }
  1320. /**
  1321. * @brief Returns the stored operation.
  1322. * @param None
  1323. * @retval the store operation, this parameter can be one of
  1324. * the @ref RTC_StoreOperation_TypeDef enumeration.
  1325. */
  1326. RTC_StoreOperation_TypeDef RTC_GetStoreOperation(void)
  1327. {
  1328. /* Return the stored operation*/
  1329. return (RTC_StoreOperation_TypeDef)(RTC->CR3 & RTC_CR3_BCK);
  1330. }
  1331. /**
  1332. * @}
  1333. */
  1334. /** @defgroup RTC_Group6 Output pin Configuration function
  1335. * @brief Output pin Configuration function
  1336. *
  1337. @verbatim
  1338. ===============================================================================
  1339. Output pin Configuration function
  1340. ===============================================================================
  1341. This section provide functions allowing to configure the RTC Output source.
  1342. @endverbatim
  1343. * @{
  1344. */
  1345. /**
  1346. * @brief Configures the RTC output for the output pin (RTC_ALARM output).
  1347. * @param RTC_OutputSel: Specifies which signal will be mapped to the output.
  1348. * This parameter can be one parameter from the
  1349. * @ref RTC_OutputSel_TypeDef enumeration.
  1350. * @param RTC_OutputPolarity: Specifies the polarity of the output signal.
  1351. * This parameter can be one parameter from the
  1352. * @ref RTC_OutputPolarity_TypeDef enumeration.
  1353. * @retval None
  1354. */
  1355. void RTC_OutputConfig(RTC_Output_TypeDef RTC_Output,
  1356. RTC_OutputPolarity_TypeDef RTC_OutputPolarity)
  1357. {
  1358. /* Check the parameters */
  1359. assert_param(IS_RTC_OUTPUT_SEL(RTC_Output));
  1360. assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
  1361. /* Disable the write protection for RTC registers */
  1362. RTC->WPR = 0xCA;
  1363. RTC->WPR = 0x53;
  1364. /* Clear the bits to be configured */
  1365. RTC->CR3 &= (uint8_t)~(RTC_CR3_OSEL | RTC_CR3_POL);
  1366. /* Configure the output selection and polarity */
  1367. RTC->CR3 |= (uint8_t)((uint8_t)RTC_Output | (uint8_t)RTC_OutputPolarity);
  1368. /* Enable the write protection for RTC registers */
  1369. RTC->WPR = 0xFF;
  1370. }
  1371. /**
  1372. * @}
  1373. */
  1374. /** @defgroup RTC_Group7 Output pin Configuration function
  1375. * @brief Output pin Configuration function
  1376. *
  1377. @verbatim
  1378. ===============================================================================
  1379. Output pin Configuration function
  1380. ===============================================================================
  1381. This section provide functions allowing to configure the RTC Output source.
  1382. @endverbatim
  1383. * @{
  1384. */
  1385. /**
  1386. * @brief Configures the Synchronization Shift Control Settings.
  1387. * @param RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
  1388. * This parameter can be one parameter from the @ref RTC_ShiftAdd1S_TypeDef
  1389. * enumeration.
  1390. * @param RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
  1391. * This parameter can be one any value from 0 to 0x7FFF.
  1392. * @retval An ErrorStatus enumeration value:
  1393. * - SUCCESS: RTC Shift registers are configured
  1394. * - ERROR: RTC Shift registers are not configured
  1395. */
  1396. ErrorStatus RTC_SynchroShiftConfig(RTC_ShiftAdd1S_TypeDef RTC_ShiftAdd1S,
  1397. uint16_t RTC_ShiftSubFS)
  1398. {
  1399. uint8_t shiftrhreg = 0;
  1400. ErrorStatus status = ERROR;
  1401. uint16_t shpfcount = 0;
  1402. /* Check the parameters */
  1403. assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
  1404. assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
  1405. /* Disable the write protection for RTC registers */
  1406. RTC->WPR = 0xCA;
  1407. RTC->WPR = 0x53;
  1408. /* Check if a Shift is pending*/
  1409. if ((RTC->ISR1 & RTC_ISR1_SHPF) != RESET)
  1410. {
  1411. /* wait until the shift is completed*/
  1412. while (((RTC->ISR1 & RTC_ISR1_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
  1413. {
  1414. shpfcount++;
  1415. }
  1416. }
  1417. /* check if the Shift pending is completed or if there is no Shift operation at all*/
  1418. if ((RTC->ISR1 & RTC_ISR1_SHPF) == RESET)
  1419. {
  1420. /* Configure the Shift settings */
  1421. shiftrhreg = (uint8_t)((uint8_t)(RTC_ShiftSubFS >> 8) | (uint8_t)(RTC_ShiftAdd1S));
  1422. RTC->SHIFTRH = (uint8_t)(shiftrhreg);
  1423. RTC->SHIFTRL = (uint8_t)(RTC_ShiftSubFS);
  1424. status = SUCCESS;
  1425. }
  1426. else
  1427. {
  1428. status = ERROR;
  1429. }
  1430. /* Enable the write protection for RTC registers */
  1431. RTC->WPR = 0xFF;
  1432. return (ErrorStatus)(status);
  1433. }
  1434. /**
  1435. * @}
  1436. */
  1437. /** @defgroup RTC_Group8 Output pin Configuration function
  1438. * @brief Output pin Configuration function
  1439. *
  1440. @verbatim
  1441. ===============================================================================
  1442. Output pin Configuration function
  1443. ===============================================================================
  1444. This section provide functions allowing to configure the RTC Output source.
  1445. @endverbatim
  1446. * @{
  1447. */
  1448. /**
  1449. * @brief Configures the Smooth Calibration Settings.
  1450. * @param RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
  1451. * This parameter can be one parameter from
  1452. * the @ref RTC_SmoothCalibPeriod_TypeDef enumeration.
  1453. * @param RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
  1454. * This parameter can be one parameter from the
  1455. * @ref RTC_SmoothCalibPlusPulses_TypeDef enumeration.
  1456. * @param RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  1457. * This parameter can be one any value from 0 to 0x01FF.
  1458. * @retval An ErrorStatus enumeration value:
  1459. * - SUCCESS: RTC Calib registers are configured
  1460. * - ERROR: RTC Calib registers are not configured
  1461. */
  1462. ErrorStatus RTC_SmoothCalibConfig(RTC_SmoothCalibPeriod_TypeDef RTC_SmoothCalibPeriod,
  1463. RTC_SmoothCalibPlusPulses_TypeDef RTC_SmoothCalibPlusPulses,
  1464. uint16_t RTC_SmouthCalibMinusPulsesValue)
  1465. {
  1466. ErrorStatus status = ERROR;
  1467. uint16_t recalpfcount = 0;
  1468. /* Check the parameters */
  1469. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
  1470. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
  1471. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
  1472. /* Disable the write protection for RTC registers */
  1473. RTC->WPR = 0xCA;
  1474. RTC->WPR = 0x53;
  1475. /* check if a calibration is pending*/
  1476. if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET)
  1477. {
  1478. /* wait until the Calibration is completed*/
  1479. while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
  1480. {
  1481. recalpfcount++;
  1482. }
  1483. }
  1484. /* check if the calibration pending is completed or if there is
  1485. no calibration operation at all*/
  1486. if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET)
  1487. {
  1488. /* Configure the Smooth calibration settings */
  1489. RTC->CALRH = (uint8_t)((uint8_t)((uint8_t)RTC_SmoothCalibPeriod | \
  1490. (uint8_t)RTC_SmoothCalibPlusPulses) | \
  1491. (uint8_t)((uint16_t)RTC_SmouthCalibMinusPulsesValue >> 8));
  1492. RTC->CALRL = (uint8_t)(RTC_SmouthCalibMinusPulsesValue);
  1493. status = SUCCESS;
  1494. }
  1495. else
  1496. {
  1497. status = ERROR;
  1498. }
  1499. /* Enable the write protection for RTC registers */
  1500. RTC->WPR = 0xFF;
  1501. return (ErrorStatus)(status);
  1502. }
  1503. /**
  1504. * @}
  1505. */
  1506. /** @defgroup RTC_Group9 Digital Calibration configuration functions
  1507. * @brief Digital Calibration configuration functions
  1508. *
  1509. @verbatim
  1510. ===============================================================================
  1511. Calibration configuration functions
  1512. ===============================================================================
  1513. @endverbatim
  1514. * @{
  1515. */
  1516. /**
  1517. * @brief Configure the Calibration Pin-out (RTC_CALIB) Selection (1Hz or 512Hz).
  1518. * @param RTC_CalibOutput : Select the Calibration output Selection .
  1519. * This parameter can be one parameter from the
  1520. * @ref RTC_CalibOutput_TypeDef enumeration.
  1521. * @retval None
  1522. */
  1523. void RTC_CalibOutputConfig(RTC_CalibOutput_TypeDef RTC_CalibOutput)
  1524. {
  1525. /* Check the parameters */
  1526. assert_param(IS_RTC_CALOUTPUT_SELECT(RTC_CalibOutput));
  1527. /* Disable the write protection for RTC registers */
  1528. RTC->WPR = 0xCA;
  1529. RTC->WPR = 0x53;
  1530. if (RTC_CalibOutput != RTC_CalibOutput_512Hz)
  1531. {
  1532. /* Enable the RTC clock output */
  1533. RTC->CR3 |= (uint8_t)RTC_CR3_COSEL;
  1534. }
  1535. else
  1536. {
  1537. /* Disable the RTC clock output */
  1538. RTC->CR3 &= (uint8_t)~RTC_CR3_COSEL;
  1539. }
  1540. /* Enable the write protection for RTC registers */
  1541. RTC->WPR = 0xFF;
  1542. }
  1543. /**
  1544. * @brief Enables or disables the RTC clock to be output through the relative pin.
  1545. * @param NewState: new state of the RTC Calibration output
  1546. * This parameter can be: ENABLE or DISABLE.
  1547. * @retval None
  1548. */
  1549. void RTC_CalibOutputCmd(FunctionalState NewState)
  1550. {
  1551. /* Check the parameters */
  1552. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1553. /* Disable the write protection for RTC registers */
  1554. RTC->WPR = 0xCA;
  1555. RTC->WPR = 0x53;
  1556. if (NewState != DISABLE)
  1557. {
  1558. /* Enable the RTC clock output */
  1559. RTC->CR3 |= (uint8_t)RTC_CR3_COE;
  1560. }
  1561. else
  1562. {
  1563. /* Disable the RTC clock output */
  1564. RTC->CR3 &= (uint8_t)~RTC_CR3_COE;
  1565. }
  1566. /* Enable the write protection for RTC registers */
  1567. RTC->WPR = 0xFF;
  1568. }
  1569. /**
  1570. * @}
  1571. */
  1572. /** @defgroup RTC_Group10 Tampers configuration functions
  1573. * @brief Tampers configuration functions
  1574. *
  1575. @verbatim
  1576. ===============================================================================
  1577. Tampers configuration functions
  1578. ===============================================================================
  1579. @endverbatim
  1580. * @{
  1581. */
  1582. /**
  1583. * @brief Configures the Tamper Sensitive Level.
  1584. * @param RTC_Tamper: Select the tamper to configure.
  1585. * This parameter can be one parameter from the @ref RTC_Tamper_TypeDef
  1586. * enumeration.
  1587. * @param RTC_TamperLevel: Select the tamper Sensitive Level.
  1588. * This parameter can be one parameter from the
  1589. * @ref RTC_TamperLevel_TypeDef enumeration.
  1590. * @retval None
  1591. */
  1592. void RTC_TamperLevelConfig(RTC_Tamper_TypeDef RTC_Tamper,
  1593. RTC_TamperLevel_TypeDef RTC_TamperLevel)
  1594. {
  1595. /* Check the parameters */
  1596. assert_param(IS_RTC_TAMPER((uint8_t)RTC_Tamper));
  1597. assert_param(IS_RTC_TAMPER_LEVEL(RTC_TamperLevel));
  1598. /* Disable the write protection for RTC registers */
  1599. RTC->WPR = 0xCA;
  1600. RTC->WPR = 0x53;
  1601. if (RTC_TamperLevel != RTC_TamperLevel_Low)
  1602. {
  1603. /* Enable the selected Tampers */
  1604. RTC->TCR1 |= (uint8_t)(RTC_Tamper << 1);
  1605. }
  1606. else
  1607. {
  1608. /* Disable the selected Tampers */
  1609. RTC->TCR1 &= (uint8_t)~(uint8_t)(RTC_Tamper << 1);
  1610. }
  1611. /* Enable the write protection for RTC registers */
  1612. RTC->WPR = 0xFF;
  1613. }
  1614. /**
  1615. * @brief Configures the Tampers Filter.
  1616. * @param RTC_TamperFilter: Specifies the tampers filter.
  1617. * This parameter can be one parameter from the
  1618. * @ref RTC_TamperFilter_TypeDef enumeration.
  1619. * @retval None
  1620. */
  1621. void RTC_TamperFilterConfig(RTC_TamperFilter_TypeDef RTC_TamperFilter)
  1622. {
  1623. /* Check the parameters */
  1624. assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
  1625. /* Disable the write protection for RTC registers */
  1626. RTC->WPR = 0xCA;
  1627. RTC->WPR = 0x53;
  1628. /*clear flags before configuration */
  1629. RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPFLT);
  1630. /* Configure the RTC_TCR register */
  1631. RTC->TCR2 |= (uint8_t)RTC_TamperFilter;
  1632. /* Enable the write protection for RTC registers */
  1633. RTC->WPR = 0xFF;
  1634. }
  1635. /**
  1636. * @brief Configures the Tampers Sampling Frequency.
  1637. * @param RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
  1638. * This parameter can be one parameter from the
  1639. * @ref RTC_TamperSamplingFreq_TypeDef enumeration.
  1640. * @retval None
  1641. */
  1642. void RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_TypeDef RTC_TamperSamplingFreq)
  1643. {
  1644. /* Check the parameters */
  1645. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
  1646. /* Disable the write protection for RTC registers */
  1647. RTC->WPR = 0xCA;
  1648. RTC->WPR = 0x53;
  1649. /* Clear flags before configuration */
  1650. RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPFREQ);
  1651. /* Configure the RTC_TCR register */
  1652. RTC->TCR2 |= (uint8_t)RTC_TamperSamplingFreq;
  1653. /* Enable the write protection for RTC registers */
  1654. RTC->WPR = 0xFF;
  1655. }
  1656. /**
  1657. * @brief Configures the Tampers Pins input Precharge Duration.
  1658. * @param RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
  1659. * Precharge Duration.
  1660. * This parameter can be one parameter from the
  1661. * @ref RTC_TamperPrechargeDuration_TypeDef enumeration.
  1662. * @retval None
  1663. */
  1664. void RTC_TamperPinsPrechargeDuration(RTC_TamperPrechargeDuration_TypeDef RTC_TamperPrechargeDuration)
  1665. {
  1666. /* Check the parameters */
  1667. assert_param(IS_RTC_TAMPER_PINS_PRECHAR_DURATION(RTC_TamperPrechargeDuration));
  1668. /* Disable the write protection for RTC registers */
  1669. RTC->WPR = 0xCA;
  1670. RTC->WPR = 0x53;
  1671. /* Clear the tampers pull-up and precharge/discharge duration Old settings*/
  1672. RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPPUDIS | RTC_TCR2_TAMPPRCH);
  1673. /* Configure the Tampers Precharge Duration and pull-up New settings*/
  1674. RTC->TCR2 |= (uint8_t)RTC_TamperPrechargeDuration;
  1675. /* Enable the write protection for RTC registers */
  1676. RTC->WPR = 0xFF;
  1677. }
  1678. /**
  1679. * @brief Enables or Disables the Tamper detection.
  1680. * @param RTC_Tamper: Select the tamper to configure.
  1681. * This parameter can be one parameter from the @ref RTC_Tamper_TypeDef
  1682. * enumeration.
  1683. * @param NewState: new state of the tamper pin.
  1684. * This parameter can be: ENABLE or DISABLE.
  1685. * @retval None
  1686. */
  1687. void RTC_TamperCmd(RTC_Tamper_TypeDef RTC_Tamper,
  1688. FunctionalState NewState)
  1689. {
  1690. /* Check the parameters */
  1691. assert_param(IS_RTC_TAMPER((uint8_t)RTC_Tamper));
  1692. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1693. /* Disable the write protection for RTC registers */
  1694. RTC->WPR = 0xCA;
  1695. RTC->WPR = 0x53;
  1696. if (NewState != DISABLE)
  1697. {
  1698. /* Enable the selected Tampers */
  1699. RTC->TCR1 |= (uint8_t)RTC_Tamper;
  1700. }
  1701. else
  1702. {
  1703. /* Disable the selected Tampers */
  1704. RTC->TCR1 &= (uint8_t)~RTC_Tamper;
  1705. }
  1706. /* Enable the write protection for RTC registers */
  1707. RTC->WPR = 0xFF;
  1708. }
  1709. /**
  1710. * @}
  1711. */
  1712. /** @defgroup RTC_Group11 Interrupts and flags management functions
  1713. * @brief Interrupts and flags management functions
  1714. *
  1715. @verbatim
  1716. ===============================================================================
  1717. Interrupts and flags management functions
  1718. ===============================================================================
  1719. To enable the RTC Alarm interrupt, the following sequence is required:
  1720. - Configure the RTC to generate RTC alarm (Alarm A) using the RTC_SetAlarm()
  1721. and RTC_AlarmCmd() functions.
  1722. To enable the RTC Wakeup interrupt, the following sequence is required:
  1723. - Configure the RTC to generate the RTC wakeup timer event using the
  1724. RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
  1725. To enable the RTC Tamper interrupt, the following sequence is required:
  1726. - Configure the RTC to detect the RTC tamper event using the
  1727. RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
  1728. @endverbatim
  1729. * @{
  1730. */
  1731. /**
  1732. * @brief Enables or Disables the specified RTC interrupts.
  1733. * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled.
  1734. * This parameter can be any combination of the following values:
  1735. * @arg RTC_IT_ALRA: Alarm A interrupt
  1736. * @arg RTC_IT_TAMP: Tampers interrupt
  1737. * @arg RTC_IT_WUT: WakeUp Timer interrupt
  1738. * @param NewState: new state of the specified RTC interrupts.
  1739. * This parameter can be: ENABLE or DISABLE.
  1740. * @retval None
  1741. */
  1742. void RTC_ITConfig(RTC_IT_TypeDef RTC_IT, FunctionalState NewState)
  1743. {
  1744. /* Check the parameters */
  1745. assert_param(IS_RTC_CONFIG_IT(RTC_IT));
  1746. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1747. /* Disable the write protection for RTC registers */
  1748. RTC->WPR = 0xCA;
  1749. RTC->WPR = 0x53;
  1750. if (NewState != DISABLE)
  1751. {
  1752. /* Enable the Interrupts */
  1753. RTC->CR2 |= (uint8_t)((uint16_t)RTC_IT & (uint16_t)0x00F0);
  1754. RTC->TCR1 |= (uint8_t)((uint16_t)RTC_IT & RTC_TCR1_TAMPIE);
  1755. }
  1756. else
  1757. {
  1758. /* Disable the Interrupts */
  1759. RTC->CR2 &= (uint8_t)~(uint8_t)((uint16_t)RTC_IT & (uint16_t)0x00F0);
  1760. RTC->TCR1 &= (uint8_t)~(uint8_t)((uint16_t)RTC_IT & (uint16_t)RTC_TCR1_TAMPIE);
  1761. }
  1762. /* Enable the write protection for RTC registers */
  1763. RTC->WPR = 0xFF;
  1764. }
  1765. /**
  1766. * @brief Checks whether the specified RTC flag is set or not.
  1767. * @param RTC_FLAG: specifies the flag to check.
  1768. * This parameter can be one of the @ref RTC_Flag_TypeDef enumeration.
  1769. * @retval Status of RTC_FLAG (SET or RESET)
  1770. */
  1771. FlagStatus RTC_GetFlagStatus(RTC_Flag_TypeDef RTC_FLAG)
  1772. {
  1773. FlagStatus flagstatus = RESET;
  1774. uint16_t tmpreg1 = 0;
  1775. uint16_t tmpreg2 = 0;
  1776. /* Check the parameters */
  1777. assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
  1778. /* Get all the flags */
  1779. tmpreg2 = (uint16_t)((uint16_t)RTC->ISR1 << 8);
  1780. tmpreg1 = (uint16_t)((uint16_t)((uint16_t)(RTC->ISR2)) | tmpreg2);
  1781. /* Return the status of the flag */
  1782. if ((tmpreg1 & (uint16_t)RTC_FLAG) != RESET)
  1783. {
  1784. flagstatus = SET;
  1785. }
  1786. else
  1787. {
  1788. flagstatus = RESET;
  1789. }
  1790. return (FlagStatus)flagstatus;
  1791. }
  1792. /**
  1793. * @brief Clears the RTC's pending flags.
  1794. * @param RTC_FLAG: specifies the RTC flag to clear.
  1795. * This parameter can be any combination of the @ref RTC_Flag_TypeDef
  1796. * enumeration.
  1797. * @retval None
  1798. */
  1799. void RTC_ClearFlag(RTC_Flag_TypeDef RTC_FLAG)
  1800. {
  1801. /* Check the parameters */
  1802. assert_param(IS_RTC_CLEAR_FLAG((uint16_t)RTC_FLAG));
  1803. /* Clear the Flags in the RTC_ISR registers */
  1804. RTC->ISR2 = (uint8_t)~((uint8_t)RTC_FLAG) ;
  1805. RTC->ISR1 = (uint8_t)(((uint8_t)~(uint8_t)((uint16_t)RTC_FLAG >> (uint8_t)8)) & ((uint8_t)~(uint8_t)(RTC_ISR1_INIT)));
  1806. }
  1807. /**
  1808. * @brief Checks whether the specified RTC interrupt has occurred or not.
  1809. * @param RTC_IT: specifies the RTC interrupt source to check.
  1810. * This parameter can be one of the @ref RTC_IT_TypeDef enumeration.
  1811. * @retval Status of RTC_IT (SET or RESET).
  1812. */
  1813. ITStatus RTC_GetITStatus(RTC_IT_TypeDef RTC_IT)
  1814. {
  1815. ITStatus itstatus = RESET;
  1816. uint8_t enablestatus = 0, tmpreg = 0;
  1817. /* Check the parameters */
  1818. assert_param(IS_RTC_GET_IT(RTC_IT));
  1819. /* Get the Interrupt enable Status */
  1820. enablestatus = (uint8_t)(RTC->CR2 & (uint16_t)RTC_IT);
  1821. /* Get the Interrupt pending bit */
  1822. tmpreg = (uint8_t)(RTC->ISR2 & (uint8_t)((uint16_t)RTC_IT >> 4));
  1823. /* Get the status of the Interrupt */
  1824. if ((enablestatus != (uint8_t)RESET) && (tmpreg != (uint8_t)RESET))
  1825. {
  1826. itstatus = SET;
  1827. }
  1828. else
  1829. {
  1830. itstatus = RESET;
  1831. }
  1832. return (ITStatus)itstatus;
  1833. }
  1834. /**
  1835. * @brief Clears the RTC's interrupt pending bits.
  1836. * @param RTC_IT: specifies the RTC interrupt pending bit to clear.
  1837. * This parameter can be any combination of the @ref RTC_IT_TypeDef
  1838. * enumeration.
  1839. * @retval None
  1840. */
  1841. void RTC_ClearITPendingBit(RTC_IT_TypeDef RTC_IT)
  1842. {
  1843. /* Check the parameters */
  1844. assert_param(IS_RTC_CLEAR_IT((uint16_t)RTC_IT));
  1845. /* Clear the interrupt pending bits in the RTC_ISR registers */
  1846. RTC->ISR2 = (uint8_t)~(uint8_t)((uint16_t)RTC_IT >> 4);
  1847. }
  1848. /**
  1849. * @}
  1850. */
  1851. /**
  1852. * @addtogroup RTC_Private_Functions
  1853. * @{
  1854. */
  1855. /**
  1856. * @brief Converts a 2 digit decimal to BCD format
  1857. * @param Value: Byte to be converted.
  1858. * @retval Converted byte
  1859. */
  1860. static uint8_t ByteToBcd2(uint8_t Value)
  1861. {
  1862. uint8_t bcdhigh = 0;
  1863. while (Value >= 10)
  1864. {
  1865. bcdhigh++;
  1866. Value -= 10;
  1867. }
  1868. return (uint8_t)((uint8_t)(bcdhigh << 4) | Value);
  1869. }
  1870. /**
  1871. * @brief Converts from 2 digit BCD to Binary format
  1872. * @param Value: BCD value to be converted.
  1873. * @retval Converted word
  1874. */
  1875. static uint8_t Bcd2ToByte(uint8_t Value)
  1876. {
  1877. uint8_t tmp = 0;
  1878. tmp = (uint8_t)((uint8_t)((uint8_t)(Value & (uint8_t)0xF0) >> 4) * (uint8_t)10);
  1879. return (uint8_t)(tmp + (Value & (uint8_t)0x0F));
  1880. }
  1881. /**
  1882. * @}
  1883. */
  1884. /**
  1885. * @}
  1886. */
  1887. /**
  1888. * @}
  1889. */
  1890. /**
  1891. * @}
  1892. */
  1893. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/