Search code examples
tcparmfreertoslwip

Receive data from TCP (FreeRTOS, LWIP)


When I start this program (see below), all is working fine. But when I disable return data back to TPCIP, program working bad.

Here is program, working fine: [1]: http://s27.postimg.org/seixg63hf/problem_2.jpg

static err_t prijata_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    void *data; 
    if (err == ERR_OK && p != NULL) {
        // information of receive data
        tcp_recved(pcb, p->tot_len);
        //free buffer
        pbuf_free(p);   
        data = p->payload;  //copy value to send out of queue
        xQueueSend(queue_ethernet, &data, 10);  //send value to queue
        //send data out
        err = tcp_write(pcb, p->payload, p->len, TCP_WRITE_FLAG_COPY);  
        tcp_sent(pcb, NULL); // No need to call back
    } 
    else 
    {
        pbuf_free(p);
    }
    if (err == ERR_OK && p == NULL) {
        close_conn(pcb);
    }
    return ERR_OK;
}

And this function going here: s

static err_t prijimani_dat(void *arg, struct tcp_pcb *pcb, err_t err){
    LWIP_UNUSED_ARG(arg);
    LWIP_UNUSED_ARG(err);
    tcp_setprio(pcb, TCP_PRIO_MIN);
    tcp_recv(pcb, prijata_data);
    tcp_err(pcb, server_err); 
    tcp_poll(pcb, server_poll, 4);
    return ERR_OK;
}

Complet communication is implement in task FreeRTOS:

extern void TCP_connection(void *pvParameters)
{
    UNUSED(pvParameters);
    const portTickType xDelayTime = 5 / portTICK_RATE_MS;

    struct tcp_pcb *tcp_server;
    tcp_server = tcp_new();

    tcp_bind(tcp_server, IP_ADDR_ANY, TCP_PORT);

    while (1){
        tcp_server = tcp_listen(tcp_server);
        tcp_accept(tcp_server, prijimani_dat);

        vTaskDelay(xDelayTime);
    }
}

But when I can only reading from TCP, and I do not want back data to TCPIP, program working bad.: [2]: http://s21.postimg.org/wlhqh31g5/problem_1.jpg

static err_t prijata_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){
    void *data;

    if (err == ERR_OK && p != NULL) {
        /* information of receive data */
        tcp_recved(pcb, p->tot_len);

        //free buffer
        pbuf_free(p); 

        data = p->payload;  /*copy value to send out of queue*/
        xQueueSend(queue_ethernet, &data, 10);  //send value to queue
    } 
    else 
    {
        pbuf_free(p);
    }

    if (err == ERR_OK && p == NULL) {
        close_conn(pcb);
    }
    return ERR_OK;
}

Solution

  • There is a very old example of how to use FreeRTOS with lwIP in the FreeRTOS Interactive site. Although a lot of the code included in this example has now been superseded the integration with lwIP should still be valid and make a reference for you.

    If you are using one of the processors listed on the FreeRTOS+TCP examples page (FreeRTOS+TCP being FreeRTOS's own TCP/IP stack) then that may provide a further reference.