stm32f7xx_hal_rtc_ex.c 56.8 KB
Newer Older
1 2 3 4 5
/**
  ******************************************************************************
  * @file    stm32f7xx_hal_rtc_ex.c
  * @author  MCD Application Team
  * @brief   RTC HAL module driver.
6
  *          This file provides firmware functions to manage the following
7 8
  *          functionalities of the Real Time Clock (RTC) Extension peripheral:
  *           + RTC Time Stamp functions
9
  *           + RTC Tamper functions
10 11
  *           + RTC Wake-up functions
  *           + Extension Control functions
12 13
  *           + Extension RTC features functions
  *
14 15 16 17
  @verbatim
  ==============================================================================
                  ##### How to use this driver #####
  ==============================================================================
18
  [..]
19
    (+) Enable the RTC domain access.
20
    (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
21
        format using the HAL_RTC_Init() function.
22

23 24
  *** RTC Wakeup configuration ***
  ================================
25
  [..]
26
    (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
27
        function. You can also configure the RTC Wakeup timer in interrupt mode
28
        using the HAL_RTC_SetWakeUpTimer_IT() function.
29
    (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
30
        function.
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  *** TimeStamp configuration ***
  ===============================
  [..]
    (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.
        You can also configure the RTC TimeStamp with interrupt mode using the
        HAL_RTC_SetTimeStamp_IT() function.
    (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
        function.

  *** Internal TimeStamp configuration ***
  ===============================
  [..]
    (+) Enables the RTC internal TimeStamp using the HAL_RTC_SetInternalTimeStamp() function.
    (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
        function.
47

48 49 50
  *** Tamper configuration ***
  ============================
  [..]
51 52
    (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
        or Level according to the Tamper filter (if equal to 0 Edge else Level)
53 54 55 56 57
        value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
        Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper
        with interrupt mode using HAL_RTC_SetTamper_IT() function.
    (+) The default configuration of the Tamper erases the backup registers. To avoid
        erase, enable the NoErase field on the RTC_TAMPCR register.
58

59 60 61 62
  *** Backup Data Registers configuration ***
  ===========================================
  [..]
    (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
63
        function.
64 65
    (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
        function.
66

67 68 69 70
   @endverbatim
  ******************************************************************************
  * @attention
  *
71 72
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.</center></h2>
73
  *
74 75 76 77
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
78 79
  *
  ******************************************************************************
80
  */
81 82 83 84 85 86 87 88

/* Includes ------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"

/** @addtogroup STM32F7xx_HAL_Driver
  * @{
  */

89
/** @defgroup RTCEx RTCEx
90 91 92 93 94 95 96 97 98 99 100
  * @brief RTC Extended HAL module driver
  * @{
  */

#ifdef HAL_RTC_MODULE_ENABLED

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
101
/* Exported functions --------------------------------------------------------*/
102 103 104 105

/** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  * @{
  */
106

107 108 109 110

/** @defgroup RTCEx_Group1 RTC TimeStamp and Tamper functions
 *  @brief   RTC TimeStamp and Tamper functions
 *
111
@verbatim
112 113
 ===============================================================================
                 ##### RTC TimeStamp and Tamper functions #####
114 115
 ===============================================================================

116 117 118 119 120 121 122 123
 [..] This section provides functions allowing to configure TimeStamp feature

@endverbatim
  * @{
  */

/**
  * @brief  Sets TimeStamp.
124
  * @note   This API must be called before enabling the TimeStamp feature.
125 126
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
127
  * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
128 129
  *         activated.
  *          This parameter can be one of the following values:
130
  *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
131
  *                                        rising edge of the related pin.
132
  *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
133 134 135 136
  *                                         falling edge of the related pin.
  * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
  *          This parameter can be one of the following values:
  *             @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
137 138
  *             @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
  *             @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.
139 140 141 142 143
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
{
  uint32_t tmpreg = 0;
144

145 146 147
  /* Check the parameters */
  assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
148 149

  /* Process Locked */
150
  __HAL_LOCK(hrtc);
151

152
  hrtc->State = HAL_RTC_STATE_BUSY;
153

154 155
  /* Get the RTC_CR register and clear the bits to be configured */
  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
156

157
  tmpreg|= TimeStampEdge;
158

159 160
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
161

162
  hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
163 164
  hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);

165 166
  /* Configure the Time Stamp TSEDGE and Enable bits */
  hrtc->Instance->CR = (uint32_t)tmpreg;
167

168
  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
169

170
  /* Enable the write protection for RTC registers */
171 172
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

173
  /* Change RTC state */
174 175 176
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
177
  __HAL_UNLOCK(hrtc);
178

179 180 181 182
  return HAL_OK;
}

