Subversion Repositories NaviCtrl

Rev

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

Rev Author Line No. Line
1 ingob 1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2
* File Name          : 91x_rtc.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 05/18/2006 : Version 1.0
5
* Description        : This file provides the RTC library software functions
6
********************************************************************************
7
* History:
8
* 05/22/2007 : Version 1.2
9
* 05/24/2006 : Version 1.1
10
* 05/18/2006 : Version 1.0
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
13
* CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS
14
* A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
15
* OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
16
* OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
17
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
/* Includes ------------------------------------------------------------------*/
21
#include "91x_rtc.h"
22
#include "91x_scu.h"
23
 
24
/* Include of other module interface headers ---------------------------------*/
25
/* Local includes ------------------------------------------------------------*/
26
/* Private typedef -----------------------------------------------------------*/
27
/* Private define ------------------------------------------------------------*/
28
/* Private macro -------------------------------------------------------------*/
29
/* Private variables ---------------------------------------------------------*/
30
/* Private function prototypes -----------------------------------------------*/
31
u8 BYTEToBCD2(u8 value);
32
u16 WORDToBCD3(u16 value);
33
u8 BCD2ToBYTE(u8 value);
34
u16 BCD3ToBYTE(u16 value);
35
/* Interface functions -------------------------------------------------------*/
36
/* Private functions ---------------------------------------------------------*/
37
 
38
/*******************************************************************************
39
* Function Name  : BYTEToBCD2
40
* Description    : Converts a 2 digit decimal to BCD format
41
* Input          : None
42
* Output         : None
43
* Return         : Converted byte
44
*******************************************************************************/
45
u8 BYTEToBCD2(u8 value)
46
{
47
  u8 bcdhigh = 0;
48
  while (value >= 10)
49
  {
50
    bcdhigh++;
51
    value -= 10;
52
  }
53
  return  (bcdhigh << 4) | value;
54
}
55
/*******************************************************************************
56
* Function Name  : WORDToBCD3
57
* Description    : Converts a 3 digit decimal to BCD format
58
* Input          : None
59
* Output         : None
60
* Return         : Converted word
61
*******************************************************************************/
62
u16 WORDToBCD3(u16 value)
63
{
64
        u16 bcdhigh = 0;
65
        while (value >= 100)
66
        {
67
                bcdhigh++;
68
                value -= 100;
69
        }
70
        bcdhigh <<= 4;
71
        while (value >= 10)
72
        {
73
                bcdhigh++;
74
                value -= 10;
75
        }
76
        return  (bcdhigh << 4) | value;
77
}
78
 
79
/*******************************************************************************
80
* Function Name  : BCD3ToWORD
81
* Description    : convert from 3 digit BCD to Binary
82
* Input          : None
83
* Output         : None
84
* Return         : Converted word
85
*******************************************************************************/
86
u16 BCD3ToWORD(u16 value)
87
{
88
  return (u16)((((value&0xF00)>>8)*100) + (((value&0x0F0)>>4)*10) + (value&0x0F));
89
}
90
 
91
/*******************************************************************************
92
* Function Name  : BCD2ToBYTE
93
* Description    : convert from 2 digit BCD to Binary
94
* Input          : None
95
* Output         : None
96
* Return         : Converted word
97
*******************************************************************************/
98
u8 BCD2ToBYTE(u8 value)
99
{
100
  u32 tmp;
101
  tmp= ((value&0xF0)>>4)*10;
102
  return (u8)(tmp+ (value&0x0F));      
103
}
104
 
105
/*******************************************************************************
106
* Function Name  : RTC_DeInit
107
* Description    : Resets the RTC peripheral registers
108
* Input          : None
109
* Output         : None
110
* Return         : None
111
*******************************************************************************/
112
void RTC_DeInit(void)
113
{
114
        SCU_APBPeriphReset(__RTC,ENABLE);
115
        SCU_APBPeriphReset(__RTC,DISABLE);
116
}
117
 
