From ac40075140ef99b7c3f74f4a3d2d0e93cb7704e0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 16 Apr 2021 04:11:25 +0800 Subject: [PATCH] add keyboard input via UART --- emulation/renode.svd | 149 ++++++++++++++++++++++++-------- services/keyboard/src/api.rs | 4 +- services/keyboard/src/lib.rs | 2 +- services/keyboard/src/main.rs | 90 ++++++++++--------- services/log-server/Cargo.toml | 2 +- services/log-server/src/main.rs | 19 +++- svd2utra/examples/soc.svd | 149 ++++++++++++++++++++++++-------- xous-rs/src/names.rs | 2 +- 8 files changed, 299 insertions(+), 118 deletions(-) diff --git a/emulation/renode.svd b/emulation/renode.svd index 46b0a65dd..e69380bcb 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x79edd899 + 0xd2990a8c 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x16 + 0x17 32 @@ -2584,10 +2584,32 @@ checked]]> 0xF0014000 KEYBOARD + + UART_CHAR + 0x0000 + 0x00 + 32 + + + char + 7 + [7:0] + 0 + + + + stb + 8 + [8:8] + 8 + + + + ROW0DAT - 0x0000 + 0x0004 0x00 32 @@ -2602,7 +2624,7 @@ checked]]> ROW1DAT - 0x0004 + 0x0008 0x00 32 @@ -2617,7 +2639,7 @@ checked]]> ROW2DAT - 0x0008 + 0x000c 0x00 32 @@ -2632,7 +2654,7 @@ checked]]> ROW3DAT - 0x000c + 0x0010 0x00 32 @@ -2647,7 +2669,7 @@ checked]]> ROW4DAT - 0x0010 + 0x0014 0x00 32 @@ -2662,7 +2684,7 @@ checked]]> ROW5DAT - 0x0014 + 0x0018 0x00 32 @@ -2677,7 +2699,7 @@ checked]]> ROW6DAT - 0x0018 + 0x001c 0x00 32 @@ -2692,7 +2714,7 @@ checked]]> ROW7DAT - 0x001c + 0x0020 0x00 32 @@ -2707,7 +2729,7 @@ checked]]> ROW8DAT - 0x0020 + 0x0024 0x00 32 @@ -2721,8 +2743,8 @@ checked]]> EV_STATUS - - 0x0024 + + 0x0028 0x00 32 @@ -2733,12 +2755,19 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + EV_PENDING - - 0x0028 + + 0x002c 0x00 32 @@ -2749,12 +2778,19 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + EV_ENABLE - - 0x002c + + 0x0030 0x00 32 @@ -2765,13 +2801,20 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + ROWCHANGE - 0x0030 + 0x0034 0x00 32 @@ -2786,7 +2829,7 @@ again until the interrupt is serviced.]]> 0 - 0x34 + 0x38 registers @@ -2795,15 +2838,51 @@ again until the interrupt is serviced.]]> - SEED + KEYINJECT 0xF0015000 + KEYINJECT + + + UART_CHAR + 0x0000 + 0x00 + 32 + + + char + 7 + [7:0] + 0 + + + + disable + 8 + [8:8] + 8 + + + + + + + 0 + 0x4 + registers + + + + SEED + 0xF0016000 SEED SEED1 0x0000 - 0x35b7f4fa + 0x5b50bb74 32 @@ -2818,7 +2897,7 @@ again until the interrupt is serviced.]]> SEED0 0x0004 - 0x10dd44b4 + 0x1b383b56 32 @@ -2838,7 +2917,7 @@ again until the interrupt is serviced.]]> KEYROM - 0xF0016000 + 0xF0017000 KEYROM @@ -2880,7 +2959,7 @@ again until the interrupt is serviced.]]> AUDIO - 0xF0017000 + 0xF0018000 AUDIO @@ -3252,7 +3331,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> TRNG_KERNEL - 0xF0018000 + 0xF0019000 TRNG_KERNEL @@ -3388,7 +3467,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> TRNG_SERVER - 0xF0019000 + 0xF001A000 TRNG_SERVER @@ -3720,7 +3799,7 @@ to max field size]]> TRNG - 0xF001A000 + 0xF001B000 TRNG @@ -4033,7 +4112,7 @@ VCCBRAM (V) = ``Value`` x 3 / 4096.]]> SHA512 - 0xF001B000 + 0xF001C000 SHA512 @@ -4528,7 +4607,7 @@ in bits]]> ENGINE - 0xF001C000 + 0xF001D000 ENGINE @@ -4757,7 +4836,7 @@ exactly reflects the binary layout of an Engine instruction.]]> JTAG - 0xF001D000 + 0xF001E000 JTAG @@ -4815,7 +4894,7 @@ exactly reflects the binary layout of an Engine instruction.]]> WDT - 0xF001E000 + 0xF001F000 WDT @@ -4975,7 +5054,7 @@ register to disable individual events.]]> IDENTIFIER_MEM - 0xF001F000 + 0xF0020000 IDENTIFIER_MEM @@ -5002,7 +5081,7 @@ register to disable individual events.]]> TIMER0 - 0xF0020000 + 0xF0021000 TIMER0 diff --git a/services/keyboard/src/api.rs b/services/keyboard/src/api.rs index de934b28e..fa1bdb2d5 100644 --- a/services/keyboard/src/api.rs +++ b/services/keyboard/src/api.rs @@ -174,8 +174,8 @@ pub(crate) enum Opcode { /// set chording interval (how long to wait for all keydowns to happen before interpreting as a chord), in ms (for braille keyboards) SetChordInterval, //(u32), - /// used by host mode emulation to inject keys - HostModeInjectKey, //(char), + /// used by host mode emulation and debug UART to inject keys + InjectKey, //(char), /// used by the interrupt handler to transfer results to the main loop HandlerTrigger, diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index 066892074..73bce355b 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -65,7 +65,7 @@ impl Keyboard { #[cfg(not(target_os = "none"))] pub fn hostmode_inject_key(&self, c: char) { send_message(self.conn, - Message::new_scalar(Opcode::HostModeInjectKey.to_usize().unwrap(), + Message::new_scalar(Opcode::InjectKey.to_usize().unwrap(), c as u32 as usize, 0, 0, 0 )).unwrap(); } diff --git a/services/keyboard/src/main.rs b/services/keyboard/src/main.rs index f809a3153..696a5f45f 100644 --- a/services/keyboard/src/main.rs +++ b/services/keyboard/src/main.rs @@ -215,11 +215,20 @@ mod implementation { fn handle_kbd(_irq_no: usize, arg: *mut usize) { let kbd = unsafe { &mut *(arg as *mut Keyboard) }; - kbd.new_state = kbd.kbd_getcodes(); - kbd.csr.wfo(utra::keyboard::EV_PENDING_KEYPRESSED, 1); // clear the interrupt + if kbd.csr.rf(utra::keyboard::EV_PENDING_KEYPRESSED) != 0 { + kbd.new_state = kbd.kbd_getcodes(); + kbd.csr.wfo(utra::keyboard::EV_PENDING_KEYPRESSED, 1); // clear the interrupt - xous::try_send_message(kbd.conn, - xous::Message::new_scalar(Opcode::HandlerTrigger.to_usize().unwrap(), 0, 0, 0, 0)).ok(); + xous::try_send_message(kbd.conn, + xous::Message::new_scalar(Opcode::HandlerTrigger.to_usize().unwrap(), 0, 0, 0, 0)).ok(); + } + if kbd.csr.rf(utra::keyboard::EV_PENDING_INJECT) != 0 { + let c = kbd.csr.rf(utra::keyboard::UART_CHAR_CHAR); + kbd.csr.wfo(utra::keyboard::EV_PENDING_INJECT, 1); // clear the interrupt + xous::try_send_message(kbd.conn, + xous::Message::new_scalar(Opcode::InjectKey.to_usize().unwrap(), c as _, 0, 0, 0) + ).ok(); + } } impl Keyboard { @@ -241,7 +250,7 @@ mod implementation { new_state: RowColVec::new(), last_state: RowColVec::new(), ticktimer, - map: KeyMap::Qwerty, + map: KeyMap::Braille, delay: 500, rate: 20, shift_down: false, @@ -262,9 +271,11 @@ mod implementation { (&mut kbd) as *mut Keyboard as *mut usize, ) .expect("couldn't claim irq"); - kbd.csr.wfo(utra::keyboard::EV_PENDING_KEYPRESSED, 1); // clear in case it's pending for some reason - kbd.csr.wfo(utra::keyboard::EV_ENABLE_KEYPRESSED, 1); - + kbd.csr.wo(utra::keyboard::EV_PENDING, kbd.csr.r(utra::keyboard::EV_PENDING)); // clear in case it's pending for some reason + kbd.csr.wo(utra::keyboard::EV_ENABLE, + kbd.csr.ms(utra::keyboard::EV_ENABLE_KEYPRESSED, 1) | + kbd.csr.ms(utra::keyboard::EV_ENABLE_INJECT, 1) + ); log::trace!("hardware initialized"); kbd @@ -389,21 +400,18 @@ mod implementation { self.chord[rc.r as usize][rc.c as usize] = true; } } - if let Some(kus) = &keyups { - for &rc in kus.iter() { - self.chord[rc.r as usize][rc.c as usize] = false; - } - } - + log::trace!("self.chord: {:?}", self.chord); let mut keystates: Vec = Vec::new(); if self.chord_active || keydowns.is_some() { let now = self.ticktimer.elapsed_ms(); if !self.chord_active && keydowns.is_some() { + log::trace!("chord_active set"); self.chord_active = true; self.chord_timestamp = now; } else if self.chord_active && ((now - self.chord_timestamp) >= self.chord_interval as u64) { + log::trace!("interpreting chords"); // extract chord state /* keyboard: @@ -427,6 +435,7 @@ mod implementation { keycode |= 1 << i; } } + log::trace!("keycode: 0x{:x}", keycode); let keychar = match keycode { 0b000_001 => 'a', 0b000_011 => 'b', @@ -487,9 +496,15 @@ mod implementation { if func { keystates.push('🏁').unwrap(); } } } + if let Some(kus) = &keyups { + for &rc in kus.iter() { + self.chord[rc.r as usize][rc.c as usize] = false; + } + } // not sure if this is the right way to handle keyups, but let's try it. if keyups.is_some() { self.chord_active = false; + log::trace!("chord_active going false"); } keystates @@ -776,7 +791,7 @@ fn send_rawstates(cb_conns: &mut [Option; 16], krs: &KeyRawStates) { fn xmain() -> ! { use crate::implementation::Keyboard; log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Trace); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -834,30 +849,27 @@ fn xmain() -> ! { Some(Opcode::SetChordInterval) => msg_scalar_unpack!(msg, delay, _, _, _, { kbd.set_chord_interval(delay as u32); }), - Some(Opcode::HostModeInjectKey) => msg_scalar_unpack!(msg, _k, _, _, _, { - #[cfg(not(target_os = "none"))] - { - let key = if let Some(a) = core::char::from_u32(_k as u32) { - a - } else { - '\u{0000}' - }; - info!("injecting emulation key press '{}'", key); - for maybe_conn in normal_conns.iter_mut() { - if let Some(conn) = maybe_conn { - match xous::send_message(*conn, - xous::Message::new_scalar(api::Callback::KeyEvent.to_usize().unwrap(), - key as u32 as usize, - '\u{0000}' as u32 as usize, - '\u{0000}' as u32 as usize, - '\u{0000}' as u32 as usize, - )) { - Err(xous::Error::ServerNotFound) => { - *maybe_conn = None - }, - Ok(xous::Result::Ok) => {}, - _ => log::error!("unhandled error in key event sending") - } + Some(Opcode::InjectKey) => msg_scalar_unpack!(msg, _k, _, _, _, { + let key = if let Some(a) = core::char::from_u32(_k as u32) { + a + } else { + '\u{0000}' + }; + info!("injecting emulation key press '{}'", key); + for maybe_conn in normal_conns.iter_mut() { + if let Some(conn) = maybe_conn { + match xous::send_message(*conn, + xous::Message::new_scalar(api::Callback::KeyEvent.to_usize().unwrap(), + key as u32 as usize, + '\u{0000}' as u32 as usize, + '\u{0000}' as u32 as usize, + '\u{0000}' as u32 as usize, + )) { + Err(xous::Error::ServerNotFound) => { + *maybe_conn = None + }, + Ok(xous::Result::Ok) => {}, + _ => log::error!("unhandled error in key event sending") } } } diff --git a/services/log-server/Cargo.toml b/services/log-server/Cargo.toml index a7bf66f87..4e45173a6 100644 --- a/services/log-server/Cargo.toml +++ b/services/log-server/Cargo.toml @@ -19,7 +19,7 @@ utralib = {path = "../../utralib"} [features] debugprint = [] # adding this allocates the UART for debugging the logger -logging = [] # adding this allocates the UART for logging of Xous +logging = [] # adding this allocates the hardware UART for console interactions #default = [] default = ["logging"] # default = ["debugprint", "logging"] diff --git a/services/log-server/src/main.rs b/services/log-server/src/main.rs index e125a0fbd..5f4c2927e 100644 --- a/services/log-server/src/main.rs +++ b/services/log-server/src/main.rs @@ -119,11 +119,20 @@ mod implementation { println!("Process: map success!"); crate::debug::DEFAULT.enable_rx(); + let inject_mem = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::keyinject::HW_KEYINJECT_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map keyinjection CSR range"); + println!("Note: character injection via console UART is enabled."); + println!("Allocating IRQ..."); xous::syscall::claim_interrupt( utra::console::CONSOLE_IRQ, handle_irq, - core::ptr::null_mut::(), + inject_mem.as_mut_ptr() as *mut usize, ) .expect("couldn't claim interrupt"); println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ); @@ -143,12 +152,14 @@ mod implementation { } } - fn handle_irq(irq_no: usize, arg: *mut usize) { + fn handle_irq(_irq_no: usize, arg: *mut usize) { if cfg!(feature = "logging") { - print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); + let mut inject_csr = CSR::new(arg as *mut u32); + //print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); while let Some(c) = crate::debug::DEFAULT.getc() { - print!("0x{:02x}", c); + //print!("0x{:02x} ", c); + inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, c as u32); } println!(); } diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 46b0a65dd..e69380bcb 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x79edd899 + 0xd2990a8c 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x16 + 0x17 32 @@ -2584,10 +2584,32 @@ checked]]> 0xF0014000 KEYBOARD + + UART_CHAR + 0x0000 + 0x00 + 32 + + + char + 7 + [7:0] + 0 + + + + stb + 8 + [8:8] + 8 + + + + ROW0DAT - 0x0000 + 0x0004 0x00 32 @@ -2602,7 +2624,7 @@ checked]]> ROW1DAT - 0x0004 + 0x0008 0x00 32 @@ -2617,7 +2639,7 @@ checked]]> ROW2DAT - 0x0008 + 0x000c 0x00 32 @@ -2632,7 +2654,7 @@ checked]]> ROW3DAT - 0x000c + 0x0010 0x00 32 @@ -2647,7 +2669,7 @@ checked]]> ROW4DAT - 0x0010 + 0x0014 0x00 32 @@ -2662,7 +2684,7 @@ checked]]> ROW5DAT - 0x0014 + 0x0018 0x00 32 @@ -2677,7 +2699,7 @@ checked]]> ROW6DAT - 0x0018 + 0x001c 0x00 32 @@ -2692,7 +2714,7 @@ checked]]> ROW7DAT - 0x001c + 0x0020 0x00 32 @@ -2707,7 +2729,7 @@ checked]]> ROW8DAT - 0x0020 + 0x0024 0x00 32 @@ -2721,8 +2743,8 @@ checked]]> EV_STATUS - - 0x0024 + + 0x0028 0x00 32 @@ -2733,12 +2755,19 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + EV_PENDING - - 0x0028 + + 0x002c 0x00 32 @@ -2749,12 +2778,19 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + EV_ENABLE - - 0x002c + + 0x0030 0x00 32 @@ -2765,13 +2801,20 @@ checked]]> 0 + + inject + 1 + [1:1] + 1 + + ROWCHANGE - 0x0030 + 0x0034 0x00 32 @@ -2786,7 +2829,7 @@ again until the interrupt is serviced.]]> 0 - 0x34 + 0x38 registers @@ -2795,15 +2838,51 @@ again until the interrupt is serviced.]]> - SEED + KEYINJECT 0xF0015000 + KEYINJECT + + + UART_CHAR + 0x0000 + 0x00 + 32 + + + char + 7 + [7:0] + 0 + + + + disable + 8 + [8:8] + 8 + + + + + + + 0 + 0x4 + registers + + + + SEED + 0xF0016000 SEED SEED1 0x0000 - 0x35b7f4fa + 0x5b50bb74 32 @@ -2818,7 +2897,7 @@ again until the interrupt is serviced.]]> SEED0 0x0004 - 0x10dd44b4 + 0x1b383b56 32 @@ -2838,7 +2917,7 @@ again until the interrupt is serviced.]]> KEYROM - 0xF0016000 + 0xF0017000 KEYROM @@ -2880,7 +2959,7 @@ again until the interrupt is serviced.]]> AUDIO - 0xF0017000 + 0xF0018000 AUDIO @@ -3252,7 +3331,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> TRNG_KERNEL - 0xF0018000 + 0xF0019000 TRNG_KERNEL @@ -3388,7 +3467,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> TRNG_SERVER - 0xF0019000 + 0xF001A000 TRNG_SERVER @@ -3720,7 +3799,7 @@ to max field size]]> TRNG - 0xF001A000 + 0xF001B000 TRNG @@ -4033,7 +4112,7 @@ VCCBRAM (V) = ``Value`` x 3 / 4096.]]> SHA512 - 0xF001B000 + 0xF001C000 SHA512 @@ -4528,7 +4607,7 @@ in bits]]> ENGINE - 0xF001C000 + 0xF001D000 ENGINE @@ -4757,7 +4836,7 @@ exactly reflects the binary layout of an Engine instruction.]]> JTAG - 0xF001D000 + 0xF001E000 JTAG @@ -4815,7 +4894,7 @@ exactly reflects the binary layout of an Engine instruction.]]> WDT - 0xF001E000 + 0xF001F000 WDT @@ -4975,7 +5054,7 @@ register to disable individual events.]]> IDENTIFIER_MEM - 0xF001F000 + 0xF0020000 IDENTIFIER_MEM @@ -5002,7 +5081,7 @@ register to disable individual events.]]> TIMER0 - 0xF0020000 + 0xF0021000 TIMER0 diff --git a/xous-rs/src/names.rs b/xous-rs/src/names.rs index fea959834..2bd0cd5cc 100644 --- a/xous-rs/src/names.rs +++ b/xous-rs/src/names.rs @@ -2,7 +2,7 @@ /* A list of descriptions of servers ("names") that are used as keys by the xous-names server -to refer to servers that are not statically bound into the kernel +to refer to servers that are not statically bound into the kernel, or have a special debug purpose. */ pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_";