summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_blinky_complex_startup/src/91x_rtc.c
blob: 27e5579dace5a12674dceb6606a323d3c071088e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
316
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_rtc.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides the RTC library software functions
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
* CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS
* A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
* OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_rtc.h"
#include "91x_scu.h"

/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
u8 BYTEToBCD2(u8 value);
u16 WORDToBCD3(u16 value);
u8 BCD2ToBYTE(u8 value);
u16 BCD3ToBYTE(u16 value);
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : BYTEToBCD2
* Description    : Converts a 2 digit decimal to BCD format
* Input          : None
* Output         : None
* Return         : Converted byte
*******************************************************************************/
u8 BYTEToBCD2(u8 value)
{
  u8 bcdhigh = 0;
  while (value >= 10)
  {
    bcdhigh++;
    value -= 10;
  }
  return  (bcdhigh << 4) | value;
}
/*******************************************************************************
* Function Name  : WORDToBCD3
* Description    : Converts a 3 digit decimal to BCD format
* Input          : None
* Output         : None
* Return         : Converted word
*******************************************************************************/
u16 WORDToBCD3(u16 value)
{
	u16 bcdhigh = 0;
	while (value >= 100)
	{
		bcdhigh++;
		value -= 100;
	}
	bcdhigh <<= 4;
	while (value >= 10)
	{
		bcdhigh++;
		value -= 10;
	}
	return  (bcdhigh << 4) | value;
}

/*******************************************************************************
* Function Name  : BCD3ToWORD
* Description    : convert from 3 digit BCD to Binary
* Input          : None
* Output         : None
* Return         : Converted word
*******************************************************************************/
u16 BCD3ToWORD(u16 value)
{
  return (u16)((((value&0xF00)>>8)*100) + (((value&0x0F0)>>4)*10) + (value&0x0F));
}

/*******************************************************************************
* Function Name  : BCD2ToBYTE
* Description    : convert from 2 digit BCD to Binary
* Input          : None
* Output         : None
* Return         : Converted word
*******************************************************************************/
u8 BCD2ToBYTE(u8 value)
{
  u32 tmp;
  tmp= ((value&0xF0)>>4)*10;
  return (u8)(tmp+ (value&0x0F));	
}

/*******************************************************************************
* Function Name  : RTC_DeInit
* Description    : Resets the RTC peripheral registers
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_DeInit(void)
{
	SCU_APBPeriphReset(__RTC,ENABLE);
	SCU_APBPeriphReset(__RTC,DISABLE);
}

/*******************************************************************************
* Function Name  : RTC_SetDate
* Description    : Sets the Date register
* Input          : struct of type RTC_DATE
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_SetDate(RTC_DATE Date)
{
	u32 tmp = 0;
	
	RTC->CR |=0x80;  /*Enable write operation in DTR register*/
	RTC->DTR = 0;
	tmp = BYTEToBCD2(Date.century);
	RTC->DTR|=tmp<<24;
	tmp = BYTEToBCD2(Date.year);
	RTC->DTR|=tmp<<16;
	tmp = BYTEToBCD2(Date.month);
	RTC->DTR|=tmp<<8;
	tmp = BYTEToBCD2(Date.weekday);
	RTC->DTR|=tmp;
  RTC->TR &=0xFFFFFF;
  tmp = BYTEToBCD2(Date.day);
	RTC->TR|=tmp<<24;
	RTC->CR &=~0x80; /*Disable write operation in DTR register*/
}
/*******************************************************************************
* Function Name  : RTC_SetTime
* Description    : Sets the Time register
* Input          : struct of type RTC_TIME
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_SetTime(RTC_TIME Time)
{
  u32 tmp = 0;
	
	RTC->CR |=0x80;  /*Enable write operation in TR register*/
	RTC->TR &= 0xFF000000;
	tmp = BYTEToBCD2(Time.hours);
	RTC->TR|=tmp<<16;
	tmp = BYTEToBCD2(Time.minutes);
	RTC->TR|=tmp<<8;
	tmp = BYTEToBCD2(Time.seconds);
	RTC->TR|=tmp;
  RTC->MILR = 0;
	RTC->MILR |= WORDToBCD3(Time.milliseconds);
	RTC->CR &=~0x80; /*Disable write operation in TR register*/
}
/*******************************************************************************
* Function Name  : RTC_SetAlarm
* Description    : Sets the Alarm register
* Input          : Struct of type RTC_ALARM
* Output         : Date
* Return         : None
*******************************************************************************/
void RTC_SetAlarm(RTC_ALARM Alarm)
{
	u32 tmp = 0;

  RTC->CR |=0x80;  /*Enable write operation in ATR register*/
  RTC->ATR = 0;
	tmp = BYTEToBCD2(Alarm.day);
	RTC->ATR|=tmp<<24;
	tmp = BYTEToBCD2(Alarm.hours);
	RTC->ATR|=tmp<<16;
	tmp = BYTEToBCD2(Alarm.minutes);
	RTC->ATR|=tmp<<8;
	tmp = BYTEToBCD2(Alarm.seconds);
	RTC->ATR|=tmp;
	RTC->CR &=~0x80; /*Disable write operation in ATR register*/
}

