OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [drivers/] [Atmel/] [at91lib/] [peripherals/] [rtc/] [rtc.c] - Blame information for rev 608

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 608 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
#ifndef trace_LEVEL
31
        #define trace_LEVEL trace_INFO
32
#endif
33
 
34
//------------------------------------------------------------------------------
35
//         Headers
36
//------------------------------------------------------------------------------
37
 
38
#include "rtc.h"
39
#include <board.h>
40
#include <utility/assert.h>
41
#include <utility/trace.h>
42
 
43
//------------------------------------------------------------------------------
44
//         Exported functions
45
//------------------------------------------------------------------------------
46
 
47
//------------------------------------------------------------------------------
48
/// Sets the RTC in either 12- or 24-hour mode.
49
/// \param mode  Hour mode.
50
//------------------------------------------------------------------------------
51
void RTC_SetHourMode(unsigned int mode)
52
{
53
        SANITY_CHECK((mode & 0xFFFFFFFE) == 0);
54
 
55
    trace_LOG(trace_DEBUG, "-D- RTC_SetHourMode()\n\r");
56
 
57
        AT91C_BASE_RTC->RTC_MR = mode;
58
}
59
 
60
//------------------------------------------------------------------------------
61
/// Enables the selected interrupt sources of the RTC.
62
/// \param sources  Interrupt sources to enable.
63
//------------------------------------------------------------------------------
64
void RTC_EnableIt(unsigned int sources)
65
{
66
    SANITY_CHECK((sources & ~0x1F) == 0);
67
 
68
    trace_LOG(trace_DEBUG, "-D- RTC_EnableIt()\n\r");
69
 
70
    AT91C_BASE_RTC->RTC_IER = sources;
71
}
72
 
73
//------------------------------------------------------------------------------
74
/// Disables the selected interrupt sources of the RTC.
75
/// \param sources  Interrupt sources to disable.
76
//------------------------------------------------------------------------------
77
void RTC_DisableIt(unsigned int sources)
78
{
79
    SANITY_CHECK((sources & ~0x1F) == 0);
80
 
81
    trace_LOG(trace_DEBUG, "-D- RTC_DisableIt()\n\r");
82
 
83
    AT91C_BASE_RTC->RTC_IDR = sources;
84
}
85
 
86
//------------------------------------------------------------------------------
87
/// Sets the current time in the RTC.
88
/// \param hour  Current hour.
89
/// \param minute  Current minute.
90
/// \param second  Current second.
91
//------------------------------------------------------------------------------
92
void RTC_SetTime(unsigned char hour, unsigned char minute, unsigned char second)
93
{
94
        unsigned int time;
95
 
96
        SANITY_CHECK(hour < 24);
97
        SANITY_CHECK(minute < 60);
98
        SANITY_CHECK(second < 60);
99
 
100
    trace_LOG(trace_DEBUG, "-D- RTC_SetTime(%02d:%02d:%02d)\n\r", hour, minute, second);
101
 
102
        time = (second % 10) | ((second / 10) << 4)
103
                   | ((minute % 10) << 8) | ((minute / 10) << 12);
104
 
105
        // 12-hour mode
106
        if ((AT91C_BASE_RTC->RTC_MR & AT91C_RTC_HRMOD) == AT91C_RTC_HRMOD) {
107
 
108
                if (hour > 12) {
109
 
110
                        hour -= 12;
111
                        time |= AT91C_RTC_AMPM;
112
                }
113
        }
114
 
115
        time |= ((hour % 10) << 16) | ((hour / 10) << 20);
116
 
117
        // Set time
118
        AT91C_BASE_RTC->RTC_CR |= AT91C_RTC_UPDTIM;
119
        while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_ACKUPD) != AT91C_RTC_ACKUPD);
120
        AT91C_BASE_RTC->RTC_SCCR = AT91C_RTC_ACKUPD;
121
        AT91C_BASE_RTC->RTC_TIMR = time;
122
        AT91C_BASE_RTC->RTC_CR &= ~AT91C_RTC_UPDTIM;
123
        SANITY_CHECK((AT91C_BASE_RTC->RTC_CR & AT91C_RTC_UPDTIM) != AT91C_RTC_UPDTIM);
124
}
125
 
