stm32f0xx_rtc.c 93 KB

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