Page 1 of 1

Tasks synchronization for TCP/IP sending and receiving data

Posted: Wed Mar 13, 2019 5:08 pm
by e2738729
Hi everyone,

I am currently working on a project that involves a PC as a TCP/IP client and 12 ESP32's as TCP/IP Server. My goal is to have the ESP32's collect data from an ADC via SPI, then send those data to the PC. The PC will use multiprocessing to collect and process these data and send a command back to the ESP32's. Based on the command received by the PC, the ESP32's will perform certain control algorithms.

I created a tcp/ip server tasks to setup the esp32 as server. This task then created another task to send data from ESP32 (TCP/IP server) to PC (TCP/IP Client) whenever the data buffer was full.

I would like to create another task to receive commands (binaries) from PC after the PC has received and processed the data mentioned above

1. Should I xEventGroupCreate() for the sending and receiving tasks then xEventGroupSync() to make sure the receiving task is done after the sending task with a xTicksToWait? Also, do I need to have xTaskCreate() even if I already have xEventGroupCreate()?

2. If yes to the above, can you provide a code snippet to do this? Is there an example somewhere?

3. If no, what would be the best way to ensure that the receiving task is done after the sending task?

I also see this in the freeRTOS documentation:

Code: Select all


/* Bits used by the three tasks. */
#define TASK_0_BIT        ( 1 << 0 )
#define TASK_1_BIT        ( 1 << 1 )
#define TASK_2_BIT        ( 1 << 2 )

#define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )

/* Use an event group to synchronise three tasks.  It is assumed this event
group has already been created elsewhere. */
EventGroupHandle_t xEventBits;

void vTask0( void *pvParameters )
{
EventBits_t uxReturn;
TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;

    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 0 in the event group to note this task has reached the
        sync point.  The other two tasks will set the other two bits defined
        by ALL_SYNC_BITS.  All three tasks have reached the synchronisation
        point when all the ALL_SYNC_BITS are set.  Wait a maximum of 100ms
        for this to happen. */
        uxReturn = xEventGroupSync( xEventBits,
                                    TASK_0_BIT,
                                    ALL_SYNC_BITS,
                                    xTicksToWait );

        if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
        {
            /* All three tasks reached the synchronisation point before the call
            to xEventGroupSync() timed out. */
        }
    }
}

void vTask1( void *pvParameters )
{
    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 1 in the event group to note this task has reached the
        synchronisation point.  The other two tasks will set the other two
        bits defined by ALL_SYNC_BITS.  All three tasks have reached the
        synchronisation point when all the ALL_SYNC_BITS are set.  Wait
        indefinitely for this to happen. */
        xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );

        /* xEventGroupSync() was called with an indefinite block time, so
        this task will only reach here if the syncrhonisation was made by all
        three tasks, so there is no need to test the return value. */
    }
}

void vTask2( void *pvParameters )
{
    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 2 in the event group to note this task has reached the
        synchronisation point.  The other two tasks will set the other two
        bits defined by ALL_SYNC_BITS.  All three tasks have reached the
        synchronisation point when all the ALL_SYNC_BITS are set.  Wait
        indefinitely for this to happen. */
        xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );

        /* xEventGroupSync() was called with an indefinite block time, so
        this task will only reach here if the syncrhonisation was made by all
        three tasks, so there is no need to test the return value. */
    }
}

What am I supposed to do with this:

Code: Select all

if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
        {
            /* All three tasks reached the synchronisation point before the call
            to xEventGroupSync() timed out. */
        }
Thanks,

Khoi