126
//------------------------------------------------------------------------------
127
/// Retrieves the current time as stored in the RTC in several variables.
128
/// \param pHour  If not null, current hour is stored in this variable.
129
/// \param pMinute  If not null, current minute is stored in this variable.
130
/// \param pSecond  If not null, current second is stored in this variable.
131
//------------------------------------------------------------------------------
132
void RTC_GetTime(
133
        unsigned char *pHour,
134
        unsigned char *pMinute,
135
        unsigned char *pSecond)
136
{
137
        unsigned int time;
138
 
139
        SANITY_CHECK(pHour || pMinute || pSecond);
140
 
141
    trace_LOG(trace_DEBUG, "-D- RTC_GetTime()\n\r");
142
 
143
        // Get current RTC time
144
        time = AT91C_BASE_RTC->RTC_TIMR;
145
        while (time != AT91C_BASE_RTC->RTC_TIMR) {
146
 
147
                time = AT91C_BASE_RTC->RTC_TIMR;
148
        }
149
 
150
        // Hour
151
        if (pHour) {
152
 
153
                *pHour = ((time & 0x00300000) >> 20) * 10
154
                                 + ((time & 0x000F0000) >> 16);
155
                if ((time & AT91C_RTC_AMPM) == AT91C_RTC_AMPM) {
156
 
157
                        *pHour += 12;
158
                }
159
        }
160
 
161
        // Minute
162
        if (pMinute) {
163
 
164
                *pMinute = ((time & 0x00007000) >> 12) * 10
165
                                   + ((time & 0x00000F00) >> 8);
166
        }
167
 
168
        // Second
169
        if (pSecond) {
170
 
171
                *pSecond = ((time & 0x00000070) >> 4) * 10
172
                                   + (time & 0x0000000F);
173
        }
174
}
175
 
176
//------------------------------------------------------------------------------
177
/// Sets a time alarm on the RTC. The match is performed only on the provided
178
/// variables; setting all pointers to 0 disables the time alarm.
179
/// Note: in AM/PM mode, the hour value must have bit #7 set for PM, cleared for
180
/// AM (as expected in the time registers).
181
/// \param pHour  If not null, the time alarm will hour-match this value.
182
/// \param pMinute  If not null, the time alarm will minute-match this value.
183
/// \param pSecond  If not null, the time alarm will second-match this value.
184
//------------------------------------------------------------------------------
185
void RTC_SetTimeAlarm(
186
        unsigned char *pHour,
187
        unsigned char *pMinute,
188
        unsigned char *pSecond)
189
{
190
        unsigned int alarm = 0;
191
 
192
    SANITY_CHECK(!pHour || ((*pHour & 0x80) == 0));
193
    SANITY_CHECK(!pMinute || (*pMinute < 60));
194
    SANITY_CHECK(!pSecond || (*pSecond < 60));
195
 
196
        trace_LOG(trace_DEBUG, "-D- RTC_SetTimeAlarm()\n\r");
197
 
198
        // Hour
199
        if (pHour) {
200
 
201
                alarm |= AT91C_RTC_HOUREN | ((*pHour / 10) << 20) | ((*pHour % 10) << 16);
202
        }
203
 
204
        // Minute
205
        if (pMinute) {
206
 
207
                alarm |= AT91C_RTC_MINEN | ((*pMinute / 10) << 12) | ((*pMinute % 10) << 8);
208
        }
209
 
210
        // Second
211
        if (pSecond) {
212
 
213
                alarm |= AT91C_RTC_SECEN | ((*pSecond / 10) << 4) | (*pSecond % 10);
214
        }
215
 
216
        AT91C_BASE_RTC->RTC_TIMALR = alarm;
217
}
218
 
219
//------------------------------------------------------------------------------
220
/// Retrieves the current year, month and day from the RTC. Month, day and week
221
/// values are numbered starting at 1.
222
/// \param pYear  Current year (optional).
223
/// \param pMonth  Current month (optional).
224
/// \param pDay  Current day (optional).
225
/// \param pWeek  Current day in current week (optional).
226
//------------------------------------------------------------------------------
227
void RTC_GetDate(
228
    unsigned short *pYear,
229
    unsigned char *pMonth,
230
    unsigned char *pDay,
231
    unsigned char *pWeek)
