/
timekeeper.c
783 lines (677 loc) · 19.7 KB
/
timekeeper.c
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/**
* @file
*
* @todo Work out what signals go here, and how this object communicates with
* the main DClock. Timekeeper should probably read the RTC, get the ticks
* from the RTC code (32dHz interrupts), convert between decimal and real time,
* handle the decimal or real time state, synchronise the decimal time with
* real time every 108 seconds, and other stuff.
*/
#include "timekeeper.h"
#include "time.h"
#include "twi.h"
#include "twi-status.h"
#include "rtc.h"
#include "serial.h"
#include "alarm.h"
#include "bsp.h"
#include "timedisplay.h"
#include <stdio.h>
Q_DEFINE_THIS_FILE;
struct Timekeeper timekeeper;
static QState tkInitial (struct Timekeeper *me);
static QState topState (struct Timekeeper *me);
static QState startupState (struct Timekeeper *me);
static QState readRTCState (struct Timekeeper *me);
static QState setupRTCState (struct Timekeeper *me);
static QState runningState (struct Timekeeper *me);
static QState tkSetTimeState (struct Timekeeper *me);
static QState tkSetAlarmState (struct Timekeeper *me);
static void inc_decimaltime(struct Timekeeper *me);
static void inc_normaltime(struct Timekeeper *me);
static void rtc_to_normal(uint8_t *bytes, struct NormalTime *normaltime);
static void normal_to_rtc(struct NormalTime *normaltime, uint8_t *bytes);
static uint8_t checkRTCdata(uint8_t *bytes);
static uint8_t checkRTCalarm(uint8_t *bytes);
static void setupRTCdata(uint8_t *bytes);
static void start_rtc_twi_read(struct Timekeeper *me,
uint8_t reg, uint8_t nbytes);
static void default_times(struct Timekeeper *me);
static void set_alarm_alarm_times(uint8_t *bytes);
static void setup_108_125(struct Timekeeper *me);
static void synchronise_108_125(struct Timekeeper *me);
void timekeeper_ctor(void)
{
QActive_ctor((QActive*)(&timekeeper), (QStateHandler)(&tkInitial));
timekeeper.decimaltime = 50000;
timekeeper.normaltime = *it2ntp(timekeeper.decimaltime);
timekeeper.ready = 73;
/* We need to start in normal mode since we do things with normal time
and the alarm very early on. */
timekeeper.mode = NORMAL_MODE;
}
static QState tkInitial(struct Timekeeper *me)
{
return Q_TRAN(&startupState);
}
static QState topState(struct Timekeeper *me)
{
switch (Q_SIG(me)) {
case WATCHDOG_SIGNAL:
BSP_watchdog();
return Q_HANDLED();
case NORMAL_MODE_SIGNAL:
me->mode = NORMAL_MODE;
return Q_HANDLED();
case DECIMAL_MODE_SIGNAL:
me->mode = DECIMAL_MODE;
return Q_HANDLED();
}
return Q_SUPER(QHsm_top);
}
static QState startupState(struct Timekeeper *me)
{
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
//QActive_arm((QActive*)me, 1);
QActive_postISR((QActive*)me, Q_TIMEOUT_SIG, 0);
return Q_HANDLED();
case Q_TIMEOUT_SIG:
return Q_TRAN(readRTCState);
}
return Q_SUPER(topState);
}
static QState readRTCState(struct Timekeeper *me)
{
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
start_rtc_twi_read(me, 0, 19); /* Start from register 0, 19
bytes */
return Q_HANDLED();
case TWI_REPLY_0_SIGNAL:
SERIALSTR("TWI_REPLY_0_SIGNAL: ");
serial_send_rom(twi_status_string(me->twiRequest0.status));
SERIALSTR("\r\n");
SERIALSTR(" status was 0x");
serial_send_hex_int(me->twiRequest0.status);
SERIALSTR(" &request=");
serial_send_hex_int((uint16_t)(&me->twiRequest0));
SERIALSTR("\r\n");
if (0xf8 == me->twiRequest0.status) {
return Q_HANDLED();
} else {
default_times(me);
return Q_TRAN(setupRTCState);
}
case TWI_REPLY_1_SIGNAL:
SERIALSTR("TWI_REPLY_1_SIGNAL: ");
serial_send_rom(twi_status_string(me->twiRequest1.status));
SERIALSTR("\r\n");
SERIALSTR(" status was 0x");
serial_send_hex_int(me->twiRequest1.status);
SERIALSTR(" &request=");
serial_send_hex_int((uint16_t)(&me->twiRequest1));
SERIALSTR("\r\n");
SERIALSTR(" bytes=");
serial_send_hex_int(me->twiBuffer1[0]);
SERIALSTR(",");
serial_send_hex_int(me->twiBuffer1[1]);
SERIALSTR(",");
serial_send_hex_int(me->twiBuffer1[2]);
SERIALSTR("\r\n");
serial_drain();
if (0xf8 != me->twiRequest1.status) {
return Q_TRAN(setupRTCState);
}
if (0 != checkRTCdata(me->twiRequest1.bytes)) {
return Q_TRAN(setupRTCState);
}
rtc_to_normal(me->twiBuffer1, &me->normaltime);
me->decimaltime = normal_to_decimal(me->normaltime);
if (0 == checkRTCalarm(me->twiRequest1.bytes)) {
set_alarm_alarm_times(me->twiBuffer1 + 7);
if (me->twiBuffer1[14] & 0b1) {
post((&alarm), ALARM_ON_SIGNAL, 0);
} else {
post((&alarm), ALARM_OFF_SIGNAL, 0);
}
}
return Q_TRAN(runningState);
}
return Q_SUPER(topState);
}
static QState setupRTCState(struct Timekeeper *me)
{
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
me->twiBuffer1[0] = 0; /* Register 0 */
setupRTCdata(me->twiBuffer1+1);
me->twiRequest1.qactive = (QActive*)me;
me->twiRequest1.signal = TWI_REPLY_1_SIGNAL;
me->twiRequest1.bytes = me->twiBuffer1;
me->twiRequest1.nbytes = 16;
me->twiRequest1.address = RTC_ADDR << 1; /* |0 for write. */
me->twiRequest1.count = 0;
me->twiRequest1.status = 0;
me->twiRequestAddresses[0] = &(me->twiRequest1);
me->twiRequestAddresses[1] = 0;
SERIALSTR("setupRTCState, bytes=");
for (uint8_t i=0; i<16; i++) {
SERIALSTR(" ");
serial_send_hex_int(me->twiBuffer1[i]);
}
SERIALSTR("\r\n");
post(&twi, TWI_REQUEST_SIGNAL,
(QParam)((uint16_t)(&(me->twiRequestAddresses))));
return Q_HANDLED();
case TWI_REPLY_1_SIGNAL:
me->decimaltime = 50000;
me->normaltime.h = 12;
me->normaltime.m = 0;
me->normaltime.s = 0;
me->normaltime.pad = 0;
SERIALSTR("setupRTCState > runningState\r\n");
return Q_TRAN(runningState);
}
return Q_SUPER(topState);
}
static QState runningState(struct Timekeeper *me)
{
uint8_t d32counter;
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
SERIALSTR("runningState\r\n");
setup_108_125(me);
set_time_mode(NORMAL_MODE);
BSP_enable_rtc_interrupt();
return Q_HANDLED();
case TICK_DECIMAL_32_SIGNAL:
d32counter = (uint8_t) Q_PAR(me);
Q_ASSERT( d32counter != 0 );
Q_ASSERT( d32counter <= 32 );
if (d32counter == 32) {
/* We set the decimal 1/32 second counter early, to
avoid the possibility that the second calculations
and display take much longer than the time before
the next timer interrupt, and therefore before the
next signal arrives. */
BSP_set_decimal_32_counter(0);
/* We've counted 32 parts of a decimal second, so tick
over to the next second. */
post(me, TICK_DECIMAL_SIGNAL, 0);
}
return Q_HANDLED();
case TICK_DECIMAL_SIGNAL:
inc_decimaltime(me);
if (me->decimal125Count < 124) {
/* Only count to 124 decimal seconds. Each 125th
decimal second is counted by the code that
synchronises the decimal and normal seconds at
125/108 second boundaries. */
me->decimal125Count ++;
post_r((&alarm), TICK_DECIMAL_SIGNAL, me->decimaltime);
post_r((&timedisplay), TICK_DECIMAL_SIGNAL, me->decimaltime);
}
return Q_HANDLED();
case TICK_NORMAL_SIGNAL:
inc_normaltime(me);
post((&alarm), TICK_NORMAL_SIGNAL, nt2it(me->normaltime));
post((&timedisplay), TICK_NORMAL_SIGNAL, nt2it(me->normaltime));
synchronise_108_125(me);
return Q_HANDLED();
case SET_DECIMAL_TIME_SIGNAL:
me->decimaltime = (uint32_t)(Q_PAR(me));
me->normaltime = decimal_to_normal(me->decimaltime);
setup_108_125(me);
return Q_TRAN(tkSetTimeState);
case SET_NORMAL_TIME_SIGNAL:
me->normaltime = it2nt(Q_PAR(me));
me->decimaltime = normal_to_decimal(me->normaltime);
setup_108_125(me);
return Q_TRAN(tkSetTimeState);
case SET_NORMAL_ALARM_SIGNAL:
me->normalalarmtime = it2nt(Q_PAR(me));
return Q_TRAN(tkSetAlarmState);
}
return Q_SUPER(topState);
}
static QState
tkSetTimeState(struct Timekeeper *me)
{
uint8_t status;
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
SERIALSTR("tkSetTimeState\r\n");
BSP_set_decimal_32_counter(0);
/* Set up a TWI buffer to write the time. */
me->twiBuffer0[0] = 0x00; /* Register address. */
normal_to_rtc(&(me->normaltime), me->twiBuffer0 + 1);
me->twiBuffer0[4] = 0x01; /* Day */
me->twiBuffer0[5] = 0x01; /* Date */
me->twiBuffer0[6] = 0x01; /* Month/Century */
me->twiBuffer0[7] = 0x99; /* Year */
me->twiRequest0.qactive = (QActive*)me;
me->twiRequest0.signal = TWI_REPLY_0_SIGNAL;
me->twiRequest0.bytes = me->twiBuffer0;
me->twiRequest0.nbytes = 8;
me->twiRequest0.address = RTC_ADDR << 1; /* |0 for write. */
me->twiRequest0.count = 0;
me->twiRequest0.status = 0;
me->twiRequestAddresses[0] = &(me->twiRequest0);
me->twiRequestAddresses[1] = 0;
SERIALSTR(" bytes=");
for (uint8_t i=0; i<8; i++) {
SERIALSTR(" ");
serial_send_hex_int(me->twiBuffer0[i]);
}
SERIALSTR("\r\n");
post(&twi, TWI_REQUEST_SIGNAL,
(QParam)((uint16_t)(&(me->twiRequestAddresses))));
return Q_HANDLED();
case TWI_REPLY_0_SIGNAL:
status = me->twiRequest0.status;
switch (status) {
case 0xf8:
SERIALSTR("tkSetTimeState: success\r\n");
break;
default:
SERIALSTR("tkSetTimeState: TWI_REPLY_0_SIGNAL: ");
serial_send_rom(twi_status_string(status));
SERIALSTR("\r\n");
break;
}
return Q_TRAN(runningState);
case Q_EXIT_SIG:
SERIALSTR("tkSetTimeState exits\r\n");
return Q_HANDLED();
}
return Q_SUPER(runningState);
}
static QState tkSetAlarmState(struct Timekeeper *me)
{
uint8_t status;
switch (Q_SIG(me)) {
case Q_ENTRY_SIG:
SERIALSTR("tkSetAlarmState\r\n");
BSP_set_decimal_32_counter(0);
/* Set up a TWI buffer to write the time. */
me->twiBuffer0[0] = 0x07; /* Register address. */
normal_to_rtc(&(me->normalalarmtime), me->twiBuffer0 + 1);
me->twiBuffer0[4] = 0x00; /* Alarm 1 day */
me->twiBuffer0[5] = 0x00; /* Alarm 2 minute */
me->twiBuffer0[6] = 0x00; /* Alarm 2 hour */
me->twiBuffer0[7] = 0x00; /* Alarm 2 day */
if (get_alarm_state(&alarm)) {
SERIALSTR(" alarm is on\r\n");
me->twiBuffer0[8] = 0x01; /* /EOSC=0 A1IE=1 */
} else {
SERIALSTR(" alarm is off\r\n");
me->twiBuffer0[8] = 0x00;
}
me->twiRequest0.qactive = (QActive*)me;
me->twiRequest0.signal = TWI_REPLY_0_SIGNAL;
me->twiRequest0.bytes = me->twiBuffer0;
me->twiRequest0.nbytes = 9;
me->twiRequest0.address = RTC_ADDR << 1; /* |0 for write. */
me->twiRequest0.count = 0;
me->twiRequest0.status = 0;
me->twiRequestAddresses[0] = &(me->twiRequest0);
me->twiRequestAddresses[1] = 0;
SERIALSTR(" bytes=");
for (uint8_t i=0; i<9; i++) {
SERIALSTR(" ");
serial_send_hex_int(me->twiBuffer0[i]);
}
SERIALSTR("\r\n");
post(&twi, TWI_REQUEST_SIGNAL,
(QParam)((uint16_t)(&(me->twiRequestAddresses))));
return Q_HANDLED();
case TWI_REPLY_0_SIGNAL:
status = me->twiRequest0.status;
switch (status) {
case 0xf8:
SERIALSTR("tkSetAlarmState: success\r\n");
break;
default:
SERIALSTR("tkSetAlarmState: TWI_REPLY_0_SIGNAL: ");
serial_send_rom(twi_status_string(status));
SERIALSTR("\r\n");
break;
}
return Q_TRAN(runningState);
case Q_EXIT_SIG:
SERIALSTR("tkSetAlarmState exits\r\n");
return Q_HANDLED();
}
return Q_SUPER(runningState);
}
/**
* Returns 0 for ok, non-zero for something wrong.
*/
static uint8_t checkRTCdata(uint8_t *bytes)
{
uint8_t e = 1;
// seconds
if ((bytes[0] & 0x0f) > 9 ) goto ret; else e++;
if ((bytes[0] & 0x70) > 0x50) goto ret; else e++;
// minutes
if ((bytes[1] & 0x0f) > 9 ) goto ret; else e++;
if ((bytes[1] & 0x70) > 0x50) goto ret; else e++;
// hours
if ((bytes[2] & 0x0f) > 9 ) goto ret; else e++;
// Force 24 hour mode.
if ( bytes[2] & 0x40) goto ret; else e++;
if ((bytes[2] & 0x30) > 0x20) goto ret; else e++;
// A1IE is used for our own alarm purposes.
// /EOSC /BBSQW /CONF /RS2 /RS1 /INTCN /A2IE ?A1IE
if ((bytes[14]& 0xfe) !=0x00) goto ret; else e++;
/* @todo work out why we always get 0xC9 out of this register. */
// /OSF /BB32kHz /CRATE1 /CRATE0 /EN32kHz BSY? A2F? A1F?
if ((bytes[15] & 0x80) != 0x80) goto ret; else e++;
return 0;
ret:
SERIALSTR("checkRTCdata: ");
serial_send_int(e);
SERIALSTR("\r\n bytes:");
for (uint8_t i=0; i<19; i++) {
SERIALSTR(" ");
serial_send_int(i);
SERIALSTR(":");
serial_send_hex_int(bytes[i]);
}
SERIALSTR("\r\n");
return e;
}
static uint8_t checkRTCalarm(uint8_t *bytes)
{
uint8_t e = 1;
// seconds
if ((bytes[7] & 0x0f) > 9 ) goto ret; else e++;
if ((bytes[7] & 0x70) > 0x50) goto ret; else e++;
// minutes
if ((bytes[8] & 0x0f) > 9 ) goto ret; else e++;
if ((bytes[8] & 0x70) > 0x50) goto ret; else e++;
// hours
if ((bytes[9] & 0x0f) > 9 ) goto ret; else e++;
// Force 24 hour mode.
if ( bytes[9] & 0x40) goto ret; else e++;
if ((bytes[9] & 0x30) > 0x20) goto ret; else e++;
return 0;
ret:
SERIALSTR("checkRTCalarm: ");
serial_send_int(e);
SERIALSTR("\r\n bytes:");
for (uint8_t i=7; i<10; i++) {
SERIALSTR(" ");
serial_send_int(i);
SERIALSTR(":");
serial_send_hex_int(bytes[i]);
}
SERIALSTR("\r\n");
return e;
}
static void setupRTCdata(uint8_t *bytes)
{
bytes[0] = 0; /* seconds */
bytes[1] = 0; /* minues */
bytes[2] = 0x12; /* hours */
bytes[3] = 0x01; /* day */
bytes[4] = 0x01; /* date */
bytes[5] = 0x01; /* month/century */
bytes[6] = 0x99; /* year */
bytes[7] = 0;
bytes[8] = 0;
bytes[9] = 0;
bytes[10] = 0;
bytes[11] = 0;
bytes[12] = 0;
bytes[13] = 0;
bytes[14] = 0; /* /EOSC etc. /EOSC must be 0 to enable the
oscillator on backup power. */
bytes[15] = 0; /* OSF, BB32kHz etc */
}
static void inc_decimaltime(struct Timekeeper *me)
{
/* There is no need to disable interrupts while we access or update
dseconds, even though it's a four byte variable, as we never touch
it during an interrupt handler. */
if (me->decimaltime > 99999) {
SERIALSTR("me->decimaltime == ");
char ds[12];
snprintf(ds, 12, "%lu", me->decimaltime);
serial_send(ds);
SERIALSTR("\r\n");
}
Q_ASSERT( me->decimaltime <= 99999 );
if (99999 == me->decimaltime) {
me->decimaltime = 0;
} else {
me->decimaltime++;
}
}
static void inc_normaltime(struct Timekeeper *me)
{
Q_ASSERT( me->normaltime.h < 24 );
Q_ASSERT( me->normaltime.m < 60 );
Q_ASSERT( me->normaltime.s < 60 );
me->normaltime.s ++;
if (me->normaltime.s == 60) {
me->normaltime.s = 0;
me->normaltime.m ++;
if (me->normaltime.m == 60) {
me->normaltime.m = 0;
me->normaltime.h ++;
if (me->normaltime.h == 24) {
me->normaltime.h = 0;
}
}
}
}
static void
normal_to_rtc(struct NormalTime *normaltime, uint8_t *bytes)
{
Q_ASSERT( normaltime->h < 24 );
Q_ASSERT( normaltime->m < 60 );
Q_ASSERT( normaltime->s < 60 );
bytes[0] = (normaltime->s % 10) | ((normaltime->s / 10) << 4);
bytes[1] = (normaltime->m % 10) | ((normaltime->m / 10) << 4);
bytes[2] = (normaltime->h % 10) | ((normaltime->h / 10) << 4);
}
static void
rtc_to_normal(uint8_t *bytes, struct NormalTime *normaltime)
{
/* hours */
normaltime->h = (bytes[2] & 0x0f) + (((bytes[2] & 0x30) >> 4) * 10);
/* minutes */
normaltime->m = (bytes[1] & 0x0f) + (((bytes[1] & 0x70) >> 4) * 10);
/* seconds */
normaltime->s = (bytes[0] & 0x0f) + (((bytes[0] & 0x70) >> 4) * 10);
normaltime->pad = 0;
}
uint32_t
get_decimal_time(void)
{
return timekeeper.decimaltime;
}
static void
set_decimal_time(uint32_t ds)
{
post_r((&timekeeper), SET_DECIMAL_TIME_SIGNAL, (QParam)ds);
}
struct NormalTime
get_normal_time(void)
{
return timekeeper.normaltime;
}
static void
set_normal_time(struct NormalTime nt)
{
post_r((&timekeeper), SET_NORMAL_TIME_SIGNAL, nt2it(nt));
}
static void
start_rtc_twi_read(struct Timekeeper *me, uint8_t reg, uint8_t nbytes)
{
static char buffer[100];
snprintf(buffer, 99, "tk>TWI: reg=%d nbytes=%d\r\n", reg, nbytes);
serial_send(buffer);
Q_ASSERT( nbytes <= 20 );
me->twiRequest0.qactive = (QActive*)me;
me->twiRequest0.signal = TWI_REPLY_0_SIGNAL;
me->twiRequest0.bytes = me->twiBuffer0;
me->twiBuffer0[0] = reg;
me->twiRequest0.nbytes = 1;
me->twiRequest0.address = RTC_ADDR << 1; /* RW = 0, write */
me->twiRequest0.count = 0;
me->twiRequest0.status = 0;
me->twiRequestAddresses[0] = &(me->twiRequest0);
me->twiRequest1.qactive = (QActive*)me;
me->twiRequest1.signal = TWI_REPLY_1_SIGNAL;
me->twiRequest1.bytes = me->twiBuffer1;
me->twiRequest1.nbytes = nbytes;
me->twiRequest1.address = (RTC_ADDR << 1) | 0b1;
me->twiRequest1.count = 0;
me->twiRequest1.status = 0;
me->twiRequestAddresses[1] = &(me->twiRequest1);
post(&twi, TWI_REQUEST_SIGNAL, (QParam)((uint16_t)(&(me->twiRequestAddresses))));
}
void get_times(uint8_t *times)
{
switch (get_time_mode()) {
case DECIMAL_MODE:
Q_ASSERT( timekeeper.decimaltime <= 99999 );
decimal_to_dtimes(timekeeper.decimaltime, times);
break;
case NORMAL_MODE:
Q_ASSERT( timekeeper.normaltime.h <= 23 );
Q_ASSERT( timekeeper.normaltime.m <= 59 );
Q_ASSERT( timekeeper.normaltime.s <= 59 );
times[0] = timekeeper.normaltime.h;
times[1] = timekeeper.normaltime.m;
times[2] = timekeeper.normaltime.s;
break;
default:
Q_ASSERT( 0 );
break;
}
}
void set_times(uint8_t *times)
{
uint32_t dt;
struct NormalTime nt;
switch (get_time_mode()) {
case DECIMAL_MODE:
/* These three values are all unsigned and can all be zero, so
we don't need to check the lower bound. */
Q_ASSERT( times[0] <= 9 );
Q_ASSERT( times[1] <= 99 );
Q_ASSERT( times[2] <= 99 );
dt = dtimes_to_decimal(times);
set_decimal_time(dt);
SERIALSTR("Time set to ");
print_decimal_time(dt);
SERIALSTR("\r\n");
break;
case NORMAL_MODE:
Q_ASSERT( times[0] <= 23 );
Q_ASSERT( times[1] <= 59 );
Q_ASSERT( times[2] <= 59 );
nt.h = times[0];
nt.m = times[1];
nt.s = times[2];
nt.pad = 0;
set_normal_time(nt);
SERIALSTR("Time set to ");
print_normal_time(nt);
SERIALSTR("\r\n");
break;
default:
Q_ASSERT( 0 );
break;
}
}
static void set_alarm_alarm_times(uint8_t *bytes)
{
struct NormalTime nat;
uint32_t dat;
rtc_to_normal(bytes, &nat);
nat.s = 0;
dat = normal_to_decimal(nat);
set_normal_alarm_time(&alarm, nat);
set_decimal_alarm_time(&alarm, dat);
}
void set_alarm_times(struct Timekeeper *me, uint8_t *times, uint8_t on)
{
uint32_t dat;
struct NormalTime nat;
switch (get_time_mode()) {
default:
Q_ASSERT( 0 );
case DECIMAL_MODE:
/* These three values are all unsigned and can all be zero, so
we don't need to check the lower bound. */
Q_ASSERT( times[0] <= 9 );
Q_ASSERT( times[1] <= 99 );
Q_ASSERT( times[2] == 0 );
dat = (times[0] * 10000L)
+ (times[1] * 100L) + times[2];
nat = decimal_to_normal(dat);
SERIALSTR("alarm time: ");
print_decimal_time(dat);
SERIALSTR(" (");
print_normal_time(nat);
SERIALSTR(")\r\n");
break;
case NORMAL_MODE:
Q_ASSERT( times[0] <= 23 );
Q_ASSERT( times[1] <= 59 );
Q_ASSERT( times[2] == 0 );
nat.h = times[0];
nat.m = times[1];
nat.s = times[2];
nat.pad = 0;
dat = normal_to_decimal(nat);
SERIALSTR("alarm time: ");
print_normal_time(nat);
SERIALSTR(" (");
print_decimal_time(dat);
SERIALSTR(")\r\n");
break;
}
set_normal_alarm_time(&alarm, nat);
set_decimal_alarm_time(&alarm, dat);
if (on) {
post((&alarm), ALARM_ON_SIGNAL, 0);
} else {
post((&alarm), ALARM_OFF_SIGNAL, 0);
}
post(me, SET_NORMAL_ALARM_SIGNAL, nt2it(nat));
}
static void default_times(struct Timekeeper *me)
{
me->normaltime.h = 0x12;
me->normaltime.m = 0x00;
me->normaltime.s = 0x00;
me->decimaltime = normal_to_decimal(me->normaltime);
}
static void setup_108_125(struct Timekeeper *me)
{
uint32_t ntd;
ntd = normal_day_seconds(&(me->normaltime));
me->normal108Count = ntd % 108;
me->decimal125Count = me->decimaltime % 125;
}
static void synchronise_108_125(struct Timekeeper *me)
{
me->normal108Count ++;
if (108 == me->normal108Count) {
me->normal108Count = 0;
me->decimal125Count = 0;
BSP_set_decimal_32_counter(0);
me->decimaltime = normal_to_decimal(me->normaltime);
/* We only count up to 124 seconds using the CPU timer and
TICK_DECIMAL_32_SIGNALs, and the 125th second is counted
here. */
post_r((&alarm), TICK_DECIMAL_SIGNAL, me->decimaltime);
post_r((&timedisplay), TICK_DECIMAL_SIGNAL, me->decimaltime);
}
}