cleanup the camera/QR testing loop in bootloader

This loop was useful in bringing up the GC2145 camera, so
we should keep this code around. had to be refactored/shuffled
around a bit to compensate for e.g. bitrot.
This commit is contained in:
bunnie 2025-05-01 16:43:53 +08:00
parent 2699146d62
commit 3f2c4e396a
3 changed files with 358 additions and 326 deletions

View file

@ -16,9 +16,9 @@ mod verifier;
#[cfg(any(feature = "board-baosec", feature = "board-baosor"))] #[cfg(any(feature = "board-baosec", feature = "board-baosor"))]
mod gfx; mod gfx;
#[cfg(feature = "qr")] #[cfg(any(feature = "qr", feature = "cam-test"))]
mod homography; mod homography;
#[cfg(feature = "qr")] #[cfg(any(feature = "qr", feature = "cam-test"))]
mod qr; mod qr;
#[cfg(feature = "usb")] #[cfg(feature = "usb")]
mod usb; mod usb;

View file

@ -14,6 +14,10 @@ use utralib::generated::*;
#[cfg(feature = "qr")] #[cfg(feature = "qr")]
use crate::platform::cramium::{homography, qr}; use crate::platform::cramium::{homography, qr};
#[global_allocator]
#[cfg(not(feature = "usb"))]
static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();
// Notes about the reset vector location // Notes about the reset vector location
// This can be set using fuses in the IFR (also called 'info') region // This can be set using fuses in the IFR (also called 'info') region
// The offset is an 8-bit value, which is shifted into a final location // The offset is an 8-bit value, which is shifted into a final location
@ -336,8 +340,8 @@ pub fn early_init() -> u32 {
// disable CCM, force PWM on for DCDC2 - required for rail stability // disable CCM, force PWM on for DCDC2 - required for rail stability
pmic.set_pwm_mode(&mut i2c, cramium_hal::axp2101::WhichDcDc::Dcdc2, true).unwrap(); pmic.set_pwm_mode(&mut i2c, cramium_hal::axp2101::WhichDcDc::Dcdc2, true).unwrap();
pmic.set_ldo(&mut i2c, Some(2.5), cramium_hal::axp2101::WhichLdo::Aldo2).unwrap(); pmic.set_ldo(&mut i2c, Some(2.7), cramium_hal::axp2101::WhichLdo::Aldo2).unwrap();
pmic.set_dcdc(&mut i2c, Some((1.2, false)), cramium_hal::axp2101::WhichDcDc::Dcdc4).unwrap(); pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc4).unwrap();
pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc5).unwrap(); pmic.set_dcdc(&mut i2c, Some((1.8, false)), cramium_hal::axp2101::WhichDcDc::Dcdc5).unwrap();
// apply proposed baosec settings // apply proposed baosec settings
@ -405,6 +409,7 @@ pub fn early_init() -> u32 {
// show the boot logo // show the boot logo
use ux_api::minigfx::FrameBuffer; use ux_api::minigfx::FrameBuffer;
let mut udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
let mut sh1107 = cramium_hal::sh1107::Oled128x128::new( let mut sh1107 = cramium_hal::sh1107::Oled128x128::new(
cramium_hal::sh1107::MainThreadToken::new(), cramium_hal::sh1107::MainThreadToken::new(),
perclk, perclk,
@ -439,69 +444,24 @@ pub fn early_init() -> u32 {
delay(250); delay(250);
} }
} }
} //------------- test camera ------------
#[cfg(feature = "cam-test")]
// Board bring-up: send characters to confirm the UART is configured & ready to go for the logging crate!
// The "boot gutter" also has a role to pause the system in "real mode" before VM is mapped in Xous
// makes things a little bit cleaner for JTAG ops, it seems.
#[cfg(feature = "board-bringup")]
{ {
use cramium_hal::{iox::IoxValue, minigfx::ColorNative, sh1107::Mono, udma::Udma}; use cramium_api::iox::IoxValue;
use ux_api::minigfx::{Line, Point}; use cramium_hal::sh1107::Mono;
use cramium_hal::udma::Udma;
use ux_api::minigfx::Point;
use crate::platform::cramium::gfx; use crate::platform::cramium::gfx;
//------------- test I2C ------------ use crate::platform::cramium::qr;
crate::println!("i2c test");
let mut id = [0u8; 8];
crate::println!("read USB ID");
i2c.i2c_read_async(0x47, 0, id.len(), false).expect("couldn't initiate read");
// this let is necessary to get `id` to go out of scope
i2c.i2c_await(Some(&mut id), false).unwrap();
crate::println!("ID result: {:x?}", id);
let mut ldo = [0u8; 11]; let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
crate::println!("AXP2101 LDO"); let udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result: {:x?}", ldo);
crate::println!("write1");
ldo[10] = 0xd;
i2c.i2c_write_async(0x34, 0x90, &ldo).expect("couldn't initiate write");
i2c.i2c_await(None, false).unwrap();
ldo.fill(0);
crate::println!("AXP2101 LDO - last value should be 0xd");
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result - last value should be 0xd: {:x?}", ldo);
crate::println!("write2");
ldo[10] = 0xe;
i2c.i2c_write_async(0x34, 0x90, &ldo).expect("couldn't initiate write");
i2c.i2c_await(None, false).unwrap();
ldo.fill(0);
crate::println!("AXP2101 LDO - last value should be 0xe");
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result - last value should be 0xe: {:x?}", ldo);
//------------- test USB ---------------
#[cfg(feature = "usb-test")]
{
crate::platform::cramium::usb::init_usb();
// this does not return if USB is initialized correctly...
unsafe {
crate::platform::cramium::usb::test_usb();
}
}
//------------- test OV2640 ------------
// setup camera pins // setup camera pins
let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_ov2640_pins(&iox); let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_camera_pins(&iox);
// disable camera powerdown // disable camera powerdown
iox.set_gpio_pin(cam_pdwn_bnk, cam_pdwn_pin, cramium_hal::iox::IoxValue::Low); iox.set_gpio_pin(cam_pdwn_bnk, cam_pdwn_pin, IoxValue::Low);
udma_global.clock_on(PeriphId::Cam); udma_global.clock_on(PeriphId::Cam);
let cam_ifram = unsafe { let cam_ifram = unsafe {
cramium_hal::ifram::IframRange::from_raw_parts( cramium_hal::ifram::IframRange::from_raw_parts(
@ -511,24 +471,19 @@ pub fn early_init() -> u32 {
) )
}; };
// this is safe because we turned on the clocks before calling it // this is safe because we turned on the clocks before calling it
let mut cam = unsafe { cramium_hal::ov2640::Ov2640::new_with_ifram(cam_ifram) }; let mut cam = unsafe { cramium_hal::gc2145::Gc2145::new_with_ifram(cam_ifram) };
cam.delay(100); cam.delay(100);
let (pid, mid) = cam.read_id(&mut i2c); let (pid, mid) = cam.read_id(&mut i2c);
crate::println!("Camera pid {:x}, mid {:x}", pid, mid); crate::println!("Camera pid {:x}, did {:x}", pid, mid);
cam.init(&mut i2c, cramium_hal::ov2640::Resolution::Res320x240); crate::println!("enter init");
cam.poke(&mut i2c, 0xFF, 0x00); cam.init(&mut i2c, cramium_api::camera::Resolution::Res320x240);
cam.poke(&mut i2c, 0xDA, 0x01); // YUV LE
cam.delay(1); cam.delay(1);
// muck with ZMOW: only works in 160x120 mode
// cam.poke(&mut i2c, 0x5A, 0x28);
// cam.delay(1);
// cam.poke(&mut i2c, 0x5B, 0x1E);
// cam.delay(1);
let (cols, _rows) = cam.resolution();
const QR_WIDTH: usize = 256; const QR_WIDTH: usize = 256;
const QR_HEIGHT: usize = 240; const QR_HEIGHT: usize = 240;
let (cols, _rows) = cam.resolution();
let border = (cols - QR_WIDTH) / 2; let border = (cols - QR_WIDTH) / 2;
cam.set_slicing((border, 0), (cols - border, QR_HEIGHT)); cam.set_slicing((border, 0), (cols - border, QR_HEIGHT));
crate::println!("320x240 resolution setup with 256x240 slicing"); crate::println!("320x240 resolution setup with 256x240 slicing");
@ -537,39 +492,45 @@ pub fn early_init() -> u32 {
csr_tt.wfo(utra::ticktimer::CLOCKS_PER_TICK_CLOCKS_PER_TICK, 200_000); csr_tt.wfo(utra::ticktimer::CLOCKS_PER_TICK_CLOCKS_PER_TICK, 200_000);
csr_tt.wfo(utra::ticktimer::CONTROL_RESET, 1); csr_tt.wfo(utra::ticktimer::CONTROL_RESET, 1);
let iox_loop = iox.clone();
let mut frames = 0; let mut frames = 0;
let mut frame = [0u8; QR_WIDTH * QR_HEIGHT]; let mut frame = [0u8; QR_WIDTH * QR_HEIGHT];
while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {} while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {}
udma_uart.setup_async_read(); udma_uart.setup_async_read();
let mut c = 0u8; let mut c = 0u8;
const BW_THRESH: u8 = 128; let mut bw_thresh: u8 = 131;
#[cfg(feature = "qr")]
loop { loop {
// toggling this off can improve performance by wasting less time "waiting" for the next frame... // toggling this off can improve performance by wasting less time "waiting" for the next
// however, you will get "frame rolling" if the capture isn't initiated at exactly the right time. // frame... however, you will get "frame rolling" if the capture isn't
// things could be improved by making this interrupt-driven. // initiated at exactly the right time. things could be improved by making
// maybe the same effect could also be achieved with frame dropping? not sure. to be researched. // this interrupt-driven. maybe the same effect could also be achieved with
// frame dropping? not sure. to be researched.
while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {} while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {}
cam.capture_async(); cam.capture_async();
// blit fb to sh1107 // blit fb to sh1107
let mut sum: u32 = 0;
let mut count: u32 = 0;
for (y, row) in frame.chunks(QR_WIDTH).enumerate() { for (y, row) in frame.chunks(QR_WIDTH).enumerate() {
if y & 1 == 0 { if y & 1 == 0 {
for (x, &pixval) in row.iter().enumerate() { for (x, &pixval) in row.iter().enumerate() {
// note: image rotation is done here by swapping x/y coords in the lower equations
if x & 1 == 0 { if x & 1 == 0 {
if x < sh1107.dimensions().x as usize * 2 if y < sh1107.dimensions().x as usize * 2
&& y < sh1107.dimensions().y as usize * 2 && x < sh1107.dimensions().y as usize * 2
- (gfx::CHAR_HEIGHT as usize + 1) * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2
{ {
let luminance = pixval & 0xff; let luminance = pixval & 0xff;
if luminance > BW_THRESH { count += 1;
sum += luminance as u32;
if luminance > bw_thresh {
// flip on y to adjust for sensor orientation. Lower left is (0, 0) // flip on y to adjust for sensor orientation. Lower left is (0, 0)
// on the display. // on the display.
sh1107.put_pixel( sh1107.put_pixel(
Point::new( Point::new(
x as isize / 2, y as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2), (sh1107.dimensions().y - 1) - (x as isize / 2),
), ),
Mono::White.into(), Mono::White.into(),
); );
@ -581,10 +542,11 @@ pub fn early_init() -> u32 {
} }
} }
} }
bw_thresh = (sum / count) as u8;
let mut candidates: [Option<Point>; 64] = [None; 64]; let mut candidates: [Option<Point>; 64] = [None; 64];
crate::println!("\n\r------------- SEARCH -----------"); crate::println!("\n\r------------- SEARCH -----------");
let finder_width = qr::find_finders(&mut candidates, &frame, BW_THRESH, QR_WIDTH) as isize; let finder_width = qr::find_finders(&mut candidates, &frame, bw_thresh, QR_WIDTH) as isize;
const CROSSHAIR_LEN: isize = 3; const CROSSHAIR_LEN: isize = 3;
let mut candidates_found = 0; let mut candidates_found = 0;
let mut candidate3 = [Point::new(0, 0); 3]; let mut candidate3 = [Point::new(0, 0); 3];
@ -612,15 +574,17 @@ pub fn early_init() -> u32 {
+ (crate::platform::cramium::qr::FINDER_SEARCH_MARGIN * finder_width) + (crate::platform::cramium::qr::FINDER_SEARCH_MARGIN * finder_width)
/ (1 + 1 + 3 + 1 + 1)) as usize, / (1 + 1 + 3 + 1 + 1)) as usize,
); );
// just doing this to avoid nesting another if level out in the test code; make this better! // just doing this to avoid nesting another if level out in the test code; make this
// better!
if maybe_qr_corners.is_none() { if maybe_qr_corners.is_none() {
continue; continue;
} }
let mut qr_corners = maybe_qr_corners.unwrap(); let mut qr_corners = maybe_qr_corners.unwrap();
let dims = Point::new(QR_WIDTH as isize, QR_HEIGHT as isize); let dims = Point::new(QR_WIDTH as isize, QR_HEIGHT as isize);
let mut il = ImageRoi::new(&mut frame, dims, BW_THRESH); let mut il = ImageRoi::new(&mut frame, dims, bw_thresh);
let (src, dst) = qr_corners.mapping(&mut il, crate::platform::cramium::qr::HOMOGRAPHY_MARGIN); let (src, dst) =
qr_corners.mapping(&mut il, crate::platform::cramium::qr::HOMOGRAPHY_MARGIN);
for s in src.iter() { for s in src.iter() {
if let Some(p) = s { if let Some(p) = s {
crate::println!("src {:?}", p); crate::println!("src {:?}", p);
@ -691,14 +655,16 @@ pub fn early_init() -> u32 {
- (gfx::CHAR_HEIGHT as usize + 1) * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2
{ {
let luminance = pixval & 0xff; let luminance = pixval & 0xff;
if luminance > BW_THRESH { if luminance > bw_thresh {
// flip on y to adjust for sensor orientation. Lower left // flip on y to adjust for sensor orientation. Lower
// left
// is (0, 0) // is (0, 0)
// on the display. // on the display.
sh1107.put_pixel( sh1107.put_pixel(
Point::new( Point::new(
x as isize / 2, x as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2), (sh1107.dimensions().y - 1)
- (y as isize / 2),
), ),
Mono::White.into(), Mono::White.into(),
); );
@ -706,7 +672,8 @@ pub fn early_init() -> u32 {
sh1107.put_pixel( sh1107.put_pixel(
Point::new( Point::new(
x as isize / 2, x as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2), (sh1107.dimensions().y - 1)
- (y as isize / 2),
), ),
Mono::Black.into(), Mono::Black.into(),
); );
@ -783,7 +750,6 @@ pub fn early_init() -> u32 {
} }
// crate::println!("frame {}", frames); // crate::println!("frame {}", frames);
sh1107.buffer_swap();
sh1107.draw(); sh1107.draw();
// clear the front buffer // clear the front buffer
@ -798,7 +764,10 @@ pub fn early_init() -> u32 {
crate::println!("CAM_LL {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_LL)); crate::println!("CAM_LL {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_LL));
crate::println!("CAM_UR {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_UR)); crate::println!("CAM_UR {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_UR));
crate::println!("CAM_CFG_SIZE {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_SIZE)); crate::println!("CAM_CFG_SIZE {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_SIZE));
crate::println!("CAM_CFG_FILTER {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_FILTER)); crate::println!(
"CAM_CFG_FILTER {:x}",
cam.csr().r(utra::udma_camera::REG_CAM_CFG_FILTER)
);
crate::println!("CAM_SYNC {:x}", cam.csr().r(utra::udma_camera::REG_CAM_VSYNC_POLARITY)); crate::println!("CAM_SYNC {:x}", cam.csr().r(utra::udma_camera::REG_CAM_VSYNC_POLARITY));
} }
@ -811,12 +780,75 @@ pub fn early_init() -> u32 {
// Stride through the slice as a u32, allowing us to make the most out of each slow // Stride through the slice as a u32, allowing us to make the most out of each slow
// read from IFRAM, and unpack the values into fast SRAM. // read from IFRAM, and unpack the values into fast SRAM.
for (&u32src, u8dest) in fb.iter().zip(frame.chunks_mut(2)) { for (&u32src, u8dest) in fb.iter().zip(frame.chunks_mut(2)) {
u8dest[0] = (u32src & 0xff) as u8; // note that GC2145 puts Y in top bytes of u16s, OV2640 puts them in the lower bytses of
u8dest[1] = ((u32src >> 16) & 0xff) as u8; // u16's.
u8dest[0] = ((u32src >> 8) & 0xff) as u8;
u8dest[1] = ((u32src >> 24) & 0xff) as u8;
} }
frames += 1; frames += 1;
} }
}
}
// Board bring-up: send characters to confirm the UART is configured & ready to go for the logging crate!
// The "boot gutter" also has a role to pause the system in "real mode" before VM is mapped in Xous
// makes things a little bit cleaner for JTAG ops, it seems.
#[cfg(feature = "board-bringup")]
{
use cramium_hal::{iox::IoxValue, minigfx::ColorNative, sh1107::Mono, udma::Udma};
use ux_api::minigfx::{Line, Point};
use crate::platform::cramium::gfx;
//------------- test I2C ------------
crate::println!("i2c test");
let mut id = [0u8; 8];
crate::println!("read USB ID");
i2c.i2c_read_async(0x47, 0, id.len(), false).expect("couldn't initiate read");
// this let is necessary to get `id` to go out of scope
i2c.i2c_await(Some(&mut id), false).unwrap();
crate::println!("ID result: {:x?}", id);
let mut ldo = [0u8; 11];
crate::println!("AXP2101 LDO");
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result: {:x?}", ldo);
crate::println!("write1");
ldo[10] = 0xd;
i2c.i2c_write_async(0x34, 0x90, &ldo).expect("couldn't initiate write");
i2c.i2c_await(None, false).unwrap();
ldo.fill(0);
crate::println!("AXP2101 LDO - last value should be 0xd");
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result - last value should be 0xd: {:x?}", ldo);
crate::println!("write2");
ldo[10] = 0xe;
i2c.i2c_write_async(0x34, 0x90, &ldo).expect("couldn't initiate write");
i2c.i2c_await(None, false).unwrap();
ldo.fill(0);
crate::println!("AXP2101 LDO - last value should be 0xe");
i2c.i2c_read_async(0x34, 0x90, ldo.len(), false).expect("couldn't initiate read");
i2c.i2c_await(Some(&mut ldo), false).unwrap();
crate::println!("LDO result - last value should be 0xe: {:x?}", ldo);
//------------- test USB ---------------
#[cfg(feature = "usb-test")]
{
crate::platform::cramium::usb::init_usb();
// this does not return if USB is initialized correctly...
unsafe {
crate::platform::cramium::usb::test_usb();
}
}
}
#[cfg(feature = "board-bringup")]
{
// ---------------- test TRNG ------------------- // ---------------- test TRNG -------------------
// configure the SCE clocks to enable the TRNG // configure the SCE clocks to enable the TRNG
let mut sce = CSR::new(HW_SCE_GLBSFR_BASE as *mut u32); let mut sce = CSR::new(HW_SCE_GLBSFR_BASE as *mut u32);

View file

@ -3,7 +3,7 @@ use core::cell::RefCell;
use core::convert::TryFrom; use core::convert::TryFrom;
use core::ops::{BitXor, Not}; use core::ops::{BitXor, Not};
use cramium_hal::minigfx::{ColorNative, FrameBuffer, Point}; use ux_api::minigfx::{ColorNative, FrameBuffer, Point};
use super::gfx; use super::gfx;
@ -30,7 +30,7 @@ pub const HOMOGRAPHY_MARGIN: isize = -4;
pub const CROSSHAIR_LEN: isize = 3; pub const CROSSHAIR_LEN: isize = 3;
pub fn draw_crosshair(image: &mut dyn FrameBuffer, p: Point) { pub fn draw_crosshair(image: &mut dyn FrameBuffer, p: Point) {
use cramium_hal::minigfx::Line; use ux_api::minigfx::Line;
gfx::line( gfx::line(
image, image,
Line::new(p + Point::new(0, CROSSHAIR_LEN), p - Point::new(0, CROSSHAIR_LEN)), Line::new(p + Point::new(0, CROSSHAIR_LEN), p - Point::new(0, CROSSHAIR_LEN)),