To open the Blink LED example, click on the navigation menu button on the top left side of the screen. Then, click on 'open example' as shown below.
Once the 'Pick Example Code To Load' window opens, select 'Blink LEDS(RP-2040)' as shown below.
Now, the Blink LED project will open up. To flash the code onto your board, follow the instructions in Section 3 of the Making Your First Project tutorial.
Breakdown of main.rs:
1. Attribute Macros and Imports
#![no_std]
#![no_main]
#![no_std]
removes the standard library to make the code suitable for bare-metal (embedded) programming.#![no_main]
tells Rust that we are defining our own entry point instead of using the standard main()
function.Low-Level and HAL Imports
use core::fmt::Write;
use core::panic::PanicInfo;
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
This section imports necessary Rust core libraries and traits:
core::fmt::Write
: Used for formatted output (though unused in this example).core::panic::PanicInfo
: Provides panic handling support.cortex_m_rt::entry
: Defines the program entry point.embedded_hal::digital::v2::OutputPin
: Trait to control GPIO pins as digital outputs.Board-Specific Imports
use adafruit_feather_rp2040::hal as hal;
use hal::{
pac::interrupt,
clocks::{init_clocks_and_plls, Clock},
pac,
watchdog::Watchdog,
Sio,
};
use adafruit_feather_rp2040::{Pins, XOSC_CRYSTAL_FREQ};
hal
(Hardware Abstraction Layer) provides access to microcontroller peripherals.init_clocks_and_plls()
function is used to configure system clocks.Watchdog
is initialized for system stability.Sio
(Single-Cycle IO) is used for GPIO initialization.2. Panic Handler
#[panic_handler]
fn panic(_panic_info: &PanicInfo) -> ! {
loop {}
}
no_std
environments. #[entry]
fn main() -> ! {
#[entry]
macro marks the function as the main entry point.!
return type, meaning it never returns.Peripheral and Clock Initialization
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
pac::Peripherals::take()
retrieves access to microcontroller peripherals.pac::CorePeripherals::take()
grants access to core peripherals.Watchdog
timer is created to prevent system hangs. let clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
).ok().unwrap();
GPIO and Timer Setup
let sio = Sio::new(pac.SIO);
let pins = Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
Sio::new(pac.SIO)
initializes the single-cycle IO system.Pins::new()
initializes GPIO pins. let mut timer = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let mut led_pin = pins.d13.into_push_pull_output();
Delay
timer is created using the system clock. loop {
led_pin.set_low().unwrap();
timer.delay_ms(delay);
led_pin.set_high().unwrap();
timer.delay_ms(delay);
}
set_low()
), waits for 500ms
, turns on (set_high()
), and waits again.