stm32f4xx_hal_rtc_ex.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief RTC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Real Time Clock (RTC) Extension peripheral:
  10. * + RTC Time Stamp functions
  11. * + RTC Tamper functions
  12. * + RTC Wake-up functions
  13. * + Extension Control functions
  14. * + Extension RTC features functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. (+) Enable the RTC domain access.
  22. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  23. format using the HAL_RTC_Init() function.
  24. *** RTC Wake-up configuration ***
  25. ================================
  26. [..]
  27. (+) To configure the RTC Wake-up Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
  28. function. You can also configure the RTC Wake-up timer in interrupt mode
  29. using the HAL_RTC_SetWakeUpTimer_IT() function.
  30. (+) To read the RTC Wake-up Counter register, use the HAL_RTC_GetWakeUpTimer()
  31. function.
  32. *** TimeStamp configuration ***
  33. ===============================
  34. [..]
  35. (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
  36. HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
  37. interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
  38. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
  39. function.
  40. (+) The TIMESTAMP alternate function can be mapped either to RTC_AF1 (PC13)
  41. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TSINSEL bit in
  42. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTimeStamp()
  43. or HAL_RTC_SetTimeStamp_IT() function.
  44. *** Tamper configuration ***
  45. ============================
  46. [..]
  47. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  48. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  49. value, sampling frequency, precharge or discharge and Pull-UP using the
  50. HAL_RTC_SetTamper() function. You can configure RTC Tamper in interrupt
  51. mode using HAL_RTC_SetTamper_IT() function.
  52. (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
  53. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TAMP1INSEL bit in
  54. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTamper()
  55. or HAL_RTC_SetTamper_IT() function.
  56. *** Backup Data Registers configuration ***
  57. ===========================================
  58. [..]
  59. (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
  60. function.
  61. (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
  62. function.
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32f4xx_hal.h"
  95. /** @addtogroup STM32F4xx_HAL_Driver
  96. * @{
  97. */
  98. /** @defgroup RTCEx RTCEx
  99. * @brief RTC HAL module driver
  100. * @{
  101. */
  102. #ifdef HAL_RTC_MODULE_ENABLED
  103. /* Private typedef -----------------------------------------------------------*/
  104. /* Private define ------------------------------------------------------------*/
  105. /* Private macro -------------------------------------------------------------*/
  106. /* Private variables ---------------------------------------------------------*/
  107. /* Private function prototypes -----------------------------------------------*/
  108. /* Private functions ---------------------------------------------------------*/
  109. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  110. * @{
  111. */
  112. /** @defgroup RTCEx_Exported_Functions_Group1 RTC TimeStamp and Tamper functions
  113. * @brief RTC TimeStamp and Tamper functions
  114. *
  115. @verbatim
  116. ===============================================================================
  117. ##### RTC TimeStamp and Tamper functions #####
  118. ===============================================================================
  119. [..] This section provides functions allowing to configure TimeStamp feature
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Sets TimeStamp.
  125. * @note This API must be called before enabling the TimeStamp feature.
  126. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  127. * the configuration information for RTC.
  128. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  129. * activated.
  130. * This parameter can be one of the following values:
  131. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  132. * rising edge of the related pin.
  133. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  134. * falling edge of the related pin.
  135. * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
  136. * This parameter can be one of the following values:
  137. * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
  138. * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin. (not applicable in the case of STM32F446xx devices)
  139. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  143. {
  144. uint32_t tmpreg = 0;
  145. /* Check the parameters */
  146. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  147. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  148. /* Process Locked */
  149. __HAL_LOCK(hrtc);
  150. hrtc->State = HAL_RTC_STATE_BUSY;
  151. /* Get the RTC_CR register and clear the bits to be configured */
  152. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  153. tmpreg|= TimeStampEdge;
  154. /* Disable the write protection for RTC registers */
  155. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  156. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  157. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  158. /* Configure the Time Stamp TSEDGE and Enable bits */
  159. hrtc->Instance->CR = (uint32_t)tmpreg;
  160. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  161. /* Enable the write protection for RTC registers */
  162. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  163. /* Change RTC state */
  164. hrtc->State = HAL_RTC_STATE_READY;
  165. /* Process Unlocked */
  166. __HAL_UNLOCK(hrtc);
  167. return HAL_OK;
  168. }
  169. /**
  170. * @brief Sets TimeStamp with Interrupt.
  171. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  172. * the configuration information for RTC.
  173. * @note This API must be called before enabling the TimeStamp feature.
  174. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  175. * activated.
  176. * This parameter can be one of the following values:
  177. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  178. * rising edge of the related pin.
  179. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  180. * falling edge of the related pin.
  181. * @param RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.
  182. * This parameter can be one of the following values:
  183. * @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
  184. * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin. (not applicable in the case of STM32F446xx devices)
  185. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  186. * @retval HAL status
  187. */
  188. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  189. {
  190. uint32_t tmpreg = 0;
  191. /* Check the parameters */
  192. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  193. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  194. /* Process Locked */
  195. __HAL_LOCK(hrtc);
  196. hrtc->State = HAL_RTC_STATE_BUSY;
  197. /* Get the RTC_CR register and clear the bits to be configured */
  198. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  199. tmpreg |= TimeStampEdge;
  200. /* Disable the write protection for RTC registers */
  201. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  202. /* Configure the Time Stamp TSEDGE and Enable bits */
  203. hrtc->Instance->CR = (uint32_t)tmpreg;
  204. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  205. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  206. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  207. /* Enable IT timestamp */
  208. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
  209. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  210. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  211. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  212. /* Enable the write protection for RTC registers */
  213. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  214. hrtc->State = HAL_RTC_STATE_READY;
  215. /* Process Unlocked */
  216. __HAL_UNLOCK(hrtc);
  217. return HAL_OK;
  218. }
  219. /**
  220. * @brief Deactivates TimeStamp.
  221. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  222. * the configuration information for RTC.
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  226. {
  227. uint32_t tmpreg = 0;
  228. /* Process Locked */
  229. __HAL_LOCK(hrtc);
  230. hrtc->State = HAL_RTC_STATE_BUSY;
  231. /* Disable the write protection for RTC registers */
  232. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  233. /* In case of interrupt mode is used, the interrupt source must disabled */
  234. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  235. /* Get the RTC_CR register and clear the bits to be configured */
  236. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  237. /* Configure the Time Stamp TSEDGE and Enable bits */
  238. hrtc->Instance->CR = (uint32_t)tmpreg;
  239. /* Enable the write protection for RTC registers */
  240. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  241. hrtc->State = HAL_RTC_STATE_READY;
  242. /* Process Unlocked */
  243. __HAL_UNLOCK(hrtc);
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Gets the RTC TimeStamp value.
  248. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  249. * the configuration information for RTC.
  250. * @param sTimeStamp: Pointer to Time structure
  251. * @param sTimeStampDate: Pointer to Date structure
  252. * @param Format: specifies the format of the entered parameters.
  253. * This parameter can be one of the following values:
  254. * RTC_FORMAT_BIN: Binary data format
  255. * RTC_FORMAT_BCD: BCD data format
  256. * @retval HAL status
  257. */
  258. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
  259. {
  260. uint32_t tmptime = 0, tmpdate = 0;
  261. /* Check the parameters */
  262. assert_param(IS_RTC_FORMAT(Format));
  263. /* Get the TimeStamp time and date registers values */
  264. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  265. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  266. /* Fill the Time structure fields with the read parameters */
  267. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  268. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
  269. sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  270. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
  271. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  272. /* Fill the Date structure fields with the read parameters */
  273. sTimeStampDate->Year = 0;
  274. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  275. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  276. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
  277. /* Check the input parameters format */
  278. if(Format == RTC_FORMAT_BIN)
  279. {
  280. /* Convert the TimeStamp structure parameters to Binary format */
  281. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  282. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  283. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  284. /* Convert the DateTimeStamp structure parameters to Binary format */
  285. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  286. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  287. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  288. }
  289. /* Clear the TIMESTAMP Flag */
  290. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  291. return HAL_OK;
  292. }
  293. /**
  294. * @brief Sets Tamper
  295. * @note By calling this API we disable the tamper interrupt for all tampers.
  296. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  297. * the configuration information for RTC.
  298. * @param sTamper: Pointer to Tamper Structure.
  299. * @retval HAL status
  300. */
  301. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  302. {
  303. uint32_t tmpreg = 0;
  304. /* Check the parameters */
  305. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  306. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  307. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  308. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  309. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  310. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  311. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  312. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  313. /* Process Locked */
  314. __HAL_LOCK(hrtc);
  315. hrtc->State = HAL_RTC_STATE_BUSY;
  316. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  317. {
  318. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  319. }
  320. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  321. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  322. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  323. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
  324. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  325. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL | (uint32_t)RTC_TAFCR_TAMPIE);
  326. hrtc->Instance->TAFCR |= tmpreg;
  327. hrtc->State = HAL_RTC_STATE_READY;
  328. /* Process Unlocked */
  329. __HAL_UNLOCK(hrtc);
  330. return HAL_OK;
  331. }
  332. /**
  333. * @brief Sets Tamper with interrupt.
  334. * @note By calling this API we force the tamper interrupt for all tampers.
  335. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  336. * the configuration information for RTC.
  337. * @param sTamper: Pointer to RTC Tamper.
  338. * @retval HAL status
  339. */
  340. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  341. {
  342. uint32_t tmpreg = 0;
  343. /* Check the parameters */
  344. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  345. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  346. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  347. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  348. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  349. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  350. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  351. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  352. /* Process Locked */
  353. __HAL_LOCK(hrtc);
  354. hrtc->State = HAL_RTC_STATE_BUSY;
  355. /* Configure the tamper trigger */
  356. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  357. {
  358. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  359. }
  360. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  361. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  362. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  363. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
  364. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  365. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL);
  366. hrtc->Instance->TAFCR |= tmpreg;
  367. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  368. hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
  369. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  370. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  371. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  372. hrtc->State = HAL_RTC_STATE_READY;
  373. /* Process Unlocked */
  374. __HAL_UNLOCK(hrtc);
  375. return HAL_OK;
  376. }
  377. /**
  378. * @brief Deactivates Tamper.
  379. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  380. * the configuration information for RTC.
  381. * @param Tamper: Selected tamper pin.
  382. * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
  383. * @retval HAL status
  384. */
  385. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  386. {
  387. assert_param(IS_RTC_TAMPER(Tamper));
  388. /* Process Locked */
  389. __HAL_LOCK(hrtc);
  390. hrtc->State = HAL_RTC_STATE_BUSY;
  391. /* Disable the selected Tamper pin */
  392. hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  393. hrtc->State = HAL_RTC_STATE_READY;
  394. /* Process Unlocked */
  395. __HAL_UNLOCK(hrtc);
  396. return HAL_OK;
  397. }
  398. /**
  399. * @brief This function handles TimeStamp interrupt request.
  400. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  401. * the configuration information for RTC.
  402. * @retval None
  403. */
  404. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  405. {
  406. if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
  407. {
  408. /* Get the status of the Interrupt */
  409. if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
  410. {
  411. /* TIMESTAMP callback */
  412. HAL_RTCEx_TimeStampEventCallback(hrtc);
  413. /* Clear the TIMESTAMP interrupt pending bit */
  414. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
  415. }
  416. }
  417. /* Get the status of the Interrupt */
  418. if(__HAL_RTC_TAMPER_GET_IT(hrtc,RTC_IT_TAMP1))
  419. {
  420. /* Get the TAMPER Interrupt enable bit and pending bit */
  421. if(((hrtc->Instance->TAFCR & (RTC_TAFCR_TAMPIE))) != (uint32_t)RESET)
  422. {
  423. /* Tamper callback */
  424. HAL_RTCEx_Tamper1EventCallback(hrtc);
  425. /* Clear the Tamper interrupt pending bit */
  426. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  427. }
  428. }
  429. /* Get the status of the Interrupt */
  430. if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP2))
  431. {
  432. /* Get the TAMPER Interrupt enable bit and pending bit */
  433. if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
  434. {
  435. /* Tamper callback */
  436. HAL_RTCEx_Tamper2EventCallback(hrtc);
  437. /* Clear the Tamper interrupt pending bit */
  438. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  439. }
  440. }
  441. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  442. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  443. /* Change RTC state */
  444. hrtc->State = HAL_RTC_STATE_READY;
  445. }
  446. /**
  447. * @brief TimeStamp callback.
  448. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  449. * the configuration information for RTC.
  450. * @retval None
  451. */
  452. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  453. {
  454. /* NOTE : This function Should not be modified, when the callback is needed,
  455. the HAL_RTC_TimeStampEventCallback could be implemented in the user file
  456. */
  457. }
  458. /**
  459. * @brief Tamper 1 callback.
  460. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  461. * the configuration information for RTC.
  462. * @retval None
  463. */
  464. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  465. {
  466. /* NOTE : This function Should not be modified, when the callback is needed,
  467. the HAL_RTC_Tamper1EventCallback could be implemented in the user file
  468. */
  469. }
  470. /**
  471. * @brief Tamper 2 callback.
  472. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  473. * the configuration information for RTC.
  474. * @retval None
  475. */
  476. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  477. {
  478. /* NOTE : This function Should not be modified, when the callback is needed,
  479. the HAL_RTC_Tamper2EventCallback could be implemented in the user file
  480. */
  481. }
  482. /**
  483. * @brief This function handles TimeStamp polling request.
  484. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  485. * the configuration information for RTC.
  486. * @param Timeout: Timeout duration
  487. * @retval HAL status
  488. */
  489. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  490. {
  491. uint32_t tickstart = 0;
  492. /* Get tick */
  493. tickstart = HAL_GetTick();
  494. while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
  495. {
  496. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  497. {
  498. /* Clear the TIMESTAMP Overrun Flag */
  499. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  500. /* Change TIMESTAMP state */
  501. hrtc->State = HAL_RTC_STATE_ERROR;
  502. return HAL_ERROR;
  503. }
  504. if(Timeout != HAL_MAX_DELAY)
  505. {
  506. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  507. {
  508. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  509. return HAL_TIMEOUT;
  510. }
  511. }
  512. }
  513. /* Change RTC state */
  514. hrtc->State = HAL_RTC_STATE_READY;
  515. return HAL_OK;
  516. }
  517. /**
  518. * @brief This function handles Tamper1 Polling.
  519. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  520. * the configuration information for RTC.
  521. * @param Timeout: Timeout duration
  522. * @retval HAL status
  523. */
  524. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  525. {
  526. uint32_t tickstart = 0;
  527. /* Get tick */
  528. tickstart = HAL_GetTick();
  529. /* Get the status of the Interrupt */
  530. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
  531. {
  532. if(Timeout != HAL_MAX_DELAY)
  533. {
  534. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  535. {
  536. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  537. return HAL_TIMEOUT;
  538. }
  539. }
  540. }
  541. /* Clear the Tamper Flag */
  542. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  543. /* Change RTC state */
  544. hrtc->State = HAL_RTC_STATE_READY;
  545. return HAL_OK;
  546. }
  547. /**
  548. * @brief This function handles Tamper2 Polling.
  549. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  550. * the configuration information for RTC.
  551. * @param Timeout: Timeout duration
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  555. {
  556. uint32_t tickstart = 0;
  557. /* Get tick */
  558. tickstart = HAL_GetTick();
  559. /* Get the status of the Interrupt */
  560. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
  561. {
  562. if(Timeout != HAL_MAX_DELAY)
  563. {
  564. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  565. {
  566. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  567. return HAL_TIMEOUT;
  568. }
  569. }
  570. }
  571. /* Clear the Tamper Flag */
  572. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
  573. /* Change RTC state */
  574. hrtc->State = HAL_RTC_STATE_READY;
  575. return HAL_OK;
  576. }
  577. /**
  578. * @}
  579. */
  580. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wake-up functions
  581. * @brief RTC Wake-up functions
  582. *
  583. @verbatim
  584. ===============================================================================
  585. ##### RTC Wake-up functions #####
  586. ===============================================================================
  587. [..] This section provides functions allowing to configure Wake-up feature
  588. @endverbatim
  589. * @{
  590. */
  591. /**
  592. * @brief Sets wake up timer.
  593. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  594. * the configuration information for RTC.
  595. * @param WakeUpCounter: Wake up counter
  596. * @param WakeUpClock: Wake up clock
  597. * @retval HAL status
  598. */
  599. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  600. {
  601. uint32_t tickstart = 0;
  602. /* Check the parameters */
  603. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  604. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  605. /* Process Locked */
  606. __HAL_LOCK(hrtc);
  607. hrtc->State = HAL_RTC_STATE_BUSY;
  608. /* Disable the write protection for RTC registers */
  609. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  610. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  611. /* Get tick */
  612. tickstart = HAL_GetTick();
  613. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  614. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  615. {
  616. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  617. {
  618. /* Enable the write protection for RTC registers */
  619. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  620. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  621. /* Process Unlocked */
  622. __HAL_UNLOCK(hrtc);
  623. return HAL_TIMEOUT;
  624. }
  625. }
  626. /* Clear the Wake-up Timer clock source bits in CR register */
  627. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  628. /* Configure the clock source */
  629. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  630. /* Configure the Wake-up Timer counter */
  631. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  632. /* Enable the Wake-up Timer */
  633. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  634. /* Enable the write protection for RTC registers */
  635. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  636. hrtc->State = HAL_RTC_STATE_READY;
  637. /* Process Unlocked */
  638. __HAL_UNLOCK(hrtc);
  639. return HAL_OK;
  640. }
  641. /**
  642. * @brief Sets wake up timer with interrupt
  643. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  644. * the configuration information for RTC.
  645. * @param WakeUpCounter: Wake up counter
  646. * @param WakeUpClock: Wake up clock
  647. * @retval HAL status
  648. */
  649. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  650. {
  651. uint32_t tickstart = 0;
  652. /* Check the parameters */
  653. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  654. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  655. /* Process Locked */
  656. __HAL_LOCK(hrtc);
  657. hrtc->State = HAL_RTC_STATE_BUSY;
  658. /* Disable the write protection for RTC registers */
  659. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  660. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  661. /* Get tick */
  662. tickstart = HAL_GetTick();
  663. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  664. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  665. {
  666. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  667. {
  668. /* Enable the write protection for RTC registers */
  669. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  670. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  671. /* Process Unlocked */
  672. __HAL_UNLOCK(hrtc);
  673. return HAL_TIMEOUT;
  674. }
  675. }
  676. /* Configure the Wake-up Timer counter */
  677. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  678. /* Clear the Wake-up Timer clock source bits in CR register */
  679. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  680. /* Configure the clock source */
  681. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  682. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  683. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  684. EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
  685. /* Configure the Interrupt in the RTC_CR register */
  686. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
  687. /* Enable the Wake-up Timer */
  688. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  689. /* Enable the write protection for RTC registers */
  690. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  691. hrtc->State = HAL_RTC_STATE_READY;
  692. /* Process Unlocked */
  693. __HAL_UNLOCK(hrtc);
  694. return HAL_OK;
  695. }
  696. /**
  697. * @brief Deactivates wake up timer counter.
  698. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  699. * the configuration information for RTC.
  700. * @retval HAL status
  701. */
  702. uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  703. {
  704. uint32_t tickstart = 0;
  705. /* Process Locked */
  706. __HAL_LOCK(hrtc);
  707. hrtc->State = HAL_RTC_STATE_BUSY;
  708. /* Disable the write protection for RTC registers */
  709. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  710. /* Disable the Wake-up Timer */
  711. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  712. /* In case of interrupt mode is used, the interrupt source must disabled */
  713. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
  714. /* Get tick */
  715. tickstart = HAL_GetTick();
  716. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  717. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  718. {
  719. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  720. {
  721. /* Enable the write protection for RTC registers */
  722. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  723. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  724. /* Process Unlocked */
  725. __HAL_UNLOCK(hrtc);
  726. return HAL_TIMEOUT;
  727. }
  728. }
  729. /* Enable the write protection for RTC registers */
  730. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  731. hrtc->State = HAL_RTC_STATE_READY;
  732. /* Process Unlocked */
  733. __HAL_UNLOCK(hrtc);
  734. return HAL_OK;
  735. }
  736. /**
  737. * @brief Gets wake up timer counter.
  738. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  739. * the configuration information for RTC.
  740. * @retval Counter value
  741. */
  742. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  743. {
  744. /* Get the counter value */
  745. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  746. }
  747. /**
  748. * @brief This function handles Wake Up Timer interrupt request.
  749. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  750. * the configuration information for RTC.
  751. * @retval None
  752. */
  753. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  754. {
  755. if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
  756. {
  757. /* Get the status of the Interrupt */
  758. if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
  759. {
  760. /* WAKEUPTIMER callback */
  761. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  762. /* Clear the WAKEUPTIMER interrupt pending bit */
  763. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  764. }
  765. }
  766. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  767. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  768. /* Change RTC state */
  769. hrtc->State = HAL_RTC_STATE_READY;
  770. }
  771. /**
  772. * @brief Wake Up Timer callback.
  773. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  774. * the configuration information for RTC.
  775. * @retval None
  776. */
  777. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  778. {
  779. /* NOTE : This function Should not be modified, when the callback is needed,
  780. the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
  781. */
  782. }
  783. /**
  784. * @brief This function handles Wake Up Timer Polling.
  785. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  786. * the configuration information for RTC.
  787. * @param Timeout: Timeout duration
  788. * @retval HAL status
  789. */
  790. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  791. {
  792. uint32_t tickstart = 0;
  793. /* Get tick */
  794. tickstart = HAL_GetTick();
  795. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  796. {
  797. if(Timeout != HAL_MAX_DELAY)
  798. {
  799. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  800. {
  801. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  802. return HAL_TIMEOUT;
  803. }
  804. }
  805. }
  806. /* Clear the WAKEUPTIMER Flag */
  807. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  808. /* Change RTC state */
  809. hrtc->State = HAL_RTC_STATE_READY;
  810. return HAL_OK;
  811. }
  812. /**
  813. * @}
  814. */
  815. /** @defgroup RTCEx_Exported_Functions_Group3 Extension Peripheral Control functions
  816. * @brief Extension Peripheral Control functions
  817. *
  818. @verbatim
  819. ===============================================================================
  820. ##### Extension Peripheral Control functions #####
  821. ===============================================================================
  822. [..]
  823. This subsection provides functions allowing to
  824. (+) Write a data in a specified RTC Backup data register
  825. (+) Read a data in a specified RTC Backup data register
  826. (+) Set the Coarse calibration parameters.
  827. (+) Deactivate the Coarse calibration parameters
  828. (+) Set the Smooth calibration parameters.
  829. (+) Configure the Synchronization Shift Control Settings.
  830. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  831. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  832. (+) Enable the RTC reference clock detection.
  833. (+) Disable the RTC reference clock detection.
  834. (+) Enable the Bypass Shadow feature.
  835. (+) Disable the Bypass Shadow feature.
  836. @endverbatim
  837. * @{
  838. */
  839. /**
  840. * @brief Writes a data in a specified RTC Backup data register.
  841. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  842. * the configuration information for RTC.
  843. * @param BackupRegister: RTC Backup data Register number.
  844. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  845. * specify the register.
  846. * @param Data: Data to be written in the specified RTC Backup data register.
  847. * @retval None
  848. */
  849. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  850. {
  851. uint32_t tmp = 0;
  852. /* Check the parameters */
  853. assert_param(IS_RTC_BKP(BackupRegister));
  854. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  855. tmp += (BackupRegister * 4);
  856. /* Write the specified register */
  857. *(__IO uint32_t *)tmp = (uint32_t)Data;
  858. }
  859. /**
  860. * @brief Reads data from the specified RTC Backup data Register.
  861. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  862. * the configuration information for RTC.
  863. * @param BackupRegister: RTC Backup data Register number.
  864. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  865. * specify the register.
  866. * @retval Read value
  867. */
  868. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  869. {
  870. uint32_t tmp = 0;
  871. /* Check the parameters */
  872. assert_param(IS_RTC_BKP(BackupRegister));
  873. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  874. tmp += (BackupRegister * 4);
  875. /* Read the specified register */
  876. return (*(__IO uint32_t *)tmp);
  877. }
  878. /**
  879. * @brief Sets the Coarse calibration parameters.
  880. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  881. * the configuration information for RTC.
  882. * @param CalibSign: Specifies the sign of the coarse calibration value.
  883. * This parameter can be one of the following values :
  884. * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
  885. * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
  886. * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
  887. *
  888. * @note This Calibration value should be between 0 and 63 when using negative
  889. * sign with a 2-ppm step.
  890. *
  891. * @note This Calibration value should be between 0 and 126 when using positive
  892. * sign with a 4-ppm step.
  893. * @retval HAL status
  894. */
  895. HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
  896. {
  897. /* Check the parameters */
  898. assert_param(IS_RTC_CALIB_SIGN(CalibSign));
  899. assert_param(IS_RTC_CALIB_VALUE(Value));
  900. /* Process Locked */
  901. __HAL_LOCK(hrtc);
  902. hrtc->State = HAL_RTC_STATE_BUSY;
  903. /* Disable the write protection for RTC registers */
  904. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  905. /* Set Initialization mode */
  906. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  907. {
  908. /* Enable the write protection for RTC registers */
  909. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  910. /* Set RTC state*/
  911. hrtc->State = HAL_RTC_STATE_ERROR;
  912. /* Process Unlocked */
  913. __HAL_UNLOCK(hrtc);
  914. return HAL_ERROR;
  915. }
  916. else
  917. {
  918. /* Enable the Coarse Calibration */
  919. __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
  920. /* Set the coarse calibration value */
  921. hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
  922. /* Exit Initialization mode */
  923. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  924. }
  925. /* Enable the write protection for RTC registers */
  926. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  927. /* Change state */
  928. hrtc->State = HAL_RTC_STATE_READY;
  929. /* Process Unlocked */
  930. __HAL_UNLOCK(hrtc);
  931. return HAL_OK;
  932. }
  933. /**
  934. * @brief Deactivates the Coarse calibration parameters.
  935. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  936. * the configuration information for RTC.
  937. * @retval HAL status
  938. */
  939. HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
  940. {
  941. /* Process Locked */
  942. __HAL_LOCK(hrtc);
  943. hrtc->State = HAL_RTC_STATE_BUSY;
  944. /* Disable the write protection for RTC registers */
  945. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  946. /* Set Initialization mode */
  947. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  948. {
  949. /* Enable the write protection for RTC registers */
  950. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  951. /* Set RTC state*/
  952. hrtc->State = HAL_RTC_STATE_ERROR;
  953. /* Process Unlocked */
  954. __HAL_UNLOCK(hrtc);
  955. return HAL_ERROR;
  956. }
  957. else
  958. {
  959. /* Enable the Coarse Calibration */
  960. __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
  961. /* Exit Initialization mode */
  962. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  963. }
  964. /* Enable the write protection for RTC registers */
  965. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  966. /* Change state */
  967. hrtc->State = HAL_RTC_STATE_READY;
  968. /* Process Unlocked */
  969. __HAL_UNLOCK(hrtc);
  970. return HAL_OK;
  971. }
  972. /**
  973. * @brief Sets the Smooth calibration parameters.
  974. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  975. * the configuration information for RTC.
  976. * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
  977. * This parameter can be can be one of the following values :
  978. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  979. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  980. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  981. * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
  982. * This parameter can be one of the following values:
  983. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  984. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  985. * @param SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  986. * This parameter can be one any value from 0 to 0x000001FF.
  987. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  988. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  989. * SmouthCalibMinusPulsesValue must be equal to 0.
  990. * @retval HAL status
  991. */
  992. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  993. {
  994. uint32_t tickstart = 0;
  995. /* Check the parameters */
  996. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  997. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  998. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  999. /* Process Locked */
  1000. __HAL_LOCK(hrtc);
  1001. hrtc->State = HAL_RTC_STATE_BUSY;
  1002. /* Disable the write protection for RTC registers */
  1003. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1004. /* check if a calibration is pending*/
  1005. if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1006. {
  1007. /* Get tick */
  1008. tickstart = HAL_GetTick();
  1009. /* check if a calibration is pending*/
  1010. while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1011. {
  1012. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1013. {
  1014. /* Enable the write protection for RTC registers */
  1015. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1016. /* Change RTC state */
  1017. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1018. /* Process Unlocked */
  1019. __HAL_UNLOCK(hrtc);
  1020. return HAL_TIMEOUT;
  1021. }
  1022. }
  1023. }
  1024. /* Configure the Smooth calibration settings */
  1025. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
  1026. /* Enable the write protection for RTC registers */
  1027. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1028. /* Change RTC state */
  1029. hrtc->State = HAL_RTC_STATE_READY;
  1030. /* Process Unlocked */
  1031. __HAL_UNLOCK(hrtc);
  1032. return HAL_OK;
  1033. }
  1034. /**
  1035. * @brief Configures the Synchronization Shift Control Settings.
  1036. * @note When REFCKON is set, firmware must not write to Shift control register.
  1037. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1038. * the configuration information for RTC.
  1039. * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
  1040. * This parameter can be one of the following values :
  1041. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1042. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1043. * @param ShiftSubFS: Select the number of Second Fractions to substitute.
  1044. * This parameter can be one any value from 0 to 0x7FFF.
  1045. * @retval HAL status
  1046. */
  1047. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1048. {
  1049. uint32_t tickstart = 0;
  1050. /* Check the parameters */
  1051. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1052. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1053. /* Process Locked */
  1054. __HAL_LOCK(hrtc);
  1055. hrtc->State = HAL_RTC_STATE_BUSY;
  1056. /* Disable the write protection for RTC registers */
  1057. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1058. /* Get tick */
  1059. tickstart = HAL_GetTick();
  1060. /* Wait until the shift is completed*/
  1061. while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
  1062. {
  1063. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1064. {
  1065. /* Enable the write protection for RTC registers */
  1066. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1067. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1068. /* Process Unlocked */
  1069. __HAL_UNLOCK(hrtc);
  1070. return HAL_TIMEOUT;
  1071. }
  1072. }
  1073. /* Check if the reference clock detection is disabled */
  1074. if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
  1075. {
  1076. /* Configure the Shift settings */
  1077. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1078. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1079. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  1080. {
  1081. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1082. {
  1083. /* Enable the write protection for RTC registers */
  1084. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1085. hrtc->State = HAL_RTC_STATE_ERROR;
  1086. /* Process Unlocked */
  1087. __HAL_UNLOCK(hrtc);
  1088. return HAL_ERROR;
  1089. }
  1090. }
  1091. }
  1092. else
  1093. {
  1094. /* Enable the write protection for RTC registers */
  1095. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1096. /* Change RTC state */
  1097. hrtc->State = HAL_RTC_STATE_ERROR;
  1098. /* Process Unlocked */
  1099. __HAL_UNLOCK(hrtc);
  1100. return HAL_ERROR;
  1101. }
  1102. /* Enable the write protection for RTC registers */
  1103. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1104. /* Change RTC state */
  1105. hrtc->State = HAL_RTC_STATE_READY;
  1106. /* Process Unlocked */
  1107. __HAL_UNLOCK(hrtc);
  1108. return HAL_OK;
  1109. }
  1110. /**
  1111. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1112. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1113. * the configuration information for RTC.
  1114. * @param CalibOutput: Select the Calibration output Selection .
  1115. * This parameter can be one of the following values:
  1116. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1117. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1118. * @retval HAL status
  1119. */
  1120. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
  1121. {
  1122. /* Check the parameters */
  1123. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1124. /* Process Locked */
  1125. __HAL_LOCK(hrtc);
  1126. hrtc->State = HAL_RTC_STATE_BUSY;
  1127. /* Disable the write protection for RTC registers */
  1128. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1129. /* Clear flags before config */
  1130. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1131. /* Configure the RTC_CR register */
  1132. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1133. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1134. /* Enable the write protection for RTC registers */
  1135. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1136. /* Change RTC state */
  1137. hrtc->State = HAL_RTC_STATE_READY;
  1138. /* Process Unlocked */
  1139. __HAL_UNLOCK(hrtc);
  1140. return HAL_OK;
  1141. }
  1142. /**
  1143. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1144. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1145. * the configuration information for RTC.
  1146. * @retval HAL status
  1147. */
  1148. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1149. {
  1150. /* Process Locked */
  1151. __HAL_LOCK(hrtc);
  1152. hrtc->State = HAL_RTC_STATE_BUSY;
  1153. /* Disable the write protection for RTC registers */
  1154. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1155. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1156. /* Enable the write protection for RTC registers */
  1157. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1158. /* Change RTC state */
  1159. hrtc->State = HAL_RTC_STATE_READY;
  1160. /* Process Unlocked */
  1161. __HAL_UNLOCK(hrtc);
  1162. return HAL_OK;
  1163. }
  1164. /**
  1165. * @brief Enables the RTC reference clock detection.
  1166. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1167. * the configuration information for RTC.
  1168. * @retval HAL status
  1169. */
  1170. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
  1171. {
  1172. /* Process Locked */
  1173. __HAL_LOCK(hrtc);
  1174. hrtc->State = HAL_RTC_STATE_BUSY;
  1175. /* Disable the write protection for RTC registers */
  1176. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1177. /* Set Initialization mode */
  1178. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1179. {
  1180. /* Enable the write protection for RTC registers */
  1181. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1182. /* Set RTC state*/
  1183. hrtc->State = HAL_RTC_STATE_ERROR;
  1184. /* Process Unlocked */
  1185. __HAL_UNLOCK(hrtc);
  1186. return HAL_ERROR;
  1187. }
  1188. else
  1189. {
  1190. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1191. /* Exit Initialization mode */
  1192. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1193. }
  1194. /* Enable the write protection for RTC registers */
  1195. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1196. /* Change RTC state */
  1197. hrtc->State = HAL_RTC_STATE_READY;
  1198. /* Process Unlocked */
  1199. __HAL_UNLOCK(hrtc);
  1200. return HAL_OK;
  1201. }
  1202. /**
  1203. * @brief Disable the RTC reference clock detection.
  1204. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1205. * the configuration information for RTC.
  1206. * @retval HAL status
  1207. */
  1208. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
  1209. {
  1210. /* Process Locked */
  1211. __HAL_LOCK(hrtc);
  1212. hrtc->State = HAL_RTC_STATE_BUSY;
  1213. /* Disable the write protection for RTC registers */
  1214. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1215. /* Set Initialization mode */
  1216. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1217. {
  1218. /* Enable the write protection for RTC registers */
  1219. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1220. /* Set RTC state*/
  1221. hrtc->State = HAL_RTC_STATE_ERROR;
  1222. /* Process Unlocked */
  1223. __HAL_UNLOCK(hrtc);
  1224. return HAL_ERROR;
  1225. }
  1226. else
  1227. {
  1228. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1229. /* Exit Initialization mode */
  1230. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1231. }
  1232. /* Enable the write protection for RTC registers */
  1233. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1234. /* Change RTC state */
  1235. hrtc->State = HAL_RTC_STATE_READY;
  1236. /* Process Unlocked */
  1237. __HAL_UNLOCK(hrtc);
  1238. return HAL_OK;
  1239. }
  1240. /**
  1241. * @brief Enables the Bypass Shadow feature.
  1242. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1243. * the configuration information for RTC.
  1244. * @note When the Bypass Shadow is enabled the calendar value are taken
  1245. * directly from the Calendar counter.
  1246. * @retval HAL status
  1247. */
  1248. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
  1249. {
  1250. /* Process Locked */
  1251. __HAL_LOCK(hrtc);
  1252. hrtc->State = HAL_RTC_STATE_BUSY;
  1253. /* Disable the write protection for RTC registers */
  1254. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1255. /* Set the BYPSHAD bit */
  1256. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1257. /* Enable the write protection for RTC registers */
  1258. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1259. /* Change RTC state */
  1260. hrtc->State = HAL_RTC_STATE_READY;
  1261. /* Process Unlocked */
  1262. __HAL_UNLOCK(hrtc);
  1263. return HAL_OK;
  1264. }
  1265. /**
  1266. * @brief Disables the Bypass Shadow feature.
  1267. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1268. * the configuration information for RTC.
  1269. * @note When the Bypass Shadow is enabled the calendar value are taken
  1270. * directly from the Calendar counter.
  1271. * @retval HAL status
  1272. */
  1273. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
  1274. {
  1275. /* Process Locked */
  1276. __HAL_LOCK(hrtc);
  1277. hrtc->State = HAL_RTC_STATE_BUSY;
  1278. /* Disable the write protection for RTC registers */
  1279. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1280. /* Reset the BYPSHAD bit */
  1281. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1282. /* Enable the write protection for RTC registers */
  1283. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1284. /* Change RTC state */
  1285. hrtc->State = HAL_RTC_STATE_READY;
  1286. /* Process Unlocked */
  1287. __HAL_UNLOCK(hrtc);
  1288. return HAL_OK;
  1289. }
  1290. /**
  1291. * @}
  1292. */
  1293. /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
  1294. * @brief Extended features functions
  1295. *
  1296. @verbatim
  1297. ===============================================================================
  1298. ##### Extended features functions #####
  1299. ===============================================================================
  1300. [..] This section provides functions allowing to:
  1301. (+) RTC Alarm B callback
  1302. (+) RTC Poll for Alarm B request
  1303. @endverbatim
  1304. * @{
  1305. */
  1306. /**
  1307. * @brief Alarm B callback.
  1308. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1309. * the configuration information for RTC.
  1310. * @retval None
  1311. */
  1312. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1313. {
  1314. /* NOTE : This function Should not be modified, when the callback is needed,
  1315. the HAL_RTC_AlarmBEventCallback could be implemented in the user file
  1316. */
  1317. }
  1318. /**
  1319. * @brief This function handles AlarmB Polling request.
  1320. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1321. * the configuration information for RTC.
  1322. * @param Timeout: Timeout duration
  1323. * @retval HAL status
  1324. */
  1325. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1326. {
  1327. uint32_t tickstart = 0;
  1328. /* Get tick */
  1329. tickstart = HAL_GetTick();
  1330. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  1331. {
  1332. if(Timeout != HAL_MAX_DELAY)
  1333. {
  1334. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1335. {
  1336. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1337. return HAL_TIMEOUT;
  1338. }
  1339. }
  1340. }
  1341. /* Clear the Alarm Flag */
  1342. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1343. /* Change RTC state */
  1344. hrtc->State = HAL_RTC_STATE_READY;
  1345. return HAL_OK;
  1346. }
  1347. /**
  1348. * @}
  1349. */
  1350. /**
  1351. * @}
  1352. */
  1353. #endif /* HAL_RTC_MODULE_ENABLED */
  1354. /**
  1355. * @}
  1356. */
  1357. /**
  1358. * @}
  1359. */
  1360. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/