OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_AT91SAM3U256_IAR/] [AT91Lib/] [peripherals/] [rtc/] [rtc.c] - Blame information for rev 580

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 580 jeremybenn
/* ----------------------------------------------------------------------------
2
 *         ATMEL Microcontroller Software Support
3
 * ----------------------------------------------------------------------------
4
 * Copyright (c) 2008, Atmel Corporation
5
 *
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * - Redistributions of source code must retain the above copyright notice,
12
 * this list of conditions and the disclaimer below.
13
 *
14
 * Atmel's name may not be used to endorse or promote products derived from
15
 * this software without specific prior written permission.
16
 *
17
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * ----------------------------------------------------------------------------
28
 */
29
 
30
//------------------------------------------------------------------------------
31
//         Headers
32
//------------------------------------------------------------------------------
33
 
34
#include "rtc.h"
35
#include <board.h>
36
#include <utility/assert.h>
37
#include <utility/trace.h>
38
 
39
//------------------------------------------------------------------------------
40
//         Exported functions
41
//------------------------------------------------------------------------------
42
 
43
//------------------------------------------------------------------------------
44
/// Sets the RTC in either 12- or 24-hour mode.
45
/// \param mode  Hour mode.
46
//------------------------------------------------------------------------------
47
void RTC_SetHourMode(unsigned int mode)
48
{
49
    SANITY_CHECK((mode & 0xFFFFFFFE) == 0);
50
 
51
    TRACE_DEBUG("RTC_SetHourMode()\n\r");
52
 
53
    AT91C_BASE_RTC->RTC_MR = mode;
54
}
55
 
56
//------------------------------------------------------------------------------
57
/// Gets the RTC mode.
58
/// \return Hour mode.
59
//------------------------------------------------------------------------------
60
unsigned int RTC_GetHourMode()
61
{
62
    unsigned int hmode;
63
 
64
    TRACE_DEBUG("RTC_SetHourMode()\n\r");
65
 
66
    hmode = AT91C_BASE_RTC->RTC_MR;
67
    hmode &= 0xFFFFFFFE;
68
 
69
    return hmode;
70
}
71
 
72
//------------------------------------------------------------------------------
73
/// Enables the selected interrupt sources of the RTC.
74
/// \param sources  Interrupt sources to enable.
75
//------------------------------------------------------------------------------
76
void RTC_EnableIt(unsigned int sources)
77
{
78
    SANITY_CHECK((sources & ~0x1F) == 0);
79
 
80
    TRACE_DEBUG("RTC_EnableIt()\n\r");
81
 
82
    AT91C_BASE_RTC->RTC_IER = sources;
83
}
84
 
85
//------------------------------------------------------------------------------
86
/// Disables the selected interrupt sources of the RTC.
87
/// \param sources  Interrupt sources to disable.
88
//------------------------------------------------------------------------------
89
void RTC_DisableIt(unsigned int sources)
90
{
91
    SANITY_CHECK((sources & ~0x1F) == 0);
92
 
93
    TRACE_DEBUG("RTC_DisableIt()\n\r");
94
 
95
    AT91C_BASE_RTC->RTC_IDR = sources;
96
}
97
 
