Author Topic: freeRTOS tasks with queue not working  (Read 5457 times)

0 Members and 1 Guest are viewing this topic.

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
freeRTOS tasks with queue not working
« on: November 22, 2018, 08:33:45 pm »
Using FreeRTOS 9.0
CMSIS-RTOS API 1.2
Chip STM32L162RET

Problem: I have two tasks and one message queue, one task sends a data struct the other task receives it, everything works as expected, but when
I add a uint8_t data[DATA_SIZE] and increase the DATA_SIZE from 5-Bytes upwards (4-Bytes works) the program hangs! I set the task stack size to 512 Bytes (nothing!),

I have no idea where else to look? I stepped through the code and the code hangs during osPoolFree (block < pool_id->pool) a HardFault_Handler is generated. 

Is there a bug with CMSIS RTOS 1.0 ?

Code: [Select]

osThreadId tid_thread1;                          // ID for thread 1
osThreadId tid_thread2;                          // for thread 2
 
typedef struct {                                 // Message object structure
  float    voltage;                              // AD result of measured voltage
  float    current;                              // AD result of measured current
  int      counter;                              // A counter value
  uint8_t data[20];
} T_MEAS;
 
osPoolDef(mpool, 16, T_MEAS);                    // Define memory pool
osPoolId  mpool;

osMessageQDef(MsgBox, 16, T_MEAS);              // Define message queue
osMessageQId  MsgBox;
 
void send_thread (void const *argument);         // forward reference
void recv_thread (void const *argument);         // forward reference



//
//  Thread 1: Send thread
//
void send_thread (void const *argument) {

for(;;)
{

HAL_GPIO_TogglePin(GPIOC, LED_1_Pin);
T_MEAS    *mptr;

mptr = osPoolAlloc(mpool);                     // Allocate memory for the message
mptr->voltage = 223.72;                        // Set the message content
mptr->current = 17.54;
mptr->counter = 120786;
memcpy(mptr->data, data_arr, sizeof(mptr->data));

osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever);  // Send Message
osDelay(100);

mptr = osPoolAlloc(mpool);                     // Allocate memory for the message
mptr->voltage = 227.23;                        // Prepare a 2nd message
mptr->current = 12.41;
mptr->counter = 170823;

osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever);  // Send Message
osThreadYield();                               // Cooperative multitasking
   // We are done here, exit this thread
}
}

//
//  Thread 2: Receive thread
//
void recv_thread (void const *argument) {
  T_MEAS  *rptr;
  osEvent  evt;
   
  for (;;) {
   
evt = osMessageGet(MsgBox, osWaitForever);  // wait for message
   
if (evt.status == osEventMessage) {
HAL_GPIO_TogglePin(GPIOC, LED_3_Pin);
      rptr = evt.value.p;
      printf ("\nVoltage: %.2f V\n", rptr->voltage);
      printf ("Current: %.2f A\n", rptr->current);
     // printf ("Number of cycles: %d\n", rptr->counter);
      osPoolFree(mpool, evt.value);                  // free memory allocated for message
    }
  }

}



void StartApplication (void) {

  mpool = osPoolCreate(osPool(mpool));                 // create memory pool
  MsgBox = osMessageCreate(osMessageQ(MsgBox), NULL);  // create msg queue
   
  osThreadDef( send_threadTask, send_thread, osPriorityNormal, NULL, 512 );
  osThreadDef( recv_threadTask, recv_thread, osPriorityNormal, NULL, 512 );

  tid_thread1 = osThreadCreate(osThread(send_threadTask), NULL);
  tid_thread2 = osThreadCreate(osThread(recv_threadTask), NULL);
 
}


« Last Edit: November 22, 2018, 08:37:21 pm by diyaudio »
 

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: freeRTOS tasks with queue not working
« Reply #1 on: November 23, 2018, 07:59:06 am »
 Looks like I'm alone on this one, I will probably have to re-create and carefully check the overall heap sizes and get back on my progress.   
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: freeRTOS tasks with queue not working
« Reply #2 on: November 23, 2018, 08:17:47 am »
Looks like I'm alone on this one, I will probably have to re-create and carefully check the overall heap sizes and get back on my progress.   

If you are moving (big) data using messaging system of the RTOS, you are doing it wrong. Save data globally and transmit state of the data (or pointer or ID) between threads, not actual data itself. Most likely that's why you are not getting answers - because hardly anybody is messaging so many bytes as you.
 
