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
/// W5500 common register addresses.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u16)]
#[allow(non_camel_case_types)]
pub enum Reg {
/// Address of the MR register.
MR = 0x0000,
/// Address of the GAR register, index 0.
GAR0 = 0x0001,
/// Address of the GAR register, index 1.
GAR1 = 0x0002,
/// Address of the GAR register, index 2.
GAR2 = 0x0003,
/// Address of the GAR register, index 3.
GAR3 = 0x0004,
/// Address of the SUBR register, index 0.
SUBR0 = 0x0005,
/// Address of the SUBR register, index 1.
SUBR1 = 0x0006,
/// Address of the SUBR register, index 2.
SUBR2 = 0x0007,
/// Address of the SUBR register, index 3.
SUBR3 = 0x0008,
/// Address of the SHAR register, index 0.
SHAR0 = 0x0009,
/// Address of the SHAR register, index 1.
SHAR1 = 0x000A,
/// Address of the SHAR register, index 2.
SHAR2 = 0x000B,
/// Address of the SHAR register, index 3.
SHAR3 = 0x000C,
/// Address of the SHAR register, index 4.
SHAR4 = 0x000D,
/// Address of the SHAR register, index 5.
SHAR5 = 0x000E,
/// Address of the SIPR register, index 0.
SIPR0 = 0x000F,
/// Address of the SIPR register, index 1.
SIPR1 = 0x0010,
/// Address of the SIPR register, index 2.
SIPR2 = 0x0011,
/// Address of the SIPR register, index 3.
SIPR3 = 0x0012,
/// Address of the INTLEVEL register, index 0.
INTLEVEL0 = 0x0013,
/// Address of the INTLEVEL register, index 1.
INTLEVEL1 = 0x0014,
/// Address of the IR register.
IR = 0x0015,
/// Address of the IMR register.
IMR = 0x0016,
/// Address of the SIR register.
SIR = 0x0017,
/// Address of the SIMR register.
SIMR = 0x0018,
/// Address of the RTR register, index 0.
RTR0 = 0x0019,
/// Address of the RTR register, index 1.
RTR1 = 0x001A,
/// Address of the RCR register.
RCR = 0x001B,
/// Address of the PTIMER register.
PTIMER = 0x001C,
/// Address of the PMAGIC register.
PMAGIC = 0x001D,
/// Address of the PHAR register, index 0.
PHAR0 = 0x001E,
/// Address of the PHAR register, index 1.
PHAR1 = 0x001F,
/// Address of the PHAR register, index 2.
PHAR2 = 0x0020,
/// Address of the PHAR register, index 3.
PHAR3 = 0x0021,
/// Address of the PHAR register, index 4.
PHAR4 = 0x0022,
/// Address of the PHAR register, index 5.
PHAR5 = 0x0023,
/// Address of the PSID register, index 0.
PSID0 = 0x0024,
/// Address of the PSID register, index 1.
PSID1 = 0x0025,
/// Address of the PMRU register, index 0.
PMRU0 = 0x0026,
/// Address of the PMRU register, index 1.
PMRU1 = 0x0027,
/// Address of the UIPR register, index 0.
UIPR0 = 0x0028,
/// Address of the UIPR register, index 1.
UIPR1 = 0x0029,
/// Address of the UIPR register, index 2.
UIPR2 = 0x002A,
/// Address of the UIPR register, index 3.
UIPR3 = 0x002B,
/// Address of the UPORTR register, index 0.
UPORTR0 = 0x002C,
/// Address of the UPORTR register, index 1.
UPORTR1 = 0x002D,
/// Address of the PHYCFGR register.
PHYCFGR = 0x002E,
/// Address of the VERSIONR register.
VERSIONR = 0x0039,
}
impl From<Reg> for u16 {
fn from(reg: Reg) -> Self {
reg as u16
}
}
impl TryFrom<u16> for Reg {
type Error = u16;
fn try_from(val: u16) -> Result<Self, Self::Error> {
match val {
x if x == Self::MR as u16 => Ok(Self::MR),
x if x == Self::GAR0 as u16 => Ok(Self::GAR0),
x if x == Self::GAR1 as u16 => Ok(Self::GAR1),
x if x == Self::GAR2 as u16 => Ok(Self::GAR2),
x if x == Self::GAR3 as u16 => Ok(Self::GAR3),
x if x == Self::SUBR0 as u16 => Ok(Self::SUBR0),
x if x == Self::SUBR1 as u16 => Ok(Self::SUBR1),
x if x == Self::SUBR2 as u16 => Ok(Self::SUBR2),
x if x == Self::SUBR3 as u16 => Ok(Self::SUBR3),
x if x == Self::SHAR0 as u16 => Ok(Self::SHAR0),
x if x == Self::SHAR1 as u16 => Ok(Self::SHAR1),
x if x == Self::SHAR2 as u16 => Ok(Self::SHAR2),
x if x == Self::SHAR3 as u16 => Ok(Self::SHAR3),
x if x == Self::SHAR4 as u16 => Ok(Self::SHAR4),
x if x == Self::SHAR5 as u16 => Ok(Self::SHAR5),
x if x == Self::SIPR0 as u16 => Ok(Self::SIPR0),
x if x == Self::SIPR1 as u16 => Ok(Self::SIPR1),
x if x == Self::SIPR2 as u16 => Ok(Self::SIPR2),
x if x == Self::SIPR3 as u16 => Ok(Self::SIPR3),
x if x == Self::INTLEVEL0 as u16 => Ok(Self::INTLEVEL0),
x if x == Self::INTLEVEL1 as u16 => Ok(Self::INTLEVEL1),
x if x == Self::IR as u16 => Ok(Self::IR),
x if x == Self::IMR as u16 => Ok(Self::IMR),
x if x == Self::SIR as u16 => Ok(Self::SIR),
x if x == Self::SIMR as u16 => Ok(Self::SIMR),
x if x == Self::RTR0 as u16 => Ok(Self::RTR0),
x if x == Self::RTR1 as u16 => Ok(Self::RTR1),
x if x == Self::RCR as u16 => Ok(Self::RCR),
x if x == Self::PTIMER as u16 => Ok(Self::PTIMER),
x if x == Self::PMAGIC as u16 => Ok(Self::PMAGIC),
x if x == Self::PHAR0 as u16 => Ok(Self::PHAR0),
x if x == Self::PHAR1 as u16 => Ok(Self::PHAR1),
x if x == Self::PHAR2 as u16 => Ok(Self::PHAR2),
x if x == Self::PHAR3 as u16 => Ok(Self::PHAR3),
x if x == Self::PHAR4 as u16 => Ok(Self::PHAR4),
x if x == Self::PHAR5 as u16 => Ok(Self::PHAR5),
x if x == Self::PSID0 as u16 => Ok(Self::PSID0),
x if x == Self::PSID1 as u16 => Ok(Self::PSID1),
x if x == Self::PMRU0 as u16 => Ok(Self::PMRU0),
x if x == Self::PMRU1 as u16 => Ok(Self::PMRU1),
x if x == Self::UIPR0 as u16 => Ok(Self::UIPR0),
x if x == Self::UPORTR0 as u16 => Ok(Self::UPORTR0),
x if x == Self::UPORTR1 as u16 => Ok(Self::UPORTR1),
x if x == Self::PHYCFGR as u16 => Ok(Self::PHYCFGR),
x if x == Self::VERSIONR as u16 => Ok(Self::VERSIONR),
_ => Err(val),
}
}
}
impl Reg {
/// Get the address of the register.
///
/// # Example
///
/// ```
/// use w5500_ll::Reg;
///
/// assert_eq!(Reg::VERSIONR.addr(), 0x0039);
/// ```
pub const fn addr(self) -> u16 {
self as u16
}
/// Returns `true` if the register is read-only.
///
/// # Example
///
/// ```
/// use w5500_ll::Reg;
///
/// assert!(Reg::VERSIONR.is_ro());
/// assert!(!Reg::MR.is_ro());
/// ```
pub const fn is_ro(self) -> bool {
matches!(
self,
Reg::UIPR0
| Reg::UIPR1
| Reg::UIPR2
| Reg::UIPR3
| Reg::UPORTR0
| Reg::UPORTR1
| Reg::VERSIONR
)
}
}
/// W5500 socket register addresses.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u16)]
#[allow(non_camel_case_types)]
pub enum SnReg {
/// Address of the SN_MR register.
MR = 0x0000,
/// Address of the SN_CR register.
CR = 0x0001,
/// Address of the SN_IR register.
IR = 0x0002,
/// Address of the SN_SR register.
SR = 0x0003,
/// Address of the SN_PORT register, index 0.
PORT0 = 0x0004,
/// Address of the SN_PORT register, index 1.
PORT1 = 0x0005,
/// Address of the SN_DHAR register, index 0.
DHAR0 = 0x0006,
/// Address of the SN_DHAR register, index 1.
DHAR1 = 0x0007,
/// Address of the SN_DHAR register, index 2.
DHAR2 = 0x0008,
/// Address of the SN_DHAR register, index 3.
DHAR3 = 0x0009,
/// Address of the SN_DHAR register, index 4.
DHAR4 = 0x000A,
/// Address of the SN_DHAR register, index 5.
DHAR5 = 0x000B,
/// Address of the SN_DIPR register, index 0.
DIPR0 = 0x000C,
/// Address of the SN_DIPR register, index 1.
DIPR1 = 0x000D,
/// Address of the SN_DIPR register, index 2.
DIPR2 = 0x000E,
/// Address of the SN_DIPR register, index 3.
DIPR3 = 0x000F,
/// Address of the SN_DPORT register, index 0.
DPORT0 = 0x0010,
/// Address of the SN_DPORT register, index 1.
DPORT1 = 0x0011,
/// Address of the SN_MSSR register, index 0.
MSSR0 = 0x0012,
/// Address of the SN_MSSR register, index 1.
MSSR1 = 0x0013,
/// Address of the SN_TOS register.
TOS = 0x0015,
/// Address of the SN_TTL register.
TTL = 0x0016,
/// Address of the SN_RXBUF_SIZE register.
RXBUF_SIZE = 0x001E,
/// Address of the SN_TXBUF_SIZE register.
TXBUF_SIZE = 0x001F,
/// Address of the SN_TX_FSR register, index 0.
TX_FSR0 = 0x0020,
/// Address of the SN_TX_FSR register, index 1.
TX_FSR1 = 0x0021,
/// Address of the SN_TX_RD register, index 0.
TX_RD0 = 0x0022,
/// Address of the SN_TX_RD register, index 1.
TX_RD1 = 0x0023,
/// Address of the SN_TX_WR register, index 0.
TX_WR0 = 0x0024,
/// Address of the SN_TX_WR register, index 1.
TX_WR1 = 0x0025,
/// Address of the SN_RX_RSR register, index 0.
RX_RSR0 = 0x0026,
/// Address of the SN_RX_RSR register, index 1.
RX_RSR1 = 0x0027,
/// Address of the SN_RX_RD register, index 0.
RX_RD0 = 0x0028,
/// Address of the SN_RX_RD register, index 1.
RX_RD1 = 0x0029,
/// Address of the SN_RX_WR register, index 0.
RX_WR0 = 0x002A,
/// Address of the SN_RX_WR register, index 1.
RX_WR1 = 0x002B,
/// Address of the SN_IMR register.
IMR = 0x002C,
/// Address of the SN_FRAG register, index 0.
FRAG0 = 0x002D,
/// Address of the SN_FRAG register, index 1.
FRAG1 = 0x002E,
/// Address of the SN_KPALVTR register.
KPALVTR = 0x002F,
}
impl From<SnReg> for u16 {
fn from(snreg: SnReg) -> Self {
snreg as u16
}
}
impl TryFrom<u16> for SnReg {
type Error = u16;
fn try_from(val: u16) -> Result<Self, Self::Error> {
match val {
x if x == Self::MR as u16 => Ok(Self::MR),
x if x == Self::CR as u16 => Ok(Self::CR),
x if x == Self::IR as u16 => Ok(Self::IR),
x if x == Self::SR as u16 => Ok(Self::SR),
x if x == Self::PORT0 as u16 => Ok(Self::PORT0),
x if x == Self::PORT1 as u16 => Ok(Self::PORT1),
x if x == Self::DHAR0 as u16 => Ok(Self::DHAR0),
x if x == Self::DHAR1 as u16 => Ok(Self::DHAR1),
x if x == Self::DHAR2 as u16 => Ok(Self::DHAR2),
x if x == Self::DHAR3 as u16 => Ok(Self::DHAR3),
x if x == Self::DHAR4 as u16 => Ok(Self::DHAR4),
x if x == Self::DHAR5 as u16 => Ok(Self::DHAR5),
x if x == Self::DIPR0 as u16 => Ok(Self::DIPR0),
x if x == Self::DIPR1 as u16 => Ok(Self::DIPR1),
x if x == Self::DIPR2 as u16 => Ok(Self::DIPR2),
x if x == Self::DIPR3 as u16 => Ok(Self::DIPR3),
x if x == Self::DPORT0 as u16 => Ok(Self::DPORT0),
x if x == Self::DPORT1 as u16 => Ok(Self::DPORT1),
x if x == Self::MSSR0 as u16 => Ok(Self::MSSR0),
x if x == Self::MSSR1 as u16 => Ok(Self::MSSR1),
x if x == Self::TOS as u16 => Ok(Self::TOS),
x if x == Self::TTL as u16 => Ok(Self::TTL),
x if x == Self::RXBUF_SIZE as u16 => Ok(Self::RXBUF_SIZE),
x if x == Self::TXBUF_SIZE as u16 => Ok(Self::TXBUF_SIZE),
x if x == Self::TX_FSR0 as u16 => Ok(Self::TX_FSR0),
x if x == Self::TX_FSR1 as u16 => Ok(Self::TX_FSR1),
x if x == Self::TX_RD0 as u16 => Ok(Self::TX_RD0),
x if x == Self::TX_RD1 as u16 => Ok(Self::TX_RD1),
x if x == Self::TX_WR0 as u16 => Ok(Self::TX_WR0),
x if x == Self::TX_WR1 as u16 => Ok(Self::TX_WR1),
x if x == Self::RX_RSR0 as u16 => Ok(Self::RX_RSR0),
x if x == Self::RX_RSR1 as u16 => Ok(Self::RX_RSR1),
x if x == Self::RX_RD0 as u16 => Ok(Self::RX_RD0),
x if x == Self::RX_RD1 as u16 => Ok(Self::RX_RD1),
x if x == Self::RX_WR0 as u16 => Ok(Self::RX_WR0),
x if x == Self::RX_WR1 as u16 => Ok(Self::RX_WR1),
x if x == Self::IMR as u16 => Ok(Self::IMR),
x if x == Self::FRAG0 as u16 => Ok(Self::FRAG0),
x if x == Self::FRAG1 as u16 => Ok(Self::FRAG1),
x if x == Self::KPALVTR as u16 => Ok(Self::KPALVTR),
_ => Err(val),
}
}
}
impl SnReg {
/// Get the address of the socket register.
///
/// # Example
///
/// ```
/// use w5500_ll::SnReg;
///
/// assert_eq!(SnReg::PORT0.addr(), 0x0004);
/// ```
pub const fn addr(self) -> u16 {
self as u16
}
/// Returns `true` if the register is read-only.
///
/// # Example
///
/// ```
/// use w5500_ll::SnReg;
///
/// assert!(SnReg::SR.is_ro());
/// assert!(!SnReg::MR.is_ro());
/// ```
pub const fn is_ro(self) -> bool {
matches!(
self,
Self::SR
| Self::TX_FSR0
| Self::TX_FSR1
| Self::TX_RD0
| Self::TX_RD1
| Self::RX_RSR0
| Self::RX_RSR1
)
}
}