118
/*******************************************************************************
119
* Function Name  : RTC_SetDate
120
* Description    : Sets the Date register
121
* Input          : struct of type RTC_DATE
122
* Output         : None
123
* Return         : None
124
*******************************************************************************/
125
void RTC_SetDate(RTC_DATE Date)
126
{
127
        u32 tmp = 0;
128
 
129
        RTC->CR |=0x80;  /*Enable write operation in DTR register*/
130
        RTC->DTR = 0;
131
        tmp = BYTEToBCD2(Date.century);
132
        RTC->DTR|=tmp<<24;
133
        tmp = BYTEToBCD2(Date.year);
134
        RTC->DTR|=tmp<<16;
135
        tmp = BYTEToBCD2(Date.month);
136
        RTC->DTR|=tmp<<8;
137
        tmp = BYTEToBCD2(Date.weekday);
138
        RTC->DTR|=tmp;
139
        RTC->TR &=0xFFFFFF;
140
        tmp = BYTEToBCD2(Date.day);
141
        RTC->TR|=tmp<<24;
142
        RTC->CR &=~0x80; /*Disable write operation in DTR register*/
143
}
144
/*******************************************************************************
145
* Function Name  : RTC_SetTime
146
* Description    : Sets the Time register
147
* Input          : struct of type RTC_TIME
148
* Output         : None
149
* Return         : None
150
*******************************************************************************/
151
void RTC_SetTime(RTC_TIME Time)
152
{
153
        u32 tmp = 0;
154
 
155
        RTC->CR |=0x80;  /*Enable write operation in TR register*/
156
        RTC->TR &= 0xFF000000;
157
        tmp = BYTEToBCD2(Time.hours);
158
        RTC->TR|=tmp<<16;
159
        tmp = BYTEToBCD2(Time.minutes);
160
        RTC->TR|=tmp<<8;
161
        tmp = BYTEToBCD2(Time.seconds);
162
        RTC->TR|=tmp;
163
        RTC->MILR = 0;
164
        RTC->MILR |= WORDToBCD3(Time.milliseconds);
165
        RTC->CR &=~0x80; /*Disable write operation in TR register*/
166
}
167
/*******************************************************************************
168
* Function Name  : RTC_SetAlarm
169
* Description    : Sets the Alarm register
170
* Input          : Struct of type RTC_ALARM
171
* Output         : Date
172
* Return         : None
173
*******************************************************************************/
174
void RTC_SetAlarm(RTC_ALARM Alarm)
175
{
176
        u32 tmp = 0;
177
 
178
        RTC->CR |=0x80;  /*Enable write operation in ATR register*/
179
        RTC->ATR = 0;
180
        tmp = BYTEToBCD2(Alarm.day);
181
        RTC->ATR|=tmp<<24;
182
        tmp = BYTEToBCD2(Alarm.hours);
183
        RTC->ATR|=tmp<<16;
184
        tmp = BYTEToBCD2(Alarm.minutes);
185
        RTC->ATR|=tmp<<8;
186
        tmp = BYTEToBCD2(Alarm.seconds);
187
        RTC->ATR|=tmp;
188
        RTC->CR &=~0x80; /*Disable write operation in ATR register*/
189
}
190
 
191
/*******************************************************************************
192
* Function Name  : RTC_GetDate
193
* Description    : Gets RTC date in BCD coded or BINARY code
194
* Input          : -Format: BCD or BINARY
195
*                  -Date: pointer to structure of type RTC_DATE to be filled by function
196
* Output         : None
197
* Return         : None
198
*******************************************************************************/
199
void RTC_GetDate(u8 Format, RTC_DATE * Date)
200
{
201
        Date->century = (u8)((RTC->DTR&0xFF000000)>>24);
202
        Date->year = (u8)((RTC->DTR&0x00FF0000)>>16);
203
        Date->month = (u8)((RTC->DTR&0x00001F00)>>8);
204
        Date->day = (u8)((RTC->TR&0x3F000000)>>24);
205
        Date->weekday = (u8)(RTC->DTR&0xF);
206
        if (Format == BINARY)
207
        {
208
                Date->century = BCD2ToBYTE(Date->century);
209
                Date->year = BCD2ToBYTE(Date->year);
210
                Date->month = BCD2ToBYTE(Date->month);
211
                Date->day = BCD2ToBYTE(Date->day);
212
                Date->weekday = BCD2ToBYTE(Date->weekday);
213
        }
214
}
215
 
