aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Miller <[email protected]>2022-03-14 14:27:24 -0500
committerGrant Miller <[email protected]>2022-03-14 15:56:08 -0500
commitf0b62bc8e06b3d75e13fd8e36738104923d56174 (patch)
treef278450fdfaac74ecc485ea242211aca8e104fe8
parent482ffea4dd9f6cfe816d9e86a3dff5ce45746409 (diff)
Use const REGS
-rw-r--r--embassy-stm32/src/spi/mod.rs118
1 files changed, 55 insertions, 63 deletions
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs
index a180c1d02..24819513c 100644
--- a/embassy-stm32/src/spi/mod.rs
+++ b/embassy-stm32/src/spi/mod.rs
@@ -10,6 +10,7 @@ use self::sealed::WordSize;
10use crate::dma::{slice_ptr_parts, slice_ptr_parts_mut, NoDma, Transfer}; 10use crate::dma::{slice_ptr_parts, slice_ptr_parts_mut, NoDma, Transfer};
11use crate::gpio::sealed::{AFType, Pin as _}; 11use crate::gpio::sealed::{AFType, Pin as _};
12use crate::gpio::AnyPin; 12use crate::gpio::AnyPin;
13use crate::pac::spi::Spi as Regs;
13use crate::pac::spi::{regs, vals}; 14use crate::pac::spi::{regs, vals};
14use crate::peripherals; 15use crate::peripherals;
15use crate::rcc::RccPeripheral; 16use crate::rcc::RccPeripheral;
@@ -17,8 +18,6 @@ use crate::time::Hertz;
17 18
18pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; 19pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
19 20
20type Regs = &'static crate::pac::spi::Spi;
21
22#[derive(Debug)] 21#[derive(Debug)]
23#[cfg_attr(feature = "defmt", derive(defmt::Format))] 22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
24pub enum Error { 23pub enum Error {
@@ -219,10 +218,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
219 218
220 #[cfg(any(spi_v1, spi_f1))] 219 #[cfg(any(spi_v1, spi_f1))]
221 unsafe { 220 unsafe {
222 T::regs().cr2().modify(|w| { 221 T::REGS.cr2().modify(|w| {
223 w.set_ssoe(false); 222 w.set_ssoe(false);
224 }); 223 });
225 T::regs().cr1().modify(|w| { 224 T::REGS.cr1().modify(|w| {
226 w.set_cpha(cpha); 225 w.set_cpha(cpha);
227 w.set_cpol(cpol); 226 w.set_cpol(cpol);
228 227
@@ -242,12 +241,12 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
242 } 241 }
243 #[cfg(spi_v2)] 242 #[cfg(spi_v2)]
244 unsafe { 243 unsafe {
245 T::regs().cr2().modify(|w| { 244 T::REGS.cr2().modify(|w| {
246 w.set_frxth(WordSize::EightBit.frxth()); 245 w.set_frxth(WordSize::EightBit.frxth());
247 w.set_ds(WordSize::EightBit.ds()); 246 w.set_ds(WordSize::EightBit.ds());
248 w.set_ssoe(false); 247 w.set_ssoe(false);
249 }); 248 });
250 T::regs().cr1().modify(|w| { 249 T::REGS.cr1().modify(|w| {
251 w.set_cpha(cpha); 250 w.set_cpha(cpha);
252 w.set_cpol(cpol); 251 w.set_cpol(cpol);
253 252
@@ -263,8 +262,8 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
263 } 262 }
264 #[cfg(spi_v3)] 263 #[cfg(spi_v3)]
265 unsafe { 264 unsafe {
266 T::regs().ifcr().write(|w| w.0 = 0xffff_ffff); 265 T::REGS.ifcr().write(|w| w.0 = 0xffff_ffff);
267 T::regs().cfg2().modify(|w| { 266 T::REGS.cfg2().modify(|w| {
268 //w.set_ssoe(true); 267 //w.set_ssoe(true);
269 w.set_ssoe(false); 268 w.set_ssoe(false);
270 w.set_cpha(cpha); 269 w.set_cpha(cpha);
@@ -279,16 +278,16 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
279 w.set_afcntr(vals::Afcntr::CONTROLLED); 278 w.set_afcntr(vals::Afcntr::CONTROLLED);
280 w.set_ssiop(vals::Ssiop::ACTIVEHIGH); 279 w.set_ssiop(vals::Ssiop::ACTIVEHIGH);
281 }); 280 });
282 T::regs().cfg1().modify(|w| { 281 T::REGS.cfg1().modify(|w| {
283 w.set_crcen(false); 282 w.set_crcen(false);
284 w.set_mbr(br); 283 w.set_mbr(br);
285 w.set_dsize(WordSize::EightBit.dsize()); 284 w.set_dsize(WordSize::EightBit.dsize());
286 }); 285 });
287 T::regs().cr2().modify(|w| { 286 T::REGS.cr2().modify(|w| {
288 w.set_tsize(0); 287 w.set_tsize(0);
289 w.set_tser(0); 288 w.set_tser(0);
290 }); 289 });
291 T::regs().cr1().modify(|w| { 290 T::REGS.cr1().modify(|w| {
292 w.set_ssi(false); 291 w.set_ssi(false);
293 w.set_spe(true); 292 w.set_spe(true);
294 }); 293 });
@@ -314,7 +313,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
314 313
315 #[cfg(any(spi_v1, spi_f1, spi_v2))] 314 #[cfg(any(spi_v1, spi_f1, spi_v2))]
316 unsafe { 315 unsafe {
317 T::regs().cr1().modify(|w| { 316 T::REGS.cr1().modify(|w| {
318 w.set_cpha(cpha); 317 w.set_cpha(cpha);
319 w.set_cpol(cpol); 318 w.set_cpol(cpol);
320 w.set_lsbfirst(lsbfirst); 319 w.set_lsbfirst(lsbfirst);
@@ -323,7 +322,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
323 322
324 #[cfg(spi_v3)] 323 #[cfg(spi_v3)]
325 unsafe { 324 unsafe {
326 T::regs().cfg2().modify(|w| { 325 T::REGS.cfg2().modify(|w| {
327 w.set_cpha(cpha); 326 w.set_cpha(cpha);
328 w.set_cpol(cpol); 327 w.set_cpol(cpol);
329 w.set_lsbfirst(lsbfirst); 328 w.set_lsbfirst(lsbfirst);
@@ -333,9 +332,9 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
333 332
334 pub fn get_current_config(&self) -> Config { 333 pub fn get_current_config(&self) -> Config {
335 #[cfg(any(spi_v1, spi_f1, spi_v2))] 334 #[cfg(any(spi_v1, spi_f1, spi_v2))]
336 let cfg = unsafe { T::regs().cr1().read() }; 335 let cfg = unsafe { T::REGS.cr1().read() };
337 #[cfg(spi_v3)] 336 #[cfg(spi_v3)]
338 let cfg = unsafe { T::regs().cfg2().read() }; 337 let cfg = unsafe { T::REGS.cfg2().read() };
339 let polarity = if cfg.cpol() == vals::Cpol::IDLELOW { 338 let polarity = if cfg.cpol() == vals::Cpol::IDLELOW {
340 Polarity::IdleLow 339 Polarity::IdleLow
341 } else { 340 } else {
@@ -366,40 +365,40 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
366 365
367 #[cfg(any(spi_v1, spi_f1))] 366 #[cfg(any(spi_v1, spi_f1))]
368 unsafe { 367 unsafe {
369 T::regs().cr1().modify(|reg| { 368 T::REGS.cr1().modify(|reg| {
370 reg.set_spe(false); 369 reg.set_spe(false);
371 reg.set_dff(word_size.dff()) 370 reg.set_dff(word_size.dff())
372 }); 371 });
373 T::regs().cr1().modify(|reg| { 372 T::REGS.cr1().modify(|reg| {
374 reg.set_spe(true); 373 reg.set_spe(true);
375 }); 374 });
376 } 375 }
377 #[cfg(spi_v2)] 376 #[cfg(spi_v2)]
378 unsafe { 377 unsafe {
379 T::regs().cr1().modify(|w| { 378 T::REGS.cr1().modify(|w| {
380 w.set_spe(false); 379 w.set_spe(false);
381 }); 380 });
382 T::regs().cr2().modify(|w| { 381 T::REGS.cr2().modify(|w| {
383 w.set_frxth(word_size.frxth()); 382 w.set_frxth(word_size.frxth());
384 w.set_ds(word_size.ds()); 383 w.set_ds(word_size.ds());
385 }); 384 });
386 T::regs().cr1().modify(|w| { 385 T::REGS.cr1().modify(|w| {
387 w.set_spe(true); 386 w.set_spe(true);
388 }); 387 });
389 } 388 }
390 #[cfg(spi_v3)] 389 #[cfg(spi_v3)]
391 unsafe { 390 unsafe {
392 T::regs().cr1().modify(|w| { 391 T::REGS.cr1().modify(|w| {
393 w.set_csusp(true); 392 w.set_csusp(true);
394 }); 393 });
395 while T::regs().sr().read().eot() {} 394 while T::REGS.sr().read().eot() {}
396 T::regs().cr1().modify(|w| { 395 T::REGS.cr1().modify(|w| {
397 w.set_spe(false); 396 w.set_spe(false);
398 }); 397 });
399 T::regs().cfg1().modify(|w| { 398 T::REGS.cfg1().modify(|w| {
400 w.set_dsize(word_size.dsize()); 399 w.set_dsize(word_size.dsize());
401 }); 400 });
402 T::regs().cr1().modify(|w| { 401 T::REGS.cr1().modify(|w| {
403 w.set_csusp(false); 402 w.set_csusp(false);
404 w.set_spe(true); 403 w.set_spe(true);
405 }); 404 });
@@ -414,34 +413,34 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
414 { 413 {
415 self.set_word_size(WordSize::EightBit); 414 self.set_word_size(WordSize::EightBit);
416 unsafe { 415 unsafe {
417 T::regs().cr1().modify(|w| { 416 T::REGS.cr1().modify(|w| {
418 w.set_spe(false); 417 w.set_spe(false);
419 }); 418 });
420 } 419 }
421 420
422 // TODO: This is unnecessary in some versions because 421 // TODO: This is unnecessary in some versions because
423 // clearing SPE automatically clears the fifos 422 // clearing SPE automatically clears the fifos
424 flush_rx_fifo(T::regs()); 423 flush_rx_fifo(T::REGS);
425 424
426 let tx_request = self.txdma.request(); 425 let tx_request = self.txdma.request();
427 let tx_dst = T::regs().tx_ptr(); 426 let tx_dst = T::REGS.tx_ptr();
428 unsafe { self.txdma.start_write(tx_request, data, tx_dst) } 427 unsafe { self.txdma.start_write(tx_request, data, tx_dst) }
429 let tx_f = Transfer::new(&mut self.txdma); 428 let tx_f = Transfer::new(&mut self.txdma);
430 429
431 unsafe { 430 unsafe {
432 set_txdmaen(T::regs(), true); 431 set_txdmaen(T::REGS, true);
433 T::regs().cr1().modify(|w| { 432 T::REGS.cr1().modify(|w| {
434 w.set_spe(true); 433 w.set_spe(true);
435 }); 434 });
436 #[cfg(spi_v3)] 435 #[cfg(spi_v3)]
437 T::regs().cr1().modify(|w| { 436 T::REGS.cr1().modify(|w| {
438 w.set_cstart(true); 437 w.set_cstart(true);
439 }); 438 });
440 } 439 }
441 440
442 tx_f.await; 441 tx_f.await;
443 442
444 finish_dma(T::regs()); 443 finish_dma(T::REGS);
445 444
446 Ok(()) 445 Ok(())
447 } 446 }
@@ -453,21 +452,21 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
453 { 452 {
454 self.set_word_size(WordSize::EightBit); 453 self.set_word_size(WordSize::EightBit);
455 unsafe { 454 unsafe {
456 T::regs().cr1().modify(|w| { 455 T::REGS.cr1().modify(|w| {
457 w.set_spe(false); 456 w.set_spe(false);
458 }); 457 });
459 set_rxdmaen(T::regs(), true); 458 set_rxdmaen(T::REGS, true);
460 } 459 }
461 460
462 let (_, clock_byte_count) = slice_ptr_parts_mut(data); 461 let (_, clock_byte_count) = slice_ptr_parts_mut(data);
463 462
464 let rx_request = self.rxdma.request(); 463 let rx_request = self.rxdma.request();
465 let rx_src = T::regs().rx_ptr(); 464 let rx_src = T::REGS.rx_ptr();
466 unsafe { self.rxdma.start_read(rx_request, rx_src, data) }; 465 unsafe { self.rxdma.start_read(rx_request, rx_src, data) };
467 let rx_f = Transfer::new(&mut self.rxdma); 466 let rx_f = Transfer::new(&mut self.rxdma);
468 467
469 let tx_request = self.txdma.request(); 468 let tx_request = self.txdma.request();
470 let tx_dst = T::regs().tx_ptr(); 469 let tx_dst = T::REGS.tx_ptr();
471 let clock_byte = 0x00u8; 470 let clock_byte = 0x00u8;
472 let tx_f = crate::dma::write_repeated( 471 let tx_f = crate::dma::write_repeated(
473 &mut self.txdma, 472 &mut self.txdma,
@@ -478,19 +477,19 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
478 ); 477 );
479 478
480 unsafe { 479 unsafe {
481 set_txdmaen(T::regs(), true); 480 set_txdmaen(T::REGS, true);
482 T::regs().cr1().modify(|w| { 481 T::REGS.cr1().modify(|w| {
483 w.set_spe(true); 482 w.set_spe(true);
484 }); 483 });
485 #[cfg(spi_v3)] 484 #[cfg(spi_v3)]
486 T::regs().cr1().modify(|w| { 485 T::REGS.cr1().modify(|w| {
487 w.set_cstart(true); 486 w.set_cstart(true);
488 }); 487 });
489 } 488 }
490 489
491 join(tx_f, rx_f).await; 490 join(tx_f, rx_f).await;
492 491
493 finish_dma(T::regs()); 492 finish_dma(T::REGS);
494 493
495 Ok(()) 494 Ok(())
496 } 495 }
@@ -506,79 +505,74 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
506 505
507 self.set_word_size(WordSize::EightBit); 506 self.set_word_size(WordSize::EightBit);
508 unsafe { 507 unsafe {
509 T::regs().cr1().modify(|w| { 508 T::REGS.cr1().modify(|w| {
510 w.set_spe(false); 509 w.set_spe(false);
511 }); 510 });
512 set_rxdmaen(T::regs(), true); 511 set_rxdmaen(T::REGS, true);
513 } 512 }
514 513
515 // TODO: This is unnecessary in some versions because 514 // TODO: This is unnecessary in some versions because
516 // clearing SPE automatically clears the fifos 515 // clearing SPE automatically clears the fifos
517 flush_rx_fifo(T::regs()); 516 flush_rx_fifo(T::REGS);
518 517
519 let rx_request = self.rxdma.request(); 518 let rx_request = self.rxdma.request();
520 let rx_src = T::regs().rx_ptr(); 519 let rx_src = T::REGS.rx_ptr();
521 unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; 520 unsafe { self.rxdma.start_read(rx_request, rx_src, read) };
522 let rx_f = Transfer::new(&mut self.rxdma); 521 let rx_f = Transfer::new(&mut self.rxdma);
523 522
524 let tx_request = self.txdma.request(); 523 let tx_request = self.txdma.request();
525 let tx_dst = T::regs().tx_ptr(); 524 let tx_dst = T::REGS.tx_ptr();
526 unsafe { self.txdma.start_write(tx_request, write, tx_dst) } 525 unsafe { self.txdma.start_write(tx_request, write, tx_dst) }
527 let tx_f = Transfer::new(&mut self.txdma); 526 let tx_f = Transfer::new(&mut self.txdma);
528 527
529 unsafe { 528 unsafe {
530 set_txdmaen(T::regs(), true); 529 set_txdmaen(T::REGS, true);
531 T::regs().cr1().modify(|w| { 530 T::REGS.cr1().modify(|w| {
532 w.set_spe(true); 531 w.set_spe(true);
533 }); 532 });
534 #[cfg(spi_v3)] 533 #[cfg(spi_v3)]
535 T::regs().cr1().modify(|w| { 534 T::REGS.cr1().modify(|w| {
536 w.set_cstart(true); 535 w.set_cstart(true);
537 }); 536 });
538 } 537 }
539 538
540 join(tx_f, rx_f).await; 539 join(tx_f, rx_f).await;
541 540
542 finish_dma(T::regs()); 541 finish_dma(T::REGS);
543 542
544 Ok(()) 543 Ok(())
545 } 544 }
546 545
547 pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> { 546 pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> {
548 self.set_word_size(W::WORDSIZE); 547 self.set_word_size(W::WORDSIZE);
549 let regs = T::regs();
550 for word in words.iter() { 548 for word in words.iter() {
551 let _ = transfer_word(regs, *word)?; 549 let _ = transfer_word(T::REGS, *word)?;
552 } 550 }
553 Ok(()) 551 Ok(())
554 } 552 }
555 553
556 pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { 554 pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> {
557 self.set_word_size(W::WORDSIZE); 555 self.set_word_size(W::WORDSIZE);
558 let regs = T::regs();
559 for word in words.iter_mut() { 556 for word in words.iter_mut() {
560 *word = transfer_word(regs, W::default())?; 557 *word = transfer_word(T::REGS, W::default())?;
561 } 558 }
562 Ok(()) 559 Ok(())
563 } 560 }
564 561
565 pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { 562 pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> {
566 self.set_word_size(W::WORDSIZE); 563 self.set_word_size(W::WORDSIZE);
567 let regs = T::regs();
568 for word in words.iter_mut() { 564 for word in words.iter_mut() {
569 *word = transfer_word(regs, *word)?; 565 *word = transfer_word(T::REGS, *word)?;
570 } 566 }
571 Ok(()) 567 Ok(())
572 } 568 }
573 569
574 pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> { 570 pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> {
575 self.set_word_size(W::WORDSIZE); 571 self.set_word_size(W::WORDSIZE);
576 let regs = T::regs();
577
578 let len = read.len().max(write.len()); 572 let len = read.len().max(write.len());
579 for i in 0..len { 573 for i in 0..len {
580 let wb = write.get(i).copied().unwrap_or_default(); 574 let wb = write.get(i).copied().unwrap_or_default();
581 let rb = transfer_word(regs, wb)?; 575 let rb = transfer_word(T::REGS, wb)?;
582 if let Some(r) = read.get_mut(i) { 576 if let Some(r) = read.get_mut(i) {
583 *r = rb; 577 *r = rb;
584 } 578 }
@@ -623,7 +617,7 @@ trait RegsExt {
623 fn rx_ptr<W>(&self) -> *mut W; 617 fn rx_ptr<W>(&self) -> *mut W;
624} 618}
625 619
626impl RegsExt for crate::pac::spi::Spi { 620impl RegsExt for Regs {
627 fn tx_ptr<W>(&self) -> *mut W { 621 fn tx_ptr<W>(&self) -> *mut W {
628 #[cfg(not(spi_v3))] 622 #[cfg(not(spi_v3))]
629 let dr = self.dr(); 623 let dr = self.dr();
@@ -938,7 +932,7 @@ pub(crate) mod sealed {
938 use super::*; 932 use super::*;
939 933
940 pub trait Instance { 934 pub trait Instance {
941 fn regs() -> &'static crate::pac::spi::Spi; 935 const REGS: Regs;
942 } 936 }
943 937
944 pub trait Word: Copy + 'static { 938 pub trait Word: Copy + 'static {
@@ -1016,9 +1010,7 @@ dma_trait!(TxDma, Instance);
1016foreach_peripheral!( 1010foreach_peripheral!(
1017 (spi, $inst:ident) => { 1011 (spi, $inst:ident) => {
1018 impl sealed::Instance for peripherals::$inst { 1012 impl sealed::Instance for peripherals::$inst {
1019 fn regs() -> &'static crate::pac::spi::Spi { 1013 const REGS: Regs = crate::pac::$inst;
1020 &crate::pac::$inst
1021 }
1022 } 1014 }
1023 1015
1024 impl Instance for peripherals::$inst {} 1016 impl Instance for peripherals::$inst {}