232
{
233
    unsigned int date;
234
 
235
    // Get current date (multiple reads are necessary to insure a stable value)
236
    do {
237
 
238
        date = AT91C_BASE_RTC->RTC_CALR;
239
    }
240
    while (date != AT91C_BASE_RTC->RTC_CALR);
241
 
242
    // Retrieve year
243
    if (pYear) {
244
 
245
        *pYear = (((date  >> 4) & 0x7) * 1000)
246
                 + ((date & 0xF) * 100)
247
                 + (((date >> 12) & 0xF) * 10)
248
                 + ((date >> 8) & 0xF);
249
    }
250
 
251
    // Retrieve month
252
    if (pMonth) {
253
 
254
        *pMonth = (((date >> 20) & 1) * 10) + ((date >> 16) & 0xF);
255
    }
256
 
257
    // Retrieve day
258
    if (pDay) {
259
 
260
        *pDay = (((date >> 28) & 0x3) * 10) + ((date >> 24) & 0xF);
261
    }
262
 
263
    // Retrieve week
264
    if (pWeek) {
265
 
266
        *pWeek = ((date >> 21) & 0x7);
267
    }
268
}
269
 
270
//------------------------------------------------------------------------------
271
/// Sets the current year, month and day in the RTC. Month, day and week values
272
/// must be numbered starting from 1.
273
/// \param year  Current year.
274
/// \param month  Current month.
275
/// \param day  Current day.
276
/// \param week  Day number in current week.
277
//------------------------------------------------------------------------------
278
void RTC_SetDate(
279
    unsigned short year,
280
    unsigned char month,
281
    unsigned char day,
282
    unsigned char week)
283
{
284
    unsigned int date;
285
 
286
    SANITY_CHECK((year >= 1900) && (year <= 2099));
287
    SANITY_CHECK((month >= 1) && (month <= 12));
288
    SANITY_CHECK((day >= 1) && (day <= 31));
289
    SANITY_CHECK((week >= 1) && (week <= 7));
290
 
291
    // Convert values to date register value
292
    date = ((year / 100) % 10)
293
           | ((year / 1000) << 4)
294
           | ((year % 10) << 8)
295
           | (((year / 10) % 10) << 12)
296
           | ((month % 10) << 16)
297
           | ((month / 10) << 20)
298
           | (week << 21)
299
           | ((day % 10) << 24)
300
           | ((day / 10) << 28);
301
 
302
    // Update calendar register
303
    AT91C_BASE_RTC->RTC_CR |= AT91C_RTC_UPDCAL;
304
    while ((AT91C_BASE_RTC->RTC_SR & AT91C_RTC_ACKUPD) != AT91C_RTC_ACKUPD);
305
    AT91C_BASE_RTC->RTC_SCCR = AT91C_RTC_ACKUPD;
306
    AT91C_BASE_RTC->RTC_CALR = date;
307
    AT91C_BASE_RTC->RTC_CR &= ~AT91C_RTC_UPDCAL;
308
}
309
 
310
//------------------------------------------------------------------------------
311
/// Sets a date alarm in the RTC. The alarm will match only the provided values;
312
/// passing a null-pointer disables the corresponding field match.
313
/// \param pMonth  If not null, the RTC alarm will month-match this value.
314
/// \param pDay  If not null, the RTC alarm will day-match this value.
315
//------------------------------------------------------------------------------
316
void RTC_SetDateAlarm(unsigned char *pMonth, unsigned char *pDay)
317
{
318
    unsigned int alarm = 0;
319
 
320
    SANITY_CHECK(!pMonth || ((*pMonth >= 1) && (*pMonth <= 12)));
321
    SANITY_CHECK(!pDay || ((*pDay >= 1) && (*pDay <= 31)));
322
 
323
    trace_LOG(trace_DEBUG, "-D- RTC_SetDateAlarm()\n\r");
324
 
325
    // Compute alarm field value
326
    if (pMonth) {
327
 
328
        alarm |= AT91C_RTC_MONTHEN | ((*pMonth / 10) << 20) | ((*pMonth % 10) << 16);
329
    }
330
    if (pDay) {
331
 
332
        alarm |= AT91C_RTC_DATEEN | ((*pDay / 10) << 28) | ((*pDay % 10) << 24);
333
    }
334
 
335
    // Set alarm
336
    AT91C_BASE_RTC->RTC_CALALR = alarm;
337
}
338
 

powered by: WebSVN 2.1.0

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