216
/*******************************************************************************
217
* Function Name  : RTC_GetTime
218
* Description    : Gets TIME in BCD coded or BINARY code
219
* Input          : -Format: BCD or BINARY
220
*                  -Time : pointer to structure of type RTC_TIME to be filled by function
221
* Output         : Time
222
* Return         : None
223
*******************************************************************************/
224
void RTC_GetTime(u8 Format, RTC_TIME * Time)
225
{
226
 
227
        Time->hours = (u8)((RTC->TR&0x003F0000)>>16);
228
        Time->minutes = (u8)((RTC->TR&0x00007F00)>>8);
229
        Time->seconds = (u8)(RTC->TR&0x7F);
230
        Time->milliseconds =(u16)(RTC->MILR&0xFFF);
231
        if (Format == BINARY)
232
        {
233
                Time->hours = BCD2ToBYTE(Time->hours);
234
                Time->minutes = BCD2ToBYTE(Time->minutes);
235
                Time->seconds = BCD2ToBYTE(Time->seconds);
236
                Time->milliseconds = BCD3ToWORD(Time->milliseconds);
237
        }
238
}
239
 
240
 
241
/*******************************************************************************
242
* Function Name  : RTC_GetAlarm
243
* Description    : Gets the RTC Alarm in BCD or BINARY code
244
* Input          : -Format: BCD or BINARY
245
*                  -Alarm : pointer to structure of type RTC_ALARM to be filled by function
246
* Output         : Alarm
247
* Return         : None
248
*******************************************************************************/
249
void RTC_GetAlarm(u8 Format,RTC_ALARM * Alarm)
250
{
251
        Alarm->day = (u8)((RTC->ATR&0x3F000000)>>24);
252
        Alarm->hours = (u8)((RTC->ATR&0x003F0000)>>16);
253
        Alarm->minutes = (u8)((RTC->ATR&0x00007F00)>>8);
254
        Alarm->seconds = (u8)((RTC->ATR)&0x7F);
255
        if (Format == BINARY)
256
        {
257
                Alarm->day = BCD2ToBYTE(Alarm->day);
258
                Alarm->hours = BCD2ToBYTE(Alarm->hours);
259
                Alarm->minutes = BCD2ToBYTE(Alarm->minutes);
260
                Alarm->seconds = BCD2ToBYTE(Alarm->seconds);
261
        }
262
}
263
 
264
/*******************************************************************************
265
* Function Name  : RTC_TamperConfig
266
* Description    : configures the Tamper mode and tamper polarity
267
* Input          : -TamperMode: RTC_TamperMode_Edge or RTC_TamperMode_Level
268
*                  -TamperPol : RTC_TamperPol_Low or RTC_TamperMode_High
269
* Output         : None
270
* Return         : None
271
*******************************************************************************/
272
void RTC_TamperConfig(u32 TamperMode, u32 TamperPol)
273
{
274
        RTC->CR&=RTC_TamperMode_Edge;
275
        if (TamperMode!=RTC_TamperMode_Edge)
276
        RTC->CR|=RTC_TamperMode_Level;
277
 
278
        RTC->CR&=RTC_TamperPol_Low;
279
        if (TamperPol!=RTC_TamperPol_Low)
280
        RTC->CR|=RTC_TamperPol_High;
281
}
282
 
283
/*******************************************************************************
284
* Function Name  : RTC_TamperCmd
285
* Description    : Enable or Disable Tamper
286
* Input          : NewState: ENABLE or DISABLE
287
* Output         : None
288
* Return         : None
289
*******************************************************************************/
290
void RTC_TamperCmd(FunctionalState NewState)
291
{
292
        RTC->CR&=0xFFFFFFFE;
293
        if (NewState==ENABLE)
294
        RTC->CR|=0x1;
295
}
296
 