/*******************************************************************************
* Function Name  : RTC_GetDate
* Description    : Gets RTC date in BCD coded or BINARY code
* Input          : -Format: BCD or BINARY
*                  -Date: pointer to structure of type RTC_DATE to be filled by function
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_GetDate(u8 Format, RTC_DATE * Date)
{
	Date->century = (u8)((RTC->DTR&0xFF000000)>>24);
	Date->year = (u8)((RTC->DTR&0x00FF0000)>>16);
	Date->month = (u8)((RTC->DTR&0x00001F00)>>8);
  Date->day = (u8)((RTC->TR&0x3F000000)>>24);
	Date->weekday = (u8)(RTC->DTR&0xF);
	if (Format == BINARY)
	{
		Date->century = BCD2ToBYTE(Date->century);
		Date->year = BCD2ToBYTE(Date->year);
		Date->month = BCD2ToBYTE(Date->month);
    Date->day = BCD2ToBYTE(Date->day);
		Date->weekday = BCD2ToBYTE(Date->weekday);
	}
}

/*******************************************************************************
* Function Name  : RTC_GetTime
* Description    : Gets TIME in BCD coded or BINARY code
* Input          : -Format: BCD or BINARY
*                  -Time : pointer to structure of type RTC_TIME to be filled by function
* Output         : Time
* Return         : None
*******************************************************************************/
void RTC_GetTime(u8 Format, RTC_TIME * Time)
{
	
	Time->hours = (u8)((RTC->TR&0x003F0000)>>16);
	Time->minutes = (u8)((RTC->TR&0x00007F00)>>8);
	Time->seconds = (u8)(RTC->TR&0x7F);
  Time->milliseconds =(u16)(RTC->MILR&0xFFF);
	if (Format == BINARY)
	{
		Time->hours = BCD2ToBYTE(Time->hours);
		Time->minutes = BCD2ToBYTE(Time->minutes);
    Time->seconds = BCD2ToBYTE(Time->seconds);
    Time->seconds = BCD3ToWORD(Time->milliseconds);
	}
}


/*******************************************************************************
* Function Name  : RTC_GetAlarm
* Description    : Gets the RTC Alarm in BCD or BINARY code
* Input          : -Format: BCD or BINARY
*                  -Alarm : pointer to structure of type RTC_ALARM to be filled by function
* Output         : Alarm
* Return         : None
*******************************************************************************/
void RTC_GetAlarm(u8 Format,RTC_ALARM * Alarm)
{
  Alarm->day = (u8)((RTC->ATR&0x3F000000)>>24);
	Alarm->hours = (u8)((RTC->ATR&0x003F0000)>>16);
	Alarm->minutes = (u8)((RTC->ATR&0x00007F00)>>8);
	Alarm->seconds = (u8)((RTC->ATR)&0x7F);
	if (Format == BINARY)
	{
		Alarm->day = BCD2ToBYTE(Alarm->day);
		Alarm->hours = BCD2ToBYTE(Alarm->hours);
		Alarm->minutes = BCD2ToBYTE(Alarm->minutes);
		Alarm->seconds = BCD2ToBYTE(Alarm->seconds);
	}
}

