-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from PietPtr/feature/GpInSupport
- Loading branch information
Showing
5 changed files
with
155 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! # gpin External Clocks example | ||
//! | ||
//! This application demonstrates how to clock the processor using an external clock on GPIO20 | ||
//! | ||
//! It may need to be adapted to your particular board layout and/or pin assignment. | ||
//! | ||
//! See the top-level `README.md` file for Copyright and license details. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use embedded_hal_0_2::digital::v2::ToggleableOutputPin; | ||
// Ensure we halt the program on panic (if we don't mention this crate it won't | ||
// be linked) | ||
use panic_halt as _; | ||
|
||
// To use the .MHz() function | ||
use fugit::RateExtU32; | ||
|
||
use rp2040_hal::clocks::ClockSource; | ||
// Alias for our HAL crate | ||
use rp2040_hal as hal; | ||
|
||
// Necessary HAL types | ||
use hal::{clocks::ClocksManager, gpin::GpIn0, gpio, Clock, Sio}; | ||
|
||
// A shorter alias for the Peripheral Access Crate, which provides low-level | ||
// register access | ||
use hal::pac; | ||
|
||
/// 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. | ||
#[link_section = ".boot2"] | ||
#[used] | ||
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H; | ||
|
||
// The external clock provided to GPIO pin 20. | ||
const GPIN_EXTERNAL_CLOCK_FREQ_HZ: u32 = 1_000_000u32; | ||
|
||
/// Entry point to our bare-metal application. | ||
/// | ||
/// The `#[rp2040_hal::entry]` macro ensures the Cortex-M start-up code calls this function | ||
/// as soon as all global variables and the spinlock are initialised. | ||
/// | ||
/// The function configures the RP2040 to accept an external clock on Gpio20, | ||
/// then configures the system clock to run off this clock. | ||
#[rp2040_hal::entry] | ||
fn main() -> ! { | ||
let mut pac = pac::Peripherals::take().unwrap(); | ||
|
||
let sio = Sio::new(pac.SIO); | ||
|
||
let pins = gpio::Pins::new( | ||
pac.IO_BANK0, | ||
pac.PADS_BANK0, | ||
sio.gpio_bank0, | ||
&mut pac.RESETS, | ||
); | ||
|
||
let gpin0_pin = pins.gpio20.reconfigure(); | ||
let gpin0: GpIn0 = GpIn0::new(gpin0_pin, GPIN_EXTERNAL_CLOCK_FREQ_HZ.Hz()); | ||
|
||
let mut clocks = ClocksManager::new(pac.CLOCKS); | ||
|
||
clocks | ||
.system_clock | ||
.configure_clock(&gpin0, gpin0.get_freq()) | ||
.unwrap(); | ||
|
||
let mut test_pin = pins.gpio0.into_push_pull_output(); | ||
|
||
loop { | ||
// Continuously toggle a pin so it's possible to observe on a scope that the pico runs on | ||
// the externally provided frequency, and is synchronized to it. | ||
test_pin.toggle().unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Defines a wrapper for the GPIO pins that can route external clocks into the RP2040. | ||
//! | ||
//! See [2.15.2.3. External Clocks](https://datasheets.raspberrypi.org/rp2040/rp2040-datasheet.pdf) for more details. | ||
//! Or see [examples/gpin.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal-examples/src/bin/gpin.rs) for a practical example | ||
use fugit::HertzU32; | ||
|
||
use crate::{ | ||
gpio::{ | ||
bank0::{Gpio20, Gpio22}, | ||
FunctionClock, Pin, PullNone, PullType, | ||
}, | ||
typelevel::Sealed, | ||
}; | ||
|
||
macro_rules! gpin { | ||
($id:ident, $pin:ident) => { | ||
/// A gpin pin: a pin that can be used as a clock input. | ||
pub struct $id<M = PullNone> | ||
where | ||
M: PullType, | ||
{ | ||
pin: Pin<$pin, FunctionClock, M>, | ||
frequency: HertzU32, | ||
} | ||
|
||
impl<M: PullType> $id<M> { | ||
#[doc = concat!("Creates a new ", stringify!($id), " given the input pin.")] | ||
pub fn new(pin: Pin<$pin, FunctionClock, M>, frequency: HertzU32) -> Self { | ||
Self { pin, frequency } | ||
} | ||
|
||
/// Set the frequency of the externally applied clock signal. | ||
/// This frequency is used when computing clock dividers. | ||
pub fn set_frequency(mut self, frequency: HertzU32) -> Self { | ||
self.frequency = frequency; | ||
self | ||
} | ||
|
||
/// Retrieve frequency | ||
pub fn frequency(&self) -> HertzU32 { | ||
self.frequency | ||
} | ||
|
||
#[doc = concat!("Release the underlying device and ", stringify!($pin), ".")] | ||
pub fn free(self) -> Pin<$pin, FunctionClock, M> { | ||
self.pin | ||
} | ||
} | ||
|
||
impl<M: PullType> Sealed for $id<M> {} | ||
}; | ||
} | ||
|
||
gpin!(GpIn0, Gpio20); | ||
gpin!(GpIn1, Gpio22); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters