Subversion Repositories dashGPS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_rtc_ex.c
4
  * @author  MCD Application Team
5
  * @brief   Extended RTC HAL module driver.
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the Real Time Clock (RTC) Extension peripheral:
8
  *           + RTC Tamper functions
9
  *           + Extension Control functions
10
  *           + Extension RTC features functions
11
  *
12
  ******************************************************************************
13
  * @attention
14
  *
15
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
16
  * All rights reserved.</center></h2>
17
  *
18
  * This software component is licensed by ST under BSD 3-Clause license,
19
  * the "License"; You may not use this file except in compliance with the
20
  * License. You may obtain a copy of the License at:
21
  *                        opensource.org/licenses/BSD-3-Clause
22
  *
23
  ******************************************************************************
24
  */
25
 
26
/* Includes ------------------------------------------------------------------*/
27
#include "stm32f1xx_hal.h"
28
 
29
/** @addtogroup STM32F1xx_HAL_Driver
30
  * @{
31
  */
32
 
33
#ifdef HAL_RTC_MODULE_ENABLED
34
 
35
/** @defgroup RTCEx RTCEx
36
  * @brief RTC Extended HAL module driver
37
  * @{
38
  */
39
 
40
/* Private typedef -----------------------------------------------------------*/
41
/* Private define ------------------------------------------------------------*/
42
/* Private macro -------------------------------------------------------------*/
43
/** @defgroup RTCEx_Private_Macros RTCEx Private Macros
44
  * @{
45
  */
46
/**
47
  * @}
48
  */
49
 
50
/* Private variables ---------------------------------------------------------*/
51
/* Private function prototypes -----------------------------------------------*/
52
/* Private functions ---------------------------------------------------------*/
53
 
54
/** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
55
  * @{
56
  */
57
 
58
/** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
59
  * @brief    RTC Tamper functions
60
  *
61
@verbatim
62
 ===============================================================================
63
                 ##### RTC Tamper functions #####
64
 ===============================================================================
65
 
66
 [..] This section provides functions allowing to configure Tamper feature
67
 
68
@endverbatim
69
  * @{
70
  */
71
 
72
/**
73
  * @brief  Sets Tamper
74
  * @note   By calling this API we disable the tamper interrupt for all tampers.
75
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
76
  *                the configuration information for RTC.
77
  * @param  sTamper: Pointer to Tamper Structure.
78
  * @note   Tamper can be enabled only if ASOE and CCO bit are reset
79
  * @retval HAL status
80
  */
81
HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
82
{
83
  /* Check input parameters */
84
  if ((hrtc == NULL) || (sTamper == NULL))
85
  {
86
    return HAL_ERROR;
87
  }
88
 
89
  /* Check the parameters */
90
  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
91
  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
92
 
93
  /* Process Locked */
94
  __HAL_LOCK(hrtc);
95
 
96
  hrtc->State = HAL_RTC_STATE_BUSY;
97
 
98
  if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
99
  {
100
    hrtc->State = HAL_RTC_STATE_ERROR;
101
 
102
    /* Process Unlocked */
103
    __HAL_UNLOCK(hrtc);
104
 
105
    return HAL_ERROR;
106
  }
107
 
108
  MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
109
 
110
  hrtc->State = HAL_RTC_STATE_READY;
111
 
112
  /* Process Unlocked */
113
  __HAL_UNLOCK(hrtc);
114
 
115
  return HAL_OK;
116
}
117
 
118
/**
119
  * @brief  Sets Tamper with interrupt.
120
  * @note   By calling this API we force the tamper interrupt for all tampers.
121
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
122
  *                the configuration information for RTC.
123
  * @param  sTamper: Pointer to RTC Tamper.
124
  * @note   Tamper can be enabled only if ASOE and CCO bit are reset
125
  * @retval HAL status
126
  */
