Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Chetan Patil on February 19, 2013
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[0] and xSend[1] 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
#include
#include
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: %u\n", 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;
}




RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Richard on February 19, 2013
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.

RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Chetan Patil on February 19, 2013
@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 ?

RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Richard on February 19, 2013
“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.

RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Chetan Patil on February 19, 2013
@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?

RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Chetan Patil on February 25, 2013
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.

RE: ST ARM Cortex M4-F : Semaphores Mutex Queues

Posted by Richard on February 26, 2013
Set configUSE_COUNTING_SEMAPHORES to 1 in FreeRTOSConfig.h.

Regards.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists