Merge branch 'master' into mac-3

This commit is contained in:
xoviat 2023-07-15 14:18:01 -05:00
commit 8a146a50ec
65 changed files with 923 additions and 93 deletions

View File

@ -165,6 +165,9 @@ impl<F: Future + 'static> TaskStorage<F> {
Poll::Ready(_) => { Poll::Ready(_) => {
this.future.drop_in_place(); this.future.drop_in_place();
this.raw.state.fetch_and(!STATE_SPAWNED, Ordering::AcqRel); this.raw.state.fetch_and(!STATE_SPAWNED, Ordering::AcqRel);
#[cfg(feature = "integrated-timers")]
this.raw.expires_at.set(Instant::MAX);
} }
Poll::Pending => {} Poll::Pending => {}
} }

View File

@ -3,7 +3,7 @@
use core::cell::RefCell; use core::cell::RefCell;
use core::future::poll_fn; use core::future::poll_fn;
use core::mem; use core::mem;
use core::task::Poll; use core::task::{Context, Poll};
use embassy_net_driver::Driver; use embassy_net_driver::Driver;
use smoltcp::iface::{Interface, SocketHandle}; use smoltcp::iface::{Interface, SocketHandle};
@ -102,7 +102,17 @@ impl<'a> UdpSocket<'a> {
/// ///
/// Returns the number of bytes received and the remote endpoint. /// Returns the number of bytes received and the remote endpoint.
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
poll_fn(move |cx| { poll_fn(move |cx| self.poll_recv_from(buf, cx)).await
}
/// Receive a datagram.
///
/// When no datagram is available, this method will return `Poll::Pending` and
/// register the current task to be notified when a datagram is received.
///
/// When a datagram is received, this method will return `Poll::Ready` with the
/// number of bytes received and the remote endpoint.
pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), Error>> {
self.with_mut(|s, _| match s.recv_slice(buf) { self.with_mut(|s, _| match s.recv_slice(buf) {
Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))),
// No data ready // No data ready
@ -111,17 +121,33 @@ impl<'a> UdpSocket<'a> {
Poll::Pending Poll::Pending
} }
}) })
})
.await
} }
/// Send a datagram to the specified remote endpoint. /// Send a datagram to the specified remote endpoint.
///
/// This method will wait until the datagram has been sent.
///
/// When the remote endpoint is not reachable, this method will return `Err(Error::NoRoute)`
pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
where where
T: Into<IpEndpoint>, T: Into<IpEndpoint>,
{ {
let remote_endpoint = remote_endpoint.into(); let remote_endpoint: IpEndpoint = remote_endpoint.into();
poll_fn(move |cx| { poll_fn(move |cx| self.poll_send_to(buf, remote_endpoint, cx)).await
}
/// Send a datagram to the specified remote endpoint.
///
/// When the datagram has been sent, this method will return `Poll::Ready(Ok())`.
///
/// When the socket's send buffer is full, this method will return `Poll::Pending`
/// and register the current task to be notified when the buffer has space available.
///
/// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`.
pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
where
T: Into<IpEndpoint>,
{
self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) {
// Entire datagram has been sent // Entire datagram has been sent
Ok(()) => Poll::Ready(Ok(())), Ok(()) => Poll::Ready(Ok(())),
@ -131,8 +157,6 @@ impl<'a> UdpSocket<'a> {
} }
Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)),
}) })
})
.await
} }
/// Returns the local endpoint of the socket. /// Returns the local endpoint of the socket.

View File

@ -308,6 +308,7 @@ pub(crate) unsafe fn init(config: ClockConfig) {
// - QSPI (we're using it to run this code!) // - QSPI (we're using it to run this code!)
// - PLLs (it may be suicide if that's what's clocking us) // - PLLs (it may be suicide if that's what's clocking us)
// - USB, SYSCFG (breaks usb-to-swd on core1) // - USB, SYSCFG (breaks usb-to-swd on core1)
// - RTC (else there would be no more time...)
let mut peris = reset::ALL_PERIPHERALS; let mut peris = reset::ALL_PERIPHERALS;
peris.set_io_qspi(false); peris.set_io_qspi(false);
// peris.set_io_bank0(false); // might be suicide if we're clocked from gpin // 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) {
// TODO investigate if usb should be unreset here // TODO investigate if usb should be unreset here
peris.set_usbctrl(false); peris.set_usbctrl(false);
peris.set_syscfg(false); peris.set_syscfg(false);
peris.set_rtc(false);
reset::reset(peris); reset::reset(peris);
// Disable resus that may be enabled from previous software // Disable resus that may be enabled from previous software

View File

@ -566,13 +566,13 @@ impl<'d, T: Pin> Flex<'d, T> {
/// Is the output level high? /// Is the output level high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
(self.pin.sio_out().value().read() & self.bit()) == 0 !self.is_set_low()
} }
/// Is the output level low? /// Is the output level low?
#[inline] #[inline]
pub fn is_set_low(&self) -> bool { pub fn is_set_low(&self) -> bool {
!self.is_set_high() (self.pin.sio_out().value().read() & self.bit()) == 0
} }
/// What level output is set to /// What level output is set to

View File

