From 663139b60787fa1b30548344cfda907b4a613270 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 24 Feb 2026 01:33:25 +0800 Subject: [PATCH] split ADC into its own service thread this allows the ADC to depend on core HAL services. It's structured so that other 'misc' services would go into the "other" module, just to keep the overhead of threads/services down a bit. Up until this point all the other services mapped to existing/other server names, like the RTC, susres, etc. --- services/bao1x-hal-service/src/lib.rs | 26 ++++- services/bao1x-hal-service/src/main.rs | 24 +---- services/bao1x-hal-service/src/servers.rs | 1 + .../bao1x-hal-service/src/servers/others.rs | 95 +++++++++++++++++++ 4 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 services/bao1x-hal-service/src/servers/others.rs diff --git a/services/bao1x-hal-service/src/lib.rs b/services/bao1x-hal-service/src/lib.rs index e0c500fe5..b2e295531 100644 --- a/services/bao1x-hal-service/src/lib.rs +++ b/services/bao1x-hal-service/src/lib.rs @@ -2,7 +2,7 @@ pub mod api; pub mod trng; use bao1x_api::*; -use bao1x_hal::udma::AdcSource; +use bao1x_hal::udma::{AdcExtChannel, AdcSource}; use num_traits::*; use xous::{Message, send_message}; use xous_api_susres::api::Opcode as SusresOp; @@ -136,19 +136,22 @@ impl Adc { pub fn new() -> Self { let xns = xous_names::XousNames::new().unwrap(); let conn = - xns.request_connection(SERVER_NAME_BAO1X_HAL).expect("Couldn't connect to bao1x HAL server"); + xns.request_connection(SERVER_NAME_BAO1X_OTHERS).expect("Couldn't connect to bao1x HAL server"); Adc { conn } } /// Averaging can range from 1..1024. Out of range values are silently converted to in-range values /// based on saturating logic. None for averaging translates to 1. + /// + /// For analog channels, the measurement goes from 0..1.208v. There is some nonlinearity observed below + /// 0.2V, so the "sweet spot" is 0.2V-1.2V. pub fn read_raw(&self, channel: AdcSource, averaging: Option) -> u16 { let averaging = averaging.unwrap_or(1).min(1).max((bao1x_hal::udma::ADC_RX_BUF_SIZE / size_of::()) as u16); match xous::send_message( self.conn, xous::Message::new_blocking_scalar( - HalOpcode::ReadAdcChannel.to_usize().unwrap(), + PeripheralOpcode::ReadAdcChannel.to_usize().unwrap(), channel.to_usize(), averaging as usize, 0, @@ -159,6 +162,23 @@ impl Adc { _ => panic!("Unhandled response on Adc::read_raw"), } } + + /// Safety: this method does no checks for concurrent use of the analog channels. + /// It is up to the caller to ensure that ADC0-3 (mapped to PA4-7) have no conflicts + /// with other I/O functions. + pub unsafe fn enable_channel(&self, channel: AdcExtChannel) { + xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + PeripheralOpcode::EnableChannel.to_usize().unwrap(), + channel.into(), + 0, + 0, + 0, + ), + ) + .ok(); + } } pub struct ClockManager { diff --git a/services/bao1x-hal-service/src/main.rs b/services/bao1x-hal-service/src/main.rs index 4d09ed973..2b8eefd47 100644 --- a/services/bao1x-hal-service/src/main.rs +++ b/services/bao1x-hal-service/src/main.rs @@ -4,10 +4,7 @@ mod servers; use std::pin::Pin; use bao1x_api::*; -use bao1x_hal::{ - iox::Iox, - udma::{AdcSource, GlobalConfig}, -}; +use bao1x_hal::{iox::Iox, udma::GlobalConfig}; use bitfield::*; use num_traits::*; use udma::UdmaGlobalConfig; @@ -281,11 +278,6 @@ fn main() { // The index of the array corresponds to the slot. let mut irq_table: [Option; 8] = [None; 8]; - // allocate the ADC - udma_global.clock_on(PeriphId::Adc); - // safety: clocks are turned on, and we can assume PERCLK is configured at boot - let mut adc = unsafe { bao1x_hal::udma::Adc::new(bao1x_api::PERCLK) }; - let mut msg_opt = None; log::debug!("Starting main loop"); loop { @@ -627,17 +619,6 @@ fn main() { if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { let new_perclk = scalar.arg1 as u32; i2c.update_perclk(new_perclk); - adc.update_perclk(new_perclk); - } - } - HalOpcode::ReadAdcChannel => { - if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { - let channel = AdcSource::from_usize(scalar.arg1); - // sets to at least 1, even if the arg is 0 - // max averaging is limited by the size of the ADC buffer - let averaging = - scalar.arg2.max(1).min(bao1x_hal::udma::ADC_RX_BUF_SIZE / size_of::()); - scalar.arg1 = adc.read_raw_averaged(channel, averaging) as usize; } } HalOpcode::InvalidCall => { @@ -658,6 +639,9 @@ fn main() { servers::trng::start_trng_service(); servers::rtc::start_rtc_service(); + // start the remaining services + servers::others::start_peri_service(clk_mgr.get_per()); + let tt = ticktimer::Ticktimer::new().unwrap(); // placeholder - we can put an actual service here but for now just sleep the main thread. loop { diff --git a/services/bao1x-hal-service/src/servers.rs b/services/bao1x-hal-service/src/servers.rs index 8f799171f..17e40472e 100644 --- a/services/bao1x-hal-service/src/servers.rs +++ b/services/bao1x-hal-service/src/servers.rs @@ -4,6 +4,7 @@ pub mod keyboard; #[cfg(all(feature = "board-baosec", not(feature = "oem-baosec-lite")))] mod baosec_hw; pub mod bio; +pub mod others; pub mod rtc; pub mod susres; pub mod trng; diff --git a/services/bao1x-hal-service/src/servers/others.rs b/services/bao1x-hal-service/src/servers/others.rs new file mode 100644 index 000000000..6434375ed --- /dev/null +++ b/services/bao1x-hal-service/src/servers/others.rs @@ -0,0 +1,95 @@ +//! Server for all the other hardware pieces that aren't covered by +//! existing well-known/shared service providers, and which also can't +//! be condensed into the main loop due to dependencies on core HAL +//! services (such as IFRAM resolution, I/O setup, etc.) + +use bao1x_api::{IoxDir, IoxEnable, IoxFunction, PeripheralOpcode, SERVER_NAME_BAO1X_OTHERS, iox::IoSetup}; +use bao1x_hal::udma::{Adc, AdcExtChannel, AdcSource}; +use bao1x_hal_service::UdmaGlobal; + +pub fn start_peri_service(clk_freq: u32) { + let _ = std::thread::spawn({ + move || { + peri_service(clk_freq); + } + }); +} + +fn peri_service(clk_freq: u32) -> ! { + let xns = xous_names::XousNames::new().unwrap(); + // claim the server name + let sid = xns.register_name(SERVER_NAME_BAO1X_OTHERS, None).unwrap(); + + let iox = crate::iox::IoxHal::new(); + let udma_global = UdmaGlobal::new(); + udma_global.udma_clock_config(bao1x_api::PeriphId::Adc, true); + // safety: clocks are turned on, and perclk already configured as so + let mut adc = unsafe { Adc::new(clk_freq) }; + + let mut msg_opt = None; + + loop { + xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); + let opcode = { + let msg = msg_opt.as_mut().unwrap(); + num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(PeripheralOpcode::InvalidCall) + }; + log::debug!("{:?}", opcode); + match opcode { + PeripheralOpcode::ReadAdcChannel => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let channel = AdcSource::from_usize(scalar.arg1); + // sets to at least 1, even if the arg is 0 + // max averaging is limited by the size of the ADC buffer + let averaging = + scalar.arg2.max(1).min(bao1x_hal::udma::ADC_RX_BUF_SIZE / size_of::()); + scalar.arg1 = adc.read_raw_averaged(channel, averaging) as usize; + } + } + PeripheralOpcode::EnableChannel => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let channel: AdcExtChannel = scalar.arg1.into(); + let pin = match channel { + AdcExtChannel::Adc0 => 4, + AdcExtChannel::Adc1 => 5, + AdcExtChannel::Adc2 => 6, + AdcExtChannel::Adc3 => 7, + }; + iox.setup_pin( + bao1x_api::IoxPort::PA, + pin, + Some(IoxDir::Input), + Some(IoxFunction::Gpio), + Some(IoxEnable::Disable), + Some(IoxEnable::Disable), + None, + None, + ); + // dabao boards have a dependency to configure this pin as an input as well. + #[cfg(feature = "board-dabao")] + if channel == AdcExtChannel::Adc0 { + iox.setup_pin( + bao1x_api::IoxPort::PC, + 9, + Some(IoxDir::Input), + Some(IoxFunction::Gpio), + Some(IoxEnable::Disable), + Some(IoxEnable::Disable), + None, + None, + ); + } + } + } + PeripheralOpcode::UpdatePerclk => { + if let Some(scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { + let new_perclk = scalar.arg1 as u32; + adc.update_perclk(new_perclk); + } + } + PeripheralOpcode::InvalidCall => { + log::error!("Invalid opcode received: {:?}", msg_opt); + } + } + } +}