aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rpi-pico-w/Cargo.toml2
-rw-r--r--examples/rpi-pico-w/src/main.rs60
-rw-r--r--examples/rpi-pico-w/src/pio.rs61
3 files changed, 40 insertions, 83 deletions
diff --git a/examples/rpi-pico-w/Cargo.toml b/examples/rpi-pico-w/Cargo.toml
index 0d789a932..17b4214d8 100644
--- a/examples/rpi-pico-w/Cargo.toml
+++ b/examples/rpi-pico-w/Cargo.toml
@@ -47,8 +47,6 @@ futures = { version = "0.3.17", default-features = false, features = [
47pio-proc = "0.2" 47pio-proc = "0.2"
48pio = "0.2.1" 48pio = "0.2.1"
49 49
50embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" }
51embedded-hal-async = { version = "0.2.0-alpha.0" }
52embedded-io = { version = "0.4.0", features = ["async", "defmt"] } 50embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
53heapless = "0.7.15" 51heapless = "0.7.15"
54 52
diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/main.rs
index 3563d165a..f30a20bac 100644
--- a/examples/rpi-pico-w/src/main.rs
+++ b/examples/rpi-pico-w/src/main.rs
@@ -1,4 +1,4 @@
1#![no_std] 1#![no_std]
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4#![feature(async_fn_in_trait)] 4#![feature(async_fn_in_trait)]
@@ -6,7 +6,7 @@
6 6
7mod pio; 7mod pio;
8 8
9use core::convert::Infallible; 9use core::slice;
10use core::str::from_utf8; 10use core::str::from_utf8;
11 11
12use defmt::*; 12use defmt::*;
@@ -16,8 +16,6 @@ use embassy_net::{Config, Stack, StackResources};
16use embassy_rp::gpio::{Flex, Level, Output}; 16use embassy_rp::gpio::{Flex, Level, Output};
17use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_24, PIN_25, PIN_29}; 17use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_24, PIN_25, PIN_29};
18use embassy_rp::pio::{Pio0, PioPeripherial, PioStateMachineInstance, Sm0}; 18use embassy_rp::pio::{Pio0, PioPeripherial, PioStateMachineInstance, Sm0};
19use embedded_hal_1::spi::ErrorType;
20use embedded_hal_async::spi::{ExclusiveDevice, SpiBusFlush, SpiBusRead, SpiBusWrite};
21use embedded_io::asynch::Write; 19use embedded_io::asynch::Write;
22use static_cell::StaticCell; 20use static_cell::StaticCell;
23use {defmt_rtt as _, panic_probe as _}; 21use {defmt_rtt as _, panic_probe as _};
@@ -37,7 +35,7 @@ async fn wifi_task(
37 runner: cyw43::Runner< 35 runner: cyw43::Runner<
38 'static, 36 'static,
39 Output<'static, PIN_23>, 37 Output<'static, PIN_23>,
40 ExclusiveDevice<PioSpi<PioStateMachineInstance<Pio0, Sm0>, DMA_CH0>, Output<'static, PIN_25>>, 38 PioSpi<PIN_25, PioStateMachineInstance<Pio0, Sm0>, DMA_CH0>,
41 >, 39 >,
42) -> ! { 40) -> ! {
43 runner.run().await 41 runner.run().await
@@ -75,8 +73,7 @@ async fn main(spawner: Spawner) {
75 73
76 let (_, sm, _, _, _) = p.PIO0.split(); 74 let (_, sm, _, _, _) = p.PIO0.split();
77 let dma = p.DMA_CH0; 75 let dma = p.DMA_CH0;
78 let bus = PioSpi::new(sm, p.PIN_24, p.PIN_29, dma); 76 let spi = PioSpi::new(sm, cs, p.PIN_24, p.PIN_29, dma);
79 let spi = ExclusiveDevice::new(bus, cs);
80 77
81 let state = singleton!(cyw43::State::new()); 78 let state = singleton!(cyw43::State::new());
82 let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; 79 let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
@@ -146,7 +143,6 @@ async fn main(spawner: Spawner) {
146 143
147 info!("rxd {}", from_utf8(&buf[..n]).unwrap()); 144 info!("rxd {}", from_utf8(&buf[..n]).unwrap());
148 145
149
150 match socket.write_all(&buf[..n]).await { 146 match socket.write_all(&buf[..n]).await {
151 Ok(()) => {} 147 Ok(()) => {}
152 Err(e) => { 148 Err(e) => {
@@ -168,31 +164,13 @@ struct MySpi {
168 /// - IRQ 164 /// - IRQ
169 /// - strap to set to gSPI mode on boot. 165 /// - strap to set to gSPI mode on boot.
170 dio: Flex<'static, PIN_24>, 166 dio: Flex<'static, PIN_24>,
171}
172 167
173impl ErrorType for MySpi { 168 /// Chip select
174 type Error = Infallible; 169 cs: Output<'static, PIN_25>,
175} 170}
176 171
177impl cyw43::SpiBusCyw43<u32> for MySpi { 172impl MySpi {
178 async fn cmd_write<'a>(&'a mut self, write: &'a [u32]) -> Result<(), Self::Error> { 173 async fn read(&mut self, words: &mut [u32]) {
179 self.write(write).await
180 }
181
182 async fn cmd_read<'a>(&'a mut self, write: &'a [u32], read: &'a mut [u32]) -> Result<(), Self::Error> {
183 self.write(write).await?;
184 self.read(read).await
185 }
186}
187
188impl SpiBusFlush for MySpi {
189 async fn flush(&mut self) -> Result<(), Self::Error> {
190 Ok(())
191 }
192}
193
194impl SpiBusRead<u32> for MySpi {
195 async fn read(&mut self, words: &mut [u32]) -> Result<(), Self::Error> {
196 self.dio.set_as_input(); 174 self.dio.set_as_input();
197 for word in words { 175 for word in words {
198 let mut w = 0; 176 let mut w = 0;
@@ -210,13 +188,9 @@ impl SpiBusRead<u32> for MySpi {
210 } 188 }
211 *word = w 189 *word = w
212 } 190 }
213
214 Ok(())
215 } 191 }
216}
217 192
218impl SpiBusWrite<u32> for MySpi { 193 async fn write(&mut self, words: &[u32]) {
219 async fn write(&mut self, words: &[u32]) -> Result<(), Self::Error> {
220 self.dio.set_as_output(); 194 self.dio.set_as_output();
221 for word in words { 195 for word in words {
222 let mut word = *word; 196 let mut word = *word;
@@ -238,6 +212,20 @@ impl SpiBusWrite<u32> for MySpi {
238 self.clk.set_low(); 212 self.clk.set_low();
239 213
240 self.dio.set_as_input(); 214 self.dio.set_as_input();
241 Ok(()) 215 }
216}
217
218impl cyw43::SpiBusCyw43 for MySpi {
219 async fn cmd_write(&mut self, write: &[u32]) {
220 self.cs.set_low();
221 self.write(write).await;
222 self.cs.set_high();
223 }
224
225 async fn cmd_read(&mut self, write: u32, read: &mut [u32]) {
226 self.cs.set_low();
227 self.write(slice::from_ref(&write)).await;
228 self.read(read).await;
229 self.cs.set_high();
242 } 230 }
243} 231}
diff --git a/examples/rpi-pico-w/src/pio.rs b/examples/rpi-pico-w/src/pio.rs
index 1bf304d5d..896fd0457 100644
--- a/examples/rpi-pico-w/src/pio.rs
+++ b/examples/rpi-pico-w/src/pio.rs
@@ -2,34 +2,27 @@ use core::slice;
2 2
3use cyw43::SpiBusCyw43; 3use cyw43::SpiBusCyw43;
4use embassy_rp::dma::Channel; 4use embassy_rp::dma::Channel;
5use embassy_rp::gpio::{Drive, Pin, Pull, SlewRate}; 5use embassy_rp::gpio::{Drive, Output, Pin, Pull, SlewRate};
6use embassy_rp::pio::{PioStateMachine, ShiftDirection}; 6use embassy_rp::pio::{PioStateMachine, ShiftDirection};
7use embassy_rp::relocate::RelocatedProgram; 7use embassy_rp::relocate::RelocatedProgram;
8use embassy_rp::{pio_instr_util, Peripheral}; 8use embassy_rp::{pio_instr_util, Peripheral};
9use embedded_hal_1::spi::ErrorType;
10use embedded_hal_async::spi::SpiBusFlush;
11use pio::Wrap; 9use pio::Wrap;
12use pio_proc::pio_asm; 10use pio_proc::pio_asm;
13 11
14pub struct PioSpi<SM, DMA> { 12pub struct PioSpi<CS: Pin, SM, DMA> {
15 // cs: Output<'static, AnyPin>, 13 cs: Output<'static, CS>,
16 sm: SM, 14 sm: SM,
17 dma: DMA, 15 dma: DMA,
18 wrap_target: u8, 16 wrap_target: u8,
19} 17}
20 18
21impl<SM, DMA> PioSpi<SM, DMA> 19impl<CS, SM, DMA> PioSpi<CS, SM, DMA>
22where 20where
23 SM: PioStateMachine, 21 SM: PioStateMachine,
24 DMA: Channel, 22 DMA: Channel,
23 CS: Pin,
25{ 24{
26 pub fn new<DIO, CLK>( 25 pub fn new<DIO, CLK>(mut sm: SM, cs: Output<'static, CS>, dio: DIO, clk: CLK, dma: DMA) -> Self
27 mut sm: SM,
28 // cs: AnyPin,
29 dio: DIO,
30 clk: CLK,
31 dma: DMA,
32 ) -> Self
33 where 26 where
34 DIO: Pin, 27 DIO: Pin,
35 CLK: Pin, 28 CLK: Pin,
@@ -105,7 +98,7 @@ where
105 pio_instr_util::set_pin(&mut sm, 0); 98 pio_instr_util::set_pin(&mut sm, 0);
106 99
107 Self { 100 Self {
108 // cs: Output::new(cs, Level::High), 101 cs,
109 sm, 102 sm,
110 dma, 103 dma,
111 wrap_target: target, 104 wrap_target: target,
@@ -156,43 +149,21 @@ where
156 } 149 }
157} 150}
158 151
159#[derive(Debug)] 152impl<CS, SM, DMA> SpiBusCyw43 for PioSpi<CS, SM, DMA>
160pub enum PioError {}
161
162impl embedded_hal_async::spi::Error for PioError {
163 fn kind(&self) -> embedded_hal_1::spi::ErrorKind {
164 embedded_hal_1::spi::ErrorKind::Other
165 }
166}
167
168impl<SM, DMA> ErrorType for PioSpi<SM, DMA>
169where
170 SM: PioStateMachine,
171{
172 type Error = PioError;
173}
174
175impl<SM, DMA> SpiBusFlush for PioSpi<SM, DMA>
176where
177 SM: PioStateMachine,
178{
179 async fn flush(&mut self) -> Result<(), Self::Error> {
180 Ok(())
181 }
182}
183
184impl<SM, DMA> SpiBusCyw43<u32> for PioSpi<SM, DMA>
185where 153where
154 CS: Pin,
186 SM: PioStateMachine, 155 SM: PioStateMachine,
187 DMA: Channel, 156 DMA: Channel,
188{ 157{
189 async fn cmd_write<'a>(&'a mut self, write: &'a [u32]) -> Result<(), Self::Error> { 158 async fn cmd_write(&mut self, write: & [u32]) {
159 self.cs.set_low();
190 self.write(write).await; 160 self.write(write).await;
191 Ok(()) 161 self.cs.set_high();
192 } 162 }
193 163
194 async fn cmd_read<'a>(&'a mut self, write: &'a [u32], read: &'a mut [u32]) -> Result<(), Self::Error> { 164 async fn cmd_read(&mut self, write: u32, read: & mut [u32]) {
195 self.cmd_read(write[0], read).await; 165 self.cs.set_low();
196 Ok(()) 166 self.cmd_read(write, read).await;
167 self.cs.set_high();
197 } 168 }
198} 169}