1 | unsigned int enc_receive_packet( unsigned int bufsize, unsigned char *buf )
|
2 | {
|
3 | unsigned char rxheader[6];
|
4 | unsigned int len, status;
|
5 | unsigned char u;
|
6 |
|
7 | // check rx packet counter
|
8 | u = enc_read_reg( ENC_REG_EPKTCNT );
|
9 | ENC_DEBUG("enc_receive: EPKTCNT=%i\n", (int) u);
|
10 | if( u == 0 ) {
|
11 | // packetcounter is 0, there is nothing to receive, go back
|
12 | return 0;
|
13 | }
|
14 |
|
15 | //set read pointer to next packet
|
16 | enc_write_reg( ENC_REG_ERDPTL, LO8(enc_next_packet_ptr) );
|
17 | enc_write_reg( ENC_REG_ERDPTH, HI8(enc_next_packet_ptr) );
|
18 |
|
19 | // read enc rx packet header
|
20 | enc_read_buf( rxheader, sizeof(rxheader) );
|
21 | enc_next_packet_ptr = rxheader[0];
|
22 | enc_next_packet_ptr |= ((unsigned)(rxheader[1])) << 8;
|
23 | len = rxheader[2];
|
24 | len |= ((unsigned)(rxheader[3])) << 8;
|
25 | status = rxheader[4];
|
26 | status |= ((unsigned)(rxheader[5])) << 8;
|
27 |
|
28 | print("IN_STATUS: %4x ", status);
|
29 | if ((status & 0x0010)) print("| CRC-ERROR ");
|
30 | if ((status & 0x0020)) print("| LEN-CHECK-ERROR ");
|
31 | if ((status & 0x0040)) print("| LENGHT Out RANGE ");
|
32 | if ((status & 0x0080)) print("| RX OK ");
|
33 | if ((status & 0x0100)) print("| RX MC ");
|
34 | if ((status & 0x0200)) print("| RX BC" );
|
35 | if ((status & 0x0800)) print("| RecControlFrame ");
|
36 | if ((status & 0x2000)) print("| UNKNOWN-OPCODE ");
|
37 | if ((status & 0x4000)) print("| DECTECT-VLAN ");
|
38 | print("\r\n");
|
39 |
|
40 | // added by Sjors: reset the ENC when needed
|
41 | // If the receive OK bit is not 1 or the zero bit is not zero, or the packet is larger then the buffer, reset the enc chip and SPI
|
42 | if ((!(status & (1<<7))) || (status & 0x8000) || (len > bufsize))
|
43 | {
|
44 | enc_init();
|
45 | }
|
46 | //ENC_DEBUG("enc_receive: status=%4x\n", (unsigned) status);
|
47 |
|
48 | // skip the checksum (4 bytes) at the end of the buffer
|
49 | len -= 4;
|
50 |
|
51 | // if the application buffer is to small, we just truncate
|
52 | if( len > bufsize ) len = bufsize;
|
53 |
|
54 | // now read the packet data into buffer
|
55 | enc_read_buf( buf, len );
|
56 |
|
57 | // adjust the ERXRDPT pointer (= free this packet in rx buffer)
|
58 | if( enc_next_packet_ptr-1 > ENC_RX_BUFFER_END
|
59 | || enc_next_packet_ptr-1 < ENC_RX_BUFFER_START ) {
|
60 | enc_write_reg( ENC_REG_ERXRDPTL, LO8(ENC_RX_BUFFER_END) );
|
61 | enc_write_reg( ENC_REG_ERXRDPTH, HI8(ENC_RX_BUFFER_END) );
|
62 | } else {
|
63 | enc_write_reg( ENC_REG_ERXRDPTL, LO8(enc_next_packet_ptr-1) );
|
64 | enc_write_reg( ENC_REG_ERXRDPTH, HI8(enc_next_packet_ptr-1) );
|
65 | }
|
66 |
|
67 | // trigger a decrement of the rx packet counter
|
68 | // this will clear PKTIF if EPKTCNT reaches 0
|
69 | enc_setbits_reg( ENC_REG_ECON2, (1<<ENC_BIT_PKTDEC) );
|
70 |
|
71 | // return number of bytes written to the buffer
|
72 | ENC_DEBUG("enc_receive: %i bytes\n", len);
|
73 | return len;
|
74 | }
|