The following users thanked this post: diyaudio

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: freeRTOS tasks with queue not working
« Reply #3 on: November 23, 2018, 09:49:55 am »
Looks like I'm alone on this one, I will probably have to re-create and carefully check the overall heap sizes and get back on my progress.   

If you are moving (big) data using messaging system of the RTOS, you are doing it wrong. Save data globally and transmit state of the data (or pointer or ID) between threads, not actual data itself. Most likely that's why you are not getting answers - because hardly anybody is messaging so many bytes as you.

Firstly thanks, you are right your answer tells me I need to re think my design. I will post another solution using a osPoolAlloc() I'm not sure if osPoolAlloc is the way to go to store global data, as its free osPoolFree(mpool, evt.value);  is causing it to hang.

 

 
 

Offline tsman

  • Frequent Contributor
  • **
  • Posts: 599
  • Country: gb
Re: freeRTOS tasks with queue not working
« Reply #4 on: November 23, 2018, 10:53:01 am »
The queues are allocated from the heap. Not the per task stack. You sure you've got enough space in there? Check the return value for osMessagePut.

What ogden said about not trying to pass large chunks of data around.
 
The following users thanked this post: diyaudio

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1920
  • Country: se
Re: freeRTOS tasks with queue not working
« Reply #5 on: November 23, 2018, 11:52:34 am »
... as its free osPoolFree(mpool, evt.value);  is causing it to hang.
Should that rather be osPoolFree(mpool, evt.value.p)?

If osPoolFree is expecting a void*, I wonder how it is able to even compile without error. :-//
Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: diyaudio

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: freeRTOS tasks with queue not working
« Reply #6 on: November 23, 2018, 09:12:21 pm »
it works now, but strange see the variation in osMessageQDef   

Code: [Select]

// ARM sample that doesn't work. !!!
osMessageQDef(MsgBox, 16, T_MEAS);

// audto generated using  the latest cubemx 5.0...  settings 256 queue size, 5 item size.  works!! (see the parameters generated compared to above)
osMessageQDef(myQueue01, 256, 5);

// modified ARM sample using sizeof works!!!
osMessageQDef(MsgBox, 16,  sizeof(T_MEAS) );


// yet macro osMessageQDef parameter type, already performs a sizeof() 
#define osMessageQDef(name, queue_sz, type)   \
const osMessageQDef_t os_messageQ_def_##name = \
{ (queue_sz), sizeof (type) }





 

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: freeRTOS tasks with queue not working
« Reply #7 on: November 23, 2018, 09:41:26 pm »
Looks like I'm alone on this one, I will probably have to re-create and carefully check the overall heap sizes and get back on my progress.   

If you are moving (big) data using messaging system of the RTOS, you are doing it wrong. Save data globally and transmit state of the data (or pointer or ID) between threads, not actual data itself. Most likely that's why you are not getting answers - because hardly anybody is messaging so many bytes as you.

 
If we look at the sample a pool allocates memory on the heap that is shared globally, the queue holds a pointer to the pool allocation see osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever);


Code: [Select]
T_MEAS    *mptr;
mptr = osPoolAlloc(mpool);                     // Allocate memory for the message
mptr->voltage = 223.72;                        // Set the message content
mptr->current = 17.54;
mptr->counter = 120786;
memcpy(mptr->data, data_arr, sizeof(mptr->data));

osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever);  // Send Message
osDelay(100);

mptr = osPoolAlloc(mpool);                     // Allocate memory for the message
mptr->voltage = 227.23;                        // Prepare a 2nd message
mptr->current = 12.41;
mptr->counter = 170823;

osMessagePut(MsgBox, (uint32_t)mptr, osWaitForever);  // Send Message



Code: [Select]
  T_MEAS  *rptr;
  osEvent  evt;
   
  for (;;) {
   
    evt = osMessageGet(MsgBox, osWaitForever);  // wait for message
   
     if (evt.status == osEventMessage) {

      rptr = evt.value.p;
      printf ("\nVoltage: %.2f V\n", rptr->voltage);
      printf ("Current: %.2f A\n", rptr->current);
      printf ("Number of cycles: %d\n", rptr->counter);
      osPoolFree(mpool, evt.value.p);                  // free memory allocated for message
    }
  }

 
The following users thanked this post: ogden


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->