stm32f4xx_rtc.c 99 KB

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