11///
22/// Basic timekeeping for the nRF5x series MCUs.
33///
4- /// This module uses RTC0 and hogs CC[3 ]
4+ /// This module uses RTC0 and hogs CC[2 ]
55/// It also sets up an interrupt to fire at certain values so that we are able to count them and
66/// keep time for centuries.
77const std = @import ("std" );
@@ -18,10 +18,9 @@ const version: enum {
1818 else = > compatibility .unsupported_chip ("time" ),
1919};
2020
21- const timer = microzig .chip .peripherals .TIMER0 ; // DELETEME
2221const rtc = microzig .chip .peripherals .RTC0 ;
2322const interrupts = microzig .chip .peripherals .interrupts ;
24- const COMPARE_INDEX = 3 ;
23+ const COMPARE_INDEX = 2 ;
2524const TIMER_BITS = 23 ;
2625
2726// Must use @atomic to load an store from here.
@@ -35,26 +34,21 @@ pub fn init() void {
3534 // 'period' on two different events:
3635 // First, when it hits the halfway point, and again on overflow.
3736
38- // TODO: Put this in clocks hal
39- // Set clock source
37+ // TODO: Use clocks hal
38+ // Set clock source and start clock
4039 microzig .chip .peripherals .CLOCK .LFCLKSRC .modify (.{ .SRC = .RC });
4140 // Start LFCLK
4241 microzig .chip .peripherals .CLOCK .TASKS_LFCLKSTART .write_raw (1 );
42+ // Enable RTC0 interrupt
4343 microzig .cpu .interrupt .enable (.RTC0 );
44- microzig .cpu .interrupt .enable_interrupts ();
45- // microzip.cpu
4644
4745 // Enable interrupt firing on compare AND on overflow
48- rtc .INTENSET .write_raw (0x00080002 );
49- // rtc.INTENSET.modify(.{
50- // // .TICK = .Enabled, // This triggers!
51- // .COMPARE3 = .Enabled,
52- // .OVRFLW = .Enabled,
53- // });
46+ rtc .INTENSET .modify (.{
47+ .COMPARE2 = .Enabled ,
48+ .OVRFLW = .Enabled ,
49+ });
5450 // Set the comparator to trigger on overflow of bottom 23 bits
55- rtc .CC [COMPARE_INDEX ].write (.{ .COMPARE = 0x8000 }); // DELETEME Just to not have to wait too
56- // long for the interrupt to fire
57- // rtc.CC[COMPARE_INDEX].write(.{ .COMPARE = 0x800000 });
51+ rtc .CC [COMPARE_INDEX ].write (.{ .COMPARE = 0x800000 });
5852
5953 // Clear counter, then start timer
6054 switch (version ) {
@@ -70,27 +64,20 @@ pub fn init() void {
7064
7165 // Wait for clear
7266 while (rtc .COUNTER .read ().COUNTER != 0 ) {}
73- // TODO: Set priority
74- // Enable interrupt
75- // TODO: Use an interrupt hal
76- // VectorTable.RTC0 = rtc_overflow_interrupt;
7767}
7868
7969/// Handle both overflow and compare interrupts. Update the period which acts as the high bits of
8070/// the elapsed time.
8171pub fn rtc_overflow_interrupt () callconv (.c ) void {
8272 if (rtc .EVENTS_OVRFLW .raw == 1 ) {
83- rtc .EVENTS_OVRFLW .raw = 0 ;
84- std .log .info ("overflow!" , .{}); // DELETEME
73+ rtc .EVENTS_OVRFLW .write_raw (0 );
8574 next_period ();
8675 }
8776
8877 if (rtc .EVENTS_COMPARE [COMPARE_INDEX ].raw == 1 ) {
8978 rtc .EVENTS_COMPARE [COMPARE_INDEX ].write_raw (0 );
90- std .log .info ("compare!" , .{}); // DELETEME
9179 next_period ();
9280 }
93- @panic ("lol" );
9481}
9582
9683inline fn next_period () void {
0 commit comments