2

As a result of large research, I found this source code for STM32f1 this link and I changed it for STM32f3. And the build and install to my STM32. My ethernet cable connect between my computer and enc28j60 module. If I debugging this code my code stack in main.c and while loop:

  while (1)
    {


        eMBPoll();
        led_poll();

        /* 从网络设备读取一个IP包,返回数据长度 */
        uip_len = tapdev_read();
        /* 收到数据 */
        **if (uip_len > 0)**
        {
            /* 处理IP数据包 */
            if (BUF->type == htons(UIP_ETHTYPE_IP))
            {
                uip_arp_ipin();
                uip_input();

                if (uip_len > 0)
                {
                    uip_arp_out();
                    tapdev_send();
                }
            }
            /* 处理ARP报文 */
            else if (BUF->type == htons(UIP_ETHTYPE_ARP))
            {
                uip_arp_arpin();
                if (uip_len > 0)
                {
                    tapdev_send();
                }
            }
        }

I stuck if (uip_len > 0) line because uip_len return 0 for this line:

(My code same as bellow github link so i dont share all of code )

enc28j_60.c in the unsigned int enc28j60_packet_receive(unsigned char *packet, unsigned int maxlen) function:

unsigned int enc28j60_packet_receive(unsigned char *packet, unsigned int maxlen)
{

    unsigned int rxstat;
    unsigned int len;

    if (enc28_read(EPKTCNT) == 0)
    {
        return (0);
    }

    enc28_write(ERDPTL, (next_pack_ptr));
    enc28_write(ERDPTH, (next_pack_ptr) >> 8);

    next_pack_ptr = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    next_pack_ptr |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    len = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    len |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    len -= 4;

    rxstat = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    rxstat |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    if (len > maxlen - 1)
    {
        len = maxlen - 1;
    }

    **if ((rxstat & 0x80) == 0)
    {
        GPIO_SetBits(GPIOE, GPIO_Pin_9);

        len = 0;
    }**
    else
    {

        des_enc28_readBuffer(packet, len);
    }

    enc28_write(ERXRDPTL, (next_pack_ptr));
    enc28_write(ERXRDPTH, (next_pack_ptr) >> 8);

    enc28_writeOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);

    return (len);
}

Why is the rxstat & 0x80) == 0? I do not understand.

CKocar
  • 566
  • 9
  • 25

1 Answers1

1

According to the ENC28J60 datasheet, it seems like RXSTAT flag should be at bit 12:

ENC28J60 Phy Registers

I am not exactly sure if des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0) is reading the right thing, but I believe you should have something like:

unsigned PHSTAT2 = des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
PHSTAT2 |= des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

unsigned RXSTAT = (PHSTAT2 & 0x1000) != 0;
if (RXSTAT)
{
    // RXSTAT flag is set
    des_enc28_readBuffer(packet, len);
}
else
{
    ...
}

I would also dump the values of this register to a log or serial port, to make sure you understand what its contents actually are:

// I noticed serialprint in your other question, so I am presuming this is your log func
serialprint("PHSTAT2 = 0x%04x\n", PHSTAT2);
vgru
  • 49,838
  • 16
  • 120
  • 201
  • Thank you for your answer.PHSTAT2 & 0x1000 return 1 but now if (BUF->type == htons(UIP_ETHTYPE_IP)) or else if (BUF->type == htons(UIP_ETHTYPE_ARP)) return 0 why is that ? – CKocar Dec 20 '18 at 10:38
  • @CKocar: presumably it's a different etherent packet type, dump it using `serialprint("0x%04x", 0xBUF->type)`. It's impossible to guess what's the problem, you won't be able to fix this without: 1) logging functionality (I believe `serialprint` is working?), 2) a debugger, and 3) [Wireshark](https://www.wireshark.org/). Install Wireshark and learn how to use it, it's indispensable for any development involving ethernet communication, and supports a [ridiculously huge list of protocols](https://www.wireshark.org/docs/dfref/). – vgru Dec 20 '18 at 10:47
  • yes i did, and i am using wireshark. every time BUF->type changing, for example 0x004b,0x0000 but it doesnt be 0x0800 or 0x0806. Why this happened – CKocar Dec 20 '18 at 11:07
  • What is Wireshark telling you? Do you see these packets there? – vgru Dec 20 '18 at 11:45
  • yesterday i didnt notice but my phstat register return everytime "rx stat reg :0x3a3a" and type every time being type 0x9773 why is that ? – CKocar Dec 21 '18 at 06:30
  • `0x3a3a` seems like you're reading the same byte twice, are you sure the second parameter in `des_enc28_readOp` is always `0`? – vgru Dec 21 '18 at 11:13