|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | +#![feature(type_alias_impl_trait)] |
| 4 | + |
| 5 | +#[rtic::app( |
| 6 | + device = rp_pico::hal::pac, |
| 7 | + dispatchers = [TIMER_IRQ_1] |
| 8 | +)] |
| 9 | +mod app { |
| 10 | + use rp_pico::hal::{ |
| 11 | + clocks, gpio, |
| 12 | + gpio::pin::bank0::{Gpio2, Gpio25, Gpio3}, |
| 13 | + gpio::pin::PushPullOutput, |
| 14 | + pac, |
| 15 | + sio::Sio, |
| 16 | + watchdog::Watchdog, |
| 17 | + I2C, |
| 18 | + }; |
| 19 | + use rp_pico::XOSC_CRYSTAL_FREQ; |
| 20 | + |
| 21 | + use core::mem::MaybeUninit; |
| 22 | + use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin}; |
| 23 | + use fugit::RateExtU32; |
| 24 | + use rtic_monotonics::systick::*; |
| 25 | + |
| 26 | + use panic_probe as _; |
| 27 | + |
| 28 | + rtic_monotonics::make_systick_handler!(); |
| 29 | + |
| 30 | + type I2CBus = I2C< |
| 31 | + pac::I2C1, |
| 32 | + ( |
| 33 | + gpio::Pin<Gpio2, gpio::FunctionI2C>, |
| 34 | + gpio::Pin<Gpio3, gpio::FunctionI2C>, |
| 35 | + ), |
| 36 | + >; |
| 37 | + |
| 38 | + #[shared] |
| 39 | + struct Shared {} |
| 40 | + |
| 41 | + #[local] |
| 42 | + struct Local { |
| 43 | + led: gpio::Pin<Gpio25, PushPullOutput>, |
| 44 | + i2c: &'static mut I2CBus, |
| 45 | + } |
| 46 | + |
| 47 | + #[init(local=[ |
| 48 | + // Task local initialized resources are static |
| 49 | + // Here we use MaybeUninit to allow for initialization in init() |
| 50 | + // This enables its usage in driver initialization |
| 51 | + i2c_ctx: MaybeUninit<I2CBus> = MaybeUninit::uninit() |
| 52 | + ])] |
| 53 | + fn init(mut ctx: init::Context) -> (Shared, Local) { |
| 54 | + // Configure the clocks, watchdog - The default is to generate a 125 MHz system clock |
| 55 | + Systick::start(ctx.core.SYST, 125_000_000); // default rp2040 clock-rate is 125MHz |
| 56 | + let mut watchdog = Watchdog::new(ctx.device.WATCHDOG); |
| 57 | + let clocks = clocks::init_clocks_and_plls( |
| 58 | + XOSC_CRYSTAL_FREQ, |
| 59 | + ctx.device.XOSC, |
| 60 | + ctx.device.CLOCKS, |
| 61 | + ctx.device.PLL_SYS, |
| 62 | + ctx.device.PLL_USB, |
| 63 | + &mut ctx.device.RESETS, |
| 64 | + &mut watchdog, |
| 65 | + ) |
| 66 | + .ok() |
| 67 | + .unwrap(); |
| 68 | + |
| 69 | + // Init LED pin |
| 70 | + let sio = Sio::new(ctx.device.SIO); |
| 71 | + let gpioa = rp_pico::Pins::new( |
| 72 | + ctx.device.IO_BANK0, |
| 73 | + ctx.device.PADS_BANK0, |
| 74 | + sio.gpio_bank0, |
| 75 | + &mut ctx.device.RESETS, |
| 76 | + ); |
| 77 | + let mut led = gpioa.led.into_push_pull_output(); |
| 78 | + led.set_low().unwrap(); |
| 79 | + |
| 80 | + // Init I2C pins |
| 81 | + let sda_pin = gpioa.gpio2.into_mode::<gpio::FunctionI2C>(); |
| 82 | + let scl_pin = gpioa.gpio3.into_mode::<gpio::FunctionI2C>(); |
| 83 | + |
| 84 | + // Init I2C itself, using MaybeUninit to overwrite the previously |
| 85 | + // uninitialized i2c_ctx variable without dropping its value |
| 86 | + // (i2c_ctx definined in init local resources above) |
| 87 | + let i2c_tmp: &'static mut _ = ctx.local.i2c_ctx.write(I2C::i2c1( |
| 88 | + ctx.device.I2C1, |
| 89 | + sda_pin, |
| 90 | + scl_pin, |
| 91 | + 100.kHz(), |
| 92 | + &mut ctx.device.RESETS, |
| 93 | + &clocks.system_clock, |
| 94 | + )); |
| 95 | + |
| 96 | + // Spawn heartbeat task |
| 97 | + heartbeat::spawn().ok(); |
| 98 | + |
| 99 | + // Return resources and timer |
| 100 | + (Shared {}, Local { led, i2c: i2c_tmp }) |
| 101 | + } |
| 102 | + |
| 103 | + #[task(local = [i2c, led])] |
| 104 | + async fn heartbeat(ctx: heartbeat::Context) { |
| 105 | + // Flicker the built-in LED |
| 106 | + _ = ctx.local.led.toggle(); |
| 107 | + |
| 108 | + // Congrats, you can use your i2c and have access to it here, |
| 109 | + // now to do something with it! |
| 110 | + |
| 111 | + // Re-spawn this task after 1 second |
| 112 | + Systick::delay(1000.millis()).await; |
| 113 | + } |
| 114 | +} |
0 commit comments