diff options
65 files changed, 923 insertions, 93 deletions
diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index bd0cff26b..f3760f589 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs | |||
| @@ -165,6 +165,9 @@ impl<F: Future + 'static> TaskStorage<F> { | |||
| 165 | Poll::Ready(_) => { | 165 | Poll::Ready(_) => { |
| 166 | this.future.drop_in_place(); | 166 | this.future.drop_in_place(); |
| 167 | this.raw.state.fetch_and(!STATE_SPAWNED, Ordering::AcqRel); | 167 | this.raw.state.fetch_and(!STATE_SPAWNED, Ordering::AcqRel); |
| 168 | |||
| 169 | #[cfg(feature = "integrated-timers")] | ||
| 170 | this.raw.expires_at.set(Instant::MAX); | ||
| 168 | } | 171 | } |
| 169 | Poll::Pending => {} | 172 | Poll::Pending => {} |
| 170 | } | 173 | } |
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 36f8d06f2..0d97b6db1 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | use core::cell::RefCell; | 3 | use core::cell::RefCell; |
| 4 | use core::future::poll_fn; | 4 | use core::future::poll_fn; |
| 5 | use core::mem; | 5 | use core::mem; |
| 6 | use core::task::Poll; | 6 | use core::task::{Context, Poll}; |
| 7 | 7 | ||
| 8 | use embassy_net_driver::Driver; | 8 | use embassy_net_driver::Driver; |
| 9 | use smoltcp::iface::{Interface, SocketHandle}; | 9 | use smoltcp::iface::{Interface, SocketHandle}; |
| @@ -102,37 +102,61 @@ impl<'a> UdpSocket<'a> { | |||
| 102 | /// | 102 | /// |
| 103 | /// Returns the number of bytes received and the remote endpoint. | 103 | /// Returns the number of bytes received and the remote endpoint. |
| 104 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | 104 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { |
| 105 | poll_fn(move |cx| { | 105 | poll_fn(move |cx| self.poll_recv_from(buf, cx)).await |
| 106 | self.with_mut(|s, _| match s.recv_slice(buf) { | 106 | } |
| 107 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), | 107 | |
| 108 | // No data ready | 108 | /// Receive a datagram. |
| 109 | Err(udp::RecvError::Exhausted) => { | 109 | /// |
| 110 | s.register_recv_waker(cx.waker()); | 110 | /// When no datagram is available, this method will return `Poll::Pending` and |
| 111 | Poll::Pending | 111 | /// register the current task to be notified when a datagram is received. |
| 112 | } | 112 | /// |
| 113 | }) | 113 | /// When a datagram is received, this method will return `Poll::Ready` with the |
| 114 | /// number of bytes received and the remote endpoint. | ||
| 115 | pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), Error>> { | ||
| 116 | self.with_mut(|s, _| match s.recv_slice(buf) { | ||
| 117 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), | ||
| 118 | // No data ready | ||
| 119 | Err(udp::RecvError::Exhausted) => { | ||
| 120 | s.register_recv_waker(cx.waker()); | ||
| 121 | Poll::Pending | ||
| 122 | } | ||
| 114 | }) | 123 | }) |
| 115 | .await | ||
| 116 | } | 124 | } |
| 117 | 125 | ||
| 118 | /// Send a datagram to the specified remote endpoint. | 126 | /// Send a datagram to the specified remote endpoint. |
| 127 | /// | ||
| 128 | /// This method will wait until the datagram has been sent. | ||
| 129 | /// | ||
| 130 | /// When the remote endpoint is not reachable, this method will return `Err(Error::NoRoute)` | ||
| 119 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> | 131 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> |
| 120 | where | 132 | where |
| 121 | T: Into<IpEndpoint>, | 133 | T: Into<IpEndpoint>, |
| 122 | { | 134 | { |
| 123 | let remote_endpoint = remote_endpoint.into(); | 135 | let remote_endpoint: IpEndpoint = remote_endpoint.into(); |
| 124 | poll_fn(move |cx| { | 136 | poll_fn(move |cx| self.poll_send_to(buf, remote_endpoint, cx)).await |
| 125 | self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { | 137 | } |
| 126 | // Entire datagram has been sent | 138 | |
| 127 | Ok(()) => Poll::Ready(Ok(())), | 139 | /// Send a datagram to the specified remote endpoint. |
| 128 | Err(udp::SendError::BufferFull) => { | 140 | /// |
| 129 | s.register_send_waker(cx.waker()); | 141 | /// When the datagram has been sent, this method will return `Poll::Ready(Ok())`. |
| 130 | Poll::Pending | 142 | /// |
| 131 | } | 143 | /// When the socket's send buffer is full, this method will return `Poll::Pending` |
| 132 | Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), | 144 | /// and register the current task to be notified when the buffer has space available. |
| 133 | }) | 145 | /// |
| 146 | /// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`. | ||
| 147 | pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), Error>> | ||
| 148 | where | ||
| 149 | T: Into<IpEndpoint>, | ||
| 150 | { | ||
| 151 | self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { | ||
| 152 | // Entire datagram has been sent | ||
| 153 | Ok(()) => Poll::Ready(Ok(())), | ||
| 154 | Err(udp::SendError::BufferFull) => { | ||
| 155 | s.register_send_waker(cx.waker()); | ||
| 156 | Poll::Pending | ||
| 157 | } | ||
| 158 | Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), | ||
| 134 | }) | 159 | }) |
| 135 | .await | ||
| 136 | } | 160 | } |
| 137 | 161 | ||
| 138 | /// Returns the local endpoint of the socket. | 162 | /// Returns the local endpoint of the socket. |
diff --git a/embassy-rp/src/clocks.rs b/embassy-rp/src/clocks.rs index ddd61d224..acb21dce5 100644 --- a/embassy-rp/src/clocks.rs +++ b/embassy-rp/src/clocks.rs | |||
| @@ -308,6 +308,7 @@ pub(crate) unsafe fn init(config: ClockConfig) { | |||
| 308 | // - QSPI (we're using it to run this code!) | 308 | // - QSPI (we're using it to run this code!) |
| 309 | // - PLLs (it may be suicide if that's what's clocking us) | 309 | // - PLLs (it may be suicide if that's what's clocking us) |
| 310 | // - USB, SYSCFG (breaks usb-to-swd on core1) | 310 | // - USB, SYSCFG (breaks usb-to-swd on core1) |
| 311 | // - RTC (else there would be no more time...) | ||
| 311 | let mut peris = reset::ALL_PERIPHERALS; | 312 | let mut peris = reset::ALL_PERIPHERALS; |
| 312 | peris.set_io_qspi(false); | 313 | peris.set_io_qspi(false); |
| 313 | // peris.set_io_bank0(false); // might be suicide if we're clocked from gpin | 314 | // peris.set_io_bank0(false); // might be suicide if we're clocked from gpin |
| @@ -317,6 +318,7 @@ pub(crate) unsafe fn init(config: ClockConfig) { | |||
| 317 | // TODO investigate if usb should be unreset here | 318 | // TODO investigate if usb should be unreset here |
| 318 | peris.set_usbctrl(false); | 319 | peris.set_usbctrl(false); |
| 319 | peris.set_syscfg(false); | 320 | peris.set_syscfg(false); |
| 321 | peris.set_rtc(false); | ||
| 320 | reset::reset(peris); | 322 | reset::reset(peris); |
| 321 | 323 | ||
| 322 | // Disable resus that may be enabled from previous software | 324 | // Disable resus that may be enabled from previous software |
diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index d18fb909c..a3d330cdc 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs | |||
| @@ -566,13 +566,13 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 566 | /// Is the output level high? | 566 | /// Is the output level high? |
| 567 | #[inline] | 567 | #[inline] |
| 568 | pub fn is_set_high(&self) -> bool { | 568 | pub fn is_set_high(&self) -> bool { |
| 569 | (self.pin.sio_out().value().read() & self.bit()) == 0 | 569 | !self.is_set_low() |
| 570 | } | 570 | } |
| 571 | 571 | ||
| 572 | /// Is the output level low? | 572 | /// Is the output level low? |
| 573 | #[inline] | 573 | #[inline] |
| 574 | pub fn is_set_low(&self) -> bool { | 574 | pub fn is_set_low(&self) -> bool { |
| 575 | !self.is_set_high() | 575 | (self.pin.sio_out().value().read() & self.bit()) == 0 |
| 576 | } | 576 | } |
| 577 | 577 | ||
| 578 | /// What level output is set to | 578 | /// What level output is set to |
diff --git a/embassy-rp/src/rtc/datetime_no_deps.rs b/embassy-rp/src/rtc/datetime_no_deps.rs index 92770e984..ea899c339 100644 --- a/embassy-rp/src/rtc/datetime_no_deps.rs +++ b/embassy-rp/src/rtc/datetime_no_deps.rs | |||
| @@ -25,6 +25,7 @@ pub enum Error { | |||
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | /// Structure containing date and time information | 27 | /// Structure containing date and time information |
| 28 | #[derive(Clone, Debug)] | ||
| 28 | pub struct DateTime { | 29 | pub struct DateTime { |
| 29 | /// 0..4095 | 30 | /// 0..4095 |
| 30 | pub year: u16, | 31 | pub year: u16, |
diff --git a/embassy-rp/src/rtc/mod.rs b/embassy-rp/src/rtc/mod.rs index b18f12fc4..1b33fdf8d 100644 --- a/embassy-rp/src/rtc/mod.rs +++ b/embassy-rp/src/rtc/mod.rs | |||
| @@ -12,26 +12,24 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; | |||
| 12 | use crate::clocks::clk_rtc_freq; | 12 | use crate::clocks::clk_rtc_freq; |
| 13 | 13 | ||
| 14 | /// A reference to the real time clock of the system | 14 | /// A reference to the real time clock of the system |
| 15 | pub struct RealTimeClock<'d, T: Instance> { | 15 | pub struct Rtc<'d, T: Instance> { |
| 16 | inner: PeripheralRef<'d, T>, | 16 | inner: PeripheralRef<'d, T>, |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | impl<'d, T: Instance> RealTimeClock<'d, T> { | 19 | impl<'d, T: Instance> Rtc<'d, T> { |
| 20 | /// Create a new instance of the real time clock, with the given date as an initial value. | 20 | /// Create a new instance of the real time clock, with the given date as an initial value. |
| 21 | /// | 21 | /// |
| 22 | /// # Errors | 22 | /// # Errors |
| 23 | /// | 23 | /// |
| 24 | /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. | 24 | /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. |
| 25 | pub fn new(inner: impl Peripheral<P = T> + 'd, initial_date: DateTime) -> Result<Self, RtcError> { | 25 | pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self { |
| 26 | into_ref!(inner); | 26 | into_ref!(inner); |
| 27 | 27 | ||
| 28 | // Set the RTC divider | 28 | // Set the RTC divider |
| 29 | inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1)); | 29 | inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1)); |
| 30 | 30 | ||
| 31 | let mut result = Self { inner }; | 31 | let result = Self { inner }; |
| 32 | result.set_leap_year_check(true); // should be on by default, make sure this is the case. | 32 | result |
| 33 | result.set_datetime(initial_date)?; | ||
| 34 | Ok(result) | ||
| 35 | } | 33 | } |
| 36 | 34 | ||
| 37 | /// Enable or disable the leap year check. The rp2040 chip will always add a Feb 29th on every year that is divisable by 4, but this may be incorrect (e.g. on century years). This function allows you to disable this check. | 35 | /// Enable or disable the leap year check. The rp2040 chip will always add a Feb 29th on every year that is divisable by 4, but this may be incorrect (e.g. on century years). This function allows you to disable this check. |
| @@ -43,7 +41,37 @@ impl<'d, T: Instance> RealTimeClock<'d, T> { | |||
| 43 | }); | 41 | }); |
| 44 | } | 42 | } |
| 45 | 43 | ||
| 46 | /// Checks to see if this RealTimeClock is running | 44 | /// Set the time from internal format |
| 45 | pub fn restore(&mut self, ymd: rp_pac::rtc::regs::Rtc1, hms: rp_pac::rtc::regs::Rtc0) { | ||
| 46 | // disable RTC while we configure it | ||
| 47 | self.inner.regs().ctrl().modify(|w| w.set_rtc_enable(false)); | ||
| 48 | while self.inner.regs().ctrl().read().rtc_active() { | ||
| 49 | core::hint::spin_loop(); | ||
| 50 | } | ||
| 51 | |||
| 52 | self.inner.regs().setup_0().write(|w| { | ||
| 53 | *w = rp_pac::rtc::regs::Setup0(ymd.0); | ||
| 54 | }); | ||
| 55 | self.inner.regs().setup_1().write(|w| { | ||
| 56 | *w = rp_pac::rtc::regs::Setup1(hms.0); | ||
| 57 | }); | ||
| 58 | |||
| 59 | // Load the new datetime and re-enable RTC | ||
| 60 | self.inner.regs().ctrl().write(|w| w.set_load(true)); | ||
| 61 | self.inner.regs().ctrl().write(|w| w.set_rtc_enable(true)); | ||
| 62 | while !self.inner.regs().ctrl().read().rtc_active() { | ||
| 63 | core::hint::spin_loop(); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | /// Get the time in internal format | ||
| 68 | pub fn save(&mut self) -> (rp_pac::rtc::regs::Rtc1, rp_pac::rtc::regs::Rtc0) { | ||
| 69 | let rtc_0: rp_pac::rtc::regs::Rtc0 = self.inner.regs().rtc_0().read(); | ||
| 70 | let rtc_1 = self.inner.regs().rtc_1().read(); | ||
| 71 | (rtc_1, rtc_0) | ||
| 72 | } | ||
| 73 | |||
| 74 | /// Checks to see if this Rtc is running | ||
| 47 | pub fn is_running(&self) -> bool { | 75 | pub fn is_running(&self) -> bool { |
| 48 | self.inner.regs().ctrl().read().rtc_active() | 76 | self.inner.regs().ctrl().read().rtc_active() |
| 49 | } | 77 | } |
| @@ -113,8 +141,8 @@ impl<'d, T: Instance> RealTimeClock<'d, T> { | |||
| 113 | /// # fn main() { } | 141 | /// # fn main() { } |
| 114 | /// # #[cfg(not(feature = "chrono"))] | 142 | /// # #[cfg(not(feature = "chrono"))] |
| 115 | /// # fn main() { | 143 | /// # fn main() { |
| 116 | /// # use embassy_rp::rtc::{RealTimeClock, DateTimeFilter}; | 144 | /// # use embassy_rp::rtc::{Rtc, DateTimeFilter}; |
| 117 | /// # let mut real_time_clock: RealTimeClock<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() }; | 145 | /// # let mut real_time_clock: Rtc<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() }; |
| 118 | /// let now = real_time_clock.now().unwrap(); | 146 | /// let now = real_time_clock.now().unwrap(); |
| 119 | /// real_time_clock.schedule_alarm( | 147 | /// real_time_clock.schedule_alarm( |
| 120 | /// DateTimeFilter::default() | 148 | /// DateTimeFilter::default() |
| @@ -150,7 +178,7 @@ impl<'d, T: Instance> RealTimeClock<'d, T> { | |||
| 150 | } | 178 | } |
| 151 | } | 179 | } |
| 152 | 180 | ||
| 153 | /// Errors that can occur on methods on [RealTimeClock] | 181 | /// Errors that can occur on methods on [Rtc] |
| 154 | #[derive(Clone, Debug, PartialEq, Eq)] | 182 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 155 | pub enum RtcError { | 183 | pub enum RtcError { |
| 156 | /// An invalid DateTime was given or stored on the hardware. | 184 | /// An invalid DateTime was given or stored on the hardware. |
diff --git a/embassy-rp/src/usb.rs b/embassy-rp/src/usb.rs index b3f3bd927..4ab881f6e 100644 --- a/embassy-rp/src/usb.rs +++ b/embassy-rp/src/usb.rs | |||
| @@ -361,6 +361,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 361 | 361 | ||
| 362 | let regs = T::regs(); | 362 | let regs = T::regs(); |
| 363 | let siestatus = regs.sie_status().read(); | 363 | let siestatus = regs.sie_status().read(); |
| 364 | let intrstatus = regs.intr().read(); | ||
| 364 | 365 | ||
| 365 | if siestatus.resume() { | 366 | if siestatus.resume() { |
| 366 | regs.sie_status().write(|w| w.set_resume(true)); | 367 | regs.sie_status().write(|w| w.set_resume(true)); |
| @@ -389,7 +390,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 389 | return Poll::Ready(Event::Reset); | 390 | return Poll::Ready(Event::Reset); |
| 390 | } | 391 | } |
| 391 | 392 | ||
| 392 | if siestatus.suspended() { | 393 | if siestatus.suspended() && intrstatus.dev_suspend() { |
| 393 | regs.sie_status().write(|w| w.set_suspended(true)); | 394 | regs.sie_status().write(|w| w.set_suspended(true)); |
| 394 | return Poll::Ready(Event::Suspend); | 395 | return Poll::Ready(Event::Suspend); |
| 395 | } | 396 | } |
diff --git a/embassy-rp/src/watchdog.rs b/embassy-rp/src/watchdog.rs index d37795cc9..f1e986ec7 100644 --- a/embassy-rp/src/watchdog.rs +++ b/embassy-rp/src/watchdog.rs | |||
| @@ -107,4 +107,36 @@ impl Watchdog { | |||
| 107 | w.set_trigger(true); | 107 | w.set_trigger(true); |
| 108 | }) | 108 | }) |
| 109 | } | 109 | } |
| 110 | |||
| 111 | /// Store data in scratch register | ||
| 112 | pub fn set_scratch(&mut self, index: usize, value: u32) { | ||
| 113 | let watchdog = pac::WATCHDOG; | ||
| 114 | match index { | ||
| 115 | 0 => watchdog.scratch0().write(|w| *w = value), | ||
| 116 | 1 => watchdog.scratch1().write(|w| *w = value), | ||
| 117 | 2 => watchdog.scratch2().write(|w| *w = value), | ||
| 118 | 3 => watchdog.scratch3().write(|w| *w = value), | ||
| 119 | 4 => watchdog.scratch4().write(|w| *w = value), | ||
| 120 | 5 => watchdog.scratch5().write(|w| *w = value), | ||
| 121 | 6 => watchdog.scratch6().write(|w| *w = value), | ||
| 122 | 7 => watchdog.scratch7().write(|w| *w = value), | ||
| 123 | _ => panic!("Invalid watchdog scratch index"), | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | /// Read data from scratch register | ||
| 128 | pub fn get_scratch(&mut self, index: usize) -> u32 { | ||
| 129 | let watchdog = pac::WATCHDOG; | ||
| 130 | match index { | ||
| 131 | 0 => watchdog.scratch0().read(), | ||
| 132 | 1 => watchdog.scratch1().read(), | ||
| 133 | 2 => watchdog.scratch2().read(), | ||
| 134 | 3 => watchdog.scratch3().read(), | ||
| 135 | 4 => watchdog.scratch4().read(), | ||
| 136 | 5 => watchdog.scratch5().read(), | ||
| 137 | 6 => watchdog.scratch6().read(), | ||
| 138 | 7 => watchdog.scratch7().read(), | ||
| 139 | _ => panic!("Invalid watchdog scratch index"), | ||
| 140 | } | ||
| 141 | } | ||
| 110 | } | 142 | } |
diff --git a/embassy-stm32-wpan/Cargo.toml b/embassy-stm32-wpan/Cargo.toml index 91540321f..868bffe74 100644 --- a/embassy-stm32-wpan/Cargo.toml +++ b/embassy-stm32-wpan/Cargo.toml | |||
| @@ -25,12 +25,11 @@ aligned = "0.4.1" | |||
| 25 | 25 | ||
| 26 | bit_field = "0.10.2" | 26 | bit_field = "0.10.2" |
| 27 | stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } | 27 | stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } |
| 28 | stm32wb-hci = { version = "0.1.2", features = ["version-5-0"], optional = true } | 28 | stm32wb-hci = { version = "0.1.3", optional = true } |
| 29 | bitflags = { version = "2.3.3", optional = true } | 29 | bitflags = { version = "2.3.3", optional = true } |
| 30 | 30 | ||
| 31 | [features] | 31 | [features] |
| 32 | default = [] | 32 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt", "stm32wb-hci?/defmt"] |
| 33 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"] | ||
| 34 | 33 | ||
| 35 | ble = ["dep:stm32wb-hci"] | 34 | ble = ["dep:stm32wb-hci"] |
| 36 | mac = ["dep:bitflags"] | 35 | mac = ["dep:bitflags"] |
diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs index 8abe541d3..58d438af8 100644 --- a/embassy-stm32/src/dma/dma.rs +++ b/embassy-stm32/src/dma/dma.rs | |||
| @@ -1,10 +1,9 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::pin::Pin; | 3 | use core::pin::Pin; |
| 4 | use core::sync::atomic::{fence, Ordering}; | 4 | use core::sync::atomic::{fence, AtomicUsize, Ordering}; |
| 5 | use core::task::{Context, Poll, Waker}; | 5 | use core::task::{Context, Poll, Waker}; |
| 6 | 6 | ||
| 7 | use atomic_polyfill::AtomicUsize; | ||
| 8 | use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; | 7 | use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; |
| 9 | use embassy_sync::waitqueue::AtomicWaker; | 8 | use embassy_sync::waitqueue::AtomicWaker; |
| 10 | 9 | ||
diff --git a/embassy-stm32/src/flash/asynch.rs b/embassy-stm32/src/flash/asynch.rs index 70a5ded62..f175349cd 100644 --- a/embassy-stm32/src/flash/asynch.rs +++ b/embassy-stm32/src/flash/asynch.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | use core::sync::atomic::{fence, Ordering}; | ||
| 2 | 3 | ||
| 3 | use atomic_polyfill::{fence, Ordering}; | ||
| 4 | use embassy_hal_common::drop::OnDrop; | 4 | use embassy_hal_common::drop::OnDrop; |
| 5 | use embassy_hal_common::into_ref; | 5 | use embassy_hal_common::into_ref; |
| 6 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | 6 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; |
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index c6cdc574b..2a374733d 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | use core::sync::atomic::{fence, Ordering}; | ||
| 2 | 3 | ||
| 3 | use atomic_polyfill::{fence, Ordering}; | ||
| 4 | use embassy_hal_common::drop::OnDrop; | 4 | use embassy_hal_common::drop::OnDrop; |
| 5 | use embassy_hal_common::{into_ref, PeripheralRef}; | 5 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 6 | use stm32_metapac::FLASH_BASE; | 6 | use stm32_metapac::FLASH_BASE; |
diff --git a/embassy-stm32/src/flash/f0.rs b/embassy-stm32/src/flash/f0.rs index 02bd4cc1f..ec8343e7c 100644 --- a/embassy-stm32/src/flash/f0.rs +++ b/embassy-stm32/src/flash/f0.rs | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::ptr::write_volatile; | 2 | use core::ptr::write_volatile; |
| 3 | 3 | use core::sync::atomic::{fence, Ordering}; | |
| 4 | use atomic_polyfill::{fence, Ordering}; | ||
| 5 | 4 | ||
| 6 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | 5 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; |
| 7 | use crate::flash::Error; | 6 | use crate::flash::Error; |
diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index b093a7837..40335d643 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::ptr::write_volatile; | 2 | use core::ptr::write_volatile; |
| 3 | 3 | use core::sync::atomic::{fence, Ordering}; | |
| 4 | use atomic_polyfill::{fence, Ordering}; | ||
| 5 | 4 | ||
| 6 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | 5 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; |
| 7 | use crate::flash::Error; | 6 | use crate::flash::Error; |
diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 242d99278..4cb39e033 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::ptr::write_volatile; | 2 | use core::ptr::write_volatile; |
| 3 | use core::sync::atomic::{fence, Ordering}; | 3 | use core::sync::atomic::{fence, AtomicBool, Ordering}; |
| 4 | 4 | ||
| 5 | use atomic_polyfill::AtomicBool; | ||
| 6 | use embassy_sync::waitqueue::AtomicWaker; | 5 | use embassy_sync::waitqueue::AtomicWaker; |
| 7 | use pac::flash::regs::Sr; | 6 | use pac::flash::regs::Sr; |
| 8 | use pac::FLASH_SIZE; | 7 | use pac::FLASH_SIZE; |
diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs index 9baf059ee..bf17b5b18 100644 --- a/embassy-stm32/src/flash/h7.rs +++ b/embassy-stm32/src/flash/h7.rs | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::ptr::write_volatile; | 2 | use core::ptr::write_volatile; |
| 3 | 3 | use core::sync::atomic::{fence, Ordering}; | |
| 4 | use atomic_polyfill::{fence, Ordering}; | ||
| 5 | 4 | ||
| 6 | use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; | 5 | use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; |
| 7 | use crate::flash::Error; | 6 | use crate::flash::Error; |
diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs index deefd05ed..243c8b51d 100644 --- a/embassy-stm32/src/flash/l.rs +++ b/embassy-stm32/src/flash/l.rs | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | use core::ptr::write_volatile; | 1 | use core::ptr::write_volatile; |
| 2 | 2 | use core::sync::atomic::{fence, Ordering}; | |
| 3 | use atomic_polyfill::{fence, Ordering}; | ||
| 4 | 3 | ||
| 5 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | 4 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; |
| 6 | use crate::flash::Error; | 5 | use crate::flash::Error; |
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 1f036d55c..208d1527d 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs | |||
| @@ -382,13 +382,18 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 382 | // I2C start | 382 | // I2C start |
| 383 | // | 383 | // |
| 384 | // ST SAD+W | 384 | // ST SAD+W |
| 385 | Self::master_write( | 385 | if let Err(err) = Self::master_write( |
| 386 | address, | 386 | address, |
| 387 | write.len().min(255), | 387 | write.len().min(255), |
| 388 | Stop::Software, | 388 | Stop::Software, |
| 389 | last_chunk_idx != 0, | 389 | last_chunk_idx != 0, |
| 390 | &check_timeout, | 390 | &check_timeout, |
| 391 | )?; | 391 | ) { |
| 392 | if send_stop { | ||
| 393 | self.master_stop(); | ||
| 394 | } | ||
| 395 | return Err(err); | ||
| 396 | } | ||
| 392 | 397 | ||
| 393 | for (number, chunk) in write.chunks(255).enumerate() { | 398 | for (number, chunk) in write.chunks(255).enumerate() { |
| 394 | if number != 0 { | 399 | if number != 0 { |
| @@ -399,18 +404,22 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 399 | // Wait until we are allowed to send data | 404 | // Wait until we are allowed to send data |
| 400 | // (START has been ACKed or last byte when | 405 | // (START has been ACKed or last byte when |
| 401 | // through) | 406 | // through) |
| 402 | self.wait_txe(&check_timeout)?; | 407 | if let Err(err) = self.wait_txe(&check_timeout) { |
| 408 | if send_stop { | ||
| 409 | self.master_stop(); | ||
| 410 | } | ||
| 411 | return Err(err); | ||
| 412 | } | ||
| 403 | 413 | ||
| 404 | T::regs().txdr().write(|w| w.set_txdata(*byte)); | 414 | T::regs().txdr().write(|w| w.set_txdata(*byte)); |
| 405 | } | 415 | } |
| 406 | } | 416 | } |
| 407 | // Wait until the write finishes | 417 | // Wait until the write finishes |
| 408 | self.wait_tc(&check_timeout)?; | 418 | let result = self.wait_tc(&check_timeout); |
| 409 | |||
| 410 | if send_stop { | 419 | if send_stop { |
| 411 | self.master_stop(); | 420 | self.master_stop(); |
| 412 | } | 421 | } |
| 413 | Ok(()) | 422 | result |
| 414 | } | 423 | } |
| 415 | 424 | ||
| 416 | async fn write_dma_internal( | 425 | async fn write_dma_internal( |
| @@ -707,13 +716,16 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 707 | let first_length = write[0].len(); | 716 | let first_length = write[0].len(); |
| 708 | let last_slice_index = write.len() - 1; | 717 | let last_slice_index = write.len() - 1; |
| 709 | 718 | ||
| 710 | Self::master_write( | 719 | if let Err(err) = Self::master_write( |
| 711 | address, | 720 | address, |
| 712 | first_length.min(255), | 721 | first_length.min(255), |
| 713 | Stop::Software, | 722 | Stop::Software, |
| 714 | (first_length > 255) || (last_slice_index != 0), | 723 | (first_length > 255) || (last_slice_index != 0), |
| 715 | &check_timeout, | 724 | &check_timeout, |
| 716 | )?; | 725 | ) { |
| 726 | self.master_stop(); | ||
| 727 | return Err(err); | ||
| 728 | } | ||
| 717 | 729 | ||
| 718 | for (idx, slice) in write.iter().enumerate() { | 730 | for (idx, slice) in write.iter().enumerate() { |
| 719 | let slice_len = slice.len(); | 731 | let slice_len = slice.len(); |
| @@ -726,27 +738,36 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 726 | let last_chunk_idx = total_chunks.saturating_sub(1); | 738 | let last_chunk_idx = total_chunks.saturating_sub(1); |
| 727 | 739 | ||
| 728 | if idx != 0 { | 740 | if idx != 0 { |
| 729 | Self::master_continue( | 741 | if let Err(err) = Self::master_continue( |
| 730 | slice_len.min(255), | 742 | slice_len.min(255), |
| 731 | (idx != last_slice_index) || (slice_len > 255), | 743 | (idx != last_slice_index) || (slice_len > 255), |
| 732 | &check_timeout, | 744 | &check_timeout, |
| 733 | )?; | 745 | ) { |
| 746 | self.master_stop(); | ||
| 747 | return Err(err); | ||
| 748 | } | ||
| 734 | } | 749 | } |
| 735 | 750 | ||
| 736 | for (number, chunk) in slice.chunks(255).enumerate() { | 751 | for (number, chunk) in slice.chunks(255).enumerate() { |
| 737 | if number != 0 { | 752 | if number != 0 { |
| 738 | Self::master_continue( | 753 | if let Err(err) = Self::master_continue( |
| 739 | chunk.len(), | 754 | chunk.len(), |
| 740 | (number != last_chunk_idx) || (idx != last_slice_index), | 755 | (number != last_chunk_idx) || (idx != last_slice_index), |
| 741 | &check_timeout, | 756 | &check_timeout, |
| 742 | )?; | 757 | ) { |
| 758 | self.master_stop(); | ||
| 759 | return Err(err); | ||
| 760 | } | ||
| 743 | } | 761 | } |
| 744 | 762 | ||
| 745 | for byte in chunk { | 763 | for byte in chunk { |
| 746 | // Wait until we are allowed to send data | 764 | // Wait until we are allowed to send data |
| 747 | // (START has been ACKed or last byte when | 765 | // (START has been ACKed or last byte when |
| 748 | // through) | 766 | // through) |
| 749 | self.wait_txe(&check_timeout)?; | 767 | if let Err(err) = self.wait_txe(&check_timeout) { |
| 768 | self.master_stop(); | ||
| 769 | return Err(err); | ||
| 770 | } | ||
| 750 | 771 | ||
| 751 | // Put byte on the wire | 772 | // Put byte on the wire |
| 752 | //self.i2c.txdr.write(|w| w.txdata().bits(*byte)); | 773 | //self.i2c.txdr.write(|w| w.txdata().bits(*byte)); |
| @@ -755,10 +776,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 755 | } | 776 | } |
| 756 | } | 777 | } |
| 757 | // Wait until the write finishes | 778 | // Wait until the write finishes |
| 758 | self.wait_tc(&check_timeout)?; | 779 | let result = self.wait_tc(&check_timeout); |
| 759 | self.master_stop(); | 780 | self.master_stop(); |
| 760 | 781 | result | |
| 761 | Ok(()) | ||
| 762 | } | 782 | } |
| 763 | 783 | ||
| 764 | pub fn blocking_write_vectored(&mut self, address: u8, write: &[&[u8]]) -> Result<(), Error> { | 784 | pub fn blocking_write_vectored(&mut self, address: u8, write: &[&[u8]]) -> Result<(), Error> { |
diff --git a/embassy-stm32/src/ipcc.rs b/embassy-stm32/src/ipcc.rs index 37f840c73..a24cba9f0 100644 --- a/embassy-stm32/src/ipcc.rs +++ b/embassy-stm32/src/ipcc.rs | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | use core::future::poll_fn; | 1 | use core::future::poll_fn; |
| 2 | use core::sync::atomic::{compiler_fence, Ordering}; | ||
| 2 | use core::task::Poll; | 3 | use core::task::Poll; |
| 3 | 4 | ||
| 4 | use atomic_polyfill::{compiler_fence, Ordering}; | ||
| 5 | |||
| 6 | use self::sealed::Instance; | 5 | use self::sealed::Instance; |
| 7 | use crate::interrupt; | 6 | use crate::interrupt; |
| 8 | use crate::interrupt::typelevel::Interrupt; | 7 | use crate::interrupt::typelevel::Interrupt; |
diff --git a/embassy-stm32/src/rcc/wl.rs b/embassy-stm32/src/rcc/wl.rs index 7072db984..6b69bb1cb 100644 --- a/embassy-stm32/src/rcc/wl.rs +++ b/embassy-stm32/src/rcc/wl.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | use crate::pac::{FLASH, RCC}; | 1 | use crate::pac::pwr::vals::Dbp; |
| 2 | use crate::pac::{FLASH, PWR, RCC}; | ||
| 2 | use crate::rcc::{set_freqs, Clocks}; | 3 | use crate::rcc::{set_freqs, Clocks}; |
| 3 | use crate::time::Hertz; | 4 | use crate::time::Hertz; |
| 4 | 5 | ||
| @@ -184,6 +185,8 @@ pub struct Config { | |||
| 184 | pub apb1_pre: APBPrescaler, | 185 | pub apb1_pre: APBPrescaler, |
| 185 | pub apb2_pre: APBPrescaler, | 186 | pub apb2_pre: APBPrescaler, |
| 186 | pub enable_lsi: bool, | 187 | pub enable_lsi: bool, |
| 188 | pub enable_rtc_apb: bool, | ||
| 189 | pub rtc_mux: RtcClockSource, | ||
| 187 | } | 190 | } |
| 188 | 191 | ||
| 189 | impl Default for Config { | 192 | impl Default for Config { |
| @@ -196,10 +199,25 @@ impl Default for Config { | |||
| 196 | apb1_pre: APBPrescaler::NotDivided, | 199 | apb1_pre: APBPrescaler::NotDivided, |
| 197 | apb2_pre: APBPrescaler::NotDivided, | 200 | apb2_pre: APBPrescaler::NotDivided, |
| 198 | enable_lsi: false, | 201 | enable_lsi: false, |
| 202 | enable_rtc_apb: false, | ||
| 203 | rtc_mux: RtcClockSource::LSI32, | ||
| 199 | } | 204 | } |
| 200 | } | 205 | } |
| 201 | } | 206 | } |
| 202 | 207 | ||
| 208 | pub enum RtcClockSource { | ||
| 209 | LSE32, | ||
| 210 | LSI32, | ||
| 211 | } | ||
| 212 | |||
| 213 | #[repr(u8)] | ||
| 214 | pub enum Lsedrv { | ||
| 215 | Low = 0, | ||
| 216 | MediumLow = 1, | ||
| 217 | MediumHigh = 2, | ||
| 218 | High = 3, | ||
| 219 | } | ||
| 220 | |||
| 203 | pub(crate) unsafe fn init(config: Config) { | 221 | pub(crate) unsafe fn init(config: Config) { |
| 204 | let (sys_clk, sw, vos) = match config.mux { | 222 | let (sys_clk, sw, vos) = match config.mux { |
| 205 | ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2), | 223 | ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2), |
| @@ -266,6 +284,32 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 266 | 284 | ||
| 267 | while FLASH.acr().read().latency() != ws {} | 285 | while FLASH.acr().read().latency() != ws {} |
| 268 | 286 | ||
| 287 | match config.rtc_mux { | ||
| 288 | RtcClockSource::LSE32 => { | ||
| 289 | // 1. Unlock the backup domain | ||
| 290 | PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED)); | ||
| 291 | |||
| 292 | // 2. Setup the LSE | ||
| 293 | RCC.bdcr().modify(|w| { | ||
| 294 | // Enable LSE | ||
| 295 | w.set_lseon(true); | ||
| 296 | // Max drive strength | ||
| 297 | // TODO: should probably be settable | ||
| 298 | w.set_lsedrv(Lsedrv::High as u8); //---// PAM - should not be commented | ||
| 299 | }); | ||
| 300 | |||
| 301 | // Wait until LSE is running | ||
| 302 | while !RCC.bdcr().read().lserdy() {} | ||
| 303 | } | ||
| 304 | RtcClockSource::LSI32 => { | ||
| 305 | // Turn on the internal 32 kHz LSI oscillator | ||
| 306 | RCC.csr().modify(|w| w.set_lsion(true)); | ||
| 307 | |||
| 308 | // Wait until LSI is running | ||
| 309 | while !RCC.csr().read().lsirdy() {} | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 269 | match config.mux { | 313 | match config.mux { |
| 270 | ClockSrc::HSI16 => { | 314 | ClockSrc::HSI16 => { |
| 271 | // Enable HSI16 | 315 | // Enable HSI16 |
| @@ -287,11 +331,26 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 287 | w.set_msirgsel(true); | 331 | w.set_msirgsel(true); |
| 288 | w.set_msirange(range.into()); | 332 | w.set_msirange(range.into()); |
| 289 | w.set_msion(true); | 333 | w.set_msion(true); |
| 334 | |||
| 335 | if let RtcClockSource::LSE32 = config.rtc_mux { | ||
| 336 | // If LSE is enabled, enable calibration of MSI | ||
| 337 | w.set_msipllen(true); | ||
| 338 | } else { | ||
| 339 | w.set_msipllen(false); | ||
| 340 | } | ||
| 290 | }); | 341 | }); |
| 291 | while !RCC.cr().read().msirdy() {} | 342 | while !RCC.cr().read().msirdy() {} |
| 292 | } | 343 | } |
| 293 | } | 344 | } |
| 294 | 345 | ||
| 346 | if config.enable_rtc_apb { | ||
| 347 | // enable peripheral clock for communication | ||
| 348 | crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true)); | ||
| 349 | |||
| 350 | // read to allow the pwr clock to enable | ||
| 351 | crate::pac::PWR.cr1().read(); | ||
| 352 | } | ||
| 353 | |||
| 295 | RCC.extcfgr().modify(|w| { | 354 | RCC.extcfgr().modify(|w| { |
| 296 | if config.shd_ahb_pre == AHBPrescaler::NotDivided { | 355 | if config.shd_ahb_pre == AHBPrescaler::NotDivided { |
| 297 | w.set_shdhpre(0); | 356 | w.set_shdhpre(0); |
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs index 7e5c64d90..8ef0ec51d 100644 --- a/embassy-stm32/src/rtc/v3.rs +++ b/embassy-stm32/src/rtc/v3.rs | |||
| @@ -172,6 +172,7 @@ impl sealed::Instance for crate::peripherals::RTC { | |||
| 172 | const BACKUP_REGISTER_COUNT: usize = 32; | 172 | const BACKUP_REGISTER_COUNT: usize = 32; |
| 173 | 173 | ||
| 174 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { | 174 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { |
| 175 | #[allow(clippy::if_same_then_else)] | ||
| 175 | if register < Self::BACKUP_REGISTER_COUNT { | 176 | if register < Self::BACKUP_REGISTER_COUNT { |
| 176 | //Some(rtc.bkpr()[register].read().bits()) | 177 | //Some(rtc.bkpr()[register].read().bits()) |
| 177 | None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC | 178 | None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC |
diff --git a/embassy-stm32/src/usb_otg/usb.rs b/embassy-stm32/src/usb_otg/usb.rs index d0284746c..fd0e22adf 100644 --- a/embassy-stm32/src/usb_otg/usb.rs +++ b/embassy-stm32/src/usb_otg/usb.rs | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | use core::cell::UnsafeCell; | 1 | use core::cell::UnsafeCell; |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::sync::atomic::{AtomicBool, AtomicU16, Ordering}; | ||
| 3 | use core::task::Poll; | 4 | use core::task::Poll; |
| 4 | 5 | ||
| 5 | use atomic_polyfill::{AtomicBool, AtomicU16, Ordering}; | ||
| 6 | use embassy_hal_common::{into_ref, Peripheral}; | 6 | use embassy_hal_common::{into_ref, Peripheral}; |
| 7 | use embassy_sync::waitqueue::AtomicWaker; | 7 | use embassy_sync::waitqueue::AtomicWaker; |
| 8 | use embassy_usb_driver::{ | 8 | use embassy_usb_driver::{ |
diff --git a/embassy-time/src/driver_std.rs b/embassy-time/src/driver_std.rs index 9f8c57b5c..32db47a37 100644 --- a/embassy-time/src/driver_std.rs +++ b/embassy-time/src/driver_std.rs | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | use core::sync::atomic::{AtomicU8, Ordering}; | ||
| 1 | use std::cell::{RefCell, UnsafeCell}; | 2 | use std::cell::{RefCell, UnsafeCell}; |
| 2 | use std::mem::MaybeUninit; | 3 | use std::mem::MaybeUninit; |
| 3 | use std::sync::{Condvar, Mutex, Once}; | 4 | use std::sync::{Condvar, Mutex, Once}; |
| 4 | use std::time::{Duration as StdDuration, Instant as StdInstant}; | 5 | use std::time::{Duration as StdDuration, Instant as StdInstant}; |
| 5 | use std::{mem, ptr, thread}; | 6 | use std::{mem, ptr, thread}; |
| 6 | 7 | ||
| 7 | use atomic_polyfill::{AtomicU8, Ordering}; | ||
| 8 | use critical_section::Mutex as CsMutex; | 8 | use critical_section::Mutex as CsMutex; |
| 9 | 9 | ||
| 10 | use crate::driver::{AlarmHandle, Driver}; | 10 | use crate::driver::{AlarmHandle, Driver}; |
diff --git a/embassy-time/src/driver_wasm.rs b/embassy-time/src/driver_wasm.rs index 63d049897..0f672dc75 100644 --- a/embassy-time/src/driver_wasm.rs +++ b/embassy-time/src/driver_wasm.rs | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | use core::sync::atomic::{AtomicU8, Ordering}; | ||
| 1 | use std::cell::UnsafeCell; | 2 | use std::cell::UnsafeCell; |
| 2 | use std::mem::MaybeUninit; | 3 | use std::mem::MaybeUninit; |
| 3 | use std::ptr; | 4 | use std::ptr; |
| 4 | use std::sync::{Mutex, Once}; | 5 | use std::sync::{Mutex, Once}; |
| 5 | 6 | ||
| 6 | use atomic_polyfill::{AtomicU8, Ordering}; | ||
| 7 | use wasm_bindgen::prelude::*; | 7 | use wasm_bindgen::prelude::*; |
| 8 | use wasm_timer::Instant as StdInstant; | 8 | use wasm_timer::Instant as StdInstant; |
| 9 | 9 | ||
diff --git a/examples/rp/src/bin/adc.rs b/examples/rp/src/bin/adc.rs index 65069cde1..81a8b8340 100644 --- a/examples/rp/src/bin/adc.rs +++ b/examples/rp/src/bin/adc.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example test the ADC (Analog to Digital Conversion) of the RS2040 pin 26, 27 and 28. | ||
| 2 | //! It also reads the temperature sensor in the chip. | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
| @@ -38,5 +41,8 @@ async fn main(_spawner: Spawner) { | |||
| 38 | 41 | ||
| 39 | fn convert_to_celsius(raw_temp: u16) -> f32 { | 42 | fn convert_to_celsius(raw_temp: u16) -> f32 { |
| 40 | // According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet | 43 | // According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet |
| 41 | 27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721 as f32 | 44 | let temp = 27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721; |
| 45 | let sign = if temp < 0.0 { -1.0 } else { 1.0 }; | ||
| 46 | let rounded_temp_x10: i16 = ((temp * 10.0) + 0.5 * sign) as i16; | ||
| 47 | (rounded_temp_x10 as f32) / 10.0 | ||
| 42 | } | 48 | } |
diff --git a/examples/rp/src/bin/blinky.rs b/examples/rp/src/bin/blinky.rs index 7aa36a19f..295b000f3 100644 --- a/examples/rp/src/bin/blinky.rs +++ b/examples/rp/src/bin/blinky.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example test the RP Pico on board LED. | ||
| 2 | //! | ||
| 3 | //! It does not work with the RP Pico W board. See wifi_blinky.rs. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/button.rs b/examples/rp/src/bin/button.rs index 0d246c093..d7aa89410 100644 --- a/examples/rp/src/bin/button.rs +++ b/examples/rp/src/bin/button.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example uses the RP Pico on board LED to test input pin 28. This is not the button on the board. | ||
| 2 | //! | ||
| 3 | //! It does not work with the RP Pico W board. Use wifi_blinky.rs and add input pin. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/flash.rs b/examples/rp/src/bin/flash.rs index 19076150c..4c4982acc 100644 --- a/examples/rp/src/bin/flash.rs +++ b/examples/rp/src/bin/flash.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | //! This example test the flash connected to the RP2040 chip. | ||
| 2 | |||
| 1 | #![no_std] | 3 | #![no_std] |
| 2 | #![no_main] | 4 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 5 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/gpio_async.rs b/examples/rp/src/bin/gpio_async.rs index 52d13a9d5..bf58044d5 100644 --- a/examples/rp/src/bin/gpio_async.rs +++ b/examples/rp/src/bin/gpio_async.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how async gpio can be used with a RP2040. | ||
| 2 | //! | ||
| 3 | //! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
| @@ -9,8 +13,6 @@ use embassy_time::{Duration, Timer}; | |||
| 9 | use gpio::{Input, Level, Output, Pull}; | 13 | use gpio::{Input, Level, Output, Pull}; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 14 | use {defmt_rtt as _, panic_probe as _}; |
| 11 | 15 | ||
| 12 | /// This example shows how async gpio can be used with a RP2040. | ||
| 13 | /// | ||
| 14 | /// It requires an external signal to be manually triggered on PIN 16. For | 16 | /// It requires an external signal to be manually triggered on PIN 16. For |
| 15 | /// example, this could be accomplished using an external power source with a | 17 | /// example, this could be accomplished using an external power source with a |
| 16 | /// button so that it is possible to toggle the signal from low to high. | 18 | /// button so that it is possible to toggle the signal from low to high. |
diff --git a/examples/rp/src/bin/gpout.rs b/examples/rp/src/bin/gpout.rs index 64461fc5f..0a3b5fa98 100644 --- a/examples/rp/src/bin/gpout.rs +++ b/examples/rp/src/bin/gpout.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how GPOUT (General purpose clock outputs) can toggle a output pin. | ||
| 2 | //! | ||
| 3 | //! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/i2c_async.rs b/examples/rp/src/bin/i2c_async.rs index cf3cf742c..93224bc43 100644 --- a/examples/rp/src/bin/i2c_async.rs +++ b/examples/rp/src/bin/i2c_async.rs | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | //! This example shows how to communicate asynchronous using i2c with external chips. | ||
| 2 | //! | ||
| 3 | //! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip. | ||
| 4 | //! (https://www.microchip.com/en-us/product/mcp23017) | ||
| 5 | |||
| 1 | #![no_std] | 6 | #![no_std] |
| 2 | #![no_main] | 7 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 8 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/i2c_blocking.rs b/examples/rp/src/bin/i2c_blocking.rs index 7623e33c8..1c8c2039d 100644 --- a/examples/rp/src/bin/i2c_blocking.rs +++ b/examples/rp/src/bin/i2c_blocking.rs | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | //! This example shows how to communicate using i2c with external chips. | ||
| 2 | //! | ||
| 3 | //! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip. | ||
| 4 | //! (https://www.microchip.com/en-us/product/mcp23017) | ||
| 5 | |||
| 1 | #![no_std] | 6 | #![no_std] |
| 2 | #![no_main] | 7 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 8 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/lora_lorawan.rs b/examples/rp/src/bin/lora_lorawan.rs index a9c84bf95..d631fafa1 100644 --- a/examples/rp/src/bin/lora_lorawan.rs +++ b/examples/rp/src/bin/lora_lorawan.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. |
| 2 | //! It demonstrates LoRaWAN join functionality. | 2 | //! It demonstrates LoRaWAN join functionality. |
| 3 | |||
| 3 | #![no_std] | 4 | #![no_std] |
| 4 | #![no_main] | 5 | #![no_main] |
| 5 | #![macro_use] | 6 | #![macro_use] |
diff --git a/examples/rp/src/bin/lora_p2p_receive.rs b/examples/rp/src/bin/lora_p2p_receive.rs index 250419202..396d669de 100644 --- a/examples/rp/src/bin/lora_p2p_receive.rs +++ b/examples/rp/src/bin/lora_p2p_receive.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. |
| 2 | //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. | 2 | //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. |
| 3 | |||
| 3 | #![no_std] | 4 | #![no_std] |
| 4 | #![no_main] | 5 | #![no_main] |
| 5 | #![macro_use] | 6 | #![macro_use] |
diff --git a/examples/rp/src/bin/lora_p2p_send.rs b/examples/rp/src/bin/lora_p2p_send.rs index 3a0544b17..a0f70fa5c 100644 --- a/examples/rp/src/bin/lora_p2p_send.rs +++ b/examples/rp/src/bin/lora_p2p_send.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. |
| 2 | //! It demonstrates LORA P2P send functionality. | 2 | //! It demonstrates LORA P2P send functionality. |
| 3 | |||
| 3 | #![no_std] | 4 | #![no_std] |
| 4 | #![no_main] | 5 | #![no_main] |
| 5 | #![macro_use] | 6 | #![macro_use] |
diff --git a/examples/rp/src/bin/lora_p2p_send_multicore.rs b/examples/rp/src/bin/lora_p2p_send_multicore.rs index eef2f7a53..89a62818d 100644 --- a/examples/rp/src/bin/lora_p2p_send_multicore.rs +++ b/examples/rp/src/bin/lora_p2p_send_multicore.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. |
| 2 | //! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core. | 2 | //! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core. |
| 3 | |||
| 3 | #![no_std] | 4 | #![no_std] |
| 4 | #![no_main] | 5 | #![no_main] |
| 5 | #![macro_use] | 6 | #![macro_use] |
diff --git a/examples/rp/src/bin/multicore.rs b/examples/rp/src/bin/multicore.rs index 57278dd6c..893b724bf 100644 --- a/examples/rp/src/bin/multicore.rs +++ b/examples/rp/src/bin/multicore.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to send messages between the two cores in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/pio_async.rs b/examples/rp/src/bin/pio_async.rs index 69034c92a..c001d6440 100644 --- a/examples/rp/src/bin/pio_async.rs +++ b/examples/rp/src/bin/pio_async.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | //! This example shows powerful PIO module in the RP2040 chip. | ||
| 2 | |||
| 1 | #![no_std] | 3 | #![no_std] |
| 2 | #![no_main] | 4 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 5 | #![feature(type_alias_impl_trait)] |
| @@ -54,7 +56,14 @@ fn setup_pio_task_sm1<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a, | |||
| 54 | // Setupm sm1 | 56 | // Setupm sm1 |
| 55 | 57 | ||
| 56 | // Read 0b10101 repeatedly until ISR is full | 58 | // Read 0b10101 repeatedly until ISR is full |
| 57 | let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",); | 59 | let prg = pio_proc::pio_asm!( |
| 60 | // | ||
| 61 | ".origin 8", | ||
| 62 | "set x, 0x15", | ||
| 63 | ".wrap_target", | ||
| 64 | "in x, 5 [31]", | ||
| 65 | ".wrap", | ||
| 66 | ); | ||
| 58 | 67 | ||
| 59 | let relocated = RelocatedProgram::new(&prg.program); | 68 | let relocated = RelocatedProgram::new(&prg.program); |
| 60 | let mut cfg = Config::default(); | 69 | let mut cfg = Config::default(); |
diff --git a/examples/rp/src/bin/pio_dma.rs b/examples/rp/src/bin/pio_dma.rs index 80c963556..9ab72e1f3 100644 --- a/examples/rp/src/bin/pio_dma.rs +++ b/examples/rp/src/bin/pio_dma.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | //! This example shows powerful PIO module in the RP2040 chip. | ||
| 2 | |||
| 1 | #![no_std] | 3 | #![no_std] |
| 2 | #![no_main] | 4 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 5 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/pio_hd44780.rs b/examples/rp/src/bin/pio_hd44780.rs index 0a4514a66..8aedd24b6 100644 --- a/examples/rp/src/bin/pio_hd44780.rs +++ b/examples/rp/src/bin/pio_hd44780.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example shows powerful PIO module in the RP2040 chip to communicate with a HD44780 display. | ||
| 2 | //! See (https://www.sparkfun.com/datasheets/LCD/HD44780.pdf) | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/pio_ws2812.rs b/examples/rp/src/bin/pio_ws2812.rs index 4a111e7aa..3de2bd48d 100644 --- a/examples/rp/src/bin/pio_ws2812.rs +++ b/examples/rp/src/bin/pio_ws2812.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example shows powerful PIO module in the RP2040 chip to communicate with WS2812 LED modules. | ||
| 2 | //! See (https://www.sparkfun.com/categories/tags/ws2812) | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs index 2b3d5d97a..9d919287c 100644 --- a/examples/rp/src/bin/pwm.rs +++ b/examples/rp/src/bin/pwm.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/rtc.rs b/examples/rp/src/bin/rtc.rs new file mode 100644 index 000000000..15aa8243f --- /dev/null +++ b/examples/rp/src/bin/rtc.rs | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | //! This example shows how to use RTC (Real Time Clock) in the RP2040 chip. | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_rp::rtc::{DateTime, DayOfWeek, Rtc}; | ||
| 10 | use embassy_time::{Duration, Timer}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let p = embassy_rp::init(Default::default()); | ||
| 16 | info!("Wait for 20s"); | ||
| 17 | |||
| 18 | let mut rtc = Rtc::new(p.RTC); | ||
| 19 | |||
| 20 | if !rtc.is_running() { | ||
| 21 | info!("Start RTC"); | ||
| 22 | let now = DateTime { | ||
| 23 | year: 2000, | ||
| 24 | month: 1, | ||
| 25 | day: 1, | ||
| 26 | day_of_week: DayOfWeek::Saturday, | ||
| 27 | hour: 0, | ||
| 28 | minute: 0, | ||
| 29 | second: 0, | ||
| 30 | }; | ||
| 31 | rtc.set_datetime(now).unwrap(); | ||
| 32 | } | ||
| 33 | |||
| 34 | Timer::after(Duration::from_millis(20000)).await; | ||
| 35 | |||
| 36 | if let Ok(dt) = rtc.now() { | ||
| 37 | info!( | ||
| 38 | "Now: {}-{:02}-{:02} {}:{:02}:{:02}", | ||
| 39 | dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, | ||
| 40 | ); | ||
| 41 | } | ||
| 42 | |||
| 43 | info!("Reboot."); | ||
| 44 | Timer::after(Duration::from_millis(200)).await; | ||
| 45 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 46 | } | ||
diff --git a/examples/rp/src/bin/spi.rs b/examples/rp/src/bin/spi.rs index a830a17a2..602348f7a 100644 --- a/examples/rp/src/bin/spi.rs +++ b/examples/rp/src/bin/spi.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! Example for resistive touch sensor in Waveshare Pico-ResTouch | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/spi_async.rs b/examples/rp/src/bin/spi_async.rs index 671a9caaf..328074e8b 100644 --- a/examples/rp/src/bin/spi_async.rs +++ b/examples/rp/src/bin/spi_async.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip. | ||
| 2 | //! No specific hardware is specified in this example. If you connect pin 11 and 12 you should get the same data back. | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/spi_display.rs b/examples/rp/src/bin/spi_display.rs index 2fd201595..26c258e1c 100644 --- a/examples/rp/src/bin/spi_display.rs +++ b/examples/rp/src/bin/spi_display.rs | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | //! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! Example written for a display using the ST7789 chip. Possibly the Waveshare Pico-ResTouch | ||
| 4 | //! (https://www.waveshare.com/wiki/Pico-ResTouch-LCD-2.8) | ||
| 5 | |||
| 1 | #![no_std] | 6 | #![no_std] |
| 2 | #![no_main] | 7 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 8 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/uart.rs b/examples/rp/src/bin/uart.rs index 05177a6b4..451c3c396 100644 --- a/examples/rp/src/bin/uart.rs +++ b/examples/rp/src/bin/uart.rs | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! No specific hardware is specified in this example. Only output on pin 0 is tested. | ||
| 4 | //! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used | ||
| 5 | //! with its UART port. | ||
| 6 | |||
| 1 | #![no_std] | 7 | #![no_std] |
| 2 | #![no_main] | 8 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 9 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/uart_buffered_split.rs b/examples/rp/src/bin/uart_buffered_split.rs index 9df99bd58..735201718 100644 --- a/examples/rp/src/bin/uart_buffered_split.rs +++ b/examples/rp/src/bin/uart_buffered_split.rs | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! No specific hardware is specified in this example. If you connect pin 0 and 1 you should get the same data back. | ||
| 4 | //! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used | ||
| 5 | //! with its UART port. | ||
| 6 | |||
| 1 | #![no_std] | 7 | #![no_std] |
| 2 | #![no_main] | 8 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 9 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/uart_unidir.rs b/examples/rp/src/bin/uart_unidir.rs index c0943a1b8..c1515a911 100644 --- a/examples/rp/src/bin/uart_unidir.rs +++ b/examples/rp/src/bin/uart_unidir.rs | |||
| @@ -1,5 +1,9 @@ | |||
| 1 | //! test TX-only and RX-only UARTs. You need to connect GPIO0 to GPIO5 for | 1 | //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip. |
| 2 | //! | ||
| 3 | //! Test TX-only and RX-only on two different UARTs. You need to connect GPIO0 to GPIO5 for | ||
| 2 | //! this to work | 4 | //! this to work |
| 5 | //! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used | ||
| 6 | //! with its UART port. | ||
| 3 | 7 | ||
| 4 | #![no_std] | 8 | #![no_std] |
| 5 | #![no_main] | 9 | #![no_main] |
diff --git a/examples/rp/src/bin/usb_ethernet.rs b/examples/rp/src/bin/usb_ethernet.rs index 91d1ec8e7..0a08f667e 100644 --- a/examples/rp/src/bin/usb_ethernet.rs +++ b/examples/rp/src/bin/usb_ethernet.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! This is a CDC-NCM class implementation, aka Ethernet over USB. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/usb_logger.rs b/examples/rp/src/bin/usb_logger.rs index 7c90d0ca3..9c5e6897d 100644 --- a/examples/rp/src/bin/usb_logger.rs +++ b/examples/rp/src/bin/usb_logger.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! This creates the possibility to send log::info/warn/error/debug! to USB serial port. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs index ca728536c..164e2052d 100644 --- a/examples/rp/src/bin/usb_serial.rs +++ b/examples/rp/src/bin/usb_serial.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! This creates a USB serial port that echos. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/watchdog.rs b/examples/rp/src/bin/watchdog.rs index ece5cfe38..fe5eaf926 100644 --- a/examples/rp/src/bin/watchdog.rs +++ b/examples/rp/src/bin/watchdog.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example shows how to use Watchdog in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! It does not work with the RP Pico W board. See wifi_blinky.rs or connect external LED and resistor. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/wifi_ap_tcp_server.rs b/examples/rp/src/bin/wifi_ap_tcp_server.rs index 3e41f83be..e3e393445 100644 --- a/examples/rp/src/bin/wifi_ap_tcp_server.rs +++ b/examples/rp/src/bin/wifi_ap_tcp_server.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example uses the RP Pico W board Wifi chip (cyw43). | ||
| 2 | //! Creates an Access point Wifi network and creates a TCP endpoint on port 1234. | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/wifi_blinky.rs b/examples/rp/src/bin/wifi_blinky.rs index 6eb207af6..33d43788c 100644 --- a/examples/rp/src/bin/wifi_blinky.rs +++ b/examples/rp/src/bin/wifi_blinky.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! This example test the RP Pico W on board LED. | ||
| 2 | //! | ||
| 3 | //! It does not work with the RP Pico board. See blinky.rs. | ||
| 4 | |||
| 1 | #![no_std] | 5 | #![no_std] |
| 2 | #![no_main] | 6 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 7 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/wifi_scan.rs b/examples/rp/src/bin/wifi_scan.rs index aef18aa24..743fab617 100644 --- a/examples/rp/src/bin/wifi_scan.rs +++ b/examples/rp/src/bin/wifi_scan.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example uses the RP Pico W board Wifi chip (cyw43). | ||
| 2 | //! Scans Wifi for ssid names. | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/rp/src/bin/wifi_tcp_server.rs b/examples/rp/src/bin/wifi_tcp_server.rs index 4fce74a66..0223a3636 100644 --- a/examples/rp/src/bin/wifi_tcp_server.rs +++ b/examples/rp/src/bin/wifi_tcp_server.rs | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | //! This example uses the RP Pico W board Wifi chip (cyw43). | ||
| 2 | //! Connects to specified Wifi network and creates a TCP endpoint on port 1234. | ||
| 3 | |||
| 1 | #![no_std] | 4 | #![no_std] |
| 2 | #![no_main] | 5 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 6 | #![feature(type_alias_impl_trait)] |
diff --git a/examples/stm32wb/Cargo.toml b/examples/stm32wb/Cargo.toml index f23c8afa6..becf2d3fb 100644 --- a/examples/stm32wb/Cargo.toml +++ b/examples/stm32wb/Cargo.toml | |||
| @@ -43,5 +43,6 @@ required-features = ["mac"] | |||
| 43 | name = "eddystone_beacon" | 43 | name = "eddystone_beacon" |
| 44 | required-features = ["ble"] | 44 | required-features = ["ble"] |
| 45 | 45 | ||
| 46 | [patch.crates-io] | 46 | [[bin]] |
| 47 | stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"} \ No newline at end of file | 47 | name = "gatt_server" |
| 48 | required-features = ["ble"] \ No newline at end of file | ||
diff --git a/examples/stm32wb/src/bin/gatt_server.rs b/examples/stm32wb/src/bin/gatt_server.rs new file mode 100644 index 000000000..7621efb11 --- /dev/null +++ b/examples/stm32wb/src/bin/gatt_server.rs | |||
| @@ -0,0 +1,397 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::time::Duration; | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::bind_interrupts; | ||
| 10 | use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; | ||
| 11 | use embassy_stm32_wpan::hci::event::command::{CommandComplete, ReturnParameters}; | ||
| 12 | use embassy_stm32_wpan::hci::host::uart::{Packet, UartHci}; | ||
| 13 | use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; | ||
| 14 | use embassy_stm32_wpan::hci::types::AdvertisingType; | ||
| 15 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{ | ||
| 16 | AddressType, AuthenticationRequirements, DiscoverableParameters, GapCommands, IoCapability, LocalName, Pin, Role, | ||
| 17 | SecureConnectionSupport, | ||
| 18 | }; | ||
| 19 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::{ | ||
| 20 | AddCharacteristicParameters, AddServiceParameters, CharacteristicEvent, CharacteristicPermission, | ||
| 21 | CharacteristicProperty, EncryptionKeySize, GattCommands, ServiceType, UpdateCharacteristicValueParameters, Uuid, | ||
| 22 | WriteResponseParameters, | ||
| 23 | }; | ||
| 24 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; | ||
| 25 | use embassy_stm32_wpan::hci::vendor::stm32wb::event::{self, AttributeHandle, Stm32Wb5xEvent}; | ||
| 26 | use embassy_stm32_wpan::hci::{BdAddr, Event}; | ||
| 27 | use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; | ||
| 28 | use embassy_stm32_wpan::sub::ble::Ble; | ||
| 29 | use embassy_stm32_wpan::TlMbox; | ||
| 30 | use {defmt_rtt as _, panic_probe as _}; | ||
| 31 | |||
| 32 | bind_interrupts!(struct Irqs{ | ||
| 33 | IPCC_C1_RX => ReceiveInterruptHandler; | ||
| 34 | IPCC_C1_TX => TransmitInterruptHandler; | ||
| 35 | }); | ||
| 36 | |||
| 37 | const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7; | ||
| 38 | |||
| 39 | #[embassy_executor::main] | ||
| 40 | async fn main(_spawner: Spawner) { | ||
| 41 | /* | ||
| 42 | How to make this work: | ||
| 43 | |||
| 44 | - Obtain a NUCLEO-STM32WB55 from your preferred supplier. | ||
| 45 | - Download and Install STM32CubeProgrammer. | ||
| 46 | - Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from | ||
| 47 | gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x | ||
| 48 | - Open STM32CubeProgrammer | ||
| 49 | - On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware. | ||
| 50 | - Once complete, click connect to connect to the device. | ||
| 51 | - On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services". | ||
| 52 | - In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file | ||
| 53 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 54 | - Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the | ||
| 55 | stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address. | ||
| 56 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 57 | - Select "Start Wireless Stack". | ||
| 58 | - Disconnect from the device. | ||
| 59 | - In the examples folder for stm32wb, modify the memory.x file to match your target device. | ||
| 60 | - Run this example. | ||
| 61 | |||
| 62 | Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name. | ||
| 63 | */ | ||
| 64 | |||
| 65 | let p = embassy_stm32::init(Default::default()); | ||
| 66 | info!("Hello World!"); | ||
| 67 | |||
| 68 | let config = Config::default(); | ||
| 69 | let mut mbox = TlMbox::init(p.IPCC, Irqs, config); | ||
| 70 | |||
| 71 | let sys_event = mbox.sys_subsystem.read().await; | ||
| 72 | info!("sys event: {}", sys_event.payload()); | ||
| 73 | |||
| 74 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; | ||
| 75 | |||
| 76 | info!("resetting BLE..."); | ||
| 77 | mbox.ble_subsystem.reset().await; | ||
| 78 | let response = mbox.ble_subsystem.read().await; | ||
| 79 | defmt::debug!("{}", response); | ||
| 80 | |||
| 81 | info!("config public address..."); | ||
| 82 | mbox.ble_subsystem | ||
| 83 | .write_config_data(&ConfigData::public_address(get_bd_addr()).build()) | ||
| 84 | .await; | ||
| 85 | let response = mbox.ble_subsystem.read().await; | ||
| 86 | defmt::debug!("{}", response); | ||
| 87 | |||
| 88 | info!("config random address..."); | ||
| 89 | mbox.ble_subsystem | ||
| 90 | .write_config_data(&ConfigData::random_address(get_random_addr()).build()) | ||
| 91 | .await; | ||
| 92 | let response = mbox.ble_subsystem.read().await; | ||
| 93 | defmt::debug!("{}", response); | ||
| 94 | |||
| 95 | info!("config identity root..."); | ||
| 96 | mbox.ble_subsystem | ||
| 97 | .write_config_data(&ConfigData::identity_root(&get_irk()).build()) | ||
| 98 | .await; | ||
| 99 | let response = mbox.ble_subsystem.read().await; | ||
| 100 | defmt::debug!("{}", response); | ||
| 101 | |||
| 102 | info!("config encryption root..."); | ||
| 103 | mbox.ble_subsystem | ||
| 104 | .write_config_data(&ConfigData::encryption_root(&get_erk()).build()) | ||
| 105 | .await; | ||
| 106 | let response = mbox.ble_subsystem.read().await; | ||
| 107 | defmt::debug!("{}", response); | ||
| 108 | |||
| 109 | info!("config tx power level..."); | ||
| 110 | mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await; | ||
| 111 | let response = mbox.ble_subsystem.read().await; | ||
| 112 | defmt::debug!("{}", response); | ||
| 113 | |||
| 114 | info!("GATT init..."); | ||
| 115 | mbox.ble_subsystem.init_gatt().await; | ||
| 116 | let response = mbox.ble_subsystem.read().await; | ||
| 117 | defmt::debug!("{}", response); | ||
| 118 | |||
| 119 | info!("GAP init..."); | ||
| 120 | mbox.ble_subsystem | ||
| 121 | .init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH) | ||
| 122 | .await; | ||
| 123 | let response = mbox.ble_subsystem.read().await; | ||
| 124 | defmt::debug!("{}", response); | ||
| 125 | |||
| 126 | info!("set IO capabilities..."); | ||
| 127 | mbox.ble_subsystem.set_io_capability(IoCapability::DisplayConfirm).await; | ||
| 128 | let response = mbox.ble_subsystem.read().await; | ||
| 129 | defmt::debug!("{}", response); | ||
| 130 | |||
| 131 | info!("set authentication requirements..."); | ||
| 132 | mbox.ble_subsystem | ||
| 133 | .set_authentication_requirement(&AuthenticationRequirements { | ||
| 134 | bonding_required: false, | ||
| 135 | keypress_notification_support: false, | ||
| 136 | mitm_protection_required: false, | ||
| 137 | encryption_key_size_range: (8, 16), | ||
| 138 | fixed_pin: Pin::Requested, | ||
| 139 | identity_address_type: AddressType::Public, | ||
| 140 | secure_connection_support: SecureConnectionSupport::Optional, | ||
| 141 | }) | ||
| 142 | .await | ||
| 143 | .unwrap(); | ||
| 144 | let response = mbox.ble_subsystem.read().await; | ||
| 145 | defmt::debug!("{}", response); | ||
| 146 | |||
| 147 | info!("set scan response data..."); | ||
| 148 | mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap(); | ||
| 149 | let response = mbox.ble_subsystem.read().await; | ||
| 150 | defmt::debug!("{}", response); | ||
| 151 | |||
| 152 | info!("set scan response data..."); | ||
| 153 | mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap(); | ||
| 154 | let response = mbox.ble_subsystem.read().await; | ||
| 155 | defmt::debug!("{}", response); | ||
| 156 | |||
| 157 | defmt::info!("initializing services and characteristics..."); | ||
| 158 | let mut ble_context = init_gatt_services(&mut mbox.ble_subsystem).await.unwrap(); | ||
| 159 | defmt::info!("{}", ble_context); | ||
| 160 | |||
| 161 | let discovery_params = DiscoverableParameters { | ||
| 162 | advertising_type: AdvertisingType::ConnectableUndirected, | ||
| 163 | advertising_interval: Some((Duration::from_millis(100), Duration::from_millis(100))), | ||
| 164 | address_type: OwnAddressType::Public, | ||
| 165 | filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan, | ||
| 166 | local_name: Some(LocalName::Complete(b"TXTX")), | ||
| 167 | advertising_data: &[], | ||
| 168 | conn_interval: (None, None), | ||
| 169 | }; | ||
| 170 | |||
| 171 | info!("set discoverable..."); | ||
| 172 | mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap(); | ||
| 173 | let response = mbox.ble_subsystem.read().await; | ||
| 174 | defmt::debug!("{}", response); | ||
| 175 | |||
| 176 | loop { | ||
| 177 | let response = mbox.ble_subsystem.read().await; | ||
| 178 | defmt::debug!("{}", response); | ||
| 179 | |||
| 180 | if let Ok(Packet::Event(event)) = response { | ||
| 181 | match event { | ||
| 182 | Event::LeConnectionComplete(_) => { | ||
| 183 | defmt::info!("connected"); | ||
| 184 | } | ||
| 185 | Event::DisconnectionComplete(_) => { | ||
| 186 | defmt::info!("disconnected"); | ||
| 187 | ble_context.is_subscribed = false; | ||
| 188 | mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap(); | ||
| 189 | } | ||
| 190 | Event::Vendor(vendor_event) => match vendor_event { | ||
| 191 | Stm32Wb5xEvent::AttReadPermitRequest(read_req) => { | ||
| 192 | defmt::info!("read request received {}, allowing", read_req); | ||
| 193 | mbox.ble_subsystem.allow_read(read_req.conn_handle).await | ||
| 194 | } | ||
| 195 | Stm32Wb5xEvent::AttWritePermitRequest(write_req) => { | ||
| 196 | defmt::info!("write request received {}, allowing", write_req); | ||
| 197 | mbox.ble_subsystem | ||
| 198 | .write_response(&WriteResponseParameters { | ||
| 199 | conn_handle: write_req.conn_handle, | ||
| 200 | attribute_handle: write_req.attribute_handle, | ||
| 201 | status: Ok(()), | ||
| 202 | value: write_req.value(), | ||
| 203 | }) | ||
| 204 | .await | ||
| 205 | .unwrap() | ||
| 206 | } | ||
| 207 | Stm32Wb5xEvent::GattAttributeModified(attribute) => { | ||
| 208 | defmt::info!("{}", ble_context); | ||
| 209 | if attribute.attr_handle.0 == ble_context.chars.notify.0 + 2 { | ||
| 210 | if attribute.data()[0] == 0x01 { | ||
| 211 | defmt::info!("subscribed"); | ||
| 212 | ble_context.is_subscribed = true; | ||
| 213 | } else { | ||
| 214 | defmt::info!("unsubscribed"); | ||
| 215 | ble_context.is_subscribed = false; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | _ => {} | ||
| 220 | }, | ||
| 221 | _ => {} | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | fn get_bd_addr() -> BdAddr { | ||
| 228 | let mut bytes = [0u8; 6]; | ||
| 229 | |||
| 230 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 231 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 232 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 233 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 234 | bytes[3] = lhci_info.device_type_id; | ||
| 235 | bytes[4] = (lhci_info.st_company_id & 0xff) as u8; | ||
| 236 | bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8; | ||
| 237 | |||
| 238 | BdAddr(bytes) | ||
| 239 | } | ||
| 240 | |||
| 241 | fn get_random_addr() -> BdAddr { | ||
| 242 | let mut bytes = [0u8; 6]; | ||
| 243 | |||
| 244 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 245 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 246 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 247 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 248 | bytes[3] = 0; | ||
| 249 | bytes[4] = 0x6E; | ||
| 250 | bytes[5] = 0xED; | ||
| 251 | |||
| 252 | BdAddr(bytes) | ||
| 253 | } | ||
| 254 | |||
| 255 | const BLE_CFG_IRK: [u8; 16] = [ | ||
| 256 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, | ||
| 257 | ]; | ||
| 258 | const BLE_CFG_ERK: [u8; 16] = [ | ||
| 259 | 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, | ||
| 260 | ]; | ||
| 261 | |||
| 262 | fn get_irk() -> EncryptionKey { | ||
| 263 | EncryptionKey(BLE_CFG_IRK) | ||
| 264 | } | ||
| 265 | |||
| 266 | fn get_erk() -> EncryptionKey { | ||
| 267 | EncryptionKey(BLE_CFG_ERK) | ||
| 268 | } | ||
| 269 | |||
| 270 | #[derive(defmt::Format)] | ||
| 271 | pub struct BleContext { | ||
| 272 | pub service_handle: AttributeHandle, | ||
| 273 | pub chars: CharHandles, | ||
| 274 | pub is_subscribed: bool, | ||
| 275 | } | ||
| 276 | |||
| 277 | #[derive(defmt::Format)] | ||
| 278 | pub struct CharHandles { | ||
| 279 | pub read: AttributeHandle, | ||
| 280 | pub write: AttributeHandle, | ||
| 281 | pub notify: AttributeHandle, | ||
| 282 | } | ||
| 283 | |||
| 284 | pub async fn init_gatt_services(ble_subsystem: &mut Ble) -> Result<BleContext, ()> { | ||
| 285 | let service_handle = gatt_add_service(ble_subsystem, Uuid::Uuid16(0x500)).await?; | ||
| 286 | |||
| 287 | let read = gatt_add_char( | ||
| 288 | ble_subsystem, | ||
| 289 | service_handle, | ||
| 290 | Uuid::Uuid16(0x501), | ||
| 291 | CharacteristicProperty::READ, | ||
| 292 | Some(b"Hello from embassy!"), | ||
| 293 | ) | ||
| 294 | .await?; | ||
| 295 | |||
| 296 | let write = gatt_add_char( | ||
| 297 | ble_subsystem, | ||
| 298 | service_handle, | ||
| 299 | Uuid::Uuid16(0x502), | ||
| 300 | CharacteristicProperty::WRITE_WITHOUT_RESPONSE | CharacteristicProperty::WRITE | CharacteristicProperty::READ, | ||
| 301 | None, | ||
| 302 | ) | ||
| 303 | .await?; | ||
| 304 | |||
| 305 | let notify = gatt_add_char( | ||
| 306 | ble_subsystem, | ||
| 307 | service_handle, | ||
| 308 | Uuid::Uuid16(0x503), | ||
| 309 | CharacteristicProperty::NOTIFY | CharacteristicProperty::READ, | ||
| 310 | None, | ||
| 311 | ) | ||
| 312 | .await?; | ||
| 313 | |||
| 314 | Ok(BleContext { | ||
| 315 | service_handle, | ||
| 316 | is_subscribed: false, | ||
| 317 | chars: CharHandles { read, write, notify }, | ||
| 318 | }) | ||
| 319 | } | ||
| 320 | |||
| 321 | async fn gatt_add_service(ble_subsystem: &mut Ble, uuid: Uuid) -> Result<AttributeHandle, ()> { | ||
| 322 | ble_subsystem | ||
| 323 | .add_service(&AddServiceParameters { | ||
| 324 | uuid, | ||
| 325 | service_type: ServiceType::Primary, | ||
| 326 | max_attribute_records: 8, | ||
| 327 | }) | ||
| 328 | .await; | ||
| 329 | let response = ble_subsystem.read().await; | ||
| 330 | defmt::debug!("{}", response); | ||
| 331 | |||
| 332 | if let Ok(Packet::Event(Event::CommandComplete(CommandComplete { | ||
| 333 | return_params: | ||
| 334 | ReturnParameters::Vendor(event::command::ReturnParameters::GattAddService(event::command::GattService { | ||
| 335 | service_handle, | ||
| 336 | .. | ||
| 337 | })), | ||
| 338 | .. | ||
| 339 | }))) = response | ||
| 340 | { | ||
| 341 | Ok(service_handle) | ||
| 342 | } else { | ||
| 343 | Err(()) | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | async fn gatt_add_char( | ||
| 348 | ble_subsystem: &mut Ble, | ||
| 349 | service_handle: AttributeHandle, | ||
| 350 | characteristic_uuid: Uuid, | ||
| 351 | characteristic_properties: CharacteristicProperty, | ||
| 352 | default_value: Option<&[u8]>, | ||
| 353 | ) -> Result<AttributeHandle, ()> { | ||
| 354 | ble_subsystem | ||
| 355 | .add_characteristic(&AddCharacteristicParameters { | ||
| 356 | service_handle, | ||
| 357 | characteristic_uuid, | ||
| 358 | characteristic_properties, | ||
| 359 | characteristic_value_len: 32, | ||
| 360 | security_permissions: CharacteristicPermission::empty(), | ||
| 361 | gatt_event_mask: CharacteristicEvent::all(), | ||
| 362 | encryption_key_size: EncryptionKeySize::with_value(7).unwrap(), | ||
| 363 | is_variable: true, | ||
| 364 | }) | ||
| 365 | .await; | ||
| 366 | let response = ble_subsystem.read().await; | ||
| 367 | defmt::debug!("{}", response); | ||
| 368 | |||
| 369 | if let Ok(Packet::Event(Event::CommandComplete(CommandComplete { | ||
| 370 | return_params: | ||
| 371 | ReturnParameters::Vendor(event::command::ReturnParameters::GattAddCharacteristic( | ||
| 372 | event::command::GattCharacteristic { | ||
| 373 | characteristic_handle, .. | ||
| 374 | }, | ||
| 375 | )), | ||
| 376 | .. | ||
| 377 | }))) = response | ||
| 378 | { | ||
| 379 | if let Some(value) = default_value { | ||
| 380 | ble_subsystem | ||
| 381 | .update_characteristic_value(&UpdateCharacteristicValueParameters { | ||
| 382 | service_handle, | ||
| 383 | characteristic_handle, | ||
| 384 | offset: 0, | ||
| 385 | value, | ||
| 386 | }) | ||
| 387 | .await | ||
| 388 | .unwrap(); | ||
| 389 | |||
| 390 | let response = ble_subsystem.read().await; | ||
| 391 | defmt::debug!("{}", response); | ||
| 392 | } | ||
| 393 | Ok(characteristic_handle) | ||
| 394 | } else { | ||
| 395 | Err(()) | ||
| 396 | } | ||
| 397 | } | ||
diff --git a/examples/stm32wl/.cargo/config.toml b/examples/stm32wl/.cargo/config.toml index 4f8094ff2..ee416fcbc 100644 --- a/examples/stm32wl/.cargo/config.toml +++ b/examples/stm32wl/.cargo/config.toml | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | runner = "probe-rs run --chip STM32WLE5JCIx" | 3 | runner = "probe-rs run --chip STM32WLE5JCIx" |
| 4 | 4 | ||
| 5 | [build] | 5 | [build] |
| 6 | target = "thumbv7em-none-eabihf" | 6 | target = "thumbv7em-none-eabi" |
| 7 | 7 | ||
| 8 | [env] | 8 | [env] |
| 9 | DEFMT_LOG = "trace" | 9 | DEFMT_LOG = "trace" |
diff --git a/examples/stm32wl/Cargo.toml b/examples/stm32wl/Cargo.toml index 99f68387f..e2c66f456 100644 --- a/examples/stm32wl/Cargo.toml +++ b/examples/stm32wl/Cargo.toml | |||
| @@ -8,8 +8,8 @@ license = "MIT OR Apache-2.0" | |||
| 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti", "chrono"] } |
| 12 | embassy-embedded-hal = {version = "0.1.0", path = "../../embassy-embedded-hal" } | 12 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } |
| 13 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } | 13 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } |
| 14 | lora-phy = { version = "1" } | 14 | lora-phy = { version = "1" } |
| 15 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } | 15 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } |
| @@ -25,6 +25,7 @@ embedded-storage = "0.3.0" | |||
| 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 27 | heapless = { version = "0.7.5", default-features = false } | 27 | heapless = { version = "0.7.5", default-features = false } |
| 28 | chrono = { version = "^0.4", default-features = false } | ||
| 28 | 29 | ||
| 29 | [patch.crates-io] | 30 | [patch.crates-io] |
| 30 | lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" } | 31 | lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" } |
diff --git a/examples/stm32wl/src/bin/rtc.rs b/examples/stm32wl/src/bin/rtc.rs new file mode 100644 index 000000000..e11825499 --- /dev/null +++ b/examples/stm32wl/src/bin/rtc.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use chrono::{NaiveDate, NaiveDateTime}; | ||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::{self, ClockSrc}; | ||
| 9 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 10 | use embassy_stm32::Config; | ||
| 11 | use embassy_time::{Duration, Timer}; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) { | ||
| 16 | let p = { | ||
| 17 | let mut config = Config::default(); | ||
| 18 | config.rcc.mux = ClockSrc::HSE32; | ||
| 19 | config.rcc.rtc_mux = rcc::RtcClockSource::LSE32; | ||
| 20 | config.rcc.enable_rtc_apb = true; | ||
| 21 | embassy_stm32::init(config) | ||
| 22 | }; | ||
| 23 | info!("Hello World!"); | ||
| 24 | |||
| 25 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) | ||
| 26 | .unwrap() | ||
| 27 | .and_hms_opt(10, 30, 15) | ||
| 28 | .unwrap(); | ||
| 29 | |||
| 30 | let mut rtc = Rtc::new( | ||
| 31 | p.RTC, | ||
| 32 | RtcConfig::default().clock_config(embassy_stm32::rtc::RtcClockSource::LSE), | ||
| 33 | ); | ||
| 34 | info!("Got RTC! {:?}", now.timestamp()); | ||
| 35 | |||
| 36 | rtc.set_datetime(now.into()).expect("datetime not set"); | ||
| 37 | |||
| 38 | // In reality the delay would be much longer | ||
| 39 | Timer::after(Duration::from_millis(20000)).await; | ||
| 40 | |||
| 41 | let then: NaiveDateTime = rtc.now().unwrap().into(); | ||
| 42 | info!("Got RTC! {:?}", then.timestamp()); | ||
| 43 | } | ||
diff --git a/tests/rp/src/bin/gpio.rs b/tests/rp/src/bin/gpio.rs index 51112d319..946b7dc88 100644 --- a/tests/rp/src/bin/gpio.rs +++ b/tests/rp/src/bin/gpio.rs | |||
| @@ -21,14 +21,46 @@ async fn main(_spawner: Spawner) { | |||
| 21 | let b = Input::new(&mut b, Pull::None); | 21 | let b = Input::new(&mut b, Pull::None); |
| 22 | 22 | ||
| 23 | { | 23 | { |
| 24 | let _a = Output::new(&mut a, Level::Low); | 24 | let a = Output::new(&mut a, Level::Low); |
| 25 | delay(); | 25 | delay(); |
| 26 | assert!(b.is_low()); | 26 | assert!(b.is_low()); |
| 27 | assert!(!b.is_high()); | ||
| 28 | assert!(a.is_set_low()); | ||
| 29 | assert!(!a.is_set_high()); | ||
| 27 | } | 30 | } |
| 28 | { | 31 | { |
| 29 | let _a = Output::new(&mut a, Level::High); | 32 | let mut a = Output::new(&mut a, Level::High); |
| 30 | delay(); | 33 | delay(); |
| 34 | assert!(!b.is_low()); | ||
| 31 | assert!(b.is_high()); | 35 | assert!(b.is_high()); |
| 36 | assert!(!a.is_set_low()); | ||
| 37 | assert!(a.is_set_high()); | ||
| 38 | |||
| 39 | // Test is_set_low / is_set_high | ||
| 40 | a.set_low(); | ||
| 41 | delay(); | ||
| 42 | assert!(b.is_low()); | ||
| 43 | assert!(a.is_set_low()); | ||
| 44 | assert!(!a.is_set_high()); | ||
| 45 | |||
| 46 | a.set_high(); | ||
| 47 | delay(); | ||
| 48 | assert!(b.is_high()); | ||
| 49 | assert!(!a.is_set_low()); | ||
| 50 | assert!(a.is_set_high()); | ||
| 51 | |||
| 52 | // Test toggle | ||
| 53 | a.toggle(); | ||
| 54 | delay(); | ||
| 55 | assert!(b.is_low()); | ||
| 56 | assert!(a.is_set_low()); | ||
| 57 | assert!(!a.is_set_high()); | ||
| 58 | |||
| 59 | a.toggle(); | ||
| 60 | delay(); | ||
| 61 | assert!(b.is_high()); | ||
| 62 | assert!(!a.is_set_low()); | ||
| 63 | assert!(a.is_set_high()); | ||
| 32 | } | 64 | } |
| 33 | } | 65 | } |
| 34 | 66 | ||
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 4fd4a6d0b..d94bd730b 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -46,9 +46,6 @@ rand_chacha = { version = "0.3", default-features = false } | |||
| 46 | 46 | ||
| 47 | chrono = { version = "^0.4", default-features = false, optional = true} | 47 | chrono = { version = "^0.4", default-features = false, optional = true} |
| 48 | 48 | ||
| 49 | [patch.crates-io] | ||
| 50 | stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"} | ||
| 51 | |||
| 52 | # BEGIN TESTS | 49 | # BEGIN TESTS |
| 53 | # Generated by gen_test.py. DO NOT EDIT. | 50 | # Generated by gen_test.py. DO NOT EDIT. |
| 54 | [[bin]] | 51 | [[bin]] |
diff --git a/tests/stm32/src/bin/gpio.rs b/tests/stm32/src/bin/gpio.rs index 67f44317e..aad174431 100644 --- a/tests/stm32/src/bin/gpio.rs +++ b/tests/stm32/src/bin/gpio.rs | |||
| @@ -40,14 +40,46 @@ async fn main(_spawner: Spawner) { | |||
| 40 | let b = Input::new(&mut b, Pull::None); | 40 | let b = Input::new(&mut b, Pull::None); |
| 41 | 41 | ||
| 42 | { | 42 | { |
| 43 | let _a = Output::new(&mut a, Level::Low, Speed::Low); | 43 | let a = Output::new(&mut a, Level::Low, Speed::Low); |
| 44 | delay(); | 44 | delay(); |
| 45 | assert!(b.is_low()); | 45 | assert!(b.is_low()); |
| 46 | assert!(!b.is_high()); | ||
| 47 | assert!(a.is_set_low()); | ||
| 48 | assert!(!a.is_set_high()); | ||
| 46 | } | 49 | } |
| 47 | { | 50 | { |
| 48 | let _a = Output::new(&mut a, Level::High, Speed::Low); | 51 | let mut a = Output::new(&mut a, Level::High, Speed::Low); |
| 49 | delay(); | 52 | delay(); |
| 53 | assert!(!b.is_low()); | ||
| 50 | assert!(b.is_high()); | 54 | assert!(b.is_high()); |
| 55 | assert!(!a.is_set_low()); | ||
| 56 | assert!(a.is_set_high()); | ||
| 57 | |||
| 58 | // Test is_set_low / is_set_high | ||
| 59 | a.set_low(); | ||
| 60 | delay(); | ||
| 61 | assert!(b.is_low()); | ||
| 62 | assert!(a.is_set_low()); | ||
| 63 | assert!(!a.is_set_high()); | ||
| 64 | |||
| 65 | a.set_high(); | ||
| 66 | delay(); | ||
| 67 | assert!(b.is_high()); | ||
| 68 | assert!(!a.is_set_low()); | ||
| 69 | assert!(a.is_set_high()); | ||
| 70 | |||
| 71 | // Test toggle | ||
| 72 | a.toggle(); | ||
| 73 | delay(); | ||
| 74 | assert!(b.is_low()); | ||
| 75 | assert!(a.is_set_low()); | ||
| 76 | assert!(!a.is_set_high()); | ||
| 77 | |||
| 78 | a.toggle(); | ||
| 79 | delay(); | ||
| 80 | assert!(b.is_high()); | ||
| 81 | assert!(!a.is_set_low()); | ||
| 82 | assert!(a.is_set_high()); | ||
| 51 | } | 83 | } |
| 52 | } | 84 | } |
| 53 | 85 | ||