127
HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
128
{
129
  /* Check input parameters */
130
  if ((hrtc == NULL) || (sTamper == NULL))
131
  {
132
    return HAL_ERROR;
133
  }
134
 
135
  /* Check the parameters */
136
  assert_param(IS_RTC_TAMPER(sTamper->Tamper));
137
  assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
138
 
139
  /* Process Locked */
140
  __HAL_LOCK(hrtc);
141
 
142
  hrtc->State = HAL_RTC_STATE_BUSY;
143
 
144
  if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
145
  {
146
    hrtc->State = HAL_RTC_STATE_ERROR;
147
 
148
    /* Process Unlocked */
149
    __HAL_UNLOCK(hrtc);
150
 
151
    return HAL_ERROR;
152
  }
153
 
154
  MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
155
 
156
  /* Configure the Tamper Interrupt in the BKP->CSR */
157
  __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
158
 
159
  hrtc->State = HAL_RTC_STATE_READY;
160
 
161
  /* Process Unlocked */
162
  __HAL_UNLOCK(hrtc);
163
 
164
  return HAL_OK;
165
}
166
 
167
/**
168
  * @brief  Deactivates Tamper.
169
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
170
  *                the configuration information for RTC.
171
  * @param  Tamper: Selected tamper pin.
172
  *          This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
173
  * @retval HAL status
174
  */
175
HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
176
{
177
  /* Check input parameters */
178
  if (hrtc == NULL)
179
  {
180
    return HAL_ERROR;
181
  }
182
  /* Prevent unused argument(s) compilation warning */
183
  UNUSED(Tamper);
184
 
185
  assert_param(IS_RTC_TAMPER(Tamper));
186
 
187
  /* Process Locked */
188
  __HAL_LOCK(hrtc);
189
 
190
  hrtc->State = HAL_RTC_STATE_BUSY;
191
 
192
  /* Disable the selected Tamper pin */
193
  CLEAR_BIT(BKP->CR, BKP_CR_TPE);
194
 
195
  /* Disable the Tamper Interrupt in the BKP->CSR */
196
  /* Configure the Tamper Interrupt in the BKP->CSR */
197
  __HAL_RTC_TAMPER_DISABLE_IT(hrtc, RTC_IT_TAMP1);
198
 
199
  /* Clear the Tamper interrupt pending bit */
200
  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
201
  SET_BIT(BKP->CSR, BKP_CSR_CTE);
202
 
203
  hrtc->State = HAL_RTC_STATE_READY;
204
 
205
  /* Process Unlocked */
206
  __HAL_UNLOCK(hrtc);
207
 
208
  return HAL_OK;
209
}
210
 
211
/**
212
  * @brief  This function handles Tamper interrupt request.
213
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
214
  *                the configuration information for RTC.
215
  * @retval None
216
  */
217
void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
218
{
219
  /* Get the status of the Interrupt */
220
  if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP1))
221
  {
222
    /* Get the TAMPER Interrupt enable bit and pending bit */
223
    if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
224
    {
225
      /* Tamper callback */
226
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
227
      hrtc->Tamper1EventCallback(hrtc);
228
#else
229
      HAL_RTCEx_Tamper1EventCallback(hrtc);
230
#endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
231
 
232
      /* Clear the Tamper interrupt pending bit */
233
      __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
234
    }
235
  }
236
 
237
  /* Change RTC state */
238
  hrtc->State = HAL_RTC_STATE_READY;
239
}
240
 
241
/**
242
  * @brief  Tamper 1 callback.
243
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
244
  *                the configuration information for RTC.
245
  * @retval None
246
  */
247
__weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
248
{
249
  /* Prevent unused argument(s) compilation warning */
250
  UNUSED(hrtc);
251
  /* NOTE : This function Should not be modified, when the callback is needed,
252
            the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
253
   */
254
}
255
 
256
/**
257
  * @brief  This function handles Tamper1 Polling.
258
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
259
  *                the configuration information for RTC.
260
  * @param  Timeout: Timeout duration
261
  * @retval HAL status
262
  */
263
HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
264
{
265
  uint32_t tickstart = HAL_GetTick();
266
 
267
  /* Check input parameters */
268
  if (hrtc == NULL)
269
  {
270
    return HAL_ERROR;
271
  }
272
 
273
  /* Get the status of the Interrupt */
274
  while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == RESET)
275
  {
276
    if (Timeout != HAL_MAX_DELAY)
277
    {
278
      if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
279
      {
280
        hrtc->State = HAL_RTC_STATE_TIMEOUT;
281
        return HAL_TIMEOUT;
282
      }
283
    }
284
  }
285
 
286
  /* Clear the Tamper Flag */
287
  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
288
 
289
  /* Change RTC state */