98
//------------------------------------------------------------------------------
99
/// Sets the current time in the RTC.
100
/// \param hour  Current hour in 24 hour mode.
101
/// \param minute  Current minute.
102
/// \param second  Current second.
103
/// \return 0 sucess, 1 fail to set
104
//------------------------------------------------------------------------------
105
int RTC_SetTime(unsigned char hour, unsigned char minute, unsigned char second)
106
{
107
    unsigned int time=0;
108
    unsigned char hour_bcd;
109
    unsigned char min_bcd;
110
    unsigned char sec_bcd;
111
 
112
    TRACE_DEBUG("RTC_SetTime(%02d:%02d:%02d)\n\r", hour, minute, second);
113
 
114
    // if 12-hour mode, set AMPM bit
115
    if ((AT91C_BASE_RTC->RTC_MR & AT91C_RTC_HRMOD) == AT91C_RTC_HRMOD) {
116
 
117
        if (hour > 12) {
118
 
119
            hour -= 12;
120
            time |= AT91C_RTC_AMPM;
121
        }
122
    }
123
    hour_bcd  = (hour%10) | ((hour/10)<<4);
124
    min_bcd   = (minute%10) | ((minute/10)<<4);
125
    sec_bcd   = (second%10) | ((second/10)<<4);
126
 
127
    //value overflow
128
    if((hour_bcd & (unsigned char)(~RTC_HOUR_BIT_LEN_MASK)) |
129
       (min_bcd & (unsigned char)(~RTC_MIN_BIT_LEN_MASK)) |
130
         (sec_bcd & (unsigned char)(~RTC_SEC_BIT_LEN_MASK)))
131
            return 1;
132
 
133
    time = sec_bcd | (min_bcd << 8) | (hour_bcd<<16);
134
 
135
//    time |= ((hour % 10) << 16) | ((hour / 10) << 20);
136
 
137
    // Set time
138
    //if((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_SECEV) != AT91C_RTC_SECEV) return 1;
139
    while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_SECEV) != AT91C_RTC_SECEV);//wait from previous set
140
    AT91C_BASE_RTC->RTC_CR |= AT91C_RTC_UPDTIM;
141
    while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_ACKUPD) != AT91C_RTC_ACKUPD);
142
    AT91C_BASE_RTC->RTC_SCCR = AT91C_RTC_ACKUPD;
143
    AT91C_BASE_RTC->RTC_TIMR = time;
144
    AT91C_BASE_RTC->RTC_CR &= ~AT91C_RTC_UPDTIM;
145
    AT91C_BASE_RTC->RTC_SCCR |= AT91C_RTC_SECEV;//clear SECENV in SCCR
146
 
147
    return (int)(AT91C_BASE_RTC->RTC_VER & AT91C_RTC_NVTIM);
148
}
149
 
150
//------------------------------------------------------------------------------
151
/// Retrieves the current time as stored in the RTC in several variables.
152
/// \param pHour  If not null, current hour is stored in this variable.
153
/// \param pMinute  If not null, current minute is stored in this variable.
154
/// \param pSecond  If not null, current second is stored in this variable.
155
//------------------------------------------------------------------------------
156
void RTC_GetTime(
157
    unsigned char *pHour,
158
    unsigned char *pMinute,
159
    unsigned char *pSecond)
160
{
161
    unsigned int time;
162
 
163
    TRACE_DEBUG("RTC_GetTime()\n\r");
164
 
165
    // Get current RTC time
166
    time = AT91C_BASE_RTC->RTC_TIMR;
167
    while (time != AT91C_BASE_RTC->RTC_TIMR) {
168
 
169
        time = AT91C_BASE_RTC->RTC_TIMR;
170
    }
171
 
172
    // Hour
173
    if (pHour) {
174
 
175
        *pHour = ((time & 0x00300000) >> 20) * 10
176
                 + ((time & 0x000F0000) >> 16);
177
        if ((time & AT91C_RTC_AMPM) == AT91C_RTC_AMPM) {
178
 
179
            *pHour += 12;
180
        }
181
    }
182
 
183
    // Minute
184
    if (pMinute) {
185
 
186
        *pMinute = ((time & 0x00007000) >> 12) * 10
187
                   + ((time & 0x00000F00) >> 8);
188
    }
189
 
190
    // Second
191
    if (pSecond) {
192
 
193
        *pSecond = ((time & 0x00000070) >> 4) * 10
194
                   + (time & 0x0000000F);
195
    }
196
}
197
 
198
//------------------------------------------------------------------------------
199
/// Sets a time alarm on the RTC. The match is performed only on the provided
200
/// variables; setting all pointers to 0 disables the time alarm.
201
/// Note: in AM/PM mode, the hour value must have bit #7 set for PM, cleared for
202
/// AM (as expected in the time registers).
203
/// \param pHour  If not null, the time alarm will hour-match this value.
204
/// \param pMinute  If not null, the time alarm will minute-match this value.
205
/// \param pSecond  If not null, the time alarm will second-match this value.
206
/// \return 0 success, 1 fail to set
207
//------------------------------------------------------------------------------
208
int RTC_SetTimeAlarm(
209
    unsigned char *pHour,
210
    unsigned char *pMinute,
211
    unsigned char *pSecond)
