firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
1use core::arch::asm;
2use core::mem::MaybeUninit;
3use crate::{syscall, SafeOption};
4use crate::input_common::Event;
5use crate::syscall::SyscallNumber;
6
7#[repr(usize)]
8#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)]
9#[cfg_attr(feature = "defmt", derive(defmt::Format))]
10pub enum InputSyscall {
11 NextEvent = 0,
12 SetTouchEnabled = 1,
13 HasEvent = 2,
14}
15
16pub fn next_event() -> Option<Event> {
17 let mut event: MaybeUninit<SafeOption<Event>> = MaybeUninit::uninit();
18
19 unsafe {
20 syscall!(
21 SyscallNumber::Input,
22 in InputSyscall::NextEvent,
23 in event.as_mut_ptr(),
24 );
25
26 event.assume_init().into()
27 }
28}
29
30pub fn set_touch_enabled(enabled: bool) {
31 unsafe {
32 syscall!(
33 SyscallNumber::Input,
34 in InputSyscall::SetTouchEnabled,
35 in enabled,
36 );
37 }
38}
39
40pub fn has_event() -> bool {
41 let mut has_event: usize;
42
43 unsafe {
44 syscall!(
45 SyscallNumber::Input,
46 out has_event in InputSyscall::HasEvent,
47 );
48 }
49
50 has_event != 0
51}
52
53/// If there are no events remaining in the event queue, allow the processor to enter a sleep state
54/// until there is a new event ready.
55#[inline(always)]
56pub fn eep() {
57 if !has_event() {
58 // SAFETY: "wfe" only (maybe) puts the processor to sleep temporarily and doesn't access any
59 // memory so this specific instruction is safe
60 // has_event() is a syscall. The SVCall exception is a WFE wakeup event, so we need two
61 // WFEs so we don't immediately wake up.
62 unsafe { asm!("wfe", "wfe"); }
63 }
64}