From 2805f7a14a8651eff5ea81e80688064f545b16c5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 29 Jan 2026 00:09:10 +0800 Subject: [PATCH] fix deadlock on boot issue io need to be active early on to get clocks to function on baosec putting that in its own thread fixes the issue. --- services/bao1x-hal-service/src/main.rs | 952 +++++++++++++------------ 1 file changed, 484 insertions(+), 468 deletions(-) diff --git a/services/bao1x-hal-service/src/main.rs b/services/bao1x-hal-service/src/main.rs index 09d3f0c78..7e09c67a6 100644 --- a/services/bao1x-hal-service/src/main.rs +++ b/services/bao1x-hal-service/src/main.rs @@ -137,137 +137,488 @@ fn main() { let sid = xns.register_name(bao1x_api::SERVER_NAME_BAO1X_HAL, None).expect("can't register server"); let self_cid = xous::connect(sid).expect("couldn't create self-connection"); - let mut ifram_allocs = [Vec::new(), Vec::new()]; - // code is written assuming the IFRAM blocks have the same size. Since this is fixed in - // hardware, it's a good assumption; but the assert is put here in case we port this to - // a new system where for some reason they have different sizes. - assert!(utralib::generated::HW_IFRAM0_MEM_LEN == utralib::generated::HW_IFRAM1_MEM_LEN); - let pages = utralib::generated::HW_IFRAM0_MEM_LEN / 4096; - for _ in 0..pages { - ifram_allocs[0].push(None); - ifram_allocs[1].push(None); - } - // mark loader-hardwired pages for IFRAM0 - for i in bao1x_hal::board::IFRAM0_RESERVED_PAGE_RANGE[0]..=bao1x_hal::board::IFRAM0_RESERVED_PAGE_RANGE[1] - { - ifram_allocs[0][i] = Some(Sender::from_usize(usize::MAX)); - } - // mark loader-hardwired pages for IFRAM1 - for i in bao1x_hal::board::IFRAM1_RESERVED_PAGE_RANGE[0]..=bao1x_hal::board::IFRAM1_RESERVED_PAGE_RANGE[1] - { - ifram_allocs[1][i] = Some(Sender::from_usize(usize::MAX)); - } - // Second page from top of IFRAM0 is occupied by the swap handler. This was allocated - // by the loader, before the kernel even started. Mark the PID properly. - #[cfg(feature = "swap")] - { - ifram_allocs[0][30] = Some(Sender::from_usize(SWAPPER_PID as usize)); - } - - let iox_page = xous::syscall::map_memory( - xous::MemoryAddress::new(utralib::generated::HW_IOX_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't claim the IOX hardware page"); - let iox = Iox::new(iox_page.as_ptr() as *mut u32); - - let udma_global = GlobalConfig::new(); - - // Note: the I2C handler can be put into a separate thread if we need the main - // HAL server to not block while a large I2C transaction is being handled. For - // now this is all placed into a single thread. However, if we ever had a situation - // where, for example, you had to do a compound I2C transaction and flip a GPIO pin - // in the middle of that transaction in order for the set of I2C transactions to - // complete, this implementation would deadlock as it would block on the I2C transaction - // before handling the GPIO request. - let i2c_channel = bao1x_hal::board::setup_i2c_pins(&iox); - udma_global.clock_on(PeriphId::from(i2c_channel)); - let i2c_pages = xous::syscall::map_memory( - xous::MemoryAddress::new(bao1x_hal::board::I2C_IFRAM_ADDR), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't claim I2C IFRAM page"); - - let i2c_ifram = unsafe { - bao1x_hal::ifram::IframRange::from_raw_parts( - bao1x_hal::board::I2C_IFRAM_ADDR, - i2c_pages.as_ptr() as usize, - i2c_pages.len(), - ) - }; - let mut i2c = unsafe { - bao1x_hal::udma::I2cDriver::new_with_ifram( - i2c_channel, - 400_000, - bao1x_api::PERCLK, - i2c_ifram, - &udma_global, - ) - }; - - // os timer initializations - let ostimer_csr = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::timer0::HW_TIMER0_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map Ticktimer CSR range"); - xous::claim_interrupt( - utralib::utra::timer0::TIMER0_IRQ, - timer_tick, - ostimer_csr.as_mut_ptr() as *mut usize, - ) - .expect("couldn't claim IRQ"); - - // setup the OS timer - let mut timer_run = false; - let mut os_timer = CSR::new(ostimer_csr.as_ptr() as *mut u32); - let ms = bao1x_api::SYSTEM_TICK_INTERVAL_MS; - os_timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer - // load its values - os_timer.wfo(utra::timer0::LOAD_LOAD, (bao1x_hal::board::DEFAULT_FCLK_FREQUENCY / 1_000) * ms); - os_timer.wfo(utra::timer0::RELOAD_RELOAD, (bao1x_hal::board::DEFAULT_FCLK_FREQUENCY / 1_000) * ms); - // enable the timer - os_timer.wfo(utra::timer0::EN_EN, 0b1); - - // Set EV_ENABLE according to timer_run - // timer is OFF at boot to help insure that processes 2 & 3 get larger time slices - // to operate & claim critical resources - os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, if timer_run { 1 } else { 0 }); - - // ---- "own" the Iox IRQ bank. This might need revision once bao1x aliasing is available. --- - let irq_page = xous::syscall::map_memory( - xous::MemoryAddress::new(utralib::utra::irqarray10::HW_IRQARRAY10_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't claim IRQ control page"); - let irq_csr = CSR::new(irq_page.as_mut_ptr() as *mut u32); - let mut irq = IrqHandler { irq_csr, cid: self_cid }; - // ensure that iox interrupts are gated off by default - for index in 0..8 { - unsafe { - iox.csr.base().add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()).add(index).write_volatile(0); + std::thread::spawn(move || { + let mut ifram_allocs = [Vec::new(), Vec::new()]; + // code is written assuming the IFRAM blocks have the same size. Since this is fixed in + // hardware, it's a good assumption; but the assert is put here in case we port this to + // a new system where for some reason they have different sizes. + assert!(utralib::generated::HW_IFRAM0_MEM_LEN == utralib::generated::HW_IFRAM1_MEM_LEN); + let pages = utralib::generated::HW_IFRAM0_MEM_LEN / 4096; + for _ in 0..pages { + ifram_allocs[0].push(None); + ifram_allocs[1].push(None); } - } - iox.csr.wo(utra::iox::SFR_INTFR, 0); - xous::claim_interrupt( - utralib::utra::irqarray10::IRQARRAY10_IRQ, - iox_irq_handler, - &mut irq as *mut IrqHandler as *mut usize, - ) - .expect("couldn't claim Iox interrupt"); - irq.irq_csr.wo(utralib::utra::irqarray10::EV_PENDING, 0xFFFF_FFFF); - irq.irq_csr.wfo(utralib::utra::irqarray10::EV_ENABLE_IOXIRQ, 1); - // Up to 8 slots where we can populate interrupt mappings in the hardware - // The index of the array corresponds to the slot. - let mut irq_table: [Option; 8] = [None; 8]; + // mark loader-hardwired pages for IFRAM0 + for i in + bao1x_hal::board::IFRAM0_RESERVED_PAGE_RANGE[0]..=bao1x_hal::board::IFRAM0_RESERVED_PAGE_RANGE[1] + { + ifram_allocs[0][i] = Some(Sender::from_usize(usize::MAX)); + } + // mark loader-hardwired pages for IFRAM1 + for i in + bao1x_hal::board::IFRAM1_RESERVED_PAGE_RANGE[0]..=bao1x_hal::board::IFRAM1_RESERVED_PAGE_RANGE[1] + { + ifram_allocs[1][i] = Some(Sender::from_usize(usize::MAX)); + } + // Second page from top of IFRAM0 is occupied by the swap handler. This was allocated + // by the loader, before the kernel even started. Mark the PID properly. + #[cfg(feature = "swap")] + { + ifram_allocs[0][30] = Some(Sender::from_usize(SWAPPER_PID as usize)); + } + + let iox_page = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::generated::HW_IOX_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't claim the IOX hardware page"); + let iox = Iox::new(iox_page.as_ptr() as *mut u32); + + let udma_global = GlobalConfig::new(); + + // Note: the I2C handler can be put into a separate thread if we need the main + // HAL server to not block while a large I2C transaction is being handled. For + // now this is all placed into a single thread. However, if we ever had a situation + // where, for example, you had to do a compound I2C transaction and flip a GPIO pin + // in the middle of that transaction in order for the set of I2C transactions to + // complete, this implementation would deadlock as it would block on the I2C transaction + // before handling the GPIO request. + let i2c_channel = bao1x_hal::board::setup_i2c_pins(&iox); + udma_global.clock_on(PeriphId::from(i2c_channel)); + let i2c_pages = xous::syscall::map_memory( + xous::MemoryAddress::new(bao1x_hal::board::I2C_IFRAM_ADDR), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't claim I2C IFRAM page"); + + let i2c_ifram = unsafe { + bao1x_hal::ifram::IframRange::from_raw_parts( + bao1x_hal::board::I2C_IFRAM_ADDR, + i2c_pages.as_ptr() as usize, + i2c_pages.len(), + ) + }; + let mut i2c = unsafe { + bao1x_hal::udma::I2cDriver::new_with_ifram( + i2c_channel, + 400_000, + bao1x_api::PERCLK, + i2c_ifram, + &udma_global, + ) + }; + + // os timer initializations + let ostimer_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::timer0::HW_TIMER0_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map Ticktimer CSR range"); + xous::claim_interrupt( + utralib::utra::timer0::TIMER0_IRQ, + timer_tick, + ostimer_csr.as_mut_ptr() as *mut usize, + ) + .expect("couldn't claim IRQ"); + + // setup the OS timer + let mut timer_run = false; + let mut os_timer = CSR::new(ostimer_csr.as_ptr() as *mut u32); + let ms = bao1x_api::SYSTEM_TICK_INTERVAL_MS; + os_timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer + // load its values + os_timer.wfo(utra::timer0::LOAD_LOAD, (bao1x_hal::board::DEFAULT_FCLK_FREQUENCY / 1_000) * ms); + os_timer.wfo(utra::timer0::RELOAD_RELOAD, (bao1x_hal::board::DEFAULT_FCLK_FREQUENCY / 1_000) * ms); + // enable the timer + os_timer.wfo(utra::timer0::EN_EN, 0b1); + + // Set EV_ENABLE according to timer_run + // timer is OFF at boot to help insure that processes 2 & 3 get larger time slices + // to operate & claim critical resources + os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, if timer_run { 1 } else { 0 }); + + // ---- "own" the Iox IRQ bank. This might need revision once bao1x aliasing is available. --- + let irq_page = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::utra::irqarray10::HW_IRQARRAY10_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't claim IRQ control page"); + let irq_csr = CSR::new(irq_page.as_mut_ptr() as *mut u32); + let mut irq = IrqHandler { irq_csr, cid: self_cid }; + // ensure that iox interrupts are gated off by default + for index in 0..8 { + unsafe { + iox.csr + .base() + .add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()) + .add(index) + .write_volatile(0); + } + } + iox.csr.wo(utra::iox::SFR_INTFR, 0); + xous::claim_interrupt( + utralib::utra::irqarray10::IRQARRAY10_IRQ, + iox_irq_handler, + &mut irq as *mut IrqHandler as *mut usize, + ) + .expect("couldn't claim Iox interrupt"); + irq.irq_csr.wo(utralib::utra::irqarray10::EV_PENDING, 0xFFFF_FFFF); + irq.irq_csr.wfo(utralib::utra::irqarray10::EV_ENABLE_IOXIRQ, 1); + // Up to 8 slots where we can populate interrupt mappings in the hardware + // The index of the array corresponds to the slot. + let mut irq_table: [Option; 8] = [None; 8]; + + let mut msg_opt = None; + log::debug!("Starting main loop"); + loop { + xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); + // this loop uses a unique form of reply_and_receive next that allows us to store and delegate + // scalar messages if necessary (that feature is currently unused). To do this, .take() the + // msg_opt and store it in a Vec; then use an explicit rsyscall to do a + // return_scalar1[2,5] to unblock all the blocking threads. The current loop doesn't + // actually do this - the feature was deemed unneeded in the end - but the structural + // refactor is left here because I can see it being useful in the future as this + // server does more and more hardware air traffic control. + let opcode = { + let msg = msg_opt.as_mut().unwrap(); + num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(HalOpcode::InvalidCall) + }; + log::debug!("{:?}", opcode); + match opcode { + HalOpcode::MapIfram => { + let sender = msg_opt.as_ref().unwrap().sender; + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let requested_size = scalar.arg1; // requested size + let requested_bank = scalar.arg2; // Specifies bank 0, 1, or don't care (any number but 0 or 1) + + let mut allocated_address = None; + for (bank, table) in ifram_allocs.iter_mut().enumerate() { + if bank == requested_bank || requested_bank > 1 { + match try_alloc(table, requested_size, sender) { + Some(offset) => { + let base = if bank == 0 { + utralib::generated::HW_IFRAM0_MEM + } else { + utralib::generated::HW_IFRAM1_MEM + }; + allocated_address = Some(base + offset); + break; + } + None => {} + } + } + } + // responds with size in arg1 (0 means could not be allocated/OOM) + // and address of allocation in arg2 + if let Some(addr) = allocated_address { + log::debug!( + "Allocated IFRAM at 0x{:x} to hold at least 0x{:x} bytes", + addr, + requested_size + ); + log::debug!("Alloc[0]: {:x?}", ifram_allocs[0]); + log::debug!("Alloc[1]: {:x?}", ifram_allocs[1]); + scalar.arg1 = requested_size; + scalar.arg2 = addr; + } else { + log::debug!( + "Could not allocate IFRAM request of 0x{:x} bytes in bank {}", + requested_size, + requested_bank + ); + scalar.arg1 = 0; + scalar.arg2 = 0; + } + } + } + HalOpcode::UnmapIfram => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + let mapped_size = scalar.arg1; + let phys_addr = scalar.arg2; + + let bank: usize; + let offset = if utralib::generated::HW_IFRAM0_MEM <= phys_addr + && phys_addr + < utralib::generated::HW_IFRAM0_MEM + utralib::generated::HW_IFRAM0_MEM_LEN + { + bank = 0; + phys_addr - utralib::generated::HW_IFRAM0_MEM + } else if utralib::generated::HW_IFRAM1_MEM <= phys_addr + && phys_addr + < utralib::generated::HW_IFRAM1_MEM + utralib::generated::HW_IFRAM1_MEM_LEN + { + bank = 1; + phys_addr - utralib::generated::HW_IFRAM1_MEM + } else { + log::error!("Mapped IFRAM address 0x{:x} is invalid", phys_addr); + panic!("Mapped IFRAM address is invalid"); + }; + let mut mapped_pages = mapped_size / 4096; + if mapped_size % 4096 != 0 { + mapped_pages += 1; + } + for record in ifram_allocs[bank][offset..offset + mapped_pages].iter_mut() { + *record = None; + } + } + } + HalOpcode::ConfigureIox => { + let buf = unsafe { + xous_ipc::Buffer::from_memory_message( + msg_opt.as_mut().unwrap().body.memory_message().unwrap(), + ) + }; + let config = buf.to_original::().unwrap(); + if let Some(f) = config.function { + iox.set_alternate_function(config.port, config.pin, f); + } + if let Some(d) = config.direction { + iox.set_gpio_dir(config.port, config.pin, d); + } + if let Some(t) = config.schmitt_trigger { + iox.set_gpio_schmitt_trigger(config.port, config.pin, t); + } + if let Some(p) = config.pullup { + iox.set_gpio_pullup(config.port, config.pin, p); + } + if let Some(s) = config.slow_slew { + iox.set_slow_slew_rate(config.port, config.pin, s); + } + if let Some(s) = config.strength { + iox.set_drive_strength(config.port, config.pin, s); + } + } + HalOpcode::ConfigureIoxIrq => { + let buf = unsafe { + xous_ipc::Buffer::from_memory_message( + msg_opt.as_mut().unwrap().body.memory_message().unwrap(), + ) + }; + let registration = buf.to_original::().unwrap(); + log::info!("Got registration request: {:?}", registration); + if let Some(index) = find_first_none(&irq_table) { + // create the reverse-lookup registration + let local_conn = xns + .request_connection(®istration.server) + .expect("couldn't connect to IRQ registree"); + let local_reg = IrqLocalRegistration { + cid: local_conn, + opcode: registration.opcode, + port: registration.port, + pin: registration.pin, + active: registration.active, + }; + irq_table[index] = Some(local_reg); + + // now activate the hardware register + let select = irq_select_from_port(registration.port, registration.pin); + let mut int_cr = IntCr(0); + int_cr.set_select(select); + match registration.active { + IoxValue::Low => int_cr.set_mode(IntMode::FallingEdge as u32), + IoxValue::High => int_cr.set_mode(IntMode::RisingEdge as u32), + } + int_cr.set_enable(true); + // safety: the index and offset are mapped to the intended range because the index is + // bounded by the size of irq_table, and the offset comes from the generated header + // file. + log::debug!( + "writing {:x} to {:x} at index {}", + int_cr.0, + unsafe { + iox.csr.base().add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()).add(index) + as usize + }, + index + ); + unsafe { + iox.csr + .base() + .add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()) + .add(index) + .write_volatile(int_cr.0); + } + } else { + panic!("Ran out of Iox interrupt slots: maximum 8 available"); + } + } + HalOpcode::IrqLocalHandler => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + // Figure out which port(s) caused the IRQ + let irq_flag = iox.csr.r(utralib::utra::iox::SFR_INTFR); + // clear the set bit by writing it back + iox.csr.wo(utralib::utra::iox::SFR_INTFR, irq_flag); + let mut found = false; + for bitpos in 0..8 { + // the bit position is flipped versus register order in memory + if ((irq_flag << (bitpos as u32)) & 0x80) != 0 { + if let Some(local_reg) = irq_table[bitpos] { + found = true; + // interrupts are "Best effort" and can gracefully fail if the receiver + // has been overwhelmed by too many + // interrupts + xous::try_send_message( + local_reg.cid, + xous::Message::new_scalar(local_reg.opcode, 0, 0, 0, 0), + ) + .ok(); + } else { + log::warn!( + "Got IRQ on position {} but no registration was found, ignoring!", + bitpos + ); + } + } + } + if !found { + if irq_flag != 0 { + log::warn!( + "No handler was found for raw flag: {:x} (note bit order is reversed)", + irq_flag + ); + } else { + log::info!( + "Interrupt received but maybe not for us? pending {:x}", + scalar.arg1 + ); + log::info!("CR0 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT0)); + log::info!("CR1 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT1)); + log::info!("CR2 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT2)); + log::info!("CR3 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT3)); + log::info!("CR4 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT4)); + log::info!("CR5 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT5)); + log::info!("CR6 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT6)); + log::info!("CR7 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT7)); + } + } + } + } + HalOpcode::SetGpioBank => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let value = scalar.arg2 as u16; + let bitmask = scalar.arg3 as u16; + iox.set_gpio_bank(port, value, bitmask); + } + } + HalOpcode::GetGpioBank => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + scalar.arg1 = iox.get_gpio_bank(port) as usize; + } + } + HalOpcode::ConfigureBio => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + if scalar.arg3 == 0 { + let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let pin = scalar.arg2 as u8; + match iox.set_bio_bit_from_port_and_pin(port, pin) { + Some(bit) => { + scalar.arg1 = bit as usize; + scalar.arg2 = 1 + } + _ => scalar.arg2 = 0, + } + } else { + let io_mode: bio::IoConfigMode = scalar.arg4.into(); + iox.set_ports_from_bio_bitmask(scalar.arg1 as u32, io_mode); + log::debug!("bio_bitmask: {:x}", iox.get_bio_mapping_bitmask()); + // no return values need to be set + } + } + } + HalOpcode::ConfigureUdmaClock => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let enable = if scalar.arg2 != 0 { true } else { false }; + if enable { + udma_global.clock_on(periph); + } else { + udma_global.clock_off(periph); + } + } + } + HalOpcode::ConfigureUdmaEvent => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + let event_offset = scalar.arg2 as u32; + let to_channel: EventChannel = + num_traits::FromPrimitive::from_usize(scalar.arg3).unwrap(); + // note: no "air traffic control" is done to prevent mapping other + // events. Maybe this should be done? but for now, let's leave it + // as bare iron. + udma_global.map_event_with_offset(periph, event_offset, to_channel); + } + } + HalOpcode::UdmaIrqStatusBits => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let bank = num_traits::FromPrimitive::from_usize(scalar.arg1) + .expect("Bad argument to UdmaIrqStatusBits"); + scalar.arg1 = udma_global.irq_status_bits(bank) as usize; + } + } + HalOpcode::PeriphReset => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { + let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); + udma_global.reset(periph); + } + } + HalOpcode::I2c => { + let mut buf = unsafe { + xous_ipc::Buffer::from_memory_message_mut( + msg_opt.as_mut().unwrap().body.memory_message_mut().unwrap(), + ) + }; + let mut list = buf.to_original::().expect("I2c message format error"); + for transaction in list.transactions.iter_mut() { + match transaction.i2c_type { + I2cTransactionType::Write => { + match i2c.i2c_write( + transaction.device, + transaction.address, + &transaction.data, + ) { + Ok(result) => transaction.result = result, + _ => transaction.result = I2cResult::Nack, + } + } + I2cTransactionType::Read | I2cTransactionType::ReadRepeatedStart => { + match i2c.i2c_read( + transaction.device, + transaction.address, + &mut transaction.data, + transaction.i2c_type == I2cTransactionType::ReadRepeatedStart, + ) { + Ok(result) => transaction.result = result, + _ => transaction.result = I2cResult::Nack, + } + } + } + } + buf.replace(list).expect("I2c message format error"); + } + HalOpcode::SetPreemptionState => { + // this is done as a blocking message because we want confirmation that + // this bit has been set before entering a critical section. + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + timer_run = scalar.arg1 != 0; + os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, if timer_run { 1 } else { 0 }); + } + } + HalOpcode::InvalidCall => { + log::error!("Invalid opcode received: {:?}", msg_opt); + } + } + } + }); servers::keyboard::start_keyboard_service(); @@ -280,344 +631,9 @@ fn main() { servers::trng::start_trng_service(); servers::rtc::start_rtc_service(); - let mut msg_opt = None; - log::debug!("Starting main loop"); + let tt = ticktimer::Ticktimer::new().unwrap(); + // placeholder - we can put an actual service here but for now just sleep the main thread. loop { - xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); - // this loop uses a unique form of reply_and_receive next that allows us to store and delegate - // scalar messages if necessary (that feature is currently unused). To do this, .take() the msg_opt - // and store it in a Vec; then use an explicit rsyscall to do a return_scalar1[2,5] to unblock all - // the blocking threads. The current loop doesn't actually do this - the feature was deemed unneeded - // in the end - but the structural refactor is left here because I can see it being useful in the - // future as this server does more and more hardware air traffic control. - let opcode = { - let msg = msg_opt.as_mut().unwrap(); - num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(HalOpcode::InvalidCall) - }; - log::debug!("{:?}", opcode); - match opcode { - HalOpcode::MapIfram => { - let sender = msg_opt.as_ref().unwrap().sender; - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - let requested_size = scalar.arg1; // requested size - let requested_bank = scalar.arg2; // Specifies bank 0, 1, or don't care (any number but 0 or 1) - - let mut allocated_address = None; - for (bank, table) in ifram_allocs.iter_mut().enumerate() { - if bank == requested_bank || requested_bank > 1 { - match try_alloc(table, requested_size, sender) { - Some(offset) => { - let base = if bank == 0 { - utralib::generated::HW_IFRAM0_MEM - } else { - utralib::generated::HW_IFRAM1_MEM - }; - allocated_address = Some(base + offset); - break; - } - None => {} - } - } - } - // responds with size in arg1 (0 means could not be allocated/OOM) - // and address of allocation in arg2 - if let Some(addr) = allocated_address { - log::debug!( - "Allocated IFRAM at 0x{:x} to hold at least 0x{:x} bytes", - addr, - requested_size - ); - log::debug!("Alloc[0]: {:x?}", ifram_allocs[0]); - log::debug!("Alloc[1]: {:x?}", ifram_allocs[1]); - scalar.arg1 = requested_size; - scalar.arg2 = addr; - } else { - log::debug!( - "Could not allocate IFRAM request of 0x{:x} bytes in bank {}", - requested_size, - requested_bank - ); - scalar.arg1 = 0; - scalar.arg2 = 0; - } - } - } - HalOpcode::UnmapIfram => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - let mapped_size = scalar.arg1; - let phys_addr = scalar.arg2; - - let bank: usize; - let offset = if utralib::generated::HW_IFRAM0_MEM <= phys_addr - && phys_addr - < utralib::generated::HW_IFRAM0_MEM + utralib::generated::HW_IFRAM0_MEM_LEN - { - bank = 0; - phys_addr - utralib::generated::HW_IFRAM0_MEM - } else if utralib::generated::HW_IFRAM1_MEM <= phys_addr - && phys_addr - < utralib::generated::HW_IFRAM1_MEM + utralib::generated::HW_IFRAM1_MEM_LEN - { - bank = 1; - phys_addr - utralib::generated::HW_IFRAM1_MEM - } else { - log::error!("Mapped IFRAM address 0x{:x} is invalid", phys_addr); - panic!("Mapped IFRAM address is invalid"); - }; - let mut mapped_pages = mapped_size / 4096; - if mapped_size % 4096 != 0 { - mapped_pages += 1; - } - for record in ifram_allocs[bank][offset..offset + mapped_pages].iter_mut() { - *record = None; - } - } - } - HalOpcode::ConfigureIox => { - let buf = unsafe { - xous_ipc::Buffer::from_memory_message( - msg_opt.as_mut().unwrap().body.memory_message().unwrap(), - ) - }; - let config = buf.to_original::().unwrap(); - if let Some(f) = config.function { - iox.set_alternate_function(config.port, config.pin, f); - } - if let Some(d) = config.direction { - iox.set_gpio_dir(config.port, config.pin, d); - } - if let Some(t) = config.schmitt_trigger { - iox.set_gpio_schmitt_trigger(config.port, config.pin, t); - } - if let Some(p) = config.pullup { - iox.set_gpio_pullup(config.port, config.pin, p); - } - if let Some(s) = config.slow_slew { - iox.set_slow_slew_rate(config.port, config.pin, s); - } - if let Some(s) = config.strength { - iox.set_drive_strength(config.port, config.pin, s); - } - } - HalOpcode::ConfigureIoxIrq => { - let buf = unsafe { - xous_ipc::Buffer::from_memory_message( - msg_opt.as_mut().unwrap().body.memory_message().unwrap(), - ) - }; - let registration = buf.to_original::().unwrap(); - log::info!("Got registration request: {:?}", registration); - if let Some(index) = find_first_none(&irq_table) { - // create the reverse-lookup registration - let local_conn = xns - .request_connection(®istration.server) - .expect("couldn't connect to IRQ registree"); - let local_reg = IrqLocalRegistration { - cid: local_conn, - opcode: registration.opcode, - port: registration.port, - pin: registration.pin, - active: registration.active, - }; - irq_table[index] = Some(local_reg); - - // now activate the hardware register - let select = irq_select_from_port(registration.port, registration.pin); - let mut int_cr = IntCr(0); - int_cr.set_select(select); - match registration.active { - IoxValue::Low => int_cr.set_mode(IntMode::FallingEdge as u32), - IoxValue::High => int_cr.set_mode(IntMode::RisingEdge as u32), - } - int_cr.set_enable(true); - // safety: the index and offset are mapped to the intended range because the index is - // bounded by the size of irq_table, and the offset comes from the generated header file. - log::debug!( - "writing {:x} to {:x} at index {}", - int_cr.0, - unsafe { - iox.csr.base().add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()).add(index) - as usize - }, - index - ); - unsafe { - iox.csr - .base() - .add(utralib::utra::iox::SFR_INTCR_CRINT0.offset()) - .add(index) - .write_volatile(int_cr.0); - } - } else { - panic!("Ran out of Iox interrupt slots: maximum 8 available"); - } - } - HalOpcode::IrqLocalHandler => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - // Figure out which port(s) caused the IRQ - let irq_flag = iox.csr.r(utralib::utra::iox::SFR_INTFR); - // clear the set bit by writing it back - iox.csr.wo(utralib::utra::iox::SFR_INTFR, irq_flag); - let mut found = false; - for bitpos in 0..8 { - // the bit position is flipped versus register order in memory - if ((irq_flag << (bitpos as u32)) & 0x80) != 0 { - if let Some(local_reg) = irq_table[bitpos] { - found = true; - // interrupts are "Best effort" and can gracefully fail if the receiver has - // been overwhelmed by too many interrupts - xous::try_send_message( - local_reg.cid, - xous::Message::new_scalar(local_reg.opcode, 0, 0, 0, 0), - ) - .ok(); - } else { - log::warn!( - "Got IRQ on position {} but no registration was found, ignoring!", - bitpos - ); - } - } - } - if !found { - if irq_flag != 0 { - log::warn!( - "No handler was found for raw flag: {:x} (note bit order is reversed)", - irq_flag - ); - } else { - log::info!("Interrupt received but maybe not for us? pending {:x}", scalar.arg1); - log::info!("CR0 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT0)); - log::info!("CR1 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT1)); - log::info!("CR2 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT2)); - log::info!("CR3 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT3)); - log::info!("CR4 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT4)); - log::info!("CR5 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT5)); - log::info!("CR6 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT6)); - log::info!("CR7 {:x}", iox.csr.r(utralib::utra::iox::SFR_INTCR_CRINT7)); - } - } - } - } - HalOpcode::SetGpioBank => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - let value = scalar.arg2 as u16; - let bitmask = scalar.arg3 as u16; - iox.set_gpio_bank(port, value, bitmask); - } - } - HalOpcode::GetGpioBank => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - scalar.arg1 = iox.get_gpio_bank(port) as usize; - } - } - HalOpcode::ConfigureBio => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - if scalar.arg3 == 0 { - let port: IoxPort = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - let pin = scalar.arg2 as u8; - match iox.set_bio_bit_from_port_and_pin(port, pin) { - Some(bit) => { - scalar.arg1 = bit as usize; - scalar.arg2 = 1 - } - _ => scalar.arg2 = 0, - } - } else { - let io_mode: bio::IoConfigMode = scalar.arg4.into(); - iox.set_ports_from_bio_bitmask(scalar.arg1 as u32, io_mode); - log::debug!("bio_bitmask: {:x}", iox.get_bio_mapping_bitmask()); - // no return values need to be set - } - } - } - HalOpcode::ConfigureUdmaClock => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - let enable = if scalar.arg2 != 0 { true } else { false }; - if enable { - udma_global.clock_on(periph); - } else { - udma_global.clock_off(periph); - } - } - } - HalOpcode::ConfigureUdmaEvent => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - let event_offset = scalar.arg2 as u32; - let to_channel: EventChannel = - num_traits::FromPrimitive::from_usize(scalar.arg3).unwrap(); - // note: no "air traffic control" is done to prevent mapping other - // events. Maybe this should be done? but for now, let's leave it - // as bare iron. - udma_global.map_event_with_offset(periph, event_offset, to_channel); - } - } - HalOpcode::UdmaIrqStatusBits => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - let bank = num_traits::FromPrimitive::from_usize(scalar.arg1) - .expect("Bad argument to UdmaIrqStatusBits"); - scalar.arg1 = udma_global.irq_status_bits(bank) as usize; - } - } - HalOpcode::PeriphReset => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message() { - let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap(); - udma_global.reset(periph); - } - } - HalOpcode::I2c => { - let mut buf = unsafe { - xous_ipc::Buffer::from_memory_message_mut( - msg_opt.as_mut().unwrap().body.memory_message_mut().unwrap(), - ) - }; - let mut list = buf.to_original::().expect("I2c message format error"); - for transaction in list.transactions.iter_mut() { - match transaction.i2c_type { - I2cTransactionType::Write => { - match i2c.i2c_write(transaction.device, transaction.address, &transaction.data) { - Ok(result) => transaction.result = result, - _ => transaction.result = I2cResult::Nack, - } - } - I2cTransactionType::Read | I2cTransactionType::ReadRepeatedStart => { - match i2c.i2c_read( - transaction.device, - transaction.address, - &mut transaction.data, - transaction.i2c_type == I2cTransactionType::ReadRepeatedStart, - ) { - Ok(result) => transaction.result = result, - _ => transaction.result = I2cResult::Nack, - } - } - } - } - buf.replace(list).expect("I2c message format error"); - } - HalOpcode::SetPreemptionState => { - // this is done as a blocking message because we want confirmation that - // this bit has been set before entering a critical section. - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - timer_run = scalar.arg1 != 0; - os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, if timer_run { 1 } else { 0 }); - } - } - HalOpcode::InvalidCall => { - log::error!("Invalid opcode received: {:?}", msg_opt); - } - HalOpcode::Quit => { - log::info!("Received quit opcode, exiting."); - break; - } - } + tt.sleep_ms(10_000).ok(); } - xns.unregister_server(sid).unwrap(); - xous::destroy_server(sid).unwrap(); - log::trace!("quitting"); - xous::terminate_process(0) }