aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-07-09 02:28:05 +0200
committerHenrik Alsér <[email protected]>2022-07-09 02:28:05 +0200
commit880b71a1e8246011109d64794ec98477722bde0e (patch)
tree327a30558b64dcbd4ede6ed45e5057209c7c0179
parent85e67d94ad1a2ce35afcb64b4f9a527b863e8a50 (diff)
impl SetConfig for stm32 i2c and SPI
-rw-r--r--embassy-stm32/Cargo.toml1
-rw-r--r--embassy-stm32/src/i2c/v1.rs21
-rw-r--r--embassy-stm32/src/i2c/v2.rs17
-rw-r--r--embassy-stm32/src/spi/mod.rs8
4 files changed, 47 insertions, 0 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index a21384be6..547aad020 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -35,6 +35,7 @@ embassy = { version = "0.1.0", path = "../embassy" }
35embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-4"]} 35embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-4"]}
36embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] } 36embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] }
37embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } 37embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" }
38embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" }
38embassy-net = { version = "0.1.0", path = "../embassy-net", optional = true } 39embassy-net = { version = "0.1.0", path = "../embassy-net", optional = true }
39embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional = true } 40embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional = true }
40 41
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs
index 87a4f9699..4bff90af0 100644
--- a/embassy-stm32/src/i2c/v1.rs
+++ b/embassy-stm32/src/i2c/v1.rs
@@ -1,5 +1,6 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2 2
3use embassy_embedded_hal::SetConfig;
3use embassy_hal_common::unborrow; 4use embassy_hal_common::unborrow;
4 5
5use crate::gpio::sealed::AFType; 6use crate::gpio::sealed::AFType;
@@ -381,3 +382,23 @@ impl Timings {
381 } 382 }
382 } 383 }
383} 384}
385
386impl<'d, T: Instance> SetConfig for I2c<'d, T> {
387 type Config = Hertz;
388 fn set_config(&mut self, config: &Self::Config) {
389 let timings = Timings::new(T::frequency(), *config);
390 unsafe {
391 T::regs().cr2().modify(|reg| {
392 reg.set_freq(timings.freq);
393 });
394 T::regs().ccr().modify(|reg| {
395 reg.set_f_s(timings.mode.f_s());
396 reg.set_duty(timings.duty.duty());
397 reg.set_ccr(timings.ccr);
398 });
399 T::regs().trise().modify(|reg| {
400 reg.set_trise(timings.trise);
401 });
402 }
403 }
404}
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index 1a085e782..8778da910 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -4,6 +4,7 @@ use core::task::Poll;
4 4
5use atomic_polyfill::{AtomicUsize, Ordering}; 5use atomic_polyfill::{AtomicUsize, Ordering};
6use embassy::waitqueue::AtomicWaker; 6use embassy::waitqueue::AtomicWaker;
7use embassy_embedded_hal::SetConfig;
7use embassy_hal_common::drop::OnDrop; 8use embassy_hal_common::drop::OnDrop;
8use embassy_hal_common::unborrow; 9use embassy_hal_common::unborrow;
9use futures::future::poll_fn; 10use futures::future::poll_fn;
@@ -899,3 +900,19 @@ cfg_if::cfg_if! {
899 } 900 }
900 } 901 }
901} 902}
903
904impl<'d, T: Instance> SetConfig for I2c<'d, T> {
905 type Config = Hertz;
906 fn set_config(&mut self, config: &Self::Config) {
907 let timings = Timings::new(T::frequency(), *config);
908 unsafe {
909 T::regs().timingr().write(|reg| {
910 reg.set_presc(timings.prescale);
911 reg.set_scll(timings.scll);
912 reg.set_sclh(timings.sclh);
913 reg.set_sdadel(timings.sdadel);
914 reg.set_scldel(timings.scldel);
915 });
916 }
917 }
918}
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs
index 7c142e507..a839bb41e 100644
--- a/embassy-stm32/src/spi/mod.rs
+++ b/embassy-stm32/src/spi/mod.rs
@@ -3,6 +3,7 @@
3use core::marker::PhantomData; 3use core::marker::PhantomData;
4use core::ptr; 4use core::ptr;
5 5
6use embassy_embedded_hal::SetConfig;
6use embassy_hal_common::unborrow; 7use embassy_hal_common::unborrow;
7pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; 8pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
8use futures::future::join; 9use futures::future::join;
@@ -1022,3 +1023,10 @@ foreach_peripheral!(
1022 impl Instance for peripherals::$inst {} 1023 impl Instance for peripherals::$inst {}
1023 }; 1024 };
1024); 1025);
1026
1027impl<'d, T: Instance, Tx, Rx> SetConfig for Spi<'d, T, Tx, Rx> {
1028 type Config = Config;
1029 fn set_config(&mut self, config: &Self::Config) {
1030 self.reconfigure(*config);
1031 }
1032}