stm32f2xx_rtc.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_rtc.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  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. * - Alarms (Alarm A and Alarm B) configuration
  12. * - WakeUp Timer configuration
  13. * - Daylight Saving configuration
  14. * - Output pin Configuration
  15. * - Coarse Calibration configuration
  16. * - TimeStamp configuration
  17. * - Tampers configuration
  18. * - Backup Data Registers configuration
  19. * - RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration
  20. * - Interrupts and flags management
  21. *
  22. * @verbatim
  23. *
  24. * ===================================================================
  25. * Backup Domain Operating Condition
  26. * ===================================================================
  27. * The real-time clock (RTC), the RTC backup registers, and the backup
  28. * SRAM (BKP SRAM) can be powered from the VBAT voltage when the main
  29. * VDD supply is powered off.
  30. * To retain the content of the RTC backup registers, backup SRAM,
  31. * and supply the RTC when VDD is turned off, VBAT pin can be connected
  32. * to an optional standby voltage supplied by a battery or by another
  33. * source.
  34. *
  35. * To allow the RTC to operate even when the main digital supply (VDD)
  36. * is turned off, the VBAT pin powers the following blocks:
  37. * 1 - The RTC
  38. * 2 - The LSE oscillator
  39. * 3 - The backup SRAM when the low power backup regulator is enabled
  40. * 4 - PC13 to PC15 I/Os, plus PI8 I/O (when available)
  41. *
  42. * When the backup domain is supplied by VDD (analog switch connected
  43. * to VDD), the following functions are available:
  44. * 1 - PC14 and PC15 can be used as either GPIO or LSE pins
  45. * 2 - PC13 can be used as a GPIO or as the RTC_AF1 pin
  46. * 3 - PI8 can be used as a GPIO or as the RTC_AF2 pin
  47. *
  48. * When the backup domain is supplied by VBAT (analog switch connected
  49. * to VBAT because VDD is not present), the following functions are available:
  50. * 1 - PC14 and PC15 can be used as LSE pins only
  51. * 2 - PC13 can be used as the RTC_AF1 pin
  52. * 3 - PI8 can be used as the RTC_AF2 pin
  53. *
  54. * ===================================================================
  55. * Backup Domain Reset
  56. * ===================================================================
  57. * The backup domain reset sets all RTC registers and the RCC_BDCR
  58. * register to their reset values. The BKPSRAM is not affected by this
  59. * reset. The only way of resetting the BKPSRAM is through the Flash
  60. * interface by requesting a protection level change from 1 to 0.
  61. * A backup domain reset is generated when one of the following events
  62. * occurs:
  63. * 1 - Software reset, triggered by setting the BDRST bit in the
  64. * RCC Backup domain control register (RCC_BDCR). You can use the
  65. * RCC_BackupResetCmd().
  66. * 2 - VDD or VBAT power on, if both supplies have previously been
  67. * powered off.
  68. *
  69. * ===================================================================
  70. * Backup Domain Access
  71. * ===================================================================
  72. * After reset, the backup domain (RTC registers, RTC backup data
  73. * registers and backup SRAM) is protected against possible unwanted
  74. * write accesses.
  75. * To enable access to the RTC Domain and RTC registers, proceed as follows:
  76. * - Enable the Power Controller (PWR) APB1 interface clock using the
  77. * RCC_APB1PeriphClockCmd() function.
  78. * - Enable access to RTC domain using the PWR_BackupAccessCmd() function.
  79. * - Select the RTC clock source using the RCC_RTCCLKConfig() function.
  80. * - Enable RTC Clock using the RCC_RTCCLKCmd() function.
  81. *
  82. * ===================================================================
  83. * RTC Driver: how to use it
  84. * ===================================================================
  85. * - Enable the RTC domain access (see description in the section above)
  86. * - Configure the RTC Prescaler (Asynchronous and Synchronous) and
  87. * RTC hour format using the RTC_Init() function.
  88. *
  89. * Time and Date configuration
  90. * ===========================
  91. * - To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
  92. * and RTC_SetDate() functions.
  93. * - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
  94. * functions.
  95. * - Use the RTC_DayLightSavingConfig() function to add or sub one
  96. * hour to the RTC Calendar.
  97. *
  98. * Alarm configuration
  99. * ===================
  100. * - To configure the RTC Alarm use the RTC_SetAlarm() function.
  101. * - Enable the selected RTC Alarm using the RTC_AlarmCmd() function
  102. * - To read the RTC Alarm, use the RTC_GetAlarm() function.
  103. *
  104. * RTC Wakeup configuration
  105. * ========================
  106. * - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
  107. * function.
  108. * - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter()
  109. * function
  110. * - Enable the RTC WakeUp using the RTC_WakeUpCmd() function
  111. * - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter()
  112. * function.
  113. *
  114. * Outputs configuration
  115. * =====================
  116. * The RTC has 2 different outputs:
  117. * - AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
  118. * and WaKeUp signals.
  119. * To output the selected RTC signal on RTC_AF1 pin, use the
  120. * RTC_OutputConfig() function.
  121. * - AFO_CALIB: this output is used to manage the RTC Clock divided
  122. * by 64 (512Hz) signal.
  123. * To output the RTC Clock on RTC_AF1 pin, use the RTC_CalibOutputCmd()
  124. * function.
  125. *
  126. * Coarse Calibration configuration
  127. * =================================
  128. * - Configure the RTC Coarse Calibration Value and the corresponding
  129. * sign using the RTC_CoarseCalibConfig() function.
  130. * - Enable the RTC Coarse Calibration using the RTC_CoarseCalibCmd()
  131. * function
  132. *
  133. * TimeStamp configuration
  134. * =======================
  135. * - Configure the RTC_AF1 trigger and enables the RTC TimeStamp
  136. * using the RTC_TimeStampCmd() function.
  137. * - To read the RTC TimeStamp Time and Date register, use the
  138. * RTC_GetTimeStamp() function.
  139. * - The TAMPER1 alternate function can be mapped either to RTC_AF1(PC13)
  140. * or RTC_AF2 (PI8) depending on the value of TAMP1INSEL bit in
  141. * RTC_TAFCR register. You can use the RTC_TamperPinSelection()
  142. * function to select the corresponding pin.
  143. *
  144. * Tamper configuration
  145. * ====================
  146. * - Configure the RTC Tamper trigger using the RTC_TamperConfig()
  147. * function.
  148. * - Enable the RTC Tamper using the RTC_TamperCmd() function.
  149. * - The TIMESTAMP alternate function can be mapped to either RTC_AF1
  150. * or RTC_AF2 depending on the value of the TSINSEL bit in the
  151. * RTC_TAFCR register. You can use the RTC_TimeStampPinSelection()
  152. * function to select the corresponding pin.
  153. *
  154. * Backup Data Registers configuration
  155. * ===================================
  156. * - To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
  157. * function.
  158. * - To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
  159. * function.
  160. *
  161. * ===================================================================
  162. * RTC and low power modes
  163. * ===================================================================
  164. * The MCU can be woken up from a low power mode by an RTC alternate
  165. * function.
  166. * The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  167. * RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  168. * These RTC alternate functions can wake up the system from the Stop
  169. * and Standby lowpower modes.
  170. * The system can also wake up from low power modes without depending
  171. * on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  172. * or the RTC wakeup events.
  173. * The RTC provides a programmable time base for waking up from the
  174. * Stop or Standby mode at regular intervals.
  175. * Wakeup from STOP and Standby modes is possible only when the RTC
  176. * clock source is LSE or LSI.
  177. *
  178. * ===================================================================
  179. * Selection of RTC_AF1 alternate functions
  180. * ===================================================================
  181. * The RTC_AF1 pin (PC13) can be used for the following purposes:
  182. * - AFO_ALARM output
  183. * - AFO_CALIB output
  184. * - AFI_TAMPER
  185. * - AFI_TIMESTAMP
  186. *
  187. * +-------------------------------------------------------------------------------------------------------------+
  188. * | Pin |AFO_ALARM |AFO_CALIB |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL | TSINSEL |ALARMOUTTYPE |
  189. * | configuration | ENABLED | ENABLED | ENABLED | ENABLED |TAMPER1 pin |TIMESTAMP pin | AFO_ALARM |
  190. * | and function | | | | | selection | selection |Configuration |
  191. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  192. * | Alarm out | | | | | Don't | Don't | |
  193. * | output OD | 1 |Don't care|Don't care | Don't care | care | care | 0 |
  194. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  195. * | Alarm out | | | | | Don't | Don't | |
  196. * | output PP | 1 |Don't care|Don't care | Don't care | care | care | 1 |
  197. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  198. * | Calibration out | | | | | Don't | Don't | |
  199. * | output PP | 0 | 1 |Don't care | Don't care | care | care | Don't care |
  200. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  201. * | TAMPER input | | | | | | Don't | |
  202. * | floating | 0 | 0 | 1 | 0 | 0 | care | Don't care |
  203. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  204. * | TIMESTAMP and | | | | | | | |
  205. * | TAMPER input | 0 | 0 | 1 | 1 | 0 | 0 | Don't care |
  206. * | floating | | | | | | | |
  207. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  208. * | TIMESTAMP input | | | | | Don't | | |
  209. * | floating | 0 | 0 | 0 | 1 | care | 0 | Don't care |
  210. * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  211. * | Standard GPIO | 0 | 0 | 0 | 0 | Don't care | Don't care | Don't care |
  212. * +-------------------------------------------------------------------------------------------------------------+
  213. *
  214. *
  215. * ===================================================================
  216. * Selection of RTC_AF2 alternate functions
  217. * ===================================================================
  218. * The RTC_AF2 pin (PI8) can be used for the following purposes:
  219. * - AFI_TAMPER
  220. * - AFI_TIMESTAMP
  221. *
  222. * +---------------------------------------------------------------------------------------+
  223. * | Pin |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL | TSINSEL |ALARMOUTTYPE |
  224. * | configuration | ENABLED | ENABLED |TAMPER1 pin |TIMESTAMP pin | AFO_ALARM |
  225. * | and function | | | selection | selection |Configuration |
  226. * |-----------------|-----------|--------------|------------|--------------|--------------|
  227. * | TAMPER input | | | | Don't | |
  228. * | floating | 1 | 0 | 1 | care | Don't care |
  229. * |-----------------|-----------|--------------|------------|--------------|--------------|
  230. * | TIMESTAMP and | | | | | |
  231. * | TAMPER input | 1 | 1 | 1 | 1 | Don't care |
  232. * | floating | | | | | |
  233. * |-----------------|-----------|--------------|------------|--------------|--------------|
  234. * | TIMESTAMP input | | | Don't | | |
  235. * | floating | 0 | 1 | care | 1 | Don't care |
  236. * |-----------------|-----------|--------------|------------|--------------|--------------|
  237. * | Standard GPIO | 0 | 0 | Don't care | Don't care | Don't care |
  238. * +---------------------------------------------------------------------------------------+
  239. *
  240. *
  241. * @endverbatim
  242. *
  243. ******************************************************************************
  244. * @attention
  245. *
  246. * Copyright (c) 2012 STMicroelectronics.
  247. * All rights reserved.
  248. *
  249. * This software is licensed under terms that can be found in the LICENSE file
  250. * in the root directory of this software component.
  251. * If no LICENSE file comes with this software, it is provided AS-IS.
  252. *
  253. ******************************************************************************
  254. */
  255. /* Includes ------------------------------------------------------------------*/
  256. #include "stm32f2xx_rtc.h"
  257. #include "stm32f2xx_rcc.h"
  258. /** @addtogroup STM32F2xx_StdPeriph_Driver
  259. * @{
  260. */
  261. /** @defgroup RTC
  262. * @brief RTC driver modules
  263. * @{
  264. */
  265. /* Private typedef -----------------------------------------------------------*/
  266. /* Private define ------------------------------------------------------------*/
  267. /* Masks Definition */
  268. #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
  269. #define RTC_DR_RESERVED_MASK ((uint32_t)0x00FFFF3F)
  270. #define RTC_INIT_MASK ((uint32_t)0xFFFFFFFF)
  271. #define RTC_RSF_MASK ((uint32_t)0xFFFFFF5F)
  272. #define RTC_FLAGS_MASK ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
  273. RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
  274. RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
  275. RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F ))
  276. #define INITMODE_TIMEOUT ((uint32_t) 0x00010000)
  277. #define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
  278. /* Private macro -------------------------------------------------------------*/
  279. /* Private variables ---------------------------------------------------------*/
  280. /* Private function prototypes -----------------------------------------------*/
  281. static uint8_t RTC_ByteToBcd2(uint8_t Value);
  282. static uint8_t RTC_Bcd2ToByte(uint8_t Value);
  283. /* Private functions ---------------------------------------------------------*/
  284. /** @defgroup RTC_Private_Functions
  285. * @{
  286. */
  287. /** @defgroup RTC_Group1 Initialization and Configuration functions
  288. * @brief Initialization and Configuration functions
  289. *
  290. @verbatim
  291. ===============================================================================
  292. Initialization and Configuration functions
  293. ===============================================================================
  294. This section provide functions allowing to initialize and configure the RTC
  295. Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
  296. Write protection, enter and exit the RTC initialization mode, RTC registers
  297. synchronization check and reference clock detection enable.
  298. 1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
  299. split into 2 programmable prescalers to minimize power consumption.
  300. - A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
  301. - When both prescalers are used, it is recommended to configure the asynchronous
  302. prescaler to a high value to minimize consumption.
  303. 2. All RTC registers are Write protected. Writing to the RTC registers
  304. is enabled by writing a key into the Write Protection register, RTC_WPR.
  305. 3. To Configure the RTC Calendar, user application should enter initialization
  306. mode. In this mode, the calendar counter is stopped and its value can be
  307. updated. When the initialization sequence is complete, the calendar restarts
  308. counting after 4 RTCCLK cycles.
  309. 4. To read the calendar through the shadow registers after Calendar initialization,
  310. calendar update or after wakeup from low power modes the software must first
  311. clear the RSF flag. The software must then wait until it is set again before
  312. reading the calendar, which means that the calendar registers have been
  313. correctly copied into the RTC_TR and RTC_DR shadow registers.
  314. The RTC_WaitForSynchro() function implements the above software sequence
  315. (RSF clear and RSF check).
  316. @endverbatim
  317. * @{
  318. */
  319. /**
  320. * @brief Deinitializes the RTC registers to their default reset values.
  321. * @note This function doesn't reset the RTC Clock source and RTC Backup Data
  322. * registers.
  323. * @param None
  324. * @retval An ErrorStatus enumeration value:
  325. * - SUCCESS: RTC registers are deinitialized
  326. * - ERROR: RTC registers are not deinitialized
  327. */
  328. ErrorStatus RTC_DeInit(void)
  329. {
  330. __IO uint32_t wutcounter = 0x00;
  331. uint32_t wutwfstatus = 0x00;
  332. ErrorStatus status = ERROR;
  333. /* Disable the write protection for RTC registers */
  334. RTC->WPR = 0xCA;
  335. RTC->WPR = 0x53;
  336. /* Set Initialization mode */
  337. if (RTC_EnterInitMode() == ERROR)
  338. {
  339. status = ERROR;
  340. }
  341. else
  342. {
  343. /* Reset TR, DR and CR registers */
  344. RTC->TR = (uint32_t)0x00000000;
  345. RTC->DR = (uint32_t)0x00002101;
  346. /* Reset All CR bits except CR[2:0] */
  347. RTC->CR &= (uint32_t)0x00000007;
  348. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  349. do
  350. {
  351. wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
  352. wutcounter++;
  353. } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
  354. if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
  355. {
  356. status = ERROR;
  357. }
  358. else
  359. {
  360. /* Reset all RTC CR register bits */
  361. RTC->CR &= (uint32_t)0x00000000;
  362. RTC->WUTR = (uint32_t)0x0000FFFF;
  363. RTC->PRER = (uint32_t)0x007F00FF;
  364. RTC->CALIBR = (uint32_t)0x00000000;
  365. RTC->ALRMAR = (uint32_t)0x00000000;
  366. RTC->ALRMBR = (uint32_t)0x00000000;
  367. /* Reset ISR register and exit initialization mode */
  368. RTC->ISR = (uint32_t)0x00000000;
  369. /* Reset Tamper and alternate functions configuration register */
  370. RTC->TAFCR = 0x00000000;
  371. if(RTC_WaitForSynchro() == ERROR)
  372. {
  373. status = ERROR;
  374. }
  375. else
  376. {
  377. status = SUCCESS;
  378. }
  379. }
  380. }
  381. /* Enable the write protection for RTC registers */
  382. RTC->WPR = 0xFF;
  383. return status;
  384. }
  385. /**
  386. * @brief Initializes the RTC registers according to the specified parameters
  387. * in RTC_InitStruct.
  388. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
  389. * the configuration information for the RTC peripheral.
  390. * @note The RTC Prescaler register is write protected and can be written in
  391. * initialization mode only.
  392. * @retval An ErrorStatus enumeration value:
  393. * - SUCCESS: RTC registers are initialized
  394. * - ERROR: RTC registers are not initialized
  395. */
  396. ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
  397. {
  398. ErrorStatus status = ERROR;
  399. /* Check the parameters */
  400. assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
  401. assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
  402. assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
  403. /* Disable the write protection for RTC registers */
  404. RTC->WPR = 0xCA;
  405. RTC->WPR = 0x53;
  406. /* Set Initialization mode */
  407. if (RTC_EnterInitMode() == ERROR)
  408. {
  409. status = ERROR;
  410. }
  411. else
  412. {
  413. /* Clear RTC CR FMT Bit */
  414. RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
  415. /* Set RTC_CR register */
  416. RTC->CR |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
  417. /* Configure the RTC PRER */
  418. RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
  419. RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
  420. /* Exit Initialization mode */
  421. RTC_ExitInitMode();
  422. status = SUCCESS;
  423. }
  424. /* Enable the write protection for RTC registers */
  425. RTC->WPR = 0xFF;
  426. return status;
  427. }
  428. /**
  429. * @brief Fills each RTC_InitStruct member with its default value.
  430. * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
  431. * initialized.
  432. * @retval None
  433. */
  434. void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
  435. {
  436. /* Initialize the RTC_HourFormat member */
  437. RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
  438. /* Initialize the RTC_AsynchPrediv member */
  439. RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
  440. /* Initialize the RTC_SynchPrediv member */
  441. RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
  442. }
  443. /**
  444. * @brief Enables or disables the RTC registers write protection.
  445. * @note All the RTC registers are write protected except for RTC_ISR[13:8],
  446. * RTC_TAFCR and RTC_BKPxR.
  447. * @note Writing a wrong key reactivates the write protection.
  448. * @note The protection mechanism is not affected by system reset.
  449. * @param NewState: new state of the write protection.
  450. * This parameter can be: ENABLE or DISABLE.
  451. * @retval None
  452. */
  453. void RTC_WriteProtectionCmd(FunctionalState NewState)
  454. {
  455. /* Check the parameters */
  456. assert_param(IS_FUNCTIONAL_STATE(NewState));
  457. if (NewState != DISABLE)
  458. {
  459. /* Enable the write protection for RTC registers */
  460. RTC->WPR = 0xFF;
  461. }
  462. else
  463. {
  464. /* Disable the write protection for RTC registers */
  465. RTC->WPR = 0xCA;
  466. RTC->WPR = 0x53;
  467. }
  468. }
  469. /**
  470. * @brief Enters the RTC Initialization mode.
  471. * @note The RTC Initialization mode is write protected, use the
  472. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  473. * @param None
  474. * @retval An ErrorStatus enumeration value:
  475. * - SUCCESS: RTC is in Init mode
  476. * - ERROR: RTC is not in Init mode
  477. */
  478. ErrorStatus RTC_EnterInitMode(void)
  479. {
  480. __IO uint32_t initcounter = 0x00;
  481. ErrorStatus status = ERROR;
  482. uint32_t initstatus = 0x00;
  483. /* Check if the Initialization mode is set */
  484. if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  485. {
  486. /* Set the Initialization mode */
  487. RTC->ISR = (uint32_t)RTC_INIT_MASK;
  488. /* Wait till RTC is in INIT state and if Time out is reached exit */
  489. do
  490. {
  491. initstatus = RTC->ISR & RTC_ISR_INITF;
  492. initcounter++;
  493. } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  494. if ((RTC->ISR & RTC_ISR_INITF) != RESET)
  495. {
  496. status = SUCCESS;
  497. }
  498. else
  499. {
  500. status = ERROR;
  501. }
  502. }
  503. else
  504. {
  505. status = SUCCESS;
  506. }
  507. return (status);
  508. }
  509. /**
  510. * @brief Exits the RTC Initialization mode.
  511. * @note When the initialization sequence is complete, the calendar restarts
  512. * counting after 4 RTCCLK cycles.
  513. * @note The RTC Initialization mode is write protected, use the
  514. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  515. * @param None
  516. * @retval None
  517. */
  518. void RTC_ExitInitMode(void)
  519. {
  520. /* Exit Initialization mode */
  521. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  522. }
  523. /**
  524. * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  525. * synchronized with RTC APB clock.
  526. * @note The RTC Resynchronization mode is write protected, use the
  527. * RTC_WriteProtectionCmd(DISABLE) before calling this function.
  528. * @note To read the calendar through the shadow registers after Calendar
  529. * initialization, calendar update or after wakeup from low power modes
  530. * the software must first clear the RSF flag.
  531. * The software must then wait until it is set again before reading
  532. * the calendar, which means that the calendar registers have been
  533. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  534. * @param None
  535. * @retval An ErrorStatus enumeration value:
  536. * - SUCCESS: RTC registers are synchronised
  537. * - ERROR: RTC registers are not synchronised
  538. */
  539. ErrorStatus RTC_WaitForSynchro(void)
  540. {
  541. __IO uint32_t synchrocounter = 0;
  542. ErrorStatus status = ERROR;
  543. uint32_t synchrostatus = 0x00;
  544. /* Disable the write protection for RTC registers */
  545. RTC->WPR = 0xCA;
  546. RTC->WPR = 0x53;
  547. /* Clear RSF flag */
  548. RTC->ISR &= (uint32_t)RTC_RSF_MASK;
  549. /* Wait the registers to be synchronised */
  550. do
  551. {
  552. synchrostatus = RTC->ISR & RTC_ISR_RSF;
  553. synchrocounter++;
  554. } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
  555. if ((RTC->ISR & RTC_ISR_RSF) != RESET)
  556. {
  557. status = SUCCESS;
  558. }
  559. else
  560. {
  561. status = ERROR;
  562. }
  563. /* Enable the write protection for RTC registers */
  564. RTC->WPR = 0xFF;
  565. return (status);
  566. }
  567. /**
  568. * @brief Enables or disables the RTC reference clock detection.
  569. * @param NewState: new state of the RTC reference clock.
  570. * This parameter can be: ENABLE or DISABLE.
  571. * @retval An ErrorStatus enumeration value:
  572. * - SUCCESS: RTC reference clock detection is enabled
  573. * - ERROR: RTC reference clock detection is disabled
  574. */
  575. ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
  576. {
  577. ErrorStatus status = ERROR;
  578. /* Check the parameters */
  579. assert_param(IS_FUNCTIONAL_STATE(NewState));
  580. /* Disable the write protection for RTC registers */
  581. RTC->WPR = 0xCA;
  582. RTC->WPR = 0x53;
  583. /* Set Initialization mode */
  584. if (RTC_EnterInitMode() == ERROR)
  585. {
  586. status = ERROR;
  587. }
  588. else
  589. {
  590. if (NewState != DISABLE)
  591. {
  592. /* Enable the RTC reference clock detection */
  593. RTC->CR |= RTC_CR_REFCKON;
  594. }
  595. else
  596. {
  597. /* Disable the RTC reference clock detection */
  598. RTC->CR &= ~RTC_CR_REFCKON;
  599. }
  600. /* Exit Initialization mode */
  601. RTC_ExitInitMode();
  602. status = SUCCESS;
  603. }
  604. /* Enable the write protection for RTC registers */
  605. RTC->WPR = 0xFF;
  606. return status;
  607. }
  608. /**
  609. * @}
  610. */
  611. /** @defgroup RTC_Group2 Time and Date configuration functions
  612. * @brief Time and Date configuration functions
  613. *
  614. @verbatim
  615. ===============================================================================
  616. Time and Date configuration functions
  617. ===============================================================================
  618. This section provide functions allowing to program and read the RTC Calendar
  619. (Time and Date).
  620. @endverbatim
  621. * @{
  622. */
  623. /**
  624. * @brief Set the RTC current time.
  625. * @param RTC_Format: specifies the format of the entered parameters.
  626. * This parameter can be one of the following values:
  627. * @arg RTC_Format_BIN: Binary data format
  628. * @arg RTC_Format_BCD: BCD data format
  629. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains
  630. * the time configuration information for the RTC.
  631. * @retval An ErrorStatus enumeration value:
  632. * - SUCCESS: RTC Time register is configured
  633. * - ERROR: RTC Time register is not configured
  634. */
  635. ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
  636. {
  637. uint32_t tmpreg = 0;
  638. ErrorStatus status = ERROR;
  639. /* Check the parameters */
  640. assert_param(IS_RTC_FORMAT(RTC_Format));
  641. if (RTC_Format == RTC_Format_BIN)
  642. {
  643. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  644. {
  645. assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
  646. assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
  647. }
  648. else
  649. {
  650. RTC_TimeStruct->RTC_H12 = 0x00;
  651. assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
  652. }
  653. assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
  654. assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
  655. }
  656. else
  657. {
  658. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  659. {
  660. tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
  661. assert_param(IS_RTC_HOUR12(tmpreg));
  662. assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
  663. }
  664. else
  665. {
  666. RTC_TimeStruct->RTC_H12 = 0x00;
  667. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
  668. }
  669. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
  670. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
  671. }
  672. /* Check the input parameters format */
  673. if (RTC_Format != RTC_Format_BIN)
  674. {
  675. tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
  676. ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
  677. ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
  678. ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));
  679. }
  680. else
  681. {
  682. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
  683. ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
  684. ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
  685. (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
  686. }
  687. /* Disable the write protection for RTC registers */
  688. RTC->WPR = 0xCA;
  689. RTC->WPR = 0x53;
  690. /* Set Initialization mode */
  691. if (RTC_EnterInitMode() == ERROR)
  692. {
  693. status = ERROR;
  694. }
  695. else
  696. {
  697. /* Set the RTC_TR register */
  698. RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  699. /* Exit Initialization mode */
  700. RTC_ExitInitMode();
  701. if(RTC_WaitForSynchro() == ERROR)
  702. {
  703. status = ERROR;
  704. }
  705. else
  706. {
  707. status = SUCCESS;
  708. }
  709. }
  710. /* Enable the write protection for RTC registers */
  711. RTC->WPR = 0xFF;
  712. return status;
  713. }
  714. /**
  715. * @brief Fills each RTC_TimeStruct member with its default value
  716. * (Time = 00h:00min:00sec).
  717. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be
  718. * initialized.
  719. * @retval None
  720. */
  721. void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
  722. {
  723. /* Time = 00h:00min:00sec */
  724. RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
  725. RTC_TimeStruct->RTC_Hours = 0;
  726. RTC_TimeStruct->RTC_Minutes = 0;
  727. RTC_TimeStruct->RTC_Seconds = 0;
  728. }
  729. /**
  730. * @brief Get the RTC current Time.
  731. * @param RTC_Format: specifies the format of the returned parameters.
  732. * This parameter can be one of the following values:
  733. * @arg RTC_Format_BIN: Binary data format
  734. * @arg RTC_Format_BCD: BCD data format
  735. * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will
  736. * contain the returned current time configuration.
  737. * @retval None
  738. */
  739. void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
  740. {
  741. uint32_t tmpreg = 0;
  742. /* Check the parameters */
  743. assert_param(IS_RTC_FORMAT(RTC_Format));
  744. /* Get the RTC_TR register */
  745. tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
  746. /* Fill the structure fields with the read parameters */
  747. RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  748. RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
  749. RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  750. RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
  751. /* Check the input parameters format */
  752. if (RTC_Format == RTC_Format_BIN)
  753. {
  754. /* Convert the structure parameters to Binary format */
  755. RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
  756. RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
  757. RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
  758. }
  759. }
  760. /**
  761. * @brief Set the RTC current date.
  762. * @param RTC_Format: specifies the format of the entered parameters.
  763. * This parameter can be one of the following values:
  764. * @arg RTC_Format_BIN: Binary data format
  765. * @arg RTC_Format_BCD: BCD data format
  766. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
  767. * the date configuration information for the RTC.
  768. * @retval An ErrorStatus enumeration value:
  769. * - SUCCESS: RTC Date register is configured
  770. * - ERROR: RTC Date register is not configured
  771. */
  772. ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
  773. {
  774. uint32_t tmpreg = 0;
  775. ErrorStatus status = ERROR;
  776. /* Check the parameters */
  777. assert_param(IS_RTC_FORMAT(RTC_Format));
  778. if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
  779. {
  780. RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
  781. }
  782. if (RTC_Format == RTC_Format_BIN)
  783. {
  784. assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
  785. assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
  786. assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
  787. }
  788. else
  789. {
  790. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
  791. tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
  792. assert_param(IS_RTC_MONTH(tmpreg));
  793. tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
  794. assert_param(IS_RTC_DATE(tmpreg));
  795. }
  796. assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
  797. /* Check the input parameters format */
  798. if (RTC_Format != RTC_Format_BIN)
  799. {
  800. tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
  801. (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
  802. ((uint32_t)RTC_DateStruct->RTC_Date) | \
  803. (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
  804. }
  805. else
  806. {
  807. tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
  808. ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
  809. ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
  810. ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
  811. }
  812. /* Disable the write protection for RTC registers */
  813. RTC->WPR = 0xCA;
  814. RTC->WPR = 0x53;
  815. /* Set Initialization mode */
  816. if (RTC_EnterInitMode() == ERROR)
  817. {
  818. status = ERROR;
  819. }
  820. else
  821. {
  822. /* Set the RTC_DR register */
  823. RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
  824. /* Exit Initialization mode */
  825. RTC_ExitInitMode();
  826. if(RTC_WaitForSynchro() == ERROR)
  827. {
  828. status = ERROR;
  829. }
  830. else
  831. {
  832. status = SUCCESS;
  833. }
  834. }
  835. /* Enable the write protection for RTC registers */
  836. RTC->WPR = 0xFF;
  837. return status;
  838. }
  839. /**
  840. * @brief Fills each RTC_DateStruct member with its default value
  841. * (Monday, January 01 xx00).
  842. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be
  843. * initialized.
  844. * @retval None
  845. */
  846. void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
  847. {
  848. /* Monday, January 01 xx00 */
  849. RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
  850. RTC_DateStruct->RTC_Date = 1;
  851. RTC_DateStruct->RTC_Month = RTC_Month_January;
  852. RTC_DateStruct->RTC_Year = 0;
  853. }
  854. /**
  855. * @brief Get the RTC current date.
  856. * @param RTC_Format: specifies the format of the returned parameters.
  857. * This parameter can be one of the following values:
  858. * @arg RTC_Format_BIN: Binary data format
  859. * @arg RTC_Format_BCD: BCD data format
  860. * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will
  861. * contain the returned current date configuration.
  862. * @retval None
  863. */
  864. void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
  865. {
  866. uint32_t tmpreg = 0;
  867. /* Check the parameters */
  868. assert_param(IS_RTC_FORMAT(RTC_Format));
  869. /* Get the RTC_TR register */
  870. tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
  871. /* Fill the structure fields with the read parameters */
  872. RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
  873. RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  874. RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
  875. RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);
  876. /* Check the input parameters format */
  877. if (RTC_Format == RTC_Format_BIN)
  878. {
  879. /* Convert the structure parameters to Binary format */
  880. RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
  881. RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
  882. RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
  883. }
  884. }
  885. /**
  886. * @}
  887. */
  888. /** @defgroup RTC_Group3 Alarms configuration functions
  889. * @brief Alarms (Alarm A and Alarm B) configuration functions
  890. *
  891. @verbatim
  892. ===============================================================================
  893. Alarms (Alarm A and Alarm B) configuration functions
  894. ===============================================================================
  895. This section provide functions allowing to program and read the RTC Alarms.
  896. @endverbatim
  897. * @{
  898. */
  899. /**
  900. * @brief Set the specified RTC Alarm.
  901. * @note The Alarm register can only be written when the corresponding Alarm
  902. * is disabled (Use the RTC_AlarmCmd(DISABLE)).
  903. * @param RTC_Format: specifies the format of the returned parameters.
  904. * This parameter can be one of the following values:
  905. * @arg RTC_Format_BIN: Binary data format
  906. * @arg RTC_Format_BCD: BCD data format
  907. * @param RTC_Alarm: specifies the alarm to be configured.
  908. * This parameter can be one of the following values:
  909. * @arg RTC_Alarm_A: to select Alarm A
  910. * @arg RTC_Alarm_B: to select Alarm B
  911. * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
  912. * contains the alarm configuration parameters.
  913. * @retval None
  914. */
  915. void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
  916. {
  917. uint32_t tmpreg = 0;
  918. /* Check the parameters */
  919. assert_param(IS_RTC_FORMAT(RTC_Format));
  920. assert_param(IS_RTC_ALARM(RTC_Alarm));
  921. assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
  922. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
  923. if (RTC_Format == RTC_Format_BIN)
  924. {
  925. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  926. {
  927. assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
  928. assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
  929. }
  930. else
  931. {
  932. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
  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. if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
  938. {
  939. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  940. }
  941. else
  942. {
  943. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
  944. }
  945. }
  946. else
  947. {
  948. if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
  949. {
  950. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
  951. assert_param(IS_RTC_HOUR12(tmpreg));
  952. assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
  953. }
  954. else
  955. {
  956. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
  957. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
  958. }
  959. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
  960. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
  961. if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
  962. {
  963. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  964. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  965. }
  966. else
  967. {
  968. tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  969. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  970. }
  971. }
  972. /* Check the input parameters format */
  973. if (RTC_Format != RTC_Format_BIN)
  974. {
  975. tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
  976. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
  977. ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
  978. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
  979. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
  980. ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
  981. ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
  982. }
  983. else
  984. {
  985. tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
  986. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
  987. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
  988. ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
  989. ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
  990. ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
  991. ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
  992. }
  993. /* Disable the write protection for RTC registers */
  994. RTC->WPR = 0xCA;
  995. RTC->WPR = 0x53;
  996. /* Configure the Alarm register */
  997. if (RTC_Alarm == RTC_Alarm_A)
  998. {
  999. RTC->ALRMAR = (uint32_t)tmpreg;
  1000. }
  1001. else
  1002. {
  1003. RTC->ALRMBR = (uint32_t)tmpreg;
  1004. }
  1005. /* Enable the write protection for RTC registers */
  1006. RTC->WPR = 0xFF;
  1007. }
  1008. /**
  1009. * @brief Fills each RTC_AlarmStruct member with its default value
  1010. * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
  1011. * all fields are masked).
  1012. * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
  1013. * will be initialized.
  1014. * @retval None
  1015. */
  1016. void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
  1017. {
  1018. /* Alarm Time Settings : Time = 00h:00mn:00sec */
  1019. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  1020. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
  1021. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
  1022. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
  1023. /* Alarm Date Settings : Date = 1st day of the month */
  1024. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  1025. RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
  1026. /* Alarm Masks Settings : Mask = all fields are not masked */
  1027. RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
  1028. }
  1029. /**
  1030. * @brief Get the RTC Alarm value and masks.
  1031. * @param RTC_Format: specifies the format of the output parameters.
  1032. * This parameter can be one of the following values:
  1033. * @arg RTC_Format_BIN: Binary data format
  1034. * @arg RTC_Format_BCD: BCD data format
  1035. * @param RTC_Alarm: specifies the alarm to be read.
  1036. * This parameter can be one of the following values:
  1037. * @arg RTC_Alarm_A: to select Alarm A
  1038. * @arg RTC_Alarm_B: to select Alarm B
  1039. * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
  1040. * contains the output alarm configuration values.
  1041. * @retval None
  1042. */
  1043. void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
  1044. {
  1045. uint32_t tmpreg = 0;
  1046. /* Check the parameters */
  1047. assert_param(IS_RTC_FORMAT(RTC_Format));
  1048. assert_param(IS_RTC_ALARM(RTC_Alarm));
  1049. /* Get the RTC_ALRMxR register */
  1050. if (RTC_Alarm == RTC_Alarm_A)
  1051. {
  1052. tmpreg = (uint32_t)(RTC->ALRMAR);
  1053. }
  1054. else
  1055. {
  1056. tmpreg = (uint32_t)(RTC->ALRMBR);
  1057. }
  1058. /* Fill the structure with the read parameters */
  1059. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
  1060. RTC_ALRMAR_HU)) >> 16);
  1061. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
  1062. RTC_ALRMAR_MNU)) >> 8);
  1063. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
  1064. RTC_ALRMAR_SU));
  1065. RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
  1066. RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
  1067. RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1068. RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
  1069. if (RTC_Format == RTC_Format_BIN)
  1070. {
  1071. RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  1072. RTC_AlarmTime.RTC_Hours);
  1073. RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  1074. RTC_AlarmTime.RTC_Minutes);
  1075. RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
  1076. RTC_AlarmTime.RTC_Seconds);
  1077. RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  1078. }
  1079. }
  1080. /**
  1081. * @brief Enables or disables the specified RTC Alarm.
  1082. * @param RTC_Alarm: specifies the alarm to be configured.
  1083. * This parameter can be any combination of the following values:
  1084. * @arg RTC_Alarm_A: to select Alarm A
  1085. * @arg RTC_Alarm_B: to select Alarm B
  1086. * @param NewState: new state of the specified alarm.
  1087. * This parameter can be: ENABLE or DISABLE.
  1088. * @retval An ErrorStatus enumeration value:
  1089. * - SUCCESS: RTC Alarm is enabled/disabled
  1090. * - ERROR: RTC Alarm is not enabled/disabled
  1091. */
  1092. ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
  1093. {
  1094. __IO uint32_t alarmcounter = 0x00;
  1095. uint32_t alarmstatus = 0x00;
  1096. ErrorStatus status = ERROR;
  1097. /* Check the parameters */
  1098. assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
  1099. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1100. /* Disable the write protection for RTC registers */
  1101. RTC->WPR = 0xCA;
  1102. RTC->WPR = 0x53;
  1103. /* Configure the Alarm state */
  1104. if (NewState != DISABLE)
  1105. {
  1106. RTC->CR |= (uint32_t)RTC_Alarm;
  1107. status = SUCCESS;
  1108. }
  1109. else
  1110. {
  1111. /* Disable the Alarm in RTC_CR register */
  1112. RTC->CR &= (uint32_t)~RTC_Alarm;
  1113. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1114. do
  1115. {
  1116. alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
  1117. alarmcounter++;
  1118. } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
  1119. if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
  1120. {
  1121. status = ERROR;
  1122. }
  1123. else
  1124. {
  1125. status = SUCCESS;
  1126. }
  1127. }
  1128. /* Enable the write protection for RTC registers */
  1129. RTC->WPR = 0xFF;
  1130. return status;
  1131. }
  1132. /**
  1133. * @}
  1134. */
  1135. /** @defgroup RTC_Group4 WakeUp Timer configuration functions
  1136. * @brief WakeUp Timer configuration functions
  1137. *
  1138. @verbatim
  1139. ===============================================================================
  1140. WakeUp Timer configuration functions
  1141. ===============================================================================
  1142. This section provide functions allowing to program and read the RTC WakeUp.
  1143. @endverbatim
  1144. * @{
  1145. */
  1146. /**
  1147. * @brief Configures the RTC Wakeup clock source.
  1148. * @note The WakeUp Clock source can only be changed when the RTC WakeUp
  1149. * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
  1150. * @param RTC_WakeUpClock: Wakeup Clock source.
  1151. * This parameter can be one of the following values:
  1152. * @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16
  1153. * @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8
  1154. * @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4
  1155. * @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2
  1156. * @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE
  1157. * @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE
  1158. * @retval None
  1159. */
  1160. void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
  1161. {
  1162. /* Check the parameters */
  1163. assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
  1164. /* Disable the write protection for RTC registers */
  1165. RTC->WPR = 0xCA;
  1166. RTC->WPR = 0x53;
  1167. /* Clear the Wakeup Timer clock source bits in CR register */
  1168. RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  1169. /* Configure the clock source */
  1170. RTC->CR |= (uint32_t)RTC_WakeUpClock;
  1171. /* Enable the write protection for RTC registers */
  1172. RTC->WPR = 0xFF;
  1173. }
  1174. /**
  1175. * @brief Configures the RTC Wakeup counter.
  1176. * @note The RTC WakeUp counter can only be written when the RTC WakeUp
  1177. * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
  1178. * @param RTC_WakeUpCounter: specifies the WakeUp counter.
  1179. * This parameter can be a value from 0x0000 to 0xFFFF.
  1180. * @retval None
  1181. */
  1182. void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
  1183. {
  1184. /* Check the parameters */
  1185. assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
  1186. /* Disable the write protection for RTC registers */
  1187. RTC->WPR = 0xCA;
  1188. RTC->WPR = 0x53;
  1189. /* Configure the Wakeup Timer counter */
  1190. RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
  1191. /* Enable the write protection for RTC registers */
  1192. RTC->WPR = 0xFF;
  1193. }
  1194. /**
  1195. * @brief Returns the RTC WakeUp timer counter value.
  1196. * @param None
  1197. * @retval The RTC WakeUp Counter value.
  1198. */
  1199. uint32_t RTC_GetWakeUpCounter(void)
  1200. {
  1201. /* Get the counter value */
  1202. return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
  1203. }
  1204. /**
  1205. * @brief Enables or Disables the RTC WakeUp timer.
  1206. * @param NewState: new state of the WakeUp timer.
  1207. * This parameter can be: ENABLE or DISABLE.
  1208. * @retval None
  1209. */
  1210. ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
  1211. {
  1212. __IO uint32_t wutcounter = 0x00;
  1213. uint32_t wutwfstatus = 0x00;
  1214. ErrorStatus status = ERROR;
  1215. /* Check the parameters */
  1216. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1217. /* Disable the write protection for RTC registers */
  1218. RTC->WPR = 0xCA;
  1219. RTC->WPR = 0x53;
  1220. if (NewState != DISABLE)
  1221. {
  1222. /* Enable the Wakeup Timer */
  1223. RTC->CR |= (uint32_t)RTC_CR_WUTE;
  1224. status = SUCCESS;
  1225. }
  1226. else
  1227. {
  1228. /* Disable the Wakeup Timer */
  1229. RTC->CR &= (uint32_t)~RTC_CR_WUTE;
  1230. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1231. do
  1232. {
  1233. wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
  1234. wutcounter++;
  1235. } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
  1236. if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
  1237. {
  1238. status = ERROR;
  1239. }
  1240. else
  1241. {
  1242. status = SUCCESS;
  1243. }
  1244. }
  1245. /* Enable the write protection for RTC registers */
  1246. RTC->WPR = 0xFF;
  1247. return status;
  1248. }
  1249. /**
  1250. * @}
  1251. */
  1252. /** @defgroup RTC_Group5 Daylight Saving configuration functions
  1253. * @brief Daylight Saving configuration functions
  1254. *
  1255. @verbatim
  1256. ===============================================================================
  1257. Daylight Saving configuration functions
  1258. ===============================================================================
  1259. This section provide functions allowing to configure the RTC DayLight Saving.
  1260. @endverbatim
  1261. * @{
  1262. */
  1263. /**
  1264. * @brief Adds or substract one hour from the current time.
  1265. * @param RTC_DayLightSaveOperation: the value of hour adjustment.
  1266. * This parameter can be one of the following values:
  1267. * @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
  1268. * @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
  1269. * @param RTC_StoreOperation: Specifies the value to be written in the BCK bit
  1270. * in CR register to store the operation.
  1271. * This parameter can be one of the following values:
  1272. * @arg RTC_StoreOperation_Reset: BCK Bit Reset
  1273. * @arg RTC_StoreOperation_Set: BCK Bit Set
  1274. * @retval None
  1275. */
  1276. void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
  1277. {
  1278. /* Check the parameters */
  1279. assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  1280. assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
  1281. /* Disable the write protection for RTC registers */
  1282. RTC->WPR = 0xCA;
  1283. RTC->WPR = 0x53;
  1284. /* Clear the bits to be configured */
  1285. RTC->CR &= (uint32_t)~(RTC_CR_BCK);
  1286. /* Configure the RTC_CR register */
  1287. RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
  1288. /* Enable the write protection for RTC registers */
  1289. RTC->WPR = 0xFF;
  1290. }
  1291. /**
  1292. * @brief Returns the RTC Day Light Saving stored operation.
  1293. * @param None
  1294. * @retval RTC Day Light Saving stored operation.
  1295. * - RTC_StoreOperation_Reset
  1296. * - RTC_StoreOperation_Set
  1297. */
  1298. uint32_t RTC_GetStoreOperation(void)
  1299. {
  1300. return (RTC->CR & RTC_CR_BCK);
  1301. }
  1302. /**
  1303. * @}
  1304. */
  1305. /** @defgroup RTC_Group6 Output pin Configuration function
  1306. * @brief Output pin Configuration function
  1307. *
  1308. @verbatim
  1309. ===============================================================================
  1310. Output pin Configuration function
  1311. ===============================================================================
  1312. This section provide functions allowing to configure the RTC Output source.
  1313. @endverbatim
  1314. * @{
  1315. */
  1316. /**
  1317. * @brief Configures the RTC output source (AFO_ALARM).
  1318. * @param RTC_Output: Specifies which signal will be routed to the RTC output.
  1319. * This parameter can be one of the following values:
  1320. * @arg RTC_Output_Disable: No output selected
  1321. * @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
  1322. * @arg RTC_Output_AlarmB: signal of AlarmB mapped to output
  1323. * @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
  1324. * @param RTC_OutputPolarity: Specifies the polarity of the output signal.
  1325. * This parameter can be one of the following:
  1326. * @arg RTC_OutputPolarity_High: The output pin is high when the
  1327. * ALRAF/ALRBF/WUTF is high (depending on OSEL)
  1328. * @arg RTC_OutputPolarity_Low: The output pin is low when the
  1329. * ALRAF/ALRBF/WUTF is high (depending on OSEL)
  1330. * @retval None
  1331. */
  1332. void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
  1333. {
  1334. /* Check the parameters */
  1335. assert_param(IS_RTC_OUTPUT(RTC_Output));
  1336. assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
  1337. /* Disable the write protection for RTC registers */
  1338. RTC->WPR = 0xCA;
  1339. RTC->WPR = 0x53;
  1340. /* Clear the bits to be configured */
  1341. RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
  1342. /* Configure the output selection and polarity */
  1343. RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
  1344. /* Enable the write protection for RTC registers */
  1345. RTC->WPR = 0xFF;
  1346. }
  1347. /**
  1348. * @}
  1349. */
  1350. /** @defgroup RTC_Group7 Coarse Calibration configuration functions
  1351. * @brief Coarse Calibration configuration functions
  1352. *
  1353. @verbatim
  1354. ===============================================================================
  1355. Coarse Calibration configuration functions
  1356. ===============================================================================
  1357. @endverbatim
  1358. * @{
  1359. */
  1360. /**
  1361. * @brief Configures the Coarse calibration parameters.
  1362. * @param RTC_CalibSign: specifies the sign of the coarse calibration value.
  1363. * This parameter can be one of the following values:
  1364. * @arg RTC_CalibSign_Positive: The value sign is positive
  1365. * @arg RTC_CalibSign_Negative: The value sign is negative
  1366. * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
  1367. *
  1368. * @note This Calibration value should be between 0 and 63 when using negative
  1369. * sign with a 2-ppm step.
  1370. *
  1371. * @note This Calibration value should be between 0 and 126 when using positive
  1372. * sign with a 4-ppm step.
  1373. *
  1374. * @retval An ErrorStatus enumeration value:
  1375. * - SUCCESS: RTC Coarse calibration are initialized
  1376. * - ERROR: RTC Coarse calibration are not initialized
  1377. */
  1378. ErrorStatus RTC_CoarseCalibConfig(uint32_t RTC_CalibSign, uint32_t Value)
  1379. {
  1380. ErrorStatus status = ERROR;
  1381. /* Check the parameters */
  1382. assert_param(IS_RTC_CALIB_SIGN(RTC_CalibSign));
  1383. assert_param(IS_RTC_CALIB_VALUE(Value));
  1384. /* Disable the write protection for RTC registers */
  1385. RTC->WPR = 0xCA;
  1386. RTC->WPR = 0x53;
  1387. /* Set Initialization mode */
  1388. if (RTC_EnterInitMode() == ERROR)
  1389. {
  1390. status = ERROR;
  1391. }
  1392. else
  1393. {
  1394. /* Set the coarse calibration value */
  1395. RTC->CALIBR = (uint32_t)(RTC_CalibSign | Value);
  1396. /* Exit Initialization mode */
  1397. RTC_ExitInitMode();
  1398. status = SUCCESS;
  1399. }
  1400. /* Enable the write protection for RTC registers */
  1401. RTC->WPR = 0xFF;
  1402. return status;
  1403. }
  1404. /**
  1405. * @brief Enables or disables the Coarse calibration process.
  1406. * @param NewState: new state of the Coarse calibration.
  1407. * This parameter can be: ENABLE or DISABLE.
  1408. * @retval An ErrorStatus enumeration value:
  1409. * - SUCCESS: RTC Coarse calibration are enabled/disabled
  1410. * - ERROR: RTC Coarse calibration are not enabled/disabled
  1411. */
  1412. ErrorStatus RTC_CoarseCalibCmd(FunctionalState NewState)
  1413. {
  1414. ErrorStatus status = ERROR;
  1415. /* Check the parameters */
  1416. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1417. /* Disable the write protection for RTC registers */
  1418. RTC->WPR = 0xCA;
  1419. RTC->WPR = 0x53;
  1420. /* Set Initialization mode */
  1421. if (RTC_EnterInitMode() == ERROR)
  1422. {
  1423. status = ERROR;
  1424. }
  1425. else
  1426. {
  1427. if (NewState != DISABLE)
  1428. {
  1429. /* Enable the Coarse Calibration */
  1430. RTC->CR |= (uint32_t)RTC_CR_DCE;
  1431. }
  1432. else
  1433. {
  1434. /* Disable the Coarse Calibration */
  1435. RTC->CR &= (uint32_t)~RTC_CR_DCE;
  1436. }
  1437. /* Exit Initialization mode */
  1438. RTC_ExitInitMode();
  1439. status = SUCCESS;
  1440. }
  1441. /* Enable the write protection for RTC registers */
  1442. RTC->WPR = 0xFF;
  1443. return status;
  1444. }
  1445. /**
  1446. * @brief Enables or disables the RTC clock to be output through the relative pin.
  1447. * @param NewState: new state of the digital calibration Output.
  1448. * This parameter can be: ENABLE or DISABLE.
  1449. * @retval None
  1450. */
  1451. void RTC_CalibOutputCmd(FunctionalState NewState)
  1452. {
  1453. /* Check the parameters */
  1454. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1455. /* Disable the write protection for RTC registers */
  1456. RTC->WPR = 0xCA;
  1457. RTC->WPR = 0x53;
  1458. if (NewState != DISABLE)
  1459. {
  1460. /* Enable the RTC clock output */
  1461. RTC->CR |= (uint32_t)RTC_CR_COE;
  1462. }
  1463. else
  1464. {
  1465. /* Disable the RTC clock output */
  1466. RTC->CR &= (uint32_t)~RTC_CR_COE;
  1467. }
  1468. /* Enable the write protection for RTC registers */
  1469. RTC->WPR = 0xFF;
  1470. }
  1471. /**
  1472. * @}
  1473. */
  1474. /** @defgroup RTC_Group8 TimeStamp configuration functions
  1475. * @brief TimeStamp configuration functions
  1476. *
  1477. @verbatim
  1478. ===============================================================================
  1479. TimeStamp configuration functions
  1480. ===============================================================================
  1481. @endverbatim
  1482. * @{
  1483. */
  1484. /**
  1485. * @brief Enables or Disables the RTC TimeStamp functionality with the
  1486. * specified time stamp pin stimulating edge.
  1487. * @param RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  1488. * activated.
  1489. * This parameter can be one of the following:
  1490. * @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising
  1491. * edge of the related pin.
  1492. * @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the
  1493. * falling edge of the related pin.
  1494. * @param NewState: new state of the TimeStamp.
  1495. * This parameter can be: ENABLE or DISABLE.
  1496. * @retval None
  1497. */
  1498. void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
  1499. {
  1500. uint32_t tmpreg = 0;
  1501. /* Check the parameters */
  1502. assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  1503. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1504. /* Get the RTC_CR register and clear the bits to be configured */
  1505. tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  1506. /* Get the new configuration */
  1507. if (NewState != DISABLE)
  1508. {
  1509. tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
  1510. }
  1511. else
  1512. {
  1513. tmpreg |= (uint32_t)(RTC_TimeStampEdge);
  1514. }
  1515. /* Disable the write protection for RTC registers */
  1516. RTC->WPR = 0xCA;
  1517. RTC->WPR = 0x53;
  1518. /* Configure the Time Stamp TSEDGE and Enable bits */
  1519. RTC->CR = (uint32_t)tmpreg;
  1520. /* Enable the write protection for RTC registers */
  1521. RTC->WPR = 0xFF;
  1522. }
  1523. /**
  1524. * @brief Get the RTC TimeStamp value and masks.
  1525. * @param RTC_Format: specifies the format of the output parameters.
  1526. * This parameter can be one of the following values:
  1527. * @arg RTC_Format_BIN: Binary data format
  1528. * @arg RTC_Format_BCD: BCD data format
  1529. * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will
  1530. * contains the TimeStamp time values.
  1531. * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will
  1532. * contains the TimeStamp date values.
  1533. * @retval None
  1534. */
  1535. void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
  1536. RTC_DateTypeDef* RTC_StampDateStruct)
  1537. {
  1538. uint32_t tmptime = 0, tmpdate = 0;
  1539. /* Check the parameters */
  1540. assert_param(IS_RTC_FORMAT(RTC_Format));
  1541. /* Get the TimeStamp time and date registers values */
  1542. tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
  1543. tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
  1544. /* Fill the Time structure fields with the read parameters */
  1545. RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  1546. RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
  1547. RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  1548. RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
  1549. /* Fill the Date structure fields with the read parameters */
  1550. RTC_StampDateStruct->RTC_Year = 0;
  1551. RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  1552. RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  1553. RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
  1554. /* Check the input parameters format */
  1555. if (RTC_Format == RTC_Format_BIN)
  1556. {
  1557. /* Convert the Time structure parameters to Binary format */
  1558. RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
  1559. RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
  1560. RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
  1561. /* Convert the Date structure parameters to Binary format */
  1562. RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
  1563. RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
  1564. RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
  1565. }
  1566. }
  1567. /**
  1568. * @}
  1569. */
  1570. /** @defgroup RTC_Group9 Tampers configuration functions
  1571. * @brief Tampers configuration functions
  1572. *
  1573. @verbatim
  1574. ===============================================================================
  1575. Tampers configuration functions
  1576. ===============================================================================
  1577. @endverbatim
  1578. * @{
  1579. */
  1580. /**
  1581. * @brief Configures the select Tamper pin edge.
  1582. * @param RTC_Tamper: Selected tamper pin.
  1583. * This parameter can be RTC_Tamper_1.
  1584. * @param RTC_TamperTrigger: Specifies the trigger on the tamper pin that
  1585. * stimulates tamper event.
  1586. * This parameter can be one of the following values:
  1587. * @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
  1588. * @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
  1589. * @retval None
  1590. */
  1591. void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
  1592. {
  1593. /* Check the parameters */
  1594. assert_param(IS_RTC_TAMPER(RTC_Tamper));
  1595. assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
  1596. if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
  1597. {
  1598. /* Configure the RTC_TAFCR register */
  1599. RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
  1600. }
  1601. else
  1602. {
  1603. /* Configure the RTC_TAFCR register */
  1604. RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
  1605. }
  1606. }
  1607. /**
  1608. * @brief Enables or Disables the Tamper detection.
  1609. * @param RTC_Tamper: Selected tamper pin.
  1610. * This parameter can be RTC_Tamper_1.
  1611. * @param NewState: new state of the tamper pin.
  1612. * This parameter can be: ENABLE or DISABLE.
  1613. * @retval None
  1614. */
  1615. void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
  1616. {
  1617. /* Check the parameters */
  1618. assert_param(IS_RTC_TAMPER(RTC_Tamper));
  1619. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1620. if (NewState != DISABLE)
  1621. {
  1622. /* Enable the selected Tamper pin */
  1623. RTC->TAFCR |= (uint32_t)RTC_Tamper;
  1624. }
  1625. else
  1626. {
  1627. /* Disable the selected Tamper pin */
  1628. RTC->TAFCR &= (uint32_t)~RTC_Tamper;
  1629. }
  1630. }
  1631. /**
  1632. * @}
  1633. */
  1634. /** @defgroup RTC_Group10 Backup Data Registers configuration functions
  1635. * @brief Backup Data Registers configuration functions
  1636. *
  1637. @verbatim
  1638. ===============================================================================
  1639. Backup Data Registers configuration functions
  1640. ===============================================================================
  1641. @endverbatim
  1642. * @{
  1643. */
  1644. /**
  1645. * @brief Writes a data in a specified RTC Backup data register.
  1646. * @param RTC_BKP_DR: RTC Backup data Register number.
  1647. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1648. * specify the register.
  1649. * @param Data: Data to be written in the specified RTC Backup data register.
  1650. * @retval None
  1651. */
  1652. void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
  1653. {
  1654. __IO uint32_t tmp = 0;
  1655. /* Check the parameters */
  1656. assert_param(IS_RTC_BKP(RTC_BKP_DR));
  1657. tmp = RTC_BASE + 0x50;
  1658. tmp += (RTC_BKP_DR * 4);
  1659. /* Write the specified register */
  1660. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1661. }
  1662. /**
  1663. * @brief Reads data from the specified RTC Backup data Register.
  1664. * @param RTC_BKP_DR: RTC Backup data Register number.
  1665. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1666. * specify the register.
  1667. * @retval None
  1668. */
  1669. uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
  1670. {
  1671. __IO uint32_t tmp = 0;
  1672. /* Check the parameters */
  1673. assert_param(IS_RTC_BKP(RTC_BKP_DR));
  1674. tmp = RTC_BASE + 0x50;
  1675. tmp += (RTC_BKP_DR * 4);
  1676. /* Read the specified register */
  1677. return (*(__IO uint32_t *)tmp);
  1678. }
  1679. /**
  1680. * @}
  1681. */
  1682. /** @defgroup RTC_Group11 RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration functions
  1683. * @brief RTC Tamper and TimeStamp Pins Selection and Output Type Config
  1684. * configuration functions
  1685. *
  1686. @verbatim
  1687. ===============================================================================
  1688. RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration
  1689. functions
  1690. ===============================================================================
  1691. @endverbatim
  1692. * @{
  1693. */
  1694. /**
  1695. * @brief Selects the RTC Tamper Pin.
  1696. * @param RTC_TamperPin: specifies the RTC Tamper Pin.
  1697. * This parameter can be one of the following values:
  1698. * @arg RTC_TamperPin_PC13: PC13 is selected as RTC Tamper Pin.
  1699. * @arg RTC_TamperPin_PI8: PI8 is selected as RTC Tamper Pin.
  1700. * @retval None
  1701. */
  1702. void RTC_TamperPinSelection(uint32_t RTC_TamperPin)
  1703. {
  1704. /* Check the parameters */
  1705. assert_param(IS_RTC_TAMPER_PIN(RTC_TamperPin));
  1706. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPINSEL);
  1707. RTC->TAFCR |= (uint32_t)(RTC_TamperPin);
  1708. }
  1709. /**
  1710. * @brief Selects the RTC TimeStamp Pin.
  1711. * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
  1712. * This parameter can be one of the following values:
  1713. * @arg RTC_TimeStampPin_PC13: PC13 is selected as RTC TimeStamp Pin.
  1714. * @arg RTC_TimeStampPin_PI8: PI8 is selected as RTC TimeStamp Pin.
  1715. * @retval None
  1716. */
  1717. void RTC_TimeStampPinSelection(uint32_t RTC_TimeStampPin)
  1718. {
  1719. /* Check the parameters */
  1720. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  1721. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TSINSEL);
  1722. RTC->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  1723. }
  1724. /**
  1725. * @brief Configures the RTC Output Pin mode.
  1726. * @param RTC_OutputType: specifies the RTC Output (PC13) pin mode.
  1727. * This parameter can be one of the following values:
  1728. * @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in
  1729. * Open Drain mode.
  1730. * @arg RTC_OutputType_PushPull: RTC Output (PC13) is configured in
  1731. * Push Pull mode.
  1732. * @retval None
  1733. */
  1734. void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
  1735. {
  1736. /* Check the parameters */
  1737. assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
  1738. RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
  1739. RTC->TAFCR |= (uint32_t)(RTC_OutputType);
  1740. }
  1741. /**
  1742. * @}
  1743. */
  1744. /** @defgroup RTC_Group12 Interrupts and flags management functions
  1745. * @brief Interrupts and flags management functions
  1746. *
  1747. @verbatim
  1748. ===============================================================================
  1749. Interrupts and flags management functions
  1750. ===============================================================================
  1751. All RTC interrupts are connected to the EXTI controller.
  1752. - To enable the RTC Alarm interrupt, the following sequence is required:
  1753. - Configure and enable the EXTI Line 17 in interrupt mode and select the rising
  1754. edge sensitivity using the EXTI_Init() function.
  1755. - Configure and enable the RTC_Alarm IRQ channel in the NVIC using the NVIC_Init()
  1756. function.
  1757. - Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B) using
  1758. the RTC_SetAlarm() and RTC_AlarmCmd() functions.
  1759. - To enable the RTC Wakeup interrupt, the following sequence is required:
  1760. - Configure and enable the EXTI Line 22 in interrupt mode and select the rising
  1761. edge sensitivity using the EXTI_Init() function.
  1762. - Configure and enable the RTC_WKUP IRQ channel in the NVIC using the NVIC_Init()
  1763. function.
  1764. - Configure the RTC to generate the RTC wakeup timer event using the
  1765. RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
  1766. - To enable the RTC Tamper interrupt, the following sequence is required:
  1767. - Configure and enable the EXTI Line 21 in interrupt mode and select the rising
  1768. edge sensitivity using the EXTI_Init() function.
  1769. - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()
  1770. function.
  1771. - Configure the RTC to detect the RTC tamper event using the
  1772. RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
  1773. - To enable the RTC TimeStamp interrupt, the following sequence is required:
  1774. - Configure and enable the EXTI Line 21 in interrupt mode and select the rising
  1775. edge sensitivity using the EXTI_Init() function.
  1776. - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()
  1777. function.
  1778. - Configure the RTC to detect the RTC time-stamp event using the
  1779. RTC_TimeStampCmd() functions.
  1780. @endverbatim
  1781. * @{
  1782. */
  1783. /**
  1784. * @brief Enables or disables the specified RTC interrupts.
  1785. * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled.
  1786. * This parameter can be any combination of the following values:
  1787. * @arg RTC_IT_TS: Time Stamp interrupt mask
  1788. * @arg RTC_IT_WUT: WakeUp Timer interrupt mask
  1789. * @arg RTC_IT_ALRB: Alarm B interrupt mask
  1790. * @arg RTC_IT_ALRA: Alarm A interrupt mask
  1791. * @arg RTC_IT_TAMP: Tamper event interrupt mask
  1792. * @param NewState: new state of the specified RTC interrupts.
  1793. * This parameter can be: ENABLE or DISABLE.
  1794. * @retval None
  1795. */
  1796. void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
  1797. {
  1798. /* Check the parameters */
  1799. assert_param(IS_RTC_CONFIG_IT(RTC_IT));
  1800. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1801. /* Disable the write protection for RTC registers */
  1802. RTC->WPR = 0xCA;
  1803. RTC->WPR = 0x53;
  1804. if (NewState != DISABLE)
  1805. {
  1806. /* Configure the Interrupts in the RTC_CR register */
  1807. RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
  1808. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  1809. RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
  1810. }
  1811. else
  1812. {
  1813. /* Configure the Interrupts in the RTC_CR register */
  1814. RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
  1815. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  1816. RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
  1817. }
  1818. /* Enable the write protection for RTC registers */
  1819. RTC->WPR = 0xFF;
  1820. }
  1821. /**
  1822. * @brief Checks whether the specified RTC flag is set or not.
  1823. * @param RTC_FLAG: specifies the flag to check.
  1824. * This parameter can be one of the following values:
  1825. * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
  1826. * @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
  1827. * @arg RTC_FLAG_TSF: Time Stamp event flag
  1828. * @arg RTC_FLAG_WUTF: WakeUp Timer flag
  1829. * @arg RTC_FLAG_ALRBF: Alarm B flag
  1830. * @arg RTC_FLAG_ALRAF: Alarm A flag
  1831. * @arg RTC_FLAG_INITF: Initialization mode flag
  1832. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  1833. * @arg RTC_FLAG_INITS: Registers Configured flag
  1834. * @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag
  1835. * @arg RTC_FLAG_ALRBWF: Alarm B Write flag
  1836. * @arg RTC_FLAG_ALRAWF: Alarm A write flag
  1837. * @retval The new state of RTC_FLAG (SET or RESET).
  1838. */
  1839. FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
  1840. {
  1841. FlagStatus bitstatus = RESET;
  1842. uint32_t tmpreg = 0;
  1843. /* Check the parameters */
  1844. assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
  1845. /* Get all the flags */
  1846. tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
  1847. /* Return the status of the flag */
  1848. if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
  1849. {
  1850. bitstatus = SET;
  1851. }
  1852. else
  1853. {
  1854. bitstatus = RESET;
  1855. }
  1856. return bitstatus;
  1857. }
  1858. /**
  1859. * @brief Clears the RTC's pending flags.
  1860. * @param RTC_FLAG: specifies the RTC flag to clear.
  1861. * This parameter can be any combination of the following values:
  1862. * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
  1863. * @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag
  1864. * @arg RTC_FLAG_TSF: Time Stamp event flag
  1865. * @arg RTC_FLAG_WUTF: WakeUp Timer flag
  1866. * @arg RTC_FLAG_ALRBF: Alarm B flag
  1867. * @arg RTC_FLAG_ALRAF: Alarm A flag
  1868. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  1869. * @retval None
  1870. */
  1871. void RTC_ClearFlag(uint32_t RTC_FLAG)
  1872. {
  1873. /* Check the parameters */
  1874. assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
  1875. /* Clear the Flags in the RTC_ISR register */
  1876. RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x00003FF7) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
  1877. }
  1878. /**
  1879. * @brief Checks whether the specified RTC interrupt has occurred or not.
  1880. * @param RTC_IT: specifies the RTC interrupt source to check.
  1881. * This parameter can be one of the following values:
  1882. * @arg RTC_IT_TS: Time Stamp interrupt
  1883. * @arg RTC_IT_WUT: WakeUp Timer interrupt
  1884. * @arg RTC_IT_ALRB: Alarm B interrupt
  1885. * @arg RTC_IT_ALRA: Alarm A interrupt
  1886. * @arg RTC_IT_TAMP1: Tamper 1 event interrupt
  1887. * @retval The new state of RTC_IT (SET or RESET).
  1888. */
  1889. ITStatus RTC_GetITStatus(uint32_t RTC_IT)
  1890. {
  1891. ITStatus bitstatus = RESET;
  1892. uint32_t tmpreg = 0, enablestatus = 0;
  1893. /* Check the parameters */
  1894. assert_param(IS_RTC_GET_IT(RTC_IT));
  1895. /* Get the TAMPER Interrupt enable bit and pending bit */
  1896. tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
  1897. /* Get the Interrupt enable Status */
  1898. enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & (RTC_IT >> 15)));
  1899. /* Get the Interrupt pending bit */
  1900. tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
  1901. /* Get the status of the Interrupt */
  1902. if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
  1903. {
  1904. bitstatus = SET;
  1905. }
  1906. else
  1907. {
  1908. bitstatus = RESET;
  1909. }
  1910. return bitstatus;
  1911. }
  1912. /**
  1913. * @brief Clears the RTC's interrupt pending bits.
  1914. * @param RTC_IT: specifies the RTC interrupt pending bit to clear.
  1915. * This parameter can be any combination of the following values:
  1916. * @arg RTC_IT_TS: Time Stamp interrupt
  1917. * @arg RTC_IT_WUT: WakeUp Timer interrupt
  1918. * @arg RTC_IT_ALRB: Alarm B interrupt
  1919. * @arg RTC_IT_ALRA: Alarm A interrupt
  1920. * @arg RTC_IT_TAMP1: Tamper 1 event interrupt
  1921. * @retval None
  1922. */
  1923. void RTC_ClearITPendingBit(uint32_t RTC_IT)
  1924. {
  1925. uint32_t tmpreg = 0;
  1926. /* Check the parameters */
  1927. assert_param(IS_RTC_CLEAR_IT(RTC_IT));
  1928. /* Get the RTC_ISR Interrupt pending bits mask */
  1929. tmpreg = (uint32_t)(RTC_IT >> 4);
  1930. /* Clear the interrupt pending bits in the RTC_ISR register */
  1931. RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x00003FF7) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
  1932. }
  1933. /**
  1934. * @}
  1935. */
  1936. /**
  1937. * @brief Converts a 2 digit decimal to BCD format.
  1938. * @param Value: Byte to be converted.
  1939. * @retval Converted byte
  1940. */
  1941. static uint8_t RTC_ByteToBcd2(uint8_t Value)
  1942. {
  1943. uint8_t bcdhigh = 0;
  1944. while (Value >= 10)
  1945. {
  1946. bcdhigh++;
  1947. Value -= 10;
  1948. }
  1949. return ((uint8_t)(bcdhigh << 4) | Value);
  1950. }
  1951. /**
  1952. * @brief Convert from 2 digit BCD to Binary.
  1953. * @param Value: BCD value to be converted.
  1954. * @retval Converted word
  1955. */
  1956. static uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1957. {
  1958. uint8_t tmp = 0;
  1959. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1960. return (tmp + (Value & (uint8_t)0x0F));
  1961. }
  1962. /**
  1963. * @}
  1964. */
  1965. /**
  1966. * @}
  1967. */
  1968. /**
  1969. * @}
  1970. */