@@ -62,9 +62,9 @@ impl EphyReg {
62
62
}
63
63
}
64
64
65
- const NUM_TX_DESCRIPTORS : usize = 10 ;
65
+ const NUM_TX_DESCRIPTORS : usize = 1 ;
66
66
const NUM_RX_DESCRIPTORS : usize = 10 ;
67
- static RX_DESCRIPTORS : [ RDES ; NUM_TX_DESCRIPTORS ] = [
67
+ static RX_DESCRIPTORS : [ RDES ; NUM_RX_DESCRIPTORS ] = [
68
68
RDES :: new ( ) ,
69
69
RDES :: new ( ) ,
70
70
RDES :: new ( ) ,
@@ -76,22 +76,14 @@ static RX_DESCRIPTORS: [RDES; NUM_TX_DESCRIPTORS] = [
76
76
RDES :: new ( ) ,
77
77
RDES :: new ( ) ,
78
78
] ;
79
- static TX_DESCRIPTORS : [ TDES ; NUM_RX_DESCRIPTORS ] = [
80
- TDES :: new ( ) ,
81
- TDES :: new ( ) ,
82
- TDES :: new ( ) ,
83
- TDES :: new ( ) ,
84
- TDES :: new ( ) ,
85
- TDES :: new ( ) ,
86
- TDES :: new ( ) ,
87
- TDES :: new ( ) ,
88
- TDES :: new ( ) ,
89
- TDES :: new ( ) ,
90
- ] ;
79
+ static TX_DESCRIPTORS : [ TDES ; NUM_TX_DESCRIPTORS ] = [ TDES :: new ( ) ] ;
91
80
92
81
const RX_BUFFER_SIZE : usize = 1536 ;
93
82
static mut RX_BUFFERS : [ [ u8 ; RX_BUFFER_SIZE ] ; NUM_RX_DESCRIPTORS ] =
94
83
[ [ 0 ; RX_BUFFER_SIZE ] ; NUM_RX_DESCRIPTORS ] ;
84
+ const TX_BUFFER_SIZE : usize = RX_BUFFER_SIZE ;
85
+ static mut TX_BUFFERS : [ [ u8 ; TX_BUFFER_SIZE ] ; NUM_TX_DESCRIPTORS ] =
86
+ [ [ 0 ; TX_BUFFER_SIZE ] ; NUM_TX_DESCRIPTORS ] ;
95
87
96
88
fn emac_reset ( emac0 : & EMAC0 ) {
97
89
emac0. dmabusmod . modify ( |_, w| w. swr ( ) . set_bit ( ) ) ;
@@ -137,37 +129,24 @@ fn emac_phy_config_set(
137
129
138
130
fn emac_init (
139
131
emac0 : & EMAC0 ,
140
- _sysclk : u32 ,
132
+ sysclk : u32 ,
141
133
mut rx_burst : u32 ,
142
134
mut tx_burst : u32 ,
143
135
desc_skip_size : u32 ,
144
136
) {
145
- // uint32_t ui32Val, ui32Div;
146
-
147
- // //
148
- // // Parameter sanity checks.
149
- // //
150
- // ASSERT(ui32DescSkipSize < 32);
137
+ // Parameter sanity checks.
151
138
assert ! ( desc_skip_size < 32 ) ;
152
- // ASSERT(ui32TxBurst < (32 * 8));
153
139
assert ! ( tx_burst < 32 * 8 ) ;
154
- // ASSERT(ui32RxBurst < (32 * 8));
155
140
assert ! ( rx_burst < 32 * 8 ) ;
156
141
157
- // //
158
- // // Make sure that the DMA software reset is clear before continuing.
159
- // //
160
- // while(HWREG(EMAC0_BASE + EMAC_O_DMABUSMOD) & EMAC_DMABUSMOD_SWR)
161
- // {
162
- // }
142
+ // Make sure that the DMA software reset is clear before continuing.
163
143
while emac0. dmabusmod . read ( ) . swr ( ) . bit_is_set ( ) { }
164
144
165
145
emac0. dmabusmod . modify ( |_, w| {
166
146
// Set common flags. Note that this driver assumes we are always using 8 word
167
147
// descriptors so we need to OR in EMAC_DMABUSMOD_ATDS here.
168
148
169
149
// Do we need to use the 8X burst length multiplier?
170
-
171
150
if tx_burst > 32 || rx_burst > 32 {
172
151
// Divide both burst lengths by 8 and set the 8X burst length multiplier.
173
152
w. _8xpbl ( ) . set_bit ( ) ;
@@ -201,28 +180,23 @@ fn emac_init(
201
180
w
202
181
} ) ;
203
182
204
- // //
205
- // // Default the MII CSR clock divider based on the fastest system clock.
206
- // //
207
- // ui32Div = g_pi16MIIClockDiv[NUM_CLOCK_DIVISORS - 1].ui32Divisor;
208
-
209
- // //
210
- // // Find the MII CSR clock divider to use based on the current system clock.
211
- // //
212
- // for(ui32Val = 0; ui32Val < NUM_CLOCK_DIVISORS; ui32Val++)
213
- // {
214
- // if(ui32SysClk <= g_pi16MIIClockDiv[ui32Val].ui32SysClockMax)
215
- // {
216
- // ui32Div = g_pi16MIIClockDiv[ui32Val].ui32Divisor;
217
- // break;
218
- // }
219
- // }
220
-
221
- // //
222
- // // Set the MII CSR clock speed.
223
- // //
224
- // HWREG(ui32Base + EMAC_O_MIIADDR) = ((HWREG(ui32Base + EMAC_O_MIIADDR) &
225
- // ~EMAC_MIIADDR_CR_M) | ui32Div);
183
+ unsafe {
184
+ emac0. miiaddr . modify ( |_, w| {
185
+ w. cr ( ) . bits ( if sysclk < 20_000_000 {
186
+ panic ! ( )
187
+ } else if sysclk < 35_000_000 {
188
+ 0x8
189
+ } else if sysclk < 60_000_000 {
190
+ 0xc
191
+ } else if sysclk < 100_000_000 {
192
+ 0x0
193
+ } else if sysclk < 150_000_000 {
194
+ 0x4
195
+ } else {
196
+ panic ! ( )
197
+ } )
198
+ } ) ;
199
+ }
226
200
227
201
// Disable all the MMC interrupts as these are enabled by default at reset.
228
202
unsafe {
@@ -268,6 +242,7 @@ pub struct EthernetDevice {
268
242
tx_index : usize ,
269
243
rx_index : usize ,
270
244
emac0 : EMAC0 ,
245
+ tx_descriptor_reserved : [ bool ; NUM_TX_DESCRIPTORS ] ,
271
246
}
272
247
273
248
impl EthernetDevice {
@@ -337,21 +312,30 @@ impl EthernetDevice {
337
312
338
313
unsafe {
339
314
for i in 0 ..NUM_TX_DESCRIPTORS {
315
+ TX_DESCRIPTORS [ i] . tdes0 . write ( |w| {
316
+ w. bits (
317
+ DES0_TX_CTRL_LAST_SEG
318
+ | DES0_TX_CTRL_FIRST_SEG
319
+ | DES0_TX_CTRL_CHAINED
320
+ | DES0_TX_CTRL_IP_ALL_CKHSUMS ,
321
+ )
322
+ } ) ;
340
323
TX_DESCRIPTORS [ i]
341
324
. tdes1
342
325
. write ( |w| w. bits ( DES1_TX_CTRL_SADDR_INSERT ) ) ;
326
+ TX_DESCRIPTORS [ i]
327
+ . tdes2
328
+ . write ( |w| w. bits ( & mut TX_BUFFERS [ i] as * mut _ as * mut _ as u32 ) ) ;
343
329
TX_DESCRIPTORS [ i] . tdes3 . write ( |w| {
344
330
w. bits ( if i == NUM_TX_DESCRIPTORS - 1 {
345
331
& TX_DESCRIPTORS [ 0 ]
346
332
} else {
347
333
& TX_DESCRIPTORS [ i + 1 ]
348
334
} as * const _ as u32 )
349
335
} ) ;
350
- TX_DESCRIPTORS [ i] . tdes0 . write ( |w| w. bits ( 0 ) ) ;
351
336
}
352
337
353
338
for i in 0 ..NUM_RX_DESCRIPTORS {
354
- RX_DESCRIPTORS [ i] . rdes0 . write ( |w| w. bits ( DES0_RX_CTRL_OWN ) ) ;
355
339
RX_DESCRIPTORS [ i] . rdes1 . write ( |w| {
356
340
w. bits (
357
341
DES1_RX_CTRL_CHAINED
@@ -368,6 +352,7 @@ impl EthernetDevice {
368
352
& RX_DESCRIPTORS [ i + 1 ]
369
353
} as * const _ as u32 )
370
354
} ) ;
355
+ RX_DESCRIPTORS [ i] . rdes0 . write ( |w| w. own ( ) . set_bit ( ) ) ;
371
356
}
372
357
373
358
emac0
@@ -392,6 +377,11 @@ impl EthernetDevice {
392
377
// unsafe {
393
378
// EMACIntClear(emac0_base, EMACIntStatus(emac0_base, false));
394
379
// }
380
+ unsafe {
381
+ emac0. dmaim . write ( |w| w. bits ( 0xffff_ffff ) ) ;
382
+ emac0. ephyim . write ( |w| w. bits ( 0xffff_ffff ) ) ;
383
+ }
384
+
395
385
emac_tx_enable ( & emac0) ;
396
386
emac_rx_enable ( & emac0) ;
397
387
nvic. enable ( tm4c129x:: Interrupt :: EMAC0 ) ;
@@ -400,6 +390,7 @@ impl EthernetDevice {
400
390
rx_index : 0 ,
401
391
tx_index : 0 ,
402
392
emac0,
393
+ tx_descriptor_reserved : [ false ; NUM_TX_DESCRIPTORS ] ,
403
394
}
404
395
}
405
396
}
@@ -410,39 +401,65 @@ impl<'a> Device<'a> for EthernetDevice {
410
401
411
402
fn receive ( & ' a mut self ) -> Option < ( Self :: RxToken , Self :: TxToken ) > {
412
403
// Make sure that we own the receive descriptor.
413
- unsafe {
414
- if RX_DESCRIPTORS [ self . rx_index ] . rdes0 . read ( ) . bits ( ) & DES0_RX_CTRL_OWN
415
- == DES0_RX_CTRL_OWN
416
- {
417
- return None ;
418
- }
404
+ let rx_descriptor = & RX_DESCRIPTORS [ self . rx_index ] ;
405
+ if rx_descriptor. rdes0 . read ( ) . own ( ) . bit_is_set ( ) {
406
+ return None ;
419
407
}
420
408
421
- let result = Some ( (
422
- RxToken {
423
- descriptor : & RX_DESCRIPTORS [ self . rx_index ] ,
424
- } ,
425
- TxToken {
426
- index : & mut self . tx_index ,
427
- emac0 : & mut self . emac0 ,
428
- } ,
429
- ) ) ;
409
+ let tx_reservation = & mut self . tx_descriptor_reserved [ self . tx_index ] ;
410
+ if * tx_reservation {
411
+ return None ;
412
+ }
413
+ let tx_descriptor = & TX_DESCRIPTORS [ self . tx_index ] ;
414
+ if tx_descriptor . tdes0 . read ( ) . own ( ) . bit_is_set ( ) {
415
+ return None ;
416
+ }
417
+ let tx_buffer = unsafe { & mut TX_BUFFERS [ self . tx_index ] } ;
430
418
431
419
self . rx_index += 1 ;
432
420
if self . rx_index == NUM_RX_DESCRIPTORS {
433
421
self . rx_index = 0 ;
434
422
}
423
+ self . tx_index += 1 ;
424
+ if self . tx_index == NUM_TX_DESCRIPTORS {
425
+ self . tx_index = 0 ;
426
+ }
435
427
436
- result
428
+ Some ( (
429
+ RxToken {
430
+ descriptor : rx_descriptor,
431
+ } ,
432
+ TxToken {
433
+ descriptor : tx_descriptor,
434
+ buffer : tx_buffer,
435
+ reservation : tx_reservation,
436
+ emac0 : & self . emac0 ,
437
+ } ,
438
+ ) )
437
439
}
438
440
439
- fn transmit ( & ' a mut self ) -> Option < ( Self :: TxToken ) > {
440
- let result = Some ( TxToken {
441
- index : & mut self . tx_index ,
442
- emac0 : & mut self . emac0 ,
443
- } ) ;
441
+ fn transmit ( & ' a mut self ) -> Option < Self :: TxToken > {
442
+ let tx_reservation = & mut self . tx_descriptor_reserved [ self . tx_index ] ;
443
+ if * tx_reservation {
444
+ return None ;
445
+ }
446
+ let tx_descriptor = & TX_DESCRIPTORS [ self . tx_index ] ;
447
+ if tx_descriptor. tdes0 . read ( ) . own ( ) . bit_is_set ( ) {
448
+ return None ;
449
+ }
450
+ let tx_buffer = unsafe { & mut TX_BUFFERS [ self . tx_index ] } ;
444
451
445
- result
452
+ self . tx_index += 1 ;
453
+ if self . tx_index == NUM_TX_DESCRIPTORS {
454
+ self . tx_index = 0 ;
455
+ }
456
+
457
+ Some ( TxToken {
458
+ descriptor : tx_descriptor,
459
+ buffer : tx_buffer,
460
+ reservation : tx_reservation,
461
+ emac0 : & self . emac0 ,
462
+ } )
446
463
}
447
464
448
465
fn capabilities ( & self ) -> DeviceCapabilities {
@@ -498,60 +515,50 @@ impl<'a> phy::RxToken for RxToken<'a> {
498
515
} else {
499
516
result = Err ( smoltcp:: Error :: Checksum ) ;
500
517
}
501
- self . descriptor . rdes0 . write ( |w| w. bits ( DES0_RX_CTRL_OWN ) ) ;
518
+ self . descriptor . rdes0 . write ( |w| w. own ( ) . set_bit ( ) ) ;
502
519
}
503
520
504
521
result
505
522
}
506
523
}
507
524
508
525
pub struct TxToken < ' a > {
509
- index : & ' a mut usize ,
510
- emac0 : & ' a mut EMAC0 ,
526
+ descriptor : & ' a TDES ,
527
+ buffer : & ' a mut [ u8 ] ,
528
+ reservation : & ' a mut bool ,
529
+ emac0 : & ' a EMAC0 ,
530
+ }
531
+
532
+ impl < ' a > Drop for TxToken < ' a > {
533
+ fn drop ( & mut self ) {
534
+ * self . reservation = false ;
535
+ }
511
536
}
512
537
513
538
impl < ' a > phy:: TxToken for TxToken < ' a > {
514
539
fn consume < R , F > ( self , _timestamp : Instant , len : usize , f : F ) -> smoltcp:: Result < R >
515
540
where
516
541
F : FnOnce ( & mut [ u8 ] ) -> smoltcp:: Result < R > ,
517
542
{
518
- let descriptor = & TX_DESCRIPTORS [ * self . index ] ;
519
- * self . index += 1 ;
520
- if * self . index == NUM_TX_DESCRIPTORS {
521
- * self . index = 0 ;
522
- }
523
-
524
- while descriptor. tdes0 . read ( ) . bits ( ) & DES0_TX_CTRL_OWN == DES0_TX_CTRL_OWN { }
525
-
526
- assert ! ( len <= RX_BUFFER_SIZE ) ;
543
+ assert ! ( self . descriptor. tdes0. read( ) . own( ) . bit_is_clear( ) ) ;
527
544
528
- let mut data : [ u8 ; RX_BUFFER_SIZE ] = unsafe { core :: mem :: uninitialized ( ) } ;
529
- let result = f ( & mut data ) ;
545
+ assert ! ( len <= self . buffer . len ( ) ) ;
546
+ let result = f ( self . buffer ) ;
530
547
531
548
// Fill in the packet size and pointer, and tell the transmitter to start work.
532
549
unsafe {
533
- descriptor. tdes1 . write ( |w| w. bits ( len as u32 ) ) ;
534
- descriptor
535
- . tdes2
536
- . write ( |w| w. bits ( & mut data as * mut _ as * mut _ as u32 ) ) ;
537
- descriptor. tdes0 . write ( |w| {
538
- w. bits (
539
- DES0_TX_CTRL_LAST_SEG
540
- | DES0_TX_CTRL_FIRST_SEG
541
- | DES0_TX_CTRL_CHAINED
542
- | DES0_TX_CTRL_OWN
543
- | DES0_TX_CTRL_IP_ALL_CKHSUMS ,
544
- )
545
- } ) ;
550
+ self . descriptor
551
+ . tdes1
552
+ . write ( |w| w. tbs1 ( ) . bits ( len. try_into ( ) . unwrap ( ) ) ) ;
546
553
}
554
+ self . descriptor . tdes0 . modify ( |_, w| w. own ( ) . set_bit ( ) ) ;
555
+ * self . reservation = false ;
547
556
548
557
// Tell the DMA to reacquire the descriptor now that we've filled it in. This
549
558
// call is benign if the transmitter hasn't stalled and checking the state takes
550
559
// longer than just issuing a poll demand so we do this for all packets.
551
560
self . emac0 . txpolld . write ( |w| w) ;
552
561
553
- while descriptor. tdes0 . read ( ) . bits ( ) & DES0_TX_CTRL_OWN == DES0_TX_CTRL_OWN { }
554
-
555
562
result
556
563
}
557
564
}
0 commit comments