aboutsummaryrefslogtreecommitdiff
path: root/embassy-net-adin1110/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-net-adin1110/src/lib.rs')
-rw-r--r--embassy-net-adin1110/src/lib.rs101
1 files changed, 40 insertions, 61 deletions
diff --git a/embassy-net-adin1110/src/lib.rs b/embassy-net-adin1110/src/lib.rs
index 8d73e024f..4af054aea 100644
--- a/embassy-net-adin1110/src/lib.rs
+++ b/embassy-net-adin1110/src/lib.rs
@@ -6,6 +6,9 @@
6#![allow(clippy::missing_panics_doc)] 6#![allow(clippy::missing_panics_doc)]
7#![doc = include_str!("../README.md")] 7#![doc = include_str!("../README.md")]
8 8
9// must go first!
10mod fmt;
11
9mod crc32; 12mod crc32;
10mod crc8; 13mod crc8;
11mod mdio; 14mod mdio;
@@ -20,12 +23,13 @@ use embassy_net_driver_channel as ch;
20use embassy_time::{Duration, Timer}; 23use embassy_time::{Duration, Timer};
21use embedded_hal_1::digital::OutputPin; 24use embedded_hal_1::digital::OutputPin;
22use embedded_hal_async::digital::Wait; 25use embedded_hal_async::digital::Wait;
23use embedded_hal_async::spi::{Operation, SpiDevice}; 26use embedded_hal_async::spi::{Error, Operation, SpiDevice};
24use heapless::Vec; 27use heapless::Vec;
25pub use mdio::MdioBus; 28pub use mdio::MdioBus;
26pub use phy::{Phy10BaseT1x, RegsC22, RegsC45}; 29pub use phy::{Phy10BaseT1x, RegsC22, RegsC45};
27pub use regs::{Config0, Config2, SpiRegisters as sr, Status0, Status1}; 30pub use regs::{Config0, Config2, SpiRegisters as sr, Status0, Status1};
28 31
32use crate::fmt::Bytes;
29use crate::regs::{LedCntrl, LedFunc, LedPol, LedPolarity, SpiHeader}; 33use crate::regs::{LedCntrl, LedFunc, LedPol, LedPolarity, SpiHeader};
30 34
31pub const PHYID: u32 = 0x0283_BC91; 35pub const PHYID: u32 = 0x0283_BC91;
@@ -153,8 +157,7 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
153 157
154 let value = u32::from_be_bytes(rx_buf[0..4].try_into().unwrap()); 158 let value = u32::from_be_bytes(rx_buf[0..4].try_into().unwrap());
155 159
156 #[cfg(feature = "defmt")] 160 trace!("REG Read {} = {:08x} SPI {}", reg, value, Bytes(&tx_buf));
157 defmt::trace!("REG Read {} = {:08x} SPI {:02x}", reg, value, &tx_buf);
158 161
159 Ok(value) 162 Ok(value)
160 } 163 }
@@ -181,8 +184,7 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
181 let _ = tx_buf.push(crc8(val.as_slice())); 184 let _ = tx_buf.push(crc8(val.as_slice()));
182 } 185 }
183 186
184 #[cfg(feature = "defmt")] 187 trace!("REG Write {} = {:08x} SPI {}", reg, value, Bytes(&tx_buf));
185 defmt::trace!("REG Write {} = {:08x} SPI {:02x}", reg, value, &tx_buf);
186 188
187 self.spi.write(&tx_buf).await.map_err(AdinError::Spi) 189 self.spi.write(&tx_buf).await.map_err(AdinError::Spi)
188 } 190 }
@@ -219,8 +221,7 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
219 let packet_size = fifo_frame_size - FRAME_HEADER_LEN - FCS_LEN; 221 let packet_size = fifo_frame_size - FRAME_HEADER_LEN - FCS_LEN;
220 222
221 if packet_size > frame.len() { 223 if packet_size > frame.len() {
222 #[cfg(feature = "defmt")] 224 trace!("MAX: {} WANT: {}", frame.len(), packet_size);
223 defmt::trace!("MAX: {} WANT: {}", frame.len(), packet_size);
224 return Err(AdinError::PACKET_TOO_BIG); 225 return Err(AdinError::PACKET_TOO_BIG);
225 } 226 }
226 227
@@ -333,14 +334,13 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
333 334
334 self.write_reg(sr::TX_FSIZE, send_len).await?; 335 self.write_reg(sr::TX_FSIZE, send_len).await?;
335 336
336 #[cfg(feature = "defmt")] 337 trace!(
337 defmt::trace!( 338 "TX: hdr {} [{}] {}-{}-{} SIZE: {}",
338 "TX: hdr {} [{}] {:02x}-{:02x}-{:02x} SIZE: {}",
339 head_data.len(), 339 head_data.len(),
340 frame.len(), 340 frame.len(),
341 head_data.as_slice(), 341 Bytes(head_data.as_slice()),
342 frame, 342 Bytes(frame),
343 tail_data.as_slice(), 343 Bytes(tail_data.as_slice()),
344 send_len, 344 send_len,
345 ); 345 );
346 346
@@ -445,16 +445,14 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
445 let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); 445 let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split();
446 446
447 loop { 447 loop {
448 #[cfg(feature = "defmt")] 448 debug!("Waiting for interrupts");
449 defmt::debug!("Waiting for interrupts");
450 match select(self.int.wait_for_low(), tx_chan.tx_buf()).await { 449 match select(self.int.wait_for_low(), tx_chan.tx_buf()).await {
451 Either::First(_) => { 450 Either::First(_) => {
452 let mut status1_clr = Status1(0); 451 let mut status1_clr = Status1(0);
453 let mut status1 = Status1(self.mac.read_reg(sr::STATUS1).await.unwrap()); 452 let mut status1 = Status1(self.mac.read_reg(sr::STATUS1).await.unwrap());
454 453
455 while status1.p1_rx_rdy() { 454 while status1.p1_rx_rdy() {
456 #[cfg(feature = "defmt")] 455 debug!("alloc RX packet buffer");
457 defmt::debug!("alloc RX packet buffer");
458 match select(rx_chan.rx_buf(), tx_chan.tx_buf()).await { 456 match select(rx_chan.rx_buf(), tx_chan.tx_buf()).await {
459 // Handle frames that needs to transmit from the wire. 457 // Handle frames that needs to transmit from the wire.
460 // Note: rx_chan.rx_buf() channel don´t accept new request 458 // Note: rx_chan.rx_buf() channel don´t accept new request
@@ -466,22 +464,18 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
466 } 464 }
467 Err(e) => match e { 465 Err(e) => match e {
468 AdinError::PACKET_TOO_BIG => { 466 AdinError::PACKET_TOO_BIG => {
469 #[cfg(feature = "defmt")] 467 error!("RX Packet too big, DROP");
470 defmt::error!("RX Packet too big, DROP");
471 self.mac.write_reg(sr::FIFO_CLR, 1).await.unwrap(); 468 self.mac.write_reg(sr::FIFO_CLR, 1).await.unwrap();
472 } 469 }
473 AdinError::PACKET_TOO_SMALL => { 470 AdinError::PACKET_TOO_SMALL => {
474 #[cfg(feature = "defmt")] 471 error!("RX Packet too small, DROP");
475 defmt::error!("RX Packet too small, DROP");
476 self.mac.write_reg(sr::FIFO_CLR, 1).await.unwrap(); 472 self.mac.write_reg(sr::FIFO_CLR, 1).await.unwrap();
477 } 473 }
478 AdinError::Spi(_) => { 474 AdinError::Spi(e) => {
479 #[cfg(feature = "defmt")] 475 error!("RX Spi error {}", e.kind());
480 defmt::error!("RX Spi error")
481 } 476 }
482 _ => { 477 _ => {
483 #[cfg(feature = "defmt")] 478 error!("RX Error");
484 defmt::error!("RX Error")
485 } 479 }
486 }, 480 },
487 }, 481 },
@@ -496,21 +490,18 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
496 490
497 let status0 = Status0(self.mac.read_reg(sr::STATUS0).await.unwrap()); 491 let status0 = Status0(self.mac.read_reg(sr::STATUS0).await.unwrap());
498 if status1.0 & !0x1b != 0 { 492 if status1.0 & !0x1b != 0 {
499 #[cfg(feature = "defmt")] 493 error!("SPE CHIP STATUS 0:{:08x} 1:{:08x}", status0.0, status1.0);
500 defmt::error!("SPE CHIP STATUS 0:{:08x} 1:{:08x}", status0.0, status1.0);
501 } 494 }
502 495
503 if status1.tx_rdy() { 496 if status1.tx_rdy() {
504 status1_clr.set_tx_rdy(true); 497 status1_clr.set_tx_rdy(true);
505 #[cfg(feature = "defmt")] 498 trace!("TX_DONE");
506 defmt::info!("TX_DONE");
507 } 499 }
508 500
509 if status1.link_change() { 501 if status1.link_change() {
510 let link = status1.p1_link_status(); 502 let link = status1.p1_link_status();
511 self.is_link_up = link; 503 self.is_link_up = link;
512 504
513 #[cfg(feature = "defmt")]
514 if link { 505 if link {
515 let link_status = self 506 let link_status = self
516 .mac 507 .mac
@@ -530,9 +521,9 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
530 .await 521 .await
531 .unwrap(); 522 .unwrap();
532 523
533 defmt::info!("LINK Changed: Link Up, Volt: {} V p-p, MSE: {:0004}", volt, mse); 524 info!("LINK Changed: Link Up, Volt: {} V p-p, MSE: {:0004}", volt, mse);
534 } else { 525 } else {
535 defmt::info!("LINK Changed: Link Down"); 526 info!("LINK Changed: Link Down");
536 } 527 }
537 528
538 state_chan.set_link_state(if link { LinkState::Up } else { LinkState::Down }); 529 state_chan.set_link_state(if link { LinkState::Up } else { LinkState::Down });
@@ -540,50 +531,42 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
540 } 531 }
541 532
542 if status1.tx_ecc_err() { 533 if status1.tx_ecc_err() {
543 #[cfg(feature = "defmt")] 534 error!("SPI TX_ECC_ERR error, CLEAR TX FIFO");
544 defmt::error!("SPI TX_ECC_ERR error, CLEAR TX FIFO");
545 self.mac.write_reg(sr::FIFO_CLR, 2).await.unwrap(); 535 self.mac.write_reg(sr::FIFO_CLR, 2).await.unwrap();
546 status1_clr.set_tx_ecc_err(true); 536 status1_clr.set_tx_ecc_err(true);
547 } 537 }
548 538
549 if status1.rx_ecc_err() { 539 if status1.rx_ecc_err() {
550 #[cfg(feature = "defmt")] 540 error!("SPI RX_ECC_ERR error");
551 defmt::error!("SPI RX_ECC_ERR error");
552 status1_clr.set_rx_ecc_err(true); 541 status1_clr.set_rx_ecc_err(true);
553 } 542 }
554 543
555 if status1.spi_err() { 544 if status1.spi_err() {
556 #[cfg(feature = "defmt")] 545 error!("SPI SPI_ERR CRC error");
557 defmt::error!("SPI SPI_ERR CRC error");
558 status1_clr.set_spi_err(true); 546 status1_clr.set_spi_err(true);
559 } 547 }
560 548
561 if status0.phyint() { 549 if status0.phyint() {
562 #[cfg_attr(not(feature = "defmt"), allow(unused_variables))]
563 let crsm_irq_st = self 550 let crsm_irq_st = self
564 .mac 551 .mac
565 .read_cl45(MDIO_PHY_ADDR, RegsC45::DA1E::CRSM_IRQ_STATUS.into()) 552 .read_cl45(MDIO_PHY_ADDR, RegsC45::DA1E::CRSM_IRQ_STATUS.into())
566 .await 553 .await
567 .unwrap(); 554 .unwrap();
568 555
569 #[cfg_attr(not(feature = "defmt"), allow(unused_variables))]
570 let phy_irq_st = self 556 let phy_irq_st = self
571 .mac 557 .mac
572 .read_cl45(MDIO_PHY_ADDR, RegsC45::DA1F::PHY_SYBSYS_IRQ_STATUS.into()) 558 .read_cl45(MDIO_PHY_ADDR, RegsC45::DA1F::PHY_SYBSYS_IRQ_STATUS.into())
573 .await 559 .await
574 .unwrap(); 560 .unwrap();
575 561
576 #[cfg(feature = "defmt")] 562 warn!(
577 defmt::warn!(
578 "SPE CHIP PHY CRSM_IRQ_STATUS {:04x} PHY_SUBSYS_IRQ_STATUS {:04x}", 563 "SPE CHIP PHY CRSM_IRQ_STATUS {:04x} PHY_SUBSYS_IRQ_STATUS {:04x}",
579 crsm_irq_st, 564 crsm_irq_st, phy_irq_st
580 phy_irq_st
581 ); 565 );
582 } 566 }
583 567
584 if status0.txfcse() { 568 if status0.txfcse() {
585 #[cfg(feature = "defmt")] 569 error!("Ethernet Frame FCS and calc FCS don't match!");
586 defmt::error!("SPE CHIP PHY TX Frame CRC error");
587 } 570 }
588 571
589 // Clear status0 572 // Clear status0
@@ -613,8 +596,7 @@ pub async fn new<const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait
613) -> (Device<'_>, Runner<'_, SPI, INT, RST>) { 596) -> (Device<'_>, Runner<'_, SPI, INT, RST>) {
614 use crate::regs::{IMask0, IMask1}; 597 use crate::regs::{IMask0, IMask1};
615 598
616 #[cfg(feature = "defmt")] 599 info!("INIT ADIN1110");
617 defmt::info!("INIT ADIN1110");
618 600
619 // Reset sequence 601 // Reset sequence
620 reset.set_low().unwrap(); 602 reset.set_low().unwrap();
@@ -634,23 +616,20 @@ pub async fn new<const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait
634 let id = mac.read_reg(sr::PHYID).await.unwrap(); 616 let id = mac.read_reg(sr::PHYID).await.unwrap();
635 assert_eq!(id, PHYID); 617 assert_eq!(id, PHYID);
636 618
637 #[cfg(feature = "defmt")] 619 debug!("SPE: CHIP MAC/ID: {:08x}", id);
638 defmt::debug!("SPE: CHIP MAC/ID: {:08x}", id);
639 620
640 #[cfg(feature = "defmt")] 621 #[cfg(any(feature = "defmt", feature = "log"))]
641 let adin_phy = Phy10BaseT1x::default(); 622 {
642 #[cfg(feature = "defmt")] 623 let adin_phy = Phy10BaseT1x::default();
643 let phy_id = adin_phy.get_id(&mut mac).await.unwrap(); 624 let phy_id = adin_phy.get_id(&mut mac).await.unwrap();
644 #[cfg(feature = "defmt")] 625 debug!("SPE: CHIP: PHY ID: {:08x}", phy_id);
645 defmt::debug!("SPE: CHIP: PHY ID: {:08x}", phy_id); 626 }
646 627
647 let mi_control = mac.read_cl22(MDIO_PHY_ADDR, RegsC22::CONTROL as u8).await.unwrap(); 628 let mi_control = mac.read_cl22(MDIO_PHY_ADDR, RegsC22::CONTROL as u8).await.unwrap();
648 #[cfg(feature = "defmt")] 629 debug!("SPE CHIP PHY MI_CONTROL {:04x}", mi_control);
649 defmt::println!("SPE CHIP PHY MI_CONTROL {:04x}", mi_control);
650 if mi_control & 0x0800 != 0 { 630 if mi_control & 0x0800 != 0 {
651 let val = mi_control & !0x0800; 631 let val = mi_control & !0x0800;
652 #[cfg(feature = "defmt")] 632 debug!("SPE CHIP PHY MI_CONTROL Disable PowerDown");
653 defmt::println!("SPE CHIP PHY MI_CONTROL Disable PowerDown");
654 mac.write_cl22(MDIO_PHY_ADDR, RegsC22::CONTROL as u8, val) 633 mac.write_cl22(MDIO_PHY_ADDR, RegsC22::CONTROL as u8, val)
655 .await 634 .await
656 .unwrap(); 635 .unwrap();