Merge pull request #638 from betrusted-io/gc2145

GC2145
This commit is contained in:
bunnie 2025-05-01 16:46:30 +08:00 committed by GitHub
commit 32ed73b4f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 2833 additions and 359 deletions

View file

@ -29,21 +29,27 @@ xous = { version = "0.9.64", features = ["v2p"] }
[features]
camera-ov2640 = []
camera-gc2145 = []
display-sh1107 = []
axp2101 = []
cramium-soc = ["utralib/cramium-soc"]
board-baosec = [
"camera-ov2640",
"camera-gc2145",
"display-sh1107",
"axp2101",
"utralib/cramium-soc",
"ux-api/board-baosec",
] # USB form factor token
loader-baosec = ["camera-ov2640", "display-sh1107", "axp2101", "ux-api/loader-baosec"]
test-baosec = ["ux-api/loader-baosec", "camera-ov2640"]
loader-baosec = [
"camera-gc2145",
"display-sh1107",
"axp2101",
"ux-api/loader-baosec",
]
test-baosec = ["ux-api/loader-baosec", "camera-gc2145"]
hosted-baosec = [] # emulated hardware on x86 platform
board-baosor = [
"camera-ov2640",
"camera-gc2145",
"axp2101",
"utralib/cramium-soc",
] # Precursor form factor

View file

