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

FreeRTOS multithreading issues (Interrupt+Queue+Tasks)

Posted by zouzman on October 28, 2016

Hi,

I am working with STM32L476xx microcontrroller and I am using FreeRTOS V8.1.2 with** heap4.c** configuration.

In my application , I have configured 2 tasks and one interrupt routine service. The first task create a queue before entering the infinite loop . In its while(1) , if the queue is successfully created I activate the interrupt for receiving data through UART and I check if the queue has a new data to print on the terminal using this function :

if( xQueueReceive( RxQueue, &bRxBuffer, portMAX_DELAY )) { UART4->TDR = bRxBuffer; }

Now , let's go back to my uart callback . Once I get a new data from the terminal I disable the interrupt which has been enabled in my first thread . Then, I read data from the UART register and I send my buffer contents to the queue using this function :

if ( xQueueSendToBackFromISR( RxQueue, &aRxBuffer, &xHigherPriorityTaskWoken )) ** portENDSWITCHINGISR( xHigherPriorityTaskWoken );

When I only implement the first task and the uart callback only without any other task , the application runs very well !!! After adding a simple task which only outputs using printf inside my IDE terminal IO window , the application crushs :(((

Following is my printf task implementation :

void CCWDoNothing() { osThreadDef(NothingTask, NothingTaskThread, osPriorityNormal, 0, configMINIMALSTACKSIZE*1); NothingTaskxHandle = osThreadCreate(osThread(NothingTask), NULL); }

static void NothingTaskThread(void const *argument) {
while (1) {
** BSWPRINTF(" NOTHING TO DOOOO !!! nr"); osThreadSuspend(NULL);
} }**

To switch between my threads , I'm using osThreadResume(NothingTaskxHandle) inside my first task and I use osThreadSuspend(NULL) in the printf task.

The first thing I thought about when facing this issue is trying to change Tasks priorities but it doesn't work.

My first task has osPriorityAboveNormal as priority and the uart callback priority is set to the lowest NVIC available level which is 15.

I have also tried to change stack sizes and it doesn't work too :(((

Please need urgent help :(


FreeRTOS multithreading issues (Interrupt+Queue+Tasks)

Posted by rtel on October 28, 2016

I don't understand "To switch between my threads , I'm using osThreadResume(NothingTaskxHandle)" first because the RTOS switches between threads, second because you do not show where it is being called, and third because it is not a FreeRTOS API function so presumably has some form of abstraction layer code between it and the actual FreeRTOS function.

A few notes:

1) Do you still get the problem if you remove the call to printf()?

2) Passing characters from a UART into a task using a queue is very inefficient. If characters are arriving slowly, for example characters typed into a terminal, then it is simple an convenient. If characters are arriving quickly then use a DMA interface ideally, and if that is not possible, simply place the characters in a circular buffer then use the efficient vTaskNotificationGiveFromISR() API function to unblock a task when the buffer contains a complete message.

3) Do you have configASSERT() defined and stack overflow protection set to 2?


FreeRTOS multithreading issues (Interrupt+Queue+Tasks)

Posted by zouzman on October 28, 2016

Thanks for your replay ! Let me answer your questions :

1) Yes . When I remove printf from the NothingTask and replace it by incrementing an integer , the application runs well .

2) Sorry but I didn't get well the difference between queues and circular buffers and how to implement the circular buffers too. I thought that in my use case , creating a queue that receives data in an interrupt callback then display it in the task by using these APIs : xQueueSendToBackFromISR and xQueueReceive will simply make me able to fill the queue and then retrieve data as a FIFO . Could you please clarify more the need for the circular buffer and if possible of course could you provide me with an example of implementation ?

3) Yes . I do have configASSERT() defined and the configCHECKFORSTACK_OVERFLOW is set to 0.


FreeRTOS multithreading issues (Interrupt+Queue+Tasks)

Posted by rtel on October 28, 2016

1) Yes . When I remove printf from the NothingTask and replace it by incrementing an integer , the application runs well .

printf() would seem to be the problem then. Some implementations of printf() are massive, not thread safe, use huge amounts of stack, call malloc(), etc. - all things that are really bad in small embedded systems. Other implementations of printf() are tiny and designed to work nicely in embedded systems. The FreeRTOS distribution has multiple copies of a file called printf-stdarg.c, some of which are better than others, that shows a light weight version. You just need to implement the function at the very top of the file to output the characters how you want (UART, terminal, whatever).

2) Sorry but I didn't get well the difference between queues and circular buffers and how to implement the circular buffers too. I thought that in my use case , creating a queue that receives data in an interrupt callback then display it in the task by using these APIs : xQueueSendToBackFromISR and xQueueReceive will simply make me able to fill the queue and then retrieve data as a FIFO . Could you please clarify more the need for the circular buffer and if possible of course could you provide me with an example of implementation ?

This is just a simple array, with a write pointer and a read pointer. Each time you write data to the buffer move the write pointer onto the next position until you reach the end, where you wrap back to the beginning. Likewise the read pointer. Nothing fancy.

Searching "thread safe circular buffer" or "thread safe circular queue" on the web will no doubt through up lots of implementations.


FreeRTOS multithreading issues (Interrupt+Queue+Tasks)

Posted by zouzman on October 28, 2016

Thanks again ! :)) I will try these tips , hope they resolve my problem ! ;)


[ 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