00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00071 #include <inet/debug.h>
00072 #include <inet/datatypes.h>
00073 #include <inet/system.h>
00074 #include <inet/ethernet.h>
00075
00076
00077 #include <inet/arch/config.h>
00078
00079 UINT8 NE2000NextPktPtr;
00080 UINT8 NE2000CurrPktPtr;
00082 UINT8 EtherSleep = 0;
00093 struct ethernet_frame received_frame;
00094
00105 struct ethernet_frame send_frame;
00106
00107
00118 void outNE2000 (UINT8 reg , UINT8 dat)
00119 {
00120
00121 UINT8 temp = reg;
00122
00123 DATADIR = DDR_OUT;
00124 DATABUS = dat;
00125 ADRBUS = (temp | 0x60);
00126
00127
00128
00129 IOW = 0;
00130 while(IOCHRDY == 0);
00131 IOW = 1;
00132
00133 }
00134
00148 void outNE2000again (UINT8 dat)
00149 {
00150
00151
00152 DATABUS = dat;
00153
00154
00155
00156 IOW = 0;
00157 while(IOCHRDY == 0);
00158 IOW = 1;
00159 }
00160
00175 void outNE2000againbuf (UINT8* buf, UINT16 len)
00176 {
00177
00178 while(len--)
00179 {
00180 DATABUS = *buf++;
00181 IOW = 0;
00182 while(IOCHRDY == 0);
00183 IOW = 1;
00184 }
00185 }
00186
00187
00198 UINT8 inNE2000 (UINT8 reg)
00199 {
00200 UINT8 temp = reg;
00201 DATADIR = DDR_IN;
00202 ADRBUS = (temp | 0x60);
00203
00204
00205
00206 IOR = 0;
00207 while(IOCHRDY == 0);
00208 temp = DATABUS;
00209 IOR = 1;
00210
00211 return temp;
00212 }
00213
00226 UINT8 inNE2000again (void)
00227 {
00228 UINT8 temp;
00229
00230
00231
00232 IOR = 0;
00233 while(IOCHRDY == 0);
00234 temp = DATABUS;
00235 IOR = 1;
00236
00237 return(temp);
00238
00239 }
00240
00252 void inNE2000againbuf (UINT8* buf, UINT16 len)
00253 {
00254 while(len--)
00255 {
00256 IOR = 0;
00257 while(IOCHRDY == 0);
00258 *buf++ = DATABUS;
00259 IOR = 1;
00260 }
00261 }
00262
00274 UINT8 NE2000CheckRxFrame (void)
00275 {
00276
00277
00278 UINT8 temp;
00279
00280 outNE2000( CR ,0x62);
00281 temp = inNE2000( CURR );
00282 outNE2000( CR, 0x02 );
00283
00284 if( temp != inNE2000( BOUNDARY ) ) {
00285
00286
00287
00288 if(inNE2000(RCR) & 0x01)
00289 return(TRUE);
00290
00291 }
00292
00293 return(FALSE);
00294 }
00295
00304 void NE2000DumpRxFrame (void)
00305 {
00306
00307 outNE2000( CR, 0x22 );
00308
00309
00310
00311 outNE2000( BOUNDARY, NE2000NextPktPtr );
00312
00313
00314 }
00315
00316
00327 void NE2000Init (UINT8* mac)
00328 {
00329
00330
00331
00332 RESETPIN_NE2000 = 1;
00333 wait(10000);
00334 RESETPIN_NE2000 = 0;
00335 wait(10000);
00336
00337 outNE2000( RESETPORT, 0 );
00338
00339
00340 wait(30000);
00341
00342
00343
00344 outNE2000( CR, 0xC1 );
00345 outNE2000( 0x01, 0xC0 );
00346 outNE2000( 0x05, 0 );
00347
00348
00349
00350
00351 outNE2000( CR, 0x41 );
00352 outNE2000( PAR5, *mac++);
00353 outNE2000( PAR4, *mac++);
00354 outNE2000( PAR3, *mac++);
00355 outNE2000( PAR2, *mac++);
00356 outNE2000( PAR1, *mac++);
00357 outNE2000( PAR0, *mac);
00358
00359 outNE2000( CURR, RXBUF_START );
00360 NE2000NextPktPtr = RXBUF_START;
00361 NE2000CurrPktPtr = RXBUF_START;
00362
00363
00364
00365 outNE2000( CR, 0 );
00366 outNE2000( PSTART, RXBUF_START);
00367 outNE2000( PSTOP, RXBUF_END );
00368 outNE2000( BOUNDARY, RXBUF_START );
00369 outNE2000( ISR, 0xFF );
00370 outNE2000( RCR, 0xC4);
00371 outNE2000( TCR, 0xE0);
00372 outNE2000( DCR, 0xB8);
00373
00374
00375
00376 outNE2000( CR, 0x22 );
00377
00378 }
00379
00380
00393 void NE2000CheckOverFlow (void)
00394 {
00395
00396
00397
00398 UINT8 temp;
00399
00400 outNE2000( CR, 0x22 );
00401
00402 if( inNE2000( ISR ) & 0x10 ) {
00403
00404
00405
00406 outNE2000( CR, 0x21 );
00407
00408 outNE2000( RBCR0, 0x00 );
00409 outNE2000( RBCR1, 0x00 );
00410
00411
00412
00413 kick_WD();
00414 while( ( inNE2000( ISR ) & 0x80 ) == 0 );
00415
00416
00417
00418
00419 outNE2000( TCR, 0x02 );
00420
00421
00422
00423
00424 outNE2000(CR,0x22);
00425
00426
00427
00428 if(inNE2000(BOUNDARY) != NE2000NextPktPtr) {
00429 ETH_DEBUGOUT("\r\n*********Normal Overflow**********\r\n");
00430 outNE2000( BOUNDARY, NE2000NextPktPtr );
00431 } else {
00432
00433
00434
00435 ETH_DEBUGOUT("???????????????????????????????????\r\n");
00436 ETH_DEBUGOUT("Unrecognized overflow!!\r\n");
00437 ETH_DEBUGOUT("¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\r\n");
00438
00439 outNE2000(CR ,0x62);
00440 temp = inNE2000(CURR);
00441 outNE2000( CR, 0x22 );
00442
00443
00444
00445 outNE2000( BOUNDARY, temp);
00446
00447 NE2000NextPktPtr = temp;
00448 NE2000CurrPktPtr = temp;
00449
00450 }
00451
00452
00453 outNE2000( ISR, 0xFF );
00454
00455
00456
00457 outNE2000( TCR, 0xE0);
00458
00459 }
00460
00461 }
00462
00482 UINT8 NE2000ReceiveFrame (void)
00483 {
00484
00485
00486 if( NE2000CheckRxFrame() == FALSE )
00487 return(FALSE);
00488
00489
00490
00491
00492 NE2000CurrPktPtr = inNE2000(BOUNDARY);
00493 outNE2000( CR, 0x22 );
00494
00495
00496
00497
00498 outNE2000( RSAR0, 0 );
00499 outNE2000( RSAR1, NE2000CurrPktPtr );
00500 outNE2000( RBCR0, 0xFF );
00501 outNE2000( RBCR1, 0x0F );
00502
00503 outNE2000( CR, 0x0A );
00504
00505 inNE2000( IOPORT );
00506
00507 NE2000NextPktPtr = inNE2000again();
00508
00509
00510
00511 received_frame.frame_size = inNE2000again();
00512 received_frame.frame_size |= ((UINT16)inNE2000again()) << 8;
00513
00514 if(received_frame.frame_size > 4)
00515 received_frame.frame_size -= 4;
00516 else
00517 return(FALSE);
00518
00519
00520
00521 received_frame.destination[5] = inNE2000again();
00522 received_frame.destination[4] = inNE2000again();
00523 received_frame.destination[3] = inNE2000again();
00524 received_frame.destination[2] = inNE2000again();
00525 received_frame.destination[1] = inNE2000again();
00526 received_frame.destination[0] = inNE2000again();
00527
00528
00529
00530 received_frame.source[5] = inNE2000again();
00531 received_frame.source[4] = inNE2000again();
00532 received_frame.source[3] = inNE2000again();
00533 received_frame.source[2] = inNE2000again();
00534 received_frame.source[1] = inNE2000again();
00535 received_frame.source[0] = inNE2000again();
00536
00537
00538
00539 received_frame.protocol = inNE2000again();
00540 received_frame.protocol <<= 8;
00541 received_frame.protocol |= inNE2000again();
00542
00543
00544
00545 received_frame.buf_index = ETH_HEADER_LEN;
00546
00547
00548
00549 outNE2000( CR, 0x22 );
00550
00551 ETH_DEBUGOUT("Ethernet Frame Received\n\r");
00552
00553 return(TRUE);
00554
00555 }
00556
00570 void InitTransmission (UINT8 page)
00571 {
00572
00573
00574 while( (inNE2000(CR) & 0x04) ) ;
00575
00576 outNE2000( CR, 0x22 );
00577
00578 outNE2000( TPSR, page );
00579
00580 outNE2000( RSAR0, 0x00 );
00581 outNE2000( RSAR1, page );
00582
00583 outNE2000( RBCR0, 0xEA );
00584 outNE2000( RBCR1, 0x05 );
00585
00586 outNE2000( CR, 0x12 );
00587
00588
00589
00590 DATADIR = DDR_OUT;
00591 ADRBUS = (IOPORT | 0x60);
00592
00593
00594 }
00595
00608 void NE2000WriteEthernetHeader (struct ethernet_frame* frame)
00609 {
00610 INT8 i;
00611
00612
00613
00614 for(i=ETH_ADDRESS_LEN-1; i >= 0; i--) {
00615 outNE2000again(frame->destination[i]);
00616 }
00617
00618
00619
00620 for(i=ETH_ADDRESS_LEN-1; i >= 0; i--) {
00621 outNE2000again(frame->source[i]);
00622 }
00623
00624
00625
00626 outNE2000again( (UINT8)(frame->protocol >> 8) );
00627 outNE2000again( (UINT8)frame->protocol );
00628
00629 }
00630
00639 void NE2000DMAInit (UINT8 page)
00640 {
00641
00642 outNE2000( CR, 0x22 );
00643
00644
00645
00646
00647
00648 outNE2000( RSAR0, 4 );
00649 outNE2000( RSAR1, page );
00650 outNE2000( RBCR0, 0xFF );
00651 outNE2000( RBCR1, 0x0F );
00652
00653 }
00654
00665 void NE2000DMAInit_position (UINT16 pos)
00666 {
00667
00668 UINT16 abspos;
00669 UINT16 page;
00670 UINT8 offset;
00671
00672
00673
00674 abspos = pos + 4;
00675
00676 page = (UINT8)(abspos >> 8);
00677 page += NE2000CurrPktPtr;
00678
00679
00680
00681 if( page >= RXBUF_END ) {
00682 page = RXBUF_START + (page - RXBUF_END);
00683 }
00684
00685 offset = (UINT8)(abspos & 0xFF);
00686
00687
00688
00689 outNE2000( CR, 0x22 );
00690
00691
00692
00693
00694
00695 outNE2000( RSAR0, offset );
00696 outNE2000( RSAR1, (UINT8)page );
00697 outNE2000( RBCR0, 0xFF );
00698 outNE2000( RBCR1, 0x0F );
00699
00700 outNE2000( CR, 0x0A );
00701
00702
00703
00704 DATADIR = DDR_IN;
00705 ADRBUS = (IOPORT | 0x60);
00706
00707
00708 }
00709
00710
00711
00725 void NE2000SendFrame ( UINT16 len )
00726 {
00727
00728
00729
00730
00731
00732
00733 while(len < 50) {
00734 outNE2000again((BYTE)0x00);
00735 len++;
00736 }
00737
00738 len += 6 + 6 + 2;
00739
00740 outNE2000( CR, (BYTE)0x22 );
00741
00742 outNE2000( TBCR0, (BYTE)(len) );
00743 outNE2000( TBCR1, (BYTE)(len >> 8 ) );
00744
00745
00746
00747 outNE2000( CR, (BYTE)0x06 );
00748
00749
00750 }
00751
00760 void NE2000EnterSleep (void)
00761 {
00762 if (EtherSleep)
00763 return;
00764
00765 EtherSleep = 1;
00766
00767 outNE2000( CR, 0xE2 );
00768 outNE2000( 0x06, 0x04);
00769 }
00770
00779 void NE2000ExitSleep (void)
00780 {
00781 if (EtherSleep) {
00782 outNE2000( CR, 0xE2 );
00783 outNE2000( 0x06, 0x00);
00784 EtherSleep = 0;
00785 }
00786
00787 }