ST ARM Cortex M4-F : Semaphores Mutex Queues

Hello, I have started with FreeRTOS and have succesfully written, compiled and ported FreeRTOS code for Cortex M4-F (STM32F4DISCOVERY). I also did same for Queue where I created two sending task with same priority (2) and one receiving task with lower priority (1). I have made use of an array xSend = {100,200,300} and I’m sending xSend and xSend to Queue via two sender task. However while receiving the receive first receives 200 and then 100 instead of 100 and then 200. May anyone please through light on why ? Also to control the things if I wish to integrate Semaphores Mutex with Queues then what should be the best way to move forward as I’m still trying to understand how things work. This is current code :
#include "stm32f4xx.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
    xQueueHandle IntQueue;
} HandlesType;
HandlesType* getHandles() {
    static HandlesType h;
    return &h;
}
uint32_t xSend[] = {100,200,300};
void task_tx(void* p) {
    int i = 0;
    while (1) {
        i++;
        if (!xQueueSend(getHandles()->IntQueue, p, 500)) {
            puts("Failed to send item to queue within 500ms");
    }
        vTaskDelay(1000);
    }
}
void task_rx(void* p) {
    int myInt = 0;

    uint32_t xReceive[3];
    while (1) {
        myInt++;
        if (!xQueueReceive(getHandles()->IntQueue, &(xReceive[myInt]), 1000)) {
            puts("Failed to receive item within 1000 ms");
        } else {
            printf("Received: %un", xReceive);
        }
    }
}
int main() {
    getHandles()->IntQueue = xQueueCreate(2, sizeof(int));
    xTaskCreate(task_tx, (signed char*) "t1", 2048, &(xSend[0]), 2, 0);
    xTaskCreate(task_tx, (signed char*) "t3", 2048, &(xSend[1]), 2, 0);
    xTaskCreate(task_rx, (signed char*) "t2", 2048, 0, 1, 0);
    vTaskStartScheduler();
    return -1;
}

ST ARM Cortex M4-F : Semaphores Mutex Queues

As far as I can see you are creating two Tx tasks that have the same priority.  One Tx task always sends 100, and the other Tx task always sends 200.  As both Tx tasks have the same priority there is no guarantee which will run first, so no guarantee which will send to the queue first.  In fact, in this case, as: - the tasks are very short, so will probably execute an itteration of their loop in a single time slice (equal priority tasks will time slice) - the Tx task that is created last is the task that sends 200 to the queue, and (because of the internal workings of the kernel) the task that is created last will be the first of the two to run it is almost certain that 200 will be received before 100. Regards.

ST ARM Cortex M4-F : Semaphores Mutex Queues

@richardbarry Thanks for replying. Before seeing your reply I tried decreasing the priority of task2 to 1 and then sequence of task execution was correct. Seeing this if I have n number of task then am I expected to provide task priority to all in sequential manner or execution ?

ST ARM Cortex M4-F : Semaphores Mutex Queues

Before seeing your reply I tried decreasing the priority of task2 to 1 and then sequence of task execution was correct.
The sequence of execution was not incorrect, just not what you expected.
Seeing this if I have n number of task then am I expected to provide task priority to all in sequential manner or execution
I’m not sure I understand your question.  The scheduler will always run the highest priority task that is able to run (not blocked), and time slice tasks of equal priority that are able to run (when their priority is the highest available).  You have to assign priorities to be correct for your application – this form of task scheduling is what you might call “traditional” in that it is the way most marketed RTOSes will function. Regards.

ST ARM Cortex M4-F : Semaphores Mutex Queues

@richardbarry Thanks for replying.
The sequence of execution was not incorrect, just not what you expected.
Yes. Correct. Now may you please let me know the best way to move forward on using semaphores and mutexes?

ST ARM Cortex M4-F : Semaphores Mutex Queues

Hello, MAX_COUNT = 3 I’m using xSemaphoreCreateCounting(MAX_COUNT,0); however when I try to compile it always through this error :
obj/src/main.o: In function `task_rx':
src/main.c:49: undefined reference to `xQueueCreateCountingSemaphore'
collect2: ld returned 1 exit status
make: *** [obj/STM32F4_Test.elf] Error 1
I understand that there is linker error, however I have properly included source and header files in Makefile and this error occurs only if I use : xSemaphoreCreateCounting(MAX_COUNT,0); if I use binary or mutex then this isn’t not issue with same Makefile and source directories. `xQueueCreateCountingSemaphore’ has been defined as a macro in semphr.h May anyone point me out where things may be going wrong ? Thanks.

ST ARM Cortex M4-F : Semaphores Mutex Queues

Set configUSE_COUNTING_SEMAPHORES to 1 in FreeRTOSConfig.h. Regards.