aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-net-adin1110/src/crc32.rs26
-rw-r--r--embassy-net-adin1110/src/lib.rs639
2 files changed, 254 insertions, 411 deletions
diff --git a/embassy-net-adin1110/src/crc32.rs b/embassy-net-adin1110/src/crc32.rs
index a41fedca7..a3474f70a 100644
--- a/embassy-net-adin1110/src/crc32.rs
+++ b/embassy-net-adin1110/src/crc32.rs
@@ -274,6 +274,15 @@ impl ETH_FSC {
274 } 274 }
275 275
276 #[must_use] 276 #[must_use]
277 pub fn update(self, data: &[u8]) -> Self {
278 let fsc = data.iter().fold(self.0 ^ u32::MAX, |crc, byte| {
279 let idx = u8::try_from(crc & 0xFF).unwrap() ^ byte;
280 CRC32R_LOOKUP_TABLE[usize::from(idx)] ^ (crc >> 8)
281 }) ^ u32::MAX;
282 Self(fsc)
283 }
284
285 #[must_use]
277 pub fn crc_ok(&self) -> bool { 286 pub fn crc_ok(&self) -> bool {
278 self.0 == Self::CRC32_OK 287 self.0 == Self::CRC32_OK
279 } 288 }
@@ -329,4 +338,21 @@ mod tests {
329 println!("{:08x}", own_crc.0); 338 println!("{:08x}", own_crc.0);
330 assert_eq!(own_crc.0, ETH_FSC::CRC32_OK); 339 assert_eq!(own_crc.0, ETH_FSC::CRC32_OK);
331 } 340 }
341
342 #[test]
343 fn crc32_update() {
344 let full_data = &[
345 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0x00, 0xe0, 0x4c, 0x68, 0xee, 0xee, 0xdd, 0x06, 0x00, 0x01, 0x08, 0x00,
346 0x06, 0x04, 0x00, 0x02, 0x00, 0xe0, 0x4c, 0x68, 0x09, 0xde, 0xc0, 0xa8, 0x01, 0x02, 0x12, 0x34, 0x56, 0x78,
347 0x9a, 0xbc, 0xc0, 0xa8, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x3d, 0x67, 0x7c,
349 ];
350
351 let (part_a, part_b) = full_data.split_at(16);
352 let crc_partially = ETH_FSC::new(part_a).update(part_b);
353
354 let crc_full = ETH_FSC::new(full_data);
355
356 assert_eq!(crc_full.0, crc_partially.0);
357 }
332} 358}
diff --git a/embassy-net-adin1110/src/lib.rs b/embassy-net-adin1110/src/lib.rs
index 8b81065c5..c0a9b44ee 100644
--- a/embassy-net-adin1110/src/lib.rs
+++ b/embassy-net-adin1110/src/lib.rs
@@ -61,13 +61,17 @@ const ETH_MIN_LEN: usize = 64;
61 61
62/// Ethernet `Frame Check Sequence` length 62/// Ethernet `Frame Check Sequence` length
63const FSC_LEN: usize = 4; 63const FSC_LEN: usize = 4;
64/// SPI Header, contains SPI action and register id.
65const SPI_HEADER_LEN: usize = 2;
66/// SPI Header CRC length
67const SPI_HEADER_CRC_LEN: usize = 1;
68/// Frame Header,
64const FRAME_HEADER_LEN: usize = 2; 69const FRAME_HEADER_LEN: usize = 2;
65const WR_HEADER_LEN: usize = 2;
66 70
67// P1 = 0x00, P2 = 0x01 71// P1 = 0x00, P2 = 0x01
68const PORT_ID_BYTE: u8 = 0x00; 72const PORT_ID_BYTE: u8 = 0x00;
69 73
70pub type Packet = Vec<u8, { MTU + FSC_LEN + WR_HEADER_LEN }>; 74pub type Packet = Vec<u8, { SPI_HEADER_LEN + FRAME_HEADER_LEN + MTU + FSC_LEN + 1 + 4 }>;
71 75
72/// Type alias for the embassy-net driver for ADIN1110 76/// Type alias for the embassy-net driver for ADIN1110
73pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; 77pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>;
@@ -192,7 +196,13 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
192 // Packet read of write to the MAC packet buffer must be a multipul of 4! 196 // Packet read of write to the MAC packet buffer must be a multipul of 4!
193 let read_size = packet_size.next_multiple_of(4); 197 let read_size = packet_size.next_multiple_of(4);
194 198
195 if packet_size < (FRAME_HEADER_LEN + FSC_LEN) || read_size > packet.len() { 199 if packet_size < (SPI_HEADER_LEN + FSC_LEN) {
200 return Err(AdinError::PACKET_TOO_SMALL);
201 }
202
203 if read_size > packet.len() {
204 #[cfg(feature = "defmt")]
205 defmt::trace!("MAX: {} WANT: {}", packet.len(), read_size);
196 return Err(AdinError::PACKET_TOO_BIG); 206 return Err(AdinError::PACKET_TOO_BIG);
197 } 207 }
198 208
@@ -209,16 +219,18 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
209 // Turn around byte, TODO: Unknown that this is. 219 // Turn around byte, TODO: Unknown that this is.
210 let _ = tx_buf.push(TURN_AROUND_BYTE); 220 let _ = tx_buf.push(TURN_AROUND_BYTE);
211 221
212 let spi_packet = &mut packet[0..read_size as usize]; 222 let spi_packet = &mut packet[0..read_size];
213 223
214 assert_eq!(spi_packet.len() & 0x03, 0x00); 224 assert_eq!(spi_packet.len() & 0x03, 0x00);
215 225
216 let mut pkt_header = [0, 0]; 226 let mut pkt_header = [0, 0];
227 let mut fsc = [0, 0, 0, 0];
217 228
218 let mut spi_op = [ 229 let mut spi_op = [
219 Operation::Write(&tx_buf), 230 Operation::Write(&tx_buf),
220 Operation::Read(&mut pkt_header), 231 Operation::Read(&mut pkt_header),
221 Operation::Read(spi_packet), 232 Operation::Read(spi_packet),
233 Operation::Read(&mut fsc),
222 ]; 234 ];
223 235
224 self.spi.transaction(&mut spi_op).await.map_err(AdinError::Spi)?; 236 self.spi.transaction(&mut spi_op).await.map_err(AdinError::Spi)?;
@@ -228,80 +240,86 @@ impl<SPI: SpiDevice> ADIN1110<SPI> {
228 240
229 /// Write to fifo ethernet packet memory send over the wire. 241 /// Write to fifo ethernet packet memory send over the wire.
230 pub async fn write_fifo(&mut self, frame: &[u8]) -> AEResult<(), SPI::Error> { 242 pub async fn write_fifo(&mut self, frame: &[u8]) -> AEResult<(), SPI::Error> {
231 let header_len = self.header_write_len(); 243 const HEAD_LEN: usize = SPI_HEADER_LEN + SPI_HEADER_CRC_LEN + FRAME_HEADER_LEN;
244 const TAIL_LEN: usize = ETH_MIN_LEN - FSC_LEN + FSC_LEN + 1;
245
246 if frame.len() < (6 + 6 + 2) {
247 return Err(AdinError::PACKET_TOO_SMALL);
248 }
249 if frame.len() > (MAX_BUFF - FRAME_HEADER_LEN) {
250 return Err(AdinError::PACKET_TOO_BIG);
251 }
232 252
233 let mut packet = Packet::new(); 253 // SPI HEADER + [OPTIONAL SPI CRC] + FRAME HEADER
254 let mut head_data = Vec::<u8, HEAD_LEN>::new();
255 // [OPTIONAL PAD DATA] + FCS + [OPTINAL BYTES MAKE SPI FRAME EVEN]
256 let mut tail_data = Vec::<u8, TAIL_LEN>::new();
234 257
235 let mut spi_hdr = SpiHeader(0); 258 let mut spi_hdr = SpiHeader(0);
236 spi_hdr.set_control(true); 259 spi_hdr.set_control(true);
237 spi_hdr.set_write(true); 260 spi_hdr.set_write(true);
238 spi_hdr.set_addr(sr::TX); 261 spi_hdr.set_addr(sr::TX);
239 262
240 packet 263 head_data
241 .extend_from_slice(spi_hdr.0.to_be_bytes().as_slice()) 264 .extend_from_slice(spi_hdr.0.to_be_bytes().as_slice())
242 .map_err(|_e| AdinError::PACKET_TOO_BIG)?; 265 .map_err(|_e| AdinError::PACKET_TOO_BIG)?;
243 266
244 if self.crc { 267 if self.crc {
245 // Add CRC for header data 268 // Add CRC for header data
246 packet 269 head_data
247 .push(crc8(&packet[0..2])) 270 .push(crc8(&head_data[0..2]))
248 .map_err(|_| AdinError::PACKET_TOO_BIG)?; 271 .map_err(|_| AdinError::PACKET_TOO_BIG)?;
249 } 272 }
250 273
251 // Add port number, ADIN1110 its fixed to zero/P1, but for ADIN2111 has two ports. 274 // Add port number, ADIN1110 its fixed to zero/P1, but for ADIN2111 has two ports.
252 packet 275 head_data
253 .extend_from_slice(u16::from(PORT_ID_BYTE).to_be_bytes().as_slice()) 276 .extend_from_slice(u16::from(PORT_ID_BYTE).to_be_bytes().as_slice())
254 .map_err(|_e| AdinError::PACKET_TOO_BIG)?; 277 .map_err(|_e| AdinError::PACKET_TOO_BIG)?;
255 278
256 // Copy packet data to spi buffer. 279 let mut frame_fcs = ETH_FSC::new(frame);
257 packet
258 .extend_from_slice(frame)
259 .map_err(|_e| AdinError::PACKET_TOO_BIG)?;
260 280
261 // Pad data up to ETH_MIN_LEN - FCS_LEN 281 // ADIN1110 MAC and PHY don“t accept ethernet packet smaller than 64 bytes.
262 for _ in packet.len()..(ETH_MIN_LEN - FSC_LEN + header_len) { 282 // So padded the data minus the FCS, FCS is automatilly added to by the MAC.
263 let _ = packet.push(0x00); 283 if let Some(pad_len) = (ETH_MIN_LEN - FSC_LEN).checked_sub(frame.len()) {
284 let _ = tail_data.resize(pad_len, 0x00);
285 frame_fcs = frame_fcs.update(&tail_data);
264 } 286 }
265 287
266 // add ethernet FCS only over the ethernet packet. 288 // Add ethernet FCS only over the ethernet packet.
267 let crc = ETH_FSC::new(&packet[header_len..]); 289 // Only usefull when `CONFIG0`, `Transmit Frame Check Sequence Validation Enable` bit is enabled.
268 let _ = packet.extend_from_slice(crc.hton_bytes().as_slice()); 290 let _ = tail_data.extend_from_slice(frame_fcs.hton_bytes().as_slice());
269 291
270 let send_len = 292 // len = frame_size + optional padding + 2 bytes Frame header
271 u32::try_from(packet.len() - header_len + FRAME_HEADER_LEN).map_err(|_| AdinError::PACKET_TOO_BIG)?; 293 let send_len_orig = frame.len() + tail_data.len() + FRAME_HEADER_LEN;
294 let spi_pad_len = send_len_orig.next_multiple_of(4);
295 let send_len = u32::try_from(send_len_orig).map_err(|_| AdinError::PACKET_TOO_BIG)?;
272 296
273 // Packet read of write to the MAC packet buffer must be a multipul of 4 bytes! 297 // Packet read of write to the MAC packet buffer must be a multipul of 4 bytes!
274 while packet.len() & 0x3 != 0 { 298 if spi_pad_len != send_len_orig {
275 let _ = packet.push(DONT_CARE_BYTE); 299 let spi_pad_len = spi_pad_len - send_len_orig;
300 let _ = tail_data.extend_from_slice(&[DONT_CARE_BYTE, DONT_CARE_BYTE, DONT_CARE_BYTE][..spi_pad_len]);
276 } 301 }
277 302
278 #[cfg(feature = "defmt")] 303 #[cfg(feature = "defmt")]
279 defmt::trace!( 304 defmt::trace!(
280 "TX: hdr {} [{}] {:02x} SIZE: {}", 305 "TX: hdr {} [{}] {:02x}-{:02x}-{:02x} SIZE: {}",
281 header_len, 306 head_data.len(),
282 packet.len(), 307 frame.len(),
283 &packet, 308 head_data.as_slice(),
309 frame,
310 tail_data.as_slice(),
284 send_len, 311 send_len,
285 ); 312 );
286 313
287 self.write_reg(sr::TX_FSIZE, send_len).await?; 314 self.write_reg(sr::TX_FSIZE, send_len).await?;
288 315
289 // Spi packet must be half word / even length 316 let mut transaction = [
290 if send_len & 1 != 0 { 317 Operation::Write(head_data.as_slice()),
291 let _ = packet.push(0x00); 318 Operation::Write(frame),
292 } 319 Operation::Write(tail_data.as_slice()),
293 320 ];
294 self.spi.write(&packet).await.map_err(AdinError::Spi)
295 }
296
297 pub fn header_write_len(&self) -> usize {
298 // u16 + [CRC] + PORT
299 WR_HEADER_LEN + FRAME_HEADER_LEN + usize::from(self.crc)
300 }
301 321
302 pub fn header_len_read(&self) -> usize { 322 self.spi.transaction(&mut transaction).await.map_err(AdinError::Spi)
303 // u16 + [CRC] + u8
304 WR_HEADER_LEN + 1 + usize::from(self.crc)
305 } 323 }
306 324
307 /// Programs the mac address in the mac filters. 325 /// Programs the mac address in the mac filters.
@@ -815,12 +833,12 @@ mod tests {
815 SpiTransaction::write_vec(vec![0xA0, 0x09, 39, 0x12, 0x34, 0x56, 0x78, 28]), 833 SpiTransaction::write_vec(vec![0xA0, 0x09, 39, 0x12, 0x34, 0x56, 0x78, 28]),
816 SpiTransaction::flush(), 834 SpiTransaction::flush(),
817 ]; 835 ];
818 let mut spi = SpiMock::new(&expectations);
819 836
837 // Basic test init block
838 let mut spi = SpiMock::new(&expectations);
820 let cs = CsPinMock::default(); 839 let cs = CsPinMock::default();
821 let delay = MockDelay {}; 840 let delay = MockDelay {};
822 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay); 841 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay);
823
824 let mut spe = ADIN1110::new(spi_dev, true); 842 let mut spe = ADIN1110::new(spi_dev, true);
825 843
826 // Write reg: 0x1FFF 844 // Write reg: 0x1FFF
@@ -829,365 +847,121 @@ mod tests {
829 spi.done(); 847 spi.done();
830 } 848 }
831 849
832 // #[test] 850 #[futures_test::test]
833 // fn write_packet_to_fifo_less_64b_with_crc() { 851 async fn write_packet_to_fifo_minimal_with_crc() {
834 // // Configure expectations 852 // Configure expectations
835 // let mut expectations = vec![ 853 let mut expectations = vec![];
836 // // HEADER 854
837 // SpiTransaction::send(0xA0), 855 // Write TX_SIZE reg
838 // SpiTransaction::read(DONT_CARE_BYTE), 856 expectations.push(SpiTransaction::write_vec(vec![160, 48, 136, 0, 0, 0, 66, 201]));
839 // SpiTransaction::send(0x30), 857 expectations.push(SpiTransaction::flush());
840 // SpiTransaction::read(DONT_CARE_BYTE), 858
841 // SpiTransaction::send(136), 859 // Write TX reg.
842 // SpiTransaction::read(DONT_CARE_BYTE), 860 // SPI Header + optional CRC + Frame Header
843 // // Frame Size 861 expectations.push(SpiTransaction::write_vec(vec![160, 49, 143, 0, 0]));
844 // SpiTransaction::send(0x00), 862 // Packet data
845 // SpiTransaction::read(DONT_CARE_BYTE), 863 let packet = [0xFF_u8; 60];
846 // SpiTransaction::send(0x00), 864 expectations.push(SpiTransaction::write_vec(packet.to_vec()));
847 // SpiTransaction::read(DONT_CARE_BYTE), 865
848 // SpiTransaction::send(0x00), 866 let mut tail = std::vec::Vec::<u8>::with_capacity(100);
849 // SpiTransaction::read(DONT_CARE_BYTE), 867 // Padding
850 // SpiTransaction::send(66), 868 if let Some(padding_len) = (ETH_MIN_LEN - FSC_LEN).checked_sub(packet.len()) {
851 // SpiTransaction::read(DONT_CARE_BYTE), 869 tail.resize(padding_len, 0x00);
852 // SpiTransaction::send(201), 870 }
853 // SpiTransaction::read(DONT_CARE_BYTE), 871 // Packet FCS + optinal padding
854 // // HEADER 872 tail.extend_from_slice(&[77, 241, 140, 244, DONT_CARE_BYTE, DONT_CARE_BYTE]);
855 // SpiTransaction::send(0xA0), 873
856 // SpiTransaction::read(DONT_CARE_BYTE), 874 expectations.push(SpiTransaction::write_vec(tail));
857 // SpiTransaction::send(0x31), 875 expectations.push(SpiTransaction::flush());
858 // SpiTransaction::read(DONT_CARE_BYTE), 876
859 // // Port 877 let mut spi = SpiMock::new(&expectations);
860 // SpiTransaction::send(0x00), 878
861 // SpiTransaction::read(DONT_CARE_BYTE), 879 let cs = CsPinMock::default();
862 // SpiTransaction::send(PORT_ID_BYTE), 880 let delay = MockDelay {};
863 // SpiTransaction::read(DONT_CARE_BYTE), 881 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay);
864 // ]; 882
865 883 let mut spe = ADIN1110::new(spi_dev, true);
866 // let mut packet = Packet::new(); 884
867 // packet.resize(ETH_MIN_LEN, 0).unwrap(); 885 assert!(spe.write_fifo(&packet).await.is_ok());
868 886
869 // for &byte in &packet[4..] { 887 spi.done();
870 // expectations.push(SpiTransaction::send(byte)); 888 }
871 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
872 // }
873
874 // // padding
875 // for _ in packet.len()..65 {
876 // expectations.push(SpiTransaction::send(0x00));
877 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
878 // }
879
880 // // fcs
881 // for &byte in &[8, 137, 18, 4] {
882 // expectations.push(SpiTransaction::send(byte));
883 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
884 // }
885
886 // let spi = SpiMock::new(&expectations);
887
888 // let cs = CsPinMock {};
889 // let mut spe = Adin1110::new(spi, cs, true);
890
891 // assert!(spe.write_fifo(&mut packet).is_ok());
892 // }
893
894 // #[test]
895 // fn write_packet_to_fifo_less_64b_no_crc() {
896 // // Configure expectations
897 // let mut expectations = vec![
898 // // HEADER
899 // SpiTransaction::send(0xA0),
900 // SpiTransaction::read(DONT_CARE_BYTE),
901 // SpiTransaction::send(0x30),
902 // SpiTransaction::read(DONT_CARE_BYTE),
903 // // Frame Size
904 // SpiTransaction::send(0x00),
905 // SpiTransaction::read(DONT_CARE_BYTE),
906 // SpiTransaction::send(0x00),
907 // SpiTransaction::read(DONT_CARE_BYTE),
908 // SpiTransaction::send(0x00),
909 // SpiTransaction::read(DONT_CARE_BYTE),
910 // SpiTransaction::send(66),
911 // SpiTransaction::read(DONT_CARE_BYTE),
912 // // HEADER
913 // SpiTransaction::send(0xA0),
914 // SpiTransaction::read(DONT_CARE_BYTE),
915 // SpiTransaction::send(0x31),
916 // SpiTransaction::read(DONT_CARE_BYTE),
917 // // Port
918 // SpiTransaction::send(0x00),
919 // SpiTransaction::read(DONT_CARE_BYTE),
920 // SpiTransaction::send(PORT_ID_BYTE),
921 // SpiTransaction::read(DONT_CARE_BYTE),
922 // ];
923
924 // let mut packet = Packet::new();
925 // packet.resize(ETH_MIN_LEN, 0).unwrap();
926
927 // for &byte in &packet[4..] {
928 // expectations.push(SpiTransaction::send(byte));
929 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
930 // }
931
932 // // padding
933 // for _ in packet.len() as u32..ETH_MIN_LEN {
934 // expectations.push(SpiTransaction::send(0x00));
935 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
936 // }
937
938 // // fcs
939 // for &byte in &[8, 137, 18, 4] {
940 // expectations.push(SpiTransaction::send(byte));
941 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
942 // }
943
944 // let spi = SpiMock::new(&expectations);
945
946 // let cs = CsPinMock {};
947 // let mut spe = Adin1110::new(spi, cs, false);
948
949 // assert!(spe.write_fifo(&mut packet).is_ok());
950 // }
951
952 // #[test]
953 // fn write_packet_to_fifo_1500b() {
954 // // Configure expectations
955 // let mut expectations = vec![
956 // // HEADER
957 // SpiTransaction::send(0xA0),
958 // SpiTransaction::read(DONT_CARE_BYTE),
959 // SpiTransaction::send(0x30),
960 // SpiTransaction::read(DONT_CARE_BYTE),
961 // // Frame Size
962 // SpiTransaction::send(0x00),
963 // SpiTransaction::read(DONT_CARE_BYTE),
964 // SpiTransaction::send(0x00),
965 // SpiTransaction::read(DONT_CARE_BYTE),
966 // SpiTransaction::send(0x05),
967 // SpiTransaction::read(DONT_CARE_BYTE),
968 // SpiTransaction::send(0xDE),
969 // SpiTransaction::read(DONT_CARE_BYTE),
970 // // HEADER
971 // SpiTransaction::send(0xA0),
972 // SpiTransaction::read(DONT_CARE_BYTE),
973 // SpiTransaction::send(0x31),
974 // SpiTransaction::read(DONT_CARE_BYTE),
975 // // Port
976 // SpiTransaction::send(0x00),
977 // SpiTransaction::read(DONT_CARE_BYTE),
978 // SpiTransaction::send(PORT_ID_BYTE),
979 // SpiTransaction::read(DONT_CARE_BYTE),
980 // ];
981
982 // let mut packet = Packet::new();
983 // packet.resize(1500, 0).unwrap();
984
985 // for &byte in &packet[4..] {
986 // expectations.push(SpiTransaction::send(byte));
987 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
988 // }
989
990 // // fcs
991 // for &byte in &[212, 114, 18, 50] {
992 // expectations.push(SpiTransaction::send(byte));
993 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
994 // }
995
996 // let spi = SpiMock::new(&expectations);
997
998 // let cs = CsPinMock {};
999 // let mut spe = Adin1110::new(spi, cs, false);
1000
1001 // assert!(spe.write_fifo(&mut packet).is_ok());
1002 // }
1003
1004 // #[test]
1005 // fn write_packet_to_fifo_65b() {
1006 // // Configure expectations
1007 // let mut expectations = vec![
1008 // // HEADER
1009 // SpiTransaction::send(0xA0),
1010 // SpiTransaction::read(DONT_CARE_BYTE),
1011 // SpiTransaction::send(0x30),
1012 // SpiTransaction::read(DONT_CARE_BYTE),
1013 // // Frame Size
1014 // SpiTransaction::send(0x00),
1015 // SpiTransaction::read(DONT_CARE_BYTE),
1016 // SpiTransaction::send(0x00),
1017 // SpiTransaction::read(DONT_CARE_BYTE),
1018 // SpiTransaction::send(0x00),
1019 // SpiTransaction::read(DONT_CARE_BYTE),
1020 // SpiTransaction::send(67),
1021 // SpiTransaction::read(DONT_CARE_BYTE),
1022 // // HEADER
1023 // SpiTransaction::send(0xA0),
1024 // SpiTransaction::read(DONT_CARE_BYTE),
1025 // SpiTransaction::send(0x31),
1026 // SpiTransaction::read(DONT_CARE_BYTE),
1027 // // Port
1028 // SpiTransaction::send(0x00),
1029 // SpiTransaction::read(DONT_CARE_BYTE),
1030 // SpiTransaction::send(PORT_ID_BYTE),
1031 // SpiTransaction::read(DONT_CARE_BYTE),
1032 // ];
1033
1034 // let mut packet = Packet::new();
1035 // packet.resize(65, 0).unwrap();
1036
1037 // for &byte in &packet[4..] {
1038 // expectations.push(SpiTransaction::send(byte));
1039 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1040 // }
1041
1042 // // padding
1043 // for _ in packet.len()..ETH_MIN_LEN {
1044 // expectations.push(SpiTransaction::send(0x00));
1045 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1046 // }
1047
1048 // // fcs
1049 // for &byte in &[54, 117, 221, 220] {
1050 // expectations.push(SpiTransaction::send(byte));
1051 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1052 // }
1053
1054 // let spi = SpiMock::new(&expectations);
1055
1056 // let cs = CsPinMock {};
1057 // let mut spe = Adin1110::new(spi, cs, false);
1058
1059 // assert!(spe.write_fifo(&mut packet).is_ok());
1060 // }
1061
1062 // #[test]
1063 // fn write_packet_to_fifo_66b() {
1064 // // Configure expectations
1065 // let mut expectations = vec![
1066 // // HEADER
1067 // SpiTransaction::send(0xA0),
1068 // SpiTransaction::read(DONT_CARE_BYTE),
1069 // SpiTransaction::send(0x30),
1070 // SpiTransaction::read(DONT_CARE_BYTE),
1071 // // Frame Size
1072 // SpiTransaction::send(0x00),
1073 // SpiTransaction::read(DONT_CARE_BYTE),
1074 // SpiTransaction::send(0x00),
1075 // SpiTransaction::read(DONT_CARE_BYTE),
1076 // SpiTransaction::send(0x00),
1077 // SpiTransaction::read(DONT_CARE_BYTE),
1078 // SpiTransaction::send(68),
1079 // SpiTransaction::read(DONT_CARE_BYTE),
1080 // // HEADER
1081 // SpiTransaction::send(0xA0),
1082 // SpiTransaction::read(DONT_CARE_BYTE),
1083 // SpiTransaction::send(0x31),
1084 // SpiTransaction::read(DONT_CARE_BYTE),
1085 // // Port
1086 // SpiTransaction::send(0x00),
1087 // SpiTransaction::read(DONT_CARE_BYTE),
1088 // SpiTransaction::send(PORT_ID_BYTE),
1089 // SpiTransaction::read(DONT_CARE_BYTE),
1090 // ];
1091
1092 // let mut packet = Packet::new();
1093 // packet.resize(66, 0).unwrap();
1094
1095 // for &byte in &packet[4..] {
1096 // expectations.push(SpiTransaction::send(byte));
1097 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1098 // }
1099
1100 // // padding
1101 // for _ in packet.len()..ETH_MIN_LEN {
1102 // expectations.push(SpiTransaction::send(0x00));
1103 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1104 // }
1105
1106 // // fcs
1107 // for &byte in &[97, 167, 100, 29] {
1108 // expectations.push(SpiTransaction::send(byte));
1109 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1110 // }
1111 // let spi = SpiMock::new(&expectations);
1112
1113 // let cs = CsPinMock {};
1114 // let mut spe = Adin1110::new(spi, cs, false);
1115
1116 // assert!(spe.write_fifo(&mut packet).is_ok());
1117 // }
1118
1119 // #[test]
1120 // fn write_packet_to_fifo_67b() {
1121 // // Configure expectations
1122 // let mut expectations = vec![
1123 // // HEADER
1124 // SpiTransaction::send(0xA0),
1125 // SpiTransaction::read(DONT_CARE_BYTE),
1126 // SpiTransaction::send(0x30),
1127 // SpiTransaction::read(DONT_CARE_BYTE),
1128 // // Frame Size
1129 // SpiTransaction::send(0x00),
1130 // SpiTransaction::read(DONT_CARE_BYTE),
1131 // SpiTransaction::send(0x00),
1132 // SpiTransaction::read(DONT_CARE_BYTE),
1133 // SpiTransaction::send(0x00),
1134 // SpiTransaction::read(DONT_CARE_BYTE),
1135 // SpiTransaction::send(69),
1136 // SpiTransaction::read(DONT_CARE_BYTE),
1137 // // HEADER
1138 // SpiTransaction::send(0xA0),
1139 // SpiTransaction::read(DONT_CARE_BYTE),
1140 // SpiTransaction::send(0x31),
1141 // SpiTransaction::read(DONT_CARE_BYTE),
1142 // // Port
1143 // SpiTransaction::send(0x00),
1144 // SpiTransaction::read(DONT_CARE_BYTE),
1145 // SpiTransaction::send(PORT_ID_BYTE),
1146 // SpiTransaction::read(DONT_CARE_BYTE),
1147 // ];
1148
1149 // let mut packet = Packet::new();
1150 // packet.resize(67, 0).unwrap();
1151
1152 // for &byte in &packet[4..] {
1153 // expectations.push(SpiTransaction::send(byte));
1154 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1155 // }
1156
1157 // // padding
1158 // for _ in packet.len()..ETH_MIN_LEN {
1159 // expectations.push(SpiTransaction::send(0x00));
1160 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1161 // }
1162
1163 // // fcs
1164 // for &byte in &[228, 218, 170, 232] {
1165 // expectations.push(SpiTransaction::send(byte));
1166 // expectations.push(SpiTransaction::read(DONT_CARE_BYTE));
1167 // }
1168 // let spi = SpiMock::new(&expectations);
1169
1170 // let cs = CsPinMock {};
1171 // let mut spe = Adin1110::new(spi, cs, false);
1172
1173 // assert!(spe.write_fifo(&mut packet).is_ok());
1174 // }
1175 889
1176 #[futures_test::test] 890 #[futures_test::test]
1177 async fn write_packet_to_fifo_arp_46bytes() { 891 async fn write_packet_to_fifo_max_mtu_with_crc() {
892 assert_eq!(MTU, 1514);
1178 // Configure expectations 893 // Configure expectations
1179 let mut expectations = vec![]; 894 let mut expectations = vec![];
1180 895
1181 let mut packet = Packet::new(); 896 // Write TX_SIZE reg
1182 //arp packet; 897 expectations.push(SpiTransaction::write_vec(vec![160, 48, 136, 0, 0, 5, 240, 159]));
1183 packet 898 expectations.push(SpiTransaction::flush());
1184 .extend_from_slice(&[ 899
1185 34, 51, 68, 85, 102, 119, 18, 52, 86, 120, 154, 188, 8, 6, 0, 1, 8, 0, 6, 4, 0, 2, 18, 52, 86, 120, 900 // Write TX reg.
1186 154, 188, 192, 168, 16, 4, 34, 51, 68, 85, 102, 119, 192, 168, 16, 1, 901 // SPI Header + optional CRC + Frame Header
1187 ]) 902 expectations.push(SpiTransaction::write_vec(vec![160, 49, 143, 0, 0]));
1188 .unwrap(); 903 // Packet data
904 let packet = [0xAA_u8; MTU];
905 expectations.push(SpiTransaction::write_vec(packet.to_vec()));
906
907 let mut tail = std::vec::Vec::<u8>::with_capacity(100);
908 // Padding
909 if let Some(padding_len) = (ETH_MIN_LEN - FSC_LEN).checked_sub(packet.len()) {
910 tail.resize(padding_len, 0x00);
911 }
912 // Packet FCS + optinal padding
913 tail.extend_from_slice(&[49, 196, 205, 160]);
914
915 expectations.push(SpiTransaction::write_vec(tail));
916 expectations.push(SpiTransaction::flush());
1189 917
1190 let mut spi_packet = Packet::new(); 918 let mut spi = SpiMock::new(&expectations);
919
920 let cs = CsPinMock::default();
921 let delay = MockDelay {};
922 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay);
923
924 let mut spe = ADIN1110::new(spi_dev, true);
925
926 assert!(spe.write_fifo(&packet).await.is_ok());
927
928 spi.done();
929 }
930
931 #[futures_test::test]
932 async fn write_packet_to_fifo_invalid_lengths() {
933 assert_eq!(MTU, 1514);
934
935 // Configure expectations
936 let expectations = vec![];
937
938 // Max packet size = MAX_BUFF - FRAME_HEADER_LEN
939 let packet = [0xAA_u8; MAX_BUFF - FRAME_HEADER_LEN + 1];
940
941 let mut spi = SpiMock::new(&expectations);
942
943 let cs = CsPinMock::default();
944 let delay = MockDelay {};
945 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay);
946
947 let mut spe = ADIN1110::new(spi_dev, true);
948
949 // minimal
950 assert!(matches!(
951 spe.write_fifo(&packet[0..(6 + 6 + 2 - 1)]).await,
952 Err(AdinError::PACKET_TOO_SMALL)
953 ));
954
955 // max + 1
956 assert!(matches!(spe.write_fifo(&packet).await, Err(AdinError::PACKET_TOO_BIG)));
957
958 spi.done();
959 }
960
961 #[futures_test::test]
962 async fn write_packet_to_fifo_arp_46bytes_with_crc() {
963 // Configure expectations
964 let mut expectations = vec![];
1191 965
1192 // Write TX_SIZE reg 966 // Write TX_SIZE reg
1193 expectations.push(SpiTransaction::write_vec(vec![160, 48, 136, 0, 0, 0, 66, 201])); 967 expectations.push(SpiTransaction::write_vec(vec![160, 48, 136, 0, 0, 0, 66, 201]));
@@ -1195,23 +969,23 @@ mod tests {
1195 969
1196 // Write TX reg. 970 // Write TX reg.
1197 // Header 971 // Header
1198 spi_packet.extend_from_slice(&[160, 49, 143, 0, 0]).unwrap(); 972 expectations.push(SpiTransaction::write_vec(vec![160, 49, 143, 0, 0]));
1199 // Packet data 973 // Packet data
1200 spi_packet.extend_from_slice(&packet).unwrap(); 974 let packet = [
1201 // Packet padding up to 60 (ETH_MIN_LEN - FCS) 975 34, 51, 68, 85, 102, 119, 18, 52, 86, 120, 154, 188, 8, 6, 0, 1, 8, 0, 6, 4, 0, 2, 18, 52, 86, 120, 154,
1202 for _ in packet.len()..(ETH_MIN_LEN - FSC_LEN) { 976 188, 192, 168, 16, 4, 34, 51, 68, 85, 102, 119, 192, 168, 16, 1,
1203 spi_packet.push(0x00).unwrap(); 977 ];
1204 } 978 expectations.push(SpiTransaction::write_vec(packet.to_vec()));
1205 // Packet FCS
1206 spi_packet.extend_from_slice(&[147, 149, 213, 68]).unwrap();
1207 979
1208 // SPI HEADER Padding of u32 980 let mut tail = std::vec::Vec::<u8>::with_capacity(100);
1209 let spi_packet_len = spi_packet.len(); 981 // Padding
1210 for _ in spi_packet_len..spi_packet_len.next_multiple_of(4) { 982 if let Some(padding_len) = (ETH_MIN_LEN - FSC_LEN).checked_sub(packet.len()) {
1211 spi_packet.push(0x00).unwrap(); 983 tail.resize(padding_len, 0x00);
1212 } 984 }
985 // Packet FCS + optinal padding
986 tail.extend_from_slice(&[147, 149, 213, 68, DONT_CARE_BYTE, DONT_CARE_BYTE]);
1213 987
1214 expectations.push(SpiTransaction::write_vec(spi_packet.to_vec())); 988 expectations.push(SpiTransaction::write_vec(tail));
1215 expectations.push(SpiTransaction::flush()); 989 expectations.push(SpiTransaction::flush());
1216 990
1217 let mut spi = SpiMock::new(&expectations); 991 let mut spi = SpiMock::new(&expectations);
@@ -1226,4 +1000,47 @@ mod tests {
1226 1000
1227 spi.done(); 1001 spi.done();
1228 } 1002 }
1003
1004 #[futures_test::test]
1005 async fn write_packet_to_fifo_arp_46bytes_without_crc() {
1006 // Configure expectations
1007 let mut expectations = vec![];
1008
1009 // Write TX_SIZE reg
1010 expectations.push(SpiTransaction::write_vec(vec![160, 48, 0, 0, 0, 66]));
1011 expectations.push(SpiTransaction::flush());
1012
1013 // Write TX reg.
1014 // SPI Header + Frame Header
1015 expectations.push(SpiTransaction::write_vec(vec![160, 49, 0, 0]));
1016 // Packet data
1017 let packet = [
1018 34, 51, 68, 85, 102, 119, 18, 52, 86, 120, 154, 188, 8, 6, 0, 1, 8, 0, 6, 4, 0, 2, 18, 52, 86, 120, 154,
1019 188, 192, 168, 16, 4, 34, 51, 68, 85, 102, 119, 192, 168, 16, 1,
1020 ];
1021 expectations.push(SpiTransaction::write_vec(packet.to_vec()));
1022
1023 let mut tail = std::vec::Vec::<u8>::with_capacity(100);
1024 // Padding
1025 if let Some(padding_len) = (ETH_MIN_LEN - FSC_LEN).checked_sub(packet.len()) {
1026 tail.resize(padding_len, 0x00);
1027 }
1028 // Packet FCS + optinal padding
1029 tail.extend_from_slice(&[147, 149, 213, 68, DONT_CARE_BYTE, DONT_CARE_BYTE]);
1030
1031 expectations.push(SpiTransaction::write_vec(tail));
1032 expectations.push(SpiTransaction::flush());
1033
1034 let mut spi = SpiMock::new(&expectations);
1035
1036 let cs = CsPinMock::default();
1037 let delay = MockDelay {};
1038 let spi_dev = ExclusiveDevice::new(spi.clone(), cs, delay);
1039
1040 let mut spe = ADIN1110::new(spi_dev, false);
1041
1042 assert!(spe.write_fifo(&packet).await.is_ok());
1043
1044 spi.done();
1045 }
1229} 1046}