aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2025-05-09 19:34:43 +0200
committerGitHub <[email protected]>2025-05-09 19:34:43 +0200
commit11364077a7bb6d14bd37567d17ddb21249409ec7 (patch)
treec1d3fb45984b5ae17a684a32f6d45aff63b5592d /examples/rp/src
parent2a27aa828cdc310b17c379183bdfaba6585ad933 (diff)
parent4621c8aa7a1ee1b55f2f0bf80fc48eddf76af320 (diff)
Merge pull request #4150 from 1-rafael-1/rp2040-overclocking
RP: rp2040 overclocking
Diffstat (limited to 'examples/rp/src')
-rw-r--r--examples/rp/src/bin/overclock.rs64
-rw-r--r--examples/rp/src/bin/overclock_manual.rs79
2 files changed, 143 insertions, 0 deletions
diff --git a/examples/rp/src/bin/overclock.rs b/examples/rp/src/bin/overclock.rs
new file mode 100644
index 000000000..9c78e0c9d
--- /dev/null
+++ b/examples/rp/src/bin/overclock.rs
@@ -0,0 +1,64 @@
1//! # Overclocking the RP2040 to 200 MHz
2//!
3//! This example demonstrates how to configure the RP2040 to run at 200 MHz.
4
5#![no_std]
6#![no_main]
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_rp::clocks::{clk_sys_freq, ClockConfig};
11use embassy_rp::config::Config;
12use embassy_rp::gpio::{Level, Output};
13use embassy_time::{Duration, Instant, Timer};
14use {defmt_rtt as _, panic_probe as _};
15
16const COUNT_TO: i64 = 10_000_000;
17
18#[embassy_executor::main]
19async fn main(_spawner: Spawner) -> ! {
20 // Set up for clock frequency of 200 MHz, setting all necessary defaults.
21 let config = Config::new(ClockConfig::system_freq(200_000_000));
22
23 // Show the voltage scale for verification
24 info!("System core voltage: {}", Debug2Format(&config.clocks.core_voltage));
25
26 // Initialize the peripherals
27 let p = embassy_rp::init(config);
28
29 // Show CPU frequency for verification
30 let sys_freq = clk_sys_freq();
31 info!("System clock frequency: {} MHz", sys_freq / 1_000_000);
32
33 // LED to indicate the system is running
34 let mut led = Output::new(p.PIN_25, Level::Low);
35
36 loop {
37 // Reset the counter at the start of measurement period
38 let mut counter = 0;
39
40 // Turn LED on while counting
41 led.set_high();
42
43 let start = Instant::now();
44
45 // This is a busy loop that will take some time to complete
46 while counter < COUNT_TO {
47 counter += 1;
48 }
49
50 let elapsed = Instant::now() - start;
51
52 // Report the elapsed time
53 led.set_low();
54 info!(
55 "At {}Mhz: Elapsed time to count to {}: {}ms",
56 sys_freq / 1_000_000,
57 counter,
58 elapsed.as_millis()
59 );
60
61 // Wait 2 seconds before starting the next measurement
62 Timer::after(Duration::from_secs(2)).await;
63 }
64}
diff --git a/examples/rp/src/bin/overclock_manual.rs b/examples/rp/src/bin/overclock_manual.rs
new file mode 100644
index 000000000..35160b250
--- /dev/null
+++ b/examples/rp/src/bin/overclock_manual.rs
@@ -0,0 +1,79 @@
1//! # Overclocking the RP2040 to 200 MHz manually
2//!
3//! This example demonstrates how to manually configure the RP2040 to run at 200 MHz.
4
5#![no_std]
6#![no_main]
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_rp::clocks;
11use embassy_rp::clocks::{ClockConfig, CoreVoltage, PllConfig};
12use embassy_rp::config::Config;
13use embassy_rp::gpio::{Level, Output};
14use embassy_time::{Duration, Instant, Timer};
15use {defmt_rtt as _, panic_probe as _};
16
17const COUNT_TO: i64 = 10_000_000;
18
19/// Configure the RP2040 for 200 MHz operation by manually specifying the PLL settings.
20fn configure_manual_overclock() -> Config {
21 // Set the PLL configuration manually, starting from default values
22 let mut config = Config::default();
23
24 // Set the system clock to 200 MHz
25 config.clocks = ClockConfig::manual_pll(
26 12_000_000, // Crystal frequency, 12 MHz is common. If using custom, set to your value.
27 PllConfig {
28 refdiv: 1, // Reference divider
29 fbdiv: 100, // Feedback divider
30 post_div1: 3, // Post divider 1
31 post_div2: 2, // Post divider 2
32 },
33 CoreVoltage::V1_15, // Core voltage, should be set to V1_15 for 200 MHz
34 );
35
36 config
37}
38
39#[embassy_executor::main]
40async fn main(_spawner: Spawner) -> ! {
41 // Initialize with our manual overclock configuration
42 let p = embassy_rp::init(configure_manual_overclock());
43
44 // Verify the actual system clock frequency
45 let sys_freq = clocks::clk_sys_freq();
46 info!("System clock frequency: {} MHz", sys_freq / 1_000_000);
47
48 // LED to indicate the system is running
49 let mut led = Output::new(p.PIN_25, Level::Low);
50
51 loop {
52 // Reset the counter at the start of measurement period
53 let mut counter = 0;
54
55 // Turn LED on while counting
56 led.set_high();
57
58 let start = Instant::now();
59
60 // This is a busy loop that will take some time to complete
61 while counter < COUNT_TO {
62 counter += 1;
63 }
64
65 let elapsed = Instant::now() - start;
66
67 // Report the elapsed time
68 led.set_low();
69 info!(
70 "At {}Mhz: Elapsed time to count to {}: {}ms",
71 sys_freq / 1_000_000,
72 counter,
73 elapsed.as_millis()
74 );
75
76 // Wait 2 seconds before starting the next measurement
77 Timer::after(Duration::from_secs(2)).await;
78 }
79}