|
| 1 | +//! # Seeeduino XIAO RP2040 Blinky Example |
| 2 | +//! |
| 3 | +//! Blinks the LED on a Seeeduino XIAO RP2040 16MB board. |
| 4 | +//! |
| 5 | +//! This will blink an LED attached to GPIO25, which is the pin the XIAO RP2040 |
| 6 | +//! uses for the on-board LED. |
| 7 | +//! |
| 8 | +//! See the `Cargo.toml` file for Copyright and license details. |
| 9 | +
|
| 10 | +#![no_std] |
| 11 | +#![no_main] |
| 12 | + |
| 13 | +// The macro for our start-up function |
| 14 | +use seeeduino_xiao_rp2040::entry; |
| 15 | + |
| 16 | +// GPIO traits |
| 17 | +use embedded_hal::digital::v2::OutputPin; |
| 18 | +use embedded_hal::PwmPin; |
| 19 | + |
| 20 | +// Time handling traits |
| 21 | +use embedded_time::rate::*; |
| 22 | + |
| 23 | +// Ensure we halt the program on panic (if we don't mention this crate it won't |
| 24 | +// be linked) |
| 25 | +use panic_halt as _; |
| 26 | + |
| 27 | +// Pull in any important traits |
| 28 | +use seeeduino_xiao_rp2040::hal::prelude::*; |
| 29 | + |
| 30 | +// A shorter alias for the Peripheral Access Crate, which provides low-level |
| 31 | +// register access |
| 32 | +use seeeduino_xiao_rp2040::hal::pac; |
| 33 | + |
| 34 | +// A shorter alias for the Hardware Abstraction Layer, which provides |
| 35 | +// higher-level drivers. |
| 36 | +use seeeduino_xiao_rp2040::hal; |
| 37 | + |
| 38 | +// The minimum PWM value (i.e. LED brightness) we want |
| 39 | +const LOW: u16 = 0; |
| 40 | + |
| 41 | +// The maximum PWM value (i.e. LED brightness) we want |
| 42 | +const HIGH: u16 = 60000; |
| 43 | + |
| 44 | +/// Entry point to our bare-metal application. |
| 45 | +/// |
| 46 | +/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function |
| 47 | +/// as soon as all global variables are initialised. |
| 48 | +/// |
| 49 | +/// The function configures the RP2040 peripherals, then blinks the LED in an |
| 50 | +/// infinite loop. |
| 51 | +#[entry] |
| 52 | +fn main() -> ! { |
| 53 | + // Grab our singleton objects |
| 54 | + let mut pac = pac::Peripherals::take().unwrap(); |
| 55 | + let core = pac::CorePeripherals::take().unwrap(); |
| 56 | + |
| 57 | + // Set up the watchdog driver - needed by the clock setup code |
| 58 | + let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); |
| 59 | + |
| 60 | + // Configure the clocks |
| 61 | + // |
| 62 | + // The default is to generate a 125 MHz system clock |
| 63 | + let clocks = hal::clocks::init_clocks_and_plls( |
| 64 | + seeeduino_xiao_rp2040::XOSC_CRYSTAL_FREQ, |
| 65 | + pac.XOSC, |
| 66 | + pac.CLOCKS, |
| 67 | + pac.PLL_SYS, |
| 68 | + pac.PLL_USB, |
| 69 | + &mut pac.RESETS, |
| 70 | + &mut watchdog, |
| 71 | + ) |
| 72 | + .ok() |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + // The single-cycle I/O block controls our GPIO pins |
| 76 | + let sio = hal::Sio::new(pac.SIO); |
| 77 | + |
| 78 | + // Set the pins up according to their function on this particular board |
| 79 | + let pins = seeeduino_xiao_rp2040::Pins::new( |
| 80 | + pac.IO_BANK0, |
| 81 | + pac.PADS_BANK0, |
| 82 | + sio.gpio_bank0, |
| 83 | + &mut pac.RESETS, |
| 84 | + ); |
| 85 | + |
| 86 | + // The delay object lets us wait for specified amounts of time (in |
| 87 | + // milliseconds) |
| 88 | + let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer()); |
| 89 | + |
| 90 | + // Init PWMs |
| 91 | + let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS); |
| 92 | + |
| 93 | + // Configure PWM4 |
| 94 | + let pwm = &mut pwm_slices.pwm0; |
| 95 | + pwm.set_ph_correct(); |
| 96 | + pwm.enable(); |
| 97 | + |
| 98 | + // Output channel B on PWM0 to the red LED pin, initially off |
| 99 | + let channel = &mut pwm.channel_b; |
| 100 | + channel.output_to(pins.led_red); |
| 101 | + channel.set_duty(u16::MAX); |
| 102 | + |
| 103 | + // Set the blue LED to be an output, initially off |
| 104 | + let mut led_blue_pin = pins.led_blue.into_push_pull_output(); |
| 105 | + led_blue_pin.set_high().unwrap(); |
| 106 | + |
| 107 | + // Turn off the green LED |
| 108 | + let mut led_green_pin = pins.led_green.into_push_pull_output(); |
| 109 | + led_green_pin.set_high().unwrap(); |
| 110 | + |
| 111 | + loop { |
| 112 | + // Blink blue LED at 1 Hz |
| 113 | + for _ in 0..5 { |
| 114 | + led_blue_pin.set_low().unwrap(); |
| 115 | + delay.delay_ms(500); |
| 116 | + led_blue_pin.set_high().unwrap(); |
| 117 | + delay.delay_ms(500); |
| 118 | + } |
| 119 | + |
| 120 | + // Ramp red LED brightness up |
| 121 | + for i in (LOW..=HIGH).skip(30) { |
| 122 | + delay.delay_us(100); |
| 123 | + channel.set_duty(u16::MAX - i); |
| 124 | + } |
| 125 | + |
| 126 | + // Ramp red LED brightness down |
| 127 | + for i in (LOW..=HIGH).rev().skip(30) { |
| 128 | + delay.delay_us(100); |
| 129 | + channel.set_duty(u16::MAX - i); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// End of file |
0 commit comments