aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkbleeke <[email protected]>2023-03-27 17:24:45 +0200
committerkbleeke <[email protected]>2023-03-27 18:19:07 +0200
commit056df98d475c3be307b7c9c3038e02b4ef79fa08 (patch)
tree3f90f3c52ea2e61eb50c6dc206c2c72a767eb714 /src
parent273e6f5b835afe9883a37814a76caba2084d6dc2 (diff)
use send status feature of cyw43 instead of manually checking status
Diffstat (limited to 'src')
-rw-r--r--src/bus.rs33
-rw-r--r--src/consts.rs1
-rw-r--r--src/runner.rs5
3 files changed, 23 insertions, 16 deletions
diff --git a/src/bus.rs b/src/bus.rs
index d2a249f97..65caea8ec 100644
--- a/src/bus.rs
+++ b/src/bus.rs
@@ -12,14 +12,14 @@ use crate::consts::*;
12pub trait SpiBusCyw43 { 12pub trait SpiBusCyw43 {
13 /// Issues a write command on the bus 13 /// Issues a write command on the bus
14 /// First 32 bits of `word` are expected to be a cmd word 14 /// First 32 bits of `word` are expected to be a cmd word
15 async fn cmd_write(&mut self, write: &[u32]); 15 async fn cmd_write(&mut self, write: &[u32]) -> u32;
16 16
17 /// Issues a read command on the bus 17 /// Issues a read command on the bus
18 /// `write` is expected to be a 32 bit cmd word 18 /// `write` is expected to be a 32 bit cmd word
19 /// `read` will contain the response of the device 19 /// `read` will contain the response of the device
20 /// Backplane reads have a response delay that produces one extra unspecified word at the beginning of `read`. 20 /// Backplane reads have a response delay that produces one extra unspecified word at the beginning of `read`.
21 /// Callers that want to read `n` word from the backplane, have to provide a slice that is `n+1` words long. 21 /// Callers that want to read `n` word from the backplane, have to provide a slice that is `n+1` words long.
22 async fn cmd_read(&mut self, write: u32, read: &mut [u32]); 22 async fn cmd_read(&mut self, write: u32, read: &mut [u32]) -> u32;
23 23
24 /// Wait for events from the Device. A typical implementation would wait for the IRQ pin to be high. 24 /// Wait for events from the Device. A typical implementation would wait for the IRQ pin to be high.
25 /// The default implementation always reports ready, resulting in active polling of the device. 25 /// The default implementation always reports ready, resulting in active polling of the device.
@@ -32,6 +32,7 @@ pub(crate) struct Bus<PWR, SPI> {
32 backplane_window: u32, 32 backplane_window: u32,
33 pwr: PWR, 33 pwr: PWR,
34 spi: SPI, 34 spi: SPI,
35 status: u32,
35} 36}
36 37
37impl<PWR, SPI> Bus<PWR, SPI> 38impl<PWR, SPI> Bus<PWR, SPI>
@@ -44,6 +45,7 @@ where
44 backplane_window: 0xAAAA_AAAA, 45 backplane_window: 0xAAAA_AAAA,
45 pwr, 46 pwr,
46 spi, 47 spi,
48 status: 0,
47 } 49 }
48 } 50 }
49 51
@@ -70,8 +72,11 @@ where
70 trace!("{:#010b}", (val & 0xff)); 72 trace!("{:#010b}", (val & 0xff));
71 73
72 // 32-bit word length, little endian (which is the default endianess). 74 // 32-bit word length, little endian (which is the default endianess).
73 self.write32_swapped(REG_BUS_CTRL, WORD_LENGTH_32 | HIGH_SPEED | INTERRUPT_HIGH | WAKE_UP) 75 self.write32_swapped(
74 .await; 76 REG_BUS_CTRL,
77 WORD_LENGTH_32 | HIGH_SPEED | INTERRUPT_HIGH | WAKE_UP | STATUS_ENABLE,
78 )
79 .await;
75 80
76 let val = self.read8(FUNC_BUS, REG_BUS_CTRL).await; 81 let val = self.read8(FUNC_BUS, REG_BUS_CTRL).await;
77 trace!("{:#b}", val); 82 trace!("{:#b}", val);
@@ -88,7 +93,7 @@ where
88 let cmd = cmd_word(READ, INC_ADDR, FUNC_WLAN, 0, len_in_u8); 93 let cmd = cmd_word(READ, INC_ADDR, FUNC_WLAN, 0, len_in_u8);
89 let len_in_u32 = (len_in_u8 as usize + 3) / 4; 94 let len_in_u32 = (len_in_u8 as usize + 3) / 4;
90 95
91 self.spi.cmd_read(cmd, &mut buf[..len_in_u32]).await; 96 self.status = self.spi.cmd_read(cmd, &mut buf[..len_in_u32]).await;
92 } 97 }
93 98
94 pub async fn wlan_write(&mut self, buf: &[u32]) { 99 pub async fn wlan_write(&mut self, buf: &[u32]) {
@@ -98,7 +103,7 @@ where
98 cmd_buf[0] = cmd; 103 cmd_buf[0] = cmd;
99 cmd_buf[1..][..buf.len()].copy_from_slice(buf); 104 cmd_buf[1..][..buf.len()].copy_from_slice(buf);
100 105
101 self.spi.cmd_write(&cmd_buf).await; 106 self.status = self.spi.cmd_write(&cmd_buf).await;
102 } 107 }
103 108
104 #[allow(unused)] 109 #[allow(unused)]
@@ -124,7 +129,7 @@ where
124 let cmd = cmd_word(READ, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32); 129 let cmd = cmd_word(READ, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
125 130
126 // round `buf` to word boundary, add one extra word for the response delay 131 // round `buf` to word boundary, add one extra word for the response delay
127 self.spi.cmd_read(cmd, &mut buf[..(len + 3) / 4 + 1]).await; 132 self.status = self.spi.cmd_read(cmd, &mut buf[..(len + 3) / 4 + 1]).await;
128 133
129 // when writing out the data, we skip the response-delay byte 134 // when writing out the data, we skip the response-delay byte
130 data[..len].copy_from_slice(&slice8_mut(&mut buf[1..])[..len]); 135 data[..len].copy_from_slice(&slice8_mut(&mut buf[1..])[..len]);
@@ -157,7 +162,7 @@ where
157 let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32); 162 let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
158 buf[0] = cmd; 163 buf[0] = cmd;
159 164
160 self.spi.cmd_write(&buf[..(len + 3) / 4 + 1]).await; 165 self.status = self.spi.cmd_write(&buf[..(len + 3) / 4 + 1]).await;
161 166
162 // Advance ptr. 167 // Advance ptr.
163 addr += len as u32; 168 addr += len as u32;
@@ -273,7 +278,7 @@ where
273 // if we are reading from the backplane, we need an extra word for the response delay 278 // if we are reading from the backplane, we need an extra word for the response delay
274 let len = if func == FUNC_BACKPLANE { 2 } else { 1 }; 279 let len = if func == FUNC_BACKPLANE { 2 } else { 1 };
275 280
276 self.spi.cmd_read(cmd, &mut buf[..len]).await; 281 self.status = self.spi.cmd_read(cmd, &mut buf[..len]).await;
277 282
278 // if we read from the backplane, the result is in the second word, after the response delay 283 // if we read from the backplane, the result is in the second word, after the response delay
279 if func == FUNC_BACKPLANE { 284 if func == FUNC_BACKPLANE {
@@ -286,7 +291,7 @@ where
286 async fn writen(&mut self, func: u32, addr: u32, val: u32, len: u32) { 291 async fn writen(&mut self, func: u32, addr: u32, val: u32, len: u32) {
287 let cmd = cmd_word(WRITE, INC_ADDR, func, addr, len); 292 let cmd = cmd_word(WRITE, INC_ADDR, func, addr, len);
288 293
289 self.spi.cmd_write(&[cmd, val]).await; 294 self.status = self.spi.cmd_write(&[cmd, val]).await;
290 } 295 }
291 296
292 async fn read32_swapped(&mut self, addr: u32) -> u32 { 297 async fn read32_swapped(&mut self, addr: u32) -> u32 {
@@ -294,7 +299,7 @@ where
294 let cmd = swap16(cmd); 299 let cmd = swap16(cmd);
295 let mut buf = [0; 1]; 300 let mut buf = [0; 1];
296 301
297 self.spi.cmd_read(cmd, &mut buf).await; 302 self.status = self.spi.cmd_read(cmd, &mut buf).await;
298 303
299 swap16(buf[0]) 304 swap16(buf[0])
300 } 305 }
@@ -303,12 +308,16 @@ where
303 let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BUS, addr, 4); 308 let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BUS, addr, 4);
304 let buf = [swap16(cmd), swap16(val)]; 309 let buf = [swap16(cmd), swap16(val)];
305 310
306 self.spi.cmd_write(&buf).await; 311 self.status = self.spi.cmd_write(&buf).await;
307 } 312 }
308 313
309 pub async fn wait_for_event(&mut self) { 314 pub async fn wait_for_event(&mut self) {
310 self.spi.wait_for_event().await; 315 self.spi.wait_for_event().await;
311 } 316 }
317
318 pub fn status(&self) -> u32 {
319 self.status
320 }
312} 321}
313 322
314fn swap16(x: u32) -> u32 { 323fn swap16(x: u32) -> u32 {
diff --git a/src/consts.rs b/src/consts.rs
index 70d6660e0..6ed7feb92 100644
--- a/src/consts.rs
+++ b/src/consts.rs
@@ -16,6 +16,7 @@ pub(crate) const WORD_LENGTH_32: u32 = 0x1;
16pub(crate) const HIGH_SPEED: u32 = 0x10; 16pub(crate) const HIGH_SPEED: u32 = 0x10;
17pub(crate) const INTERRUPT_HIGH: u32 = 1 << 5; 17pub(crate) const INTERRUPT_HIGH: u32 = 1 << 5;
18pub(crate) const WAKE_UP: u32 = 1 << 7; 18pub(crate) const WAKE_UP: u32 = 1 << 7;
19pub(crate) const STATUS_ENABLE: u32 = 0x10000;
19 20
20// SPI_STATUS_REGISTER bits 21// SPI_STATUS_REGISTER bits
21pub(crate) const STATUS_DATA_NOT_AVAILABLE: u32 = 0x00000001; 22pub(crate) const STATUS_DATA_NOT_AVAILABLE: u32 = 0x00000001;
diff --git a/src/runner.rs b/src/runner.rs
index abfac3ae3..ccdbbf1ac 100644
--- a/src/runner.rs
+++ b/src/runner.rs
@@ -315,10 +315,7 @@ where
315 /// Handle F2 events while status register is set 315 /// Handle F2 events while status register is set
316 async fn check_status(&mut self, buf: &mut [u32; 512]) { 316 async fn check_status(&mut self, buf: &mut [u32; 512]) {
317 loop { 317 loop {
318 let mut status = 0xFFFF_FFFF; 318 let status = self.bus.status();
319 while status == 0xFFFF_FFFF {
320 status = self.bus.read32(FUNC_BUS, REG_BUS_STATUS).await;
321 }
322 trace!("check status{}", FormatStatus(status)); 319 trace!("check status{}", FormatStatus(status));
323 320
324 if status & STATUS_F2_PKT_AVAILABLE != 0 { 321 if status & STATUS_F2_PKT_AVAILABLE != 0 {