STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_hal_rtc_ex.c
Go to the documentation of this file.
1 
125 /* Includes ------------------------------------------------------------------*/
126 #include "stm32f4xx_hal.h"
127 
137 #ifdef HAL_RTC_MODULE_ENABLED
138 
139 /* Private typedef -----------------------------------------------------------*/
140 /* Private define ------------------------------------------------------------*/
141 /* Private macro -------------------------------------------------------------*/
142 /* Private variables ---------------------------------------------------------*/
143 /* Private function prototypes -----------------------------------------------*/
144 /* Exported functions --------------------------------------------------------*/
145 
186 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
187 {
188  uint32_t tmpreg = 0U;
189 
190  /* Check the parameters */
191  assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
192  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
193 
194  /* Process Locked */
195  __HAL_LOCK(hrtc);
196 
197  /* Change RTC state to BUSY */
198  hrtc->State = HAL_RTC_STATE_BUSY;
199 
200  hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
201  hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
202 
203  /* Get the RTC_CR register and clear the bits to be configured */
204  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
205 
206  /* Configure the Timestamp TSEDGE bit */
207  tmpreg |= RTC_TimeStampEdge;
208 
209  /* Disable the write protection for RTC registers */
210  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
211 
212  /* Copy the desired configuration into the CR register */
213  hrtc->Instance->CR = (uint32_t)tmpreg;
214 
215  /* Clear RTC Timestamp flag */
216  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
217 
218  /* Clear RTC Timestamp overrun Flag */
219  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
220 
221  /* Enable the Timestamp saving */
222  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
223 
224  /* Enable the write protection for RTC registers */
225  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
226 
227  /* Change RTC state back to READY */
228  hrtc->State = HAL_RTC_STATE_READY;
229 
230  /* Process Unlocked */
231  __HAL_UNLOCK(hrtc);
232 
233  return HAL_OK;
234 }
235 
258 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
259 {
260  uint32_t tmpreg = 0U;
261 
262  /* Check the parameters */
263  assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
264  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
265 
266  /* Process Locked */
267  __HAL_LOCK(hrtc);
268 
269  /* Change RTC state to BUSY */
270  hrtc->State = HAL_RTC_STATE_BUSY;
271 
272  hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
273  hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
274 
275  /* Get the RTC_CR register and clear the bits to be configured */
276  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
277 
278  /* Configure the Timestamp TSEDGE bit */
279  tmpreg |= RTC_TimeStampEdge;
280 
281  /* Disable the write protection for RTC registers */
282  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
283 
284  /* Copy the desired configuration into the CR register */
285  hrtc->Instance->CR = (uint32_t)tmpreg;
286 
287  /* Clear RTC Timestamp flag */
288  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
289 
290  /* Clear RTC Timestamp overrun Flag */
291  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
292 
293  /* Enable the Timestamp saving */
294  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
295 
296  /* Enable IT Timestamp */
297  __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
298 
299  /* Enable the write protection for RTC registers */
300  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
301 
302  /* RTC Timestamp Interrupt Configuration: EXTI configuration */
303  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
304  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
305 
306  /* Change RTC state back to READY */
307  hrtc->State = HAL_RTC_STATE_READY;
308 
309  /* Process Unlocked */
310  __HAL_UNLOCK(hrtc);
311 
312  return HAL_OK;
313 }
314 
322 {
323  uint32_t tmpreg = 0U;
324 
325  /* Process Locked */
326  __HAL_LOCK(hrtc);
327 
328  hrtc->State = HAL_RTC_STATE_BUSY;
329 
330  /* Disable the write protection for RTC registers */
331  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
332 
333  /* In case of interrupt mode is used, the interrupt source must disabled */
334  __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
335 
336  /* Get the RTC_CR register and clear the bits to be configured */
337  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
338 
339  /* Configure the Timestamp TSEDGE and Enable bits */
340  hrtc->Instance->CR = (uint32_t)tmpreg;
341 
342  /* Enable the write protection for RTC registers */
343  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
344 
345  hrtc->State = HAL_RTC_STATE_READY;
346 
347  /* Process Unlocked */
348  __HAL_UNLOCK(hrtc);
349 
350  return HAL_OK;
351 }
352 
365 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
366 {
367  uint32_t tmptime = 0U;
368  uint32_t tmpdate = 0U;
369 
370  /* Check the parameters */
371  assert_param(IS_RTC_FORMAT(Format));
372 
373  /* Get the Timestamp time and date registers values */
374  tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
375  tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
376 
377  /* Fill the Time structure fields with the read parameters */
378  sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
379  sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
380  sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
381  sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
382  sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
383 
384  /* Fill the Date structure fields with the read parameters */
385  sTimeStampDate->Year = 0U;
386  sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
387  sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
388  sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
389 
390  /* Check the input parameters format */
391  if (Format == RTC_FORMAT_BIN)
392  {
393  /* Convert the Timestamp structure parameters to Binary format */
394  sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
395  sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
396  sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
397 
398  /* Convert the DateTimeStamp structure parameters to Binary format */
399  sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
400  sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
401  sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
402  }
403 
404  /* Clear the Timestamp Flag */
405  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
406 
407  return HAL_OK;
408 }
409 
418 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
419 {
420  uint32_t tmpreg = 0U;
421 
422  /* Check the parameters */
423  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
424  assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
425  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
426  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
427  assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
428  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
429  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
430  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
431  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
432 
433  /* Process Locked */
434  __HAL_LOCK(hrtc);
435 
436  hrtc->State = HAL_RTC_STATE_BUSY;
437 
438  /* Copy control register into temporary variable */
439  tmpreg = hrtc->Instance->TAFCR;
440 
441  /* Enable selected tamper */
442  tmpreg |= (sTamper->Tamper);
443 
444  /* Configure the tamper trigger bit (this bit is just on the right of the
445  tamper enable bit, hence the one-time right shift before updating it) */
446  if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
447  {
448  /* Set the tamper trigger bit (case of falling edge or high level) */
449  tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
450  }
451  else
452  {
453  /* Clear the tamper trigger bit (case of rising edge or low level) */
454  tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
455  }
456 
457  /* Clear remaining fields before setting them */
458  tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
459  RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
460  RTC_TAMPERPRECHARGEDURATION_MASK | \
461  RTC_TAMPER_PULLUP_MASK | \
462  RTC_TAFCR_TAMP1INSEL | \
463  RTC_TIMESTAMPONTAMPERDETECTION_MASK);
464 
465  /* Set remaining parameters of desired configuration into temporary variable */
466  tmpreg |= ((uint32_t)sTamper->Filter | \
467  (uint32_t)sTamper->SamplingFrequency | \
468  (uint32_t)sTamper->PrechargeDuration | \
469  (uint32_t)sTamper->TamperPullUp | \
470  (uint32_t)sTamper->PinSelection | \
471  (uint32_t)sTamper->TimeStampOnTamperDetection);
472 
473  /* Disable tamper global interrupt in case it is enabled */
474  tmpreg &= (uint32_t)~RTC_TAFCR_TAMPIE;
475 
476  /* Copy desired configuration into configuration register */
477  hrtc->Instance->TAFCR = tmpreg;
478 
479  hrtc->State = HAL_RTC_STATE_READY;
480 
481  /* Process Unlocked */
482  __HAL_UNLOCK(hrtc);
483 
484  return HAL_OK;
485 }
486 
496 {
497  uint32_t tmpreg = 0U;
498 
499  /* Check the parameters */
500  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
501  assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
502  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
503  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
504  assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
505  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
506  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
507  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
508  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
509 
510  /* Process Locked */
511  __HAL_LOCK(hrtc);
512 
513  hrtc->State = HAL_RTC_STATE_BUSY;
514 
515  /* Copy control register into temporary variable */
516  tmpreg = hrtc->Instance->TAFCR;
517 
518  /* Enable selected tamper */
519  tmpreg |= (sTamper->Tamper);
520 
521  /* Configure the tamper trigger bit (this bit is just on the right of the
522  tamper enable bit, hence the one-time right shift before updating it) */
523  if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
524  {
525  /* Set the tamper trigger bit (case of falling edge or high level) */
526  tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
527  }
528  else
529  {
530  /* Clear the tamper trigger bit (case of rising edge or low level) */
531  tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
532  }
533 
534  /* Clear remaining fields before setting them */
535  tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
536  RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
537  RTC_TAMPERPRECHARGEDURATION_MASK | \
538  RTC_TAMPER_PULLUP_MASK | \
539  RTC_TAFCR_TAMP1INSEL | \
540  RTC_TIMESTAMPONTAMPERDETECTION_MASK);
541 
542  /* Set remaining parameters of desired configuration into temporary variable */
543  tmpreg |= ((uint32_t)sTamper->Filter | \
544  (uint32_t)sTamper->SamplingFrequency | \
545  (uint32_t)sTamper->PrechargeDuration | \
546  (uint32_t)sTamper->TamperPullUp | \
547  (uint32_t)sTamper->PinSelection | \
548  (uint32_t)sTamper->TimeStampOnTamperDetection);
549 
550  /* Enable global tamper interrupt */
551  tmpreg |= (uint32_t)RTC_TAFCR_TAMPIE;
552 
553  /* Copy desired configuration into configuration register */
554  hrtc->Instance->TAFCR = tmpreg;
555 
556  /* RTC Tamper Interrupt Configuration: EXTI configuration */
557  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
558  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
559 
560  hrtc->State = HAL_RTC_STATE_READY;
561 
562  /* Process Unlocked */
563  __HAL_UNLOCK(hrtc);
564 
565  return HAL_OK;
566 }
567 
580 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
581 {
582  assert_param(IS_RTC_TAMPER(Tamper));
583 
584  /* Process Locked */
585  __HAL_LOCK(hrtc);
586 
587  hrtc->State = HAL_RTC_STATE_BUSY;
588 
589  /* Disable the selected Tamper pin */
590  hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
591 
592  hrtc->State = HAL_RTC_STATE_READY;
593 
594  /* Process Unlocked */
595  __HAL_UNLOCK(hrtc);
596 
597  return HAL_OK;
598 }
599 
607 {
608  /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
609  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
610 
611  /* Get the Timestamp interrupt source enable status */
612  if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
613  {
614  /* Get the pending status of the Timestamp Interrupt */
615  if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
616  {
617  /* Timestamp callback */
618 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
619  hrtc->TimeStampEventCallback(hrtc);
620 #else
622 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
623 
624  /* Clear the Timestamp interrupt pending bit after returning from callback
625  as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
626  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
627  }
628  }
629 
630  /* Get the Tamper 1 interrupt source enable status */
631  if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
632  {
633  /* Get the pending status of the Tamper 1 Interrupt */
634  if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
635  {
636  /* Clear the Tamper interrupt pending bit */
637  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
638 
639  /* Tamper callback */
640 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
641  hrtc->Tamper1EventCallback(hrtc);
642 #else
644 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
645  }
646  }
647 
648 #if defined(RTC_TAMPER2_SUPPORT)
649  /* Get the Tamper 2 interrupt source enable status */
650  if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
651  {
652  /* Get the pending status of the Tamper 2 Interrupt */
653  if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
654  {
655  /* Clear the Tamper interrupt pending bit */
656  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
657 
658  /* Tamper callback */
659 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
660  hrtc->Tamper2EventCallback(hrtc);
661 #else
663 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
664  }
665  }
666 #endif /* RTC_TAMPER2_SUPPORT */
667 
668  /* Change RTC state */
669  hrtc->State = HAL_RTC_STATE_READY;
670 }
671 
679 {
680  /* Prevent unused argument(s) compilation warning */
681  UNUSED(hrtc);
682 
683  /* NOTE: This function should not be modified, when the callback is needed,
684  the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
685  */
686 }
687 
695 {
696  /* Prevent unused argument(s) compilation warning */
697  UNUSED(hrtc);
698 
699  /* NOTE: This function should not be modified, when the callback is needed,
700  the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
701  */
702 }
703 
704 #if defined(RTC_TAMPER2_SUPPORT)
712 {
713  /* Prevent unused argument(s) compilation warning */
714  UNUSED(hrtc);
715 
716  /* NOTE: This function should not be modified, when the callback is needed,
717  the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
718  */
719 }
720 #endif /* RTC_TAMPER2_SUPPORT */
721 
729 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
730 {
731  uint32_t tickstart = 0U;
732 
733  /* Get tick */
734  tickstart = HAL_GetTick();
735 
736  while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
737  {
738  if (Timeout != HAL_MAX_DELAY)
739  {
740  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
741  {
742  hrtc->State = HAL_RTC_STATE_TIMEOUT;
743  return HAL_TIMEOUT;
744  }
745  }
746 
747  if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
748  {
749  /* Clear the Timestamp Overrun Flag */
750  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
751 
752  /* Change Timestamp state */
753  hrtc->State = HAL_RTC_STATE_ERROR;
754 
755  return HAL_ERROR;
756  }
757  }
758 
759  /* Change RTC state */
760  hrtc->State = HAL_RTC_STATE_READY;
761 
762  return HAL_OK;
763 }
764 
772 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
773 {
774  uint32_t tickstart = 0U;
775 
776  /* Get tick */
777  tickstart = HAL_GetTick();
778 
779  /* Get the status of the Interrupt */
780  while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
781  {
782  if (Timeout != HAL_MAX_DELAY)
783  {
784  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
785  {
786  hrtc->State = HAL_RTC_STATE_TIMEOUT;
787  return HAL_TIMEOUT;
788  }
789  }
790  }
791 
792  /* Clear the Tamper Flag */
793  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
794 
795  /* Change RTC state */
796  hrtc->State = HAL_RTC_STATE_READY;
797 
798  return HAL_OK;
799 }
800 
801 #if defined(RTC_TAMPER2_SUPPORT)
809 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
810 {
811  uint32_t tickstart = 0U;
812 
813  /* Get tick */
814  tickstart = HAL_GetTick();
815 
816  /* Get the status of the Interrupt */
817  while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
818  {
819  if (Timeout != HAL_MAX_DELAY)
820  {
821  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
822  {
823  hrtc->State = HAL_RTC_STATE_TIMEOUT;
824  return HAL_TIMEOUT;
825  }
826  }
827  }
828 
829  /* Clear the Tamper Flag */
830  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
831 
832  /* Change RTC state */
833  hrtc->State = HAL_RTC_STATE_READY;
834 
835  return HAL_OK;
836 }
837 #endif /* RTC_TAMPER2_SUPPORT */
838 
865 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
866 {
867  uint32_t tickstart = 0U;
868 
869  /* Check the parameters */
870  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
871  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
872 
873  /* Process Locked */
874  __HAL_LOCK(hrtc);
875 
876  hrtc->State = HAL_RTC_STATE_BUSY;
877 
878  /* Disable the write protection for RTC registers */
879  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
880 
881  /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
882  if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
883  {
884  tickstart = HAL_GetTick();
885 
886  /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
887  while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
888  {
889  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
890  {
891  /* Enable the write protection for RTC registers */
892  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
893 
894  hrtc->State = HAL_RTC_STATE_TIMEOUT;
895 
896  /* Process Unlocked */
897  __HAL_UNLOCK(hrtc);
898 
899  return HAL_TIMEOUT;
900  }
901  }
902  }
903 
904  /* Disable the Wakeup timer */
905  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
906 
907  /* Clear the Wakeup flag */
908  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
909 
910  /* Get tick */
911  tickstart = HAL_GetTick();
912 
913  /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
914  while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
915  {
916  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
917  {
918  /* Enable the write protection for RTC registers */
919  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
920 
921  hrtc->State = HAL_RTC_STATE_TIMEOUT;
922 
923  /* Process Unlocked */
924  __HAL_UNLOCK(hrtc);
925 
926  return HAL_TIMEOUT;
927  }
928  }
929 
930  /* Clear the Wakeup Timer clock source bits in CR register */
931  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
932 
933  /* Configure the clock source */
934  hrtc->Instance->CR |= (uint32_t)WakeUpClock;
935 
936  /* Configure the Wakeup Timer counter */
937  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
938 
939  /* Enable the Wakeup Timer */
940  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
941 
942  /* Enable the write protection for RTC registers */
943  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
944 
945  hrtc->State = HAL_RTC_STATE_READY;
946 
947  /* Process Unlocked */
948  __HAL_UNLOCK(hrtc);
949 
950  return HAL_OK;
951 }
952 
961 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
962 {
963  __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
964 
965  /* Check the parameters */
966  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
967  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
968 
969  /* Process Locked */
970  __HAL_LOCK(hrtc);
971 
972  hrtc->State = HAL_RTC_STATE_BUSY;
973 
974  /* Disable the write protection for RTC registers */
975  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
976 
977  /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
978  if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
979  {
980  /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
981  do
982  {
983  count = count - 1U;
984  if (count == 0U)
985  {
986  /* Enable the write protection for RTC registers */
987  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
988 
989  hrtc->State = HAL_RTC_STATE_TIMEOUT;
990 
991  /* Process Unlocked */
992  __HAL_UNLOCK(hrtc);
993 
994  return HAL_TIMEOUT;
995  }
996  } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
997  }
998 
999  /* Disable the Wakeup timer */
1000  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1001 
1002  /* Clear the Wakeup flag */
1003  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1004 
1005  /* Reload the counter */
1006  count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1007 
1008  /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1009  do
1010  {
1011  count = count - 1U;
1012  if (count == 0U)
1013  {
1014  /* Enable the write protection for RTC registers */
1015  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1016 
1017  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1018 
1019  /* Process Unlocked */
1020  __HAL_UNLOCK(hrtc);
1021 
1022  return HAL_TIMEOUT;
1023  }
1024  } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
1025 
1026  /* Clear the Wakeup Timer clock source bits in CR register */
1027  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1028 
1029  /* Configure the clock source */
1030  hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1031 
1032  /* Configure the Wakeup Timer counter */
1033  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1034 
1035  /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
1036  __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1037  __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1038 
1039  /* Configure the interrupt in the RTC_CR register */
1040  __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
1041 
1042  /* Enable the Wakeup Timer */
1043  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1044 
1045  /* Enable the write protection for RTC registers */
1046  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1047 
1048  hrtc->State = HAL_RTC_STATE_READY;
1049 
1050  /* Process Unlocked */
1051  __HAL_UNLOCK(hrtc);
1052 
1053  return HAL_OK;
1054 }
1055 
1063 {
1064  uint32_t tickstart = 0U;
1065 
1066  /* Process Locked */
1067  __HAL_LOCK(hrtc);
1068 
1069  hrtc->State = HAL_RTC_STATE_BUSY;
1070 
1071  /* Disable the write protection for RTC registers */
1072  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1073 
1074  /* Disable the Wakeup Timer */
1075  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1076 
1077  /* In case of interrupt mode is used, the interrupt source must disabled */
1078  __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
1079 
1080  /* Get tick */
1081  tickstart = HAL_GetTick();
1082 
1083  /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1084  while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1085  {
1086  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1087  {
1088  /* Enable the write protection for RTC registers */
1089  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1090 
1091  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1092 
1093  /* Process Unlocked */
1094  __HAL_UNLOCK(hrtc);
1095 
1096  return HAL_TIMEOUT;
1097  }
1098  }
1099 
1100  /* Enable the write protection for RTC registers */
1101  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1102 
1103  hrtc->State = HAL_RTC_STATE_READY;
1104 
1105  /* Process Unlocked */
1106  __HAL_UNLOCK(hrtc);
1107 
1108  return HAL_OK;
1109 }
1110 
1118 {
1119  /* Get the counter value */
1120  return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1121 }
1122 
1135 {
1136  /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1137  __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1138 
1139  /* Get the pending status of the Wakeup timer Interrupt */
1140  if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1141  {
1142  /* Clear the Wakeup timer interrupt pending bit */
1143  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1144 
1145  /* Wakeup timer callback */
1146 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1147  hrtc->WakeUpTimerEventCallback(hrtc);
1148 #else
1150 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1151  }
1152 
1153  /* Change RTC state */
1154  hrtc->State = HAL_RTC_STATE_READY;
1155 }
1156 
1164 {
1165  /* Prevent unused argument(s) compilation warning */
1166  UNUSED(hrtc);
1167 
1168  /* NOTE: This function should not be modified, when the callback is needed,
1169  the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1170  */
1171 }
1172 
1180 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1181 {
1182  uint32_t tickstart = 0U;
1183 
1184  /* Get tick */
1185  tickstart = HAL_GetTick();
1186 
1187  while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1188  {
1189  if (Timeout != HAL_MAX_DELAY)
1190  {
1191  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1192  {
1193  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1194  return HAL_TIMEOUT;
1195  }
1196  }
1197  }
1198 
1199  /* Clear the Wakeup timer Flag */
1200  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1201 
1202  /* Change RTC state */
1203  hrtc->State = HAL_RTC_STATE_READY;
1204 
1205  return HAL_OK;
1206 }
1207 
1248 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1249 {
1250  uint32_t tmp = 0U;
1251 
1252  /* Check the parameters */
1253  assert_param(IS_RTC_BKP(BackupRegister));
1254 
1255  tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1256  tmp += (BackupRegister * 4U);
1257 
1258  /* Write the specified register */
1259  *(__IO uint32_t *)tmp = (uint32_t)Data;
1260 }
1261 
1271 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1272 {
1273  uint32_t tmp = 0U;
1274 
1275  /* Check the parameters */
1276  assert_param(IS_RTC_BKP(BackupRegister));
1277 
1278  tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1279  tmp += (BackupRegister * 4U);
1280 
1281  /* Read the specified register */
1282  return (*(__IO uint32_t *)tmp);
1283 }
1284 
1302 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef *hrtc, uint32_t CalibSign, uint32_t Value)
1303 {
1304  HAL_StatusTypeDef status;
1305 
1306  /* Check the parameters */
1307  assert_param(IS_RTC_CALIB_SIGN(CalibSign));
1308  assert_param(IS_RTC_CALIB_VALUE(Value));
1309 
1310  /* Process Locked */
1311  __HAL_LOCK(hrtc);
1312 
1313  hrtc->State = HAL_RTC_STATE_BUSY;
1314 
1315  /* Disable the write protection for RTC registers */
1316  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1317 
1318  /* Enter Initialization mode */
1319  status = RTC_EnterInitMode(hrtc);
1320 
1321  if (status == HAL_OK)
1322  {
1323  /* Enable the Coarse Calibration */
1324  __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
1325 
1326  /* Set the coarse calibration value */
1327  hrtc->Instance->CALIBR = (uint32_t)(CalibSign | Value);
1328 
1329  /* Exit Initialization mode */
1330  status = RTC_ExitInitMode(hrtc);
1331  }
1332 
1333  if (status == HAL_OK)
1334  {
1335  hrtc->State = HAL_RTC_STATE_READY;
1336  }
1337 
1338  /* Enable the write protection for RTC registers */
1339  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1340 
1341  /* Process Unlocked */
1342  __HAL_UNLOCK(hrtc);
1343 
1344  return status;
1345 }
1346 
1354 {
1355  HAL_StatusTypeDef status;
1356 
1357  /* Process Locked */
1358  __HAL_LOCK(hrtc);
1359 
1360  hrtc->State = HAL_RTC_STATE_BUSY;
1361 
1362  /* Disable the write protection for RTC registers */
1363  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1364 
1365  /* Enter Initialization mode */
1366  status = RTC_EnterInitMode(hrtc);
1367 
1368  if (status == HAL_OK)
1369  {
1370  /* Disable the Coarse Calibration */
1371  __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
1372 
1373  /* Exit Initialization mode */
1374  status = RTC_ExitInitMode(hrtc);
1375  }
1376 
1377  if (status == HAL_OK)
1378  {
1379  hrtc->State = HAL_RTC_STATE_READY;
1380  }
1381 
1382  /* Enable the write protection for RTC registers */
1383  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1384 
1385  /* Process Unlocked */
1386  __HAL_UNLOCK(hrtc);
1387 
1388  return status;
1389 }
1390 
1411 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1412 {
1413  uint32_t tickstart = 0U;
1414 
1415  /* Check the parameters */
1416  assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1417  assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1418  assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1419 
1420  /* Process Locked */
1421  __HAL_LOCK(hrtc);
1422 
1423  hrtc->State = HAL_RTC_STATE_BUSY;
1424 
1425  /* Disable the write protection for RTC registers */
1426  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1427 
1428  /* check if a calibration is pending*/
1429  if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1430  {
1431  /* Get tick */
1432  tickstart = HAL_GetTick();
1433 
1434  /* check if a calibration is pending*/
1435  while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1436  {
1437  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1438  {
1439  /* Enable the write protection for RTC registers */
1440  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1441 
1442  /* Change RTC state */
1443  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1444 
1445  /* Process Unlocked */
1446  __HAL_UNLOCK(hrtc);
1447 
1448  return HAL_TIMEOUT;
1449  }
1450  }
1451  }
1452 
1453  /* Configure the Smooth calibration settings */
1454  hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | \
1455  (uint32_t)SmoothCalibPlusPulses | \
1456  (uint32_t)SmoothCalibMinusPulsesValue);
1457 
1458  /* Enable the write protection for RTC registers */
1459  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1460 
1461  /* Change RTC state */
1462  hrtc->State = HAL_RTC_STATE_READY;
1463 
1464  /* Process Unlocked */
1465  __HAL_UNLOCK(hrtc);
1466 
1467  return HAL_OK;
1468 }
1469 
1483 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1484 {
1485  uint32_t tickstart = 0U;
1486 
1487  /* Check the parameters */
1488  assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1489  assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1490 
1491  /* Process Locked */
1492  __HAL_LOCK(hrtc);
1493 
1494  hrtc->State = HAL_RTC_STATE_BUSY;
1495 
1496  /* Disable the write protection for RTC registers */
1497  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1498 
1499  /* Get tick */
1500  tickstart = HAL_GetTick();
1501 
1502  /* Wait until the shift is completed */
1503  while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1504  {
1505  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1506  {
1507  /* Enable the write protection for RTC registers */
1508  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1509 
1510  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1511 
1512  /* Process Unlocked */
1513  __HAL_UNLOCK(hrtc);
1514 
1515  return HAL_TIMEOUT;
1516  }
1517  }
1518 
1519  /* Check if the reference clock detection is disabled */
1520  if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1521  {
1522  /* Configure the Shift settings */
1523  hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1524 
1525  /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1526  if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1527  {
1528  if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1529  {
1530  /* Enable the write protection for RTC registers */
1531  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1532 
1533  hrtc->State = HAL_RTC_STATE_ERROR;
1534 
1535  /* Process Unlocked */
1536  __HAL_UNLOCK(hrtc);
1537 
1538  return HAL_ERROR;
1539  }
1540  }
1541  }
1542  else
1543  {
1544  /* Enable the write protection for RTC registers */
1545  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1546 
1547  /* Change RTC state */
1548  hrtc->State = HAL_RTC_STATE_ERROR;
1549 
1550  /* Process Unlocked */
1551  __HAL_UNLOCK(hrtc);
1552 
1553  return HAL_ERROR;
1554  }
1555 
1556  /* Enable the write protection for RTC registers */
1557  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1558 
1559  /* Change RTC state */
1560  hrtc->State = HAL_RTC_STATE_READY;
1561 
1562  /* Process Unlocked */
1563  __HAL_UNLOCK(hrtc);
1564 
1565  return HAL_OK;
1566 }
1567 
1578 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1579 {
1580  /* Check the parameters */
1581  assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1582 
1583  /* Process Locked */
1584  __HAL_LOCK(hrtc);
1585 
1586  hrtc->State = HAL_RTC_STATE_BUSY;
1587 
1588  /* Disable the write protection for RTC registers */
1589  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1590 
1591  /* Clear flags before config */
1592  hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1593 
1594  /* Configure the RTC_CR register */
1595  hrtc->Instance->CR |= (uint32_t)CalibOutput;
1596 
1597  __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1598 
1599  /* Enable the write protection for RTC registers */
1600  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1601 
1602  /* Change RTC state */
1603  hrtc->State = HAL_RTC_STATE_READY;
1604 
1605  /* Process Unlocked */
1606  __HAL_UNLOCK(hrtc);
1607 
1608  return HAL_OK;
1609 }
1610 
1618 {
1619  /* Process Locked */
1620  __HAL_LOCK(hrtc);
1621 
1622  hrtc->State = HAL_RTC_STATE_BUSY;
1623 
1624  /* Disable the write protection for RTC registers */
1625  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1626 
1627  __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1628 
1629  /* Enable the write protection for RTC registers */
1630  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1631 
1632  /* Change RTC state */
1633  hrtc->State = HAL_RTC_STATE_READY;
1634 
1635  /* Process Unlocked */
1636  __HAL_UNLOCK(hrtc);
1637 
1638  return HAL_OK;
1639 }
1640 
1648 {
1649  HAL_StatusTypeDef status;
1650 
1651  /* Process Locked */
1652  __HAL_LOCK(hrtc);
1653 
1654  hrtc->State = HAL_RTC_STATE_BUSY;
1655 
1656  /* Disable the write protection for RTC registers */
1657  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1658 
1659  /* Enter Initialization mode */
1660  status = RTC_EnterInitMode(hrtc);
1661 
1662  if (status == HAL_OK)
1663  {
1664  /* Enable the reference clock detection */
1665  __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1666 
1667  /* Exit Initialization mode */
1668  status = RTC_ExitInitMode(hrtc);
1669  }
1670 
1671  if (status == HAL_OK)
1672  {
1673  hrtc->State = HAL_RTC_STATE_READY;
1674  }
1675 
1676  /* Enable the write protection for RTC registers */
1677  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1678 
1679  /* Process Unlocked */
1680  __HAL_UNLOCK(hrtc);
1681 
1682  return status;
1683 }
1684 
1692 {
1693  HAL_StatusTypeDef status;
1694 
1695  /* Process Locked */
1696  __HAL_LOCK(hrtc);
1697 
1698  hrtc->State = HAL_RTC_STATE_BUSY;
1699 
1700  /* Disable the write protection for RTC registers */
1701  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1702 
1703  /* Enter Initialization mode */
1704  status = RTC_EnterInitMode(hrtc);
1705 
1706  if (status == HAL_OK)
1707  {
1708  /* Disable the reference clock detection */
1709  __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1710 
1711  /* Exit Initialization mode */
1712  status = RTC_ExitInitMode(hrtc);
1713  }
1714 
1715  if (status == HAL_OK)
1716  {
1717  hrtc->State = HAL_RTC_STATE_READY;
1718  }
1719 
1720  /* Enable the write protection for RTC registers */
1721  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1722 
1723  /* Process Unlocked */
1724  __HAL_UNLOCK(hrtc);
1725 
1726  return status;
1727 }
1728 
1738 {
1739  /* Process Locked */
1740  __HAL_LOCK(hrtc);
1741 
1742  hrtc->State = HAL_RTC_STATE_BUSY;
1743 
1744  /* Disable the write protection for RTC registers */
1745  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1746 
1747  /* Set the BYPSHAD bit */
1748  hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1749 
1750  /* Enable the write protection for RTC registers */
1751  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1752 
1753  /* Change RTC state */
1754  hrtc->State = HAL_RTC_STATE_READY;
1755 
1756  /* Process Unlocked */
1757  __HAL_UNLOCK(hrtc);
1758 
1759  return HAL_OK;
1760 }
1761 
1771 {
1772  /* Process Locked */
1773  __HAL_LOCK(hrtc);
1774 
1775  hrtc->State = HAL_RTC_STATE_BUSY;
1776 
1777  /* Disable the write protection for RTC registers */
1778  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1779 
1780  /* Reset the BYPSHAD bit */
1781  hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1782 
1783  /* Enable the write protection for RTC registers */
1784  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1785 
1786  /* Change RTC state */
1787  hrtc->State = HAL_RTC_STATE_READY;
1788 
1789  /* Process Unlocked */
1790  __HAL_UNLOCK(hrtc);
1791 
1792  return HAL_OK;
1793 }
1794 
1821 {
1822  /* Prevent unused argument(s) compilation warning */
1823  UNUSED(hrtc);
1824 
1825  /* NOTE: This function should not be modified, when the callback is needed,
1826  the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1827  */
1828 }
1829 
1837 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1838 {
1839  uint32_t tickstart = 0U;
1840 
1841  /* Get tick */
1842  tickstart = HAL_GetTick();
1843 
1844  /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
1845  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
1846  {
1847  if (Timeout != HAL_MAX_DELAY)
1848  {
1849  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1850  {
1851  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1852  return HAL_TIMEOUT;
1853  }
1854  }
1855  }
1856 
1857  /* Clear the Alarm flag */
1858  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1859 
1860  /* Change RTC state */
1861  hrtc->State = HAL_RTC_STATE_READY;
1862 
1863  return HAL_OK;
1864 }
1865 
1874 #endif /* HAL_RTC_MODULE_ENABLED */
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
Timestamp callback.
HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
Deactivates Timestamp.
void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
Tamper 1 callback.
HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
Sets Tamper with interrupt.
HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
Sets Tamper.
HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
Gets the RTC Timestamp value.
void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
Handles Timestamp and Tamper interrupt request.
HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handles Tamper 2 Polling.
HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
Sets Timestamp.
HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
Sets Timestamp with Interrupt.
HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handles Tamper 1 Polling.
HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
Deactivates Tamper.
void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
Tamper 2 callback.
HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handles Timestamp polling request.
HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handles Wakeup Timer Polling.
HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
Deactivates wakeup timer counter.
void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
Handles Wakeup Timer interrupt request.
HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
Sets wakeup timer with interrupt.
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
Wakeup Timer callback.
HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
Sets wakeup timer.
uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
Gets wakeup timer counter.
HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
Configures the Synchronization Shift Control Settings.
HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef *hrtc, uint32_t CalibSign, uint32_t Value)
Sets the Coarse calibration parameters.
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
Reads data from the specified RTC Backup data Register.
HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
Sets the Smooth calibration parameters.
HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
Enables the Bypass Shadow feature.
HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef *hrtc)
Deactivates the Coarse calibration parameters.
HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
Disable the RTC reference clock detection.
HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
Disables the Bypass Shadow feature.
HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
Enables the RTC reference clock detection.
void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
Writes a data in a specified RTC Backup data register.
HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
Alarm B callback.
HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handles Alarm B Polling request.
uint32_t TimeStampOnTamperDetection
RTC Tamper structure definition.
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock.
struct __RTC_HandleTypeDef else typedef struct endif RTC_HandleTypeDef
RTC Handle Structure definition.
@ HAL_RTC_STATE_TIMEOUT
@ HAL_RTC_STATE_ERROR
@ HAL_RTC_STATE_READY
@ HAL_RTC_STATE_BUSY
RTC Date structure definition.
RTC Time structure definition.
HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
Exits the RTC Initialization mode.
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
Enters the RTC Initialization mode.
uint8_t RTC_Bcd2ToByte(uint8_t number)
Converts a 2-digit number from BCD to decimal format.
This file contains all the functions prototypes for the HAL module driver.