aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/stm32h755cm4/Cargo.toml10
-rw-r--r--examples/stm32h755cm4/src/bin/intercore.rs182
-rw-r--r--examples/stm32h755cm7/Cargo.toml12
-rw-r--r--examples/stm32h755cm7/src/bin/intercore.rs228
4 files changed, 412 insertions, 20 deletions
diff --git a/examples/stm32h755cm4/Cargo.toml b/examples/stm32h755cm4/Cargo.toml
index eaaeb1ac0..cf324d3df 100644
--- a/examples/stm32h755cm4/Cargo.toml
+++ b/examples/stm32h755cm4/Cargo.toml
@@ -36,13 +36,6 @@ chrono = { version = "^0.4", default-features = false }
36grounded = "0.2.0" 36grounded = "0.2.0"
37 37
38# cargo build/run 38# cargo build/run
39[profile.dev]
40codegen-units = 1
41debug = 2
42debug-assertions = true # <-
43incremental = false
44opt-level = 3 # <-
45overflow-checks = true # <-
46 39
47# cargo test 40# cargo test
48[profile.test] 41[profile.test]
@@ -55,11 +48,10 @@ overflow-checks = true # <-
55 48
56# cargo build/run --release 49# cargo build/run --release
57[profile.release] 50[profile.release]
58codegen-units = 1 51codegen-units = 16
59debug = 2 52debug = 2
60debug-assertions = false # <- 53debug-assertions = false # <-
61incremental = false 54incremental = false
62lto = 'fat'
63opt-level = 3 # <- 55opt-level = 3 # <-
64overflow-checks = false # <- 56overflow-checks = false # <-
65 57
diff --git a/examples/stm32h755cm4/src/bin/intercore.rs b/examples/stm32h755cm4/src/bin/intercore.rs
new file mode 100644
index 000000000..d5e3e7648
--- /dev/null
+++ b/examples/stm32h755cm4/src/bin/intercore.rs
@@ -0,0 +1,182 @@
1#![no_std]
2#![no_main]
3
4//! STM32H7 Secondary Core (CM4) Intercore Communication Example
5//!
6//! This example demonstrates reliable communication between the Cortex-M7 and
7//! Cortex-M4 cores. This secondary core monitors shared memory for LED state
8//! changes and updates the physical LEDs accordingly.
9//!
10//! The CM4 core handles:
11//! - Responding to state changes from CM7
12//! - Controlling the physical green and yellow LEDs
13//! - Providing visual feedback via a heartbeat on the red LED
14//!
15//! Usage:
16//! 1. Flash this CM4 (secondary) core binary first
17//! 2. Then flash the CM7 (primary) core binary
18//! 3. The red LED should blink continuously as a heartbeat
19//! 4. Green and yellow LEDs should toggle according to CM7 core signals
20
21/// Module providing shared memory constructs for intercore communication
22mod shared {
23 use core::sync::atomic::{AtomicU32, Ordering};
24
25 /// State shared between CM7 and CM4 cores for LED control
26 #[repr(C, align(4))]
27 pub struct SharedLedState {
28 pub magic: AtomicU32,
29 pub counter: AtomicU32,
30 pub led_states: AtomicU32,
31 }
32
33 // Bit positions in led_states
34 pub const GREEN_LED_BIT: u32 = 0;
35 pub const YELLOW_LED_BIT: u32 = 1;
36
37 impl SharedLedState {
38 pub const fn new() -> Self {
39 Self {
40 magic: AtomicU32::new(0xDEADBEEF),
41 counter: AtomicU32::new(0),
42 led_states: AtomicU32::new(0),
43 }
44 }
45
46 /// Set LED state by manipulating the appropriate bit in the led_states field
47 #[inline(never)]
48 #[allow(dead_code)]
49 pub fn set_led(&self, is_green: bool, state: bool) {
50 let bit = if is_green { GREEN_LED_BIT } else { YELLOW_LED_BIT };
51 let current = self.led_states.load(Ordering::SeqCst);
52
53 let new_value = if state {
54 current | (1 << bit) // Set bit
55 } else {
56 current & !(1 << bit) // Clear bit
57 };
58 self.led_states.store(new_value, Ordering::SeqCst);
59 }
60
61 /// Get current LED state
62 #[inline(never)]
63 pub fn get_led(&self, is_green: bool) -> bool {
64 let bit = if is_green { GREEN_LED_BIT } else { YELLOW_LED_BIT };
65
66 let value = self.led_states.load(Ordering::SeqCst);
67 (value & (1 << bit)) != 0
68 }
69
70 /// Increment counter and return new value
71 #[inline(never)]
72 #[allow(dead_code)]
73 pub fn increment_counter(&self) -> u32 {
74 let current = self.counter.load(Ordering::SeqCst);
75 let new_value = current.wrapping_add(1);
76 self.counter.store(new_value, Ordering::SeqCst);
77 new_value
78 }
79
80 /// Get current counter value
81 #[inline(never)]
82 pub fn get_counter(&self) -> u32 {
83 let value = self.counter.load(Ordering::SeqCst);
84 value
85 }
86 }
87
88 #[link_section = ".ram_d3"]
89 pub static SHARED_LED_STATE: SharedLedState = SharedLedState::new();
90}
91
92use core::mem::MaybeUninit;
93
94use defmt::*;
95use embassy_executor::Spawner;
96use embassy_stm32::gpio::{Level, Output, Speed};
97use embassy_stm32::SharedData;
98use embassy_time::Timer;
99use shared::SHARED_LED_STATE;
100use {defmt_rtt as _, panic_probe as _};
101
102#[link_section = ".ram_d3"]
103static SHARED_DATA: MaybeUninit<SharedData> = MaybeUninit::uninit();
104
105/// Task that continuously blinks the red LED as a heartbeat indicator
106#[embassy_executor::task]
107async fn blink_heartbeat(mut led: Output<'static>) {
108 loop {
109 led.toggle();
110 info!("CM4 heartbeat");
111 Timer::after_millis(500).await;
112 }
113}
114
115#[embassy_executor::main]
116async fn main(spawner: Spawner) -> ! {
117 // Initialize the secondary core
118 let p = embassy_stm32::init_secondary(&SHARED_DATA);
119 info!("CM4 core initialized!");
120
121 // Verify shared memory is accessible
122 let magic = SHARED_LED_STATE.magic.load(core::sync::atomic::Ordering::SeqCst);
123 info!("CM4: Magic value = 0x{:X}", magic);
124
125 // Set up LEDs
126 let mut green_led = Output::new(p.PB0, Level::Low, Speed::Low); // LD1
127 let mut yellow_led = Output::new(p.PE1, Level::Low, Speed::Low); // LD2
128 let red_led = Output::new(p.PB14, Level::Low, Speed::Low); // LD3 (heartbeat)
129
130 // Start heartbeat task
131 unwrap!(spawner.spawn(blink_heartbeat(red_led)));
132
133 // Track previous values to detect changes
134 let mut prev_green = false;
135 let mut prev_yellow = false;
136 let mut prev_counter = 0;
137
138 info!("CM4: Starting main loop");
139 loop {
140 // Read current values from shared memory
141 let green_state = SHARED_LED_STATE.get_led(true);
142 let yellow_state = SHARED_LED_STATE.get_led(false);
143 let counter = SHARED_LED_STATE.get_counter();
144
145 // Detect changes
146 let green_changed = green_state != prev_green;
147 let yellow_changed = yellow_state != prev_yellow;
148 let counter_changed = counter != prev_counter;
149
150 // Update LEDs and logs when values change
151 if green_changed || yellow_changed || counter_changed {
152 if counter_changed {
153 info!("CM4: Counter = {}", counter);
154 prev_counter = counter;
155 }
156
157 if green_changed {
158 if green_state {
159 green_led.set_high();
160 info!("CM4: Green LED ON");
161 } else {
162 green_led.set_low();
163 info!("CM4: Green LED OFF");
164 }
165 prev_green = green_state;
166 }
167
168 if yellow_changed {
169 if yellow_state {
170 yellow_led.set_high();
171 info!("CM4: Yellow LED ON");
172 } else {
173 yellow_led.set_low();
174 info!("CM4: Yellow LED OFF");
175 }
176 prev_yellow = yellow_state;
177 }
178 }
179
180 Timer::after_millis(10).await;
181 }
182}
diff --git a/examples/stm32h755cm7/Cargo.toml b/examples/stm32h755cm7/Cargo.toml
index e7fc05948..ac1429f30 100644
--- a/examples/stm32h755cm7/Cargo.toml
+++ b/examples/stm32h755cm7/Cargo.toml
@@ -35,15 +35,6 @@ static_cell = "2"
35chrono = { version = "^0.4", default-features = false } 35chrono = { version = "^0.4", default-features = false }
36grounded = "0.2.0" 36grounded = "0.2.0"
37 37
38# cargo build/run
39[profile.dev]
40codegen-units = 1
41debug = 2
42debug-assertions = true # <-
43incremental = false
44opt-level = 3 # <-
45overflow-checks = true # <-
46
47# cargo test 38# cargo test
48[profile.test] 39[profile.test]
49codegen-units = 1 40codegen-units = 1
@@ -55,11 +46,10 @@ overflow-checks = true # <-
55 46
56# cargo build/run --release 47# cargo build/run --release
57[profile.release] 48[profile.release]
58codegen-units = 1 49codegen-units = 16
59debug = 2 50debug = 2
60debug-assertions = false # <- 51debug-assertions = false # <-
61incremental = false 52incremental = false
62lto = 'fat'
63opt-level = 3 # <- 53opt-level = 3 # <-
64overflow-checks = false # <- 54overflow-checks = false # <-
65 55
diff --git a/examples/stm32h755cm7/src/bin/intercore.rs b/examples/stm32h755cm7/src/bin/intercore.rs
new file mode 100644
index 000000000..a4e1b5ff4
--- /dev/null
+++ b/examples/stm32h755cm7/src/bin/intercore.rs
@@ -0,0 +1,228 @@
1#![no_std]
2#![no_main]
3
4//! STM32H7 Primary Core (CM7) Intercore Communication Example
5//!
6//! This example demonstrates reliable communication between the Cortex-M7 and
7//! Cortex-M4 cores using a shared memory region configured as non-cacheable
8//! via MPU settings.
9//!
10//! The CM7 core handles:
11//! - MPU configuration to make shared memory non-cacheable
12//! - Clock initialization
13//! - Toggling LED states in shared memory
14//!
15//! Usage:
16//! 1. Flash the CM4 (secondary) core binary first
17//! 2. Then flash this CM7 (primary) core binary
18//! 3. The system will start with CM7 toggling LED states and CM4 responding by
19//! physically toggling the LEDs
20
21use core::mem::MaybeUninit;
22
23use cortex_m::asm;
24use cortex_m::peripheral::MPU;
25use defmt::*;
26use embassy_executor::Spawner;
27use embassy_stm32::{Config, SharedData};
28use embassy_time::Timer;
29use shared::{SHARED_LED_STATE, SRAM4_BASE_ADDRESS, SRAM4_REGION_NUMBER, SRAM4_SIZE_LOG2};
30use {defmt_rtt as _, panic_probe as _};
31
32/// Module providing shared memory constructs for intercore communication
33mod shared {
34 use core::sync::atomic::{AtomicU32, Ordering};
35
36 /// State shared between CM7 and CM4 cores for LED control
37 #[repr(C, align(4))]
38 pub struct SharedLedState {
39 pub magic: AtomicU32,
40 pub counter: AtomicU32,
41 pub led_states: AtomicU32,
42 }
43
44 // Bit positions in led_states
45 pub const GREEN_LED_BIT: u32 = 0;
46 pub const YELLOW_LED_BIT: u32 = 1;
47
48 impl SharedLedState {
49 pub const fn new() -> Self {
50 Self {
51 magic: AtomicU32::new(0xDEADBEEF),
52 counter: AtomicU32::new(0),
53 led_states: AtomicU32::new(0),
54 }
55 }
56
57 /// Set LED state by manipulating the appropriate bit in the led_states field
58 #[inline(never)]
59 pub fn set_led(&self, is_green: bool, state: bool) {
60 let bit = if is_green { GREEN_LED_BIT } else { YELLOW_LED_BIT };
61 let current = self.led_states.load(Ordering::SeqCst);
62
63 let new_value = if state {
64 current | (1 << bit) // Set bit
65 } else {
66 current & !(1 << bit) // Clear bit
67 };
68
69 self.led_states.store(new_value, Ordering::SeqCst);
70 }
71
72 /// Get current LED state
73 #[inline(never)]
74 #[allow(dead_code)]
75 pub fn get_led(&self, is_green: bool) -> bool {
76 let bit = if is_green { GREEN_LED_BIT } else { YELLOW_LED_BIT };
77
78 let value = self.led_states.load(Ordering::SeqCst);
79 (value & (1 << bit)) != 0
80 }
81
82 /// Increment counter and return new value
83 #[inline(never)]
84 pub fn increment_counter(&self) -> u32 {
85 let current = self.counter.load(Ordering::SeqCst);
86 let new_value = current.wrapping_add(1);
87 self.counter.store(new_value, Ordering::SeqCst);
88 new_value
89 }
90
91 /// Get current counter value
92 #[inline(never)]
93 #[allow(dead_code)]
94 pub fn get_counter(&self) -> u32 {
95 let value = self.counter.load(Ordering::SeqCst);
96 value
97 }
98 }
99
100 #[link_section = ".ram_d3"]
101 pub static SHARED_LED_STATE: SharedLedState = SharedLedState::new();
102
103 // Memory region constants for MPU configuration
104 pub const SRAM4_BASE_ADDRESS: u32 = 0x38000000;
105 pub const SRAM4_SIZE_LOG2: u32 = 15; // 64KB = 2^(15+1)
106 pub const SRAM4_REGION_NUMBER: u8 = 0;
107}
108
109#[link_section = ".ram_d3"]
110static SHARED_DATA: MaybeUninit<SharedData> = MaybeUninit::uninit();
111
112/// Configure MPU to make SRAM4 region non-cacheable
113fn configure_mpu_non_cacheable(mpu: &mut MPU) {
114 asm::dmb();
115 unsafe {
116 // Disable MPU
117 mpu.ctrl.write(0);
118
119 // Configure SRAM4 as non-cacheable
120 mpu.rnr.write(SRAM4_REGION_NUMBER as u32);
121
122 // Set base address with region number
123 mpu.rbar.write(SRAM4_BASE_ADDRESS | (1 << 4));
124
125 // Configure region attributes
126 let rasr_value: u32 = (SRAM4_SIZE_LOG2 << 1) | // SIZE=15 (64KB)
127 (1 << 0) | // ENABLE=1
128 (3 << 24) | // AP=3 (Full access)
129 (1 << 19) | // TEX=1
130 (1 << 18); // S=1 (Shareable)
131
132 mpu.rasr.write(rasr_value);
133
134 // Enable MPU with default memory map as background
135 mpu.ctrl.write(1 | (1 << 2)); // MPU_ENABLE | PRIVDEFENA
136 }
137
138 asm::dsb();
139 asm::isb();
140
141 info!("MPU configured - SRAM4 set as non-cacheable");
142}
143
144#[embassy_executor::main]
145async fn main(_spawner: Spawner) -> ! {
146 // Set up MPU and cache configuration
147 {
148 let mut cp = cortex_m::Peripherals::take().unwrap();
149 let scb = &mut cp.SCB;
150
151 // First disable caches
152 scb.disable_icache();
153 scb.disable_dcache(&mut cp.CPUID);
154
155 // Configure MPU
156 configure_mpu_non_cacheable(&mut cp.MPU);
157
158 // Re-enable caches
159 scb.enable_icache();
160 scb.enable_dcache(&mut cp.CPUID);
161 asm::dsb();
162 asm::isb();
163 }
164
165 // Configure the clock system
166 let mut config = Config::default();
167 {
168 use embassy_stm32::rcc::*;
169 config.rcc.hsi = Some(HSIPrescaler::DIV1);
170 config.rcc.csi = true;
171 config.rcc.hsi48 = Some(Default::default());
172 config.rcc.pll1 = Some(Pll {
173 source: PllSource::HSI,
174 prediv: PllPreDiv::DIV4,
175 mul: PllMul::MUL50,
176 divp: Some(PllDiv::DIV2),
177 divq: Some(PllDiv::DIV8),
178 divr: None,
179 });
180 config.rcc.sys = Sysclk::PLL1_P;
181 config.rcc.ahb_pre = AHBPrescaler::DIV2;
182 config.rcc.apb1_pre = APBPrescaler::DIV2;
183 config.rcc.apb2_pre = APBPrescaler::DIV2;
184 config.rcc.apb3_pre = APBPrescaler::DIV2;
185 config.rcc.apb4_pre = APBPrescaler::DIV2;
186 config.rcc.voltage_scale = VoltageScale::Scale1;
187 config.rcc.supply_config = SupplyConfig::DirectSMPS;
188 }
189
190 // Initialize the CM7 core
191 let _p = embassy_stm32::init_primary(config, &SHARED_DATA);
192 info!("CM7 core initialized with non-cacheable SRAM4!");
193
194 // Verify shared memory is accessible
195 let magic = SHARED_LED_STATE.magic.load(core::sync::atomic::Ordering::SeqCst);
196 info!("CM7: Magic value = 0x{:X}", magic);
197
198 // Initialize LED states
199 SHARED_LED_STATE.set_led(true, false); // Green LED off
200 SHARED_LED_STATE.set_led(false, false); // Yellow LED off
201
202 // Main loop - periodically toggle LED states
203 let mut green_state = false;
204 let mut yellow_state = false;
205 let mut loop_count = 0;
206
207 info!("CM7: Starting main loop");
208 loop {
209 loop_count += 1;
210 let counter = SHARED_LED_STATE.increment_counter();
211
212 // Toggle green LED every second
213 if loop_count % 10 == 0 {
214 green_state = !green_state;
215 SHARED_LED_STATE.set_led(true, green_state);
216 info!("CM7: Counter = {}, Set green LED to {}", counter, green_state);
217 }
218
219 // Toggle yellow LED every 3 seconds
220 if loop_count % 30 == 0 {
221 yellow_state = !yellow_state;
222 SHARED_LED_STATE.set_led(false, yellow_state);
223 info!("CM7: Counter = {}, Set yellow LED to {}", counter, yellow_state);
224 }
225
226 Timer::after_millis(100).await;
227 }
228}