/*******************************************************************************
* Function Name  : RTC_TamperConfig
* Description    : configures the Tamper mode and tamper polarity
* Input          : -TamperMode: RTC_TamperMode_Edge or RTC_TamperMode_Level
*                  -TamperPol : RTC_TamperPol_Low or RTC_TamperMode_High
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_TamperConfig(u32 TamperMode, u32 TamperPol)
{
	RTC->CR&=RTC_TamperMode_Edge;
	if (TamperMode!=RTC_TamperMode_Edge)
	RTC->CR|=RTC_TamperMode_Level;
	
	RTC->CR&=RTC_TamperPol_Low;
	if (TamperPol!=RTC_TamperPol_Low)
	RTC->CR|=RTC_TamperPol_High;
}

/*******************************************************************************
* Function Name  : RTC_TamperCmd
* Description    : Enable or Disable Tamper
* Input          : NewState: ENABLE or DISABLE
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_TamperCmd(FunctionalState NewState)
{
	RTC->CR&=0xFFFFFFFE;
	if (NewState==ENABLE)
	RTC->CR|=0x1;
}

/*******************************************************************************
* Function Name  : RTC_AlarmCmd
* Description    : Enable or Disable Alarm
* Input          : NewState: ENABLE or DISABLE
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_AlarmCmd(FunctionalState NewState)
{
	RTC->CR&=~0x100000;
	if (NewState==ENABLE)
	RTC->CR|=0x100000;
}

/*******************************************************************************
* Function Name  : RTC_CalibClockCmd
* Description    : Enable or Disable RTC Calibration Clock Output
* Input          : NewState: ENABLE or DISABLE
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_CalibClockCmd(FunctionalState NewState)
{
	RTC->CR&=~0x40;
	if (NewState ==ENABLE)
	RTC->CR|=0x40;
}

/*******************************************************************************
* Function Name  : SRAMBattPowerCmd
* Description    : Enable or Disable SRAM backup Power by VBATT
* Input          : NewState : ENABLE or DISABLE
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_SRAMBattPowerCmd(FunctionalState NewState)
{
	RTC->CR&=~0x8;
	if (NewState ==ENABLE)
	RTC->CR|=0x8;
}

/*******************************************************************************
* Function Name  : RTC_PeridicIntConfig
* Description    : Select a Periodic CLock
* Input          : PeriodicClock
* Output         : None
* Return         : None
* Note           : When PeriodicClock = RTC_Per_DISABLE the Periodic clock generation
*                  will be disabled.
*******************************************************************************/
void RTC_PeriodicIntConfig(u32 PeriodicClock)
{
	RTC->CR &=~0xF0000;
	RTC->CR|=PeriodicClock;
}

/*******************************************************************************
* Function Name  : RTC_ITConfig
* Description    : Enable or Disable an interrupt
* Input          : -RTC_IT : RTC interrupt
*                  -Newstate: Enable or Disable
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_ITConfig(u32 RTC_IT, FunctionalState NewState)
{
	RTC->CR&=~RTC_IT;
	if (NewState==ENABLE)
	RTC->CR|=RTC_IT;
}

/*******************************************************************************
* Function Name  : RTC_GetFlagStatus
* Description    : Gets a RTC flag status 
* Input          : RTC_FLAG
* Output         : None
* Return         : FlagStatus :SET or RESET
*******************************************************************************/
FlagStatus RTC_GetFlagStatus(u32 RTC_FLAG)
{
	if (RTC->SR&RTC_FLAG) return SET;
	else return RESET;
}

/*******************************************************************************
* Function Name  : RTC_ClearFlag
* Description    : Clears a RTC flag
* Input          : RTC_FLAG
* Output         : None
* Return         : None
* Note           : Before clearing the RTC Periodic Flag you need to disable the 
*                  Periodic interrupt generation, to do this use function 
*                  RTC_PeriodicIntConfig(RTC_Per_DISABLE)
*******************************************************************************/
void RTC_ClearFlag(u32 RTC_FLAG)
{
  vu32 tmp=0;
  if (RTC_FLAG == RTC_FLAG_Per)  tmp=RTC->SR; 
  else if (RTC_FLAG == RTC_FLAG_Alarm) RTC->CR&=~0x100000;
  else if (RTC_FLAG == RTC_FLAG_Tamper) RTC->CR&=~0x1;
}


/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud