forked from ninjasource/w5500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1126 lines (991 loc) · 32.7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![no_std]
extern crate byteorder;
extern crate embedded_hal as hal;
use byteorder::BigEndian;
use byteorder::ByteOrder;
use hal::blocking::spi::Transfer;
use hal::digital::v2::OutputPin;
const COMMAND_READ: u8 = 0x00 << 2;
const COMMAND_WRITE: u8 = 0x01 << 2;
const VARIABLE_DATA_LENGTH: u8 = 0b_00;
#[derive(Debug)]
pub enum Error<SpiError, PinError> {
/// SPI communication error
Spi(SpiError),
/// CS output pin error
Pin(PinError),
}
impl<SpiError, PinError> From<SpiError> for Error<SpiError, PinError> {
fn from(err: SpiError) -> Self {
Self::Spi(err)
}
}
#[derive(Copy, Clone, PartialOrd, PartialEq, Default, Debug)]
pub struct IpAddress {
pub address: [u8; 4],
}
impl IpAddress {
pub fn new(a0: u8, a1: u8, a2: u8, a3: u8) -> IpAddress {
IpAddress {
address: [a0, a1, a2, a3],
}
}
}
impl ::core::fmt::Display for IpAddress {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(
f,
"{}.{}.{}.{}",
self.address[0], self.address[1], self.address[2], self.address[3],
)
}
}
#[derive(Copy, Clone, PartialOrd, PartialEq, Default, Debug)]
pub struct MacAddress {
pub address: [u8; 6],
}
impl MacAddress {
pub fn new(a0: u8, a1: u8, a2: u8, a3: u8, a4: u8, a5: u8) -> MacAddress {
MacAddress {
address: [a0, a1, a2, a3, a4, a5],
}
}
}
impl ::core::fmt::Display for MacAddress {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.address[0],
self.address[1],
self.address[2],
self.address[3],
self.address[4],
self.address[5],
)
}
}
pub struct W5500<CS> {
cs: CS,
}
pub enum BufferSize {
Size0KB,
Size1KB,
Size2KBDefault,
Size4KB,
Size8KB,
Size16KB,
}
impl<CS, PinError> W5500<CS>
where
CS: OutputPin<Error = PinError>,
{
pub fn new(cs: CS) -> Self {
W5500 { cs }
}
pub fn init<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
) -> Result<(), Error<E, PinError>> {
self.reset(spi)?;
self.set_mode(spi, false, false, false, false)?;
Ok(())
}
pub fn reset<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
) -> Result<(), Error<E, PinError>> {
self.write_to(
spi,
Register::CommonRegister(0x00_00_u16),
&[
0b1000_0000, // Mode Register (force reset)
],
)
}
pub fn set_mode<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
wol: bool,
ping_block: bool,
ppoe: bool,
force_arp: bool,
) -> Result<(), Error<E, PinError>> {
let mut mode = 0x00;
if wol {
mode |= 1 << 5;
}
if ping_block {
mode |= 1 << 4;
}
if ppoe {
mode |= 1 << 3;
}
if force_arp {
mode |= 1 << 1;
}
self.write_to(spi, Register::CommonRegister(0x00_00_u16), &[mode])
}
pub fn set_interrupt_mask<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
sockets: &[Socket],
) -> Result<(), Error<E, PinError>> {
let mut mask = 0u8;
for socket in sockets.iter() {
mask |= match *socket {
Socket::Socket0 => 1 << 0,
Socket::Socket1 => 1 << 1,
Socket::Socket2 => 1 << 2,
Socket::Socket3 => 1 << 3,
Socket::Socket4 => 1 << 4,
Socket::Socket5 => 1 << 5,
Socket::Socket6 => 1 << 6,
Socket::Socket7 => 1 << 7,
};
}
self.write_to(spi, Register::CommonRegister(0x00_17_u16), &[mask])
}
pub fn set_socket_interrupt_mask<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
interrupts: &[Interrupt],
) -> Result<(), Error<E, PinError>> {
let mut mask = 0u8;
for interrupt in interrupts.iter() {
mask |= *interrupt as u8;
}
self.write_to(spi, socket.at(SocketRegister::InterruptMask), &[mask])
}
pub fn set_gateway<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
gateway: &IpAddress,
) -> Result<(), Error<E, PinError>> {
self.write_to(spi, Register::CommonRegister(0x00_01_u16), &gateway.address)
}
pub fn set_subnet<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
subnet: &IpAddress,
) -> Result<(), Error<E, PinError>> {
self.write_to(spi, Register::CommonRegister(0x00_05_u16), &subnet.address)
}
pub fn set_mac<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
mac: &MacAddress,
) -> Result<(), Error<E, PinError>> {
self.write_to(spi, Register::CommonRegister(0x00_09_u16), &mac.address)
}
pub fn get_mac<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
) -> Result<MacAddress, Error<E, PinError>> {
let mut mac = MacAddress::default();
self.read_from(spi, Register::CommonRegister(0x00_09_u16), &mut mac.address)?;
Ok(mac)
}
pub fn set_ip<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
ip: &IpAddress,
) -> Result<(), Error<E, PinError>> {
self.write_to(spi, Register::CommonRegister(0x00_0F_u16), &ip.address)
}
pub fn is_interrupt_set<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
interrupt: Interrupt,
) -> Result<bool, Error<E, PinError>> {
let mut state = [0u8; 1];
self.read_from(spi, socket.at(SocketRegister::Interrupt), &mut state)?;
Ok(state[0] & interrupt as u8 != 0)
}
pub fn reset_interrupt<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
interrupt: Interrupt,
) -> Result<(), Error<E, PinError>> {
self.write_to(
spi,
socket.at(SocketRegister::Interrupt),
&[interrupt as u8],
)
}
pub fn close<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
) -> Result<(), Error<E, PinError>> {
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Close as u8,
)
}
pub fn dissconnect<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
) -> Result<(), Error<E, PinError>> {
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Disconnect as u8,
)
}
pub fn get_socket_status<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
) -> Result<Option<SocketStatus>, Error<E, PinError>> {
let status = self.read_u8(spi, socket.at(SocketRegister::Status))?;
Ok(match status {
status if status == SocketStatus::Closed as u8 => Some(SocketStatus::Closed),
status if status == SocketStatus::Init as u8 => Some(SocketStatus::Init),
status if status == SocketStatus::Listen as u8 => Some(SocketStatus::Listen),
status if status == SocketStatus::Established as u8 => Some(SocketStatus::Established),
status if status == SocketStatus::CloseWait as u8 => Some(SocketStatus::CloseWait),
status if status == SocketStatus::Udp as u8 => Some(SocketStatus::Udp),
status if status == SocketStatus::MacRaw as u8 => Some(SocketStatus::MacRaw),
status if status == SocketStatus::SynSent as u8 => Some(SocketStatus::SynSent),
status if status == SocketStatus::SynRecv as u8 => Some(SocketStatus::SynRecv),
status if status == SocketStatus::FinWait as u8 => Some(SocketStatus::FinWait),
status if status == SocketStatus::Closing as u8 => Some(SocketStatus::Closing),
status if status == SocketStatus::TimeWait as u8 => Some(SocketStatus::TimeWait),
status if status == SocketStatus::LastAck as u8 => Some(SocketStatus::LastAck),
_ => None,
})
}
// See pages 46 and 49
pub fn read_registers<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
) -> Result<(u8, u8, u8, u8, u16, u8), Error<E, PinError>> {
let mode = self.read_u8(spi, socket.at(SocketRegister::Mode))?;
let command = self.read_u8(spi, socket.at(SocketRegister::Command))?;
let interrupt = self.read_u8(spi, socket.at(SocketRegister::Interrupt))?;
let status = self.read_u8(spi, socket.at(SocketRegister::Status))?;
let port = self.read_u16(spi, socket.at(SocketRegister::LocalPort))?;
let interrupt_mask = self.read_u8(spi, socket.at(SocketRegister::InterruptMask))?;
Ok((mode, command, status, interrupt, port, interrupt_mask))
}
pub fn send_udp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
local_port: u16,
host: &IpAddress,
host_port: u16,
data: &[u8],
) -> Result<(), Error<E, PinError>> {
// TODO not always socket 0
// TODO check if in use
self.write_to(
spi,
socket.at(SocketRegister::Mode),
&[
Protocol::UDP as u8, // Socket Mode Register
SocketCommand::Open as u8, // Socket Command Regsiter
],
)?;
{
let local_port = u16_to_be_bytes(local_port);
let host_port = u16_to_be_bytes(host_port);
self.write_to(
spi,
socket.at(SocketRegister::LocalPort),
&[
local_port[0],
local_port[1], // local port u16
0x00,
0x00,
0x00,
0x00,
0x00,
0x00, // destination mac
host.address[0],
host.address[1],
host.address[2],
host.address[3], // target IP
host_port[0],
host_port[1], // destination port (5354)
],
)?;
}
let data_length = data.len() as u16;
{
let data_length = u16_to_be_bytes(data_length);
self.write_to(
spi,
socket.at(SocketRegister::TxReadPointer),
&[0x00, 0x00, data_length[0], data_length[1]],
)?;
}
self.write_to(
spi,
socket.tx_register_at(0x00_00),
&data[..data_length as usize],
)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Send as u8,
)
}
pub fn listen_udp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
port: u16,
) -> Result<(), Error<E, PinError>> {
self.write_u16(spi, socket.at(SocketRegister::LocalPort), port)?;
self.write_u8(spi, socket.at(SocketRegister::Mode), Protocol::UDP as u8)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Open as u8,
)
}
pub fn set_protocol<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
protocol: Protocol,
) -> Result<(), Error<E, PinError>> {
self.write_u8(spi, socket.at(SocketRegister::Mode), protocol as u8)?;
Ok(())
}
pub fn connect<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
host_ip: &IpAddress,
host_port: u16,
) -> Result<(), Error<E, PinError>> {
self.write_to(
spi,
socket.at(SocketRegister::DestinationIp),
&[
host_ip.address[0],
host_ip.address[1],
host_ip.address[2],
host_ip.address[3],
],
)?;
self.write_u16(spi, socket.at(SocketRegister::DestinationPort), host_port)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Connect as u8,
)?;
Ok(())
}
pub fn open_tcp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
) -> Result<(), Error<E, PinError>> {
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Open as u8,
)?;
loop {
let status = self.read_u8(spi, socket.at(SocketRegister::Status))?;
if status == SocketStatus::Init as u8 {
return Ok(());
}
}
}
pub fn send_tcp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
data: &[u8],
) -> Result<usize, Error<E, PinError>> {
let mut data_length = data.len() as u16;
if data_length == 0 {
return Ok(0);
}
let max_length = self.read_u16(spi, socket.at(SocketRegister::TransmitBuffer))?;
data_length = if data_length < max_length {
data_length
} else {
max_length
};
loop {
let tx_free_size = self.read_u16_atomic(spi, socket.at(SocketRegister::TxFreeSize))?;
if data_length < tx_free_size {
break;
}
}
// get the write pointer and write data at that address
let ptr = self.read_u16_atomic(spi, socket.at(SocketRegister::TxWritePointer))?;
self.write_to(
spi,
socket.tx_register_at(ptr),
&data[..data_length as usize],
)?;
// update the write pointer
self.write_u16(
spi,
socket.at(SocketRegister::TxWritePointer),
ptr + data_length,
)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Send as u8,
)?;
// wait for bytes to be sent and clear the interrupt
loop {
let interrupt = self.read_u8(spi, socket.at(SocketRegister::Interrupt))?;
if interrupt & 0b0001_0000 == 0b0001_0000 {
self.write_u8(spi, socket.at(SocketRegister::Interrupt), 0b0001_0000)?;
break;
}
}
Ok(data_length as usize)
}
pub fn listen_tcp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
port: u16,
) -> Result<(), Error<E, PinError>> {
self.write_u16(spi, socket.at(SocketRegister::LocalPort), port)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Listen as u8,
)?;
loop {
let status = self.read_u8(spi, socket.at(SocketRegister::Status))?;
if status == SocketStatus::Listen as u8 {
return Ok(());
}
}
}
pub fn set_rx_buffer_size<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
buffer_size: BufferSize,
) -> Result<(), Error<E, PinError>> {
let value = match buffer_size {
BufferSize::Size0KB => 0,
BufferSize::Size1KB => 1,
BufferSize::Size2KBDefault => 2,
BufferSize::Size4KB => 4,
BufferSize::Size8KB => 8,
BufferSize::Size16KB => 16,
};
self.write_u8(spi, socket.at(SocketRegister::ReceiveBuffer), value)?;
Ok(())
}
pub fn set_tx_buffer_size<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
buffer_size: BufferSize,
) -> Result<(), Error<E, PinError>> {
let value = match buffer_size {
BufferSize::Size0KB => 0,
BufferSize::Size1KB => 1,
BufferSize::Size2KBDefault => 2,
BufferSize::Size4KB => 4,
BufferSize::Size8KB => 8,
BufferSize::Size16KB => 16,
};
self.write_u8(spi, socket.at(SocketRegister::TransmitBuffer), value)?;
Ok(())
}
pub fn send_keep_alive_tcp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
port: u16,
) -> Result<(), Error<E, PinError>> {
self.write_u16(spi, socket.at(SocketRegister::LocalPort), port)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Listen as u8,
)?;
loop {
let status = self.read_u8(spi, socket.at(SocketRegister::Status))?;
if status == SocketStatus::Listen as u8 {
return Ok(());
}
}
}
pub fn try_receive_tcp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
destination: &mut [u8],
) -> Result<Option<usize>, Error<E, PinError>> {
if self.read_u8(spi, socket.at(SocketRegister::InterruptMask))? & 0x04 == 0 {
return Ok(None);
}
let receive_size = loop {
let s0 = self.read_u16(spi, socket.at(SocketRegister::RxReceivedSize))?;
let s1 = self.read_u16(spi, socket.at(SocketRegister::RxReceivedSize))?;
if s0 == s1 {
break s0 as usize;
}
};
let buffer_len = destination.len();
if receive_size == 0 || buffer_len == 0 {
return Ok(None);
}
let size = if receive_size > buffer_len {
buffer_len
} else {
receive_size
};
let read_pointer = self.read_u16(spi, socket.at(SocketRegister::RxReadPointer))?;
self.read_from(
spi,
socket.rx_register_at(read_pointer),
&mut destination[..size],
)?;
// reset
self.write_u16(
spi,
socket.at(SocketRegister::RxReadPointer),
read_pointer + size as u16,
)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Recv as u8,
)?;
Ok(Some(size))
}
/// TODO destination buffer has to be as large as the receive buffer or complete read is not guaranteed
pub fn try_receive_udp<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
socket: Socket,
destination: &mut [u8],
) -> Result<Option<(IpAddress, u16, usize)>, Error<E, PinError>> {
if self.read_u8(spi, socket.at(SocketRegister::InterruptMask))? & 0x04 == 0 {
return Ok(None);
}
let receive_size = loop {
let s0 = self.read_u16(spi, socket.at(SocketRegister::RxReceivedSize))?;
let s1 = self.read_u16(spi, socket.at(SocketRegister::RxReceivedSize))?;
if s0 == s1 {
break s0 as usize;
}
};
if receive_size >= 8 {
let read_pointer = self.read_u16(spi, socket.at(SocketRegister::RxReadPointer))?;
// |<-- read_pointer read_pointer + received_size -->|
// |Destination IP Address | Destination Port | Byte Size of DATA | Actual DATA ... |
// | --- 4 Bytes --- | --- 2 Bytes --- | --- 2 Bytes --- | .... |
let ip = self.read_ip(spi, socket.rx_register_at(read_pointer))?;
let port = self.read_u16(spi, socket.rx_register_at(read_pointer + 4))?;
let data_length = destination
.len()
.min(self.read_u16(spi, socket.rx_register_at(read_pointer + 6))? as usize);
self.read_from(
spi,
socket.rx_register_at(read_pointer + 8),
&mut destination[..data_length],
)?;
// reset
self.write_u16(
spi,
socket.at(SocketRegister::RxReadPointer),
read_pointer + receive_size as u16,
)?;
self.write_u8(
spi,
socket.at(SocketRegister::Command),
SocketCommand::Recv as u8,
)?;
Ok(Some((ip, port, data_length)))
} else {
Ok(None)
}
}
pub fn read_u8<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
) -> Result<u8, Error<E, PinError>> {
let mut buffer = [0u8; 1];
self.read_from(spi, register, &mut buffer)?;
Ok(buffer[0])
}
pub fn read_u16<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
) -> Result<u16, Error<E, PinError>> {
let mut buffer = [0u8; 2];
self.read_from(spi, register, &mut buffer)?;
Ok(BigEndian::read_u16(&buffer))
}
fn read_u16_atomic<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
) -> Result<u16, Error<E, PinError>> {
loop {
let s0 = self.read_u16(spi, register)?;
let s1 = self.read_u16(spi, register)?;
if s0 == s1 {
return Ok(s0);
}
}
}
pub fn read_ip<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
) -> Result<IpAddress, Error<E, PinError>> {
let mut ip = IpAddress::default();
self.read_from(spi, register, &mut ip.address)?;
Ok(ip)
}
pub fn read_from<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
target: &mut [u8],
) -> Result<(), Error<E, PinError>> {
self.chip_select().map_err(Error::Pin)?;
let mut request = [
0_u8,
0_u8,
register.control_byte() | COMMAND_READ | VARIABLE_DATA_LENGTH,
];
BigEndian::write_u16(&mut request[..2], register.address());
let result = self
.write_bytes(spi, &request)
.and_then(|_| self.read_bytes(spi, target));
self.chip_deselect().map_err(Error::Pin)?;
result
}
pub fn write_u8<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
value: u8,
) -> Result<(), Error<E, PinError>> {
self.write_to(spi, register, &[value])
}
pub fn write_u16<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
value: u16,
) -> Result<(), Error<E, PinError>> {
let mut data = [0u8; 2];
BigEndian::write_u16(&mut data, value);
self.write_to(spi, register, &data)
}
pub fn write_to<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
register: Register,
data: &[u8],
) -> Result<(), Error<E, PinError>> {
self.chip_select().map_err(Error::Pin)?;
let mut request = [
0_u8,
0_u8,
register.control_byte() | COMMAND_WRITE | VARIABLE_DATA_LENGTH,
];
BigEndian::write_u16(&mut request[..2], register.address());
let result = self
.write_bytes(spi, &request)
.and_then(|_| self.write_bytes(spi, data));
self.chip_deselect().map_err(Error::Pin)?;
result
}
fn read_bytes<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
bytes: &mut [u8],
) -> Result<(), Error<E, PinError>> {
for i in 0..bytes.len() {
bytes[i] = self.read(spi)?;
}
Ok(())
}
fn read<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
) -> Result<u8, Error<E, PinError>> {
let command = &mut [0x00];
let result = spi.transfer(command)?;
Ok(result[0])
}
fn write_bytes<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
bytes: &[u8],
) -> Result<(), Error<E, PinError>> {
for b in bytes {
self.write(spi, *b)?;
}
Ok(())
}
fn write<E>(
&mut self,
spi: &mut impl Transfer<u8, Error = E>,
byte: u8,
) -> Result<(), Error<E, PinError>> {
spi.transfer(&mut [byte])?;
Ok(())
}
fn chip_select(&mut self) -> Result<(), PinError> {
self.cs.set_low()
}
fn chip_deselect(&mut self) -> Result<(), PinError> {
self.cs.set_high()
}
}
fn u16_to_be_bytes(u16: u16) -> [u8; 2] {
let mut bytes = [0u8; 2];
BigEndian::write_u16(&mut bytes, u16);
bytes
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SocketStatus {
Closed = 0x0000,
Init = 0x0013,
Listen = 0x0014,
Established = 0x0017,
CloseWait = 0x001C,
Udp = 0x0022,
MacRaw = 0x0042,
// the statuses below are temporary status indicated during changing the status of Socket n
SynSent = 0x0015,
SynRecv = 0x0016,
FinWait = 0x0018,
Closing = 0x001A,
TimeWait = 0x001B,
LastAck = 0x001D,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SocketRegister {
Mode = 0x0000,
Command = 0x0001,
Interrupt = 0x0002,
Status = 0x0003,
LocalPort = 0x0004,
DestinationMac = 0x0006,
DestinationIp = 0x000C,
DestinationPort = 0x0010,
MaxSegmentSize = 0x0012,
// Reserved 0x0014
TypeOfService = 0x0015,
TimeToLive = 0x0016,
// Reserved 0x0017 - 0x001D
ReceiveBuffer = 0x001E,
TransmitBuffer = 0x001F,
TxFreeSize = 0x0020,
TxReadPointer = 0x0022,
TxWritePointer = 0x0024,
RxReceivedSize = 0x0026,
RxReadPointer = 0x0028,
RxWritePointer = 0x002A,
InterruptMask = 0x002C,
FragmentOffset = 0x002D,
KeepAliveTimer = 0x002F,
// Reserved 0x0030 - 0xFFFF
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Interrupt {
SendOk = 1 << 4,
Timeout = 1 << 3,
Received = 1 << 2,
Disconnected = 1 << 1,
Connected = 1 << 0,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Protocol {
TCP = 0b0001,
UDP = 0b0010,
MACRAW = 0b0100,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SocketCommand {
Open = 0x01,
Listen = 0x02,
Connect = 0x04,
Disconnect = 0x08,
Close = 0x10,
Send = 0x20,
SendMac = 0x21,
SendKeep = 0x22,
Recv = 0x40,
}
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub enum Socket {
Socket0,
Socket1,
Socket2,
Socket3,
Socket4,
Socket5,
Socket6,
Socket7,
}
impl Socket {
pub fn number(&self) -> usize {
match *self {
Socket::Socket0 => 0,
Socket::Socket1 => 1,
Socket::Socket2 => 2,
Socket::Socket3 => 3,
Socket::Socket4 => 4,
Socket::Socket5 => 5,
Socket::Socket6 => 6,
Socket::Socket7 => 7,
}
}
fn tx_register_at(&self, address: u16) -> Register {
match *self {
Socket::Socket0 => Register::Socket0TxBuffer(address),
Socket::Socket1 => Register::Socket1TxBuffer(address),
Socket::Socket2 => Register::Socket2TxBuffer(address),
Socket::Socket3 => Register::Socket3TxBuffer(address),
Socket::Socket4 => Register::Socket4TxBuffer(address),
Socket::Socket5 => Register::Socket5TxBuffer(address),
Socket::Socket6 => Register::Socket6TxBuffer(address),
Socket::Socket7 => Register::Socket7TxBuffer(address),
}
}
fn rx_register_at(&self, address: u16) -> Register {
match *self {
Socket::Socket0 => Register::Socket0RxBuffer(address),
Socket::Socket1 => Register::Socket1RxBuffer(address),
Socket::Socket2 => Register::Socket2RxBuffer(address),
Socket::Socket3 => Register::Socket3RxBuffer(address),
Socket::Socket4 => Register::Socket4RxBuffer(address),
Socket::Socket5 => Register::Socket5RxBuffer(address),
Socket::Socket6 => Register::Socket6RxBuffer(address),
Socket::Socket7 => Register::Socket7RxBuffer(address),
}
}
fn register_at(&self, address: u16) -> Register {
match *self {
Socket::Socket0 => Register::Socket0Register(address),
Socket::Socket1 => Register::Socket1Register(address),
Socket::Socket2 => Register::Socket2Register(address),
Socket::Socket3 => Register::Socket3Register(address),