firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

at a3ff1f4e4e2aaec4a4c1ec5def91c21d36dc12f7 115 lines 2.4 kB view raw
1use core::mem::MaybeUninit; 2use crate::syscall; 3use crate::syscall::SyscallNumber; 4 5#[cfg(feature = "critical-section-impl")] 6use once_cell::sync::OnceCell; 7 8#[repr(usize)] 9#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)] 10#[cfg_attr(feature = "defmt", derive(defmt::Format))] 11pub enum MiscSyscall { 12 GetSerial = 0, 13 LogMessage = 1, 14 GetTimeMicros = 2, 15} 16 17#[repr(usize)] 18#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)] 19#[cfg_attr(feature = "defmt", derive(defmt::Format))] 20pub enum LogLevel { 21 Trace = 0, 22 Debug = 1, 23 Info = 2, 24 Warn = 3, 25 Error = 4, 26} 27 28pub fn get_serial_raw() -> [u8; 16] { 29 let mut buf: MaybeUninit<[u8; 16]> = MaybeUninit::uninit(); 30 let ptr = buf.as_mut_ptr(); 31 32 unsafe { 33 syscall!( 34 SyscallNumber::Misc, 35 in MiscSyscall::GetSerial, 36 in ptr, 37 ); 38 39 buf.assume_init() 40 } 41} 42 43#[cfg(feature = "critical-section-impl")] 44static SERIAL: OnceCell<[u8; 16]> = OnceCell::new(); 45 46#[cfg(feature = "critical-section-impl")] 47pub fn get_serial() -> &'static str { 48 if SERIAL.get().is_none() { 49 SERIAL.set(get_serial_raw()).unwrap(); 50 } 51 52 unsafe { 53 core::str::from_utf8_unchecked(SERIAL.get().unwrap()) 54 } 55} 56 57 58pub fn log(message: &str, level: LogLevel) { 59 let len = message.len(); 60 let ptr = message.as_ptr(); 61 62 unsafe { 63 syscall!( 64 SyscallNumber::Misc, 65 in MiscSyscall::LogMessage, 66 in level, 67 in len, 68 in ptr, 69 ); 70 } 71} 72 73pub fn trace(message: &str) { 74 log(message, LogLevel::Trace); 75} 76 77pub fn debug(message: &str) { 78 log(message, LogLevel::Debug); 79} 80 81pub fn info(message: &str) { 82 log(message, LogLevel::Info); 83} 84 85pub fn warn(message: &str) { 86 log(message, LogLevel::Warn); 87} 88 89pub fn error(message: &str) { 90 log(message, LogLevel::Error); 91} 92 93pub fn get_time_micros() -> u64 { 94 let mut low_word: u32; 95 let mut high_word: u32; 96 97 unsafe { 98 syscall!( 99 SyscallNumber::Misc, 100 out high_word in MiscSyscall::GetTimeMicros, 101 out low_word, 102 ) 103 } 104 105 ((high_word as u64) << 32) | low_word as u64 106} 107 108 109#[cfg(feature = "fugit")] 110pub type Instant = fugit::TimerInstantU64<1_000_000>; 111 112#[cfg(feature = "fugit")] 113pub fn now() -> Instant { 114 Instant::from_ticks(get_time_micros()) 115}