#![no_std]
#![no_main]
use panic_halt as _;
use rp2040_hal as hal;
use hal::pac;
use core::fmt::Write;
use hal::fugit::RateExtU32;
use hal::Clock;
use hal::uart::{DataBits, StopBits, UartConfig};
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
const SYSTICK_RELOAD: u32 = 0x00FF_FFFF;
#[rp2040_hal::entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let mut core = pac::CorePeripherals::take().unwrap();
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
let clocks = hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.unwrap();
let sio = hal::Sio::new(pac.SIO);
let pins = hal::gpio::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let uart_pins = (
pins.gpio0.into_function::<hal::gpio::FunctionUart>(),
pins.gpio1.into_function::<hal::gpio::FunctionUart>(),
);
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
.enable(
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
clocks.peripheral_clock.freq(),
)
.unwrap();
writeln!(uart, "ROM Copyright: {}", hal::rom_data::copyright_string()).unwrap();
writeln!(
uart,
"ROM Git Revision: 0x{:x}",
hal::rom_data::git_revision()
)
.unwrap();
writeln!(
uart,
"popcount32(0xF000_0001) = {}",
hal::rom_data::popcount32(0xF000_0001)
)
.unwrap();
let x = hal::rom_data::popcount32(0xFF) as f32; let y = hal::rom_data::popcount32(0xFFF) as f32;
core.SYST.set_reload(SYSTICK_RELOAD);
core.SYST.clear_current();
core.SYST.enable_counter();
let start_soft = cortex_m::peripheral::SYST::get_current();
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
let soft_result = x * y;
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
let end_soft = cortex_m::peripheral::SYST::get_current();
writeln!(
uart,
"{} x {} = {} in {} systicks (doing soft-float maths)",
x,
y,
soft_result,
calc_delta(start_soft, end_soft)
)
.unwrap();
let fmul = hal::rom_data::float_funcs::fmul::ptr();
let start_rom = cortex_m::peripheral::SYST::get_current();
let rom_result = fmul(x, y);
let end_rom = cortex_m::peripheral::SYST::get_current();
writeln!(
uart,
"{} x {} = {} in {} systicks (using the ROM)",
x,
y,
rom_result,
calc_delta(start_rom, end_rom)
)
.unwrap();
for _ in 0..1_000_000 {
cortex_m::asm::nop();
}
rp2040_hal::rom_data::reset_to_usb_boot(0, 0);
loop {
cortex_m::asm::wfi();
}
}
fn calc_delta(start: u32, end: u32) -> u32 {
if start < end {
(start.wrapping_sub(end)) & SYSTICK_RELOAD
} else {
start - end
}
}