/**
183
  * @brief  Sets TimeStamp with Interrupt.
184 185 186
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @note   This API must be called before enabling the TimeStamp feature.
187
  * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
188 189
  *         activated.
  *          This parameter can be one of the following values:
190
  *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
191
  *                                        rising edge of the related pin.
192
  *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
193 194 195 196
  *                                         falling edge of the related pin.
  * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
  *          This parameter can be one of the following values:
  *             @arg RTC_TIMESTAMPPIN_PC13: PC13 is selected as RTC TimeStamp Pin.
197 198
  *             @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin.
  *             @arg RTC_TIMESTAMPPIN_PC1: PC1 is selected as RTC TimeStamp Pin.
199 200 201 202 203
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
{
  uint32_t tmpreg = 0;
204

205 206 207
  /* Check the parameters */
  assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
208 209

  /* Process Locked */
210
  __HAL_LOCK(hrtc);
211

212
  hrtc->State = HAL_RTC_STATE_BUSY;
213

214 215
  /* Get the RTC_CR register and clear the bits to be configured */
  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
216

217
  tmpreg |= TimeStampEdge;
218

219 220
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
221

222 223
  /* Configure the Time Stamp TSEDGE and Enable bits */
  hrtc->Instance->CR = (uint32_t)tmpreg;
224

225 226
  hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
  hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);
227

228 229
  /* Clear RTC Timestamp flag */
  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
230

231
  __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
232 233

  /* Enable IT timestamp */
234
  __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
235

236 237
  /* RTC timestamp Interrupt Configuration: EXTI configuration */
  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
238

239
  EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
240

241
  /* Enable the write protection for RTC registers */
242 243 244 245 246
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
247
  __HAL_UNLOCK(hrtc);
248

249 250 251 252
  return HAL_OK;
}

/**
253
  * @brief  Deactivates TimeStamp.
254 255 256 257 258 259 260
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
{
  uint32_t tmpreg = 0U;
261 262

  /* Process Locked */
263
  __HAL_LOCK(hrtc);
264

265
  hrtc->State = HAL_RTC_STATE_BUSY;
266

267 268
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
269 270

  /* In case of interrupt mode is used, the interrupt source must disabled */
271
  __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
272

273 274
  /* Get the RTC_CR register and clear the bits to be configured */
  tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
275

276 277
  /* Configure the Time Stamp TSEDGE and Enable bits */
  hrtc->Instance->CR = (uint32_t)tmpreg;
278

279 280
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
281 282 283 284

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
285
  __HAL_UNLOCK(hrtc);
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  return HAL_OK;
}

/**
  * @brief  Sets Internal TimeStamp.
  * @note   This API must be called before enabling the internal TimeStamp feature.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
{
  /* Process Locked */
  __HAL_LOCK(hrtc);

  hrtc->State = HAL_RTC_STATE_BUSY;

  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);

  /* Configure the internal Time Stamp Enable bits */
  __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);

  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;

316
  /* Process Unlocked */
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  __HAL_UNLOCK(hrtc);

  return HAL_OK;
}

/**
  * @brief  Deactivates internal TimeStamp.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
{
  /* Process Locked */
  __HAL_LOCK(hrtc);

  hrtc->State = HAL_RTC_STATE_BUSY;

  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);

  /* Configure the internal Time Stamp Enable bits */
  __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);

  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
  __HAL_UNLOCK(hrtc);

  return HAL_OK;
}

/**
  * @brief  Gets the RTC TimeStamp value.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  sTimeStamp Pointer to Time structure
357
  * @param  sTimeStampDate Pointer to Date structure
358 359
  * @param  Format specifies the format of the entered parameters.
  *          This parameter can be one of the following values:
360
  *             FORMAT_BIN: Binary data format
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  *             FORMAT_BCD: BCD data format
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
{
  uint32_t tmptime = 0U, tmpdate = 0U;

  /* Check the parameters */
  assert_param(IS_RTC_FORMAT(Format));

  /* Get the TimeStamp time and date registers values */
  tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);

  /* Fill the Time structure fields with the read parameters */
  sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
379
  sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16U);
380
  sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
381

382 383 384 385 386 387 388 389 390 391 392 393 394
  /* Fill the Date structure fields with the read parameters */
  sTimeStampDate->Year = 0U;
  sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13U);

  /* Check the input parameters format */
  if(Format == RTC_FORMAT_BIN)
  {
    /* Convert the TimeStamp structure parameters to Binary format */
    sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
    sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
    sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
395

396 397 398 399 400
    /* Convert the DateTimeStamp structure parameters to Binary format */
    sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
    sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
    sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  }
401

402 403
  /* Clear the TIMESTAMP Flag */
  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
404

405 406 407 408 409
  return HAL_OK;
}

/**
  * @brief  Sets Tamper
410
  * @note   By calling this API we disable the tamper interrupt for all tampers.
411 412 413 414 415 416 417 418
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  sTamper Pointer to Tamper Structure.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
{
  uint32_t tmpreg = 0U;
419

420
  /* Check the parameters */
421
  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
422 423 424 425
  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
426
  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
427 428 429
  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
430 431

  /* Process Locked */
432
  __HAL_LOCK(hrtc);
433

434 435 436
  hrtc->State = HAL_RTC_STATE_BUSY;

  if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
