|
1 | 1 | import { BasePeripheral, Peripheral } from './peripheral';
|
2 | 2 |
|
| 3 | +const RTC_SETUP0 = 0x04; |
| 4 | +const RTC_SETUP1 = 0x08; |
3 | 5 | const RTC_CTRL = 0x0c;
|
4 | 6 | const IRQ_SETUP_0 = 0x10;
|
| 7 | +const RTC_RTC1 = 0x18; |
| 8 | +const RTC_RTC0 = 0x1c; |
| 9 | + |
| 10 | +const RTC_ENABLE_BITS = 0x01; |
5 | 11 | const RTC_ACTIVE_BITS = 0x2;
|
| 12 | +const RTC_LOAD_BITS = 0x10; |
6 | 13 |
|
7 | 14 | 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; |
9 | 20 |
|
10 | 21 | readUint32(offset: number) {
|
11 | 22 | switch (offset) {
|
| 23 | + case RTC_SETUP0: |
| 24 | + return this.setup0; |
| 25 | + case RTC_SETUP1: |
| 26 | + return this.setup1; |
12 | 27 | case RTC_CTRL:
|
13 |
| - return this.running ? RTC_ACTIVE_BITS : 0; |
| 28 | + return this.ctrl; |
14 | 29 | case IRQ_SETUP_0:
|
15 | 30 | return 0;
|
| 31 | + case RTC_RTC1: |
| 32 | + return this.rtc1; |
| 33 | + case RTC_RTC0: |
| 34 | + return this.rtc0; |
16 | 35 | }
|
17 | 36 | return super.readUint32(offset);
|
18 | 37 | }
|
19 | 38 |
|
20 | 39 | writeUint32(offset: number, value: number) {
|
21 | 40 | switch (offset) {
|
| 41 | + case RTC_SETUP0: |
| 42 | + this.setup0 = value; |
| 43 | + break; |
| 44 | + case RTC_SETUP1: |
| 45 | + this.setup1 = value; |
| 46 | + break; |
22 | 47 | 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 | + } |
24 | 66 | break;
|
25 | 67 | default:
|
26 | 68 | super.writeUint32(offset, value);
|
|
0 commit comments