CKocar
CKocar

Reputation: 586

How to use the MODBUS TCP with STM32F3?

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.

Upvotes: 2

Views: 1537

Answers (1)

vgru
vgru

Reputation: 51302

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);

Upvotes: 1

Related Questions