437 438 439 440
  {
    sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
  }

441
  if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
442
  {
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    sTamper->NoErase = 0;
    if((sTamper->Tamper & RTC_TAMPER_1) != 0U)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
    }
    if((sTamper->Tamper & RTC_TAMPER_2) != 0U)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
    }
    if((sTamper->Tamper & RTC_TAMPER_3) != 0U)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
    }
  }

  if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  {
    sTamper->MaskFlag = 0;
    if((sTamper->Tamper & RTC_TAMPER_1) != 0U)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
    }
    if((sTamper->Tamper & RTC_TAMPER_2) != 0U)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
    }
    if((sTamper->Tamper & RTC_TAMPER_3) != 0U)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
    }
  }
474

475 476 477 478 479 480 481 482 483 484 485
  tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\
            (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
            (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);

  hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
                                       (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
                                       (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
                                       (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
                                       (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
                                       (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);

486 487 488
  hrtc->Instance->TAMPCR |= tmpreg;

  hrtc->State = HAL_RTC_STATE_READY;
489

490
  /* Process Unlocked */
491
  __HAL_UNLOCK(hrtc);
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506
  return HAL_OK;
}

/**
  * @brief  Sets Tamper with interrupt.
  * @note   By calling this API we force the tamper interrupt for all tampers.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  sTamper Pointer to RTC Tamper.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
{
  uint32_t tmpreg = 0;
507

508
  /* Check the parameters */
509
  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
510 511 512 513 514
  assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
515
  assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
516 517 518
  assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
519 520

  /* Process Locked */
521
  __HAL_LOCK(hrtc);
522

523
  hrtc->State = HAL_RTC_STATE_BUSY;
524

525 526
  /* Configure the tamper trigger */
  if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
527 528 529 530
  {
    sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  }

531
  if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
532
  {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    sTamper->NoErase = 0;
    if((sTamper->Tamper & RTC_TAMPER_1) != 0)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
    }
    if((sTamper->Tamper & RTC_TAMPER_2) != 0)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
    }
    if((sTamper->Tamper & RTC_TAMPER_3) != 0)
    {
      sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
    }
  }

  if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  {
    sTamper->MaskFlag = 0;
    if((sTamper->Tamper & RTC_TAMPER_1) != 0)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
    }
    if((sTamper->Tamper & RTC_TAMPER_2) != 0)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
    }
    if((sTamper->Tamper & RTC_TAMPER_3) != 0)
    {
      sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
    }
  }
564

565 566 567
  tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\
            (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
            (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
568

569 570 571 572 573 574 575 576
  hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
                                       (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
                                       (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
                                       (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
                                       (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
                                       (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);

  hrtc->Instance->TAMPCR |= tmpreg;
577

578 579 580 581 582 583 584 585
  if(sTamper->Tamper == RTC_TAMPER_1)
  {
    /* Clear RTC Tamper 1 flag */
    __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  }
  else if(sTamper->Tamper == RTC_TAMPER_2)
  {
    /* Clear RTC Tamper 2 flag */
586
    __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
587 588 589 590
  }
  else
  {
    /* Clear RTC Tamper 3 flag */
591
    __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
592 593 594 595 596 597
  }

  /* RTC Tamper Interrupt Configuration: EXTI configuration */
  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();

  EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
598 599 600 601

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
602
  __HAL_UNLOCK(hrtc);
603

604 605 606 607 608 609 610 611 612 613 614 615 616
  return HAL_OK;
}

/**
  * @brief  Deactivates Tamper.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Tamper Selected tamper pin.
  *          This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
{
617 618 619
  assert_param(IS_RTC_TAMPER(Tamper));

  /* Process Locked */
620
  __HAL_LOCK(hrtc);
621

622
  hrtc->State = HAL_RTC_STATE_BUSY;
623

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/* Disable the selected Tamper pin */
  hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;

  if ((Tamper & RTC_TAMPER_1) != 0)
  {
    /* Disable the Tamper1 interrupt */
    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);
  }
  if ((Tamper & RTC_TAMPER_2) != 0)
  {
    /* Disable the Tamper2 interrupt */
    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);
  }
  if ((Tamper & RTC_TAMPER_3) != 0)
  {
    /* Disable the Tamper2 interrupt */
    hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);
641 642 643 644 645
  }

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
646
  __HAL_UNLOCK(hrtc);
647 648

  return HAL_OK;
649 650 651 652 653 654 655 656 657
}

/**
  * @brief  This function handles TimeStamp interrupt request.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
658
{
659 660 661 662 663
  if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
  {
    /* Get the status of the Interrupt */
    if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
    {
664 665 666 667 668 669
      /* TIMESTAMP callback */
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      hrtc->TimeStampEventCallback(hrtc);
#else
      HAL_RTCEx_TimeStampEventCallback(hrtc);
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
670 671 672 673 674 675 676 677 678 679 680 681 682 683

      /* Clear the TIMESTAMP interrupt pending bit */
      __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
    }
  }

  /* Get the status of the Interrupt */
  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== SET)
  {
    /* Get the TAMPER Interrupt enable bit and pending bit */
    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP1IE)) != (uint32_t)RESET))
    {
      /* Tamper callback */
684 685 686
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      hrtc->Tamper1EventCallback(hrtc);
#else
687
      HAL_RTCEx_Tamper1EventCallback(hrtc);
688
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
689 690 691 692 693 694 695 696 697 698 699 700 701 702

      /* Clear the Tamper interrupt pending bit */
      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
    }
  }

  /* Get the status of the Interrupt */
  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F)== SET)
  {
    /* Get the TAMPER Interrupt enable bit and pending bit */
    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP2IE)) != (uint32_t)RESET))
    {
      /* Tamper callback */
703 704 705
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      hrtc->Tamper2EventCallback(hrtc);
#else
706
      HAL_RTCEx_Tamper2EventCallback(hrtc);
707
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
708 709 710 711 712 713 714 715 716 717 718 719 720 721

      /* Clear the Tamper interrupt pending bit */
      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
    }
  }

  /* Get the status of the Interrupt */
  if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F)== SET)
  {
    /* Get the TAMPER Interrupt enable bit and pending bit */
    if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
       (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP3IE)) != (uint32_t)RESET))
    {
      /* Tamper callback */
722 723 724
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      hrtc->Tamper3EventCallback(hrtc);
#else
725
      HAL_RTCEx_Tamper3EventCallback(hrtc);
726
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
727 728 729 730 731

      /* Clear the Tamper interrupt pending bit */
      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
    }
  }
732

733 734 735 736 737 738 739 740
  /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();

  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;
}

/**
741
  * @brief  TimeStamp callback.
742 743 744 745 746 747 748 749
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
__weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
750

751 752 753 754 755 756
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTC_TimeStampEventCallback could be implemented in the user file
  */
}

/**
757
  * @brief  Tamper 1 callback.
758 759 760 761 762 763 764 765
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
__weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
766

767 768 769 770 771 772
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTC_Tamper1EventCallback could be implemented in the user file
   */
}

/**
773
  * @brief  Tamper 2 callback.
774 775 776 777 778 779 780 781
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
__weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
782

783 784 785 786 787 788
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTC_Tamper2EventCallback could be implemented in the user file
   */
}

/**
789
  * @brief  Tamper 3 callback.
790 791 792 793 794 795 796
  * @param  hrtc RTC handle
  * @retval None
  */
__weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
797

798 799 800 801 802 803 804 805 806 807 808 809 810
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
   */
}

/**
  * @brief  This function handles TimeStamp polling request.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
811 812
{
  uint32_t tickstart = 0;
813 814 815 816 817

  /* Get tick */
  tickstart = HAL_GetTick();

  while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
818
  {
819 820 821 822 823 824 825 826 827
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        return HAL_TIMEOUT;
      }
    }
  }
828

829 830 831 832
  if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  {
    /* Clear the TIMESTAMP OverRun Flag */
    __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
833

834
    /* Change TIMESTAMP state */
835 836 837
    hrtc->State = HAL_RTC_STATE_ERROR;

    return HAL_ERROR;
838
   }
839

840
  /* Change RTC state */
841 842 843
  hrtc->State = HAL_RTC_STATE_READY;

  return HAL_OK;
844
}
845

846 847 848 849 850 851 852 853
/**
  * @brief  This function handles Tamper1 Polling.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
854 855
{
  uint32_t tickstart = 0U;
856 857 858

  /* Get tick */
  tickstart = HAL_GetTick();
859

860 861 862 863 864 865 866 867 868 869 870 871
  /* Get the status of the Interrupt */
  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        return HAL_TIMEOUT;
      }
    }
  }
872

873 874
  /* Clear the Tamper Flag */
  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
875

876 877
  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;
878 879

  return HAL_OK;
880 881 882 883 884 885 886 887 888 889
}

/**
  * @brief  This function handles Tamper2 Polling.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
890 891
{
  uint32_t tickstart = 0;
892 893 894

  /* Get tick */
  tickstart = HAL_GetTick();
895

896 897 898 899 900 901 902 903 904 905 906 907
  /* Get the status of the Interrupt */
  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        return HAL_TIMEOUT;
      }
    }
  }
908

909 910
  /* Clear the Tamper Flag */
  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
911

912 913
  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;
914

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
  return HAL_OK;
}

/**
  * @brief  This function handles Tamper3 Polling.
  * @param  hrtc RTC handle
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
{
  uint32_t tickstart = HAL_GetTick();

  /* Get the status of the Interrupt */
  while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == RESET)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        return HAL_TIMEOUT;
      }
    }
  }

  /* Clear the Tamper Flag */
  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);

  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;

  return HAL_OK;
}

/**
  * @}
  */
953

954 955 956
/** @defgroup RTCEx_Group2 RTC Wake-up functions
 *  @brief   RTC Wake-up functions
 *
957
@verbatim
958 959
 ===============================================================================
                        ##### RTC Wake-up functions #####
960 961
 ===============================================================================

962 963 964 965 966 967 968
 [..] This section provides functions allowing to configure Wake-up feature

@endverbatim
  * @{
  */

/**
969
  * @brief  Sets wake up timer.
970 971 972
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  WakeUpCounter Wake up counter
973
  * @param  WakeUpClock Wake up clock
974 975 976 977 978 979 980 981 982
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
{
  uint32_t tickstart = 0;

  /* Check the parameters */
  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
983 984

  /* Process Locked */
985
  __HAL_LOCK(hrtc);
986

987
  hrtc->State = HAL_RTC_STATE_BUSY;
988

989 990
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);

  /* Get tick */
  tickstart = HAL_GetTick();

  /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  {
    /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
    while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
    {
      if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
      {
        /* Enable the write protection for RTC registers */
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1007 1008 1009 1010

        hrtc->State = HAL_RTC_STATE_TIMEOUT;

        /* Process Unlocked */
1011
        __HAL_UNLOCK(hrtc);
1012

1013
        return HAL_TIMEOUT;
1014
      }
1015 1016
    }
  }
1017

1018 1019
  /* Clear the Wakeup Timer clock source bits in CR register */
  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1020

1021 1022
  /* Configure the clock source */
  hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1023

1024 1025
  /* Configure the Wakeup Timer counter */
  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1026

1027
   /* Enable the Wakeup Timer */
1028 1029
  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);

1030
  /* Enable the write protection for RTC registers */
1031 1032 1033 1034 1035
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1036
  __HAL_UNLOCK(hrtc);
1037

1038 1039 1040 1041 1042 1043 1044 1045
  return HAL_OK;
}

/**
  * @brief  Sets wake up timer with interrupt
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  WakeUpCounter Wake up counter
1046
  * @param  WakeUpClock Wake up clock
1047 1048 1049 1050 1051 1052 1053 1054 1055
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
{
  __IO uint32_t count;

  /* Check the parameters */
  assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1056 1057

  /* Process Locked */
1058
  __HAL_LOCK(hrtc);
1059

1060
  hrtc->State = HAL_RTC_STATE_BUSY;
1061

1062 1063
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1064

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);

  /* Check RTC WUTWF flag is reset only when wake up timer enabled */
  if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  {
    /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
    count = RTC_TIMEOUT_VALUE  * (SystemCoreClock / 32U / 1000U);
    do
    {
      if(count-- == 0U)
      {
        /* Enable the write protection for RTC registers */
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

        hrtc->State = HAL_RTC_STATE_TIMEOUT;

        /* Process Unlocked */
        __HAL_UNLOCK(hrtc);

        return HAL_TIMEOUT;
      }
    }
    while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET);
  }

  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);

  /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  count = RTC_TIMEOUT_VALUE  * (SystemCoreClock / 32U / 1000U);
  do
  {
    if(count-- == 0U)
    {
      /* Enable the write protection for RTC registers */
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

      hrtc->State = HAL_RTC_STATE_TIMEOUT;

      /* Process Unlocked */
      __HAL_UNLOCK(hrtc);

      return HAL_TIMEOUT;
    }
  }
  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET);
1110

1111 1112 1113 1114 1115 1116 1117 1118
  /* Configure the Wake-up Timer counter */
  hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;

  /* Clear the Wakeup Timer clock source bits in CR register */
  hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;

  /* Configure the clock source */
  hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1119

1120 1121
  /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1122

1123
  EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
1124

1125 1126
  /* Clear RTC Wake Up timer Flag */
  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1127

1128 1129
  /* Configure the Interrupt in the RTC_CR register */
  __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
1130

1131 1132
  /* Enable the Wakeup Timer */
  __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1133

1134
  /* Enable the write protection for RTC registers */
1135 1136 1137 1138 1139
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1140
  __HAL_UNLOCK(hrtc);
1141

1142 1143 1144 1145 1146 1147
  return HAL_OK;
}

/**
  * @brief  Deactivates wake up timer counter.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1148
  *                the configuration information for RTC.
1149 1150 1151 1152 1153
  * @retval HAL status
  */
uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
{
  uint32_t tickstart = 0U;
1154 1155

  /* Process Locked */
1156
  __HAL_LOCK(hrtc);
1157

1158
  hrtc->State = HAL_RTC_STATE_BUSY;
1159

1160 1161
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1162

1163 1164
  /* Disable the Wakeup Timer */
  __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1165 1166

  /* In case of interrupt mode is used, the interrupt source must disabled */
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
  __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);

  /* Get tick */
  tickstart = HAL_GetTick();

  /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  {
    if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
    {
      /* Enable the write protection for RTC registers */
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1179 1180 1181 1182

      hrtc->State = HAL_RTC_STATE_TIMEOUT;

      /* Process Unlocked */
1183
      __HAL_UNLOCK(hrtc);
1184

1185
      return HAL_TIMEOUT;
1186
    }
1187
  }
1188

1189 1190
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1191 1192 1193 1194

  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1195
  __HAL_UNLOCK(hrtc);
1196

1197 1198 1199 1200 1201 1202
  return HAL_OK;
}

/**
  * @brief  Gets wake up timer counter.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1203
  *                the configuration information for RTC.
1204 1205 1206 1207 1208
  * @retval Counter value
  */
uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
{
  /* Get the counter value */
1209
  return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1210 1211 1212 1213 1214 1215 1216 1217 1218
}

/**
  * @brief  This function handles Wake Up Timer interrupt request.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1219
{
1220 1221 1222 1223 1224
  if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
  {
    /* Get the status of the Interrupt */
    if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
    {
1225 1226 1227 1228
      /* WAKEUPTIMER callback */
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
      hrtc->WakeUpTimerEventCallback(hrtc);
#else
1229
      HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1230 1231
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */

1232 1233 1234 1235
      /* Clear the WAKEUPTIMER interrupt pending bit */
      __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
    }
  }
1236

1237 1238
  /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1239

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;
}

/**
  * @brief  Wake Up Timer callback.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
__weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
   */
}

/**
  * @brief  This function handles Wake Up Timer Polling.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1268 1269
{
  uint32_t tickstart = 0;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

  /* Get tick */
  tickstart = HAL_GetTick();

  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1281

1282 1283 1284 1285
        return HAL_TIMEOUT;
      }
    }
  }
1286

1287 1288
  /* Clear the WAKEUPTIMER Flag */
  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1289

1290 1291
  /* Change RTC state */
  hrtc->State = HAL_RTC_STATE_READY;
1292 1293

  return HAL_OK;
1294 1295 1296 1297 1298 1299 1300
}

/**
  * @}
  */


1301 1302
/** @defgroup RTCEx_Group3 Extension Peripheral Control functions
 *  @brief   Extension Peripheral Control functions
1303
 *
1304
@verbatim
1305 1306
 ===============================================================================
              ##### Extension Peripheral Control functions #####
1307
 ===============================================================================
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    [..]
    This subsection provides functions allowing to
      (+) Write a data in a specified RTC Backup data register
      (+) Read a data in a specified RTC Backup data register
      (+) Set the Coarse calibration parameters.
      (+) Deactivate the Coarse calibration parameters
      (+) Set the Smooth calibration parameters.
      (+) Configure the Synchronization Shift Control Settings.
      (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
      (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
      (+) Enable the RTC reference clock detection.
      (+) Disable the RTC reference clock detection.
      (+) Enable the Bypass Shadow feature.
      (+) Disable the Bypass Shadow feature.

@endverbatim
  * @{
  */

/**
  * @brief  Writes a data in a specified RTC Backup data register.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1330
  *                the configuration information for RTC.
1331
  * @param  BackupRegister RTC Backup data Register number.
1332
  *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
1333
  *                                 specify the register.
1334
  * @param  Data Data to be written in the specified RTC Backup data register.
1335 1336 1337 1338 1339
  * @retval None
  */
void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
{
  uint32_t tmp = 0;
1340

1341 1342
  /* Check the parameters */
  assert_param(IS_RTC_BKP(BackupRegister));
1343

1344 1345
  tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  tmp += (BackupRegister * 4);
1346

1347 1348 1349 1350 1351 1352 1353
  /* Write the specified register */
  *(__IO uint32_t *)tmp = (uint32_t)Data;
}

/**
  * @brief  Reads data from the specified RTC Backup data Register.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1354
  *                the configuration information for RTC.
1355
  * @param  BackupRegister RTC Backup data Register number.
1356 1357
  *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  *                                 specify the register.
1358 1359 1360 1361 1362
  * @retval Read value
  */
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
{
  uint32_t tmp = 0;
1363

1364 1365 1366 1367 1368
  /* Check the parameters */
  assert_param(IS_RTC_BKP(BackupRegister));

  tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  tmp += (BackupRegister * 4);
1369

1370 1371 1372 1373 1374 1375 1376
  /* Read the specified register */
  return (*(__IO uint32_t *)tmp);
}

/**
  * @brief  Sets the Smooth calibration parameters.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1377
  *                the configuration information for RTC.
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
  * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
  *          This parameter can be can be one of the following values :
  *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  *          This parameter can be one of the following values:
  *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulses every 2*11 pulses.
  *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  * @param  SmouthCalibMinusPulsesValue Select the value of CALM[80] bits.
  *          This parameter can be one any value from 0 to 0x000001FF.
1389 1390 1391
  * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  *         SmouthCalibMinusPulsesValue must be equal to 0.
1392 1393 1394 1395 1396
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
{
  uint32_t tickstart = 0;
1397

1398 1399 1400 1401
  /* Check the parameters */
  assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
1402 1403

  /* Process Locked */
1404
  __HAL_LOCK(hrtc);
1405

1406
  hrtc->State = HAL_RTC_STATE_BUSY;
1407