@ -25,6 +25,7 @@ pub enum Error {
} }
/// Structure containing date and time information /// Structure containing date and time information
#[derive(Clone, Debug)]
pub struct DateTime { pub struct DateTime {
/// 0..4095 /// 0..4095
pub year: u16, pub year: u16,

View File

@ -12,26 +12,24 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
use crate::clocks::clk_rtc_freq; use crate::clocks::clk_rtc_freq;
/// A reference to the real time clock of the system /// A reference to the real time clock of the system
pub struct RealTimeClock<'d, T: Instance> { pub struct Rtc<'d, T: Instance> {
inner: PeripheralRef<'d, T>, inner: PeripheralRef<'d, T>,
} }
impl<'d, T: Instance> RealTimeClock<'d, T> { impl<'d, T: Instance> Rtc<'d, T> {
/// Create a new instance of the real time clock, with the given date as an initial value. /// Create a new instance of the real time clock, with the given date as an initial value.
/// ///
/// # Errors /// # Errors
/// ///
/// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range.
pub fn new(inner: impl Peripheral<P = T> + 'd, initial_date: DateTime) -> Result<Self, RtcError> { pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self {
into_ref!(inner); into_ref!(inner);
// Set the RTC divider // Set the RTC divider
inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1)); inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1));
let mut result = Self { inner }; let result = Self { inner };
result.set_leap_year_check(true); // should be on by default, make sure this is the case. result
result.set_datetime(initial_date)?;
Ok(result)
} }
/// 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. /// 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> {
}); });
} }
/// Checks to see if this RealTimeClock is running /// Set the time from internal format
pub fn restore(&mut self, ymd: rp_pac::rtc::regs::Rtc1, hms: rp_pac::rtc::regs::Rtc0) {
// disable RTC while we configure it
self.inner.regs().ctrl().modify(|w| w.set_rtc_enable(false));
while self.inner.regs().ctrl().read().rtc_active() {
core::hint::spin_loop();
}
self.inner.regs().setup_0().write(|w| {
*w = rp_pac::rtc::regs::Setup0(ymd.0);
});
self.inner.regs().setup_1().write(|w| {
*w = rp_pac::rtc::regs::Setup1(hms.0);
});
// Load the new datetime and re-enable RTC
self.inner.regs().ctrl().write(|w| w.set_load(true));
self.inner.regs().ctrl().write(|w| w.set_rtc_enable(true));
while !self.inner.regs().ctrl().read().rtc_active() {
core::hint::spin_loop();
}
}
/// Get the time in internal format
pub fn save(&mut self) -> (rp_pac::rtc::regs::Rtc1, rp_pac::rtc::regs::Rtc0) {
let rtc_0: rp_pac::rtc::regs::Rtc0 = self.inner.regs().rtc_0().read();
let rtc_1 = self.inner.regs().rtc_1().read();
(rtc_1, rtc_0)
}
/// Checks to see if this Rtc is running
pub fn is_running(&self) -> bool { pub fn is_running(&self) -> bool {
self.inner.regs().ctrl().read().rtc_active() self.inner.regs().ctrl().read().rtc_active()
} }
@ -113,8 +141,8 @@ impl<'d, T: Instance> RealTimeClock<'d, T> {
/// # fn main() { } /// # fn main() { }
/// # #[cfg(not(feature = "chrono"))] /// # #[cfg(not(feature = "chrono"))]
/// # fn main() { /// # fn main() {
/// # use embassy_rp::rtc::{RealTimeClock, DateTimeFilter}; /// # use embassy_rp::rtc::{Rtc, DateTimeFilter};
/// # let mut real_time_clock: RealTimeClock<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() }; /// # let mut real_time_clock: Rtc<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() };
/// let now = real_time_clock.now().unwrap(); /// let now = real_time_clock.now().unwrap();
/// real_time_clock.schedule_alarm( /// real_time_clock.schedule_alarm(
/// DateTimeFilter::default() /// DateTimeFilter::default()
@ -150,7 +178,7 @@ impl<'d, T: Instance> RealTimeClock<'d, T> {
} }
} }
/// Errors that can occur on methods on [RealTimeClock] /// Errors that can occur on methods on [Rtc]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum RtcError { pub enum RtcError {
/// An invalid DateTime was given or stored on the hardware. /// An invalid DateTime was given or stored on the hardware.

View File

@ -361,6 +361,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> {
let regs = T::regs(); let regs = T::regs();
let siestatus = regs.sie_status().read(); let siestatus = regs.sie_status().read();
let intrstatus = regs.intr().read();
if siestatus.resume() { if siestatus.resume() {
regs.sie_status().write(|w| w.set_resume(true)); regs.sie_status().write(|w| w.set_resume(true));
@ -389,7 +390,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> {
return Poll::Ready(Event::Reset); return Poll::Ready(Event::Reset);
} }
if siestatus.suspended() { if siestatus.suspended() && intrstatus.dev_suspend() {
regs.sie_status().write(|w| w.set_suspended(true)); regs.sie_status().write(|w| w.set_suspended(true));
return Poll::Ready(Event::Suspend); return Poll::Ready(Event::Suspend);
} }

View File

@ -107,4 +107,36 @@ impl Watchdog {
w.set_trigger(true); w.set_trigger(true);
}) })
} }
/// Store data in scratch register
pub fn set_scratch(&mut self, index: usize, value: u32) {
let watchdog = pac::WATCHDOG;
match index {
0 => watchdog.scratch0().write(|w| *w = value),
1 => watchdog.scratch1().write(|w| *w = value),
2 => watchdog.scratch2().write(|w| *w = value),
3 => watchdog.scratch3().write(|w| *w = value),
4 => watchdog.scratch4().write(|w| *w = value),
5 => watchdog.scratch5().write(|w| *w = value),
6 => watchdog.scratch6().write(|w| *w = value),
7 => watchdog.scratch7().write(|w| *w = value),
_ => panic!("Invalid watchdog scratch index"),
}
}
/// Read data from scratch register
pub fn get_scratch(&mut self, index: usize) -> u32 {
let watchdog = pac::WATCHDOG;
match index {
0 => watchdog.scratch0().read(),
1 => watchdog.scratch1().read(),
2 => watchdog.scratch2().read(),
3 => watchdog.scratch3().read(),
4 => watchdog.scratch4().read(),
5 => watchdog.scratch5().read(),
6 => watchdog.scratch6().read(),
7 => watchdog.scratch7().read(),
_ => panic!("Invalid watchdog scratch index"),
}
}
} }

