]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F1/stm32f1xx_hal_rtc_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F1 / stm32f1xx_hal_rtc_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_hal_rtc_ex.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    15-December-2014
7   * @brief   Extended RTC HAL module driver.
8   *          This file provides firmware functions to manage the following 
9   *          functionalities of the Real Time Clock (RTC) Extension peripheral:
10   *           + RTC Tamper functions 
11   *           + Extension Control functions
12   *           + Extension RTC features functions    
13   *         
14   ******************************************************************************
15   * @attention
16   *
17   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
18   *
19   * Redistribution and use in source and binary forms, with or without modification,
20   * are permitted provided that the following conditions are met:
21   *   1. Redistributions of source code must retain the above copyright notice,
22   *      this list of conditions and the following disclaimer.
23   *   2. Redistributions in binary form must reproduce the above copyright notice,
24   *      this list of conditions and the following disclaimer in the documentation
25   *      and/or other materials provided with the distribution.
26   *   3. Neither the name of STMicroelectronics nor the names of its contributors
27   *      may be used to endorse or promote products derived from this software
28   *      without specific prior written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
34   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   ******************************************************************************  
42   */ 
43
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32f1xx_hal.h"
46
47 /** @addtogroup STM32F1xx_HAL_Driver
48   * @{
49   */
50   
51 #ifdef HAL_RTC_MODULE_ENABLED
52
53 /** @defgroup RTCEx RTCEx
54   * @brief RTC Extended HAL module driver
55   * @{
56   */
57   
58 /* Private typedef -----------------------------------------------------------*/
59 /* Private define ------------------------------------------------------------*/
60 /* Private macro -------------------------------------------------------------*/
61 /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
62   * @{
63   */
64 /**
65   * @}
66   */
67   
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Private functions ---------------------------------------------------------*/
71
72 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
73   * @{
74   */
75   
76 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
77   * @brief    RTC Tamper functions
78   *
79 @verbatim   
80  ===============================================================================
81                  ##### RTC Tamper functions #####
82  ===============================================================================  
83  
84  [..] This section provides functions allowing to configure Tamper feature
85
86 @endverbatim
87   * @{
88   */
89
90 /**
91   * @brief  Sets Tamper
92   * @note   By calling this API we disable the tamper interrupt for all tampers. 
93   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
94   *                the configuration information for RTC.
95   * @param  sTamper: Pointer to Tamper Structure.
96   * @note   Tamper can be enabled only if ASOE and CCO bit are reset
97   * @retval HAL status
98   */
99 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
100 {
101   /* Check input parameters */
102   if((hrtc == NULL) || (sTamper == NULL))
103   {
104      return HAL_ERROR;
105   }
106   
107   /* Check the parameters */
108   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
109   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
110
111   /* Process Locked */
112   __HAL_LOCK(hrtc);
113
114   hrtc->State = HAL_RTC_STATE_BUSY;
115   
116   if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
117   {
118     hrtc->State = HAL_RTC_STATE_ERROR;
119     
120     /* Process Unlocked */
121     __HAL_UNLOCK(hrtc);
122     
123     return HAL_ERROR;
124   }
125
126   MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
127
128   hrtc->State = HAL_RTC_STATE_READY; 
129
130   /* Process Unlocked */
131   __HAL_UNLOCK(hrtc);
132
133   return HAL_OK;
134 }
135
136 /**
137   * @brief  Sets Tamper with interrupt.
138   * @note   By calling this API we force the tamper interrupt for all tampers.
139   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
140   *                the configuration information for RTC.
141   * @param  sTamper: Pointer to RTC Tamper.
142   * @note   Tamper can be enabled only if ASOE and CCO bit are reset
143   * @retval HAL status
144   */
145 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
146 {
147   /* Check input parameters */
148   if((hrtc == NULL) || (sTamper == NULL))
149   {
150      return HAL_ERROR;
151   }
152   
153   /* Check the parameters */
154   assert_param(IS_RTC_TAMPER(sTamper->Tamper)); 
155   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
156
157   /* Process Locked */
158   __HAL_LOCK(hrtc);
159
160   hrtc->State = HAL_RTC_STATE_BUSY;
161
162   if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
163   {
164     hrtc->State = HAL_RTC_STATE_ERROR;
165     
166     /* Process Unlocked */
167     __HAL_UNLOCK(hrtc);
168     
169     return HAL_ERROR;
170   }
171
172   MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
173
174   /* Configure the Tamper Interrupt in the BKP->CSR */
175   __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
176
177   hrtc->State = HAL_RTC_STATE_READY;
178
179   /* Process Unlocked */
180   __HAL_UNLOCK(hrtc);
181
182   return HAL_OK;
183 }
184
185 /**
186   * @brief  Deactivates Tamper.
187   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
188   *                the configuration information for RTC.
189   * @param  Tamper: Selected tamper pin.
190   *          This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
191   * @retval HAL status
192   */
193 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
194 {
195   /* Check input parameters */
196   if(hrtc == NULL)
197   {
198      return HAL_ERROR;
199   }
200   
201   assert_param(IS_RTC_TAMPER(Tamper));
202
203   /* Process Locked */
204   __HAL_LOCK(hrtc);
205
206   hrtc->State = HAL_RTC_STATE_BUSY;
207
208   /* Disable the selected Tamper pin */
209   CLEAR_BIT(BKP->CR, BKP_CR_TPE);
210   
211   /* Disable the Tamper Interrupt in the BKP->CSR */
212   /* Configure the Tamper Interrupt in the BKP->CSR */
213   __HAL_RTC_TAMPER_DISABLE_IT(hrtc, RTC_IT_TAMP1);
214   
215   /* Clear the Tamper interrupt pending bit */
216   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
217   SET_BIT(BKP->CSR, BKP_CSR_CTE);
218   
219   hrtc->State = HAL_RTC_STATE_READY;
220
221   /* Process Unlocked */
222   __HAL_UNLOCK(hrtc);
223
224   return HAL_OK;
225 }
226
227 /**
228   * @brief  This function handles Tamper interrupt request.
229   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
230   *                the configuration information for RTC.
231   * @retval None
232   */
233 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
234 {  
235   /* Get the status of the Interrupt */
236   if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP1))
237   {
238     /* Get the TAMPER Interrupt enable bit and pending bit */
239     if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
240     {
241       /* Tamper callback */ 
242       HAL_RTCEx_Tamper1EventCallback(hrtc);
243   
244       /* Clear the Tamper interrupt pending bit */
245       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
246     }
247   }
248
249   /* Change RTC state */
250   hrtc->State = HAL_RTC_STATE_READY;
251 }
252
253 /**
254   * @brief  Tamper 1 callback. 
255   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
256   *                the configuration information for RTC.
257   * @retval None
258   */
259 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
260 {
261   /* NOTE : This function Should not be modified, when the callback is needed,
262             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
263    */
264 }
265
266 /**
267   * @brief  This function handles Tamper1 Polling.
268   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
269   *                the configuration information for RTC.
270   * @param  Timeout: Timeout duration
271   * @retval HAL status
272   */
273 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
274 {  
275   uint32_t tickstart = HAL_GetTick();
276
277   /* Check input parameters */
278   if(hrtc == NULL)
279   {
280      return HAL_ERROR;
281   }
282   
283   /* Get the status of the Interrupt */
284   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP1F)== RESET)
285   {
286     if(Timeout != HAL_MAX_DELAY)
287     {
288       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
289       {
290         hrtc->State = HAL_RTC_STATE_TIMEOUT;
291         return HAL_TIMEOUT;
292       }
293     }
294   }
295
296   /* Clear the Tamper Flag */
297   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
298
299   /* Change RTC state */
300   hrtc->State = HAL_RTC_STATE_READY;
301
302   return HAL_OK;
303 }
304
305 /**
306   * @}
307   */
308   
309 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Second functions
310   * @brief    RTC Second functions
311   *
312 @verbatim   
313  ===============================================================================
314                  ##### RTC Second functions #####
315  ===============================================================================  
316  
317  [..] This section provides functions implementing second interupt handlers
318
319 @endverbatim
320   * @{
321   */
322
323 /**
324   * @brief  Sets Interrupt for second
325   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
326   *                the configuration information for RTC.
327   * @retval HAL status
328   */
329 HAL_StatusTypeDef HAL_RTCEx_SetSecond_IT(RTC_HandleTypeDef *hrtc)
330 {
331   /* Check input parameters */
332   if(hrtc == NULL)
333   {
334      return HAL_ERROR;
335   }
336   
337   /* Process Locked */
338   __HAL_LOCK(hrtc);
339
340   hrtc->State = HAL_RTC_STATE_BUSY;
341
342   /* Enable Second interuption */
343   __HAL_RTC_SECOND_ENABLE_IT(hrtc, RTC_IT_SEC);
344   
345   hrtc->State = HAL_RTC_STATE_READY;
346
347   /* Process Unlocked */
348   __HAL_UNLOCK(hrtc);
349
350   return HAL_OK;
351 }
352
353 /**
354   * @brief  Deactivates Second.
355   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
356   *                the configuration information for RTC.
357   * @retval HAL status
358   */
359 HAL_StatusTypeDef HAL_RTCEx_DeactivateSecond(RTC_HandleTypeDef *hrtc)
360 {
361   /* Check input parameters */
362   if(hrtc == NULL)
363   {
364      return HAL_ERROR;
365   }
366   
367   /* Process Locked */
368   __HAL_LOCK(hrtc);
369
370   hrtc->State = HAL_RTC_STATE_BUSY;
371
372   /* Deactivate Second interuption*/ 
373   __HAL_RTC_SECOND_DISABLE_IT(hrtc, RTC_IT_SEC);
374   
375   hrtc->State = HAL_RTC_STATE_READY;
376
377   /* Process Unlocked */
378   __HAL_UNLOCK(hrtc);
379
380   return HAL_OK;
381 }
382
383 /**
384   * @brief  This function handles second interrupt request.
385   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
386   *                the configuration information for RTC.
387   * @retval None
388   */
389 void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef* hrtc)
390 {
391   if(__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
392   {
393     /* Get the status of the Interrupt */
394     if(__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
395     {
396       /* Check if Overrun occurred */
397       if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
398       {
399         /* Second error callback */ 
400         HAL_RTCEx_RTCEventErrorCallback(hrtc);
401         
402         /* Clear flag Second */
403         __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
404         
405         /* Change RTC state */
406         hrtc->State = HAL_RTC_STATE_ERROR; 
407       }
408       else 
409       {
410         /* Second callback */ 
411         HAL_RTCEx_RTCEventCallback(hrtc);
412         
413         /* Change RTC state */
414         hrtc->State = HAL_RTC_STATE_READY; 
415       }
416       
417       /* Clear flag Second */
418       __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
419     }
420   }
421 }
422
423 /**
424   * @brief  Second event callback.
425   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
426   *                the configuration information for RTC.
427   * @retval None
428   */
429 __weak void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
430 {
431   /* NOTE : This function Should not be modified, when the callback is needed,
432             the HAL_RTCEx_RTCEventCallback could be implemented in the user file
433    */
434 }
435
436 /**
437   * @brief  Second event error callback.
438   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
439   *                the configuration information for RTC.
440   * @retval None
441   */
442 __weak void HAL_RTCEx_RTCEventErrorCallback(RTC_HandleTypeDef *hrtc)
443 {
444   /* NOTE : This function Should not be modified, when the callback is needed,
445             the HAL_RTCEx_RTCEventErrorCallback could be implemented in the user file
446    */
447 }
448
449 /**
450   * @}
451   */
452   
453 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
454   * @brief    Extended Peripheral Control functions
455   *
456 @verbatim   
457  ===============================================================================
458               ##### Extension Peripheral Control functions #####
459  ===============================================================================  
460     [..]
461     This subsection provides functions allowing to
462       (+) Writes a data in a specified RTC Backup data register
463       (+) Read a data in a specified RTC Backup data register
464       (+) Sets the Smooth calibration parameters.
465
466 @endverbatim
467   * @{
468   */
469
470 /**
471   * @brief  Writes a data in a specified RTC Backup data register.
472   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
473   *                the configuration information for RTC. 
474   * @param  BackupRegister: RTC Backup data Register number.
475   *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to 
476   *                                 specify the register (depending devices).
477   * @param  Data: Data to be written in the specified RTC Backup data register.                     
478   * @retval None
479   */
480 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
481 {
482   uint32_t tmp = 0;
483
484   /* Check the parameters */
485   assert_param(IS_RTC_BKP(BackupRegister));
486   
487   tmp = (uint32_t)BKP_BASE; 
488   tmp += (BackupRegister * 4);
489
490   *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
491 }
492
493 /**
494   * @brief  Reads data from the specified RTC Backup data Register.
495   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
496   *                the configuration information for RTC. 
497   * @param  BackupRegister: RTC Backup data Register number.
498   *          This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to 
499   *                                 specify the register (depending devices).
500   * @retval Read value
501   */
502 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
503 {
504   uint32_t backupregister = 0;
505   uint32_t pvalue = 0;
506
507   /* Check the parameters */
508   assert_param(IS_RTC_BKP(BackupRegister));
509
510   backupregister = (uint32_t)BKP_BASE; 
511   backupregister += (BackupRegister * 4);
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   
537   /* Check the parameters */
538   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
539   
540   /* Process Locked */
541   __HAL_LOCK(hrtc);
542
543   hrtc->State = HAL_RTC_STATE_BUSY;
544
545   /* Sets RTC Clock Calibration value.*/
546   MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
547
548   /* Change RTC state */
549   hrtc->State = HAL_RTC_STATE_READY;
550
551   /* Process Unlocked */
552   __HAL_UNLOCK(hrtc);
553
554   return HAL_OK;
555 }
556
557 /**
558   * @}
559   */
560
561 /**
562   * @}
563   */
564   
565 /**
566   * @}
567   */
568
569 #endif /* HAL_RTC_MODULE_ENABLED */
570
571 /**
572   * @}
573   */
574
575 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
576