Merge pull request #3 from mattnite/busy-sleep

add busy sleep functions
wch-ch32v003
Matt Knight 2 years ago committed by GitHub
commit c71e73759c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ const regs = microzig.chip.regsisters;
pub const gpio = @import("hal/gpio.zig"); pub const gpio = @import("hal/gpio.zig");
pub const clocks = @import("hal/clocks.zig"); pub const clocks = @import("hal/clocks.zig");
pub const multicore = @import("hal/multicore.zig"); pub const multicore = @import("hal/multicore.zig");
pub const time = @import("hal/time.zig");
pub const default_clock_config = clocks.GlobalConfiguration.init(.{ pub const default_clock_config = clocks.GlobalConfiguration.init(.{
//.ref = .{ .source = .src_xosc }, //.ref = .{ .source = .src_xosc },

@ -0,0 +1,44 @@
const microzig = @import("microzig");
const TIMER = microzig.chip.registers.TIMER;
pub const Absolute = struct {
us_since_boot: u64,
};
pub fn getTimeSinceBoot() Absolute {
var high_word = TIMER.TIMERAWH.*;
return while (true) {
var low_word = TIMER.TIMERAWL.*;
const next_high_word = TIMER.TIMERAWH.*;
if (next_high_word == high_word)
break Absolute{
.us_since_boot = @intCast(u64, high_word) << 32 | low_word,
};
high_word = next_high_word;
} else unreachable;
}
pub fn makeTimeoutUs(timeout_us: u64) Absolute {
return Absolute{
.us_since_boot = getTimeSinceBoot().us_since_boot + timeout_us,
};
}
pub fn reached(time: Absolute) bool {
const now = getTimeSinceBoot();
return now.us_since_boot >= time.us_since_boot;
}
pub fn sleepMs(time_ms: u32) void {
sleepUs(time_ms * 1000);
}
pub fn sleepUs(time_us: u64) void {
const end_time = Absolute{
.us_since_boot = time_us + getTimeSinceBoot().us_since_boot,
};
while (!reached(end_time)) {}
}
Loading…
Cancel
Save