stm32f4xx_rtc.c 101 KB

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