aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-06-08 17:42:49 +0200
committerGitHub <[email protected]>2021-06-08 17:42:49 +0200
commit80eb0ad5264ab0ddc75ad721aefe4a2cff34afbb (patch)
treeb739b2a4809b113a54f5b273086a26d77dbc6462
parentaf0f8082f079bb80d8b478331ff505f5462a9120 (diff)
parentf7394e56ef6275ffc528e68ff6532dc14a976611 (diff)
Merge pull request #218 from lulf/stm32-clk-enable
RccPeripharal + generate SPI clock enable
-rw-r--r--embassy-stm32/src/rcc/mod.rs44
-rw-r--r--embassy-stm32/src/spi/mod.rs4
-rw-r--r--embassy-stm32/src/spi/v1.rs2
-rw-r--r--embassy-stm32/src/spi/v2.rs2
-rw-r--r--embassy-stm32/src/spi/v3.rs2
m---------stm32-data0
-rw-r--r--stm32-metapac/build.rs25
7 files changed, 77 insertions, 2 deletions
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs
index 7112ad02f..3c5b53b05 100644
--- a/embassy-stm32/src/rcc/mod.rs
+++ b/embassy-stm32/src/rcc/mod.rs
@@ -1,3 +1,6 @@
1#![macro_use]
2
3use crate::peripherals;
1use crate::time::Hertz; 4use crate::time::Hertz;
2use core::mem::MaybeUninit; 5use core::mem::MaybeUninit;
3 6
@@ -44,3 +47,44 @@ cfg_if::cfg_if! {
44 } 47 }
45 } 48 }
46} 49}
50
51pub(crate) mod sealed {
52 pub trait RccPeripheral {
53 fn reset();
54 fn enable();
55 fn disable();
56 }
57}
58
59pub trait RccPeripheral: sealed::RccPeripheral + 'static {}
60
61crate::pac::peripheral_rcc!(
62 ($inst:ident, $enable:ident, $reset:ident, $perien:ident, $perirst:ident) => {
63 impl sealed::RccPeripheral for peripherals::$inst {
64 fn enable() {
65 critical_section::with(|_| {
66 unsafe {
67 crate::pac::RCC.$enable().modify(|w| w.$perien(true));
68 }
69 })
70 }
71 fn disable() {
72 critical_section::with(|_| {
73 unsafe {
74 crate::pac::RCC.$enable().modify(|w| w.$perien(false));
75 }
76 })
77 }
78 fn reset() {
79 critical_section::with(|_| {
80 unsafe {
81 crate::pac::RCC.$reset().modify(|w| w.$perirst(true));
82 crate::pac::RCC.$reset().modify(|w| w.$perirst(false));
83 }
84 })
85 }
86 }
87
88 impl RccPeripheral for peripherals::$inst {}
89 };
90);
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs
index 730169ec0..9b04c03aa 100644
--- a/embassy-stm32/src/spi/mod.rs
+++ b/embassy-stm32/src/spi/mod.rs
@@ -4,7 +4,7 @@
4#[cfg_attr(spi_v2, path = "v2.rs")] 4#[cfg_attr(spi_v2, path = "v2.rs")]
5#[cfg_attr(spi_v3, path = "v3.rs")] 5#[cfg_attr(spi_v3, path = "v3.rs")]
6mod _version; 6mod _version;
7use crate::peripherals; 7use crate::{peripherals, rcc::RccPeripheral};
8pub use _version::*; 8pub use _version::*;
9 9
10use crate::gpio::Pin; 10use crate::gpio::Pin;
@@ -64,7 +64,7 @@ pub(crate) mod sealed {
64 } 64 }
65} 65}
66 66
67pub trait Instance: sealed::Instance + 'static {} 67pub trait Instance: sealed::Instance + RccPeripheral + 'static {}
68 68
69pub trait SckPin<T: Instance>: sealed::SckPin<T> + 'static {} 69pub trait SckPin<T: Instance>: sealed::SckPin<T> + 'static {}
70 70
diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs
index e3057a3b5..227a36a89 100644
--- a/embassy-stm32/src/spi/v1.rs
+++ b/embassy-stm32/src/spi/v1.rs
@@ -61,6 +61,8 @@ impl<'d, T: Instance> Spi<'d, T> {
61 let br = Self::compute_baud_rate(pclk, freq.into()); 61 let br = Self::compute_baud_rate(pclk, freq.into());
62 62
63 unsafe { 63 unsafe {
64 T::enable();
65 T::reset();
64 T::regs().cr1().modify(|w| { 66 T::regs().cr1().modify(|w| {
65 w.set_cpha( 67 w.set_cpha(
66 match config.mode.phase == Phase::CaptureOnSecondTransition { 68 match config.mode.phase == Phase::CaptureOnSecondTransition {
diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs
index 46fe817ea..a7ac54cdd 100644
--- a/embassy-stm32/src/spi/v2.rs
+++ b/embassy-stm32/src/spi/v2.rs
@@ -63,6 +63,8 @@ impl<'d, T: Instance> Spi<'d, T> {
63 let br = Self::compute_baud_rate(pclk, freq.into()); 63 let br = Self::compute_baud_rate(pclk, freq.into());
64 64
65 unsafe { 65 unsafe {
66 T::enable();
67 T::reset();
66 T::regs().cr2().modify(|w| { 68 T::regs().cr2().modify(|w| {
67 w.set_ssoe(false); 69 w.set_ssoe(false);
68 }); 70 });
diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs
index da4686b9c..6073616bd 100644
--- a/embassy-stm32/src/spi/v3.rs
+++ b/embassy-stm32/src/spi/v3.rs
@@ -64,6 +64,8 @@ impl<'d, T: Instance> Spi<'d, T> {
64 64
65 let br = Self::compute_baud_rate(pclk, freq.into()); 65 let br = Self::compute_baud_rate(pclk, freq.into());
66 unsafe { 66 unsafe {
67 T::enable();
68 T::reset();
67 T::regs().ifcr().write(|w| w.0 = 0xffff_ffff); 69 T::regs().ifcr().write(|w| w.0 = 0xffff_ffff);
68 T::regs().cfg2().modify(|w| { 70 T::regs().cfg2().modify(|w| {
69 //w.set_ssoe(true); 71 //w.set_ssoe(true);
diff --git a/stm32-data b/stm32-data
Subproject 6e4da8f04205dcc48767d12fac5cdfd170e52f1 Subproject 4bb1b178cd1c555cfedaea31ad0be3c6a7b9956
diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs
index 083e06bfd..008c9eb37 100644
--- a/stm32-metapac/build.rs
+++ b/stm32-metapac/build.rs
@@ -136,6 +136,7 @@ fn main() {
136 let mut interrupt_table: Vec<Vec<String>> = Vec::new(); 136 let mut interrupt_table: Vec<Vec<String>> = Vec::new();
137 let mut peripherals_table: Vec<Vec<String>> = Vec::new(); 137 let mut peripherals_table: Vec<Vec<String>> = Vec::new();
138 let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new(); 138 let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new();
139 let mut peripheral_rcc_table: Vec<Vec<String>> = Vec::new();
139 140
140 let dma_base = chip 141 let dma_base = chip
141 .peripherals 142 .peripherals
@@ -216,6 +217,29 @@ fn main() {
216 }; 217 };
217 assert_eq!(p.address, dma_base + dma_stride * dma_num); 218 assert_eq!(p.address, dma_base + dma_stride * dma_num);
218 } 219 }
220 "spi" => {
221 if let Some(clock) = &p.clock {
222 // Workaround for APB1 register being split on some chip families. Assume
223 // first register until we can find a way to hint which register is used
224 let reg = clock.to_ascii_lowercase();
225 let (enable_reg, reset_reg) = if chip.family == "STM32H7" && clock == "APB1"
226 {
227 (format!("{}lenr", reg), format!("{}lrstr", reg))
228 } else if chip.family.starts_with("STM32L4") && clock == "APB1" {
229 (format!("{}enr1", reg), format!("{}rstr1", reg))
230 } else {
231 (format!("{}enr", reg), format!("{}rstr", reg))
232 };
233 let field = name.to_ascii_lowercase();
234 peripheral_rcc_table.push(vec![
235 name.clone(),
236 enable_reg,
237 reset_reg,
238 format!("set_{}en", field),
239 format!("set_{}rst", field),
240 ]);
241 }
242 }
219 _ => {} 243 _ => {}
220 } 244 }
221 } 245 }
@@ -255,6 +279,7 @@ fn main() {
255 make_table(&mut extra, "peripherals", &peripherals_table); 279 make_table(&mut extra, "peripherals", &peripherals_table);
256 make_table(&mut extra, "peripheral_versions", &peripheral_version_table); 280 make_table(&mut extra, "peripheral_versions", &peripheral_version_table);
257 make_table(&mut extra, "peripheral_pins", &peripheral_pins_table); 281 make_table(&mut extra, "peripheral_pins", &peripheral_pins_table);
282 make_table(&mut extra, "peripheral_rcc", &peripheral_rcc_table);
258 283
259 for (module, version) in peripheral_versions { 284 for (module, version) in peripheral_versions {
260 println!("loading {} {}", module, version); 285 println!("loading {} {}", module, version);