#![no_std] #![no_main] use cortex_m::prelude::_embedded_hal_PwmPin; use defmt::info; use defmt::{println, write}; use defmt_rtt as _; use panic_probe as _; // Alias for our HAL crate use rp2040_hal as hal; // A shorter alias for the Peripheral Access Crate, which provides low-level // register access use hal::fugit::RateExtU32; use hal::pac; // Some traits we need use embedded_hal::digital::OutputPin; use embedded_hal::digital::{InputPin, StatefulOutputPin}; use sh1106_pico_rs::graphics::GraphicsBuf; use sh1106_pico_rs::sh1106::SH1106Dev; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. /// Note: This boot block is not necessary when using a rp-hal based BSP /// as the BSPs already perform this step. #[unsafe(link_section = ".boot_loader")] #[used] pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H; /// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust /// if your board has a different frequency const XTAL_FREQ_HZ: u32 = 12_000_000u32; #[rp2040_hal::entry] fn main() -> ! { // Grab our singleton objects let mut pac = pac::Peripherals::take().unwrap(); let core = pac::CorePeripherals::take().unwrap(); // Set up the watchdog driver - needed by the clock setup code let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); // Configure the clocks 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 mut delay = cortex_m::delay::Delay::new(core.SYST, 133_000_000u32); // The single-cycle I/O block controls our GPIO pins let sio = hal::Sio::new(pac.SIO); // Set the pins to their default state let pins = hal::gpio::Pins::new( pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS, ); let mut adc = hal::Adc::new(pac.ADC, &mut pac.RESETS); let mut adc_pin = hal::adc::AdcPin::new(pins.gpio26.into_floating_input()).unwrap(); let mut adc_fifo = adc .build_fifo() .set_channel(&mut adc_pin) .clock_divider(47999, 0) .start_paused(); let mut led_pin = pins.gpio25.into_push_pull_output(); led_pin.set_high(); // Init PWMs let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS); // Configure PWM0 let pwm0 = &mut pwm_slices.pwm0; pwm0.set_ph_correct(); pwm0.set_div_frac(0); pwm0.enable(); pwm0.channel_a.output_to(pins.gpio16); let mut on_off_input = pins.gpio15.into_pull_up_input(); let mut tune_up_input = pins.gpio14.into_pull_up_input(); let mut tune_dn_input = pins.gpio13.into_pull_up_input(); let mut tick = 0u32; let mut last_input = 0u32; let mut broadcast_on = false; let mut current_tune = 108; const MAX_TUNE: u16 = 350u16; const MIN_TUNE: u16 = 4u16; pwm0.set_top(current_tune); pwm0.channel_a.set_duty(current_tune / 2); // Create the I²C drive let mut i2c = hal::I2C::i2c0( pac.I2C0, pins.gpio8.reconfigure(), pins.gpio9.reconfigure(), 400.kHz(), &mut pac.RESETS, &clocks.system_clock, ); let mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c); sh1106_dev.set_vertical_flip(&mut i2c, true); let sh1106_dev = sh1106_dev; let mut gfx_buf = GraphicsBuf::new(); let mut last_gfx_update = 0u32; // Infinite loop, fading LED up and down loop { tick = tick.saturating_add(1); if tick.wrapping_sub(last_gfx_update) > 1_000_000 { gfx_buf.clear(); gfx_buf.draw_string(20, 20, "Hello Radio!"); let tuned_freq = 125_000u32 / (current_tune * 2) as u32; let mut tune_str_buf = [0u8; 4]; u16_into_str(tuned_freq as u16, &mut tune_str_buf); let tune_str = str::from_utf8(&tune_str_buf).unwrap(); info!("{}, {}", tune_str, tune_str.len()); gfx_buf.draw_string(20, 34, tune_str); match broadcast_on { true => gfx_buf.draw_string(20, 48, "ON THE AIR"), false => (), } gfx_buf.redraw(); sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf); last_gfx_update = tick; } if tick.wrapping_sub(last_input) < 5_000_000 { continue; } if on_off_input.is_low().unwrap() { last_input = tick; broadcast_on = !broadcast_on; match broadcast_on { true => pwm0.enable(), false => pwm0.disable(), } continue; } if tune_up_input.is_low().unwrap() { last_input = tick; current_tune = MAX_TUNE.min(current_tune + 2); pwm0.set_top(current_tune); pwm0.channel_a.set_duty(current_tune / 2); continue; } if tune_dn_input.is_low().unwrap() { last_input = tick; current_tune = MIN_TUNE.max(current_tune - 2); pwm0.set_top(current_tune); pwm0.channel_a.set_duty(current_tune / 2); } } } fn u16_into_str(mut value: u16, str_buf: &mut [u8; 4]) { for i in 0..4 { str_buf[3 - i] = b'0' + (value % 10) as u8; value /= 10; } info!("utf8 buf {}", str_buf); }