Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f0xx_ll_rtc.c
4
  * @author  MCD Application Team
5
  * @brief   RTC LL module driver.
6
  ******************************************************************************
7
  * @attention
8
  *
9
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
10
  * All rights reserved.</center></h2>
11
  *
12
  * This software component is licensed by ST under BSD 3-Clause license,
13
  * the "License"; You may not use this file except in compliance with the
14
  * License. You may obtain a copy of the License at:
15
  *                        opensource.org/licenses/BSD-3-Clause
16
  *
17
  ******************************************************************************
18
  */
19
 
20
#if defined(USE_FULL_LL_DRIVER)
21
 
22
/* Includes ------------------------------------------------------------------*/
23
#include "stm32f0xx_ll_rtc.h"
24
#include "stm32f0xx_ll_cortex.h"
25
#ifdef  USE_FULL_ASSERT
26
#include "stm32_assert.h"
27
#else
28
#define assert_param(expr) ((void)0U)
29
#endif
30
 
31
/** @addtogroup STM32F0xx_LL_Driver
32
  * @{
33
  */
34
 
35
#if defined(RTC)
36
 
37
/** @addtogroup RTC_LL
38
  * @{
39
  */
40
 
41
/* Private types -------------------------------------------------------------*/
42
/* Private variables ---------------------------------------------------------*/
43
/* Private constants ---------------------------------------------------------*/
44
/** @addtogroup RTC_LL_Private_Constants
45
  * @{
46
  */
47
/* Default values used for prescaler */
48
#define RTC_ASYNCH_PRESC_DEFAULT     0x0000007FU
49
#define RTC_SYNCH_PRESC_DEFAULT      0x000000FFU
50
 
51
/* Values used for timeout */
52
#define RTC_INITMODE_TIMEOUT         1000U /* 1s when tick set to 1ms */
53
#define RTC_SYNCHRO_TIMEOUT          1000U /* 1s when tick set to 1ms */
54
/**
55
  * @}
56
  */
57
 
58
/* Private macros ------------------------------------------------------------*/
59
/** @addtogroup RTC_LL_Private_Macros
60
  * @{
61
  */
62
 
63
#define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
64
                                      || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
65
 
66
#define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
67
 
68
#define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x7FFFU)
69
 
70
#define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
71
                                  || ((__VALUE__) == LL_RTC_FORMAT_BCD))
72
 
73
#define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
74
                                       || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
75
 
76
#define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
77
#define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
78
#define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
79
#define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
80
 
81
#define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
82
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
83
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
84
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
85
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
86
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
87
                                   || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
88
 
89
#define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
90
 
91
#define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
92
                                 || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
93
                                 || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
94
                                 || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
95
                                 || ((__VALUE__) == LL_RTC_MONTH_MAY) \
96
                                 || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
97
                                 || ((__VALUE__) == LL_RTC_MONTH_JULY) \
98
                                 || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
99
                                 || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
100
                                 || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
101
                                 || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
102
                                 || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
103
 
104
#define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
105
 
106
#define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
107
                                     || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
108
                                     || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
109
                                     || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
110
                                     || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
111
                                     || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
112
 
113
 
114
#define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
115
                                                  ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
116
 
117
 
118
/**
119
  * @}
120
  */
121
/* Private function prototypes -----------------------------------------------*/
122
/* Exported functions --------------------------------------------------------*/
123
/** @addtogroup RTC_LL_Exported_Functions
124
  * @{
125
  */
126
 
127
/** @addtogroup RTC_LL_EF_Init
128
  * @{
129
  */
130
 
131
/**
132
  * @brief  De-Initializes the RTC registers to their default reset values.
133
  * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
134
  *         registers.
135
  * @param  RTCx RTC Instance
136
  * @retval An ErrorStatus enumeration value:
137
  *          - SUCCESS: RTC registers are de-initialized
138
  *          - ERROR: RTC registers are not de-initialized
139
  */
