replace watchdog hack with an atomicbool; reset cam on setup

This patch replaces the watchdog thread with a spin-wait on
an atomicbool that is set by the IRQ handler once an IRQ
triggers. This is a much cleaner, lower-overhead way to handle
the time out.

This patch also puts the camera setup into a retry loop, and
by default on setup it does a hardware reset of the display
subsystem (this includes the OLED screen). This hopefully
gives the subsystem a much better chance of recovering from
the camera being in a scrambled state. The working theory
for why the camera might get scrambled is that it assumes
it is a sole I2C device, and when its init sequence is
interrupted by other devices handling requests it does
not handle this situation gracefully.
This commit is contained in:
bunnie 2026-05-29 00:08:02 +08:00
parent 542bec7541
commit a421b7abfa
4 changed files with 261 additions and 91 deletions

View file

@ -63,7 +63,6 @@ hosted-baosec = ["bao1x-emu", "ux-api/hosted-baosec", "no-gam"]
autotest = []
# factory feature - makes it keep on scanning forever - used for calibrating factory test jigs
eternal-scan = []
cam-watchdog = []
with-logo = []
bao1x = ["utralib/bao1x", "blitstr2/bao1x"]

View file

@ -15,6 +15,7 @@ mod qr;
mod qr_warmup;
#[cfg(feature = "gfx-testing")]
mod testing;
mod waitscreen;
use std::{
collections::VecDeque,
sync::{
@ -173,6 +174,7 @@ pub fn blit_to_display(display: &mut Oled128x128, frame: &[u8], display_cleared:
struct CamIrq {
csr: utralib::CSR<u32>,
cid: u32,
got_irq: Arc<AtomicBool>,
}
#[cfg(not(feature = "hosted-baosec"))]
@ -182,6 +184,8 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) {
let pending = cam_irq.csr.r(utra::irqarray8::EV_PENDING);
cam_irq.csr.wo(utra::irqarray8::EV_PENDING, pending);
cam_irq.got_irq.store(true, Ordering::SeqCst);
// activate the handler
xous::try_send_message(
cam_irq.cid,
@ -385,6 +389,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let cid = xous::connect(sid).unwrap(); // self-connection always succeeds
// ---- register interrupt handler
let got_irq = Arc::new(AtomicBool::new(false));
#[cfg(not(feature = "hosted-baosec"))]
let cam_irq; // this binding has to out-live the temporaries below
#[cfg(not(feature = "hosted-baosec"))]
@ -399,7 +404,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let mut irq_csr = utralib::CSR::new(irq.as_mut_ptr() as *mut u32);
irq_csr.wo(utra::irqarray8::EV_PENDING, 0xFFFF); // clear any pending interrupts
cam_irq = CamIrq { csr: utralib::CSR::new(irq.as_mut_ptr() as *mut u32), cid };
cam_irq =
CamIrq { csr: utralib::CSR::new(irq.as_mut_ptr() as *mut u32), cid, got_irq: got_irq.clone() };
let irq_arg = &cam_irq as *const CamIrq as *mut usize;
log::info!("irq_arg: {:x}", irq_arg as usize);
xous::claim_interrupt(utra::irqarray8::IRQARRAY8_IRQ, handle_irq, irq_arg)
@ -432,8 +438,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let mut bw_thresh: u8 = 128;
let mut qr_request: Option<xous::MessageEnvelope> = None;
let mut kbd_listeners: Vec<(CID, usize)> = Vec::new();
#[cfg(feature = "cam-watchdog")]
let cam_started = Arc::new(AtomicBool::new(false));
let mut dry_run = false;
#[allow(unused_mut)]
let mut orientation = DisplayOrientation::Normal;
@ -473,89 +477,122 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
} else {
log::error!("test image failed to decode, this shouldn't happen!");
}
// fix orientation if it's upside down
if orientation == DisplayOrientation::UpsideDown {
// camera hardware can be a bit finnicky about starting reliably. give it
// a couple of tries before giving up. I think it might have to do with I2C
// contention - the working theory is that if another I2C driver inserts
// a transaction in the middle of the long camera init sequence, we can
// end up with the camera in a bad/unknown state. Unfortunately, the I2C
// "atomic" implemenation can't handle the size of the I2C poke list,
// so for now we're going to do a re-try hoping that on the retry the
// I2C bus is clear for us.
const RETRY_LIMIT: usize = 3;
let mut retries = 0;
while retries < RETRY_LIMIT {
// reset camera UDMA block
udma_global.reset(PeriphId::Cam);
// orientation is fixed through a reset of the whole display subsystem
// now issue a PRST_N - this will reset camera and OLED
iox.set_gpio_pin_value(IoxPort::PA, 6, IoxValue::High);
tt.sleep_ms(5).ok();
iox.set_gpio_pin_value(IoxPort::PA, 6, IoxValue::Low);
// have to re-init display after reset
tt.sleep_ms(100).ok();
display
.flip_vertical(false)
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display))
.init()
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display));
// display "wait" icon
display.blit_screen(&crate::waitscreen::BITMAP);
display
.redraw()
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display));
// this will defer response until later
qr_request = msg_opt.take();
// power up the camera
// starts MCLK
iox.setup_pin(
cam_clk.0,
cam_clk.1,
Some(IoxDir::Output),
Some(IoxFunction::AF3),
None,
None,
Some(IoxEnable::Disable),
Some(IoxDriveStrength::Drive8mA),
);
timer.wo(utra::pwm::REG_CH_EN, 1);
timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1);
tt.sleep_ms(10).ok(); // wait for camera to clock-up
// bring camera out of powerdown
iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low);
tt.sleep_ms(10).ok(); // wait for camera to power-up
let (pid, mid) = cam.read_id(&mut i2c);
log::info!("Camera pid {:x}, mid {:x}", pid, mid);
cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240);
tt.sleep_ms(15).ok();
let (cols, _rows) = cam.resolution();
let border = (cols - IMAGE_WIDTH) / 2;
cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT));
log::info!("320x240 resolution setup with 256x240 slicing");
hal.set_preemption(false);
const STARTUP_TIMEOUT_MS: u128 = 2500;
let start = std::time::Instant::now();
cam_irq.got_irq.store(false, Ordering::SeqCst);
// now start an acquisition
cam.capture_async();
let mut started = false;
while std::time::Instant::now().duration_since(start).as_millis()
< STARTUP_TIMEOUT_MS
{
// this effectively halts anything else from happening until a CamIrq is
// produced
if cam_irq.got_irq.load(Ordering::SeqCst) {
started = true;
break;
}
}
if started {
break;
}
// not started, reset camera and try again
hal.set_preemption(true);
retries += 1;
log::warn!("Retrying camera start-up sequence {}/{}", retries, RETRY_LIMIT);
// power down the camera
iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::High);
// stop MCLK
tt.sleep_ms(2).ok();
timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0);
timer.wo(utra::pwm::REG_CH_EN, 0);
iox.setup_pin(
cam_clk.0,
cam_clk.1,
Some(IoxDir::Input),
Some(IoxFunction::Gpio),
None,
None,
None,
None,
);
// restore the message into its original location
msg_opt = qr_request.take();
}
// reset camera UDMA block
udma_global.reset(PeriphId::Cam);
// display.stash(); // save a copy of the UI
// this will defer response until later
qr_request = msg_opt.take();
// power up the camera
// starts MCLK
iox.setup_pin(
cam_clk.0,
cam_clk.1,
Some(IoxDir::Output),
Some(IoxFunction::AF3),
None,
None,
Some(IoxEnable::Disable),
Some(IoxDriveStrength::Drive8mA),
);
timer.wo(utra::pwm::REG_CH_EN, 1);
timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1);
tt.sleep_ms(10).ok(); // wait for camera to clock-up
// bring camera out of powerdown
iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low);
tt.sleep_ms(10).ok(); // wait for camera to power-up
let (pid, mid) = cam.read_id(&mut i2c);
log::info!("Camera pid {:x}, mid {:x}", pid, mid);
cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240);
tt.sleep_ms(15).ok();
let (cols, _rows) = cam.resolution();
let border = (cols - IMAGE_WIDTH) / 2;
cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT));
log::info!("320x240 resolution setup with 256x240 slicing");
#[cfg(feature = "cam-watchdog")]
cam_started.store(false, Ordering::SeqCst);
#[cfg(not(feature = "cam-watchdog"))]
hal.set_preemption(false);
// start watchdog to make sure camera is running
#[cfg(feature = "cam-watchdog")]
let _ = std::thread::spawn({
let cid = cid.clone();
let cam_started = cam_started.clone();
move || {
let hal = Hal::new();
let start = std::time::Instant::now();
while !cam_started.load(Ordering::SeqCst)
&& std::time::Instant::now().duration_since(start).as_millis() < 3000
{
std::thread::sleep(std::time::Duration::from_millis(50));
}
if !cam_started.load(Ordering::SeqCst) {
// force an IRQ into the pipe to start the camera
xous::try_send_message(
cid,
xous::Message::new_scalar(
GfxOpcode::CamIrq.to_usize().unwrap(),
0,
0,
0,
0,
),
)
.ok();
}
// turning off preemption makes camera acquisition smoother; the OS will
// naturally try to schedule other tasks
// between camera frames after each CamIrq interrupt
hal.set_preemption(false);
}
});
// now start an acquisition
cam.capture_async();
if retries == RETRY_LIMIT {
log::error!("Couldn't start camera, rebooting whole system");
let xns = xous_names::XousNames::new().unwrap();
let susres = susres::Susres::new_without_hook(&xns).unwrap();
susres.reboot(true).ok();
}
}
// if qr_request is already pending, ignore any new acquisition requests
}
@ -580,9 +617,12 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
display_timeout_handler(&udma_global, &mut display)
})
}
// remove "frozen" frame
display.clear();
display
.pop()
.redraw()
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display));
hal.set_preemption(true);
}
}
@ -605,8 +645,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
}
}
GfxOpcode::CamIrq => {
#[cfg(feature = "cam-watchdog")]
cam_started.store(true, Ordering::SeqCst);
// copy the camera data to our FB
let fb: &[u32] = cam.rx_buf();
// fb is an array of IMAGE_WIDTH x IMAGE_HEIGHT x u16
@ -701,11 +739,12 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
// the Message `Drop`s. It will also cause the sampling of the camera to
// stop on the next frame.
if let Some(mut envelope) = qr_request.take() {
/*
display.pop().unwrap_or_else(|_| {
// remove "frozen" frame
display.clear();
display.redraw().unwrap_or_else(|_| {
display_timeout_handler(&udma_global, &mut display)
})
*/
});
let metadata = format!("{:?}", meta);
#[cfg(not(feature = "hosted-baosec"))]
if content.starts_with("test://") {

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -0,0 +1,132 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
pub const BITMAP: [u32; 512] = [
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0x03ffffff, 0xffffff80, 0xffffffff,
0xffffffff, 0xefffffff, 0xffffffef, 0xffffffff,
0xffffffff, 0xefffffff, 0xffffffef, 0xffffffff,
0xffffffff, 0x6fffffff, 0xffffffed, 0xffffffff,
0xffffffff, 0x9fffffff, 0xfffffff2, 0xffffffff,
0xffffffff, 0x3fffffff, 0xfffffff9, 0xffffffff,
0xffffffff, 0x7fffffff, 0xfffffffd, 0xffffffff,
0xffffffff, 0x3fffffff, 0xfffffff9, 0xffffffff,
0xffffffff, 0xdfffffff, 0xfffffff7, 0xffffffff,
0xffffffff, 0xefffffff, 0xffffffee, 0xffffffff,
0xffffffff, 0x4fffffff, 0xffffffe5, 0xffffffff,
0xffffffff, 0xafffffff, 0xffffffea, 0xffffffff,
0xffffffff, 0x03ffffff, 0xffffff80, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
];