View File

@ -25,12 +25,11 @@ aligned = "0.4.1"
bit_field = "0.10.2" bit_field = "0.10.2"
stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] }
stm32wb-hci = { version = "0.1.2", features = ["version-5-0"], optional = true } stm32wb-hci = { version = "0.1.3", optional = true }
bitflags = { version = "2.3.3", optional = true } bitflags = { version = "2.3.3", optional = true }
[features] [features]
default = [] defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt", "stm32wb-hci?/defmt"]
defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"]
ble = ["dep:stm32wb-hci"] ble = ["dep:stm32wb-hci"]
mac = ["dep:bitflags"] mac = ["dep:bitflags"]

View File

@ -1,10 +1,9 @@
use core::future::Future; use core::future::Future;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::pin::Pin; use core::pin::Pin;
use core::sync::atomic::{fence, Ordering}; use core::sync::atomic::{fence, AtomicUsize, Ordering};
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
use atomic_polyfill::AtomicUsize;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;

View File

@ -1,6 +1,6 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::into_ref; use embassy_hal_common::into_ref;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

View File

@ -1,6 +1,6 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
use stm32_metapac::FLASH_BASE; use stm32_metapac::FLASH_BASE;

View File

@ -1,7 +1,6 @@
use core::convert::TryInto; use core::convert::TryInto;
use core::ptr::write_volatile; use core::ptr::write_volatile;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;

View File

@ -1,7 +1,6 @@
use core::convert::TryInto; use core::convert::TryInto;
use core::ptr::write_volatile; use core::ptr::write_volatile;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;

View File

@ -1,8 +1,7 @@
use core::convert::TryInto; use core::convert::TryInto;
use core::ptr::write_volatile; use core::ptr::write_volatile;
use core::sync::atomic::{fence, Ordering}; use core::sync::atomic::{fence, AtomicBool, Ordering};
use atomic_polyfill::AtomicBool;
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use pac::flash::regs::Sr; use pac::flash::regs::Sr;
use pac::FLASH_SIZE; use pac::FLASH_SIZE;

View File

@ -1,7 +1,6 @@
use core::convert::TryInto; use core::convert::TryInto;
use core::ptr::write_volatile; use core::ptr::write_volatile;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;

View File

@ -1,6 +1,5 @@
use core::ptr::write_volatile; use core::ptr::write_volatile;
use core::sync::atomic::{fence, Ordering};
use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;

View File

