Skip to content

Commit fa0dc6c

Browse files
authored
feat(rtc): RTC setup & load (#110)
1 parent 46e93d8 commit fa0dc6c

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/peripherals/rtc.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
11
import { BasePeripheral, Peripheral } from './peripheral';
22

3+
const RTC_SETUP0 = 0x04;
4+
const RTC_SETUP1 = 0x08;
35
const RTC_CTRL = 0x0c;
46
const IRQ_SETUP_0 = 0x10;
7+
const RTC_RTC1 = 0x18;
8+
const RTC_RTC0 = 0x1c;
9+
10+
const RTC_ENABLE_BITS = 0x01;
511
const RTC_ACTIVE_BITS = 0x2;
12+
const RTC_LOAD_BITS = 0x10;
613

714
export class RP2040RTC extends BasePeripheral implements Peripheral {
8-
running = true;
15+
setup0 = 0;
16+
setup1 = 0;
17+
rtc1 = 0;
18+
rtc0 = 0;
19+
ctrl = 0;
920

1021
readUint32(offset: number) {
1122
switch (offset) {
23+
case RTC_SETUP0:
24+
return this.setup0;
25+
case RTC_SETUP1:
26+
return this.setup1;
1227
case RTC_CTRL:
13-
return this.running ? RTC_ACTIVE_BITS : 0;
28+
return this.ctrl;
1429
case IRQ_SETUP_0:
1530
return 0;
31+
case RTC_RTC1:
32+
return this.rtc1;
33+
case RTC_RTC0:
34+
return this.rtc0;
1635
}
1736
return super.readUint32(offset);
1837
}
1938

2039
writeUint32(offset: number, value: number) {
2140
switch (offset) {
41+
case RTC_SETUP0:
42+
this.setup0 = value;
43+
break;
44+
case RTC_SETUP1:
45+
this.setup1 = value;
46+
break;
2247
case RTC_CTRL:
23-
this.running = value > 0; // TODO consult the datasheet
48+
// Though RTC_LOAD_BITS is type SC and should be cleared on next cycle, pico-sdk write
49+
// RTC_LOAD_BITS & RTC_ENABLE_BITS seperatly.
50+
// https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_rtc/rtc.c#L76-L80
51+
if (value & RTC_LOAD_BITS) {
52+
this.ctrl |= RTC_LOAD_BITS;
53+
}
54+
if (value & RTC_ENABLE_BITS) {
55+
this.ctrl |= RTC_ENABLE_BITS;
56+
this.ctrl |= RTC_ACTIVE_BITS
57+
if (this.ctrl & RTC_LOAD_BITS) {
58+
this.rtc1 = this.setup0;
59+
this.rtc0 = this.setup1;
60+
this.ctrl &= ~RTC_LOAD_BITS;
61+
}
62+
} else {
63+
this.ctrl &= ~RTC_ENABLE_BITS
64+
this.ctrl &= ~RTC_ACTIVE_BITS
65+
}
2466
break;
2567
default:
2668
super.writeUint32(offset, value);

0 commit comments

Comments
 (0)