290
  hrtc->State = HAL_RTC_STATE_READY;
291
 
292
  return HAL_OK;
293
}
294
 
295
/**
296
  * @}
297
  */
298
 
299
/** @defgroup RTCEx_Exported_Functions_Group2 RTC Second functions
300
  * @brief    RTC Second functions
301
  *
302
@verbatim
303
 ===============================================================================
304
                 ##### RTC Second functions #####
305
 ===============================================================================
306
 
307
 [..] This section provides functions implementing second interupt handlers
308
 
309
@endverbatim
310
  * @{
311
  */
312
 
313
/**
314
  * @brief  Sets Interrupt for second
315
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
316
  *                the configuration information for RTC.
317
  * @retval HAL status
318
  */
319
HAL_StatusTypeDef HAL_RTCEx_SetSecond_IT(RTC_HandleTypeDef *hrtc)
320
{
321
  /* Check input parameters */
322
  if (hrtc == NULL)
323
  {
324
    return HAL_ERROR;
325
  }
326
 
327
  /* Process Locked */
328
  __HAL_LOCK(hrtc);
329
 
330
  hrtc->State = HAL_RTC_STATE_BUSY;
331
 
332
  /* Enable Second interuption */
333
  __HAL_RTC_SECOND_ENABLE_IT(hrtc, RTC_IT_SEC);
334
 
335
  hrtc->State = HAL_RTC_STATE_READY;
336
 
337
  /* Process Unlocked */
338
  __HAL_UNLOCK(hrtc);
339
 
340
  return HAL_OK;
341
}
342
 
343
/**
344
  * @brief  Deactivates Second.
345
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
346
  *                the configuration information for RTC.
347
  * @retval HAL status
348
  */
349
HAL_StatusTypeDef HAL_RTCEx_DeactivateSecond(RTC_HandleTypeDef *hrtc)
350
{
351
  /* Check input parameters */
352
  if (hrtc == NULL)
353
  {
354
    return HAL_ERROR;
355
  }
356
 
357
  /* Process Locked */
358
  __HAL_LOCK(hrtc);
359
 
360
  hrtc->State = HAL_RTC_STATE_BUSY;
361
 
362
  /* Deactivate Second interuption*/
363
  __HAL_RTC_SECOND_DISABLE_IT(hrtc, RTC_IT_SEC);
364
 
365
  hrtc->State = HAL_RTC_STATE_READY;
366
 
367
  /* Process Unlocked */
368
  __HAL_UNLOCK(hrtc);
369
 
370
  return HAL_OK;
371
}
372
 
373
/**
374
  * @brief  This function handles second interrupt request.
375
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
376
  *                the configuration information for RTC.
377
  * @retval None
378
  */