@ -382,13 +382,18 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
// I2C start // I2C start
// //
// ST SAD+W // ST SAD+W
Self::master_write( if let Err(err) = Self::master_write(
address, address,
write.len().min(255), write.len().min(255),
Stop::Software, Stop::Software,
last_chunk_idx != 0, last_chunk_idx != 0,
&check_timeout, &check_timeout,
)?; ) {
if send_stop {
self.master_stop();
}
return Err(err);
}
for (number, chunk) in write.chunks(255).enumerate() { for (number, chunk) in write.chunks(255).enumerate() {
if number != 0 { if number != 0 {
@ -399,18 +404,22 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
// Wait until we are allowed to send data // Wait until we are allowed to send data
// (START has been ACKed or last byte when // (START has been ACKed or last byte when
// through) // through)
self.wait_txe(&check_timeout)?; if let Err(err) = self.wait_txe(&check_timeout) {
if send_stop {
self.master_stop();
}
return Err(err);
}
T::regs().txdr().write(|w| w.set_txdata(*byte)); T::regs().txdr().write(|w| w.set_txdata(*byte));
} }
} }
// Wait until the write finishes // Wait until the write finishes
self.wait_tc(&check_timeout)?; let result = self.wait_tc(&check_timeout);
if send_stop { if send_stop {
self.master_stop(); self.master_stop();
} }
Ok(()) result
} }
async fn write_dma_internal( async fn write_dma_internal(
@ -707,13 +716,16 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
let first_length = write[0].len(); let first_length = write[0].len();
let last_slice_index = write.len() - 1; let last_slice_index = write.len() - 1;
Self::master_write( if let Err(err) = Self::master_write(
address, address,
first_length.min(255), first_length.min(255),
Stop::Software, Stop::Software,
(first_length > 255) || (last_slice_index != 0), (first_length > 255) || (last_slice_index != 0),
&check_timeout, &check_timeout,
)?; ) {
self.master_stop();
return Err(err);
}
for (idx, slice) in write.iter().enumerate() { for (idx, slice) in write.iter().enumerate() {
let slice_len = slice.len(); let slice_len = slice.len();
@ -726,27 +738,36 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
let last_chunk_idx = total_chunks.saturating_sub(1); let last_chunk_idx = total_chunks.saturating_sub(1);
if idx != 0 { if idx != 0 {
Self::master_continue( if let Err(err) = Self::master_continue(
slice_len.min(255), slice_len.min(255),
(idx != last_slice_index) || (slice_len > 255), (idx != last_slice_index) || (slice_len > 255),
&check_timeout, &check_timeout,
)?; ) {
self.master_stop();
return Err(err);
}
} }
for (number, chunk) in slice.chunks(255).enumerate() { for (number, chunk) in slice.chunks(255).enumerate() {
if number != 0 { if number != 0 {
Self::master_continue( if let Err(err) = Self::master_continue(
chunk.len(), chunk.len(),
(number != last_chunk_idx) || (idx != last_slice_index), (number != last_chunk_idx) || (idx != last_slice_index),
&check_timeout, &check_timeout,
)?; ) {
self.master_stop();
return Err(err);
}
} }
for byte in chunk { for byte in chunk {
// Wait until we are allowed to send data // Wait until we are allowed to send data
// (START has been ACKed or last byte when // (START has been ACKed or last byte when
// through) // through)
self.wait_txe(&check_timeout)?; if let Err(err) = self.wait_txe(&check_timeout) {
self.master_stop();
return Err(err);
}
// Put byte on the wire // Put byte on the wire
//self.i2c.txdr.write(|w| w.txdata().bits(*byte)); //self.i2c.txdr.write(|w| w.txdata().bits(*byte));
@ -755,10 +776,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
} }
} }
// Wait until the write finishes // Wait until the write finishes
self.wait_tc(&check_timeout)?; let result = self.wait_tc(&check_timeout);
self.master_stop(); self.master_stop();
result
Ok(())
} }
pub fn blocking_write_vectored(&mut self, address: u8, write: &[&[u8]]) -> Result<(), Error> { pub fn blocking_write_vectored(&mut self, address: u8, write: &[&[u8]]) -> Result<(), Error> {

View File

@ -1,8 +1,7 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll; use core::task::Poll;
use atomic_polyfill::{compiler_fence, Ordering};
use self::sealed::Instance; use self::sealed::Instance;
use crate::interrupt; use crate::interrupt;
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;

View File

@ -1,4 +1,5 @@
use crate::pac::{FLASH, RCC}; use crate::pac::pwr::vals::Dbp;
use crate::pac::{FLASH, PWR, RCC};
use crate::rcc::{set_freqs, Clocks}; use crate::rcc::{set_freqs, Clocks};
use crate::time::Hertz; use crate::time::Hertz;
@ -184,6 +185,8 @@ pub struct Config {
pub apb1_pre: APBPrescaler, pub apb1_pre: APBPrescaler,
pub apb2_pre: APBPrescaler, pub apb2_pre: APBPrescaler,
pub enable_lsi: bool, pub enable_lsi: bool,
pub enable_rtc_apb: bool,
pub rtc_mux: RtcClockSource,
} }
impl Default for Config { impl Default for Config {
@ -196,10 +199,25 @@ impl Default for Config {
apb1_pre: APBPrescaler::NotDivided, apb1_pre: APBPrescaler::NotDivided,
apb2_pre: APBPrescaler::NotDivided, apb2_pre: APBPrescaler::NotDivided,
enable_lsi: false, enable_lsi: false,
enable_rtc_apb: false,
rtc_mux: RtcClockSource::LSI32,
} }
} }
} }
pub enum RtcClockSource {
LSE32,
LSI32,
}
#[repr(u8)]
pub enum Lsedrv {
Low = 0,
MediumLow = 1,
MediumHigh = 2,
High = 3,
}
pub(crate) unsafe fn init(config: Config) { pub(crate) unsafe fn init(config: Config) {
let (sys_clk, sw, vos) = match config.mux { let (sys_clk, sw, vos) = match config.mux {
ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2), ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2),
@ -266,6 +284,32 @@ pub(crate) unsafe fn init(config: Config) {
while FLASH.acr().read().latency() != ws {} while FLASH.acr().read().latency() != ws {}
match config.rtc_mux {
RtcClockSource::LSE32 => {
// 1. Unlock the backup domain
PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED));
// 2. Setup the LSE
RCC.bdcr().modify(|w| {
// Enable LSE
w.set_lseon(true);
// Max drive strength
// TODO: should probably be settable
w.set_lsedrv(Lsedrv::High as u8); //---// PAM - should not be commented
});
// Wait until LSE is running
while !RCC.bdcr().read().lserdy() {}
}
RtcClockSource::LSI32 => {
// Turn on the internal 32 kHz LSI oscillator
RCC.csr().modify(|w| w.set_lsion(true));
// Wait until LSI is running
while !RCC.csr().read().lsirdy() {}
}
}
match config.mux { match config.mux {
ClockSrc::HSI16 => { ClockSrc::HSI16 => {
// Enable HSI16 // Enable HSI16
@ -287,11 +331,26 @@ pub(crate) unsafe fn init(config: Config) {
w.set_msirgsel(true); w.set_msirgsel(true);
w.set_msirange(range.into()); w.set_msirange(range.into());
w.set_msion(true); w.set_msion(true);
if let RtcClockSource::LSE32 = config.rtc_mux {
// If LSE is enabled, enable calibration of MSI
w.set_msipllen(true);
} else {
w.set_msipllen(false);
}
}); });
while !RCC.cr().read().msirdy() {} while !RCC.cr().read().msirdy() {}
} }
} }
if config.enable_rtc_apb {
// enable peripheral clock for communication
crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true));
// read to allow the pwr clock to enable
crate::pac::PWR.cr1().read();
}
RCC.extcfgr().modify(|w| { RCC.extcfgr().modify(|w| {
if config.shd_ahb_pre == AHBPrescaler::NotDivided { if config.shd_ahb_pre == AHBPrescaler::NotDivided {
w.set_shdhpre(0); w.set_shdhpre(0);

View File

@ -172,6 +172,7 @@ impl sealed::Instance for crate::peripherals::RTC {
const BACKUP_REGISTER_COUNT: usize = 32; const BACKUP_REGISTER_COUNT: usize = 32;
fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> {
#[allow(clippy::if_same_then_else)]
if register < Self::BACKUP_REGISTER_COUNT { if register < Self::BACKUP_REGISTER_COUNT {
//Some(rtc.bkpr()[register].read().bits()) //Some(rtc.bkpr()[register].read().bits())
None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC

View File

@ -1,8 +1,8 @@
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::sync::atomic::{AtomicBool, AtomicU16, Ordering};
use core::task::Poll; use core::task::Poll;
use atomic_polyfill::{AtomicBool, AtomicU16, Ordering};
use embassy_hal_common::{into_ref, Peripheral}; use embassy_hal_common::{into_ref, Peripheral};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use embassy_usb_driver::{ use embassy_usb_driver::{

View File

@ -1,10 +1,10 @@
use core::sync::atomic::{AtomicU8, Ordering};
use std::cell::{RefCell, UnsafeCell}; use std::cell::{RefCell, UnsafeCell};
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::sync::{Condvar, Mutex, Once}; use std::sync::{Condvar, Mutex, Once};
use std::time::{Duration as StdDuration, Instant as StdInstant}; use std::time::{Duration as StdDuration, Instant as StdInstant};
use std::{mem, ptr, thread}; use std::{mem, ptr, thread};
use atomic_polyfill::{AtomicU8, Ordering};
use critical_section::Mutex as CsMutex; use critical_section::Mutex as CsMutex;
use crate::driver::{AlarmHandle, Driver}; use crate::driver::{AlarmHandle, Driver};

View File

@ -1,9 +1,9 @@
use core::sync::atomic::{AtomicU8, Ordering};
use std::cell::UnsafeCell; use std::cell::UnsafeCell;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::ptr; use std::ptr;
use std::sync::{Mutex, Once}; use std::sync::{Mutex, Once};
use atomic_polyfill::{AtomicU8, Ordering};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_timer::Instant as StdInstant; use wasm_timer::Instant as StdInstant;

View File

@ -1,3 +1,6 @@
//! This example test the ADC (Analog to Digital Conversion) of the RS2040 pin 26, 27 and 28.
//! It also reads the temperature sensor in the chip.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
@ -38,5 +41,8 @@ async fn main(_spawner: Spawner) {
fn convert_to_celsius(raw_temp: u16) -> f32 { fn convert_to_celsius(raw_temp: u16) -> f32 {
// According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet // According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet
27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721 as f32 let temp = 27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721;
let sign = if temp < 0.0 { -1.0 } else { 1.0 };
let rounded_temp_x10: i16 = ((temp * 10.0) + 0.5 * sign) as i16;
(rounded_temp_x10 as f32) / 10.0
} }

View File

@ -1,3 +1,7 @@
//! This example test the RP Pico on board LED.
//!
//! It does not work with the RP Pico W board. See wifi_blinky.rs.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example uses the RP Pico on board LED to test input pin 28. This is not the button on the board.
//!
//! It does not work with the RP Pico W board. Use wifi_blinky.rs and add input pin.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,5 @@
//! This example test the flash connected to the RP2040 chip.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example shows how async gpio can be used with a RP2040.
//!
//! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
@ -9,8 +13,6 @@ use embassy_time::{Duration, Timer};
use gpio::{Input, Level, Output, Pull}; use gpio::{Input, Level, Output, Pull};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
/// This example shows how async gpio can be used with a RP2040.
///
/// It requires an external signal to be manually triggered on PIN 16. For /// It requires an external signal to be manually triggered on PIN 16. For
/// example, this could be accomplished using an external power source with a /// example, this could be accomplished using an external power source with a
/// button so that it is possible to toggle the signal from low to high. /// button so that it is possible to toggle the signal from low to high.

View File

@ -1,3 +1,7 @@
//! This example shows how GPOUT (General purpose clock outputs) can toggle a output pin.
//!
//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,8 @@
//! This example shows how to communicate asynchronous using i2c with external chips.
//!
//! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip.
//! (https://www.microchip.com/en-us/product/mcp23017)
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,8 @@
//! This example shows how to communicate using i2c with external chips.
//!
//! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip.
//! (https://www.microchip.com/en-us/product/mcp23017)
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,5 +1,6 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LoRaWAN join functionality. //! It demonstrates LoRaWAN join functionality.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]

View File

@ -1,5 +1,6 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]

View File

@ -1,5 +1,6 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P send functionality. //! It demonstrates LORA P2P send functionality.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]

View File

@ -1,5 +1,6 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core. //! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]

View File

@ -1,3 +1,7 @@
//! This example shows how to send messages between the two cores in the RP2040 chip.
//!
//! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,5 @@
//! This example shows powerful PIO module in the RP2040 chip.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
@ -54,7 +56,14 @@ fn setup_pio_task_sm1<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a,
// Setupm sm1 // Setupm sm1
// Read 0b10101 repeatedly until ISR is full // Read 0b10101 repeatedly until ISR is full
let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",); let prg = pio_proc::pio_asm!(
//
".origin 8",
"set x, 0x15",
".wrap_target",
"in x, 5 [31]",
".wrap",
);
let relocated = RelocatedProgram::new(&prg.program); let relocated = RelocatedProgram::new(&prg.program);
let mut cfg = Config::default(); let mut cfg = Config::default();

View File

@ -1,3 +1,5 @@
//! This example shows powerful PIO module in the RP2040 chip.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example shows powerful PIO module in the RP2040 chip to communicate with a HD44780 display.
//! See (https://www.sparkfun.com/datasheets/LCD/HD44780.pdf)
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example shows powerful PIO module in the RP2040 chip to communicate with WS2812 LED modules.
//! See (https://www.sparkfun.com/categories/tags/ws2812)
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip.
//!
//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -0,0 +1,46 @@
//! This example shows how to use RTC (Real Time Clock) in the RP2040 chip.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::rtc::{DateTime, DayOfWeek, Rtc};
use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
info!("Wait for 20s");
let mut rtc = Rtc::new(p.RTC);
if !rtc.is_running() {
info!("Start RTC");
let now = DateTime {
year: 2000,
month: 1,
day: 1,
day_of_week: DayOfWeek::Saturday,
hour: 0,
minute: 0,
second: 0,
};
rtc.set_datetime(now).unwrap();
}
Timer::after(Duration::from_millis(20000)).await;
if let Ok(dt) = rtc.now() {
info!(
"Now: {}-{:02}-{:02} {}:{:02}:{:02}",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
);
}
info!("Reboot.");
Timer::after(Duration::from_millis(200)).await;
cortex_m::peripheral::SCB::sys_reset();
}

View File

@ -1,3 +1,7 @@
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
//!
//! Example for resistive touch sensor in Waveshare Pico-ResTouch
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
//! No specific hardware is specified in this example. If you connect pin 11 and 12 you should get the same data back.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,8 @@
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
//!
//! Example written for a display using the ST7789 chip. Possibly the Waveshare Pico-ResTouch
//! (https://www.waveshare.com/wiki/Pico-ResTouch-LCD-2.8)
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,9 @@
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
//!
//! No specific hardware is specified in this example. Only output on pin 0 is tested.
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
//! with its UART port.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,9 @@
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
//!
//! No specific hardware is specified in this example. If you connect pin 0 and 1 you should get the same data back.
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
//! with its UART port.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,5 +1,9 @@
//! test TX-only and RX-only UARTs. You need to connect GPIO0 to GPIO5 for //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
//!
//! Test TX-only and RX-only on two different UARTs. You need to connect GPIO0 to GPIO5 for
//! this to work //! this to work
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
//! with its UART port.
#![no_std] #![no_std]
#![no_main] #![no_main]

View File

@ -1,3 +1,7 @@
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
//!
//! This is a CDC-NCM class implementation, aka Ethernet over USB.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
//!
//! This creates the possibility to send log::info/warn/error/debug! to USB serial port.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
//!
//! This creates a USB serial port that echos.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example shows how to use Watchdog in the RP2040 chip.
//!
//! It does not work with the RP Pico W board. See wifi_blinky.rs or connect external LED and resistor.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example uses the RP Pico W board Wifi chip (cyw43).
//! Creates an Access point Wifi network and creates a TCP endpoint on port 1234.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,7 @@
//! This example test the RP Pico W on board LED.
//!
//! It does not work with the RP Pico board. See blinky.rs.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example uses the RP Pico W board Wifi chip (cyw43).
//! Scans Wifi for ssid names.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -1,3 +1,6 @@
//! This example uses the RP Pico W board Wifi chip (cyw43).
//! Connects to specified Wifi network and creates a TCP endpoint on port 1234.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]

View File

@ -43,5 +43,6 @@ required-features = ["mac"]
name = "eddystone_beacon" name = "eddystone_beacon"
required-features = ["ble"] required-features = ["ble"]
[patch.crates-io] [[bin]]
stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"} name = "gatt_server"
required-features = ["ble"]

View File

@ -0,0 +1,397 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use core::time::Duration;
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::bind_interrupts;
use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
use embassy_stm32_wpan::hci::event::command::{CommandComplete, ReturnParameters};
use embassy_stm32_wpan::hci::host::uart::{Packet, UartHci};
use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
use embassy_stm32_wpan::hci::types::AdvertisingType;
use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{
AddressType, AuthenticationRequirements, DiscoverableParameters, GapCommands, IoCapability, LocalName, Pin, Role,
SecureConnectionSupport,
};
use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::{
AddCharacteristicParameters, AddServiceParameters, CharacteristicEvent, CharacteristicPermission,
CharacteristicProperty, EncryptionKeySize, GattCommands, ServiceType, UpdateCharacteristicValueParameters, Uuid,
WriteResponseParameters,
};
use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
use embassy_stm32_wpan::hci::vendor::stm32wb::event::{self, AttributeHandle, Stm32Wb5xEvent};
use embassy_stm32_wpan::hci::{BdAddr, Event};
use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
use embassy_stm32_wpan::sub::ble::Ble;
use embassy_stm32_wpan::TlMbox;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs{
IPCC_C1_RX => ReceiveInterruptHandler;
IPCC_C1_TX => TransmitInterruptHandler;
});
const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
/*
How to make this work:
- Obtain a NUCLEO-STM32WB55 from your preferred supplier.
- Download and Install STM32CubeProgrammer.
- Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from
gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x
- Open STM32CubeProgrammer
- On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware.
- Once complete, click connect to connect to the device.
- On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services".
- In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the
stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address.
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Select "Start Wireless Stack".
- Disconnect from the device.
- In the examples folder for stm32wb, modify the memory.x file to match your target device.
- Run this example.
Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name.
*/
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let config = Config::default();
let mut mbox = TlMbox::init(p.IPCC, Irqs, config);
let sys_event = mbox.sys_subsystem.read().await;
info!("sys event: {}", sys_event.payload());
mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await;
info!("resetting BLE...");
mbox.ble_subsystem.reset().await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("config public address...");
mbox.ble_subsystem
.write_config_data(&ConfigData::public_address(get_bd_addr()).build())
.await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("config random address...");
mbox.ble_subsystem
.write_config_data(&ConfigData::random_address(get_random_addr()).build())
.await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("config identity root...");
mbox.ble_subsystem
.write_config_data(&ConfigData::identity_root(&get_irk()).build())
.await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("config encryption root...");
mbox.ble_subsystem
.write_config_data(&ConfigData::encryption_root(&get_erk()).build())
.await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("config tx power level...");
mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("GATT init...");
mbox.ble_subsystem.init_gatt().await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("GAP init...");
mbox.ble_subsystem
.init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH)
.await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("set IO capabilities...");
mbox.ble_subsystem.set_io_capability(IoCapability::DisplayConfirm).await;
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("set authentication requirements...");
mbox.ble_subsystem
.set_authentication_requirement(&AuthenticationRequirements {
bonding_required: false,
keypress_notification_support: false,
mitm_protection_required: false,
encryption_key_size_range: (8, 16),
fixed_pin: Pin::Requested,
identity_address_type: AddressType::Public,
secure_connection_support: SecureConnectionSupport::Optional,
})
.await
.unwrap();
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("set scan response data...");
mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap();
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
info!("set scan response data...");
mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap();
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
defmt::info!("initializing services and characteristics...");
let mut ble_context = init_gatt_services(&mut mbox.ble_subsystem).await.unwrap();
defmt::info!("{}", ble_context);
let discovery_params = DiscoverableParameters {
advertising_type: AdvertisingType::ConnectableUndirected,
advertising_interval: Some((Duration::from_millis(100), Duration::from_millis(100))),
address_type: OwnAddressType::Public,
filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan,
local_name: Some(LocalName::Complete(b"TXTX")),
advertising_data: &[],
conn_interval: (None, None),
};
info!("set discoverable...");
mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap();
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
loop {
let response = mbox.ble_subsystem.read().await;
defmt::debug!("{}", response);
if let Ok(Packet::Event(event)) = response {
match event {
Event::LeConnectionComplete(_) => {
defmt::info!("connected");
}
Event::DisconnectionComplete(_) => {
defmt::info!("disconnected");
ble_context.is_subscribed = false;
mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap();
}
Event::Vendor(vendor_event) => match vendor_event {
Stm32Wb5xEvent::AttReadPermitRequest(read_req) => {
defmt::info!("read request received {}, allowing", read_req);
mbox.ble_subsystem.allow_read(read_req.conn_handle).await
}
Stm32Wb5xEvent::AttWritePermitRequest(write_req) => {
defmt::info!("write request received {}, allowing", write_req);
mbox.ble_subsystem
.write_response(&WriteResponseParameters {
conn_handle: write_req.conn_handle,
attribute_handle: write_req.attribute_handle,
status: Ok(()),
value: write_req.value(),
})
.await
.unwrap()
}
Stm32Wb5xEvent::GattAttributeModified(attribute) => {
defmt::info!("{}", ble_context);
if attribute.attr_handle.0 == ble_context.chars.notify.0 + 2 {
if attribute.data()[0] == 0x01 {
defmt::info!("subscribed");
ble_context.is_subscribed = true;
} else {
defmt::info!("unsubscribed");
ble_context.is_subscribed = false;
}
}
}
_ => {}
},
_ => {}
}
}
}
}
fn get_bd_addr() -> BdAddr {
let mut bytes = [0u8; 6];
let lhci_info = LhciC1DeviceInformationCcrp::new();
bytes[0] = (lhci_info.uid64 & 0xff) as u8;
bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
bytes[3] = lhci_info.device_type_id;
bytes[4] = (lhci_info.st_company_id & 0xff) as u8;
bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8;
BdAddr(bytes)
}
fn get_random_addr() -> BdAddr {
let mut bytes = [0u8; 6];
let lhci_info = LhciC1DeviceInformationCcrp::new();
bytes[0] = (lhci_info.uid64 & 0xff) as u8;
bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
bytes[3] = 0;
bytes[4] = 0x6E;
bytes[5] = 0xED;
BdAddr(bytes)
}
const BLE_CFG_IRK: [u8; 16] = [
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
];
const BLE_CFG_ERK: [u8; 16] = [
0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21,
];
fn get_irk() -> EncryptionKey {
EncryptionKey(BLE_CFG_IRK)
}
fn get_erk() -> EncryptionKey {
EncryptionKey(BLE_CFG_ERK)
}
#[derive(defmt::Format)]
pub struct BleContext {
pub service_handle: AttributeHandle,
pub chars: CharHandles,
pub is_subscribed: bool,
}
#[derive(defmt::Format)]
pub struct CharHandles {
pub read: AttributeHandle,
pub write: AttributeHandle,
pub notify: AttributeHandle,
}
pub async fn init_gatt_services(ble_subsystem: &mut Ble) -> Result<BleContext, ()> {
let service_handle = gatt_add_service(ble_subsystem, Uuid::Uuid16(0x500)).await?;
let read = gatt_add_char(
ble_subsystem,
service_handle,
Uuid::Uuid16(0x501),
CharacteristicProperty::READ,
Some(b"Hello from embassy!"),
)
.await?;
let write = gatt_add_char(
ble_subsystem,
service_handle,
Uuid::Uuid16(0x502),
CharacteristicProperty::WRITE_WITHOUT_RESPONSE | CharacteristicProperty::WRITE | CharacteristicProperty::READ,
None,
)
.await?;
let notify = gatt_add_char(
ble_subsystem,
service_handle,
Uuid::Uuid16(0x503),
CharacteristicProperty::NOTIFY | CharacteristicProperty::READ,
None,
)
.await?;
Ok(BleContext {
service_handle,
is_subscribed: false,
chars: CharHandles { read, write, notify },
})
}
async fn gatt_add_service(ble_subsystem: &mut Ble, uuid: Uuid) -> Result<AttributeHandle, ()> {
ble_subsystem
.add_service(&AddServiceParameters {
uuid,
service_type: ServiceType::Primary,
max_attribute_records: 8,
})
.await;
let response = ble_subsystem.read().await;
defmt::debug!("{}", response);
if let Ok(Packet::Event(Event::CommandComplete(CommandComplete {
return_params:
ReturnParameters::Vendor(event::command::ReturnParameters::GattAddService(event::command::GattService {
service_handle,
..
})),
..
}))) = response
{
Ok(service_handle)
} else {
Err(())
}
}
async fn gatt_add_char(
ble_subsystem: &mut Ble,
service_handle: AttributeHandle,
characteristic_uuid: Uuid,
characteristic_properties: CharacteristicProperty,
default_value: Option<&[u8]>,
) -> Result<AttributeHandle, ()> {
ble_subsystem
.add_characteristic(&AddCharacteristicParameters {
service_handle,
characteristic_uuid,
characteristic_properties,
characteristic_value_len: 32,
security_permissions: CharacteristicPermission::empty(),
gatt_event_mask: CharacteristicEvent::all(),
encryption_key_size: EncryptionKeySize::with_value(7).unwrap(),
is_variable: true,
})
.await;
let response = ble_subsystem.read().await;
defmt::debug!("{}", response);
if let Ok(Packet::Event(Event::CommandComplete(CommandComplete {
return_params:
ReturnParameters::Vendor(event::command::ReturnParameters::GattAddCharacteristic(
event::command::GattCharacteristic {
characteristic_handle, ..
},
)),
..
}))) = response
{
if let Some(value) = default_value {
ble_subsystem
.update_characteristic_value(&UpdateCharacteristicValueParameters {
service_handle,
characteristic_handle,
offset: 0,
value,
})
.await
.unwrap();
let response = ble_subsystem.read().await;
defmt::debug!("{}", response);
}
Ok(characteristic_handle)
} else {
Err(())
}
}

View File

@ -3,7 +3,7 @@
runner = "probe-rs run --chip STM32WLE5JCIx" runner = "probe-rs run --chip STM32WLE5JCIx"
[build] [build]
target = "thumbv7em-none-eabihf" target = "thumbv7em-none-eabi"
[env] [env]
DEFMT_LOG = "trace" DEFMT_LOG = "trace"

View File

@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } 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"] }
embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" }
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] }
lora-phy = { version = "1" } lora-phy = { version = "1" }
@ -25,6 +25,7 @@ embedded-storage = "0.3.0"
panic-probe = { version = "0.3", features = ["print-defmt"] } panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false } heapless = { version = "0.7.5", default-features = false }
chrono = { version = "^0.4", default-features = false }
[patch.crates-io] [patch.crates-io]
lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" } lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" }

View File

@ -0,0 +1,43 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use chrono::{NaiveDate, NaiveDateTime};
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::rcc::{self, ClockSrc};
use embassy_stm32::rtc::{Rtc, RtcConfig};
use embassy_stm32::Config;
use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = {
let mut config = Config::default();
config.rcc.mux = ClockSrc::HSE32;
config.rcc.rtc_mux = rcc::RtcClockSource::LSE32;
config.rcc.enable_rtc_apb = true;
embassy_stm32::init(config)
};
info!("Hello World!");
let now = NaiveDate::from_ymd_opt(2020, 5, 15)
.unwrap()
.and_hms_opt(10, 30, 15)
.unwrap();
let mut rtc = Rtc::new(
p.RTC,
RtcConfig::default().clock_config(embassy_stm32::rtc::RtcClockSource::LSE),
);
info!("Got RTC! {:?}", now.timestamp());
rtc.set_datetime(now.into()).expect("datetime not set");
// In reality the delay would be much longer
Timer::after(Duration::from_millis(20000)).await;
let then: NaiveDateTime = rtc.now().unwrap().into();
info!("Got RTC! {:?}", then.timestamp());
}

View File

@ -21,14 +21,46 @@ async fn main(_spawner: Spawner) {
let b = Input::new(&mut b, Pull::None); let b = Input::new(&mut b, Pull::None);
{ {
let _a = Output::new(&mut a, Level::Low); let a = Output::new(&mut a, Level::Low);
delay(); delay();
assert!(b.is_low()); assert!(b.is_low());
assert!(!b.is_high());
assert!(a.is_set_low());
assert!(!a.is_set_high());
} }
{ {
let _a = Output::new(&mut a, Level::High); let mut a = Output::new(&mut a, Level::High);
delay();
assert!(!b.is_low());
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test is_set_low / is_set_high
a.set_low();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.set_high();
delay(); delay();
assert!(b.is_high()); assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test toggle
a.toggle();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.toggle();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
} }
} }

