Newer
Older
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
/* ****************************************************************************
* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*
*
**************************************************************************** */
#include "mxc_config.h"
#include "rtc_regs.h"
#include "rtc.h"
#include "mxc_sys.h"
#include "mxc_delay.h"
#include "gpio_regs.h"
#include "mxc_errors.h"
#define RTC_CTRL_RESET_DEFAULT (0x0000UL)
#define RTC_IS_BUSY (MXC_RTC->ctrl & MXC_F_RTC_CTRL_BUSY)
#define RTC_IS_ENABLED (MXC_RTC->ctrl & MXC_F_RTC_CTRL_RTCE)
#define BUSY_TIMEOUT 1000 // Timeout counts for the Busy bit
// *****************************************************************************
int RTC_EnableTimeofdayInterrupt(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_ADE; // Enable Time-of-day Interrupt
return E_SUCCESS;
}
// *****************************************************************************
int RTC_DisableTimeofdayInterrupt(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_ADE; // Disable Time-of-day Interrupt
if (RTC_CheckBusy()) {
return E_BUSY;
}
return E_SUCCESS;
}
// *****************************************************************************
int RTC_EnableSubsecondInterrupt(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_ASE; // Enable Sub-Second Interrupt
return E_SUCCESS;
}
// *****************************************************************************
int RTC_DisableSubsecondInterrupt(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_ASE; // Alarm Sub-Second Interrupt disabled
if (RTC_CheckBusy()) {
return E_BUSY;
}
return E_SUCCESS;
}
// *****************************************************************************
int RTC_SetTimeofdayAlarm(mxc_rtc_regs_t *rtc, uint32_t ras)
{
// ras can only be written if BUSY = 0 & (RTCE = 0 or ADE = 0);
if(RTC_DisableTimeofdayInterrupt(rtc) == E_BUSY) {
return E_BUSY;
}
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ras = (ras << MXC_F_RTC_RAS_RAS_POS) & MXC_F_RTC_RAS_RAS;
if(RTC_EnableTimeofdayInterrupt(rtc) == E_BUSY) {
return E_BUSY;
}
return E_SUCCESS;
}
// *****************************************************************************
int RTC_SetSubsecondAlarm(mxc_rtc_regs_t *rtc, uint32_t rssa)
{
// ras can only be written if BUSY = 0 & (RTCE = 0 or ASE = 0);
if(RTC_DisableSubsecondInterrupt(rtc) == E_BUSY) {
return E_BUSY;
}
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->rssa = (rssa << MXC_F_RTC_RSSA_RSSA_POS) & MXC_F_RTC_RSSA_RSSA;
if(RTC_EnableSubsecondInterrupt(rtc) == E_BUSY) {
return E_BUSY;
}
return E_SUCCESS;
}
// *****************************************************************************
int RTC_EnableRTCE(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
if (RTC_CheckBusy()) {
return E_BUSY;
}
// Can only write if WE=1 and BUSY=0
rtc->ctrl |= MXC_F_RTC_CTRL_RTCE; // setting RTCE = 1
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
return E_SUCCESS;
}
// *****************************************************************************
int RTC_DisableRTCE(mxc_rtc_regs_t *rtc)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
if (RTC_CheckBusy()) {
return E_BUSY;
}
// Can only write if WE=1 and BUSY=0
rtc->ctrl &= ~MXC_F_RTC_CTRL_RTCE; // setting RTCE = 0
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
return E_SUCCESS;
}
// *****************************************************************************
int RTC_Init(mxc_rtc_regs_t *rtc, uint32_t sec, uint16_t ssec, sys_cfg_rtc_t *sys_cfg)
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
{
SYS_RTCClockEnable(sys_cfg);
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl = MXC_F_RTC_CTRL_WE; // Allow Writes
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl = RTC_CTRL_RESET_DEFAULT; // Start with a Clean Register
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Set Write Enable, allow writing to reg.
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ssec = ssec;
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->sec = sec;
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Prevent Writing...
return E_SUCCESS;
}
// *****************************************************************************
int RTC_SquareWave(mxc_rtc_regs_t *rtc, rtc_sqwave_en_t sqe, rtc_freq_sel_t ft,
rtc_osc_mode_t x32kmd, const sys_cfg_rtc_t* sys_cfg)
{
SYS_RTC_SqwavInit(sys_cfg); // Set the Output pins for the squarewave.
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
if (RTC_CheckBusy()) {
return E_BUSY;
}
if (sqe == SQUARE_WAVE_ENABLED) {
if (ft == F_32KHZ){ // if 32KHz output is selected...
rtc->oscctrl |= MXC_F_RTC_OSCCTRL_32KOUT; // Enable 32KHz wave
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_SQE; // Enable output on the pin
} else { // if 1Hz, 512Hz, 4KHz output is selected
rtc->oscctrl &= ~MXC_F_RTC_OSCCTRL_32KOUT; // Must make sure that the 32KHz is disabled
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~(MXC_F_RTC_CTRL_FT | MXC_F_RTC_CTRL_X32KMD);
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= (MXC_F_RTC_CTRL_SQE | ft | x32kmd); // Enable Sq. wave,
}
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_RTCE; // Enable Real Time Clock
} else { // Turn off the square wave output on the pin
rtc->oscctrl &= ~MXC_F_RTC_OSCCTRL_32KOUT; // Must make sure that the 32KHz is disabled
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_SQE; // No sq. wave output
}
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Disable Writing to register
return E_SUCCESS;
}
// *****************************************************************************
int RTC_Trim(mxc_rtc_regs_t *rtc, int8_t trim)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl |= MXC_F_RTC_CTRL_WE;
if (RTC_CheckBusy()) {
return E_BUSY;
}
MXC_SETFIELD(rtc->trim, MXC_F_RTC_TRIM_TRIM, trim << MXC_F_RTC_TRIM_TRIM_POS);
if (RTC_CheckBusy()) {
return E_BUSY;
}
rtc->ctrl &= ~MXC_F_RTC_CTRL_WE; // Disable Writing to register
return E_SUCCESS;
}
// *****************************************************************************
int RTC_CheckBusy(void)
{
// Time-out transfer if it takes > BUSY_TIMEOUT microseconds
mxc_delay_start(MXC_DELAY_USEC(BUSY_TIMEOUT));
while (RTC_IS_BUSY) {
if (mxc_delay_check() != E_BUSY){
return E_BUSY;
}
}
return E_SUCCESS;
}
// *****************************************************************************
int RTC_GetFlags(void)
{
return MXC_RTC->ctrl & (MXC_F_RTC_CTRL_ALDF | MXC_F_RTC_CTRL_ALSF | MXC_F_RTC_CTRL_RDY);
}
// *****************************************************************************
int RTC_ClearFlags(int flags)
{
if (RTC_CheckBusy()) {
return E_BUSY;
}
MXC_RTC->ctrl &= ~(flags & (MXC_F_RTC_CTRL_ALDF | MXC_F_RTC_CTRL_ALSF | MXC_F_RTC_CTRL_RDY));
return E_SUCCESS;
}
// *****************************************************************************
int RTC_GetSubSecond(void)
{
return MXC_RTC->ssec;
}
// *****************************************************************************
int RTC_GetSecond(void)
{
return MXC_RTC->sec;
}
// *****************************************************************************
int RTC_GetTime(uint32_t* sec, uint32_t* subsec)
{
uint32_t temp_sec;
do {
// Check if an update is about to happen.
if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
return E_BUSY;
}
// Read the seconds count.
temp_sec = RTC_GetSecond();
// Check if an update is about to happen.
if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
return E_BUSY;
}
// Read the sub-seconds count.
*subsec = RTC_GetSubSecond();
// Check if an update is about to happen.
if(!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {
return E_BUSY;
}
// Read the seconds count.
*sec = RTC_GetSecond();
// Repeat until a steady state is reached.
} while (temp_sec != *sec);
return E_NO_ERROR;
}