FreeRTOS eating memory

heinbali01 wrote on Thursday, October 02, 2014:

Bonjours Jonathan,

Thanks for your reply, but I’m still wondering why you need to create 30 tasks?
Why can’t you control all ~200 points from a single task?

Once you’ve decided that a single task becomes the ‘owner’ of those measurement points, programming may become a lot easier: no more locking is needed, you can use plain variables and change settings whenever you like.

You might want to give this task a queue so you can send it messages from anywhere. The task will then become like a server.

I don’t see how I could use the event system as protection for variables? Any sample?

Sure. An event group has up to 24 bits, which can be used as 24 individual semaphores :slight_smile:

Assuming that the right to access each of 200 variables is independent, you could use code like this:


/* The code below has not been tested, it's just a sketch */

/* An example of using group events as semaphores */

#define VARIABLE_COUNT   200
#define BITS_PER_GROUP    24
#define GROUP_COUNT      ( VARIABLE_COUNT / BITS_PER_GROUP )

/* 9 groups will be needed for 200 variables */
EventGroupHandle_t xEventGroups[ GROUP_COUNT ];

void init( void )
{
    for( x = 0; x < GROUP_COUNT; x++ )
    {
        xEventGroups[ x ] = xEventGroupCreate( );
        /* Set all bits to '1' so the 'semaphore' can be taken */
        xEventGroupSetBits( xEventGroups[ x ],
            ( 1u << BITS_PER_GROUP ) - 1 );
    }
}

void access( BaseType_t xIndex )
{
    EventBits_t xBitMask = 1U << ( xIndex % BITS_PER_GROUP );

    /* Setting xClearOnExit to true is important */
    xEventGroupWaitBits( xEventGroups[ xIndex / BITS_PER_GROUP ],
        xBitMask, pdTRUE, pdFALSE, ( TickType_t ) portMAX_DELAY )
    /* access a variable, an object, an array or whatever */

    /* And release the 'semaphore' for this variable */
    xEventGroupSetBits( xEventGroups[ xIndex / BITS_PER_GROUP ],
        xBitMask );
}

Maybe the variables are clustered in groups, and you might find that a single event group is enough to guard the access to all clusters.

Note:
xEventGroupWaitBits() has a parameter ‘xWaitForAllBits’ which can become useful if you have a hierarchy of access rights. Suppose you want to lock the access a set of variables (expressed as a bit-mask), this can easily be done.

Regards,
Hein