212
{
213
    unsigned int alarm = 0;
214
 
215
    TRACE_DEBUG("RTC_SetTimeAlarm()\n\r");
216
 
217
    // Hour
218
    if (pHour) {
219
 
220
        alarm |= AT91C_RTC_HOUREN | ((*pHour / 10) << 20) | ((*pHour % 10) << 16);
221
    }
222
 
223
    // Minute
224
    if (pMinute) {
225
 
226
        alarm |= AT91C_RTC_MINEN | ((*pMinute / 10) << 12) | ((*pMinute % 10) << 8);
227
    }
228
 
229
    // Second
230
    if (pSecond) {
231
 
232
        alarm |= AT91C_RTC_SECEN | ((*pSecond / 10) << 4) | (*pSecond % 10);
233
    }
234
 
235
    AT91C_BASE_RTC->RTC_TIMALR = alarm;
236
 
237
    return (int)(AT91C_BASE_RTC->RTC_VER & AT91C_RTC_NVTIMALR);
238
}
239
 
240
//------------------------------------------------------------------------------
241
/// Retrieves the current year, month and day from the RTC. Month, day and week
242
/// values are numbered starting at 1.
243
/// \param pYear  Current year (optional).
244
/// \param pMonth  Current month (optional).
245
/// \param pDay  Current day (optional).
246
/// \param pWeek  Current day in current week (optional).
247
//------------------------------------------------------------------------------
248
void RTC_GetDate(
249
    unsigned short *pYear,
250
    unsigned char *pMonth,
251
    unsigned char *pDay,
252
    unsigned char *pWeek)
253
{
254
    unsigned int date;
255
 
256
    // Get current date (multiple reads are necessary to insure a stable value)
257
    do {
258
 
259
        date = AT91C_BASE_RTC->RTC_CALR;
260
    }
261
    while (date != AT91C_BASE_RTC->RTC_CALR);
262
 
263
    // Retrieve year
264
    if (pYear) {
265
 
266
        *pYear = (((date  >> 4) & 0x7) * 1000)
267
                 + ((date & 0xF) * 100)
268
                 + (((date >> 12) & 0xF) * 10)
269
                 + ((date >> 8) & 0xF);
270
    }
271
 
272
    // Retrieve month
273
    if (pMonth) {
274
 
275
        *pMonth = (((date >> 20) & 1) * 10) + ((date >> 16) & 0xF);
276
    }
277
 
278
    // Retrieve day
279
    if (pDay) {
280
 
281
        *pDay = (((date >> 28) & 0x3) * 10) + ((date >> 24) & 0xF);
282
    }
283
 
284
    // Retrieve week
285
    if (pWeek) {
286
 
287
        *pWeek = ((date >> 21) & 0x7);
288
    }
289
}
290
 
291
//------------------------------------------------------------------------------
292
/// Sets the current year, month and day in the RTC. Month, day and week values
293
/// must be numbered starting from 1.
294
/// \param year  Current year.
295
/// \param month  Current month.
296
/// \param day  Current day.
297
/// \param week  Day number in current week.
298
/// \return 0 success, 1 fail to set
299
//------------------------------------------------------------------------------
300
int RTC_SetDate(
301
    unsigned short year,
302
    unsigned char month,
303
    unsigned char day,
304
    unsigned char week)
