boot1 improvements

- add boardtype setting
- work around usb serial tx timeout hangs - not sure why they happen,
but let's not hang the repl on them.
- various cleanup code & comments
- note boot frequency
This commit is contained in:
bunnie 2025-10-01 02:48:36 +08:00
parent b0378d346c
commit b85ed18cd8
4 changed files with 81 additions and 49 deletions

View file

@ -10,7 +10,10 @@ mod secboot;
mod uf2;
use alloc::collections::VecDeque;
use core::{cell::RefCell, sync::atomic::Ordering};
use core::{
cell::RefCell,
sync::atomic::{AtomicBool, Ordering},
};
use bao1x_api::{BoardTypeCoding, BootWaitCoding};
use bao1x_hal::{board::KeyPress, iox::Iox, usb::driver::UsbDeviceState};
@ -27,7 +30,8 @@ static UART_RX: Mutex<RefCell<VecDeque<u8>>> = Mutex::new(RefCell::new(VecDeque:
#[allow(dead_code)]
static USB_RX: Mutex<RefCell<VecDeque<u8>>> = Mutex::new(RefCell::new(VecDeque::new()));
static USB_TX: Mutex<RefCell<VecDeque<u8>>> = Mutex::new(RefCell::new(VecDeque::new()));
static USB_CONNECTED: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false);
static USB_CONNECTED: AtomicBool = AtomicBool::new(false);
static DISK_BUSY: AtomicBool = AtomicBool::new(false);
pub fn uart_irq_handler() {
use crate::debug::SerialRead;
@ -59,6 +63,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
crate::println_d!("TX_IDLE: {:?}", crate::platform::usb::TX_IDLE.load(Ordering::SeqCst));
board_type = crate::platform::early_init(board_type);
crate::println!("\n~~Boot1 up!~~\n");
crate::println!("Configured board type: {:?}", board_type);
let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
@ -163,7 +168,9 @@ pub unsafe extern "C" fn rust_entry() -> ! {
}
_ => (),
};
glue::flush_tx();
if !DISK_BUSY.load(Ordering::SeqCst) {
glue::flush_tx();
}
} else {
// Handle keyboard events.
critical_section::with(|cs| {

View file

@ -224,7 +224,7 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> bao1x_api::Boar
enable_irq(utra::irqarray5::IRQARRAY5_IRQ);
crate::debug::USE_CONSOLE.store(true, core::sync::atomic::Ordering::SeqCst);
crate::println!("boot1 udma console up!");
crate::println!("boot1 udma console up, CPU @ {}MHz!", fclk_freq / 2_000_000);
board_type
}
@ -275,7 +275,6 @@ pub fn delay(ms: usize) {
let mut timer = utralib::CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32);
timer.wfo(utra::timer0::EV_PENDING_ZERO, 1);
for _ in 0..ms {
// comment this out for testing on MPW
while timer.rf(utra::timer0::EV_PENDING_ZERO) == 0 {}
timer.wfo(utra::timer0::EV_PENDING_ZERO, 1);
}

View file

@ -185,47 +185,41 @@ pub fn usb_ep1_bulk_out_complete(
if let Some((write_offset, len)) = this.callback_wr.take() {
let app_buf = conjure_app_buf();
let disk = conjure_disk();
if false {
// update the received data to the disk
// process the received data as if it were a uf2 sector
for (i, chunk) in app_buf[..len].chunks(512).enumerate() {
let (new_block, uf2_data) = critical_section::with(|cs| {
super::glue::SECTOR
.borrow(cs)
.borrow_mut()
.extend_from_slice(write_offset + i * 512, chunk)
});
// program the flash if a valid u2f block was found
if let Some(record) = uf2_data {
if record.address() as usize >= bao1x_api::BAREMETAL_START
&& (record.address() as usize) < utralib::HW_RERAM_MEM + bao1x_api::RRAM_STORAGE_LEN
&& record.family() == bao1x_api::BAOCHIP_1X_UF2_FAMILY
{
let mut rram = bao1x_hal::rram::Reram::new();
let offset = record.address() as usize - utralib::HW_RERAM_MEM;
rram.write_slice(offset, record.data());
crate::println_d!(
"Wrote {} to 0x{:x}: {:x?}",
record.data().len(),
record.address(),
&record.data()[..8]
);
} else {
crate::println_d!("Invalid write address {:x}, block ignored!", record.address());
}
}
// replace the tracking block with the new one, if a new one was provided
if let Some(block) = new_block {
critical_section::with(|cs| super::glue::SECTOR.borrow(cs).replace(block));
}
}
if write_offset + len < disk.len() {
// update the received data to the disk if it fits within the allocated region
disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]);
} else {
// process the received data as if it were a uf2 sector
for (i, chunk) in app_buf[..len].chunks(512).enumerate() {
let (new_block, uf2_data) = critical_section::with(|cs| {
super::glue::SECTOR
.borrow(cs)
.borrow_mut()
.extend_from_slice(write_offset + i * 512, chunk)
});
// program the flash if a valid u2f block was found
if let Some(record) = uf2_data {
if record.address() as usize >= bao1x_api::BAREMETAL_START
&& (record.address() as usize)
< utralib::HW_RERAM_MEM + bao1x_api::RRAM_STORAGE_LEN
&& record.family() == bao1x_api::BAOCHIP_1X_UF2_FAMILY
{
let mut rram = bao1x_hal::rram::Reram::new();
let offset = record.address() as usize - utralib::HW_RERAM_MEM;
rram.write_slice(offset, record.data());
crate::println_d!(
"Wrote {} to 0x{:x}: {:x?}",
record.data().len(),
record.address(),
&record.data()[..8]
);
} else {
crate::println_d!("Invalid write address {:x}, block ignored!", record.address());
}
}
// replace the tracking block with the new one, if a new one was provided
if let Some(block) = new_block {
critical_section::with(|cs| super::glue::SECTOR.borrow(cs).replace(block));
}
}
if write_offset + len < disk.len() {
// update the received data to the disk if it fits within the allocated region
disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]);
}
}
if let Some((offset, remaining_len)) = this.remaining_wr.take() {
this.setup_big_write(app_buf_addr(), app_buf_len(), offset, remaining_len);
@ -235,6 +229,7 @@ pub fn usb_ep1_bulk_out_complete(
csw.residue = 0;
csw.status = 0;
csw.send(this);
crate::DISK_BUSY.store(false, Ordering::SeqCst);
}
} else {
crate::println_d!("Data completion reached without destination for data copy! data dropped.");
@ -263,6 +258,7 @@ pub fn usb_ep1_bulk_in_complete(
} else {
this.bulk_xfer(1, USB_SEND, CSW_ADDR, 13, 0, 0);
this.ms_state = UmsState::StatusPhase;
crate::DISK_BUSY.store(false, Ordering::SeqCst);
}
} else if UmsState::StatusPhase == this.ms_state && length == 13 {
//CSW
@ -315,10 +311,17 @@ pub fn flush() {
}
pub fn flush_tx(this: &mut CorigineUsb) {
const TIMEOUT: usize = 100_000;
let mut written = 0;
while !crate::platform::usb::TX_IDLE.swap(false, core::sync::atomic::Ordering::SeqCst) {
// wait for tx to go idle
let mut timeout = 0;
while !crate::platform::usb::TX_IDLE.swap(false, core::sync::atomic::Ordering::SeqCst)
&& timeout < TIMEOUT
{
timeout += 1;
if timeout % 20_000 == 0 {
crate::println!("txw {}", timeout);
}
}
let tx_buf = this.cdc_acm_tx_slice();
@ -581,6 +584,7 @@ fn process_unsupported_command(this: &mut CorigineUsb, cbw: Cbw) {
}
fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) {
crate::DISK_BUSY.store(true, Ordering::SeqCst);
let mut csw = Csw::derive();
let mut lba;
let mut length;
@ -605,7 +609,7 @@ fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) {
return;
}
crate::println_d!(
"DISK READ address = 0x{:x}, length = 0x{:x}",
"DISK READ @ 0x{:x} .. 0x{:x}",
RAMDISK_ADDRESS + lba as usize * SECTOR_SIZE as usize,
length
);
@ -639,6 +643,7 @@ fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) {
}
fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) {
crate::DISK_BUSY.store(true, Ordering::SeqCst);
let mut csw = Csw::derive();
let mut lba: u32;
@ -669,7 +674,7 @@ fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) {
}
crate::println_d!(
"Write address = 0x{:x}, length = 0x{:x}",
"DISK WRITE @ 0x{:x} .. 0x{:x}",
RAMDISK_ADDRESS + lba as usize * SECTOR_SIZE as usize,
length
);
@ -702,6 +707,7 @@ fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) {
}
fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) {
crate::DISK_BUSY.store(true, Ordering::SeqCst);
let mut csw = Csw::derive();
let mut lba;

View file

@ -148,6 +148,26 @@ impl Repl {
return Err(Error::help("bootwait [check | toggle]"));
}
}
"boardtype" => {
let one_way = OneWayCounter::new();
if args.len() != 1 {
return Err(Error::help("boardtype [dabao | baosec | oem]"));
}
let new_type = match args[0].as_str() {
"dabao" => bao1x_api::BoardTypeCoding::Dabao,
"baosec" => bao1x_api::BoardTypeCoding::Baosec,
"oem" => bao1x_api::BoardTypeCoding::Oem,
_ => return Err(Error::help("boardtype [dabao | baosec | oem]")),
};
let mut count = 0;
while one_way.get_decoded::<bao1x_api::BoardTypeCoding>().expect("owc coding error")
!= new_type
{
one_way.inc_coded::<bao1x_api::BoardTypeCoding>().expect("increment error");
count += 1;
}
crate::println!("Board type set to {:?} after {} increments", new_type, count);
}
#[cfg(feature = "unsafe-debug")]
"peek" => {
const COLUMNS: usize = 4;