diff --git a/bao1x-boot/boot1/Cargo.toml b/bao1x-boot/boot1/Cargo.toml index dcdb6337b..07fc522df 100644 --- a/bao1x-boot/boot1/Cargo.toml +++ b/bao1x-boot/boot1/Cargo.toml @@ -33,6 +33,8 @@ bao1x = [ "bao1x-hal/loader-baosec", # "bao1x-hal/debug-print-usb", "bao1x-api", + # needed for the boot logo + "ux-api/loader-baosec", ] unsafe-debug = [] verbose-debug = ["bao1x-hal/debug-print-usb", "bao1x-hal/verbose-debug"] diff --git a/bao1x-boot/boot1/src/font6x12_1bpp.raw b/bao1x-boot/boot1/src/font6x12_1bpp.raw new file mode 100644 index 000000000..3a25b9beb Binary files /dev/null and b/bao1x-boot/boot1/src/font6x12_1bpp.raw differ diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index 3e8fba0ac..bd8f3311b 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -17,10 +17,12 @@ use core::{ use bao1x_api::{BoardTypeCoding, BootWaitCoding}; use bao1x_hal::{board::KeyPress, iox::Iox, usb::driver::UsbDeviceState}; +use bao1x_hal::{sh1107::Oled128x128, udma::GlobalConfig}; use critical_section::Mutex; use platform::*; #[allow(unused_imports)] use utralib::*; +use ux_api::minigfx::{DrawStyle, FrameBuffer, Point, Rectangle}; use crate::delay; use crate::platform::usb::glue; @@ -61,7 +63,8 @@ pub unsafe extern "C" fn rust_entry() -> ! { one_way.get_decoded::().expect("Board type coding error"); crate::println_d!("TX_IDLE: {:?}", crate::platform::usb::TX_IDLE.load(Ordering::SeqCst)); - board_type = crate::platform::early_init(board_type); + let perclk: u32; + (board_type, perclk) = crate::platform::early_init(board_type); crate::println!("\n~~Boot1 up!~~\n"); crate::println!("Configured board type: {:?}", board_type); @@ -95,14 +98,41 @@ pub unsafe extern "C" fn rust_entry() -> ! { crate::println!("Boot bypassed with keypress: {:?}", current_key); } + // grab a handle to the OLED if it exists on the board. + let mut udma_global = GlobalConfig::new(); + let mut oled_iox = iox.clone(); + let mut oled = if board_type == BoardTypeCoding::Baosec { + Some(bao1x_hal::sh1107::Oled128x128::new( + bao1x_hal::sh1107::MainThreadToken::new(), + perclk, + &mut oled_iox, + &mut udma_global, + )) + } else { + None + }; + let (se0_port, se0_pin) = match board_type { BoardTypeCoding::Baosec => bao1x_hal::board::setup_usb_pins(&iox), _ => crate::platform::setup_dabao_se0_pin(&iox), }; iox.set_gpio_pin(se0_port, se0_pin, bao1x_api::IoxValue::Low); // put the USB port into SE0 - delay(500); + delay(100); + // use the USB disconnect time to initialize the display - at least 100ms is + // needed after reset for the display to initialize + if let Some(ref mut sh1107) = oled { + // show the boot logo + sh1107.init(); + sh1107.buffer_mut().fill(0xFFFF_FFFF); + sh1107.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP); + sh1107.draw(); + } + delay(400); + // setup the USB port let (mut last_usb_state, mut portsc) = glue::setup(); + + // remainder of the 1-second total wait target time for USB disconnect delay(500); // release SE0 iox.set_gpio_pin(se0_port, se0_pin, bao1x_api::IoxValue::High); @@ -117,6 +147,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { } // USB should have a solid shot of connecting now. crate::println!("USB device ready"); + if let Some(ref mut sh1107) = oled { + marquee(sh1107, "Update mode"); + } // provide some feedback on the run state of the BIO by peeking at the program counter // value, and provide feedback on the CPU operation by flashing the RGB LEDs. @@ -145,6 +178,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { crate::println!("USB is connected!"); last_usb_state = new_usb_state; USB_CONNECTED.store(true, core::sync::atomic::Ordering::SeqCst); + if let Some(ref mut sh1107) = oled { + marquee(sh1107, "Connected"); + } } } @@ -205,6 +241,41 @@ pub unsafe extern "C" fn rust_entry() -> ! { } } + if let Some(ref mut sh1107) = oled { + marquee(sh1107, "Booting..."); + } + // when we get to this point, there's only two options... crate::secboot::boot_or_die(); } + +fn marquee(sh1107: &mut Oled128x128, msg: &str) { + use bao1x_hal::sh1107::{COLUMN, ROW}; + use ux_api::bitmaps::baochip128x128::MARQUEE_BELOW; + + // blank out the marquee + ux_api::minigfx::op::rectangle( + sh1107, + Rectangle::new_with_style( + Point::new(0, MARQUEE_BELOW as isize), + Point::new(COLUMN, ROW), + DrawStyle::new(ux_api::minigfx::PixelColor::Dark, ux_api::minigfx::PixelColor::Dark, 1), + ), + None, + false, + ); + + // now try best-effort to fit the message. No word-wrapping here. + let msg_width = msg.len() as isize * crate::gfx::CHAR_WIDTH; + let x_pos = (COLUMN - msg_width) / 2; + let y_midline = MARQUEE_BELOW as isize + (ROW - MARQUEE_BELOW as isize) / 2; + let y_pos = y_midline - crate::gfx::CHAR_HEIGHT / 2; + gfx::msg( + sh1107, + msg, + Point::new(x_pos, y_pos), + bao1x_hal::sh1107::Mono::White.into(), + bao1x_hal::sh1107::Mono::Black.into(), + ); + sh1107.draw(); +} diff --git a/bao1x-boot/boot1/src/platform/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x.rs index c8d21455b..7cc53156b 100644 --- a/bao1x-boot/boot1/src/platform/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x.rs @@ -1,6 +1,7 @@ pub mod bao1x; pub use bao1x::*; pub mod debug; +pub mod gfx; pub mod irq; pub mod usb; diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index f16109df3..5dc084228 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -69,7 +69,7 @@ pub fn setup_dabao_se0_pin(iox: &T) -> (IoxPort, u8) { /// This can change the board type coding to a safer, simpler board type if the declared board type has /// problems booting. -pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> bao1x_api::BoardTypeCoding { +pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::BoardTypeCoding, u32) { let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // setup board-specific I/Os - early boot set. These are items that have to be @@ -172,6 +172,18 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> bao1x_api::Boar let (se0_port, se0_pin) = bao1x_hal::board::setup_usb_pins(&iox); iox.set_gpio_pin(se0_port, se0_pin, IoxValue::Low); // put the USB port into SE0 while we initialize things + // setup display - turn on its power, reset the framebuffer + bao1x_hal::board::setup_display_pins(&iox); + // power on + let (oled_on_port, oled_on_pin) = bao1x_hal::board::setup_oled_power_pin(&iox); + iox.set_gpio_pin_value(oled_on_port, oled_on_pin, IoxValue::High); + // reset enable + let (peri_rst_port, peri_reset_pin) = bao1x_hal::board::setup_periph_reset_pin(&iox); + iox.set_gpio_pin_value(peri_rst_port, peri_reset_pin, IoxValue::Low); + // delay for reset assert + delay(1); + iox.set_gpio_pin_value(peri_rst_port, peri_reset_pin, IoxValue::High); + // keyboard can setup at keyboard read time } } @@ -226,7 +238,7 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> bao1x_api::Boar crate::debug::USE_CONSOLE.store(true, core::sync::atomic::Ordering::SeqCst); crate::println!("boot1 udma console up, CPU @ {}MHz!", fclk_freq / 2_000_000); - board_type + (board_type, perclk) } pub fn setup_timer(sysclk_freq: u32) { diff --git a/bao1x-boot/boot1/src/platform/bao1x/gfx.rs b/bao1x-boot/boot1/src/platform/bao1x/gfx.rs new file mode 100644 index 000000000..9648e1609 --- /dev/null +++ b/bao1x-boot/boot1/src/platform/bao1x/gfx.rs @@ -0,0 +1,143 @@ +#![allow(dead_code)] +use bao1x_hal::sh1107::Mono; +use ux_api::minigfx::*; + +// The discipline for all the APIs in this module are that they act on a FrameBuffer which is +// passed to the function. This allows us to bind the drawing computation on the caller-side of +// of a shared frame buffer. + +// this font is from the embedded graphics crate https://docs.rs/embedded-graphics/0.7.1/embedded_graphics/ +const FONT_IMAGE: &'static [u8] = include_bytes!("../../font6x12_1bpp.raw"); +pub const CHAR_HEIGHT: isize = 12; +pub const CHAR_WIDTH: isize = 6; +const FONT_IMAGE_WIDTH: isize = 96; +const LEFT_MARGIN: isize = 10; + +fn char_offset(c: char) -> isize { + let fallback = ' ' as isize - ' ' as isize; + if c < ' ' { + return fallback; + } + if c <= '~' { + return c as isize - ' ' as isize; + } + fallback +} + +pub fn msg<'a>(fb: &mut dyn FrameBuffer, text: &'a str, ll_pos: Point, fg: ColorNative, bg: ColorNative) { + let mut ll_pos = ll_pos.clone(); + // this routine is adapted from the embedded graphics crate https://docs.rs/embedded-graphics/0.7.1/embedded_graphics/ + let char_per_row = FONT_IMAGE_WIDTH / CHAR_WIDTH; + let mut idx = 0; + let mut x_update = 0; + for current_char in text.chars() { + let mut char_walk_x = 0; + let mut char_walk_y = 0; + + loop { + // Char _code_ offset from first char, most often a space + // E.g. first char = ' ' (32), target char = '!' (33), offset = 33 - 32 = 1 + let char_offset = char_offset(current_char); + let row = char_offset / char_per_row; + + // Top left corner of character, in pixels + let char_x = (char_offset - (row * char_per_row)) * CHAR_WIDTH; + let char_y = row * CHAR_HEIGHT; + + // Bit index + // = X pixel offset for char + // + Character row offset (row 0 = 0, row 1 = (192 * 8) = 1536) + // + X offset for the pixel block that comprises this char + // + Y offset for pixel block + let bitmap_bit_index = + char_x + (FONT_IMAGE_WIDTH * char_y) + char_walk_x + (char_walk_y * FONT_IMAGE_WIDTH); + + let bitmap_byte = bitmap_bit_index / 8; + let bitmap_bit = 7 - (bitmap_bit_index % 8); + + let color = if FONT_IMAGE[bitmap_byte as usize] & (1 << bitmap_bit) != 0 { fg } else { bg }; + + let x = ll_pos.x + CHAR_WIDTH * idx + char_walk_x; + let y = ll_pos.y + char_walk_y; + + // draw color at x, y + if (current_char as u8 != 0xd) && (current_char as u8 != 0xa) { + // don't draw CRLF specials + fb.put_pixel(Point::new(x, y), color); + } + + char_walk_x += 1; + + if char_walk_x >= CHAR_WIDTH { + char_walk_x = 0; + char_walk_y += 1; + + // Done with this char, move on to the next one + if char_walk_y >= CHAR_HEIGHT { + if current_char as u8 == 0xd { + // '\n' + ll_pos.y += CHAR_HEIGHT; + } else if current_char as u8 == 0xa { + // '\r' + ll_pos.x = LEFT_MARGIN; + x_update = 0; + } else { + idx += 1; + x_update += CHAR_WIDTH; + } + + break; + } + } + } + } + ll_pos.x += x_update; +} + +pub fn line(fb: &mut dyn FrameBuffer, l: Line, clip: Option, xor: bool) { + let color: ColorNative; + if l.style.stroke_color.is_some() { + color = l.style.stroke_color.unwrap().into(); + } else { + return; + } + let mut x0 = l.start.x; + let mut y0 = l.start.y; + let x1 = l.end.x; + let y1 = l.end.y; + + let dx = (x1 - x0).abs(); + let sx = if x0 < x1 { 1 } else { -1 }; + let dy = -((y1 - y0).abs()); + let sy = if y0 < y1 { 1 } else { -1 }; + let mut err = dx + dy; // error value e_xy + loop { + if x0 >= 0 && y0 >= 0 && x0 < (fb.dimensions().x as _) && y0 < (fb.dimensions().y as _) { + if clip.is_none() || (clip.unwrap().intersects_point(Point::new(x0, y0))) { + if !xor { + fb.put_pixel(Point::new(x0 as _, y0 as _), color); + } else { + if let Some(existing) = fb.get_pixel(Point::new(x0 as _, y0 as _)) { + if existing == Mono::Black.into() { + fb.put_pixel(Point::new(x0 as _, y0 as _), Mono::White.into()); + } else { + fb.put_pixel(Point::new(x0 as _, y0 as _), Mono::Black.into()); + } + } + } + } + } + if x0 == x1 && y0 == y1 { + break; + } + let e2 = 2 * err; + if e2 >= dy { + err += dy; + x0 += sx; + } + if e2 <= dx { + err += dx; + y0 += sy; + } + } +}