View File

@ -46,9 +46,6 @@ rand_chacha = { version = "0.3", default-features = false }
chrono = { version = "^0.4", default-features = false, optional = true} chrono = { version = "^0.4", default-features = false, optional = true}
[patch.crates-io]
stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"}
# BEGIN TESTS # BEGIN TESTS
# Generated by gen_test.py. DO NOT EDIT. # Generated by gen_test.py. DO NOT EDIT.
[[bin]] [[bin]]

View File

@ -40,14 +40,46 @@ async fn main(_spawner: Spawner) {
let b = Input::new(&mut b, Pull::None); let b = Input::new(&mut b, Pull::None);
{ {
let _a = Output::new(&mut a, Level::Low, Speed::Low); let a = Output::new(&mut a, Level::Low, Speed::Low);
delay(); delay();
assert!(b.is_low()); assert!(b.is_low());
assert!(!b.is_high());
assert!(a.is_set_low());
assert!(!a.is_set_high());
} }
{ {
let _a = Output::new(&mut a, Level::High, Speed::Low); let mut a = Output::new(&mut a, Level::High, Speed::Low);
delay();
assert!(!b.is_low());
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test is_set_low / is_set_high
a.set_low();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.set_high();
delay(); delay();
assert!(b.is_high()); assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test toggle
a.toggle();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.toggle();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
} }
} }