fix dabao target - keyboard server

dabao needs to have a vestigial keyboard server which handles
forwarding USB/serial key events to listeners. otherwise, there
is no input interface.
This commit is contained in:
bunnie 2025-11-28 11:35:17 +08:00
parent 1c31390106
commit 647ec0dfb4
3 changed files with 79 additions and 46 deletions

View file

@ -256,7 +256,6 @@ fn main() {
// The index of the array corresponds to the slot. // The index of the array corresponds to the slot.
let mut irq_table: [Option<IrqLocalRegistration>; 8] = [None; 8]; let mut irq_table: [Option<IrqLocalRegistration>; 8] = [None; 8];
#[cfg(feature = "board-baosec")]
servers::keyboard::start_keyboard_service(); servers::keyboard::start_keyboard_service();
// claim BIO // claim BIO

View file

@ -1,4 +1,3 @@
#[cfg(feature = "board-baosec")]
pub mod keyboard; pub mod keyboard;
// TRNG server is only needed for baosec; dabao can cheat and use the kernel TRNG port // TRNG server is only needed for baosec; dabao can cheat and use the kernel TRNG port

View file

@ -1,16 +1,24 @@
#[cfg(feature = "board-baosec")]
use arbitrary_int::{Number, u4}; use arbitrary_int::{Number, u4};
#[cfg(feature = "board-baosec")]
use bao1x_api::IrqNotification; use bao1x_api::IrqNotification;
use bao1x_api::keyboard::*; use bao1x_api::keyboard::*;
#[cfg(feature = "board-baosec")]
use bao1x_hal::board::KeyPress; use bao1x_hal::board::KeyPress;
#[cfg(feature = "board-baosec")]
use bao1x_hal::kpc_aoint::{AoIntStatus, KpcAoInt}; use bao1x_hal::kpc_aoint::{AoIntStatus, KpcAoInt};
use num_traits::*; use num_traits::*;
#[cfg(feature = "board-baosec")]
use utralib::utra::irqarray2; use utralib::utra::irqarray2;
#[cfg(feature = "board-baosec")]
use utralib::*; use utralib::*;
use xous::{CID, MessageSender, msg_blocking_scalar_unpack, msg_scalar_unpack}; use xous::{CID, MessageSender, msg_blocking_scalar_unpack, msg_scalar_unpack};
use xous_ipc::Buffer; use xous_ipc::Buffer;
#[cfg(feature = "board-baosec")]
const KEYUP_DELAY_MS: u64 = 80; const KEYUP_DELAY_MS: u64 = 80;
#[cfg(feature = "board-baosec")]
pub fn handler(_irq_no: usize, arg: *mut usize) { pub fn handler(_irq_no: usize, arg: *mut usize) {
let kpc_aoint = unsafe { &mut *(arg as *mut KpcAoInt) }; let kpc_aoint = unsafe { &mut *(arg as *mut KpcAoInt) };
let pending = kpc_aoint.irq.r(irqarray2::EV_PENDING); let pending = kpc_aoint.irq.r(irqarray2::EV_PENDING);
@ -43,15 +51,18 @@ pub fn handler(_irq_no: usize, arg: *mut usize) {
} }
} }
#[cfg(feature = "board-baosec")]
struct KeypressTimestamp { struct KeypressTimestamp {
pub kp: KeyPress, pub kp: KeyPress,
pub next_repeat_time: u64, pub next_repeat_time: u64,
} }
#[cfg(feature = "board-baosec")]
struct KeyTracker { struct KeyTracker {
pub rate_ms: usize, pub rate_ms: usize,
pub delay_ms: usize, pub delay_ms: usize,
pub keys: Vec<KeypressTimestamp>, pub keys: Vec<KeypressTimestamp>,
} }
#[cfg(feature = "board-baosec")]
impl KeyTracker { impl KeyTracker {
pub fn new() -> Self { KeyTracker { rate_ms: KEYUP_DELAY_MS as usize, delay_ms: 500, keys: Vec::new() } } pub fn new() -> Self { KeyTracker { rate_ms: KEYUP_DELAY_MS as usize, delay_ms: 500, keys: Vec::new() } }
@ -113,7 +124,7 @@ impl KeyTracker {
pub fn clear_keys(&mut self) { self.keys.clear(); } pub fn clear_keys(&mut self) { self.keys.clear(); }
} }
#[cfg(feature = "board-baosec")]
fn map_keypress(kp: KeyPress) -> char { fn map_keypress(kp: KeyPress) -> char {
match kp { match kp {
KeyPress::Down => '↓', KeyPress::Down => '↓',
@ -162,53 +173,68 @@ fn keyboard_service() {
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
// "own" the KPC & Always-on registers // "own" the KPC & Always-on registers
#[cfg(feature = "board-baosec")]
{
let iox = crate::iox::IoxHal::new();
let (_row, _col) = bao1x_hal::board::setup_kpc_pins(&iox);
}
#[cfg(feature = "board-baosec")]
let mut kpc_aoint = bao1x_hal::kpc_aoint::KpcAoInt::new(Some(handler)); let mut kpc_aoint = bao1x_hal::kpc_aoint::KpcAoInt::new(Some(handler));
kpc_aoint.ao.wo(utra::ao_sysctrl::SFR_IOX, 1); // connect PF directly to KP unit, overrides IO mux even??? #[cfg(feature = "board-baosec")]
kpc_aoint.ao.wo(utra::ao_sysctrl::CR_WKUPMASK, 0x3F); {
kpc_aoint.ao.wo(utra::ao_sysctrl::SFR_IOX, 1); // connect PF directly to KP unit, overrides IO mux even???
kpc_aoint.ao.wo(utra::ao_sysctrl::CR_WKUPMASK, 0x3F);
// KPOPO0 defines drive state in phase 0 - here we set it to high // KPOPO0 defines drive state in phase 0 - here we set it to high
// KPOPO1 defines drive state in phase 1 - here we set it to low // KPOPO1 defines drive state in phase 1 - here we set it to low
// KPOE0 defines OE state in phase 0 - here we set it to drive // KPOE0 defines OE state in phase 0 - here we set it to drive
// KPOE1 defines OE state in phase 1 - here we set it to drive // KPOE1 defines OE state in phase 1 - here we set it to drive
let cfg0 = kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOPO0, 1) let cfg0 = kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOPO0, 1)
// | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOPO1, 1) // | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOPO1, 1)
| kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOOE0, 1) | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOOE0, 1)
| kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOOE1, 1) | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_KPOOE1, 1)
| kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_DKPCEN, 1); | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG0_DKPCEN, 1);
kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG0, cfg0); kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG0, cfg0);
let cfg1 = kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_STEP, 2) let cfg1 = kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_STEP, 2)
| kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_FILTER, 2) | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_FILTER, 2)
| kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_CNT1MS, 4); | kpc_aoint.kpc.ms(utra::dkpc::SFR_CFG1_CFG_CNT1MS, 4);
kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG1, cfg1); kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG1, cfg1);
// CFG2 has format of interval kpo | stop drive kpo | sample point kpi | start drive kpo, each 8 bits wide // CFG2 has format of interval kpo | stop drive kpo | sample point kpi | start drive kpo, each 8 bits
kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG2, 0x40_05_03_01); // sets ~24ms scanning rate // wide
kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG3, 0xFFFF_0000); // fall[15:0] | rise[15:0] detection kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG2, 0x40_05_03_01); // sets ~24ms scanning rate
// this is the deep sleep interval for debouncing the keyboard array kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG3, 0xFFFF_0000); // fall[15:0] | rise[15:0] detection
kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG4, 256); // this is the deep sleep interval for debouncing the keyboard array
// drain any pending events kpc_aoint.kpc.wo(utra::dkpc::SFR_CFG4, 256);
while kpc_aoint.kpc.r(utra::dkpc::SFR_SR1) != 0 { // drain any pending events
// this register didn't get mapped in register extraction because its type while kpc_aoint.kpc.r(utra::dkpc::SFR_SR1) != 0 {
// is `apb_buf2`: FIXME - adjust the register extraction script to capture this type. // this register didn't get mapped in register extraction because its type
// this register drains the pending interrupts from the wakeup/keyboard queue // is `apb_buf2`: FIXME - adjust the register extraction script to capture this type.
let _ = unsafe { kpc_aoint.kpc.base().add(8).read_volatile() }; // this register drains the pending interrupts from the wakeup/keyboard queue
let _ = unsafe { kpc_aoint.kpc.base().add(8).read_volatile() };
}
} }
let kbd_sid = xns.register_name(bao1x_api::SERVER_NAME_KBD, None).expect("can't register server"); let kbd_sid = xns.register_name(bao1x_api::SERVER_NAME_KBD, None).expect("can't register server");
let kbd_conn = xous::connect(kbd_sid).unwrap();
let kpc_int = u4::new(bao1x_hal::kpc_aoint::IrqMapping::AoInt as u8); #[cfg(feature = "board-baosec")]
kpc_aoint.add_irq_notifier(IrqNotification { {
bit: kpc_int, let kbd_conn = xous::connect(kbd_sid).unwrap();
conn: kbd_conn,
opcode: KeyboardOpcode::HandlerTrigger.to_usize().unwrap(),
args: [0, 0, 0, 0],
});
// feels like a bit of an abstraction violation, but I don't know how much the other interrupts
// require edge-triggered handling
kpc_aoint.irq.wo(utra::irqarray2::EV_EDGE_TRIGGERED, 1 << kpc_int.as_u32());
kpc_aoint.irq.wo(utra::irqarray2::EV_POLARITY, 1 << kpc_int.as_u32());
kpc_aoint.modify_irq_ena(kpc_int, true);
let kpc_int = u4::new(bao1x_hal::kpc_aoint::IrqMapping::AoInt as u8);
kpc_aoint.add_irq_notifier(IrqNotification {
bit: kpc_int,
conn: kbd_conn,
opcode: KeyboardOpcode::HandlerTrigger.to_usize().unwrap(),
args: [0, 0, 0, 0],
});
// feels like a bit of an abstraction violation, but I don't know how much the other interrupts
// require edge-triggered handling
kpc_aoint.irq.wo(utra::irqarray2::EV_EDGE_TRIGGERED, 1 << kpc_int.as_u32());
kpc_aoint.irq.wo(utra::irqarray2::EV_POLARITY, 1 << kpc_int.as_u32());
kpc_aoint.modify_irq_ena(kpc_int, true);
}
#[cfg(feature = "board-baosec")]
let tt = ticktimer::Ticktimer::new().unwrap(); let tt = ticktimer::Ticktimer::new().unwrap();
let mut listeners: Vec<(CID, usize)> = Vec::new(); let mut listeners: Vec<(CID, usize)> = Vec::new();
@ -220,9 +246,6 @@ fn keyboard_service() {
// storage for any blocking listeners // storage for any blocking listeners
let mut blocking_listener = Vec::<MessageSender>::new(); let mut blocking_listener = Vec::<MessageSender>::new();
let iox = crate::iox::IoxHal::new();
let (_row, _col) = bao1x_hal::board::setup_kpc_pins(&iox);
#[cfg(feature = "keyboard-testing")] #[cfg(feature = "keyboard-testing")]
// this routine is useful for mapping out raw keys on new hardware builds // this routine is useful for mapping out raw keys on new hardware builds
std::thread::spawn({ std::thread::spawn({
@ -254,7 +277,9 @@ fn keyboard_service() {
} }
}); });
#[cfg(feature = "board-baosec")]
let mut key_tracker = KeyTracker::new(); let mut key_tracker = KeyTracker::new();
#[cfg(feature = "board-baosec")]
let mut last_key_event = 0u64; let mut last_key_event = 0u64;
loop { loop {
let msg = xous::receive_message(kbd_sid).unwrap(); // this blocks until we get a message let msg = xous::receive_message(kbd_sid).unwrap(); // this blocks until we get a message
@ -302,10 +327,15 @@ fn keyboard_service() {
log::warn!("Defaulting to DVORAK map"); log::warn!("Defaulting to DVORAK map");
xous::return_scalar(msg.sender, KeyMap::Dvorak.into()).expect("can't retrieve keymap"); xous::return_scalar(msg.sender, KeyMap::Dvorak.into()).expect("can't retrieve keymap");
}), }),
#[cfg(feature = "board-baosec")]
Some(KeyboardOpcode::SetRepeat) => msg_scalar_unpack!(msg, rate, delay, _, _, { Some(KeyboardOpcode::SetRepeat) => msg_scalar_unpack!(msg, rate, delay, _, _, {
key_tracker.rate_ms = rate; key_tracker.rate_ms = rate;
key_tracker.delay_ms = delay; key_tracker.delay_ms = delay;
}), }),
#[cfg(not(feature = "board-baosec"))]
Some(KeyboardOpcode::SetRepeat) => {
msg_scalar_unpack!(msg, _rate, _delay, _, _, { unimplemented!() })
}
Some(KeyboardOpcode::SetChordInterval) => msg_scalar_unpack!(msg, _delay, _, _, _, { Some(KeyboardOpcode::SetChordInterval) => msg_scalar_unpack!(msg, _delay, _, _, _, {
// chording allows us to interpret multiple key hits as a whole new separate key. // chording allows us to interpret multiple key hits as a whole new separate key.
// for now we don't implement this feature. // for now we don't implement this feature.
@ -401,6 +431,11 @@ fn keyboard_service() {
xous::return_scalar2(listener, key as u32 as usize, 0).unwrap(); xous::return_scalar2(listener, key as u32 as usize, 0).unwrap();
} }
}), }),
#[cfg(not(feature = "board-baosec"))]
Some(KeyboardOpcode::HandlerTrigger) => msg_scalar_unpack!(msg, _pending, _, _, _, {
log::error!("This target does not support keyboard interrupts, yet somehow we got one!");
}),
#[cfg(feature = "board-baosec")]
Some(KeyboardOpcode::HandlerTrigger) => msg_scalar_unpack!(msg, pending, _, _, _, { Some(KeyboardOpcode::HandlerTrigger) => msg_scalar_unpack!(msg, pending, _, _, _, {
if pending & (1 << bao1x_hal::kpc_aoint::IrqMapping::AoInt as usize) != 0 { if pending & (1 << bao1x_hal::kpc_aoint::IrqMapping::AoInt as usize) != 0 {
let mut kc: Vec<char> = Vec::new(); let mut kc: Vec<char> = Vec::new();