@ -192,7 +192,7 @@ pub fn setup_i2c_pins(iox: &dyn IoSetup) -> I2cChannel {
}
/// returns the power-down port and pin number
pub fn setup_ov2640_pins<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
pub fn setup_camera_pins<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
// power-down pin - default to powered down
iox.set_gpio_pin_value(IoxPort::PC, 14, IoxValue::High);
iox.setup_pin(

View file

@ -0,0 +1,37 @@
#![allow(dead_code)]
/*
The constant tables below are derived from https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/video/gc2145.c
with the following license:
* Copyright (c) 2024 Felipe Neves
*
* SPDX-License-Identifier: Apache-2.0
*/
pub(crate) const GC2145_REG_AMODE1: u8 = 0x17;
pub(crate) const GC2145_AMODE1_WINDOW_MASK: u8 = 0xFC;
pub(crate) const GC2145_REG_AMODE1_DEF: u8 = 0x14;
pub(crate) const GC2145_REG_OUTPUT_FMT: u8 = 0x84;
pub(crate) const GC2145_REG_OUTPUT_FMT_MASK: u8 = 0x1F;
pub(crate) const GC2145_REG_OUTPUT_FMT_RGB565: u8 = 0x06;
pub(crate) const GC2145_REG_OUTPUT_FMT_YCBYCR: u8 = 0x02;
pub(crate) const GC2145_REG_SYNC_MODE: u8 = 0x86;
pub(crate) const GC2145_REG_SYNC_MODE_DEF: u8 = 0x23;
pub(crate) const GC2145_REG_SYNC_MODE_COL_SWITCH: u8 = 0x10;
pub(crate) const GC2145_REG_SYNC_MODE_ROW_SWITCH: u8 = 0x20;
pub(crate) const GC2145_REG_RESET: u8 = 0xFE;
pub(crate) const GC2145_REG_SW_RESET: u8 = 0x80;
pub(crate) const GC2145_SET_P0_REGS: u8 = 0x00;
pub(crate) const GC2145_REG_CROP_ENABLE: u8 = 0x90;
pub(crate) const GC2145_CROP_SET_ENABLE: u8 = 0x01;
pub(crate) const GC2145_REG_BLANK_WINDOW_BASE: u8 = 0x09;
pub(crate) const GC2145_REG_WINDOW_BASE: u8 = 0x91;
pub(crate) const GC2145_REG_SUBSAMPLE: u8 = 0x99;
pub(crate) const GC2145_REG_SUBSAMPLE_MODE: u8 = 0x9A;
pub(crate) const GC2145_SUBSAMPLE_MODE_SMOOTH: u8 = 0x0E;
pub(crate) const GC2145_PIDH: u8 = 0xF0;
pub(crate) const GC2145_PIDL: u8 = 0xF1;
pub(crate) const GC2145_I2C_ID: u8 = 0xFB;
pub(crate) const UXGA_HSIZE: u16 = 1600;
pub(crate) const UXGA_VSIZE: u16 = 1200;

View file

@ -0,0 +1,328 @@
use cramium_api::camera::*;
use cramium_api::*;
use utralib::CSR;
use utralib::utra;
use utralib::utra::udma_camera::REG_CAM_CFG_GLOB;
use super::constants::*;
use super::tables::*;
use crate::ifram::IframRange;
use crate::udma::Udma;
use crate::udma::*;
pub const GC2145_DEV: u8 = 0x3C;
pub const CFG_FRAMEDROP_EN: utralib::Field = utralib::Field::new(1, 0, REG_CAM_CFG_GLOB);
pub const CFG_FRAMEDROP_VAL: utralib::Field = utralib::Field::new(6, 1, REG_CAM_CFG_GLOB);
pub const CFG_FRAMESLICE_EN: utralib::Field = utralib::Field::new(1, 7, REG_CAM_CFG_GLOB);
pub const CFG_FORMAT: utralib::Field = utralib::Field::new(3, 8, REG_CAM_CFG_GLOB);
pub const CFG_SHIFT: utralib::Field = utralib::Field::new(4, 11, REG_CAM_CFG_GLOB);
pub const CFG_GLOB_EN: utralib::Field = utralib::Field::new(1, 31, REG_CAM_CFG_GLOB);
pub struct Gc2145 {
csr: CSR<u32>,
ifram: Option<IframRange>,
resolution: Resolution,
slicing: Option<(usize, usize)>,
}
impl Udma for Gc2145 {
fn csr_mut(&mut self) -> &mut CSR<u32> { &mut self.csr }
fn csr(&self) -> &CSR<u32> { &self.csr }
}
impl Gc2145 {
#[cfg(feature = "std")]
/// Safety: clocks must be turned on before this is called
pub unsafe fn new() -> Result<Self, xous::Error> {
let ifram_virt = xous::syscall::map_memory(
xous::MemoryAddress::new(crate::board::CAM_IFRAM_ADDR),
None,
crate::board::CAM_IFRAM_LEN_PAGES * 4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)?;
let ifram = IframRange::from_raw_parts(
crate::board::CAM_IFRAM_ADDR,
ifram_virt.as_ptr() as usize,
ifram_virt.len(),
);
Ok(Gc2145::new_with_ifram(ifram))
}
pub unsafe fn new_with_ifram(ifram: IframRange) -> Self {
#[cfg(target_os = "xous")]
let csr_range = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::udma_camera::HW_UDMA_CAMERA_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map cam port");
#[cfg(target_os = "xous")]
let csr = CSR::new(csr_range.as_mut_ptr() as *mut u32);
#[cfg(not(target_os = "xous"))]
let csr = CSR::new(utra::udma_camera::HW_UDMA_CAMERA_BASE as *mut u32);
Self {
csr,
ifram: Some(ifram),
// bogus value
resolution: Resolution::Res160x120,
slicing: None,
}
}
pub fn release_ifram(&mut self) {
if let Some(ifram) = self.ifram.take() {
xous::syscall::unmap_memory(ifram.virt_range).unwrap();
}
}
pub fn claim_ifram(&mut self) -> Result<(), xous::Error> {
if self.ifram.is_none() {
let ifram = xous::syscall::map_memory(
xous::MemoryAddress::new(crate::board::CAM_IFRAM_ADDR),
None,
crate::board::CAM_IFRAM_LEN_PAGES * 4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)?;
let cam_ifram = unsafe {
crate::ifram::IframRange::from_raw_parts(
crate::board::CAM_IFRAM_ADDR,
ifram.as_ptr() as usize,
ifram.len(),
)
};
self.ifram = Some(cam_ifram);
}
Ok(())
}
pub fn has_ifram(&self) -> bool { self.ifram.is_some() }
pub fn poke(&self, i2c: &mut dyn I2cApi, adr: u8, dat: u8) {
i2c.i2c_write(GC2145_DEV, adr, &[dat]).expect("write failed");
}
// chip does not support sequential reads
pub fn peek(&self, i2c: &mut dyn I2cApi, adr: u8, dat: &mut [u8]) {
for (i, d) in dat.iter_mut().enumerate() {
let mut one_byte = [0u8];
i2c.i2c_read(GC2145_DEV, adr + i as u8, &mut one_byte, false).expect("read failed");
*d = one_byte[0];
}
}
fn gc2145_set_window(&self, i2c: &mut dyn I2cApi, mut reg: u8, x: u16, y: u16, w: u16, h: u16) {
self.poke(i2c, GC2145_REG_RESET, GC2145_SET_P0_REGS);
/* Y/row offset */
self.poke(i2c, reg, (y >> 8) as u8);
reg += 1;
self.poke(i2c, reg, (y & 0xff) as u8);
reg += 1;
/* X/col offset */
self.poke(i2c, reg, (x >> 8) as u8);
reg += 1;
self.poke(i2c, reg, (x & 0xff) as u8);
reg += 1;
/* Window height */
self.poke(i2c, reg, (h >> 8) as u8);
reg += 1;
self.poke(i2c, reg, (h & 0xff) as u8);
reg += 1;
/* Window width */
self.poke(i2c, reg, (w >> 8) as u8);
reg += 1;
self.poke(i2c, reg, (w & 0xff) as u8);
}
fn set_resolution(&self, i2c: &mut dyn I2cApi, w: u16, h: u16) {
// TODO: figure out what this parameter even means in the context of an...unconstrained variable
// resolution? Maybe? It's not even clear to me that was the original intent from the C driver.
// 160x120 base scaling
// let c_ratio = 4u16;
// let r_ratio = 4u16;
// 320x240 base scaling
let c_ratio = 3u16;
let r_ratio = 3u16;
// 640x480 base scaling
// let c_ratio = 2u16;
// let r_ratio = 2u16;
/* Calculates the window boundaries to obtain the desired resolution */
let win_w = w * c_ratio;
let win_h = h * r_ratio;
let x = ((win_w / c_ratio) - w) / 2;
let y = ((win_h / r_ratio) - h) / 2;
let win_x = (UXGA_HSIZE - win_w) / 2;
let win_y = (UXGA_VSIZE - win_h) / 2;
/* Set readout window first. */
self.gc2145_set_window(i2c, GC2145_REG_BLANK_WINDOW_BASE, win_x, win_y, win_w + 16, win_h + 8);
/* Set cropping window next. */
self.gc2145_set_window(i2c, GC2145_REG_WINDOW_BASE, x, y, w, h);
/* Enable crop */
self.poke(i2c, GC2145_REG_CROP_ENABLE, GC2145_CROP_SET_ENABLE);
// self.poke(i2c, GC2145_REG_CROP_ENABLE, 0);
/* Set Sub-sampling ratio and mode */
self.poke(i2c, GC2145_REG_SUBSAMPLE, ((r_ratio << 4) | c_ratio) as u8);
self.poke(i2c, GC2145_REG_SUBSAMPLE_MODE, GC2145_SUBSAMPLE_MODE_SMOOTH);
self.delay(30);
// divide the clock down, so that the system can keep up.
// self.poke(i2c, 0xFA, 0x32); // this offers a higher frame rate, but a greater bus congestion -
// maybe available on NTO
// self.poke(i2c, 0xFA, 0x63); // this is necessary for MPW due to SPI backpressure bug
self.poke(i2c, 0xFA, 0x52); // this is necessary for MPW due to SPI backpressure bug
}
#[inline(never)]
pub fn init(&mut self, i2c: &mut dyn I2cApi, resolution: Resolution) {
// initiate a reset
self.poke(i2c, GC2145_REG_RESET, GC2145_REG_SW_RESET);
self.delay(300); // wait for reset
// do the init pokes, these settings are from the zephyr-OS reference code
for &[adr, dat] in GC2145_INIT.iter() {
self.poke(i2c, adr, dat);
}
// setup AEC, these settings are yanked out of the Linux kernel
for &[adr, dat] in GC2145_AEC.iter() {
self.poke(i2c, adr, dat);
}
// set up YUV mode
self.poke(i2c, GC2145_REG_RESET, GC2145_SET_P0_REGS);
self.delay(30);
let mut buf = [0u8; 1];
self.peek(i2c, GC2145_REG_OUTPUT_FMT, &mut buf);
self.delay(30);
self.poke(
i2c,
GC2145_REG_OUTPUT_FMT,
(buf[0] & !GC2145_REG_OUTPUT_FMT_MASK) | GC2145_REG_OUTPUT_FMT_YCBYCR,
);
self.delay(30);
let (w, h) = resolution.into();
crate::println!("resolution set to {}x{}", w, h);
self.set_resolution(i2c, w as u16, h as u16);
self.resolution = resolution;
crate::println!("udma setup");
// set sync polarity
let vsync_pol = 0;
let hsync_pol = match resolution {
Resolution::Res320x240 => 0,
_ => 0,
};
self.csr.wo(
utra::udma_camera::REG_CAM_VSYNC_POLARITY,
self.csr.ms(utra::udma_camera::REG_CAM_VSYNC_POLARITY_R_CAM_VSYNC_POLARITY, vsync_pol)
| self.csr.ms(utra::udma_camera::REG_CAM_VSYNC_POLARITY_R_CAM_HSYNC_POLARITY, hsync_pol),
);
// multiply by 1
self.csr.wo(utra::udma_camera::REG_CAM_CFG_FILTER, 0x01_01_01);
let (x, _y) = resolution.into();
self.csr.wo(utra::udma_camera::REG_CAM_CFG_SIZE, (x as u32 - 1) << 16);
let global = self.csr.ms(CFG_FRAMEDROP_EN, 0)
| self.csr.ms(CFG_FORMAT, Format::BypassLe as u32)
| self.csr.ms(CFG_FRAMESLICE_EN, 0)
| self.csr.ms(CFG_SHIFT, 0);
self.csr.wo(utra::udma_camera::REG_CAM_CFG_GLOB, global);
}
/// TODO: figure out how to length-bound this to...the frame slice size? line size? idk...
pub fn rx_buf<T: UdmaWidths>(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_slice() }
/// TODO: figure out how to length-bound this to...the frame slice size? line size? idk...
pub unsafe fn rx_buf_phys<T: UdmaWidths>(&self) -> &[T] { &self.ifram.as_ref().unwrap().as_phys_slice() }
/// TODO: Rework this to use the frame sync + automatic re-initiation on capture_await() for frames
/// TODO: Also make an interrupt driven version of this.
pub fn capture_async(&mut self) {
// we want the sliced resolution so resolve resolution through the method call wrapper
let (cols, rows) = self.resolution();
let total_len = rows * cols;
self.csr.rmwf(CFG_GLOB_EN, 1);
unsafe { self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::<u16>()[..total_len], CFG_EN | CFG_SIZE_16) }
}
pub fn capture_await(&mut self, _use_yield: bool) {
while self.udma_busy(Bank::Rx) {
#[cfg(feature = "std")]
if _use_yield {
xous::yield_slice();
}
}
}
pub fn resolution(&self) -> (usize, usize) {
if let Some((x, y)) = self.slicing { (x, y) } else { self.resolution.into() }
}
pub fn set_slicing(&mut self, ll: (usize, usize), ur: (usize, usize)) {
let (llx, lly) = ll;
let (urxx, uryy) = ur;
let urx = urxx.saturating_sub(1);
let ury = uryy.saturating_sub(1);
self.csr.wo(utra::udma_camera::REG_CAM_CFG_LL, llx as u32 & 0xFFFF | ((lly as u32 & 0xFFFF) << 16));
self.csr.wo(utra::udma_camera::REG_CAM_CFG_UR, urx as u32 & 0xFFFF | ((ury as u32 & 0xFFFF) << 16));
self.csr.rmwf(CFG_FRAMESLICE_EN, 1);
self.slicing = Some((urxx - llx, uryy - lly));
// self.csr.wo(utra::udma_camera::REG_CAM_CFG_SIZE, (urx - llx) as u32 - 1);
}
pub fn disable_slicing(&mut self) {
self.csr.rmwf(CFG_FRAMESLICE_EN, 0);
self.slicing = None;
}
/// Returns (product ID, manufacturer ID)
/// Should be 0x2155, 0x0078
pub fn read_id(&self, i2c: &mut dyn I2cApi) -> (u16, u16) {
let mut pid = [0u8; 2];
let mut did = [0u8; 1];
self.peek(i2c, GC2145_PIDH, &mut pid); // should return 0x2155
self.peek(i2c, GC2145_I2C_ID, &mut did);
(u16::from_be_bytes(pid), did[0] as u16)
}
pub fn delay(&self, quantum: usize) {
#[cfg(feature = "std")]
{
let tt = xous_api_ticktimer::Ticktimer::new().unwrap();
tt.sleep_ms(quantum).ok();
}
#[cfg(not(feature = "std"))]
{
use utralib::{CSR, utra};
// abuse the d11ctime timer to create some time-out like thing
let mut d11c = CSR::new(utra::d11ctime::HW_D11CTIME_BASE as *mut u32);
d11c.wfo(utra::d11ctime::CONTROL_COUNT, 333_333); // 1.0ms per interval
let mut polarity = d11c.rf(utra::d11ctime::HEARTBEAT_BEAT);
for _ in 0..quantum {
while polarity == d11c.rf(utra::d11ctime::HEARTBEAT_BEAT) {}
polarity = d11c.rf(utra::d11ctime::HEARTBEAT_BEAT);
}
// we have to split this because we don't know where we caught the previous interval
if quantum == 1 {
while polarity == d11c.rf(utra::d11ctime::HEARTBEAT_BEAT) {}
}
}
}
}