140
ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
141
{
142
  ErrorStatus status = ERROR;
143
 
144
  /* Check the parameter */
145
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
146
 
147
  /* Disable the write protection for RTC registers */
148
  LL_RTC_DisableWriteProtection(RTCx);
149
 
150
  /* Set Initialization mode */
151
  if (LL_RTC_EnterInitMode(RTCx) != ERROR)
152
  {
153
    /* Reset TR, DR and CR registers */
154
    LL_RTC_WriteReg(RTCx, TR,       0x00000000U);
155
#if defined(RTC_WAKEUP_SUPPORT)
156
    LL_RTC_WriteReg(RTCx, WUTR,     RTC_WUTR_WUT);
157
#endif /* RTC_WAKEUP_SUPPORT */
158
    LL_RTC_WriteReg(RTCx, DR, (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
159
    /* Reset All CR bits except CR[2:0] */
160
#if defined(RTC_WAKEUP_SUPPORT)
161
    LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
162
#else
163
    LL_RTC_WriteReg(RTCx, CR, 0x00000000U);
164
#endif /* RTC_WAKEUP_SUPPORT */
165
    LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
166
    LL_RTC_WriteReg(RTCx, ALRMAR,   0x00000000U);
167
    LL_RTC_WriteReg(RTCx, SHIFTR,   0x00000000U);
168
    LL_RTC_WriteReg(RTCx, CALR,     0x00000000U);
169
    LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U);
170
 
171
    /* Reset ISR register and exit initialization mode */
172
    LL_RTC_WriteReg(RTCx, ISR,      0x00000000U);
173
 
174
    /* Reset Tamper and alternate functions configuration register */
175
    LL_RTC_WriteReg(RTCx, TAFCR, 0x00000000U);
176
 
177
    /* Wait till the RTC RSF flag is set */
178
    status = LL_RTC_WaitForSynchro(RTCx);
179
  }
180
 
181
  /* Enable the write protection for RTC registers */
182
  LL_RTC_EnableWriteProtection(RTCx);
183
 
184
  return status;
185
}
186
 
187
/**
188
  * @brief  Initializes the RTC registers according to the specified parameters
189
  *         in RTC_InitStruct.
190
  * @param  RTCx RTC Instance
191
  * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
192
  *         the configuration information for the RTC peripheral.
193
  * @note   The RTC Prescaler register is write protected and can be written in
194
  *         initialization mode only.
195
  * @retval An ErrorStatus enumeration value:
196
  *          - SUCCESS: RTC registers are initialized
197
  *          - ERROR: RTC registers are not initialized
198
  */
199
ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
200
{
201
  ErrorStatus status = ERROR;
202
 
203
  /* Check the parameters */
204
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
205
  assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
206
  assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
207
  assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
208
 
209
  /* Disable the write protection for RTC registers */
210
  LL_RTC_DisableWriteProtection(RTCx);
211
 
212
  /* Set Initialization mode */
213
  if (LL_RTC_EnterInitMode(RTCx) != ERROR)
214
  {
215
    /* Set Hour Format */
216
    LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
217
 
218
    /* Configure Synchronous and Asynchronous prescaler factor */
219
    LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
220
    LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
221
 
222
    /* Exit Initialization mode */
223
    LL_RTC_DisableInitMode(RTCx);
224
 
225
    status = SUCCESS;
226
  }
227
  /* Enable the write protection for RTC registers */
228
  LL_RTC_EnableWriteProtection(RTCx);
229
 
230
  return status;
231
}
232
 
233
/**
234
  * @brief  Set each @ref LL_RTC_InitTypeDef field to default value.
235
  * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
236
  * @retval None
237
  */
238
void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
239
{
240
  /* Set RTC_InitStruct fields to default values */
241
  RTC_InitStruct->HourFormat      = LL_RTC_HOURFORMAT_24HOUR;
242
  RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
243
  RTC_InitStruct->SynchPrescaler  = RTC_SYNCH_PRESC_DEFAULT;
244
}
245
 
246
/**
247
  * @brief  Set the RTC current time.
248
  * @param  RTCx RTC Instance
249
  * @param  RTC_Format This parameter can be one of the following values:
250
  *         @arg @ref LL_RTC_FORMAT_BIN
251
  *         @arg @ref LL_RTC_FORMAT_BCD
252
  * @param  RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
253
  *                        the time configuration information for the RTC.
254
  * @retval An ErrorStatus enumeration value:
255
  *          - SUCCESS: RTC Time register is configured
256
  *          - ERROR: RTC Time register is not configured
257
  */
258
ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
259
{
260
  ErrorStatus status = ERROR;
261
 
262
  /* Check the parameters */
263
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
264
  assert_param(IS_LL_RTC_FORMAT(RTC_Format));
265
 
266
  if (RTC_Format == LL_RTC_FORMAT_BIN)
267
  {
268
    if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
269
    {
270
      assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
271
      assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
272
    }
273
    else
274
    {
275
      RTC_TimeStruct->TimeFormat = 0x00U;
276
      assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
277
    }
278
    assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
279
    assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
280
  }
281
  else
282
  {
283
    if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
284
    {
285
      assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
286
      assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
287
    }
288
    else
289
    {
290
      RTC_TimeStruct->TimeFormat = 0x00U;
291
      assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
292
    }
293
    assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
294
    assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
295
  }
296
 
297
  /* Disable the write protection for RTC registers */
298
  LL_RTC_DisableWriteProtection(RTCx);
299
 
300
  /* Set Initialization mode */
301
  if (LL_RTC_EnterInitMode(RTCx) != ERROR)
302
  {
303
    /* Check the input parameters format */
304
    if (RTC_Format != LL_RTC_FORMAT_BIN)
305
    {
306
      LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
307
                         RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
308
    }
309
    else
310
    {
311
      LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
312
                         __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
313
                         __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
314
    }
315
 
316
    /* Exit Initialization mode */
317
    LL_RTC_DisableInitMode(RTC);
318
 
319
    /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
320
    if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
321
    {
322
      status = LL_RTC_WaitForSynchro(RTCx);
323
    }
324
    else
325
    {
326
      status = SUCCESS;
327
    }
328
  }
329
  /* Enable the write protection for RTC registers */
330
  LL_RTC_EnableWriteProtection(RTCx);
331
 
332
  return status;
333
}
334
 
335
/**
336
  * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
337
  * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
338
  * @retval None
339
  */
340
void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
341
{
342
  /* Time = 00h:00min:00sec */
343
  RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
344
  RTC_TimeStruct->Hours      = 0U;
345
  RTC_TimeStruct->Minutes    = 0U;
346
  RTC_TimeStruct->Seconds    = 0U;
347
}
348
 
349
/**
350
  * @brief  Set the RTC current date.
351
  * @param  RTCx RTC Instance
352
  * @param  RTC_Format This parameter can be one of the following values:
353
  *         @arg @ref LL_RTC_FORMAT_BIN
354
  *         @arg @ref LL_RTC_FORMAT_BCD
355
  * @param  RTC_DateStruct pointer to a RTC_DateTypeDef structure that contains
356
  *                         the date configuration information for the RTC.
357
  * @retval An ErrorStatus enumeration value:
358
  *          - SUCCESS: RTC Day register is configured
359
  *          - ERROR: RTC Day register is not configured
360
  */
361
ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
362
{
363
  ErrorStatus status = ERROR;
364
 
365
  /* Check the parameters */
366
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
367
  assert_param(IS_LL_RTC_FORMAT(RTC_Format));
368
 
369
  if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
370
  {
371
    RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU;
372
  }
373
  if (RTC_Format == LL_RTC_FORMAT_BIN)
374
  {
375
    assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
376
    assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
377
    assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
378
  }
379
  else
380
  {
381
    assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
382
    assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
383
    assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
384
  }
385
  assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
386
 
387
  /* Disable the write protection for RTC registers */
388
  LL_RTC_DisableWriteProtection(RTCx);
389
 
390
  /* Set Initialization mode */
391
  if (LL_RTC_EnterInitMode(RTCx) != ERROR)
392
  {
393
    /* Check the input parameters format */
394
    if (RTC_Format != LL_RTC_FORMAT_BIN)
395
    {
396
      LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
397
    }
398
    else
399
    {
400
      LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
401
                         __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
402
    }
403
 
404
    /* Exit Initialization mode */
405
    LL_RTC_DisableInitMode(RTC);
406
 
407
    /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
408
    if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
409
    {
410
      status = LL_RTC_WaitForSynchro(RTCx);
411
    }
412
    else
413
    {
414
      status = SUCCESS;
415
    }
416
  }
417
  /* Enable the write protection for RTC registers */
418
  LL_RTC_EnableWriteProtection(RTCx);
419
 
420
  return status;
421
}
422
 
423
/**
424
  * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
425
  * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
426
  * @retval None
427
  */
428
void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
429
{
430
  /* Monday, January 01 xx00 */
431
  RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
432
  RTC_DateStruct->Day     = 1U;
433
  RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
434
  RTC_DateStruct->Year    = 0U;
435
}
436
 
437
/**
438
  * @brief  Set the RTC Alarm A.
439
  * @note   The Alarm register can only be written when the corresponding Alarm
440
  *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
441
  * @param  RTCx RTC Instance
442
  * @param  RTC_Format This parameter can be one of the following values:
443
  *         @arg @ref LL_RTC_FORMAT_BIN
444
  *         @arg @ref LL_RTC_FORMAT_BCD
445
  * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
446
  *                         contains the alarm configuration parameters.
447
  * @retval An ErrorStatus enumeration value:
448
  *          - SUCCESS: ALARMA registers are configured
449
  *          - ERROR: ALARMA registers are not configured
450
  */
451
ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
452
{
453
  /* Check the parameters */
454
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
455
  assert_param(IS_LL_RTC_FORMAT(RTC_Format));
456
  assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
457
  assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
458
 
459
  if (RTC_Format == LL_RTC_FORMAT_BIN)
460
  {
461
    if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
462
    {
463
      assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
464
      assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
465
    }
466
    else
467
    {
468
      RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
469
      assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
470
    }
471
    assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
472
    assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
473
 
474
    if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
475
    {
476
      assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
477
    }
478
    else
479
    {
480
      assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
481
    }
482
  }
483
  else
484
  {
485
    if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
486
    {
487
      assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
488
      assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
489
    }
490
    else
491
    {
492
      RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
493
      assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
494
    }
495
 
496
    assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
497
    assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
498
 
499
    if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
500
    {
501
      assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
502
    }
503
    else
504
    {
505
      assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
506
    }
507
  }
508
 
509
  /* Disable the write protection for RTC registers */
510
  LL_RTC_DisableWriteProtection(RTCx);
511
 
512
  /* Select weekday selection */
513
  if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
514
  {
515
    /* Set the date for ALARM */
516
    LL_RTC_ALMA_DisableWeekday(RTCx);
517
    if (RTC_Format != LL_RTC_FORMAT_BIN)
518
    {
519
      LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
520
    }
521
    else
522
    {
523
      LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
524
    }
525
  }
526
  else
527
  {
528
    /* Set the week day for ALARM */
529
    LL_RTC_ALMA_EnableWeekday(RTCx);
530
    LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
531
  }
532
 
533
  /* Configure the Alarm register */
534
  if (RTC_Format != LL_RTC_FORMAT_BIN)
535
  {
536
    LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
537
                           RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
538
  }
539
  else
540
  {
541
    LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
542
                           __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
543
                           __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
544
                           __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
545
  }
546
  /* Set ALARM mask */
547
  LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
548
 
549
  /* Enable the write protection for RTC registers */
550
  LL_RTC_EnableWriteProtection(RTCx);
551
 
552
  return SUCCESS;
553
}
554
 
555
/**
556
  * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
557
  *         Day = 1st day of the month/Mask = all fields are masked).
558
  * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
559
  * @retval None
560
  */
561
void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
562
{
563
  /* Alarm Time Settings : Time = 00h:00mn:00sec */
564
  RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
565
  RTC_AlarmStruct->AlarmTime.Hours      = 0U;
566
  RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
567
  RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
568
 
569
  /* Alarm Day Settings : Day = 1st day of the month */
570
  RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
571
  RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
572
 
573
  /* Alarm Masks Settings : Mask =  all fields are not masked */
574
  RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
575
}
576
 
577
/**
578
  * @brief  Enters the RTC Initialization mode.
579
  * @note   The RTC Initialization mode is write protected, use the
580
  *         @ref LL_RTC_DisableWriteProtection before calling this function.
581
  * @param  RTCx RTC Instance
582
  * @retval An ErrorStatus enumeration value:
583
  *          - SUCCESS: RTC is in Init mode
584
  *          - ERROR: RTC is not in Init mode
585
  */
586
ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
587
{
588
  __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
589
  ErrorStatus status = SUCCESS;
590
  uint32_t tmp = 0U;
591
 
592
  /* Check the parameter */
593
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
594
 
595
  /* Check if the Initialization mode is set */
596
  if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
597
  {
598
    /* Set the Initialization mode */
599
    LL_RTC_EnableInitMode(RTCx);
600
 
601
    /* Wait till RTC is in INIT state and if Time out is reached exit */
602
    tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
603
    while ((timeout != 0U) && (tmp != 1U))
604
    {
605
      if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
606
      {
607
        timeout --;
608
      }
609
      tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
610
      if (timeout == 0U)
611
      {
612
        status = ERROR;
613
      }
614
    }
615
  }
616
  return status;
617
}
618
 
619
/**
620
  * @brief  Exit the RTC Initialization mode.
621
  * @note   When the initialization sequence is complete, the calendar restarts
622
  *         counting after 4 RTCCLK cycles.
623
  * @note   The RTC Initialization mode is write protected, use the
624
  *         @ref LL_RTC_DisableWriteProtection before calling this function.
625
  * @param  RTCx RTC Instance
626
  * @retval An ErrorStatus enumeration value:
627
  *          - SUCCESS: RTC exited from in Init mode
628
  *          - ERROR: Not applicable
629
  */
630
ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
631
{
632
  /* Check the parameter */
633
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
634
 
635
  /* Disable initialization mode */
636
  LL_RTC_DisableInitMode(RTCx);
637
 
638
  return SUCCESS;
639
}
640
 
641
/**
642
  * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
643
  *         synchronized with RTC APB clock.
644
  * @note   The RTC Resynchronization mode is write protected, use the
645
  *         @ref LL_RTC_DisableWriteProtection before calling this function.
646
  * @note   To read the calendar through the shadow registers after Calendar
647
  *         initialization, calendar update or after wakeup from low power modes
648
  *         the software must first clear the RSF flag.
649
  *         The software must then wait until it is set again before reading
650
  *         the calendar, which means that the calendar registers have been
651
  *         correctly copied into the RTC_TR and RTC_DR shadow registers.
652
  * @param  RTCx RTC Instance
653
  * @retval An ErrorStatus enumeration value:
654
  *          - SUCCESS: RTC registers are synchronised
655
  *          - ERROR: RTC registers are not synchronised
656
  */
657
ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
658
{
659
  __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
660
  ErrorStatus status = SUCCESS;
661
  uint32_t tmp = 0U;
662
 
663
  /* Check the parameter */
664
  assert_param(IS_RTC_ALL_INSTANCE(RTCx));
665
 
666
  /* Clear RSF flag */
667
  LL_RTC_ClearFlag_RS(RTCx);
668
 
669
  /* Wait the registers to be synchronised */
670
  tmp = LL_RTC_IsActiveFlag_RS(RTCx);
671
  while ((timeout != 0U) && (tmp != 0U))
672
  {
673
    if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
674
    {
675
      timeout--;
676
    }
677
    tmp = LL_RTC_IsActiveFlag_RS(RTCx);
678
    if (timeout == 0U)
679
    {
680
      status = ERROR;
681
    }
682
  }
683
 
684
  if (status != ERROR)
685
  {
686
    timeout = RTC_SYNCHRO_TIMEOUT;
687
    tmp = LL_RTC_IsActiveFlag_RS(RTCx);
688
    while ((timeout != 0U) && (tmp != 1U))
689
    {
690
      if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
691
      {
692
        timeout--;
693
      }
694
      tmp = LL_RTC_IsActiveFlag_RS(RTCx);
695
      if (timeout == 0U)
696
      {
697
        status = ERROR;
698
      }
699
    }
700
  }
701
 
702
  return (status);
703
}
704
 
705
/**
706
  * @}
707
  */
708
 
709
/**
710
  * @}
711
  */
712
 
713
/**
714
  * @}
715
  */
716
 
717
#endif /* defined(RTC) */
718
 
719
/**
720
  * @}
721
  */
722
 
723
#endif /* USE_FULL_LL_DRIVER */
724
 
725
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/