aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32-wpan/src/ble.rs4
-rw-r--r--embassy-stm32-wpan/src/lib.rs78
-rw-r--r--embassy-stm32-wpan/src/mm.rs2
-rw-r--r--embassy-stm32-wpan/src/sys.rs6
-rw-r--r--embassy-stm32-wpan/src/unsafe_linked_list.rs105
-rw-r--r--tests/stm32/Cargo.toml2
-rw-r--r--tests/stm32/src/bin/tl_mbox.rs17
7 files changed, 158 insertions, 56 deletions
diff --git a/embassy-stm32-wpan/src/ble.rs b/embassy-stm32-wpan/src/ble.rs
index 19955a8a3..57195ef9d 100644
--- a/embassy-stm32-wpan/src/ble.rs
+++ b/embassy-stm32-wpan/src/ble.rs
@@ -18,7 +18,7 @@ impl Ble {
18 unsafe { 18 unsafe {
19 LinkedListNode::init_head(EVT_QUEUE.as_mut_ptr()); 19 LinkedListNode::init_head(EVT_QUEUE.as_mut_ptr());
20 20
21 TL_BLE_TABLE = MaybeUninit::new(BleTable { 21 TL_BLE_TABLE.as_mut_ptr().write_volatile(BleTable {
22 pcmd_buffer: BLE_CMD_BUFFER.as_mut_ptr().cast(), 22 pcmd_buffer: BLE_CMD_BUFFER.as_mut_ptr().cast(),
23 pcs_buffer: CS_BUFFER.as_ptr().cast(), 23 pcs_buffer: CS_BUFFER.as_ptr().cast(),
24 pevt_queue: EVT_QUEUE.as_ptr().cast(), 24 pevt_queue: EVT_QUEUE.as_ptr().cast(),
@@ -41,6 +41,8 @@ impl Ble {
41 let event = EvtBox::new(event); 41 let event = EvtBox::new(event);
42 42
43 EVT_CHANNEL.try_send(event).unwrap(); 43 EVT_CHANNEL.try_send(event).unwrap();
44
45 break;
44 } 46 }
45 } 47 }
46 48
diff --git a/embassy-stm32-wpan/src/lib.rs b/embassy-stm32-wpan/src/lib.rs
index c37b67dc4..000282f2b 100644
--- a/embassy-stm32-wpan/src/lib.rs
+++ b/embassy-stm32-wpan/src/lib.rs
@@ -4,6 +4,7 @@
4pub mod fmt; 4pub mod fmt;
5 5
6use core::mem::MaybeUninit; 6use core::mem::MaybeUninit;
7use core::sync::atomic::{compiler_fence, Ordering};
7 8
8use cmd::CmdPacket; 9use cmd::CmdPacket;
9use embassy_futures::block_on; 10use embassy_futures::block_on;
@@ -189,33 +190,76 @@ impl<'d> TlMbox<'d> {
189 into_ref!(ipcc); 190 into_ref!(ipcc);
190 191
191 unsafe { 192 unsafe {
192 TL_REF_TABLE = MaybeUninit::new(RefTable { 193 TL_REF_TABLE.as_mut_ptr().write_volatile(RefTable {
193 device_info_table: TL_DEVICE_INFO_TABLE.as_mut_ptr(), 194 device_info_table: TL_DEVICE_INFO_TABLE.as_ptr(),
194 ble_table: TL_BLE_TABLE.as_ptr(), 195 ble_table: TL_BLE_TABLE.as_ptr(),
195 thread_table: TL_THREAD_TABLE.as_ptr(), 196 thread_table: TL_THREAD_TABLE.as_ptr(),
196 sys_table: TL_SYS_TABLE.as_ptr(), 197 sys_table: TL_SYS_TABLE.as_ptr(),
197 mem_manager_table: TL_MEM_MANAGER_TABLE.as_ptr(), 198 mem_manager_table: TL_MEM_MANAGER_TABLE.as_ptr(),
198 traces_table: TL_TRACES_TABLE.as_ptr(), 199 traces_table: TL_TRACES_TABLE.as_ptr(),
199 mac_802_15_4_table: TL_MAC_802_15_4_TABLE.as_ptr(), 200 mac_802_15_4_table: TL_MAC_802_15_4_TABLE.as_ptr(),
201 // zigbee_table: TL_ZIGBEE_TABLE.as_ptr(),
202 // lld_tests_table: TL_LLD_TESTS_TABLE.as_ptr(),
203 // ble_lld_table: TL_BLE_LLD_TABLE.as_ptr(),
200 }); 204 });
201 205
202 TL_SYS_TABLE = MaybeUninit::zeroed(); 206 TL_SYS_TABLE
203 TL_DEVICE_INFO_TABLE = MaybeUninit::zeroed(); 207 .as_mut_ptr()
204 TL_BLE_TABLE = MaybeUninit::zeroed(); 208 .write_volatile(MaybeUninit::zeroed().assume_init());
205 TL_THREAD_TABLE = MaybeUninit::zeroed(); 209 TL_DEVICE_INFO_TABLE
206 TL_MEM_MANAGER_TABLE = MaybeUninit::zeroed(); 210 .as_mut_ptr()
207 TL_TRACES_TABLE = MaybeUninit::zeroed(); 211 .write_volatile(MaybeUninit::zeroed().assume_init());
208 TL_MAC_802_15_4_TABLE = MaybeUninit::zeroed(); 212 TL_BLE_TABLE
209 213 .as_mut_ptr()
210 EVT_POOL = MaybeUninit::zeroed(); 214 .write_volatile(MaybeUninit::zeroed().assume_init());
211 SYS_SPARE_EVT_BUF = MaybeUninit::zeroed(); 215 TL_THREAD_TABLE
212 BLE_SPARE_EVT_BUF = MaybeUninit::zeroed(); 216 .as_mut_ptr()
213 217 .write_volatile(MaybeUninit::zeroed().assume_init());
214 CS_BUFFER = MaybeUninit::zeroed(); 218 TL_MEM_MANAGER_TABLE
215 BLE_CMD_BUFFER = MaybeUninit::zeroed(); 219 .as_mut_ptr()
216 HCI_ACL_DATA_BUFFER = MaybeUninit::zeroed(); 220 .write_volatile(MaybeUninit::zeroed().assume_init());
221
222 TL_TRACES_TABLE
223 .as_mut_ptr()
224 .write_volatile(MaybeUninit::zeroed().assume_init());
225 TL_MAC_802_15_4_TABLE
226 .as_mut_ptr()
227 .write_volatile(MaybeUninit::zeroed().assume_init());
228// TL_ZIGBEE_TABLE
229// .as_mut_ptr()
230// .write_volatile(MaybeUninit::zeroed().assume_init());
231// TL_LLD_TESTS_TABLE
232// .as_mut_ptr()
233// .write_volatile(MaybeUninit::zeroed().assume_init());
234// TL_BLE_LLD_TABLE
235// .as_mut_ptr()
236// .write_volatile(MaybeUninit::zeroed().assume_init());
237
238 EVT_POOL
239 .as_mut_ptr()
240 .write_volatile(MaybeUninit::zeroed().assume_init());
241 SYS_SPARE_EVT_BUF
242 .as_mut_ptr()
243 .write_volatile(MaybeUninit::zeroed().assume_init());
244 BLE_SPARE_EVT_BUF
245 .as_mut_ptr()
246 .write_volatile(MaybeUninit::zeroed().assume_init());
247
248 {
249 BLE_CMD_BUFFER
250 .as_mut_ptr()
251 .write_volatile(MaybeUninit::zeroed().assume_init());
252 HCI_ACL_DATA_BUFFER
253 .as_mut_ptr()
254 .write_volatile(MaybeUninit::zeroed().assume_init());
255 CS_BUFFER
256 .as_mut_ptr()
257 .write_volatile(MaybeUninit::zeroed().assume_init());
258 }
217 } 259 }
218 260
261 compiler_fence(Ordering::SeqCst);
262
219 Ipcc::enable(config); 263 Ipcc::enable(config);
220 264
221 sys::Sys::enable(); 265 sys::Sys::enable();
diff --git a/embassy-stm32-wpan/src/mm.rs b/embassy-stm32-wpan/src/mm.rs
index ed13b0dbf..e46c63558 100644
--- a/embassy-stm32-wpan/src/mm.rs
+++ b/embassy-stm32-wpan/src/mm.rs
@@ -20,7 +20,7 @@ impl MemoryManager {
20 LinkedListNode::init_head(FREE_BUF_QUEUE.as_mut_ptr()); 20 LinkedListNode::init_head(FREE_BUF_QUEUE.as_mut_ptr());
21 LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()); 21 LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr());
22 22
23 TL_MEM_MANAGER_TABLE = MaybeUninit::new(MemManagerTable { 23 TL_MEM_MANAGER_TABLE.as_mut_ptr().write_volatile(MemManagerTable {
24 spare_ble_buffer: BLE_SPARE_EVT_BUF.as_ptr().cast(), 24 spare_ble_buffer: BLE_SPARE_EVT_BUF.as_ptr().cast(),
25 spare_sys_buffer: SYS_SPARE_EVT_BUF.as_ptr().cast(), 25 spare_sys_buffer: SYS_SPARE_EVT_BUF.as_ptr().cast(),
26 blepool: EVT_POOL.as_ptr().cast(), 26 blepool: EVT_POOL.as_ptr().cast(),
diff --git a/embassy-stm32-wpan/src/sys.rs b/embassy-stm32-wpan/src/sys.rs
index a19d12d27..0a1db7c1f 100644
--- a/embassy-stm32-wpan/src/sys.rs
+++ b/embassy-stm32-wpan/src/sys.rs
@@ -15,7 +15,7 @@ impl Sys {
15 unsafe { 15 unsafe {
16 LinkedListNode::init_head(SYSTEM_EVT_QUEUE.as_mut_ptr()); 16 LinkedListNode::init_head(SYSTEM_EVT_QUEUE.as_mut_ptr());
17 17
18 TL_SYS_TABLE = MaybeUninit::new(SysTable { 18 TL_SYS_TABLE.as_mut_ptr().write_volatile(SysTable {
19 pcmd_buffer: SYS_CMD_BUF.as_mut_ptr(), 19 pcmd_buffer: SYS_CMD_BUF.as_mut_ptr(),
20 sys_queue: SYSTEM_EVT_QUEUE.as_ptr(), 20 sys_queue: SYSTEM_EVT_QUEUE.as_ptr(),
21 }) 21 })
@@ -51,12 +51,14 @@ impl Sys {
51 let node_ptr_ptr: *mut _ = &mut node_ptr; 51 let node_ptr_ptr: *mut _ = &mut node_ptr;
52 52
53 while !LinkedListNode::is_empty(SYSTEM_EVT_QUEUE.as_mut_ptr()) { 53 while !LinkedListNode::is_empty(SYSTEM_EVT_QUEUE.as_mut_ptr()) {
54 LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr(), node_ptr_ptr); 54 LinkedListNode::get_next_node(SYSTEM_EVT_QUEUE.as_mut_ptr(), node_ptr_ptr);
55 55
56 let event = node_ptr.cast(); 56 let event = node_ptr.cast();
57 let event = EvtBox::new(event); 57 let event = EvtBox::new(event);
58 58
59 EVT_CHANNEL.try_send(event).unwrap(); 59 EVT_CHANNEL.try_send(event).unwrap();
60
61 break;
60 } 62 }
61 } 63 }
62 64
diff --git a/embassy-stm32-wpan/src/unsafe_linked_list.rs b/embassy-stm32-wpan/src/unsafe_linked_list.rs
index 52c106fa2..95f4bef44 100644
--- a/embassy-stm32-wpan/src/unsafe_linked_list.rs
+++ b/embassy-stm32-wpan/src/unsafe_linked_list.rs
@@ -11,6 +11,8 @@
11 unused_mut 11 unused_mut
12)] 12)]
13 13
14use core::ptr;
15
14use cortex_m::interrupt; 16use cortex_m::interrupt;
15 17
16#[derive(Copy, Clone)] 18#[derive(Copy, Clone)]
@@ -30,54 +32,102 @@ impl Default for LinkedListNode {
30} 32}
31 33
32impl LinkedListNode { 34impl LinkedListNode {
33 pub unsafe fn init_head(mut list_head: *mut LinkedListNode) { 35 pub unsafe fn init_head(mut p_list_head: *mut LinkedListNode) {
34 (*list_head).next = list_head; 36 ptr::write_volatile(
35 (*list_head).prev = list_head; 37 p_list_head,
38 LinkedListNode {
39 next: p_list_head,
40 prev: p_list_head,
41 },
42 );
36 } 43 }
37 44
38 pub unsafe fn is_empty(mut list_head: *mut LinkedListNode) -> bool { 45 pub unsafe fn is_empty(mut p_list_head: *mut LinkedListNode) -> bool {
39 interrupt::free(|_| ((*list_head).next) == list_head) 46 interrupt::free(|_| ptr::read_volatile(p_list_head).next == p_list_head)
40 } 47 }
41 48
42 pub unsafe fn insert_head(mut list_head: *mut LinkedListNode, mut node: *mut LinkedListNode) { 49 /// Insert `node` after `list_head` and before the next node
50 pub unsafe fn insert_head(mut p_list_head: *mut LinkedListNode, mut p_node: *mut LinkedListNode) {
43 interrupt::free(|_| { 51 interrupt::free(|_| {
44 (*node).next = (*list_head).next; 52 let mut list_head = ptr::read_volatile(p_list_head);
45 (*node).prev = list_head; 53 let mut node_next = ptr::read_volatile(list_head.next);
46 (*list_head).next = node; 54 let node = LinkedListNode {
47 (*(*node).next).prev = node; 55 next: list_head.next,
56 prev: p_list_head,
57 };
58
59 list_head.next = p_node;
60 node_next.prev = p_node;
61
62 // All nodes must be written because they will all be seen by another core
63 ptr::write_volatile(p_node, node);
64 ptr::write_volatile(node.next, node_next);
65 ptr::write_volatile(p_list_head, list_head);
48 }); 66 });
49 } 67 }
50 68
51 pub unsafe fn insert_tail(mut list_head: *mut LinkedListNode, mut node: *mut LinkedListNode) { 69 /// Insert `node` before `list_tail` and after the second-to-last node
70 pub unsafe fn insert_tail(mut p_list_tail: *mut LinkedListNode, mut p_node: *mut LinkedListNode) {
52 interrupt::free(|_| { 71 interrupt::free(|_| {
53 (*node).next = list_head; 72 let mut list_tail = ptr::read_volatile(p_list_tail);
54 (*node).prev = (*list_head).prev; 73 let mut node_prev = ptr::read_volatile(list_tail.prev);
55 (*list_head).prev = node; 74 let node = LinkedListNode {
56 (*(*node).prev).next = node; 75 next: p_list_tail,
76 prev: list_tail.prev,
77 };
78
79 list_tail.prev = p_node;
80 node_prev.next = p_node;
81
82 // All nodes must be written because they will all be seen by another core
83 ptr::write_volatile(p_node, node);
84 ptr::write_volatile(node.prev, node_prev);
85 ptr::write_volatile(p_list_tail, list_tail);
57 }); 86 });
58 } 87 }
59 88
60 /// Remove `node` from the linked list 89 /// Remove `node` from the linked list
61 pub unsafe fn remove_node(mut node: *mut LinkedListNode) { 90 // pub unsafe fn remove_node(mut node: *mut LinkedListNode) {
91 // interrupt::free(|_| {
92 // (*(*node).prev).next = (*node).next;
93 // (*(*node).next).prev = (*node).prev;
94 // });
95 // }
96
97 /// Remove `node` from the linked list
98 pub unsafe fn remove_node(mut p_node: *mut LinkedListNode) {
62 interrupt::free(|_| { 99 interrupt::free(|_| {
63 (*(*node).prev).next = (*node).next; 100 let node = ptr::read_volatile(p_node);
64 (*(*node).next).prev = (*node).prev; 101 let mut node_prev = ptr::read_volatile(node.prev);
102 let mut node_next = ptr::read_volatile(node.next);
103
104 node_prev.next = node.next;
105 node_next.prev = node.prev;
106
107 ptr::write_volatile(node.prev, node_prev);
108 ptr::write_volatile(node.next, node_next);
65 }); 109 });
66 } 110 }
67 111
68 /// Remove `list_head` into `node` 112 /// Remove `list_head` into `node`
69 pub unsafe fn remove_head(mut list_head: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) { 113 pub unsafe fn remove_head(mut p_list_head: *mut LinkedListNode, mut p_node: *mut *mut LinkedListNode) {
70 interrupt::free(|_| { 114 interrupt::free(|_| {
71 *node = (*list_head).next; 115 let list_head = ptr::read_volatile(p_list_head);
72 Self::remove_node((*list_head).next); 116
117 // Allowed because a removed node is not seen by another core
118 *p_node = list_head.next;
119 Self::remove_node(list_head.next);
73 }); 120 });
74 } 121 }
75 122
76 /// Remove `list_tail` into `node` 123 /// Remove `list_tail` into `node`
77 pub unsafe fn remove_tail(mut list_tail: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) { 124 pub unsafe fn remove_tail(mut p_list_tail: *mut LinkedListNode, mut p_node: *mut *mut LinkedListNode) {
78 interrupt::free(|_| { 125 interrupt::free(|_| {
79 *node = (*list_tail).prev; 126 let list_tail = ptr::read_volatile(p_list_tail);
80 Self::remove_node((*list_tail).prev); 127
128 // Allowed because a removed node is not seen by another core
129 *p_node = list_tail.prev;
130 Self::remove_node(list_tail.prev);
81 }); 131 });
82 } 132 }
83 133
@@ -114,9 +164,12 @@ impl LinkedListNode {
114 }) 164 })
115 } 165 }
116 166
117 pub unsafe fn get_next_node(mut ref_node: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) { 167 pub unsafe fn get_next_node(mut p_ref_node: *mut LinkedListNode, mut p_node: *mut *mut LinkedListNode) {
118 interrupt::free(|_| { 168 interrupt::free(|_| {
119 *node = (*ref_node).next; 169 let ref_node = ptr::read_volatile(p_ref_node);
170
171 // Allowed because a removed node is not seen by another core
172 *p_node = ref_node.next;
120 }); 173 });
121 } 174 }
122 175
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml
index dcc7d9b8a..487ef4626 100644
--- a/tests/stm32/Cargo.toml
+++ b/tests/stm32/Cargo.toml
@@ -12,7 +12,7 @@ stm32g071rb = ["embassy-stm32/stm32g071rb", "not-gpdma"] # Nucleo
12stm32c031c6 = ["embassy-stm32/stm32c031c6", "not-gpdma"] # Nucleo 12stm32c031c6 = ["embassy-stm32/stm32c031c6", "not-gpdma"] # Nucleo
13stm32g491re = ["embassy-stm32/stm32g491re", "not-gpdma"] # Nucleo 13stm32g491re = ["embassy-stm32/stm32g491re", "not-gpdma"] # Nucleo
14stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "not-gpdma"] # Nucleo 14stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "not-gpdma"] # Nucleo
15stm32wb55rg = ["embassy-stm32/stm32wb55rg", "not-gpdma" ] # Nucleo 15stm32wb55rg = ["embassy-stm32/stm32wb55rg", "not-gpdma", "ble" ] # Nucleo
16stm32h563zi = ["embassy-stm32/stm32h563zi"] # Nucleo 16stm32h563zi = ["embassy-stm32/stm32h563zi"] # Nucleo
17stm32u585ai = ["embassy-stm32/stm32u585ai"] # IoT board 17stm32u585ai = ["embassy-stm32/stm32u585ai"] # IoT board
18 18
diff --git a/tests/stm32/src/bin/tl_mbox.rs b/tests/stm32/src/bin/tl_mbox.rs
index b57519a54..26d9cc68e 100644
--- a/tests/stm32/src/bin/tl_mbox.rs
+++ b/tests/stm32/src/bin/tl_mbox.rs
@@ -11,6 +11,7 @@ use embassy_executor::Spawner;
11use embassy_stm32::bind_interrupts; 11use embassy_stm32::bind_interrupts;
12use embassy_stm32::ipcc::Config; 12use embassy_stm32::ipcc::Config;
13use embassy_stm32_wpan::TlMbox; 13use embassy_stm32_wpan::TlMbox;
14use embassy_stm32_wpan::rc::RadioCoprocessor;
14use embassy_time::{Duration, Timer}; 15use embassy_time::{Duration, Timer};
15 16
16bind_interrupts!(struct Irqs{ 17bind_interrupts!(struct Irqs{
@@ -50,14 +51,14 @@ async fn main(_spawner: Spawner) {
50 Timer::after(Duration::from_millis(50)).await; 51 Timer::after(Duration::from_millis(50)).await;
51 } 52 }
52 53
53 // let mut rc = RadioCoprocessor::new(mbox); 54 let mut rc = RadioCoprocessor::new(mbox);
54 // 55
55 // let response = rc.read().await; 56 let response = rc.read().await;
56 // info!("coprocessor ready {}", response); 57 info!("coprocessor ready {}", response);
57 // 58
58 // rc.write(&[0x01, 0x03, 0x0c, 0x00, 0x00]); 59 rc.write(&[0x01, 0x03, 0x0c, 0x00, 0x00]);
59 // let response = rc.read().await; 60 let response = rc.read().await;
60 // info!("ble reset rsp {}", response); 61 info!("ble reset rsp {}", response);
61 62
62 info!("Test OK"); 63 info!("Test OK");
63 cortex_m::asm::bkpt(); 64 cortex_m::asm::bkpt();