View file

@ -0,0 +1,4 @@
mod constants;
pub mod gc2145;
mod tables;
pub use gc2145::*;

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,8 @@ pub mod debug;
#[cfg(feature = "axp2101")]
pub mod axp2101;
pub mod board;
#[cfg(feature = "camera-gc2145")]
pub mod gc2145;
pub mod ifram;
pub mod iox;
#[cfg(feature = "camera-ov2640")]

View file

@ -155,6 +155,10 @@ impl Ov2640 {
}
self.resolution = resolution;
// Setup YUV output mode
self.poke(&mut i2c, 0xFF, 0x00);
self.poke(&mut i2c, 0xDA, 0x01); // YUV LE
// set sync polarity
let vsync_pol = 0;
let hsync_pol = match resolution {

View file

@ -114,7 +114,7 @@ swap = ["aes-gcm-siv"]
# cramium target flags
board-bringup = []
sh1107-bringup = [] # used to discover FB parameters
sh1107-bringup = [] # used to discover FB parameters
sram-margin = []
boot-delay = []
simulation-only = []
@ -128,11 +128,12 @@ dump-trng = []
fast-fclk = []
clock-tests = []
reset-debug = []
cam-test = ["nalgebra", "libm", "cramium-hal/debug-print-usb"]
usb = []
qr = ["nalgebra", "libm"]
updates = []
cramium-mpw = ["cramium-hal/mpw"]
userspace-swap-debug = [] # sets up app UART mapping.
userspace-swap-debug = [] # sets up app UART mapping.
# general flags
debug-print = []

View file

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

View file

@ -14,6 +14,10 @@ use utralib::generated::*;
#[cfg(feature = "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
// 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
@ -336,8 +340,8 @@ pub fn early_init() -> u32 {
// 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_ldo(&mut i2c, Some(2.5), cramium_hal::axp2101::WhichLdo::Aldo2).unwrap();
pmic.set_dcdc(&mut i2c, Some((1.2, false)), cramium_hal::axp2101::WhichDcDc::Dcdc4).unwrap();
pmic.set_ldo(&mut i2c, Some(2.7), cramium_hal::axp2101::WhichLdo::Aldo2).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();
// apply proposed baosec settings
@ -405,6 +409,7 @@ pub fn early_init() -> u32 {
// show the boot logo
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(
cramium_hal::sh1107::MainThreadToken::new(),
perclk,
@ -439,6 +444,350 @@ pub fn early_init() -> u32 {
delay(250);
}
}
//------------- test camera ------------
#[cfg(feature = "cam-test")]
{
use cramium_api::iox::IoxValue;
use cramium_hal::sh1107::Mono;
use cramium_hal::udma::Udma;
use ux_api::minigfx::Point;
use crate::platform::cramium::gfx;
use crate::platform::cramium::qr;
let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
let udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
// setup camera pins
let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_camera_pins(&iox);
// disable camera powerdown
iox.set_gpio_pin(cam_pdwn_bnk, cam_pdwn_pin, IoxValue::Low);
udma_global.clock_on(PeriphId::Cam);
let cam_ifram = unsafe {
cramium_hal::ifram::IframRange::from_raw_parts(
cramium_hal::board::CAM_IFRAM_ADDR,
cramium_hal::board::CAM_IFRAM_ADDR,
cramium_hal::board::CAM_IFRAM_LEN_PAGES * 4096,
)
};
// this is safe because we turned on the clocks before calling it
let mut cam = unsafe { cramium_hal::gc2145::Gc2145::new_with_ifram(cam_ifram) };
cam.delay(100);
let (pid, mid) = cam.read_id(&mut i2c);
crate::println!("Camera pid {:x}, did {:x}", pid, mid);
crate::println!("enter init");
cam.init(&mut i2c, cramium_api::camera::Resolution::Res320x240);
cam.delay(1);
const QR_WIDTH: usize = 256;
const QR_HEIGHT: usize = 240;
let (cols, _rows) = cam.resolution();
let border = (cols - QR_WIDTH) / 2;
cam.set_slicing((border, 0), (cols - border, QR_HEIGHT));
crate::println!("320x240 resolution setup with 256x240 slicing");
let mut csr_tt = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
csr_tt.wfo(utra::ticktimer::CLOCKS_PER_TICK_CLOCKS_PER_TICK, 200_000);
csr_tt.wfo(utra::ticktimer::CONTROL_RESET, 1);
let iox_loop = iox.clone();
let mut frames = 0;
let mut frame = [0u8; QR_WIDTH * QR_HEIGHT];
while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {}
udma_uart.setup_async_read();
let mut c = 0u8;
let mut bw_thresh: u8 = 131;
loop {
// toggling this off can improve performance by wasting less time "waiting" for the next
// frame... however, you will get "frame rolling" if the capture isn't
// initiated at exactly the right time. things could be improved by making
// 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 {}
cam.capture_async();
// blit fb to sh1107
let mut sum: u32 = 0;
let mut count: u32 = 0;
for (y, row) in frame.chunks(QR_WIDTH).enumerate() {
if y & 1 == 0 {
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 y < sh1107.dimensions().x as usize * 2
&& x < sh1107.dimensions().y as usize * 2
- (gfx::CHAR_HEIGHT as usize + 1) * 2
{
let luminance = pixval & 0xff;
count += 1;
sum += luminance as u32;
if luminance > bw_thresh {
// flip on y to adjust for sensor orientation. Lower left is (0, 0)
// on the display.
sh1107.put_pixel(
Point::new(
y as isize / 2,
(sh1107.dimensions().y - 1) - (x as isize / 2),
),
Mono::White.into(),
);
}
} else {
break;
}
}
}
}
}
bw_thresh = (sum / count) as u8;
let mut candidates: [Option<Point>; 64] = [None; 64];
crate::println!("\n\r------------- SEARCH -----------");
let finder_width = qr::find_finders(&mut candidates, &frame, bw_thresh, QR_WIDTH) as isize;
const CROSSHAIR_LEN: isize = 3;
let mut candidates_found = 0;
let mut candidate3 = [Point::new(0, 0); 3];
for candidate in candidates.iter() {
if let Some(c) = candidate {
if candidates_found < candidate3.len() {
candidate3[candidates_found] = *c;
}
candidates_found += 1;
crate::println!("****** candidate: {}, {} ******", c.x, c.y);
// remap image to screen coordinates (it's 2:1)
let mut c_screen = *c / 2;
// flip coordinates to match the camera data
c_screen = Point::new(c_screen.x, sh1107.dimensions().y - 1 - c_screen.y);
draw_crosshair(&mut sh1107, c_screen);
}
}
use crate::platform::cramium::qr::*;
if candidates_found == 3 {
let maybe_qr_corners = QrCorners::from_finders(
&candidate3,
Point::new(QR_WIDTH as isize, QR_HEIGHT as isize),
// add a search margin on the finder width
(finder_width
+ (crate::platform::cramium::qr::FINDER_SEARCH_MARGIN * finder_width)
/ (1 + 1 + 3 + 1 + 1)) as usize,
);
// just doing this to avoid nesting another if level out in the test code; make this
// better!
if maybe_qr_corners.is_none() {
continue;
}
let mut qr_corners = maybe_qr_corners.unwrap();
let dims = Point::new(QR_WIDTH as isize, QR_HEIGHT as isize);
let mut il = ImageRoi::new(&mut frame, dims, bw_thresh);
let (src, dst) =
qr_corners.mapping(&mut il, crate::platform::cramium::qr::HOMOGRAPHY_MARGIN);
for s in src.iter() {
if let Some(p) = s {
crate::println!("src {:?}", p);
draw_crosshair(&mut sh1107, *p / 2);
}
}
for d in dst.iter() {
if let Some(p) = d {
crate::println!("dst {:?}", p);
draw_crosshair(&mut sh1107, *p / 2);
}
}
let mut src_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
let mut dst_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
let mut all_found = true;
for (s, s_f32) in src.iter().zip(src_f.iter_mut()) {
if let Some(p) = s {
*s_f32 = p.to_f32();
} else {
all_found = false;
}
}
for (d, d_f32) in dst.iter().zip(dst_f.iter_mut()) {
if let Some(p) = d {
*d_f32 = p.to_f32();
} else {
all_found = false;
}
}
if all_found {
use crate::platform::cramium::homography::*;
if let Some(h) = find_homography(src_f, dst_f) {
if let Some(h_inv) = h.try_inverse() {
// crate::println!("{:?}", h_inv);
let h_inv_fp = matrix3_to_fixp(h_inv);
// crate::println!("{:?}", h_inv_fp);
// apply homography to generate a new buffer for processing
let mut aligned = [0u8; QR_WIDTH * QR_HEIGHT];
// iterate through pixels and apply homography
for y in 0..dims.y {
for x in 0..dims.x {
let (x_src, y_src) =
apply_fixp_homography(&h_inv_fp, (x as i32, y as i32));
if (x_src as i32 >= 0)
&& ((x_src as i32) < dims.x as i32)
&& (y_src as i32 >= 0)
&& ((y_src as i32) < dims.y as i32)
{
// println!("{},{} -> {},{}", x_src as i32, y_src as i32, x, y);
aligned[QR_WIDTH * y as usize + x as usize] =
frame[QR_WIDTH * y_src as usize + x_src as usize];
} else {
aligned[QR_WIDTH * y as usize + x as usize] = 255;
}
}
}
// blit aligned to sh1107
for (y, row) in aligned.chunks(QR_WIDTH).enumerate() {
if y & 1 == 0 {
for (x, &pixval) in row.iter().enumerate() {
if x & 1 == 0 {
if x < sh1107.dimensions().x as usize * 2
&& y < sh1107.dimensions().y as usize * 2
- (gfx::CHAR_HEIGHT as usize + 1) * 2
{
let luminance = pixval & 0xff;
if luminance > bw_thresh {
// flip on y to adjust for sensor orientation. Lower
// left
// is (0, 0)
// on the display.
sh1107.put_pixel(
Point::new(
x as isize / 2,
(sh1107.dimensions().y - 1)
- (y as isize / 2),
),
Mono::White.into(),
);
} else {
sh1107.put_pixel(
Point::new(
x as isize / 2,
(sh1107.dimensions().y - 1)
- (y as isize / 2),
),
Mono::Black.into(),
);
}
} else {
break;
}
}
}
}
}
gfx::msg(
&mut sh1107,
"Aligning...",
Point::new(0, 0),
Mono::White.into(),
Mono::Black.into(),
);
if udma_uart.read_async(&mut c) != 0 {
if c == ' ' as u32 as u8 {
crate::println!("Dumping...");
crate::println!("frame {}", frames);
udma_uart.write(
"------------------ divider ------------------\n\r".as_bytes(),
);
let hex_to_ascii = [
'0' as u32 as u8,
'1' as u32 as u8,
'2' as u32 as u8,
'3' as u32 as u8,
'4' as u32 as u8,
'5' as u32 as u8,
'6' as u32 as u8,
'7' as u32 as u8,
'8' as u32 as u8,
'9' as u32 as u8,
'A' as u32 as u8,
'B' as u32 as u8,
'C' as u32 as u8,
'D' as u32 as u8,
'E' as u32 as u8,
'F' as u32 as u8,
];
for line in frame.chunks(32) {
let mut output = [0u8; 3 * 32 + 3];
for (i, &b) in line.iter().enumerate() {
output[i * 3 + 0] = hex_to_ascii[(b >> 4) as usize];
output[i * 3 + 1] = hex_to_ascii[(b & 0xF) as usize];
output[i * 3 + 2] = ',' as u32 as u8;
}
output[3 * 32] = '\\' as u32 as u8;
output[3 * 32 + 1] = '\n' as u32 as u8;
output[3 * 32 + 2] = '\r' as u32 as u8;
udma_uart.write(&output);
}
// continue with boot
break;
}
}
}
}
}
} else {
gfx::msg(
&mut sh1107,
"Searching...",
Point::new(0, 0),
Mono::White.into(),
Mono::Black.into(),
);
}
// crate::println!("frame {}", frames);
sh1107.draw();
// clear the front buffer
sh1107.clear();
// inspect camera values
if false {
crate::println!("RX_SADDR {:x}", cam.csr().r(utra::udma_camera::REG_RX_SADDR));
crate::println!("RX_SIZE {:x}", cam.csr().r(utra::udma_camera::REG_RX_SIZE));
crate::println!("RX_CFG {:x}", cam.csr().r(utra::udma_camera::REG_RX_CFG));
crate::println!("CAM_GLOB {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_GLOB));
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_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_SYNC {:x}", cam.csr().r(utra::udma_camera::REG_CAM_VSYNC_POLARITY));
}
// wait for the transfer to finish
cam.capture_await(false);
let fb: &[u32] = cam.rx_buf();
// fb is non-cacheable, slow memory. If we stride through it in u16 chunks, we end
// up fetching each location *twice*, because the native width of the bus is a u32
// 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.
for (&u32src, u8dest) in fb.iter().zip(frame.chunks_mut(2)) {
// note that GC2145 puts Y in top bytes of u16s, OV2640 puts them in the lower bytses of
// u16's.
u8dest[0] = ((u32src >> 8) & 0xff) as u8;
u8dest[1] = ((u32src >> 24) & 0xff) as u8;
}
frames += 1;
}
}
}
// Board bring-up: send characters to confirm the UART is configured & ready to go for the logging crate!
@ -496,327 +845,10 @@ pub fn early_init() -> u32 {
crate::platform::cramium::usb::test_usb();
}
}
}
//------------- test OV2640 ------------
// setup camera pins
let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_ov2640_pins(&iox);
// disable camera powerdown
iox.set_gpio_pin(cam_pdwn_bnk, cam_pdwn_pin, cramium_hal::iox::IoxValue::Low);
udma_global.clock_on(PeriphId::Cam);
let cam_ifram = unsafe {
cramium_hal::ifram::IframRange::from_raw_parts(
cramium_hal::board::CAM_IFRAM_ADDR,
cramium_hal::board::CAM_IFRAM_ADDR,
cramium_hal::board::CAM_IFRAM_LEN_PAGES * 4096,
)
};
// 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) };
cam.delay(100);
let (pid, mid) = cam.read_id(&mut i2c);
crate::println!("Camera pid {:x}, mid {:x}", pid, mid);
cam.init(&mut i2c, cramium_hal::ov2640::Resolution::Res320x240);
cam.poke(&mut i2c, 0xFF, 0x00);
cam.poke(&mut i2c, 0xDA, 0x01); // YUV LE
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_HEIGHT: usize = 240;
let border = (cols - QR_WIDTH) / 2;
cam.set_slicing((border, 0), (cols - border, QR_HEIGHT));
crate::println!("320x240 resolution setup with 256x240 slicing");
let mut csr_tt = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
csr_tt.wfo(utra::ticktimer::CLOCKS_PER_TICK_CLOCKS_PER_TICK, 200_000);
csr_tt.wfo(utra::ticktimer::CONTROL_RESET, 1);
let mut frames = 0;
let mut frame = [0u8; QR_WIDTH * QR_HEIGHT];
while iox_loop.get_gpio_pin(IoxPort::PB, 9) == IoxValue::High {}
udma_uart.setup_async_read();
let mut c = 0u8;
const BW_THRESH: u8 = 128;
#[cfg(feature = "qr")]
loop {
// toggling this off can improve performance by wasting less time "waiting" for the next frame...
// however, you will get "frame rolling" if the capture isn't initiated at exactly the right time.
// things could be improved by making 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 {}
cam.capture_async();
// blit fb to sh1107
for (y, row) in frame.chunks(QR_WIDTH).enumerate() {
if y & 1 == 0 {
for (x, &pixval) in row.iter().enumerate() {
if x & 1 == 0 {
if x < sh1107.dimensions().x as usize * 2
&& y < sh1107.dimensions().y as usize * 2
- (gfx::CHAR_HEIGHT as usize + 1) * 2
{
let luminance = pixval & 0xff;
if luminance > BW_THRESH {
// flip on y to adjust for sensor orientation. Lower left is (0, 0)
// on the display.
sh1107.put_pixel(
Point::new(
x as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2),
),
Mono::White.into(),
);
}
} else {
break;
}
}
}
}
}
let mut candidates: [Option<Point>; 64] = [None; 64];
crate::println!("\n\r------------- SEARCH -----------");
let finder_width = qr::find_finders(&mut candidates, &frame, BW_THRESH, QR_WIDTH) as isize;
const CROSSHAIR_LEN: isize = 3;
let mut candidates_found = 0;
let mut candidate3 = [Point::new(0, 0); 3];
for candidate in candidates.iter() {
if let Some(c) = candidate {
if candidates_found < candidate3.len() {
candidate3[candidates_found] = *c;
}
candidates_found += 1;
crate::println!("****** candidate: {}, {} ******", c.x, c.y);
// remap image to screen coordinates (it's 2:1)
let mut c_screen = *c / 2;
// flip coordinates to match the camera data
c_screen = Point::new(c_screen.x, sh1107.dimensions().y - 1 - c_screen.y);
draw_crosshair(&mut sh1107, c_screen);
}
}
use crate::platform::cramium::qr::*;
if candidates_found == 3 {
let maybe_qr_corners = QrCorners::from_finders(
&candidate3,
Point::new(QR_WIDTH as isize, QR_HEIGHT as isize),
// add a search margin on the finder width
(finder_width
+ (crate::platform::cramium::qr::FINDER_SEARCH_MARGIN * finder_width)
/ (1 + 1 + 3 + 1 + 1)) as usize,
);
// just doing this to avoid nesting another if level out in the test code; make this better!
if maybe_qr_corners.is_none() {
continue;
}
let mut qr_corners = maybe_qr_corners.unwrap();
let dims = Point::new(QR_WIDTH as isize, QR_HEIGHT as isize);
let mut il = ImageRoi::new(&mut frame, dims, BW_THRESH);
let (src, dst) = qr_corners.mapping(&mut il, crate::platform::cramium::qr::HOMOGRAPHY_MARGIN);
for s in src.iter() {
if let Some(p) = s {
crate::println!("src {:?}", p);
draw_crosshair(&mut sh1107, *p / 2);
}
}
for d in dst.iter() {
if let Some(p) = d {
crate::println!("dst {:?}", p);
draw_crosshair(&mut sh1107, *p / 2);
}
}
let mut src_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
let mut dst_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
let mut all_found = true;
for (s, s_f32) in src.iter().zip(src_f.iter_mut()) {
if let Some(p) = s {
*s_f32 = p.to_f32();
} else {
all_found = false;
}
}
for (d, d_f32) in dst.iter().zip(dst_f.iter_mut()) {
if let Some(p) = d {
*d_f32 = p.to_f32();
} else {
all_found = false;
}
}
if all_found {
use crate::platform::cramium::homography::*;
if let Some(h) = find_homography(src_f, dst_f) {
if let Some(h_inv) = h.try_inverse() {
// crate::println!("{:?}", h_inv);
let h_inv_fp = matrix3_to_fixp(h_inv);
// crate::println!("{:?}", h_inv_fp);
// apply homography to generate a new buffer for processing
let mut aligned = [0u8; QR_WIDTH * QR_HEIGHT];
// iterate through pixels and apply homography
for y in 0..dims.y {
for x in 0..dims.x {
let (x_src, y_src) =
apply_fixp_homography(&h_inv_fp, (x as i32, y as i32));
if (x_src as i32 >= 0)
&& ((x_src as i32) < dims.x as i32)
&& (y_src as i32 >= 0)
&& ((y_src as i32) < dims.y as i32)
{
// println!("{},{} -> {},{}", x_src as i32, y_src as i32, x, y);
aligned[QR_WIDTH * y as usize + x as usize] =
frame[QR_WIDTH * y_src as usize + x_src as usize];
} else {
aligned[QR_WIDTH * y as usize + x as usize] = 255;
}
}
}
// blit aligned to sh1107
for (y, row) in aligned.chunks(QR_WIDTH).enumerate() {
if y & 1 == 0 {
for (x, &pixval) in row.iter().enumerate() {
if x & 1 == 0 {
if x < sh1107.dimensions().x as usize * 2
&& y < sh1107.dimensions().y as usize * 2
- (gfx::CHAR_HEIGHT as usize + 1) * 2
{
let luminance = pixval & 0xff;
if luminance > BW_THRESH {
// flip on y to adjust for sensor orientation. Lower left
// is (0, 0)
// on the display.
sh1107.put_pixel(
Point::new(
x as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2),
),
Mono::White.into(),
);
} else {
sh1107.put_pixel(
Point::new(
x as isize / 2,
(sh1107.dimensions().y - 1) - (y as isize / 2),
),
Mono::Black.into(),
);
}
} else {
break;
}
}
}
}
}
gfx::msg(
&mut sh1107,
"Aligning...",
Point::new(0, 0),
Mono::White.into(),
Mono::Black.into(),
);
if udma_uart.read_async(&mut c) != 0 {
if c == ' ' as u32 as u8 {
crate::println!("Dumping...");
crate::println!("frame {}", frames);
udma_uart.write(
"------------------ divider ------------------\n\r".as_bytes(),
);
let hex_to_ascii = [
'0' as u32 as u8,
'1' as u32 as u8,
'2' as u32 as u8,
'3' as u32 as u8,
'4' as u32 as u8,
'5' as u32 as u8,
'6' as u32 as u8,
'7' as u32 as u8,
'8' as u32 as u8,
'9' as u32 as u8,
'A' as u32 as u8,
'B' as u32 as u8,
'C' as u32 as u8,
'D' as u32 as u8,
'E' as u32 as u8,
'F' as u32 as u8,
];
for line in frame.chunks(32) {
let mut output = [0u8; 3 * 32 + 3];
for (i, &b) in line.iter().enumerate() {
output[i * 3 + 0] = hex_to_ascii[(b >> 4) as usize];
output[i * 3 + 1] = hex_to_ascii[(b & 0xF) as usize];
output[i * 3 + 2] = ',' as u32 as u8;
}
output[3 * 32] = '\\' as u32 as u8;
output[3 * 32 + 1] = '\n' as u32 as u8;
output[3 * 32 + 2] = '\r' as u32 as u8;
udma_uart.write(&output);
}
// continue with boot
break;
}
}
}
}
}
} else {
gfx::msg(
&mut sh1107,
"Searching...",
Point::new(0, 0),
Mono::White.into(),
Mono::Black.into(),
);
}
// crate::println!("frame {}", frames);
sh1107.buffer_swap();
sh1107.draw();
// clear the front buffer
sh1107.clear();
// inspect camera values
if false {
crate::println!("RX_SADDR {:x}", cam.csr().r(utra::udma_camera::REG_RX_SADDR));
crate::println!("RX_SIZE {:x}", cam.csr().r(utra::udma_camera::REG_RX_SIZE));
crate::println!("RX_CFG {:x}", cam.csr().r(utra::udma_camera::REG_RX_CFG));
crate::println!("CAM_GLOB {:x}", cam.csr().r(utra::udma_camera::REG_CAM_CFG_GLOB));
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_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_SYNC {:x}", cam.csr().r(utra::udma_camera::REG_CAM_VSYNC_POLARITY));
}
// wait for the transfer to finish
cam.capture_await(false);
let fb: &[u32] = cam.rx_buf();
// fb is non-cacheable, slow memory. If we stride through it in u16 chunks, we end
// up fetching each location *twice*, because the native width of the bus is a u32
// 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.
for (&u32src, u8dest) in fb.iter().zip(frame.chunks_mut(2)) {
u8dest[0] = (u32src & 0xff) as u8;
u8dest[1] = ((u32src >> 16) & 0xff) as u8;
}
frames += 1;
}
#[cfg(feature = "board-bringup")]
{
// ---------------- test TRNG -------------------
// configure the SCE clocks to enable the TRNG
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::ops::{BitXor, Not};
use cramium_hal::minigfx::{ColorNative, FrameBuffer, Point};
use ux_api::minigfx::{ColorNative, FrameBuffer, Point};
use super::gfx;
@ -30,7 +30,7 @@ pub const HOMOGRAPHY_MARGIN: isize = -4;
pub const CROSSHAIR_LEN: isize = 3;
pub fn draw_crosshair(image: &mut dyn FrameBuffer, p: Point) {
use cramium_hal::minigfx::Line;
use ux_api::minigfx::Line;
gfx::line(
image,
Line::new(p + Point::new(0, CROSSHAIR_LEN), p - Point::new(0, CROSSHAIR_LEN)),

View file

@ -23,7 +23,7 @@ use cram_hal_service::{I2c, UdmaGlobal};
use cramium_api::*;
#[cfg(feature = "hosted-baosec")]
use cramium_emu::{
camera::Ov2640,
camera::Gc2145,
display::{MainThreadToken, Mono, Oled128x128, claim_main_thread},
i2c::I2c,
udma::UdmaGlobal,
@ -32,7 +32,7 @@ use cramium_emu::{
// - For GC0308 drivers, look in code/esp32-camera for sample code/constants
#[cfg(feature = "board-baosec")]
use cramium_hal::{
ov2640::Ov2640,
gc2145::Gc2145,
sh1107::{MainThreadToken, Mono, Oled128x128, claim_main_thread},
};
#[cfg(feature = "board-baosec")]
@ -64,7 +64,6 @@ use xous_ipc::Buffer;
pub const IMAGE_WIDTH: usize = 256;
pub const IMAGE_HEIGHT: usize = 240;
pub const BW_THRESH: u8 = 128;
// Next steps for performance improvement:
//
@ -77,25 +76,29 @@ pub const BW_THRESH: u8 = 128;
/// This converts a frame of `[u8]` grayscale pixels that may be larger than the native
/// frame buffer resolution into a black and white bitmap.
pub fn blit_to_display(display: &mut Oled128x128, frame: &[u8], display_cleared: bool) {
pub fn blit_to_display(display: &mut Oled128x128, frame: &[u8], display_cleared: bool, bw_thresh: &mut u8) {
let mut sum: u32 = 0;
let mut count: u32 = 0;
for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() {
if y & 1 == 0 {
// skip every other line
for (x, &pixval) in row.iter().enumerate() {
if x & 1 == 0 {
// skip every other pixel
if x < display.dimensions().x as usize * 2
&& y < display.dimensions().y as usize * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2
if y < display.dimensions().x as usize * 2
&& x < display.dimensions().y as usize * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2
{
let luminance = pixval & 0xff;
if luminance > BW_THRESH {
display.put_pixel(Point::new(x as isize / 2, y as isize / 2), Mono::White.into());
sum += luminance as u32;
count += 1;
if luminance > *bw_thresh {
display.put_pixel(Point::new(y as isize / 2, x as isize / 2), Mono::White.into());
} else {
// optimization to avoid some computation if we're blitting to an already-black
// buffer
if !display_cleared {
display.put_pixel(
Point::new(x as isize / 2, y as isize / 2),
Point::new(y as isize / 2, x as isize / 2),
Mono::Black.into(),
);
}
@ -107,6 +110,7 @@ pub fn blit_to_display(display: &mut Oled128x128, frame: &[u8], display_cleared:
}
}
}
*bw_thresh = (sum / count) as u8;
}
#[repr(align(32))]
@ -235,21 +239,19 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
#[cfg(not(feature = "hosted-baosec"))]
{
// 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
iox.set_gpio_pin_value(cam_pdwn_bnk, cam_pdwn_pin, IoxValue::Low);
}
udma_global.udma_clock_config(PeriphId::Cam, true);
// this is safe because we turned on the clocks before calling it
let mut cam = unsafe { Ov2640::new().expect("couldn't allocate camera") };
let mut cam = unsafe { Gc2145::new().expect("couldn't allocate camera") };
tt.sleep_ms(100).ok();
let (pid, mid) = cam.read_id(&mut i2c);
log::info!("Camera pid {:x}, mid {:x}", pid, mid);
cam.init(&mut i2c, cramium_api::camera::Resolution::Res320x240);
cam.poke(&mut i2c, 0xFF, 0x00);
cam.poke(&mut i2c, 0xDA, 0x01); // YUV LE
tt.sleep_ms(1).ok();
let (cols, _rows) = cam.resolution();
@ -312,6 +314,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let mut msg_opt = None;
#[cfg(feature = "gfx-testing")]
testing::tests();
let mut bw_thresh: u8 = 128;
loop {
if !is_panic.load(Ordering::Relaxed) {
xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();
@ -339,9 +342,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
for (y_src, line) in fb.chunks(IMAGE_WIDTH / 2).enumerate() {
for (x_src, &u32src) in line.iter().enumerate() {
frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src] =
(u32src & 0xff) as u8;
((u32src >> 8) & 0xff) as u8;
frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src + 1] =
((u32src >> 16) & 0xff) as u8;
((u32src >> 24) & 0xff) as u8;
}
}
frames += 1;
@ -350,7 +353,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
decode_success = false;
log::info!("------------- SEARCH {} -----------", frames);
let finder_width =
qr::find_finders(&mut candidates, &frame, BW_THRESH, IMAGE_WIDTH) as isize;
qr::find_finders(&mut candidates, &frame, bw_thresh, IMAGE_WIDTH) as isize;
if candidates.len() == 3 {
let candidates_orig = candidates.clone();
let mut x_candidates: [Point; 3] = [Point::new(0, 0); 3];
@ -365,7 +368,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
as usize,
) {
let dims = Point::new(IMAGE_WIDTH as isize, IMAGE_HEIGHT as isize);
let mut il = qr::ImageRoi::new(&mut frame, dims, BW_THRESH);
let mut il = qr::ImageRoi::new(&mut frame, dims, bw_thresh);
let (src, dst) = qr_corners.mapping(&mut il, qr::HOMOGRAPHY_MARGIN);
let mut src_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
let mut dst_f: [(f32, f32); 4] = [(0.0, 0.0); 4];
@ -450,7 +453,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
*dst = src;
}
}
blit_to_display(&mut display, &frame, true);
blit_to_display(&mut display, &frame, true, &mut bw_thresh);
// we now have a QR code in "canonical" orientation, with a
// known width in pixels
@ -461,7 +464,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
// Confirm that the finders coordinates are valid
let mut checked_candidates = Vec::<Point>::new();
let x_finder_width =
qr::find_finders(&mut checked_candidates, &aligned, BW_THRESH, qr_width as _)
qr::find_finders(&mut checked_candidates, &aligned, bw_thresh, qr_width as _)
as isize;
log::info!("x_finder width: {}", x_finder_width);
@ -494,13 +497,14 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let qr = qr::ImageRoi::new(
&mut aligned,
Point::new(qr_width as _, qr_width as _),
BW_THRESH,
bw_thresh,
);
let grid = modules::stream_to_grid(
&qr,
qr_width,
modules,
qr::HOMOGRAPHY_MARGIN.abs() as usize,
bw_thresh,
);
println!("grid len {}", grid.len());
@ -560,7 +564,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
);
}
} else {
blit_to_display(&mut display, &frame, true);
blit_to_display(&mut display, &frame, true, &mut bw_thresh);
for c in candidates_orig.iter() {
log::debug!("****** candidate: {}, {} ******", c.x, c.y);
// remap image to screen coordinates (it's 2:1)
@ -579,7 +583,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
}
} else {
// blit raw camera fb to display
blit_to_display(&mut display, &frame, true);
blit_to_display(&mut display, &frame, true, &mut bw_thresh);
gfx::msg(
&mut display,
"Scan QR code...",

View file

@ -107,6 +107,7 @@ pub fn stream_to_grid(
qr_size_pixels: usize,
qr_size_modules: usize,
margin: usize,
bw_thresh: u8,
) -> Vec<bool> {
let pix_per_module = ((qr_size_pixels - margin * 2) << FIXED_POINT_SHIFT) / qr_size_modules;
let mut x_frac = FracIter::new(
@ -128,9 +129,7 @@ pub fn stream_to_grid(
while let Some(x) = x_frac.next() {
if true {
// seems to work better with the simple threshold?
if image.data[(y + margin - FUDGE_Y) * image.width + (x + margin + FUDGE_X)]
< crate::BW_THRESH
{
if image.data[(y + margin - FUDGE_Y) * image.width + (x + margin + FUDGE_X)] < bw_thresh {
grid.push(true);
} else {
grid.push(false);
@ -139,7 +138,7 @@ pub fn stream_to_grid(
if image
.neighbor_luma(Point::new(x as isize + margin as isize, y as isize + margin as isize))
.unwrap()
< crate::BW_THRESH
< bw_thresh
{
grid.push(true);
} else {

View file

@ -1,3 +1,4 @@
#![allow(dead_code)]
use cramium_api::camera;
use cramium_api::*;
@ -83,3 +84,83 @@ impl Ov2640 {
std::thread::sleep(std::time::Duration::from_millis(quantum as u64));
}
}
#[repr(C)]
pub struct Gc2145 {
frame: [u16; EMU_COLS * EMU_ROWS],
}
impl Gc2145 {
/// Safety: clocks must be turned on before this is called
pub unsafe fn new() -> Result<Self, xous::Error> { Ok(Self { frame: [0u16; EMU_COLS * EMU_ROWS] }) }
pub fn release_ifram(&mut self) {}
pub fn claim_ifram(&mut self) -> Result<(), xous::Error> { Ok(()) }
pub fn has_ifram(&self) -> bool { false }
pub fn poke(&self, _i2c: &mut dyn I2cApi, _adr: u8, _dat: u8) {}
// chip does not support sequential reads
pub fn peek(&self, _i2c: &mut dyn I2cApi, _adr: u8, _dat: &mut [u8]) {}
pub fn init(&mut self, _i2c: &mut dyn I2cApi, _resolution: camera::Resolution) {}
/// Safety: definitely not safe, but only used in emulation mode so I'm not going to worry about it too
/// much.
pub fn rx_buf<T: UdmaWidths>(&self) -> &[T] {
unsafe {
core::slice::from_raw_parts(
self.frame.as_ptr() as *const T,
self.frame.len() / core::mem::size_of::<T>(),
)
}
}
/// Safety: definitely not safe, but only used in emulation mode so I'm not going to worry about it too
/// much.
pub unsafe fn rx_buf_phys<T: UdmaWidths>(&self) -> &[T] {
unsafe {
core::slice::from_raw_parts(
self.frame.as_ptr() as *const T,
self.frame.len() / core::mem::size_of::<T>(),
)
}
}
pub fn capture_async(&mut self) {}
pub fn capture_await(&mut self, _use_yield: bool) {}
/// For emulation, we fix the resolution at 320x240
pub fn resolution(&self) -> (usize, usize) { (EMU_COLS, EMU_ROWS) }
pub fn set_slicing(&mut self, _ll: (usize, usize), _ur: (usize, usize)) {}
pub fn disable_slicing(&mut self) {}
// Commented out to avoid leaking the ColorMode type
// pub fn color_mode(&mut self, _i2c: &mut dyn I2cApi, _mode: ColorMode) {}
// Commented out to avoid leaking the Contrast & Brightness types
/*
pub fn contrast_brightness(
&mut self,
_i2c: &mut dyn I2cApi,
_contrast: Contrast,
_brightness: Brightness,
) {
}
*/
// Commented out to avoid leaking the Effect types
// pub fn effect(&mut self, _i2c: &mut dyn I2cApi, _effect: Effect) {}
/// Returns (product ID, manufacturer ID) - hard coded to match GC2145 values
pub fn read_id(&self, _i2c: &mut dyn I2cApi) -> (u16, u16) { (0x2155, 0x0078) }
pub fn delay(&self, quantum: usize) {
std::thread::sleep(std::time::Duration::from_millis(quantum as u64));
}
}

View file

@ -634,6 +634,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
builder.add_loader_feature("sram-margin");
builder.add_loader_feature("usb");
builder.add_loader_feature("updates");
// builder.add_loader_feature("cam-test");
match task.as_deref() {
Some("baosec") => builder.target_cramium_soc(),
_ => panic!("should be unreachable"),