mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Merge #850
850: Shared buses with SetConfig r=Dirbaio a=kalkyl Addresses issue #830 Co-authored-by: Henrik Alsér <henrik@mindbite.se>
This commit is contained in:
commit
5f43c1d37e
@ -5,12 +5,14 @@ edition = "2021"
|
||||
|
||||
[features]
|
||||
std = []
|
||||
# Enable nightly-only features
|
||||
nightly = ["embedded-hal-async", "embedded-storage-async"]
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../embassy" }
|
||||
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" }
|
||||
embedded-hal-async = { version = "0.1.0-alpha.1" }
|
||||
embedded-hal-async = { version = "0.1.0-alpha.1", optional = true }
|
||||
embedded-storage = "0.3.0"
|
||||
embedded-storage-async = "0.3.0"
|
||||
embedded-storage-async = { version = "0.3.0", optional = true }
|
||||
nb = "1.0.0"
|
@ -1,6 +1,12 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![feature(generic_associated_types)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))]
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
pub mod adapter;
|
||||
|
||||
pub mod shared_bus;
|
||||
|
||||
pub trait SetConfig {
|
||||
type Config;
|
||||
fn set_config(&mut self, config: &Self::Config);
|
||||
}
|
||||
|
@ -22,28 +22,14 @@
|
||||
//! let i2c_dev2 = I2cBusDevice::new(i2c_bus);
|
||||
//! let mpu = Mpu6050::new(i2c_dev2);
|
||||
//! ```
|
||||
use core::fmt::Debug;
|
||||
use core::future::Future;
|
||||
|
||||
use embassy::blocking_mutex::raw::RawMutex;
|
||||
use embassy::mutex::Mutex;
|
||||
use embedded_hal_async::i2c;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum I2cBusDeviceError<BUS> {
|
||||
I2c(BUS),
|
||||
}
|
||||
|
||||
impl<BUS> i2c::Error for I2cBusDeviceError<BUS>
|
||||
where
|
||||
BUS: i2c::Error + Debug,
|
||||
{
|
||||
fn kind(&self) -> i2c::ErrorKind {
|
||||
match self {
|
||||
Self::I2c(e) => e.kind(),
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::shared_bus::I2cBusDeviceError;
|
||||
use crate::SetConfig;
|
||||
|
||||
pub struct I2cBusDevice<'a, M: RawMutex, BUS> {
|
||||
bus: &'a Mutex<M, BUS>,
|
||||
@ -116,3 +102,81 @@ where
|
||||
async move { todo!() }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> {
|
||||
bus: &'a Mutex<M, BUS>,
|
||||
config: BUS::Config,
|
||||
}
|
||||
|
||||
impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> {
|
||||
pub fn new(bus: &'a Mutex<M, BUS>, config: BUS::Config) -> Self {
|
||||
Self { bus, config }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M, BUS> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS>
|
||||
where
|
||||
BUS: i2c::ErrorType,
|
||||
M: RawMutex,
|
||||
BUS: SetConfig,
|
||||
{
|
||||
type Error = I2cBusDeviceError<BUS::Error>;
|
||||
}
|
||||
|
||||
impl<M, BUS> i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS>
|
||||
where
|
||||
M: RawMutex + 'static,
|
||||
BUS: i2c::I2c + SetConfig + 'static,
|
||||
{
|
||||
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
||||
|
||||
fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
||||
async move {
|
||||
let mut bus = self.bus.lock().await;
|
||||
bus.set_config(&self.config);
|
||||
bus.read(address, buffer).await.map_err(I2cBusDeviceError::I2c)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
||||
|
||||
fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
|
||||
async move {
|
||||
let mut bus = self.bus.lock().await;
|
||||
bus.set_config(&self.config);
|
||||
bus.write(address, bytes).await.map_err(I2cBusDeviceError::I2c)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
||||
|
||||
fn write_read<'a>(
|
||||
&'a mut self,
|
||||
address: u8,
|
||||
wr_buffer: &'a [u8],
|
||||
rd_buffer: &'a mut [u8],
|
||||
) -> Self::WriteReadFuture<'a> {
|
||||
async move {
|
||||
let mut bus = self.bus.lock().await;
|
||||
bus.set_config(&self.config);
|
||||
bus.write_read(address, wr_buffer, rd_buffer)
|
||||
.await
|
||||
.map_err(I2cBusDeviceError::I2c)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
|
||||
|
||||
fn transaction<'a, 'b>(
|
||||
&'a mut self,
|
||||
address: u8,
|
||||
operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
|
||||
) -> Self::TransactionFuture<'a, 'b> {
|
||||
let _ = address;
|
||||
let _ = operations;
|
||||
async move { todo!() }
|
||||
}
|
||||
}
|
3
embassy-embedded-hal/src/shared_bus/asynch/mod.rs
Normal file
3
embassy-embedded-hal/src/shared_bus/asynch/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
//! Asynchronous shared bus implementations for embedded-hal-async
|
||||
pub mod i2c;
|
||||
pub mod spi;
|
@ -25,7 +25,6 @@
|
||||
//! let spi_dev2 = SpiBusDevice::new(spi_bus, cs_pin2);
|
||||
//! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128);
|
||||
//! ```
|
||||
use core::fmt::Debug;
|
||||
use core::future::Future;
|
||||
|
||||
use embassy::blocking_mutex::raw::RawMutex;
|
||||
@ -34,24 +33,8 @@ use embedded_hal_1::digital::blocking::OutputPin;
|
||||
use embedded_hal_1::spi::ErrorType;
|
||||
use embedded_hal_async::spi;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum SpiBusDeviceError<BUS, CS> {
|
||||
Spi(BUS),
|
||||
Cs(CS),
|
||||
}
|
||||
|
||||
impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS>
|
||||
where
|
||||
BUS: spi::Error + Debug,
|
||||
CS: Debug,
|
||||
{
|
||||
fn kind(&self) -> spi::ErrorKind {
|
||||
match self {
|
||||
Self::Spi(e) => e.kind(),
|
||||
Self::Cs(_) => spi::ErrorKind::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::shared_bus::SpiBusDeviceError;
|
||||
use crate::SetConfig;
|
||||
|
||||
pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> {
|
||||
bus: &'a Mutex<M, BUS>,
|
||||
@ -109,3 +92,63 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> {
|
||||
bus: &'a Mutex<M, BUS>,
|
||||
cs: CS,
|
||||
config: BUS::Config,
|
||||
}
|
||||
|
||||
impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiBusDeviceWithConfig<'a, M, BUS, CS> {
|
||||
pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: BUS::Config) -> Self {
|
||||
Self { bus, cs, config }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M, BUS, CS> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS>
|
||||
where
|
||||
BUS: spi::ErrorType + SetConfig,
|
||||
CS: OutputPin,
|
||||
M: RawMutex,
|
||||
{
|
||||
type Error = SpiBusDeviceError<BUS::Error, CS::Error>;
|
||||
}
|
||||
|
||||
impl<M, BUS, CS> spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS>
|
||||
where
|
||||
M: RawMutex + 'static,
|
||||
BUS: spi::SpiBusFlush + SetConfig + 'static,
|
||||
CS: OutputPin,
|
||||
{
|
||||
type Bus = BUS;
|
||||
|
||||
type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a
|
||||
where
|
||||
Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a,
|
||||
Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a;
|
||||
|
||||
fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
|
||||
where
|
||||
R: 'a,
|
||||
F: FnOnce(*mut Self::Bus) -> Fut + 'a,
|
||||
Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a,
|
||||
{
|
||||
async move {
|
||||
let mut bus = self.bus.lock().await;
|
||||
bus.set_config(&self.config);
|
||||
self.cs.set_low().map_err(SpiBusDeviceError::Cs)?;
|
||||
|
||||
let f_res = f(&mut *bus).await;
|
||||
|
||||
// On failure, it's important to still flush and deassert CS.
|
||||
let flush_res = bus.flush().await;
|
||||
let cs_res = self.cs.set_high();
|
||||
|
||||
let f_res = f_res.map_err(SpiBusDeviceError::Spi)?;
|
||||
flush_res.map_err(SpiBusDeviceError::Spi)?;
|
||||
cs_res.map_err(SpiBusDeviceError::Cs)?;
|
||||
|
||||
Ok(f_res)
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,8 @@ use embassy::blocking_mutex::Mutex;
|
||||
use embedded_hal_1::i2c::blocking::{I2c, Operation};
|
||||
use embedded_hal_1::i2c::ErrorType;
|
||||
|
||||
use crate::shared_bus::i2c::I2cBusDeviceError;
|
||||
use crate::shared_bus::I2cBusDeviceError;
|
||||
use crate::SetConfig;
|
||||
|
||||
pub struct I2cBusDevice<'a, M: RawMutex, BUS> {
|
||||
bus: &'a Mutex<M, RefCell<BUS>>,
|
||||
@ -141,3 +142,87 @@ where
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> {
|
||||
bus: &'a Mutex<M, RefCell<BUS>>,
|
||||
config: BUS::Config,
|
||||
}
|
||||
|
||||
impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> {
|
||||
pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self {
|
||||
Self { bus, config }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M, BUS> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS>
|
||||
where
|
||||
M: RawMutex,
|
||||
BUS: ErrorType + SetConfig,
|
||||
{
|
||||
type Error = I2cBusDeviceError<BUS::Error>;
|
||||
}
|
||||
|
||||
impl<M, BUS> I2c for I2cBusDeviceWithConfig<'_, M, BUS>
|
||||
where
|
||||
M: RawMutex,
|
||||
BUS: I2c + SetConfig,
|
||||
{
|
||||
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.bus.lock(|bus| {
|
||||
let mut bus = bus.borrow_mut();
|
||||
bus.set_config(&self.config);
|
||||
bus.read(address, buffer).map_err(I2cBusDeviceError::I2c)
|
||||
})
|
||||
}
|
||||
|
||||
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
self.bus.lock(|bus| {
|
||||
let mut bus = bus.borrow_mut();
|
||||
bus.set_config(&self.config);
|
||||
bus.write(address, bytes).map_err(I2cBusDeviceError::I2c)
|
||||
})
|
||||
}
|
||||
|
||||
fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.bus.lock(|bus| {
|
||||
let mut bus = bus.borrow_mut();
|
||||
bus.set_config(&self.config);
|
||||
bus.write_read(address, wr_buffer, rd_buffer)
|
||||
.map_err(I2cBusDeviceError::I2c)
|
||||
})
|
||||
}
|
||||
|
||||
fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
|
||||
let _ = address;
|
||||
let _ = operations;
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> {
|
||||
let _ = addr;
|
||||
let _ = bytes;
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn write_iter_read<B: IntoIterator<Item = u8>>(
|
||||
&mut self,
|
||||
addr: u8,
|
||||
bytes: B,
|
||||
buffer: &mut [u8],
|
||||
) -> Result<(), Self::Error> {
|
||||
let _ = addr;
|
||||
let _ = bytes;
|
||||
let _ = buffer;
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>(
|
||||
&mut self,
|
||||
address: u8,
|
||||
operations: O,
|
||||
) -> Result<(), Self::Error> {
|
||||
let _ = address;
|
||||
let _ = operations;
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,8 @@ use embedded_hal_1::digital::blocking::OutputPin;
|
||||
use embedded_hal_1::spi;
|
||||
use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice};
|
||||
|
||||
use crate::shared_bus::spi::SpiBusDeviceError;
|
||||
use crate::shared_bus::SpiBusDeviceError;
|
||||
use crate::SetConfig;
|
||||
|
||||
pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> {
|
||||
bus: &'a Mutex<M, RefCell<BUS>>,
|
||||
@ -115,3 +116,52 @@ where
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> {
|
||||
bus: &'a Mutex<M, RefCell<BUS>>,
|
||||
cs: CS,
|
||||
config: BUS::Config,
|
||||
}
|
||||
|
||||
impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiBusDeviceWithConfig<'a, M, BUS, CS> {
|
||||
pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS, config: BUS::Config) -> Self {
|
||||
Self { bus, cs, config }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M, BUS, CS> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS>
|
||||
where
|
||||
M: RawMutex,
|
||||
BUS: spi::ErrorType + SetConfig,
|
||||
CS: OutputPin,
|
||||
{
|
||||
type Error = SpiBusDeviceError<BUS::Error, CS::Error>;
|
||||
}
|
||||
|
||||
impl<BUS, M, CS> SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS>
|
||||
where
|
||||
M: RawMutex,
|
||||
BUS: SpiBusFlush + SetConfig,
|
||||
CS: OutputPin,
|
||||
{
|
||||
type Bus = BUS;
|
||||
|
||||
fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> {
|
||||
self.bus.lock(|bus| {
|
||||
let mut bus = bus.borrow_mut();
|
||||
bus.set_config(&self.config);
|
||||
self.cs.set_low().map_err(SpiBusDeviceError::Cs)?;
|
||||
|
||||
let f_res = f(&mut bus);
|
||||
|
||||
// On failure, it's important to still flush and deassert CS.
|
||||
let flush_res = bus.flush();
|
||||
let cs_res = self.cs.set_high();
|
||||
|
||||
let f_res = f_res.map_err(SpiBusDeviceError::Spi)?;
|
||||
flush_res.map_err(SpiBusDeviceError::Spi)?;
|
||||
cs_res.map_err(SpiBusDeviceError::Cs)?;
|
||||
Ok(f_res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,44 @@
|
||||
//! Shared bus implementations
|
||||
use core::fmt::Debug;
|
||||
|
||||
use embedded_hal_1::{i2c, spi};
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
pub mod asynch;
|
||||
|
||||
pub mod blocking;
|
||||
/// Shared i2c bus implementation for embedded-hal-async
|
||||
pub mod i2c;
|
||||
/// Shared SPI bus implementation for embedded-hal-async
|
||||
pub mod spi;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum I2cBusDeviceError<BUS> {
|
||||
I2c(BUS),
|
||||
}
|
||||
|
||||
impl<BUS> i2c::Error for I2cBusDeviceError<BUS>
|
||||
where
|
||||
BUS: i2c::Error + Debug,
|
||||
{
|
||||
fn kind(&self) -> i2c::ErrorKind {
|
||||
match self {
|
||||
Self::I2c(e) => e.kind(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum SpiBusDeviceError<BUS, CS> {
|
||||
Spi(BUS),
|
||||
Cs(CS),
|
||||
}
|
||||
|
||||
impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS>
|
||||
where
|
||||
BUS: spi::Error + Debug,
|
||||
CS: Debug,
|
||||
{
|
||||
fn kind(&self) -> spi::ErrorKind {
|
||||
match self {
|
||||
Self::Spi(e) => e.kind(),
|
||||
Self::Cs(_) => spi::ErrorKind::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ time = ["embassy/time"]
|
||||
defmt = ["dep:defmt", "embassy/defmt", "embassy-usb?/defmt", "embedded-io?/defmt"]
|
||||
|
||||
# Enable nightly-only features
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-usb", "embedded-storage-async", "dep:embedded-io"]
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-usb", "embedded-storage-async", "dep:embedded-io", "embassy-embedded-hal/nightly"]
|
||||
|
||||
# Reexport the PAC for the currently enabled chip at `embassy_nrf::pac`.
|
||||
# This is unstable because semver-minor (non-breaking) releases of embassy-nrf may major-bump (breaking) the PAC version.
|
||||
@ -68,6 +68,7 @@ embassy = { version = "0.1.0", path = "../embassy" }
|
||||
embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]}
|
||||
embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]}
|
||||
embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" }
|
||||
embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" }
|
||||
embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true }
|
||||
|
||||
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
|
||||
|
@ -4,6 +4,7 @@ use core::marker::PhantomData;
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
use core::task::Poll;
|
||||
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::unborrow;
|
||||
pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
||||
use futures::future::poll_fn;
|
||||
@ -521,3 +522,46 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> SetConfig for Spim<'d, T> {
|
||||
type Config = Config;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
let r = T::regs();
|
||||
// Configure mode.
|
||||
let mode = config.mode;
|
||||
r.config.write(|w| {
|
||||
match mode {
|
||||
MODE_0 => {
|
||||
w.order().msb_first();
|
||||
w.cpol().active_high();
|
||||
w.cpha().leading();
|
||||
}
|
||||
MODE_1 => {
|
||||
w.order().msb_first();
|
||||
w.cpol().active_high();
|
||||
w.cpha().trailing();
|
||||
}
|
||||
MODE_2 => {
|
||||
w.order().msb_first();
|
||||
w.cpol().active_low();
|
||||
w.cpha().leading();
|
||||
}
|
||||
MODE_3 => {
|
||||
w.order().msb_first();
|
||||
w.cpol().active_low();
|
||||
w.cpha().trailing();
|
||||
}
|
||||
}
|
||||
|
||||
w
|
||||
});
|
||||
|
||||
// Configure frequency.
|
||||
let frequency = config.frequency;
|
||||
r.frequency.write(|w| w.frequency().variant(frequency));
|
||||
|
||||
// Set over-read character
|
||||
let orc = config.orc;
|
||||
r.orc.write(|w| unsafe { w.orc().bits(orc) });
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ use core::task::Poll;
|
||||
#[cfg(feature = "time")]
|
||||
use embassy::time::{Duration, Instant};
|
||||
use embassy::waitqueue::AtomicWaker;
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::unborrow;
|
||||
use futures::future::poll_fn;
|
||||
|
||||
@ -24,6 +25,7 @@ use crate::interrupt::{Interrupt, InterruptExt};
|
||||
use crate::util::{slice_in_ram, slice_in_ram_or};
|
||||
use crate::{gpio, pac, Unborrow};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Frequency {
|
||||
#[doc = "26738688: 100 kbps"]
|
||||
K100 = 26738688,
|
||||
@ -877,3 +879,12 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> SetConfig for Twim<'d, T> {
|
||||
type Config = Config;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
let r = T::regs();
|
||||
r.frequency
|
||||
.write(|w| unsafe { w.frequency().bits(config.frequency as u32) });
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ flavors = [
|
||||
unstable-pac = []
|
||||
|
||||
# Enable nightly-only features
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async"]
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-embedded-hal/nightly"]
|
||||
|
||||
# Implement embedded-hal 1.0 alpha traits.
|
||||
# Implement embedded-hal-async traits if `nightly` is set as well.
|
||||
@ -30,6 +30,7 @@ unstable-traits = ["embedded-hal-1"]
|
||||
embassy = { version = "0.1.0", path = "../embassy", features = [ "time-tick-1mhz", "nightly"] }
|
||||
embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]}
|
||||
embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" }
|
||||
embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" }
|
||||
embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["rp"]}
|
||||
atomic-polyfill = "0.1.5"
|
||||
defmt = { version = "0.3", optional = true }
|
||||
|
@ -1,5 +1,6 @@
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::unborrow;
|
||||
pub use embedded_hal_02::spi::{Phase, Polarity};
|
||||
|
||||
@ -350,3 +351,20 @@ mod eh1 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> SetConfig for Spi<'d, T> {
|
||||
type Config = Config;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
let p = self.inner.regs();
|
||||
let (presc, postdiv) = calc_prescs(config.frequency);
|
||||
unsafe {
|
||||
p.cpsr().write(|w| w.set_cpsdvsr(presc));
|
||||
p.cr0().write(|w| {
|
||||
w.set_dss(0b0111); // 8bit
|
||||
w.set_spo(config.polarity == Polarity::IdleHigh);
|
||||
w.set_sph(config.phase == Phase::CaptureOnSecondTransition);
|
||||
w.set_scr(postdiv);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ embassy = { version = "0.1.0", path = "../embassy" }
|
||||
embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-4"]}
|
||||
embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] }
|
||||
embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" }
|
||||
embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" }
|
||||
embassy-net = { version = "0.1.0", path = "../embassy-net", optional = true }
|
||||
embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional = true }
|
||||
|
||||
@ -90,7 +91,7 @@ time-driver-tim12 = ["_time-driver"]
|
||||
time-driver-tim15 = ["_time-driver"]
|
||||
|
||||
# Enable nightly-only features
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb"]
|
||||
nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb", "embassy-embedded-hal/nightly"]
|
||||
|
||||
# Reexport stm32-metapac at `embassy_stm32::pac`.
|
||||
# This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version.
|
||||
|
@ -1,5 +1,6 @@
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::unborrow;
|
||||
|
||||
use crate::gpio::sealed::AFType;
|
||||
@ -449,3 +450,23 @@ impl Timings {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> SetConfig for I2c<'d, T> {
|
||||
type Config = Hertz;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
let timings = Timings::new(T::frequency(), *config);
|
||||
unsafe {
|
||||
T::regs().cr2().modify(|reg| {
|
||||
reg.set_freq(timings.freq);
|
||||
});
|
||||
T::regs().ccr().modify(|reg| {
|
||||
reg.set_f_s(timings.mode.f_s());
|
||||
reg.set_duty(timings.duty.duty());
|
||||
reg.set_ccr(timings.ccr);
|
||||
});
|
||||
T::regs().trise().modify(|reg| {
|
||||
reg.set_trise(timings.trise);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ use core::task::Poll;
|
||||
|
||||
use atomic_polyfill::{AtomicUsize, Ordering};
|
||||
use embassy::waitqueue::AtomicWaker;
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::drop::OnDrop;
|
||||
use embassy_hal_common::unborrow;
|
||||
use futures::future::poll_fn;
|
||||
@ -941,3 +942,19 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> SetConfig for I2c<'d, T> {
|
||||
type Config = Hertz;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
let timings = Timings::new(T::frequency(), *config);
|
||||
unsafe {
|
||||
T::regs().timingr().write(|reg| {
|
||||
reg.set_presc(timings.prescale);
|
||||
reg.set_scll(timings.scll);
|
||||
reg.set_sclh(timings.sclh);
|
||||
reg.set_sdadel(timings.sdadel);
|
||||
reg.set_scldel(timings.scldel);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr;
|
||||
|
||||
use embassy_embedded_hal::SetConfig;
|
||||
use embassy_hal_common::unborrow;
|
||||
pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
||||
use futures::future::join;
|
||||
@ -1022,3 +1023,10 @@ foreach_peripheral!(
|
||||
impl Instance for peripherals::$inst {}
|
||||
};
|
||||
);
|
||||
|
||||
impl<'d, T: Instance, Tx, Rx> SetConfig for Spi<'d, T, Tx, Rx> {
|
||||
type Config = Config;
|
||||
fn set_config(&mut self, config: &Self::Config) {
|
||||
self.reconfigure(*config);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user