aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-07-15 18:33:32 +0200
committerDario Nieuwenhuis <[email protected]>2022-07-16 08:26:54 +0200
commit7dfdea87971bf7a951b2b1d3fc2e50e245005d07 (patch)
tree64fc113c9a6e75a65f59457bbf995ebc82182be5 /examples
parent31410aa5b7d570184b0abcb78d04654d939660db (diff)
Switch to embedded-hal SPI, GPIO traits.
Diffstat (limited to 'examples')
-rw-r--r--examples/rpi-pico-w/src/main.rs125
1 files changed, 109 insertions, 16 deletions
diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/main.rs
index e08ee8e95..655535f9d 100644
--- a/examples/rpi-pico-w/src/main.rs
+++ b/examples/rpi-pico-w/src/main.rs
@@ -1,8 +1,9 @@
1#![no_std] 1#![no_std]
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait, concat_bytes)] 3#![feature(generic_associated_types, type_alias_impl_trait)]
4 4
5use core::slice; 5use core::convert::Infallible;
6use core::future::Future;
6 7
7use defmt::{assert, assert_eq, panic, *}; 8use defmt::{assert, assert_eq, panic, *};
8use embassy::executor::Spawner; 9use embassy::executor::Spawner;
@@ -13,8 +14,9 @@ use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
13use embassy_rp::gpio::{Flex, Level, Output, Pin}; 14use embassy_rp::gpio::{Flex, Level, Output, Pin};
14use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29}; 15use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29};
15use embassy_rp::Peripherals; 16use embassy_rp::Peripherals;
17use embedded_hal_1::spi::ErrorType;
18use embedded_hal_async::spi::{ExclusiveDevice, SpiBusFlush, SpiBusRead, SpiBusWrite};
16use embedded_io::asynch::{Read, Write}; 19use embedded_io::asynch::{Read, Write};
17use heapless::Vec;
18use {defmt_rtt as _, panic_probe as _}; 20use {defmt_rtt as _, panic_probe as _};
19 21
20macro_rules! forever { 22macro_rules! forever {
@@ -26,7 +28,9 @@ macro_rules! forever {
26} 28}
27 29
28#[embassy::task] 30#[embassy::task]
29async fn wifi_task(runner: cyw43::Runner<'static, PIN_23, PIN_25, PIN_29, PIN_24>) -> ! { 31async fn wifi_task(
32 runner: cyw43::Runner<'static, Output<'static, PIN_23>, ExclusiveDevice<MySpi, Output<'static, PIN_25>>>,
33) -> ! {
30 runner.run().await 34 runner.run().await
31} 35}
32 36
@@ -39,25 +43,25 @@ async fn net_task(stack: &'static Stack<cyw43::NetDevice<'static>>) -> ! {
39async fn main(spawner: Spawner, p: Peripherals) { 43async fn main(spawner: Spawner, p: Peripherals) {
40 info!("Hello World!"); 44 info!("Hello World!");
41 45
42 let (pwr, cs, clk, dio) = (p.PIN_23, p.PIN_25, p.PIN_29, p.PIN_24); 46 let pwr = Output::new(p.PIN_23, Level::Low);
43 //let (pwr, cs, clk, dio) = (p.PIN_23, p.PIN_0, p.PIN_1, p.PIN_2); 47 let cs = Output::new(p.PIN_25, Level::High);
48 let clk = Output::new(p.PIN_29, Level::Low);
49 let mut dio = Flex::new(p.PIN_24);
50 dio.set_low();
51 dio.set_as_output();
52
53 let bus = MySpi { clk, dio };
54 let spi = ExclusiveDevice::new(bus, cs);
44 55
45 let state = forever!(cyw43::State::new()); 56 let state = forever!(cyw43::State::new());
46 let (mut control, runner) = cyw43::new( 57 let (mut control, runner) = cyw43::new(state, pwr, spi).await;
47 state,
48 Output::new(pwr, Level::Low),
49 Output::new(cs, Level::High),
50 Output::new(clk, Level::Low),
51 Flex::new(dio),
52 )
53 .await;
54 58
55 spawner.spawn(wifi_task(runner)).unwrap(); 59 spawner.spawn(wifi_task(runner)).unwrap();
56 60
57 let net_device = control.init().await; 61 let net_device = control.init().await;
58 62
59 control.join_open("MikroTik-951589").await; 63 //control.join_open("MikroTik-951589").await;
60 //control.join_wpa2("MikroTik-951589", "asdfasdfasdfasdf").await; 64 control.join_wpa2("DirbaioWifi", "HelloWorld").await;
61 65
62 let config = embassy_net::ConfigStrategy::Dhcp; 66 let config = embassy_net::ConfigStrategy::Dhcp;
63 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { 67 //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config {
@@ -122,3 +126,92 @@ async fn main(spawner: Spawner, p: Peripherals) {
122 } 126 }
123 } 127 }
124} 128}
129
130struct MySpi {
131 /// SPI clock
132 clk: Output<'static, PIN_29>,
133
134 /// 4 signals, all in one!!
135 /// - SPI MISO
136 /// - SPI MOSI
137 /// - IRQ
138 /// - strap to set to gSPI mode on boot.
139 dio: Flex<'static, PIN_24>,
140}
141
142impl ErrorType for MySpi {
143 type Error = Infallible;
144}
145
146impl SpiBusFlush for MySpi {
147 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
148 where
149 Self: 'a;
150
151 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
152 async move { Ok(()) }
153 }
154}
155
156impl SpiBusRead for MySpi {
157 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>>
158 where
159 Self: 'a;
160
161 fn read<'a>(&'a mut self, words: &'a mut [u8]) -> Self::ReadFuture<'a> {
162 async move {
163 self.dio.set_as_input();
164 for word in words {
165 let mut w = 0;
166 for _ in 0..8 {
167 w = w << 1;
168
169 // rising edge, sample data
170 if self.dio.is_high() {
171 w |= 0x01;
172 }
173 self.clk.set_high();
174
175 // falling edge
176 self.clk.set_low();
177 }
178 *word = w
179 }
180
181 Ok(())
182 }
183 }
184}
185
186impl SpiBusWrite for MySpi {
187 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>>
188 where
189 Self: 'a;
190
191 fn write<'a>(&'a mut self, words: &'a [u8]) -> Self::WriteFuture<'a> {
192 async move {
193 self.dio.set_as_output();
194 for word in words {
195 let mut word = *word;
196 for _ in 0..8 {
197 // falling edge, setup data
198 self.clk.set_low();
199 if word & 0x80 == 0 {
200 self.dio.set_low();
201 } else {
202 self.dio.set_high();
203 }
204
205 // rising edge
206 self.clk.set_high();
207
208 word = word << 1;
209 }
210 }
211 self.clk.set_low();
212
213 self.dio.set_as_input();
214 Ok(())
215 }
216 }
217}