297
/*******************************************************************************
298
* Function Name  : RTC_AlarmCmd
299
* Description    : Enable or Disable Alarm
300
* Input          : NewState: ENABLE or DISABLE
301
* Output         : None
302
* Return         : None
303
*******************************************************************************/
304
void RTC_AlarmCmd(FunctionalState NewState)
305
{
306
        RTC->CR&=~0x100000;
307
        if (NewState==ENABLE)
308
        RTC->CR|=0x100000;
309
}
310
 
311
/*******************************************************************************
312
* Function Name  : RTC_CalibClockCmd
313
* Description    : Enable or Disable RTC Calibration Clock Output
314
* Input          : NewState: ENABLE or DISABLE
315
* Output         : None
316
* Return         : None
317
*******************************************************************************/
318
void RTC_CalibClockCmd(FunctionalState NewState)
319
{
320
        RTC->CR&=~0x40;
321
        if (NewState ==ENABLE)
322
        RTC->CR|=0x40;
323
}
324
 
325
/*******************************************************************************
326
* Function Name  : SRAMBattPowerCmd
327
* Description    : Enable or Disable SRAM backup Power by VBATT
328
* Input          : NewState : ENABLE or DISABLE
329
* Output         : None
330
* Return         : None
331
*******************************************************************************/
332
void RTC_SRAMBattPowerCmd(FunctionalState NewState)
333
{
334
        RTC->CR&=~0x8;
335
        if (NewState ==ENABLE)
336
        RTC->CR|=0x8;
337
}
338
 
339
/*******************************************************************************
340
* Function Name  : RTC_PeridicIntConfig
341
* Description    : Select a Periodic CLock
342
* Input          : PeriodicClock
343
* Output         : None
344
* Return         : None
345
* Note           : When PeriodicClock = RTC_Per_DISABLE the Periodic clock generation
346
*                  will be disabled.
347
*******************************************************************************/
348
void RTC_PeriodicIntConfig(u32 PeriodicClock)
349
{
350
        RTC->CR &=~0xF0000;
351
        RTC->CR|=PeriodicClock;
352
}
353
 
354
/*******************************************************************************
355
* Function Name  : RTC_ITConfig
356
* Description    : Enable or Disable an interrupt
357
* Input          : -RTC_IT : RTC interrupt
358
*                  -Newstate: Enable or Disable
359
* Output         : None
360
* Return         : None
361
*******************************************************************************/
362
void RTC_ITConfig(u32 RTC_IT, FunctionalState NewState)
363
{
364
        RTC->CR&=~RTC_IT;
365
        if (NewState==ENABLE)
366
        RTC->CR|=RTC_IT;
367
}
368
 
369
/*******************************************************************************
370
* Function Name  : RTC_GetFlagStatus
371
* Description    : Gets a RTC flag status
372
* Input          : RTC_FLAG
373
* Output         : None
374
* Return         : FlagStatus :SET or RESET
375
*******************************************************************************/
376
FlagStatus RTC_GetFlagStatus(u32 RTC_FLAG)
377
{
378
        if (RTC->SR&RTC_FLAG) return SET;
379
        else return RESET;
380
}
381
 
382
/*******************************************************************************
383
* Function Name  : RTC_ClearFlag
384
* Description    : Clears a RTC flag
385
* Input          : RTC_FLAG
386
* Output         : None
387
* Return         : None
388
* Note           : Before clearing the RTC Periodic Flag you need to disable the
389
*                  Periodic interrupt generation, to do this use function
390
*                  RTC_PeriodicIntConfig(RTC_Per_DISABLE)
391
*******************************************************************************/
392
void RTC_ClearFlag(u32 RTC_FLAG)
393
{
394
  vu32 tmp=0;
395
  if (RTC_FLAG == RTC_FLAG_Per)  tmp=RTC->SR;
396
  else if (RTC_FLAG == RTC_FLAG_Alarm) RTC->CR&=~0x100000;
397
  else if (RTC_FLAG == RTC_FLAG_Tamper) RTC->CR&=~0x1;
398
}
399
 
400
 
401
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/