Implement RTC #1

Merged
paoda merged 14 commits from rtc into main 2022-09-17 23:36:34 +00:00
1 changed files with 7 additions and 44 deletions
Showing only changes of commit d6b182f245 - Show all commits

View File

@ -364,10 +364,7 @@ const Clock = struct {
return switch (register) { return switch (register) {
.Control => @truncate(u1, switch (self.count) { .Control => @truncate(u1, switch (self.count) {
0 => clock.cnt.raw >> idx, 0 => clock.cnt.raw >> idx,
else => { else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 1 byte)", .{ self.count, register }),
log.err("RTC: {} is only 1 byte wide", .{register});
@panic("Out-of-bounds RTC read");
},
}), }),
.DateTime => @truncate(u1, switch (self.count) { .DateTime => @truncate(u1, switch (self.count) {
// Date // Date
@ -380,19 +377,13 @@ const Clock = struct {
4 => @as(u8, clock.hour) >> idx, 4 => @as(u8, clock.hour) >> idx,
5 => @as(u8, clock.minute) >> idx, 5 => @as(u8, clock.minute) >> idx,
6 => @as(u8, clock.second) >> idx, 6 => @as(u8, clock.second) >> idx,
else => { else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 7 bytes)", .{ self.count, register }),
log.err("RTC: {} is only 7 bytes wide", .{register});
@panic("Out-of-bounds RTC read");
},
}), }),
.Time => @truncate(u1, switch (self.count) { .Time => @truncate(u1, switch (self.count) {
0 => @as(u8, clock.hour) >> idx, 0 => @as(u8, clock.hour) >> idx,
1 => @as(u8, clock.minute) >> idx, 1 => @as(u8, clock.minute) >> idx,
2 => @as(u8, clock.second) >> idx, 2 => @as(u8, clock.second) >> idx,
else => { else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 3 bytes)", .{ self.count, register }),
log.err("RTC: {} is only 3 bytes wide", .{register});
@panic("Out-of-bounds RTC read");
},
}), }),
}; };
} }
@ -441,38 +432,10 @@ const Clock = struct {
// FIXME: What do do about unused bits? // FIXME: What do do about unused bits?
switch (register) { switch (register) {
.Control => switch (self.count) { .Control => switch (self.count) {
0 => clock.cnt.raw = self.buf, 0 => clock.cnt.raw = (clock.cnt.raw & 0x80) | (self.buf & 0x7F), // Bit 7 read-only
else => { else => std.debug.panic("Tried to write to byte #{} of {} (hint: there's only 1 byte)", .{ self.count, register }),
log.err("RTC :{} is only 1 byte wide", .{register});
@panic("Out-of-bounds RTC write");
},
},
.DateTime => switch (self.count) {
// Date
0 => clock.year = @truncate(@TypeOf(clock.year), self.buf),
1 => clock.month = @truncate(@TypeOf(clock.month), self.buf),
2 => clock.day = @truncate(@TypeOf(clock.day), self.buf),
3 => clock.day_of_week = @truncate(@TypeOf(clock.day_of_week), self.buf),
// Time
4 => clock.hour = @truncate(@TypeOf(clock.hour), self.buf),
5 => clock.minute = @truncate(@TypeOf(clock.minute), self.buf),
6 => clock.second = @truncate(@TypeOf(clock.second), self.buf),
else => {
log.err("RTC :{} is only 1 byte wide", .{register});
@panic("Out-of-bounds RTC write");
},
},
.Time => switch (self.count) {
// Time
0 => clock.hour = @truncate(@TypeOf(clock.hour), self.buf),
1 => clock.minute = @truncate(@TypeOf(clock.minute), self.buf),
2 => clock.second = @truncate(@TypeOf(clock.second), self.buf),
else => {
log.err("RTC :{} is only 1 byte wide", .{register});
@panic("Out-of-bounds RTC write");
},
}, },
.DateTime, .Time => log.debug("RTC: Ignoring {} write", .{register}),
} }
} }