305
{
306
    unsigned int date;
307
    unsigned char cent_bcd;
308
    unsigned char year_bcd;
309
    unsigned char month_bcd;
310
    unsigned char day_bcd;
311
    unsigned char week_bcd;
312
 
313
    cent_bcd  = ((year/100)%10) | ((year/1000)<<4);
314
    year_bcd  = (year%10) | ((year/10)%10);
315
    month_bcd = ((month%10) | (month/10)<<4);
316
    day_bcd   = ((day%10) | (day/10)<<4);
317
    week_bcd  = ((week%10) | (week/10)<<4);
318
 
319
    //value over flow
320
    if((cent_bcd & (unsigned char)(~RTC_CENT_BIT_LEN_MASK)) |
321
        (year_bcd & (unsigned char)(~RTC_YEAR_BIT_LEN_MASK)) |
322
          (month_bcd & (unsigned char)(~RTC_MONTH_BIT_LEN_MASK)) |
323
            (week_bcd & (unsigned char)(~RTC_WEEK_BIT_LEN_MASK)) |
324
              (day_bcd & (unsigned char)(~RTC_DATE_BIT_LEN_MASK)))
325
                return 1;
326
 
327
 
328
    // Convert values to date register value
329
    date = cent_bcd |
330
            (year_bcd << 8) |
331
              (month_bcd << 16) |
332
                (week_bcd << 21) |
333
                  (day_bcd << 24);
334
 
335
 
336
    // Update calendar register
337
    //if((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_SECEV) != AT91C_RTC_SECEV) return 1;
338
    while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_SECEV) != AT91C_RTC_SECEV);//wait from previous set
339
    AT91C_BASE_RTC->RTC_CR |= AT91C_RTC_UPDCAL;
340
    while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_ACKUPD) != AT91C_RTC_ACKUPD);
341
    AT91C_BASE_RTC->RTC_SCCR = AT91C_RTC_ACKUPD;
342
    AT91C_BASE_RTC->RTC_CALR = date;
343
    AT91C_BASE_RTC->RTC_CR &= ~AT91C_RTC_UPDCAL;
344
    AT91C_BASE_RTC->RTC_SCCR |= AT91C_RTC_SECEV;//clear SECENV in SCCR
345
 
346
    return (int)(AT91C_BASE_RTC->RTC_VER & AT91C_RTC_NVCAL);
347
}
348
 
349
//------------------------------------------------------------------------------
350
/// Sets a date alarm in the RTC. The alarm will match only the provided values;
351
/// passing a null-pointer disables the corresponding field match.
352
/// \param pMonth  If not null, the RTC alarm will month-match this value.
353
/// \param pDay  If not null, the RTC alarm will day-match this value.
354
/// \return 0 success, 1 fail to set
355
//------------------------------------------------------------------------------
356
int RTC_SetDateAlarm(unsigned char *pMonth, unsigned char *pDay)
357
{
358
    unsigned int alarm = 0x01010000;
359
 
360
    TRACE_DEBUG("RTC_SetDateAlarm()\n\r");
361
 
362
    // Compute alarm field value
363
    if (pMonth) {
364
 
365
        alarm |= AT91C_RTC_MONTHEN | ((*pMonth / 10) << 20) | ((*pMonth % 10) << 16);
366
    }
367
    if (pDay) {
368
 
369
        alarm |= AT91C_RTC_DATEEN | ((*pDay / 10) << 28) | ((*pDay % 10) << 24);
370
    }
371
 
372
    // Set alarm
373
    AT91C_BASE_RTC->RTC_CALALR = alarm;
374
 
375
    return (int)(AT91C_BASE_RTC->RTC_VER & AT91C_RTC_NVCALALR);
376
}
377
 
378
//------------------------------------------------------------------------------
379
/// Clear flag bits of status clear command register in the RTC. 
380
/// \param mask Bits mask of cleared events
381
//------------------------------------------------------------------------------
382
void RTC_ClearSCCR(unsigned int mask)
383
{
384
    // Clear all flag bits in status clear command register
385
    mask &= AT91C_RTC_ACKUPD | AT91C_RTC_ALARM | AT91C_RTC_SECEV | \
386
                                    AT91C_RTC_TIMEV | AT91C_RTC_CALEV;
387
 
388
    AT91C_BASE_RTC->RTC_SCCR = mask;
389
}
390
 
391
//------------------------------------------------------------------------------
392
/// Get flag bits of status register in the RTC. 
393
/// \param mask Bits mask of Status Register
394
/// \return Status register & mask
395
//------------------------------------------------------------------------------
396
unsigned int RTC_GetSR(unsigned int mask)
397
{
398
    unsigned int event;
399
 
400
    event = AT91C_BASE_RTC->RTC_SR;
401
 
402
    return (event & mask);
403
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.