1408 1409
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1410

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  /* check if a calibration is pending*/
  if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  {
  /* Get tick */
  tickstart = HAL_GetTick();

    /* check if a calibration is pending*/
    while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
    {
      if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
      {
        /* Enable the write protection for RTC registers */
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1424

1425
        /* Change RTC state */
1426 1427 1428
        hrtc->State = HAL_RTC_STATE_TIMEOUT;

        /* Process Unlocked */
1429
        __HAL_UNLOCK(hrtc);
1430

1431 1432 1433 1434
        return HAL_TIMEOUT;
      }
    }
  }
1435

1436 1437
  /* Configure the Smooth calibration settings */
  hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
1438

1439 1440
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1441

1442
  /* Change RTC state */
1443 1444 1445
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1446
  __HAL_UNLOCK(hrtc);
1447

1448 1449 1450 1451 1452
  return HAL_OK;
}

/**
  * @brief  Configures the Synchronization Shift Control Settings.
1453
  * @note   When REFCKON is set, firmware must not write to Shift control register.
1454
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1455
  *                the configuration information for RTC.
1456 1457
  * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
  *          This parameter can be one of the following values :
1458
  *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  *             @arg RTC_SHIFTADD1S_RESET: No effect.
  * @param  ShiftSubFS Select the number of Second Fractions to substitute.
  *          This parameter can be one any value from 0 to 0x7FFF.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
{
  uint32_t tickstart = 0;

  /* Check the parameters */
  assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));

1472
  /* Process Locked */
1473
  __HAL_LOCK(hrtc);
1474

1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
  hrtc->State = HAL_RTC_STATE_BUSY;

  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);

  /* Get tick */
  tickstart = HAL_GetTick();

    /* Wait until the shift is completed*/
    while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
    {
      if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1487
      {
1488
        /* Enable the write protection for RTC registers */
1489 1490
        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

1491
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
1492 1493

        /* Process Unlocked */
1494
        __HAL_UNLOCK(hrtc);
1495

1496 1497 1498
        return HAL_TIMEOUT;
      }
    }
1499

1500 1501 1502 1503 1504
    /* Check if the reference clock detection is disabled */
    if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
    {
      /* Configure the Shift settings */
      hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1505

1506 1507 1508 1509 1510 1511
      /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
      if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
      {
        if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
        {
          /* Enable the write protection for RTC registers */
1512 1513
          __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

1514
          hrtc->State = HAL_RTC_STATE_ERROR;
1515 1516

          /* Process Unlocked */
1517
          __HAL_UNLOCK(hrtc);
1518

1519 1520 1521 1522 1523 1524 1525 1526
          return HAL_ERROR;
        }
      }
    }
    else
    {
      /* Enable the write protection for RTC registers */
      __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1527

1528
      /* Change RTC state */
1529 1530 1531
      hrtc->State = HAL_RTC_STATE_ERROR;

      /* Process Unlocked */
1532
      __HAL_UNLOCK(hrtc);
1533

1534 1535
      return HAL_ERROR;
    }
1536

1537 1538
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1539

1540
  /* Change RTC state */
1541 1542 1543
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1544
  __HAL_UNLOCK(hrtc);
1545

1546 1547 1548 1549 1550 1551
  return HAL_OK;
}

/**
  * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1552
  *                the configuration information for RTC.
1553 1554
  * @param  CalibOutput Select the Calibration output Selection .
  *          This parameter can be one of the following values:
1555
  *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1556 1557 1558 1559 1560 1561 1562
  *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
{
  /* Check the parameters */
  assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1563 1564

  /* Process Locked */
1565
  __HAL_LOCK(hrtc);
1566

1567 1568 1569 1570
  hrtc->State = HAL_RTC_STATE_BUSY;

  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1571

1572 1573
  /* Clear flags before config */
  hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1574

1575 1576
  /* Configure the RTC_CR register */
  hrtc->Instance->CR |= (uint32_t)CalibOutput;
1577

1578
  __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1579

1580 1581
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1582

1583
  /* Change RTC state */
1584 1585 1586
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1587
  __HAL_UNLOCK(hrtc);
1588

1589 1590 1591 1592 1593 1594
  return HAL_OK;
}

/**
  * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1595
  *                the configuration information for RTC.
1596 1597 1598 1599
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
{
1600
  /* Process Locked */
1601
  __HAL_LOCK(hrtc);
1602

1603
  hrtc->State = HAL_RTC_STATE_BUSY;
1604

1605 1606
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1607

1608
  __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1609

1610 1611
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1612

1613
  /* Change RTC state */
1614 1615 1616
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1617
  __HAL_UNLOCK(hrtc);
1618

1619 1620 1621 1622 1623 1624
  return HAL_OK;
}

/**
  * @brief  Enables the RTC reference clock detection.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1625
  *                the configuration information for RTC.
1626 1627 1628 1629
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
{
1630
  /* Process Locked */
1631
  __HAL_LOCK(hrtc);
1632

1633
  hrtc->State = HAL_RTC_STATE_BUSY;
1634

1635 1636
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1637

1638 1639 1640 1641
  /* Set Initialization mode */
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
  {
    /* Enable the write protection for RTC registers */
1642 1643
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

1644 1645
    /* Set RTC state*/
    hrtc->State = HAL_RTC_STATE_ERROR;
1646 1647

    /* Process Unlocked */
1648
    __HAL_UNLOCK(hrtc);
1649

1650
    return HAL_ERROR;
1651
  }
1652 1653 1654 1655 1656
  else
  {
    __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);

    /* Exit Initialization mode */
1657
    hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1658
  }
1659

1660 1661
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1662

1663
   /* Change RTC state */
1664 1665 1666
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1667
  __HAL_UNLOCK(hrtc);
1668

1669 1670 1671 1672 1673 1674
  return HAL_OK;
}

/**
  * @brief  Disable the RTC reference clock detection.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1675
  *                the configuration information for RTC.
1676 1677 1678
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
1679 1680
{
  /* Process Locked */
1681
  __HAL_LOCK(hrtc);
1682

1683
  hrtc->State = HAL_RTC_STATE_BUSY;
1684

1685 1686
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1687

1688 1689 1690 1691
  /* Set Initialization mode */
  if(RTC_EnterInitMode(hrtc) != HAL_OK)
  {
    /* Enable the write protection for RTC registers */
1692 1693
    __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

1694 1695
    /* Set RTC state*/
    hrtc->State = HAL_RTC_STATE_ERROR;
1696 1697

    /* Process Unlocked */
1698
    __HAL_UNLOCK(hrtc);
1699

1700
    return HAL_ERROR;
1701
  }
1702 1703 1704
  else
  {
    __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1705

1706
    /* Exit Initialization mode */
1707
    hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1708
  }
1709

1710 1711
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1712

1713
  /* Change RTC state */
1714 1715 1716
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1717
  __HAL_UNLOCK(hrtc);
1718

1719 1720 1721 1722 1723 1724
  return HAL_OK;
}

/**
  * @brief  Enables the Bypass Shadow feature.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1725 1726
  *                the configuration information for RTC.
  * @note   When the Bypass Shadow is enabled the calendar value are taken
1727 1728 1729 1730 1731
  *         directly from the Calendar counter.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
{
1732
  /* Process Locked */
1733
  __HAL_LOCK(hrtc);
1734

1735
  hrtc->State = HAL_RTC_STATE_BUSY;
1736

1737 1738
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1739

1740 1741
  /* Set the BYPSHAD bit */
  hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1742

1743 1744
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1745

1746
  /* Change RTC state */
1747 1748 1749
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1750
  __HAL_UNLOCK(hrtc);
1751

1752 1753 1754 1755 1756 1757
  return HAL_OK;
}

/**
  * @brief  Disables the Bypass Shadow feature.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1758 1759
  *                the configuration information for RTC.
  * @note   When the Bypass Shadow is enabled the calendar value are taken
1760 1761 1762 1763 1764
  *         directly from the Calendar counter.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
{
1765
  /* Process Locked */
1766
  __HAL_LOCK(hrtc);
1767

1768
  hrtc->State = HAL_RTC_STATE_BUSY;
1769

1770 1771
  /* Disable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1772

1773 1774
  /* Reset the BYPSHAD bit */
  hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1775

1776 1777
  /* Enable the write protection for RTC registers */
  __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1778

1779
  /* Change RTC state */
1780 1781 1782
  hrtc->State = HAL_RTC_STATE_READY;

  /* Process Unlocked */
1783
  __HAL_UNLOCK(hrtc);
1784

1785 1786 1787 1788 1789 1790 1791
  return HAL_OK;
}

/**
  * @}
  */

1792 1793
  /** @defgroup RTCEx_Group4 Extended features functions
 *  @brief    Extended features functions
1794
 *
1795
@verbatim
1796 1797
 ===============================================================================
                 ##### Extended features functions #####
1798
 ===============================================================================
1799 1800 1801
    [..]  This section provides functions allowing to:
      (+) RTC Alram B callback
      (+) RTC Poll for Alarm B request
1802

1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
@endverbatim
  * @{
  */

/**
  * @brief  Alarm B callback.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @retval None
  */
__weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hrtc);
1817

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_RTC_AlarmBEventCallback could be implemented in the user file
   */
}

/**
  * @brief  This function handles AlarmB Polling request.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1831 1832
{
  uint32_t tickstart = 0;
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847

  /* Get tick */
  tickstart = HAL_GetTick();

  while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
        return HAL_TIMEOUT;
      }
    }
  }
1848

1849 1850
  /* Clear the Alarm Flag */
  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1851

1852
  /* Change RTC state */
1853 1854 1855
  hrtc->State = HAL_RTC_STATE_READY;

  return HAL_OK;
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
}

/**
  * @}
  */

/**
  * @}
  */

#endif /* HAL_RTC_MODULE_ENABLED */
/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/