379
void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc)
380
{
381
  if (__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
382
  {
383
    /* Get the status of the Interrupt */
384
    if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
385
    {
386
      /* Check if Overrun occurred */
387
      if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
388
      {
389
        /* Second error callback */
390
        HAL_RTCEx_RTCEventErrorCallback(hrtc);
391
 
392
        /* Clear flag Second */
393
        __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
394
 
395
        /* Change RTC state */
396
        hrtc->State = HAL_RTC_STATE_ERROR;
397
      }
398
      else
399
      {
400
        /* Second callback */
401
        HAL_RTCEx_RTCEventCallback(hrtc);
402
 
403
        /* Change RTC state */
404
        hrtc->State = HAL_RTC_STATE_READY;
405
      }
406
 
407
      /* Clear flag Second */
408
      __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
409
    }
410
  }
411
}
412
 
413
/**
414
  * @brief  Second event callback.
415
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
416
  *                the configuration information for RTC.
417
  * @retval None
418
  */
419
__weak void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
420
{
421
  /* Prevent unused argument(s) compilation warning */
422
  UNUSED(hrtc);
423
  /* NOTE : This function Should not be modified, when the callback is needed,
424
            the HAL_RTCEx_RTCEventCallback could be implemented in the user file
425
   */
426
}
427
 
428
/**
429
  * @brief  Second event error callback.
430
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
431
  *                the configuration information for RTC.
432
  * @retval None
433
  */
434
__weak void HAL_RTCEx_RTCEventErrorCallback(RTC_HandleTypeDef *hrtc)
435
{
436
  /* Prevent unused argument(s) compilation warning */
437
  UNUSED(hrtc);
438
  /* NOTE : This function Should not be modified, when the callback is needed,
439
            the HAL_RTCEx_RTCEventErrorCallback could be implemented in the user file
440
   */
441
}
442
 
443
/**
444
  * @}
445
  */
446
 
447
/** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
448
  * @brief    Extended Peripheral Control functions
449
  *
450
@verbatim
451
 ===============================================================================
452
              ##### Extension Peripheral Control functions #####
453
 ===============================================================================
454
    [..]
455
    This subsection provides functions allowing to
456
      (+) Writes a data in a specified RTC Backup data register
457
      (+) Read a data in a specified RTC Backup data register
458
      (+) Sets the Smooth calibration parameters.
459
 
460
@endverbatim
461
  * @{
462
  */
463
 
464
/**
465
  * @brief  Writes a data in a specified RTC Backup data register.
466
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
467
  *                the configuration information for RTC.
468
  * @param  BackupRegister: RTC Backup data Register number.
469
  *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
470
  *                                 specify the register (depending devices).
471
  * @param  Data: Data to be written in the specified RTC Backup data register.
472
  * @retval None
473
  */
474
void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
475
{
476
  uint32_t tmp = 0U;
477
 
478
  /* Prevent unused argument(s) compilation warning */
479
  UNUSED(hrtc);
480
 
481
  /* Check the parameters */
482
  assert_param(IS_RTC_BKP(BackupRegister));
483
 
484
  tmp = (uint32_t)BKP_BASE;
485
  tmp += (BackupRegister * 4U);
486
 
487
  *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
488
}
489
 
490
/**
491
  * @brief  Reads data from the specified RTC Backup data Register.
492
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
493
  *                the configuration information for RTC.
494
  * @param  BackupRegister: RTC Backup data Register number.
495
  *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
496
  *                                 specify the register (depending devices).
497
  * @retval Read value
498
  */
499
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
500
{
501
  uint32_t backupregister = 0U;
502
  uint32_t pvalue = 0U;
503
 
504
  /* Prevent unused argument(s) compilation warning */
505
  UNUSED(hrtc);
506
 
507
  /* Check the parameters */
508
  assert_param(IS_RTC_BKP(BackupRegister));
509
 
510
  backupregister = (uint32_t)BKP_BASE;
511
  backupregister += (BackupRegister * 4U);
512
 
513
  pvalue = (*(__IO uint32_t *)(backupregister)) & BKP_DR1_D;
514
 
515
  /* Read the specified register */
516
  return pvalue;
517
}
518
 
519
 
520
/**
521
  * @brief  Sets the Smooth calibration parameters.
522
  * @param  hrtc: RTC handle
523
  * @param  SmoothCalibPeriod: Not used (only present for compatibility with another families)
524
  * @param  SmoothCalibPlusPulses: Not used (only present for compatibility with another families)
525
  * @param  SmouthCalibMinusPulsesValue: specifies the RTC Clock Calibration value.
526
  *          This parameter must be a number between 0 and 0x7F.
527
  * @retval HAL status
528
  */
529
HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
530
{
531
  /* Check input parameters */
532
  if (hrtc == NULL)
533
  {
534
    return HAL_ERROR;
535
  }
536
  /* Prevent unused argument(s) compilation warning */
537
  UNUSED(SmoothCalibPeriod);
538
  UNUSED(SmoothCalibPlusPulses);
539
 
540
  /* Check the parameters */
541
  assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
542
 
543
  /* Process Locked */
544
  __HAL_LOCK(hrtc);
545
 
546
  hrtc->State = HAL_RTC_STATE_BUSY;
547
 
548
  /* Sets RTC Clock Calibration value.*/
549
  MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
550
 
551
  /* Change RTC state */
552
  hrtc->State = HAL_RTC_STATE_READY;
553
 
554
  /* Process Unlocked */
555
  __HAL_UNLOCK(hrtc);
556
 
557
  return HAL_OK;
558
}
559
 
560
/**
561
  * @}
562
  */
563
 
564
/**
565
  * @}
566
  */
567
 
568
/**
569
  * @}
570
  */
571
 
572
#endif /* HAL_RTC_MODULE_ENABLED */
573
 
574
/**
575
  * @}
576
  */
577
 
578
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
579