problems with priorities

pietro77 wrote on Tuesday, September 25, 2007:

I have some problems using priorities with tasks.

I have two tasks, the first one (t1) must to take chars form UART, check them and fills a queue (q1). Soon after it sends an ACK orNACK via UART; the second one (t2) must to be listening on q1 and when this queue became to be filled it takes the elements and after a little computation send some response to t1 via a second queue (q2); t1 listens q2 and when it receives somthing writes on the UART.

static portTASK_FUNCTION( t1, pvParameters ) //pvParameters is q1
        portCHAR rxCharPC;
    FramePC DataToUART;

    xUART2 = xSerialPortInit( serCOM2, ser9600, serNO_PARITY, serBITS_8, serSTOP_1, mainCOMM2_QUEUE_LENGTH );

    for( ;; )
           if ( xSerialGetChar( xUART2, &rxCharPC, mainNO_BLOCK ) )
                    put results inside Frame_PC after checking.
       if ( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &Frame_PC, pollqNO_DELAY ) == pdTRUE )
           send ACK    via UART           
           send NACK via UART           

           while ( uxQueueMessagesWaiting( q2 ) )
        xQueueReceive( q2, &DataToUART, pollqNO_DELAY );

        // SEND DATA TO UART
        SendUART( DataToUART );


t2 is

static portTASK_FUNCTION( t2, pvParameters ) //pvPArameters is q1
    FramePC DataRXed;

    for( ;; )
        while ( ( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) ) )
            xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &DataRXed, pollqNO_DELAY );

                        // computations here

            switch (DataRXed.cmd)
                case PC_CMD_REQ_INFO :
                           xQueueSend( q2, ( void * ) &DataRXed, pollqNO_DELAY );
                   default: break;

The problem is that if I use the some priority for both tasks, t1 and t2 start correctly, but t1 doesn’n work well. It doesn’t put the right characters on UART to send ACK or NACK and doesn’t perform the operation that follows soon after (sendUART).
Otherwise If I do an operation of sending ACK that post onto UART LESS character (like 4) t1 works well!!!

I obtain the perfect performance when I write only 2 chars in the ACK (or NACK) and 2 chars in the sendUART.

If I try to set a higher prioritiy to t1 rather than t2, t1 send correctly ACK or NACK, but t2 doesn’t start.
If I try to set a higher prioritiy to t2 rather than t1, t2 waits on q1 forever as t1 doesn’t start.
If I introduce some delays on t1 (after sending NACK or after SendUART) t2 doesn’t start.

I introduce delays in this way:

I declare

#define SYS_DELAY        ( ( portTickType ) 100 / portTICK_RATE_MS ) // 100ms

And then I put

vTaskDelay( SYS_DELAY ); in the locations specified before.

I wrong something surely.
How can I solve this problem?

Thank you,


rtel wrote on Tuesday, September 25, 2007:

I don’t quite follow the sequence here, but it looks like you are not not blocking on your queue send/receives, but instead polling.  When the tasks are the same priority this will be inefficient.  When the tasks are of different priorities then the low priority task will get starved of processing time and never run.  This is because, without blocking, the higher priority task is always wanting processor time.