stm32f4xx_rtc.c 98 KB

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