n32g45x_rtc.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_rtc.c
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_rtc.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup RTC
  40. * @brief RTC driver modules
  41. * @{
  42. */
  43. /* Masks Definition */
  44. #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
  45. #define RTC_DATE_RESERVED_MASK ((uint32_t)0x00FFFF3F)
  46. #define RTC_RSF_MASK ((uint32_t)0xFFFFFFDF)
  47. #define RTC_FLAGS_MASK \
  48. ((uint32_t)(RTC_FLAG_TISOVF | RTC_FLAG_TISF | RTC_FLAG_WTF | RTC_FLAG_ALBF | RTC_FLAG_ALAF | RTC_FLAG_INITF \
  49. | RTC_FLAG_RSYF | RTC_FLAG_INITSF | RTC_FLAG_WTWF | RTC_FLAG_ALBWF | RTC_FLAG_ALAWF | RTC_FLAG_RECPF \
  50. | RTC_FLAG_SHOPF))
  51. #define INITMODE_TIMEOUT ((uint32_t)0x00002000)
  52. #define SYNCHRO_TIMEOUT ((uint32_t)0x00008000)
  53. #define RECALPF_TIMEOUT ((uint32_t)0x00001000)
  54. #define SHPF_TIMEOUT ((uint32_t)0x00002000)
  55. static uint8_t RTC_ByteToBcd2(uint8_t Value);
  56. static uint8_t RTC_Bcd2ToByte(uint8_t Value);
  57. /** @addtogroup RTC_Private_Functions
  58. * @{
  59. */
  60. /** @addtogroup RTC_Group1 Initialization and Configuration functions
  61. * @brief Initialization and Configuration functions
  62. *
  63. @verbatim
  64. ===============================================================================
  65. ##### Initialization and Configuration functions #####
  66. ===============================================================================
  67. [..] This section provide functions allowing to initialize and configure the
  68. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  69. RTC registers Write protection, enter and exit the RTC initialization mode,
  70. RTC registers synchronization check and reference clock detection enable.
  71. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  72. It is split into 2 programmable prescalers to minimize power consumption.
  73. (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
  74. (++) When both prescalers are used, it is recommended to configure the
  75. asynchronous prescaler to a high value to minimize consumption.
  76. (#) All RTC registers are Write protected. Writing to the RTC registers
  77. is enabled by writing a key into the Write Protection register, RTC_WRP.
  78. (#) To Configure the RTC Calendar, user application should enter
  79. initialization mode. In this mode, the calendar counter is stopped
  80. and its value can be updated. When the initialization sequence is
  81. complete, the calendar restarts counting after 4 RTCCLK cycles.
  82. (#) To read the calendar through the shadow registers after Calendar
  83. initialization, calendar update or after wakeup from low power modes
  84. the software must first clear the RSYF flag. The software must then
  85. wait until it is set again before reading the calendar, which means
  86. that the calendar registers have been correctly copied into the
  87. RTC_TSH and RTC_DATE shadow registers.The RTC_WaitForSynchro() function
  88. implements the above software sequence (RSYF clear and RSYF check).
  89. @endverbatim
  90. * @{
  91. */
  92. /**
  93. * @brief Deinitializes the RTC registers to their default reset values.
  94. * @note This function doesn't reset the RTC Clock source
  95. * @return An ErrorStatus enumeration value:
  96. * - SUCCESS: RTC registers are deinitialized
  97. * - ERROR: RTC registers are not deinitialized
  98. */
  99. ErrorStatus RTC_DeInit(void)
  100. {
  101. __IO uint32_t wutcounter = 0x00;
  102. uint32_t wutwfstatus = 0x00;
  103. ErrorStatus status = ERROR;
  104. /* Disable the write protection for RTC registers */
  105. RTC->WRP = 0xCA;
  106. RTC->WRP = 0x53;
  107. /* Set Initialization mode */
  108. if (RTC_EnterInitMode() == ERROR)
  109. {
  110. status = ERROR;
  111. }
  112. else
  113. {
  114. /* Reset TSH, DAT and CTRL registers */
  115. RTC->TSH = (uint32_t)0x00000000;
  116. RTC->DATE = (uint32_t)0x00002101;
  117. /* Reset All CTRL bits except CTRL[2:0] */
  118. RTC->CTRL &= (uint32_t)0x00000007;
  119. /* Wait till RTC WTWF flag is set and if Time out is reached exit */
  120. do
  121. {
  122. wutwfstatus = RTC->INITSTS & RTC_INITSTS_WTWF;
  123. wutcounter++;
  124. } while ((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
  125. if ((RTC->INITSTS & RTC_INITSTS_WTWF) == RESET)
  126. {
  127. status = ERROR;
  128. }
  129. else
  130. {
  131. /* Reset all RTC CTRL register bits */
  132. RTC->CTRL &= (uint32_t)0x00000000;
  133. RTC->WKUPT = (uint32_t)0x0000FFFF;
  134. RTC->PRE = (uint32_t)0x007F00FF;
  135. RTC->ALARMA = (uint32_t)0x00000000;
  136. RTC->ALARMB = (uint32_t)0x00000000;
  137. RTC->SCTRL = (uint32_t)0x00000000;
  138. RTC->CALIB = (uint32_t)0x00000000;
  139. RTC->ALRMASS = (uint32_t)0x00000000;
  140. RTC->ALRMBSS = (uint32_t)0x00000000;
  141. /* Reset INTSTS register and exit initialization mode */
  142. RTC->INITSTS = (uint32_t)0x00000000;
  143. RTC->OPT = (uint32_t)0x00000000;
  144. RTC->TSCWKUPCTRL = (uint32_t)0x00000008;
  145. RTC->TSCWKUPCNT = (uint32_t)0x000002FE;
  146. /* Wait till the RTC RSYF flag is set */
  147. if (RTC_WaitForSynchro() == ERROR)
  148. {
  149. status = ERROR;
  150. }
  151. else
  152. {
  153. status = SUCCESS;
  154. }
  155. }
  156. }
  157. /* Enable the write protection for RTC registers */
  158. RTC->WRP = 0xFF;
  159. return status;
  160. }
  161. /**
  162. * @brief Initializes the RTC registers according to the specified parameters
  163. * in RTC_InitStruct.
  164. * @param RTC_InitStruct pointer to a RTC_InitType structure that contains
  165. * the configuration information for the RTC peripheral.
  166. * @note The RTC Prescaler register is write protected and can be written in
  167. * initialization mode only.
  168. * @return An ErrorStatus enumeration value:
  169. * - SUCCESS: RTC registers are initialized
  170. * - ERROR: RTC registers are not initialized
  171. */
  172. ErrorStatus RTC_Init(RTC_InitType* RTC_InitStruct)
  173. {
  174. ErrorStatus status = ERROR;
  175. uint32_t i =0;
  176. /* Check the parameters */
  177. assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
  178. assert_param(IS_RTC_PREDIV_ASYNCH(RTC_InitStruct->RTC_AsynchPrediv));
  179. assert_param(IS_RTC_PREDIV_SYNCH(RTC_InitStruct->RTC_SynchPrediv));
  180. /* Disable the write protection for RTC registers */
  181. RTC->WRP = 0xCA;
  182. RTC->WRP = 0x53;
  183. /* Set Initialization mode */
  184. if (RTC_EnterInitMode() == ERROR)
  185. {
  186. status = ERROR;
  187. }
  188. else
  189. {
  190. /* Clear RTC CTRL HFMT Bit */
  191. RTC->CTRL &= ((uint32_t) ~(RTC_CTRL_HFMT));
  192. /* Set RTC_CTRL register */
  193. RTC->CTRL |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
  194. /* Configure the RTC PRE */
  195. RTC->PRE = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
  196. RTC->PRE |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
  197. /* Exit Initialization mode */
  198. RTC_ExitInitMode();
  199. status = SUCCESS;
  200. }
  201. /* Enable the write protection for RTC registers */
  202. RTC->WRP = 0xFF;
  203. /* Delay for the RTC prescale effect */
  204. for(i=0;i<0x2FF;i++);
  205. return status;
  206. }
  207. /**
  208. * @brief Fills each RTC_InitStruct member with its default value.
  209. * @param RTC_InitStruct pointer to a RTC_InitType structure which will be
  210. * initialized.
  211. */
  212. void RTC_StructInit(RTC_InitType* RTC_InitStruct)
  213. {
  214. /* Initialize the RTC_HourFormat member */
  215. RTC_InitStruct->RTC_HourFormat = RTC_24HOUR_FORMAT;
  216. /* Initialize the RTC_AsynchPrediv member */
  217. RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
  218. /* Initialize the RTC_SynchPrediv member */
  219. RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
  220. }
  221. /**
  222. * @brief Enables or disables the RTC registers write protection.
  223. * @note All the RTC registers are write protected except for RTC_INITSTS[13:8].
  224. * @note Writing a wrong key reactivates the write protection.
  225. * @note The protection mechanism is not affected by system reset.
  226. * @param Cmd new state of the write protection.
  227. * This parameter can be: ENABLE or DISABLE.
  228. */
  229. void RTC_EnableWriteProtection(FunctionalState Cmd)
  230. {
  231. /* Check the parameters */
  232. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  233. if (Cmd != DISABLE)
  234. {
  235. /* Enable the write protection for RTC registers */
  236. RTC->WRP = 0xFF;
  237. }
  238. else
  239. {
  240. /* Disable the write protection for RTC registers */
  241. RTC->WRP = 0xCA;
  242. RTC->WRP = 0x53;
  243. }
  244. }
  245. /**
  246. * @brief Enters the RTC Initialization mode.
  247. * @note The RTC Initialization mode is write protected, use the
  248. * RTC_EnableWriteProtection(DISABLE) before calling this function.
  249. * @return An ErrorStatus enumeration value:
  250. * - SUCCESS: RTC is in Init mode
  251. * - ERROR: RTC is not in Init mode
  252. */
  253. ErrorStatus RTC_EnterInitMode(void)
  254. {
  255. __IO uint32_t initcounter = 0x00;
  256. ErrorStatus status = ERROR;
  257. uint32_t initstatus = 0x00;
  258. /* Check if the Initialization mode is set */
  259. if ((RTC->INITSTS & RTC_INITSTS_INITF) == (uint32_t)RESET)
  260. {
  261. /* Set the Initialization mode */
  262. RTC->INITSTS = (uint32_t)RTC_INITSTS_INITM;
  263. /* Wait till RTC is in INIT state and if Time out is reached exit */
  264. do
  265. {
  266. initstatus = RTC->INITSTS & RTC_INITSTS_INITF;
  267. initcounter++;
  268. } while ((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
  269. if ((RTC->INITSTS & RTC_INITSTS_INITF) != RESET)
  270. {
  271. status = SUCCESS;
  272. }
  273. else
  274. {
  275. status = ERROR;
  276. }
  277. }
  278. else
  279. {
  280. status = SUCCESS;
  281. }
  282. return (status);
  283. }
  284. /**
  285. * @brief Exits the RTC Initialization mode.
  286. * @note When the initialization sequence is complete, the calendar restarts
  287. * counting after 4 RTCCLK cycles.
  288. * @note The RTC Initialization mode is write protected, use the
  289. * RTC_EnableWriteProtection(DISABLE) before calling this function.
  290. */
  291. void RTC_ExitInitMode(void)
  292. {
  293. /* Exit Initialization mode */
  294. RTC->INITSTS &= (uint32_t)~RTC_INITSTS_INITM;
  295. }
  296. /**
  297. * @brief Waits until the RTC Time and Date registers (RTC_TSH and RTC_DATE) are
  298. * synchronized with RTC APB clock.
  299. * @note The RTC Resynchronization mode is write protected, use the
  300. * RTC_EnableWriteProtection(DISABLE) before calling this function.
  301. * @note To read the calendar through the shadow registers after Calendar
  302. * initialization, calendar update or after wakeup from low power modes
  303. * the software must first clear the RSYF flag.
  304. * The software must then wait until it is set again before reading
  305. * the calendar, which means that the calendar registers have been
  306. * correctly copied into the RTC_TSH and RTC_DATE shadow registers.
  307. * @return An ErrorStatus enumeration value:
  308. * - SUCCESS: RTC registers are synchronised
  309. * - ERROR: RTC registers are not synchronised
  310. */
  311. ErrorStatus RTC_WaitForSynchro(void)
  312. {
  313. __IO uint32_t synchrocounter = 0;
  314. ErrorStatus status = ERROR;
  315. uint32_t synchrostatus = 0x00;
  316. /* Disable the write protection for RTC registers */
  317. RTC->WRP = 0xCA;
  318. RTC->WRP = 0x53;
  319. /* Clear RSYF flag */
  320. RTC->INITSTS &= (uint32_t)RTC_RSF_MASK;
  321. /* Wait the registers to be synchronised */
  322. do
  323. {
  324. synchrostatus = RTC->INITSTS & RTC_INITSTS_RSYF;
  325. synchrocounter++;
  326. } while ((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
  327. if ((RTC->INITSTS & RTC_INITSTS_RSYF) != RESET)
  328. {
  329. status = SUCCESS;
  330. }
  331. else
  332. {
  333. status = ERROR;
  334. }
  335. /* Enable the write protection for RTC registers */
  336. RTC->WRP = 0xFF;
  337. return (status);
  338. }
  339. /**
  340. * @brief Enables or Disables the Bypass Shadow feature.
  341. * @note When the Bypass Shadow is enabled the calendar value are taken
  342. * directly from the Calendar counter.
  343. * @param Cmd new state of the Bypass Shadow feature.
  344. * This parameter can be: ENABLE or DISABLE.
  345. */
  346. void RTC_EnableBypassShadow(FunctionalState Cmd)
  347. {
  348. /* Check the parameters */
  349. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  350. /* Disable the write protection for RTC registers */
  351. RTC->WRP = 0xCA;
  352. RTC->WRP = 0x53;
  353. if (Cmd != DISABLE)
  354. {
  355. /* Set the BYPS bit */
  356. RTC->CTRL |= (uint8_t)RTC_CTRL_BYPS;
  357. }
  358. else
  359. {
  360. /* Reset the BYPS bit */
  361. RTC->CTRL &= (uint8_t)~RTC_CTRL_BYPS;
  362. }
  363. /* Enable the write protection for RTC registers */
  364. RTC->WRP = 0xFF;
  365. }
  366. /**
  367. * @}
  368. */
  369. /** @addtogroup RTC_Group2 Time and Date configuration functions
  370. * @brief Time and Date configuration functions
  371. *
  372. @verbatim
  373. ===============================================================================
  374. ##### Time and Date configuration functions #####
  375. ===============================================================================
  376. [..] This section provide functions allowing to program and read the RTC
  377. Calendar (Time and Date).
  378. @endverbatim
  379. * @{
  380. */
  381. /**
  382. * @brief Set the RTC current time.
  383. * @param RTC_Format specifies the format of the entered parameters.
  384. * This parameter can be one of the following values:
  385. * @arg RTC_FORMAT_BIN Binary data format.
  386. * @arg RTC_FORMAT_BCD BCD data format.
  387. * @param RTC_TimeStruct pointer to a RTC_TimeType structure that contains
  388. * the time configuration information for the RTC.
  389. * @return An ErrorStatus enumeration value:
  390. * - SUCCESS: RTC Time register is configured
  391. * - ERROR: RTC Time register is not configured
  392. */
  393. ErrorStatus RTC_ConfigTime(uint32_t RTC_Format, RTC_TimeType* RTC_TimeStruct)
  394. {
  395. uint32_t tmpregister = 0;
  396. ErrorStatus status = ERROR;
  397. /* Check the parameters */
  398. assert_param(IS_RTC_FORMAT(RTC_Format));
  399. if (RTC_Format == RTC_FORMAT_BIN)
  400. {
  401. if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
  402. {
  403. assert_param(IS_RTC_12HOUR(RTC_TimeStruct->Hours));
  404. assert_param(IS_RTC_H12(RTC_TimeStruct->H12));
  405. }
  406. else
  407. {
  408. RTC_TimeStruct->H12 = 0x00;
  409. assert_param(IS_RTC_24HOUR(RTC_TimeStruct->Hours));
  410. }
  411. assert_param(IS_RTC_MINUTES(RTC_TimeStruct->Minutes));
  412. assert_param(IS_RTC_SECONDS(RTC_TimeStruct->Seconds));
  413. }
  414. else
  415. {
  416. if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
  417. {
  418. tmpregister = RTC_Bcd2ToByte(RTC_TimeStruct->Hours);
  419. assert_param(IS_RTC_12HOUR(tmpregister));
  420. assert_param(IS_RTC_H12(RTC_TimeStruct->H12));
  421. }
  422. else
  423. {
  424. RTC_TimeStruct->H12 = 0x00;
  425. assert_param(IS_RTC_24HOUR(RTC_Bcd2ToByte(RTC_TimeStruct->Hours)));
  426. }
  427. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->Minutes)));
  428. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->Seconds)));
  429. }
  430. /* Check the input parameters format */
  431. if (RTC_Format != RTC_FORMAT_BIN)
  432. {
  433. tmpregister = (((uint32_t)(RTC_TimeStruct->Hours) << 16) | ((uint32_t)(RTC_TimeStruct->Minutes) << 8)
  434. | ((uint32_t)RTC_TimeStruct->Seconds) | ((uint32_t)(RTC_TimeStruct->H12) << 16));
  435. }
  436. else
  437. {
  438. tmpregister =
  439. (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Hours) << 16)
  440. | ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Minutes) << 8)
  441. | ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Seconds)) | (((uint32_t)RTC_TimeStruct->H12) << 16));
  442. }
  443. /* Disable the write protection for RTC registers */
  444. RTC->WRP = 0xCA;
  445. RTC->WRP = 0x53;
  446. /* Set Initialization mode */
  447. if (RTC_EnterInitMode() == ERROR)
  448. {
  449. status = ERROR;
  450. }
  451. else
  452. {
  453. /* Set the RTC_TSH register */
  454. RTC->TSH = (uint32_t)(tmpregister & RTC_TR_RESERVED_MASK);
  455. /* Exit Initialization mode */
  456. RTC_ExitInitMode();
  457. /* If RTC_CTRL_BYPS bit = 0, wait for synchro else this check is not needed */
  458. if ((RTC->CTRL & RTC_CTRL_BYPS) == RESET)
  459. {
  460. if (RTC_WaitForSynchro() == ERROR)
  461. {
  462. status = ERROR;
  463. }
  464. else
  465. {
  466. status = SUCCESS;
  467. }
  468. }
  469. else
  470. {
  471. status = SUCCESS;
  472. }
  473. }
  474. /* Enable the write protection for RTC registers */
  475. RTC->WRP = 0xFF;
  476. /* Waits until the RTC Time and Date registers
  477. (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock. */
  478. status=RTC_WaitForSynchro();
  479. return status;
  480. }
  481. /**
  482. * @brief Fills each RTC_TimeStruct member with its default value
  483. * (Time = 00h:00min:00sec).
  484. * @param RTC_TimeStruct pointer to a RTC_TimeType structure which will be
  485. * initialized.
  486. */
  487. void RTC_TimeStructInit(RTC_TimeType* RTC_TimeStruct)
  488. {
  489. /* Time = 00h:00min:00sec */
  490. RTC_TimeStruct->H12 = RTC_AM_H12;
  491. RTC_TimeStruct->Hours = 0;
  492. RTC_TimeStruct->Minutes = 0;
  493. RTC_TimeStruct->Seconds = 0;
  494. }
  495. /**
  496. * @brief Get the RTC current Time.
  497. * @param RTC_Format specifies the format of the returned parameters.
  498. * This parameter can be one of the following values:
  499. * @arg RTC_FORMAT_BIN Binary data format.
  500. * @arg RTC_FORMAT_BCD BCD data format.
  501. * @param RTC_TimeStruct pointer to a RTC_TimeType structure that will
  502. * contain the returned current time configuration.
  503. */
  504. void RTC_GetTime(uint32_t RTC_Format, RTC_TimeType* RTC_TimeStruct)
  505. {
  506. uint32_t tmpregister = 0;
  507. /* Check the parameters */
  508. assert_param(IS_RTC_FORMAT(RTC_Format));
  509. /* Get the RTC_TSH register */
  510. tmpregister = (uint32_t)(RTC->TSH & RTC_TR_RESERVED_MASK);
  511. /* Fill the structure fields with the read parameters */
  512. RTC_TimeStruct->Hours = (uint8_t)((tmpregister & (RTC_TSH_HOT | RTC_TSH_HOU)) >> 16);
  513. RTC_TimeStruct->Minutes = (uint8_t)((tmpregister & (RTC_TSH_MIT | RTC_TSH_MIU)) >> 8);
  514. RTC_TimeStruct->Seconds = (uint8_t)(tmpregister & (RTC_TSH_SCT | RTC_TSH_SCU));
  515. RTC_TimeStruct->H12 = (uint8_t)((tmpregister & (RTC_TSH_APM)) >> 16);
  516. /* Check the input parameters format */
  517. if (RTC_Format == RTC_FORMAT_BIN)
  518. {
  519. /* Convert the structure parameters to Binary format */
  520. RTC_TimeStruct->Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Hours);
  521. RTC_TimeStruct->Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Minutes);
  522. RTC_TimeStruct->Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Seconds);
  523. }
  524. }
  525. /**
  526. * @brief Gets the RTC current Calendar Subseconds value.
  527. * @return RTC current Calendar Subseconds value.
  528. */
  529. uint32_t RTC_GetSubSecond(void)
  530. {
  531. uint32_t tmpregister = 0;
  532. /* Get subseconds values from the correspondent registers*/
  533. tmpregister = (uint32_t)(RTC->SUBS);
  534. return (tmpregister);
  535. }
  536. /**
  537. * @brief Set the RTC current date.
  538. * @param RTC_Format specifies the format of the entered parameters.
  539. * This parameter can be one of the following values:
  540. * @arg RTC_FORMAT_BIN Binary data format.
  541. * @arg RTC_FORMAT_BCD BCD data format.
  542. * @param RTC_DateStruct pointer to a RTC_DateType structure that contains
  543. * the date configuration information for the RTC.
  544. * @return An ErrorStatus enumeration value:
  545. * - SUCCESS: RTC Date register is configured
  546. * - ERROR: RTC Date register is not configured
  547. */
  548. ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateType* RTC_DateStruct)
  549. {
  550. uint32_t tmpregister = 0;
  551. ErrorStatus status = ERROR;
  552. /* Check the parameters */
  553. assert_param(IS_RTC_FORMAT(RTC_Format));
  554. if ((RTC_Format == RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10) == 0x10))
  555. {
  556. RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t) ~(0x10)) + 0x0A;
  557. }
  558. if (RTC_Format == RTC_FORMAT_BIN)
  559. {
  560. assert_param(IS_RTC_YEAR(RTC_DateStruct->Year));
  561. assert_param(IS_RTC_MONTH(RTC_DateStruct->Month));
  562. assert_param(IS_RTC_DATE(RTC_DateStruct->Date));
  563. }
  564. else
  565. {
  566. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->Year)));
  567. tmpregister = RTC_Bcd2ToByte(RTC_DateStruct->Month);
  568. assert_param(IS_RTC_MONTH(tmpregister));
  569. tmpregister = RTC_Bcd2ToByte(RTC_DateStruct->Date);
  570. assert_param(IS_RTC_DATE(tmpregister));
  571. }
  572. assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
  573. /* Check the input parameters format */
  574. if (RTC_Format != RTC_FORMAT_BIN)
  575. {
  576. tmpregister = ((((uint32_t)RTC_DateStruct->Year) << 16) | (((uint32_t)RTC_DateStruct->Month) << 8)
  577. | ((uint32_t)RTC_DateStruct->Date) | (((uint32_t)RTC_DateStruct->WeekDay) << 13));
  578. }
  579. else
  580. {
  581. tmpregister = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Year) << 16)
  582. | ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Month) << 8)
  583. | ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Date)) | ((uint32_t)RTC_DateStruct->WeekDay << 13));
  584. }
  585. /* Disable the write protection for RTC registers */
  586. RTC->WRP = 0xCA;
  587. RTC->WRP = 0x53;
  588. /* Set Initialization mode */
  589. if (RTC_EnterInitMode() == ERROR)
  590. {
  591. status = ERROR;
  592. }
  593. else
  594. {
  595. /* Set the RTC_DATE register */
  596. RTC->DATE = (uint32_t)(tmpregister & RTC_DATE_RESERVED_MASK);
  597. /* Exit Initialization mode */
  598. RTC_ExitInitMode();
  599. /* If RTC_CTRL_BYPS bit = 0, wait for synchro else this check is not needed */
  600. if ((RTC->CTRL & RTC_CTRL_BYPS) == RESET)
  601. {
  602. if (RTC_WaitForSynchro() == ERROR)
  603. {
  604. status = ERROR;
  605. }
  606. else
  607. {
  608. status = SUCCESS;
  609. }
  610. }
  611. else
  612. {
  613. status = SUCCESS;
  614. }
  615. }
  616. /* Enable the write protection for RTC registers */
  617. RTC->WRP = 0xFF;
  618. /* Waits until the RTC Time and Date registers
  619. (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock. */
  620. status=RTC_WaitForSynchro();
  621. return status;
  622. }
  623. /**
  624. * @brief Fills each RTC_DateStruct member with its default value
  625. * (Monday, January 01 xx00).
  626. * @param RTC_DateStruct pointer to a RTC_DateType structure which will be
  627. * initialized.
  628. */
  629. void RTC_DateStructInit(RTC_DateType* RTC_DateStruct)
  630. {
  631. /* Monday, January 01 xx00 */
  632. RTC_DateStruct->WeekDay = RTC_WEEKDAY_MONDAY;
  633. RTC_DateStruct->Date = 1;
  634. RTC_DateStruct->Month = RTC_MONTH_JANUARY;
  635. RTC_DateStruct->Year = 0;
  636. }
  637. /**
  638. * @brief Get the RTC current date.
  639. * @param RTC_Format specifies the format of the returned parameters.
  640. * This parameter can be one of the following values:
  641. * @arg RTC_FORMAT_BIN Binary data format.
  642. * @arg RTC_FORMAT_BCD BCD data format.
  643. * @param RTC_DateStruct pointer to a RTC_DateType structure that will
  644. * contain the returned current date configuration.
  645. */
  646. void RTC_GetDate(uint32_t RTC_Format, RTC_DateType* RTC_DateStruct)
  647. {
  648. uint32_t tmpregister = 0;
  649. /* Check the parameters */
  650. assert_param(IS_RTC_FORMAT(RTC_Format));
  651. /* Get the RTC_TSH register */
  652. tmpregister = (uint32_t)(RTC->DATE & RTC_DATE_RESERVED_MASK);
  653. /* Fill the structure fields with the read parameters */
  654. RTC_DateStruct->Year = (uint8_t)((tmpregister & (RTC_DATE_YRT | RTC_DATE_YRU)) >> 16);
  655. RTC_DateStruct->Month = (uint8_t)((tmpregister & (RTC_DATE_MOT | RTC_DATE_MOU)) >> 8);
  656. RTC_DateStruct->Date = (uint8_t)(tmpregister & (RTC_DATE_DAT | RTC_DATE_DAU));
  657. RTC_DateStruct->WeekDay = (uint8_t)((tmpregister & (RTC_DATE_WDU)) >> 13);
  658. /* Check the input parameters format */
  659. if (RTC_Format == RTC_FORMAT_BIN)
  660. {
  661. /* Convert the structure parameters to Binary format */
  662. RTC_DateStruct->Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Year);
  663. RTC_DateStruct->Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Month);
  664. RTC_DateStruct->Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Date);
  665. }
  666. }
  667. /**
  668. * @}
  669. */
  670. /** @addtogroup RTC_Group3 Alarms configuration functions
  671. * @brief Alarms (Alarm A and Alarm B) configuration functions
  672. *
  673. @verbatim
  674. ===============================================================================
  675. ##### Alarms (Alarm A and Alarm B) configuration functions #####
  676. ===============================================================================
  677. [..] This section provide functions allowing to program and read the RTC
  678. Alarms.
  679. @endverbatim
  680. * @{
  681. */
  682. /**
  683. * @brief Set the specified RTC Alarm.
  684. * @note The Alarm register can only be written when the corresponding Alarm
  685. * is disabled (Use the RTC_EnableAlarm(DISABLE)).
  686. * @param RTC_Format specifies the format of the returned parameters.
  687. * This parameter can be one of the following values:
  688. * @arg RTC_FORMAT_BIN Binary data format.
  689. * @arg RTC_FORMAT_BCD BCD data format.
  690. * @param RTC_Alarm specifies the alarm to be configured.
  691. * This parameter can be one of the following values:
  692. * @arg RTC_A_ALARM to select Alarm A.
  693. * @arg RTC_B_ALARM to select Alarm B.
  694. * @param RTC_AlarmStruct pointer to a RTC_AlarmType structure that
  695. * contains the alarm configuration parameters.
  696. */
  697. void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType* RTC_AlarmStruct)
  698. {
  699. uint32_t tmpregister = 0;
  700. /* Check the parameters */
  701. assert_param(IS_RTC_FORMAT(RTC_Format));
  702. assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
  703. assert_param(IS_ALARM_MASK(RTC_AlarmStruct->AlarmMask));
  704. assert_param(IS_RTC_ALARM_WEEKDAY_SEL(RTC_AlarmStruct->DateWeekMode));
  705. if (RTC_Format == RTC_FORMAT_BIN)
  706. {
  707. if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
  708. {
  709. assert_param(IS_RTC_12HOUR(RTC_AlarmStruct->AlarmTime.Hours));
  710. assert_param(IS_RTC_H12(RTC_AlarmStruct->AlarmTime.H12));
  711. }
  712. else
  713. {
  714. RTC_AlarmStruct->AlarmTime.H12 = 0x00;
  715. assert_param(IS_RTC_24HOUR(RTC_AlarmStruct->AlarmTime.Hours));
  716. }
  717. assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
  718. assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
  719. if (RTC_AlarmStruct->DateWeekMode == RTC_ALARM_SEL_WEEKDAY_DATE)
  720. {
  721. assert_param(IS_RTC_ALARM_WEEKDAY_DATE(RTC_AlarmStruct->DateWeekValue));
  722. }
  723. else
  724. {
  725. assert_param(IS_RTC_ALARM_WEEKDAY_WEEKDAY(RTC_AlarmStruct->DateWeekValue));
  726. }
  727. }
  728. else
  729. {
  730. if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
  731. {
  732. tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours);
  733. assert_param(IS_RTC_12HOUR(tmpregister));
  734. assert_param(IS_RTC_H12(RTC_AlarmStruct->AlarmTime.H12));
  735. }
  736. else
  737. {
  738. RTC_AlarmStruct->AlarmTime.H12 = 0x00;
  739. assert_param(IS_RTC_24HOUR(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours)));
  740. }
  741. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Minutes)));
  742. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Seconds)));
  743. if (RTC_AlarmStruct->DateWeekMode == RTC_ALARM_SEL_WEEKDAY_DATE)
  744. {
  745. tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
  746. assert_param(IS_RTC_ALARM_WEEKDAY_DATE(tmpregister));
  747. }
  748. else
  749. {
  750. tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
  751. assert_param(IS_RTC_ALARM_WEEKDAY_WEEKDAY(tmpregister));
  752. }
  753. }
  754. /* Check the input parameters format */
  755. if (RTC_Format != RTC_FORMAT_BIN)
  756. {
  757. tmpregister =
  758. (((uint32_t)(RTC_AlarmStruct->AlarmTime.Hours) << 16)
  759. | ((uint32_t)(RTC_AlarmStruct->AlarmTime.Minutes) << 8) | ((uint32_t)RTC_AlarmStruct->AlarmTime.Seconds)
  760. | ((uint32_t)(RTC_AlarmStruct->AlarmTime.H12) << 16) | ((uint32_t)(RTC_AlarmStruct->DateWeekValue) << 24)
  761. | ((uint32_t)RTC_AlarmStruct->DateWeekMode) | ((uint32_t)RTC_AlarmStruct->AlarmMask));
  762. }
  763. else
  764. {
  765. tmpregister = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Hours) << 16)
  766. | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Minutes) << 8)
  767. | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Seconds))
  768. | ((uint32_t)(RTC_AlarmStruct->AlarmTime.H12) << 16)
  769. | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->DateWeekValue) << 24)
  770. | ((uint32_t)RTC_AlarmStruct->DateWeekMode) | ((uint32_t)RTC_AlarmStruct->AlarmMask));
  771. }
  772. /* Disable the write protection for RTC registers */
  773. RTC->WRP = 0xCA;
  774. RTC->WRP = 0x53;
  775. /* Configure the Alarm register */
  776. if (RTC_Alarm == RTC_A_ALARM)
  777. {
  778. RTC->ALARMA = (uint32_t)tmpregister;
  779. }
  780. else
  781. {
  782. RTC->ALARMB = (uint32_t)tmpregister;
  783. }
  784. /* Enable the write protection for RTC registers */
  785. RTC->WRP = 0xFF;
  786. }
  787. /**
  788. * @brief Fills each RTC_AlarmStruct member with its default value
  789. * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
  790. * all fields are masked).
  791. * @param RTC_AlarmStruct pointer to a @ref RTC_AlarmType structure which
  792. * will be initialized.
  793. */
  794. void RTC_AlarmStructInit(RTC_AlarmType* RTC_AlarmStruct)
  795. {
  796. /* Alarm Time Settings : Time = 00h:00mn:00sec */
  797. RTC_AlarmStruct->AlarmTime.H12 = RTC_AM_H12;
  798. RTC_AlarmStruct->AlarmTime.Hours = 0;
  799. RTC_AlarmStruct->AlarmTime.Minutes = 0;
  800. RTC_AlarmStruct->AlarmTime.Seconds = 0;
  801. /* Alarm Date Settings : Date = 1st day of the month */
  802. RTC_AlarmStruct->DateWeekMode = RTC_ALARM_SEL_WEEKDAY_DATE;
  803. RTC_AlarmStruct->DateWeekValue = 1;
  804. /* Alarm Masks Settings : Mask = all fields are not masked */
  805. RTC_AlarmStruct->AlarmMask = RTC_ALARMMASK_NONE;
  806. }
  807. /**
  808. * @brief Get the RTC Alarm value and masks.
  809. * @param RTC_Format specifies the format of the output parameters.
  810. * This parameter can be one of the following values:
  811. * @arg RTC_FORMAT_BIN Binary data format.
  812. * @arg RTC_FORMAT_BCD BCD data format.
  813. * @param RTC_Alarm specifies the alarm to be read.
  814. * This parameter can be one of the following values:
  815. * @arg RTC_A_ALARM to select Alarm A.
  816. * @arg RTC_B_ALARM to select Alarm B.
  817. * @param RTC_AlarmStruct pointer to a RTC_AlarmType structure that will
  818. * contains the output alarm configuration values.
  819. */
  820. void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType* RTC_AlarmStruct)
  821. {
  822. uint32_t tmpregister = 0;
  823. /* Check the parameters */
  824. assert_param(IS_RTC_FORMAT(RTC_Format));
  825. assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
  826. /* Get the RTC_ALARMx register */
  827. if (RTC_Alarm == RTC_A_ALARM)
  828. {
  829. tmpregister = (uint32_t)(RTC->ALARMA);
  830. }
  831. else
  832. {
  833. tmpregister = (uint32_t)(RTC->ALARMB);
  834. }
  835. /* Fill the structure with the read parameters */
  836. RTC_AlarmStruct->AlarmTime.Hours = (uint32_t)((tmpregister & (RTC_ALARMA_HOT | RTC_ALARMA_HOU)) >> 16);
  837. RTC_AlarmStruct->AlarmTime.Minutes = (uint32_t)((tmpregister & (RTC_ALARMA_MIT | RTC_ALARMA_MIU)) >> 8);
  838. RTC_AlarmStruct->AlarmTime.Seconds = (uint32_t)(tmpregister & (RTC_ALARMA_SET | RTC_ALARMA_SEU));
  839. RTC_AlarmStruct->AlarmTime.H12 = (uint32_t)((tmpregister & RTC_ALARMA_APM) >> 16);
  840. RTC_AlarmStruct->DateWeekValue = (uint32_t)((tmpregister & (RTC_ALARMA_DTT | RTC_ALARMA_DTU)) >> 24);
  841. RTC_AlarmStruct->DateWeekMode = (uint32_t)(tmpregister & RTC_ALARMA_WKDSEL);
  842. RTC_AlarmStruct->AlarmMask = (uint32_t)(tmpregister & RTC_ALARMMASK_ALL);
  843. if (RTC_Format == RTC_FORMAT_BIN)
  844. {
  845. RTC_AlarmStruct->AlarmTime.Hours = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours);
  846. RTC_AlarmStruct->AlarmTime.Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Minutes);
  847. RTC_AlarmStruct->AlarmTime.Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Seconds);
  848. RTC_AlarmStruct->DateWeekValue = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
  849. }
  850. }
  851. /**
  852. * @brief Enables or disables the specified RTC Alarm.
  853. * @param RTC_Alarm specifies the alarm to be configured.
  854. * This parameter can be any combination of the following values:
  855. * @arg RTC_A_ALARM to select Alarm A.
  856. * @arg RTC_B_ALARM to select Alarm B.
  857. * @param Cmd new state of the specified alarm.
  858. * This parameter can be: ENABLE or DISABLE.
  859. * @return An ErrorStatus enumeration value:
  860. * - SUCCESS: RTC Alarm is enabled/disabled
  861. * - ERROR: RTC Alarm is not enabled/disabled
  862. */
  863. ErrorStatus RTC_EnableAlarm(uint32_t RTC_Alarm, FunctionalState Cmd)
  864. {
  865. __IO uint32_t alarmcounter = 0x00;
  866. uint32_t alarmstatus = 0x00;
  867. ErrorStatus status = ERROR;
  868. /* Check the parameters */
  869. assert_param(IS_RTC_ALARM_ENABLE(RTC_Alarm));
  870. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  871. /* Disable the write protection for RTC registers */
  872. RTC->WRP = 0xCA;
  873. RTC->WRP = 0x53;
  874. /* Configure the Alarm state */
  875. if (Cmd != DISABLE)
  876. {
  877. RTC->CTRL |= (uint32_t)RTC_Alarm;
  878. status = SUCCESS;
  879. }
  880. else
  881. {
  882. /* Disable the Alarm in RTC_CTRL register */
  883. RTC->CTRL &= (uint32_t)~RTC_Alarm;
  884. /* Wait till RTC ALxWF flag is set and if Time out is reached exit */
  885. do
  886. {
  887. alarmstatus = RTC->INITSTS & (RTC_Alarm >> 8);
  888. alarmcounter++;
  889. } while ((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
  890. if ((RTC->INITSTS & (RTC_Alarm >> 8)) == RESET)
  891. {
  892. status = ERROR;
  893. }
  894. else
  895. {
  896. status = SUCCESS;
  897. }
  898. }
  899. /* Enable the write protection for RTC registers */
  900. RTC->WRP = 0xFF;
  901. return status;
  902. }
  903. /**
  904. * @brief Configure the RTC AlarmA/B Subseconds value and mask.*
  905. * @note This function is performed only when the Alarm is disabled.
  906. * @param RTC_Alarm specifies the alarm to be configured.
  907. * This parameter can be one of the following values:
  908. * @arg RTC_A_ALARM to select Alarm A.
  909. * @arg RTC_B_ALARM to select Alarm B.
  910. * @param RTC_AlarmSubSecondValue specifies the Subseconds value.
  911. * This parameter can be a value from 0 to 0x00007FFF.
  912. * @param RTC_AlarmSubSecondMask specifies the Subseconds Mask.
  913. * This parameter can be any combination of the following values:
  914. * @arg RTC_SUBS_MASK_ALL All Alarm SS fields are masked.
  915. * There is no comparison on sub seconds for Alarm.
  916. * @arg RTC_SUBS_MASK_SS14_1 SS[14:1] are don't care in Alarm comparison.
  917. * Only SS[0] is compared
  918. * @arg RTC_SUBS_MASK_SS14_2 SS[14:2] are don't care in Alarm comparison.
  919. * Only SS[1:0] are compared
  920. * @arg RTC_SUBS_MASK_SS14_3 SS[14:3] are don't care in Alarm comparison.
  921. * Only SS[2:0] are compared
  922. * @arg RTC_SUBS_MASK_SS14_4 SS[14:4] are don't care in Alarm comparison.
  923. * Only SS[3:0] are compared
  924. * @arg RTC_SUBS_MASK_SS14_5 SS[14:5] are don't care in Alarm comparison.
  925. * Only SS[4:0] are compared.
  926. * @arg RTC_SUBS_MASK_SS14_6 SS[14:6] are don't care in Alarm comparison.
  927. * Only SS[5:0] are compared.
  928. * @arg RTC_SUBS_MASK_SS14_7 SS[14:7] are don't care in Alarm comparison.
  929. * Only SS[6:0] are compared.
  930. * @arg RTC_SUBS_MASK_SS14_8 SS[14:8] are don't care in Alarm comparison.
  931. * Only SS[7:0] are compared.
  932. * @arg RTC_SUBS_MASK_SS14_9 SS[14:9] are don't care in Alarm comparison.
  933. * Only SS[8:0] are compared.
  934. * @arg RTC_SUBS_MASK_SS14_10 SS[14:10] are don't care in Alarm comparison.
  935. * Only SS[9:0] are compared.
  936. * @arg RTC_SUBS_MASK_SS14_11 SS[14:11] are don't care in Alarm comparison.
  937. * Only SS[10:0] are compared.
  938. * @arg RTC_SUBS_MASK_SS14_12 SS[14:12] are don't care in Alarm comparison.
  939. * Only SS[11:0] are compared.
  940. * @arg RTC_SUBS_MASK_SS14_13 SS[14:13] are don't care in Alarm comparison.
  941. * Only SS[12:0] are compared.
  942. * @arg RTC_SUBS_MASK_SS14_14 SS[14] is don't care in Alarm comparison.
  943. * Only SS[13:0] are compared.
  944. * @arg RTC_SUBS_MASK_NONE SS[14:0] are compared and must match
  945. * to activate alarm.
  946. */
  947. void RTC_ConfigAlarmSubSecond(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
  948. {
  949. uint32_t tmpregister = 0;
  950. /* Check the parameters */
  951. assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
  952. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
  953. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK_MODE(RTC_AlarmSubSecondMask));
  954. /* Disable the write protection for RTC registers */
  955. RTC->WRP = 0xCA;
  956. RTC->WRP = 0x53;
  957. /* Configure the Alarm A or Alarm B SubSecond registers */
  958. tmpregister = (uint32_t)(uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
  959. if (RTC_Alarm == RTC_A_ALARM)
  960. {
  961. /* Configure the AlarmA SubSecond register */
  962. RTC->ALRMASS = tmpregister;
  963. }
  964. else
  965. {
  966. /* Configure the Alarm B SubSecond register */
  967. RTC->ALRMBSS = tmpregister;
  968. }
  969. /* Enable the write protection for RTC registers */
  970. RTC->WRP = 0xFF;
  971. }
  972. /**
  973. * @brief Gets the RTC Alarm Subseconds value.
  974. * @param RTC_Alarm specifies the alarm to be read.
  975. * This parameter can be one of the following values:
  976. * @arg RTC_A_ALARM to select Alarm A.
  977. * @arg RTC_B_ALARM to select Alarm B.
  978. * @return RTC Alarm Subseconds value.
  979. */
  980. uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
  981. {
  982. uint32_t tmpregister = 0;
  983. /* Get the RTC_ALARMx register */
  984. if (RTC_Alarm == RTC_A_ALARM)
  985. {
  986. tmpregister = (uint32_t)((RTC->ALRMASS) & RTC_ALRMASS_SSV);
  987. }
  988. else
  989. {
  990. tmpregister = (uint32_t)((RTC->ALRMBSS) & RTC_ALRMBSS_SSV);
  991. }
  992. return (tmpregister);
  993. }
  994. /**
  995. * @}
  996. */
  997. /** @addtogroup RTC_Group4 WakeUp Timer configuration functions
  998. * @brief WakeUp Timer configuration functions
  999. *
  1000. @verbatim
  1001. ===============================================================================
  1002. ##### WakeUp Timer configuration functions #####
  1003. ===============================================================================
  1004. [..] This section provide functions allowing to program and read the RTC WakeUp.
  1005. @endverbatim
  1006. * @{
  1007. */
  1008. /**
  1009. * @brief Configures the RTC Wakeup clock source.
  1010. * @note The WakeUp Clock source can only be changed when the RTC WakeUp
  1011. * is disabled (Use the RTC_EnableWakeUp(DISABLE)).
  1012. * @param RTC_WakeUpClock Wakeup Clock source.
  1013. * This parameter can be one of the following values:
  1014. * @arg RTC_WKUPCLK_RTCCLK_DIV16 RTC Wakeup Counter Clock = RTCCLK/16.
  1015. * @arg RTC_WKUPCLK_RTCCLK_DIV8 RTC Wakeup Counter Clock = RTCCLK/8.
  1016. * @arg RTC_WKUPCLK_RTCCLK_DIV4 RTC Wakeup Counter Clock = RTCCLK/4.
  1017. * @arg RTC_WKUPCLK_RTCCLK_DIV2 RTC Wakeup Counter Clock = RTCCLK/2.
  1018. * @arg RTC_WKUPCLK_CK_SPRE_16BITS RTC Wakeup Counter Clock = CK_SPRE.
  1019. */
  1020. void RTC_ConfigWakeUpClock(uint32_t RTC_WakeUpClock)
  1021. {
  1022. /* Check the parameters */
  1023. assert_param(IS_RTC_WKUP_CLOCK(RTC_WakeUpClock));
  1024. /* Disable the write protection for RTC registers */
  1025. RTC->WRP = 0xCA;
  1026. RTC->WRP = 0x53;
  1027. /* Clear the Wakeup Timer clock source bits in CTRL register */
  1028. RTC->CTRL &= (uint32_t)~RTC_CTRL_WKUPSEL;
  1029. /* Configure the clock source */
  1030. RTC->CTRL |= (uint32_t)RTC_WakeUpClock;
  1031. /* Enable the write protection for RTC registers */
  1032. RTC->WRP = 0xFF;
  1033. }
  1034. /**
  1035. * @brief Configures the RTC Wakeup counter.
  1036. * @note The RTC WakeUp counter can only be written when the RTC WakeUp.
  1037. * is disabled (Use the RTC_EnableWakeUp(DISABLE)).
  1038. * @param RTC_WakeUpCounter specifies the WakeUp counter.
  1039. * This parameter can be a value from 0x0000 to 0xFFFF.
  1040. */
  1041. void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
  1042. {
  1043. /* Check the parameters */
  1044. assert_param(IS_RTC_WKUP_COUNTER(RTC_WakeUpCounter));
  1045. /* Disable the write protection for RTC registers */
  1046. RTC->WRP = 0xCA;
  1047. RTC->WRP = 0x53;
  1048. /* Configure the Wakeup Timer counter */
  1049. RTC->WKUPT = (uint32_t)RTC_WakeUpCounter;
  1050. /* Enable the write protection for RTC registers */
  1051. RTC->WRP = 0xFF;
  1052. }
  1053. /**
  1054. * @brief Returns the RTC WakeUp timer counter value.
  1055. * @return The RTC WakeUp Counter value.
  1056. */
  1057. uint32_t RTC_GetWakeUpCounter(void)
  1058. {
  1059. /* Get the counter value */
  1060. return ((uint32_t)(RTC->WKUPT & RTC_WKUPT_WKUPT));
  1061. }
  1062. /**
  1063. * @brief Enables or Disables the RTC WakeUp timer.
  1064. * @param Cmd new state of the WakeUp timer.
  1065. * This parameter can be: ENABLE or DISABLE.
  1066. */
  1067. ErrorStatus RTC_EnableWakeUp(FunctionalState Cmd)
  1068. {
  1069. __IO uint32_t wutcounter = 0x00;
  1070. uint32_t wutwfstatus = 0x00;
  1071. ErrorStatus status = ERROR;
  1072. /* Check the parameters */
  1073. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1074. /* Disable the write protection for RTC registers */
  1075. RTC->WRP = 0xCA;
  1076. RTC->WRP = 0x53;
  1077. if (Cmd != DISABLE)
  1078. {
  1079. /* Enable the Wakeup Timer */
  1080. RTC->CTRL |= (uint32_t)RTC_CTRL_WTEN;
  1081. status = SUCCESS;
  1082. }
  1083. else
  1084. {
  1085. /* Disable the Wakeup Timer */
  1086. RTC->CTRL &= (uint32_t)~RTC_CTRL_WTEN;
  1087. /* Wait till RTC WTWF flag is set and if Time out is reached exit */
  1088. do
  1089. {
  1090. wutwfstatus = RTC->INITSTS & RTC_INITSTS_WTWF;
  1091. wutcounter++;
  1092. } while ((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
  1093. if ((RTC->INITSTS & RTC_INITSTS_WTWF) == RESET)
  1094. {
  1095. status = ERROR;
  1096. }
  1097. else
  1098. {
  1099. status = SUCCESS;
  1100. }
  1101. }
  1102. /* Enable the write protection for RTC registers */
  1103. RTC->WRP = 0xFF;
  1104. return status;
  1105. }
  1106. /**
  1107. * @}
  1108. */
  1109. /** @addtogroup RTC_Group5 Daylight Saving configuration functions
  1110. * @brief Daylight Saving configuration functions
  1111. *
  1112. @verbatim
  1113. ===============================================================================
  1114. ##### Daylight Saving configuration functions #####
  1115. ===============================================================================
  1116. [..] This section provide functions allowing to configure the RTC DayLight Saving.
  1117. @endverbatim
  1118. * @{
  1119. */
  1120. /**
  1121. * @brief Adds or substract one hour from the current time.
  1122. * @param RTC_DayLightSaving the value of hour adjustment.
  1123. * This parameter can be one of the following values:
  1124. * @arg RTC_DAYLIGHT_SAVING_SUB1H Substract one hour (winter time).
  1125. * @arg RTC_DAYLIGHT_SAVING_ADD1H Add one hour (summer time).
  1126. * @param RTC_StoreOperation Specifies the value to be written in the BCK bit
  1127. * in CTRL register to store the operation.
  1128. * This parameter can be one of the following values:
  1129. * @arg RTC_STORE_OPERATION_RESET BCK Bit Reset.
  1130. * @arg RTC_STORE_OPERATION_SET BCK Bit Set.
  1131. */
  1132. void RTC_ConfigDayLightSaving(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
  1133. {
  1134. /* Check the parameters */
  1135. assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  1136. assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
  1137. /* Disable the write protection for RTC registers */
  1138. RTC->WRP = 0xCA;
  1139. RTC->WRP = 0x53;
  1140. /* Clear the bits to be configured */
  1141. RTC->CTRL &= (uint32_t) ~(RTC_CTRL_BAKP);
  1142. /* Clear the SU1H and AD1H bits to be configured */
  1143. RTC->CTRL &= (uint32_t) ~(RTC_CTRL_SU1H & RTC_CTRL_AD1H);
  1144. /* Configure the RTC_CTRL register */
  1145. RTC->CTRL |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
  1146. /* Enable the write protection for RTC registers */
  1147. RTC->WRP = 0xFF;
  1148. }
  1149. /**
  1150. * @brief Returns the RTC Day Light Saving stored operation.
  1151. * @return RTC Day Light Saving stored operation.
  1152. * - RTC_STORE_OPERATION_RESET
  1153. * - RTC_STORE_OPERATION_SET
  1154. */
  1155. uint32_t RTC_GetStoreOperation(void)
  1156. {
  1157. return (RTC->CTRL & RTC_CTRL_BAKP);
  1158. }
  1159. /**
  1160. * @}
  1161. */
  1162. /** @addtogroup RTC_Group6 Output pin Configuration function
  1163. * @brief Output pin Configuration function
  1164. *
  1165. @verbatim
  1166. ===============================================================================
  1167. ##### Output pin Configuration function #####
  1168. ===============================================================================
  1169. [..] This section provide functions allowing to configure the RTC Output source.
  1170. @endverbatim
  1171. * @{
  1172. */
  1173. /**
  1174. * @brief Configures the RTC output source (AFO_ALARM).
  1175. * @param RTC_Output Specifies which signal will be routed to the RTC output.
  1176. * This parameter can be one of the following values:
  1177. * @arg RTC_OUTPUT_DIS No output selected
  1178. * @arg RTC_OUTPUT_ALA signal of AlarmA mapped to output.
  1179. * @arg RTC_OUTPUT_ALB signal of AlarmB mapped to output.
  1180. * @arg RTC_OUTPUT_WKUP signal of WakeUp mapped to output.
  1181. * @param RTC_OutputPolarity Specifies the polarity of the output signal.
  1182. * This parameter can be one of the following:
  1183. * @arg RTC_OUTPOL_HIGH The output pin is high when the
  1184. * ALRAF/ALRBF/WUTF is high (depending on OSEL).
  1185. * @arg RTC_OUTPOL_LOW The output pin is low when the
  1186. * ALRAF/ALRBF/WUTF is high (depending on OSEL).
  1187. */
  1188. void RTC_ConfigOutput(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
  1189. {
  1190. /* Check the parameters */
  1191. assert_param(IS_RTC_OUTPUT_MODE(RTC_Output));
  1192. assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
  1193. /* Disable the write protection for RTC registers */
  1194. RTC->WRP = 0xCA;
  1195. RTC->WRP = 0x53;
  1196. /* Clear the bits to be configured */
  1197. RTC->CTRL &= (uint32_t) ~(RTC_CTRL_OUTSEL | RTC_CTRL_OPOL);
  1198. /* Configure the output selection and polarity */
  1199. RTC->CTRL |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
  1200. /* Enable the write protection for RTC registers */
  1201. RTC->WRP = 0xFF;
  1202. }
  1203. /**
  1204. * @}
  1205. */
  1206. /** @addtogroup RTC_Group7 Coarse and Smooth Calibrations configuration functions
  1207. * @brief Coarse and Smooth Calibrations configuration functions
  1208. *
  1209. @verbatim
  1210. ===============================================================================
  1211. ##### Coarse and Smooth Calibrations configuration functions #####
  1212. ===============================================================================
  1213. @endverbatim
  1214. * @{
  1215. */
  1216. /**
  1217. * @brief Enables or disables the RTC clock to be output through the relative
  1218. * pin.
  1219. * @param Cmd new state of the coarse calibration Output.
  1220. * This parameter can be: ENABLE or DISABLE.
  1221. */
  1222. void RTC_EnableCalibOutput(FunctionalState Cmd)
  1223. {
  1224. /* Check the parameters */
  1225. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1226. /* Disable the write protection for RTC registers */
  1227. RTC->WRP = 0xCA;
  1228. RTC->WRP = 0x53;
  1229. if (Cmd != DISABLE)
  1230. {
  1231. /* Enable the RTC clock output */
  1232. RTC->CTRL |= (uint32_t)RTC_CTRL_COEN;
  1233. }
  1234. else
  1235. {
  1236. /* Disable the RTC clock output */
  1237. RTC->CTRL &= (uint32_t)~RTC_CTRL_COEN;
  1238. }
  1239. /* Enable the write protection for RTC registers */
  1240. RTC->WRP = 0xFF;
  1241. }
  1242. /**
  1243. * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1244. * @param RTC_CalibOutput Select the Calibration output Selection .
  1245. * This parameter can be one of the following values:
  1246. * @arg RTC_CALIB_OUTPUT_256HZ A signal has a regular waveform at 256Hz.
  1247. * @arg RTC_CALIB_OUTPUT_1HZ A signal has a regular waveform at 1Hz.
  1248. */
  1249. void RTC_ConfigCalibOutput(uint32_t RTC_CalibOutput)
  1250. {
  1251. /* Check the parameters */
  1252. assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
  1253. /* Disable the write protection for RTC registers */
  1254. RTC->WRP = 0xCA;
  1255. RTC->WRP = 0x53;
  1256. /*clear flags before config*/
  1257. RTC->CTRL &= (uint32_t) ~(RTC_CTRL_CALOSEL);
  1258. /* Configure the RTC_CTRL register */
  1259. RTC->CTRL |= (uint32_t)RTC_CalibOutput;
  1260. /* Enable the write protection for RTC registers */
  1261. RTC->WRP = 0xFF;
  1262. }
  1263. /**
  1264. * @brief Configures the Smooth Calibration Settings.
  1265. * @param RTC_SmoothCalibPeriod Select the Smooth Calibration Period.
  1266. * This parameter can be can be one of the following values:
  1267. * @arg SMOOTH_CALIB_32SEC The smooth calibration periode is 32s.
  1268. * @arg SMOOTH_CALIB_16SEC The smooth calibration periode is 16s.
  1269. * @arg SMOOTH_CALIB_8SEC The smooth calibartion periode is 8s.
  1270. * @param RTC_SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  1271. * This parameter can be one of the following values:
  1272. * @arg RTC_SMOOTH_CALIB_PLUS_PULSES_SET Add one RTCCLK puls every 2**11 pulses.
  1273. * @arg RTC_SMOOTH_CALIB_PLUS_PULSES__RESET No RTCCLK pulses are added.
  1274. * @param RTC_SmouthCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  1275. * This parameter can be one any value from 0 to 0x000001FF.
  1276. * @return An ErrorStatus enumeration value:
  1277. * - SUCCESS: RTC Calib registers are configured
  1278. * - ERROR: RTC Calib registers are not configured
  1279. */
  1280. ErrorStatus RTC_ConfigSmoothCalib(uint32_t RTC_SmoothCalibPeriod,
  1281. uint32_t RTC_SmoothCalibPlusPulses,
  1282. uint32_t RTC_SmouthCalibMinusPulsesValue)
  1283. {
  1284. ErrorStatus status = ERROR;
  1285. uint32_t recalpfcount = 0;
  1286. /* Check the parameters */
  1287. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD_SEL(RTC_SmoothCalibPeriod));
  1288. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
  1289. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
  1290. /* Disable the write protection for RTC registers */
  1291. RTC->WRP = 0xCA;
  1292. RTC->WRP = 0x53;
  1293. /* check if a calibration is pending*/
  1294. if ((RTC->INITSTS & RTC_INITSTS_RECPF) != RESET)
  1295. {
  1296. /* wait until the Calibration is completed*/
  1297. while (((RTC->INITSTS & RTC_INITSTS_RECPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
  1298. {
  1299. recalpfcount++;
  1300. }
  1301. }
  1302. /* check if the calibration pending is completed or if there is no calibration operation at all*/
  1303. if ((RTC->INITSTS & RTC_INITSTS_RECPF) == RESET)
  1304. {
  1305. /* Configure the Smooth calibration settings */
  1306. RTC->CALIB = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses
  1307. | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
  1308. status = SUCCESS;
  1309. }
  1310. else
  1311. {
  1312. status = ERROR;
  1313. }
  1314. /* Enable the write protection for RTC registers */
  1315. RTC->WRP = 0xFF;
  1316. return (ErrorStatus)(status);
  1317. }
  1318. /**
  1319. * @}
  1320. */
  1321. /** @addtogroup RTC_Group8 TimeStamp configuration functions
  1322. * @brief TimeStamp configuration functions
  1323. *
  1324. @verbatim
  1325. ===============================================================================
  1326. ##### TimeStamp configuration functions #####
  1327. ===============================================================================
  1328. @endverbatim
  1329. * @{
  1330. */
  1331. /**
  1332. * @brief Enables or Disables the RTC TimeStamp functionality with the
  1333. * specified time stamp pin stimulating edge.
  1334. * @param RTC_TimeStampEdge Specifies the pin edge on which the TimeStamp is
  1335. * activated.
  1336. * This parameter can be one of the following:
  1337. * @arg RTC_TIMESTAMP_EDGE_RISING the Time stamp event occurs on the rising
  1338. * edge of the related pin.
  1339. * @arg RTC_TIMESTAMP_EDGE_FALLING the Time stamp event occurs on the
  1340. * falling edge of the related pin.
  1341. * @param Cmd new state of the TimeStamp.
  1342. * This parameter can be: ENABLE or DISABLE.
  1343. */
  1344. void RTC_EnableTimeStamp(uint32_t RTC_TimeStampEdge, FunctionalState Cmd)
  1345. {
  1346. uint32_t tmpregister = 0;
  1347. /* Check the parameters */
  1348. assert_param(IS_RTC_TIMESTAMP_EDGE_MODE(RTC_TimeStampEdge));
  1349. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1350. /* Get the RTC_CTRL register and clear the bits to be configured */
  1351. tmpregister = (uint32_t)(RTC->CTRL & (uint32_t) ~(RTC_CTRL_TSPOL | RTC_CTRL_TSEN));
  1352. /* Get the new configuration */
  1353. if (Cmd != DISABLE)
  1354. {
  1355. tmpregister |= (uint32_t)(RTC_TimeStampEdge | RTC_CTRL_TSEN);
  1356. }
  1357. else
  1358. {
  1359. tmpregister |= (uint32_t)(RTC_TimeStampEdge);
  1360. }
  1361. /* Disable the write protection for RTC registers */
  1362. RTC->WRP = 0xCA;
  1363. RTC->WRP = 0x53;
  1364. /* Configure the Time Stamp TSEDGE and Enable bits */
  1365. RTC->CTRL = (uint32_t)tmpregister;
  1366. /* Enable the write protection for RTC registers */
  1367. RTC->WRP = 0xFF;
  1368. }
  1369. /**
  1370. * @brief Get the RTC TimeStamp value and masks.
  1371. * @param RTC_Format specifies the format of the output parameters.
  1372. * This parameter can be one of the following values:
  1373. * @arg RTC_FORMAT_BIN Binary data format
  1374. * @arg RTC_FORMAT_BCD BCD data format
  1375. * @param RTC_StampTimeStruct pointer to a RTC_TimeType structure that will
  1376. * contains the TimeStamp time values.
  1377. * @param RTC_StampDateStruct pointer to a RTC_DateType structure that will
  1378. * contains the TimeStamp date values.
  1379. */
  1380. void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeType* RTC_StampTimeStruct, RTC_DateType* RTC_StampDateStruct)
  1381. {
  1382. uint32_t tmptime = 0, tmpdate = 0;
  1383. /* Check the parameters */
  1384. assert_param(IS_RTC_FORMAT(RTC_Format));
  1385. /* Get the TimeStamp time and date registers values */
  1386. tmptime = (uint32_t)(RTC->TST & RTC_TR_RESERVED_MASK);
  1387. tmpdate = (uint32_t)(RTC->TSD & RTC_DATE_RESERVED_MASK);
  1388. /* Fill the Time structure fields with the read parameters */
  1389. RTC_StampTimeStruct->Hours = (uint8_t)((tmptime & (RTC_TSH_HOT | RTC_TSH_HOU)) >> 16);
  1390. RTC_StampTimeStruct->Minutes = (uint8_t)((tmptime & (RTC_TSH_MIT | RTC_TSH_MIU)) >> 8);
  1391. RTC_StampTimeStruct->Seconds = (uint8_t)(tmptime & (RTC_TSH_SCT | RTC_TSH_SCU));
  1392. RTC_StampTimeStruct->H12 = (uint8_t)((tmptime & (RTC_TSH_APM)) >> 16);
  1393. /* Fill the Date structure fields with the read parameters */
  1394. RTC_StampDateStruct->Year = (uint8_t)((tmpdate & (RTC_DATE_YRT | RTC_DATE_YRU)) >> 16);
  1395. RTC_StampDateStruct->Month = (uint8_t)((tmpdate & (RTC_DATE_MOT | RTC_DATE_MOU)) >> 8);
  1396. RTC_StampDateStruct->Date = (uint8_t)(tmpdate & (RTC_DATE_DAT | RTC_DATE_DAU));
  1397. RTC_StampDateStruct->WeekDay = (uint8_t)((tmpdate & (RTC_DATE_WDU)) >> 13);
  1398. /* Check the input parameters format */
  1399. if (RTC_Format == RTC_FORMAT_BIN)
  1400. {
  1401. /* Convert the Time structure parameters to Binary format */
  1402. RTC_StampTimeStruct->Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Hours);
  1403. RTC_StampTimeStruct->Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Minutes);
  1404. RTC_StampTimeStruct->Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Seconds);
  1405. /* Convert the Date structure parameters to Binary format */
  1406. RTC_StampDateStruct->Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->Month);
  1407. RTC_StampDateStruct->Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->Date);
  1408. RTC_StampDateStruct->WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->WeekDay);
  1409. }
  1410. }
  1411. /**
  1412. * @brief Get the RTC timestamp Subseconds value.
  1413. * @return RTC current timestamp Subseconds value.
  1414. */
  1415. uint32_t RTC_GetTimeStampSubSecond(void)
  1416. {
  1417. /* Get timestamp subseconds values from the correspondent registers */
  1418. return (uint32_t)(RTC->TSSS);
  1419. }
  1420. /**
  1421. * @}
  1422. */
  1423. /** @addtogroup RTC_Group11 Output Type Config configuration functions
  1424. * @brief Output Type Config configuration functions
  1425. *
  1426. @verbatim
  1427. ===============================================================================
  1428. ##### Output Type Config configuration functions #####
  1429. ===============================================================================
  1430. @endverbatim
  1431. * @{
  1432. */
  1433. /**
  1434. * @brief Configures the RTC Output Pin mode.
  1435. * @param RTC_OutputType specifies the RTC Output (PC13) pin mode.
  1436. * This parameter can be one of the following values:
  1437. * @arg RTC_OUTPUT_OPENDRAIN RTC Output (PC13) is configured in
  1438. * Open Drain mode.
  1439. * @arg RTC_OUTPUT_PUSHPULL RTC Output (PC13) is configured in
  1440. * Push Pull mode.
  1441. */
  1442. void RTC_ConfigOutputType(uint32_t RTC_OutputType)
  1443. {
  1444. /* Check the parameters */
  1445. assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
  1446. RTC->OPT &= (uint32_t) ~(RTC_OPT_TYPE);
  1447. RTC->OPT |= (uint32_t)(RTC_OutputType);
  1448. }
  1449. /**
  1450. * @}
  1451. */
  1452. /** @addtogroup RTC_Group12 Shift control synchronisation functions
  1453. * @brief Shift control synchronisation functions
  1454. *
  1455. @verbatim
  1456. ===============================================================================
  1457. ##### Shift control synchronisation functions #####
  1458. ===============================================================================
  1459. @endverbatim
  1460. * @{
  1461. */
  1462. /**
  1463. * @brief Configures the Synchronization Shift Control Settings.
  1464. * @param RTC_ShiftAdd1S Select to add or not 1 second to the time Calendar.
  1465. * This parameter can be one of the following values :
  1466. * @arg RTC_SHIFT_SUB1S_DISABLE Add one second to the clock calendar.
  1467. * @arg RTC_SHIFT_SUB1S_ENABLE No effect.
  1468. * @param RTC_ShiftAddFS Select the number of Second Fractions to Substitute.
  1469. * This parameter can be one any value from 0 to 0x7FFF.
  1470. * @return An ErrorStatus enumeration value:
  1471. * - SUCCESS: RTC Shift registers are configured
  1472. * - ERROR: RTC Shift registers are not configured
  1473. */
  1474. ErrorStatus RTC_ConfigSynchroShift(uint32_t RTC_ShiftAddFS, uint32_t RTC_ShiftSub1s)
  1475. {
  1476. ErrorStatus status = ERROR;
  1477. uint32_t shpfcount = 0;
  1478. /* Check the parameters */
  1479. assert_param(IS_RTC_SHIFT_ADFS(RTC_ShiftAddFS));
  1480. assert_param(IS_RTC_SHIFT_SUB1S(RTC_ShiftSub1s));
  1481. /* Disable the write protection for RTC registers */
  1482. RTC->WRP = 0xCA;
  1483. RTC->WRP = 0x53;
  1484. /* Check if a Shift is pending*/
  1485. if ((RTC->INITSTS & RTC_INITSTS_SHOPF) != RESET)
  1486. {
  1487. /* Wait until the shift is completed*/
  1488. while (((RTC->INITSTS & RTC_INITSTS_SHOPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
  1489. {
  1490. shpfcount++;
  1491. }
  1492. }
  1493. /* Check if the Shift pending is completed or if there is no Shift operation at all*/
  1494. if ((RTC->INITSTS & RTC_INITSTS_SHOPF) == RESET)
  1495. {
  1496. {
  1497. /* Configure the Shift settings */
  1498. RTC->SCTRL = (uint32_t)(uint32_t)(RTC_ShiftAddFS) | (uint32_t)(RTC_ShiftSub1s);
  1499. if (RTC_WaitForSynchro() == ERROR)
  1500. {
  1501. status = ERROR;
  1502. }
  1503. else
  1504. {
  1505. status = SUCCESS;
  1506. }
  1507. }
  1508. }
  1509. else
  1510. {
  1511. status = ERROR;
  1512. }
  1513. /* Enable the write protection for RTC registers */
  1514. RTC->WRP = 0xFF;
  1515. return (ErrorStatus)(status);
  1516. }
  1517. /**
  1518. * @}
  1519. */
  1520. /** @addtogroup RTC_Group13 Interrupts and flags management functions
  1521. * @brief Interrupts and flags management functions
  1522. *
  1523. @verbatim
  1524. ===============================================================================
  1525. ##### Interrupts and flags management functions #####
  1526. ===============================================================================
  1527. [..] All RTC interrupts are connected to the EXTI controller.
  1528. (+) To enable the RTC Alarm interrupt, the following sequence is required:
  1529. (+) Configure and enable the EXTI Line 17 in interrupt mode and select
  1530. the rising edge sensitivity using the EXTI_InitPeripheral() function.
  1531. (+) Configure and enable the RTC_Alarm IRQ channel in the NVIC using
  1532. the NVIC_Init() function.
  1533. (+) Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B)
  1534. using the RTC_SetAlarm() and RTC_EnableAlarm() functions.
  1535. (+) To enable the RTC Wakeup interrupt, the following sequence is required:
  1536. (+) Configure and enable the EXTI Line 20 in interrupt mode and select
  1537. the rising edge sensitivity using the EXTI_InitPeripheral() function.
  1538. (+) Configure and enable the RTC_WKUP IRQ channel in the NVIC using the
  1539. NVIC_Init() function.
  1540. (+) Configure the RTC to generate the RTC wakeup timer event using the
  1541. RTC_ConfigWakeUpClock(), RTC_SetWakeUpCounter() and RTC_EnableWakeUp()
  1542. functions.
  1543. (+) To enable the RTC Tamper interrupt, the following sequence is required:
  1544. (+) Configure and enable the EXTI Line 19 in interrupt mode and select
  1545. the rising edge sensitivity using the EXTI_InitPeripheral() function.
  1546. (+) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
  1547. the NVIC_Init() function.
  1548. (+) Configure the RTC to detect the RTC tamper event using the
  1549. RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
  1550. (+) To enable the RTC TimeStamp interrupt, the following sequence is
  1551. required:
  1552. (+) Configure and enable the EXTI Line 19 in interrupt mode and select
  1553. the rising edge sensitivity using the EXTI_InitPeripheral() function.
  1554. (+) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
  1555. the NVIC_Init() function.
  1556. (+) Configure the RTC to detect the RTC time-stamp event using the
  1557. RTC_EnableTimeStamp() functions.
  1558. @endverbatim
  1559. * @{
  1560. */
  1561. /**
  1562. * @brief Enables or disables the specified RTC interrupts.
  1563. * @param RTC_INT specifies the RTC interrupt sources to be enabled or disabled.
  1564. * This parameter can be any combination of the following values:
  1565. * @arg RTC_INT_WUT WakeUp Timer interrupt mask.
  1566. * @arg RTC_INT_ALRB Alarm B interrupt mask.
  1567. * @arg RTC_INT_ALRA Alarm A interrupt mask.
  1568. * @param Cmd new state of the specified RTC interrupts.
  1569. * This parameter can be: ENABLE or DISABLE.
  1570. */
  1571. void RTC_ConfigInt(uint32_t RTC_INT, FunctionalState Cmd)
  1572. {
  1573. /* Check the parameters */
  1574. assert_param(IS_RTC_CONFIG_INT(RTC_INT));
  1575. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1576. /* Disable the write protection for RTC registers */
  1577. RTC->WRP = 0xCA;
  1578. RTC->WRP = 0x53;
  1579. if (Cmd != DISABLE)
  1580. {
  1581. /* Configure the Interrupts in the RTC_CTRL register */
  1582. RTC->CTRL |= RTC_INT ;
  1583. }
  1584. else
  1585. {
  1586. /* Configure the Interrupts in the RTC_CTRL register */
  1587. RTC->CTRL &= (uint32_t) ~(RTC_INT);
  1588. }
  1589. /* Enable the write protection for RTC registers */
  1590. RTC->WRP = 0xFF;
  1591. }
  1592. /**
  1593. * @brief Checks whether the specified RTC flag is set or not.
  1594. * @param RTC_FLAG specifies the flag to check.
  1595. * This parameter can be one of the following values:
  1596. * @arg RTC_FLAG_RECPF RECALPF event flag.
  1597. * @arg RTC_FLAG_TISOVF Time Stamp OverFlow flag.
  1598. * @arg RTC_FLAG_TISF Time Stamp event flag.
  1599. * @arg RTC_FLAG_WTF WakeUp Timer flag.
  1600. * @arg RTC_FLAG_ALBF Alarm B flag.
  1601. * @arg RTC_FLAG_ALAF Alarm A flag.
  1602. * @arg RTC_FLAG_INITF Initialization mode flag.
  1603. * @arg RTC_FLAG_RSYF Registers Synchronized flag.
  1604. * @arg RTC_FLAG_INITSF Registers Configured flag.
  1605. * @arg RTC_FLAG_SHOPF Shift operation pending flag.
  1606. * @arg RTC_FLAG_WTWF WakeUp Timer Write flag.
  1607. * @arg RTC_FLAG_ALBWF Alarm B Write flag.
  1608. * @arg RTC_FLAG_ALAWF Alarm A write flag.
  1609. * @return The new state of RTC_FLAG (SET or RESET).
  1610. */
  1611. FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
  1612. {
  1613. FlagStatus bitstatus = RESET;
  1614. uint32_t tmpregister = 0;
  1615. /* Check the parameters */
  1616. assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
  1617. /* Get all the flags */
  1618. tmpregister = (uint32_t)(RTC->INITSTS & RTC_FLAGS_MASK);
  1619. /* Return the status of the flag */
  1620. if ((tmpregister & RTC_FLAG) != (uint32_t)RESET)
  1621. {
  1622. bitstatus = SET;
  1623. }
  1624. else
  1625. {
  1626. bitstatus = RESET;
  1627. }
  1628. return bitstatus;
  1629. }
  1630. /**
  1631. * @brief Clears the RTC's pending flags.
  1632. * @param RTC_FLAG specifies the RTC flag to clear.
  1633. * This parameter can be any combination of the following values:.
  1634. * @arg RTC_FLAG_TISOVF Time Stamp Overflow flag.
  1635. * @arg RTC_FLAG_TISF Time Stamp event flag.
  1636. * @arg RTC_FLAG_WTF WakeUp Timer flag.
  1637. * @arg RTC_FLAG_ALBF Alarm B flag.
  1638. * @arg RTC_FLAG_ALAF Alarm A flag.
  1639. * @arg RTC_FLAG_RSYF Registers Synchronized flag.
  1640. */
  1641. void RTC_ClrFlag(uint32_t RTC_FLAG)
  1642. {
  1643. /* Check the parameters */
  1644. assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
  1645. /* Clear the Flags in the RTC_INITSTS register */
  1646. RTC->INITSTS = (uint32_t)(
  1647. (uint32_t)(~((RTC_FLAG | RTC_INITSTS_INITM) & 0x00011FFF) | (uint32_t)(RTC->INITSTS & RTC_INITSTS_INITM)));
  1648. }
  1649. /**
  1650. * @brief Checks whether the specified RTC interrupt has occurred or not.
  1651. * @param RTC_INT specifies the RTC interrupt source to check.
  1652. * This parameter can be one of the following values:
  1653. * @arg RTC_INT_WUT WakeUp Timer interrupt.
  1654. * @arg RTC_INT_ALRB Alarm B interrupt.
  1655. * @arg RTC_INT_ALRA Alarm A interrupt.
  1656. * @return The new state of RTC_INT (SET or RESET).
  1657. */
  1658. INTStatus RTC_GetITStatus(uint32_t RTC_INT)
  1659. {
  1660. INTStatus bitstatus = RESET;
  1661. uint32_t tmpregister = 0, enablestatus = 0;
  1662. /* Check the parameters */
  1663. assert_param(IS_RTC_GET_INT(RTC_INT));
  1664. /* Get the Interrupt enable Status */
  1665. enablestatus = (uint32_t)((RTC->CTRL & RTC_INT));
  1666. /* Get the Interrupt pending bit */
  1667. tmpregister = (uint32_t)((RTC->INITSTS & (uint32_t)(RTC_INT >> 4)));
  1668. /* Get the status of the Interrupt */
  1669. if ((enablestatus != (uint32_t)RESET) && ((tmpregister & 0x0000FFFF) != (uint32_t)RESET))
  1670. {
  1671. bitstatus = SET;
  1672. }
  1673. else
  1674. {
  1675. bitstatus = RESET;
  1676. }
  1677. return bitstatus;
  1678. }
  1679. /**
  1680. * @brief Clears the RTC's interrupt pending bits.
  1681. * @param RTC_INT specifies the RTC interrupt pending bit to clear.
  1682. * This parameter can be any combination of the following values:
  1683. * @arg RTC_INT_WUT WakeUp Timer interrupt
  1684. * @arg RTC_INT_ALRB Alarm B interrupt
  1685. * @arg RTC_INT_ALRA Alarm A interrupt
  1686. */
  1687. void RTC_ClrIntPendingBit(uint32_t RTC_INT)
  1688. {
  1689. uint32_t tmpregister = 0;
  1690. /* Check the parameters */
  1691. assert_param(IS_RTC_CLEAR_INT(RTC_INT));
  1692. /* Get the RTC_INITSTS Interrupt pending bits mask */
  1693. tmpregister = (uint32_t)(RTC_INT >> 4);
  1694. /* Clear the interrupt pending bits in the RTC_INITSTS register */
  1695. RTC->INITSTS = (uint32_t)(
  1696. (uint32_t)(~((tmpregister | RTC_INITSTS_INITM) & 0x0000FFFF) | (uint32_t)(RTC->INITSTS & RTC_INITSTS_INITM)));
  1697. }
  1698. /**
  1699. * @}
  1700. */
  1701. /**
  1702. * @brief Converts a 2 digit decimal to BCD format.
  1703. * @param Value Byte to be converted.
  1704. * @return Converted byte
  1705. */
  1706. static uint8_t RTC_ByteToBcd2(uint8_t Value)
  1707. {
  1708. uint8_t bcdhigh = 0;
  1709. while (Value >= 10)
  1710. {
  1711. bcdhigh++;
  1712. Value -= 10;
  1713. }
  1714. return ((uint8_t)(bcdhigh << 4) | Value);
  1715. }
  1716. /**
  1717. * @brief Convert from 2 digit BCD to Binary.
  1718. * @param Value BCD value to be converted.
  1719. * @return Converted word
  1720. */
  1721. static uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1722. {
  1723. uint8_t tmp = 0;
  1724. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1725. return (tmp + (Value & (uint8_t)0x0F));
  1726. }
  1727. /**
  1728. * @brief Enable wakeup tsc functionand wakeup by the set time
  1729. * @param count wakeup time.
  1730. */
  1731. void RTC_EnableWakeUpTsc(uint32_t count)
  1732. {
  1733. // Wait until bit RTC_TSCWKUPCTRL_WKUPOFF is 1
  1734. while (!(RTC->TSCWKUPCTRL & RTC_TSCWKUPCTRL_WKUPOFF))
  1735. {
  1736. }
  1737. // enter config wakeup cnt mode
  1738. RTC->TSCWKUPCTRL = RTC_TSCWKUPCTRL_WKUPCNF;
  1739. // config tsc wakeup cnt ,tsc wakeup module counting cycle = WAKUPCNT * LSE/LSI
  1740. RTC->TSCWKUPCNT = count;
  1741. // exit config wakeup cnt mode
  1742. RTC->TSCWKUPCTRL &= ~(RTC_TSCWKUPCTRL_WKUPCNF);
  1743. while (!(RTC->TSCWKUPCTRL & RTC_TSCWKUPCTRL_WKUPOFF))
  1744. {
  1745. }
  1746. // TSC wakeup enable
  1747. RTC->TSCWKUPCTRL = RTC_TSCWKUPCTRL_WKUPEN;
  1748. }
  1749. /**
  1750. * @}
  1751. */
  1752. /**
  1753. * @}
  1754. */
  1755. /**
  1756. * @}
  1757. */