Best way to pass simple data into a task

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Best way to pass simple data into a task

703 次查看
mittymcdoogle
Contributor III

I have a question about the best way to pass data into a task. I understand there is a message system for tasks, but I think the message system may be a bit overkill for what I'm trying to do. I have a board with multiple temperature sensors and I select which temperature sensor to read data from through a parameter I pass to 'temperatureRead' (see highlighted in red below). For example, to read temperature data from device 0, I call 'temperatureRead( 0 )'. To read temperature data from device 1, I call 'temperatureRead( 1 )'.

My question is whether or not my use of a global variable to specify the device (device 0, 1, 2, 3, 4, etc.) is the best way to accomplish what I am trying to accomplish. I like to avoid using global variables, but I also want a solution that is relatively simple.

void Temperature_task( uint32_t initial_data)

{

  void *event_ptr; // Events are used to block tasks when not needed

  if (_event_open("event.global", &event_ptr) != MQX_OK)

  {

  //TODO error message

  }

  while(TRUE)

  {

  // Go onto standby until a temp event is triggered

  if (_event_wait_all_ticks(event_ptr, TEMP_EVENT_MASK, 0) != MQX_OK)

  {

  printf("\nEvent Wait failed");

  _task_block();

  }

  // Lock the MUTEX to prevent too much communication across the I2C bus

  if (_mutex_lock(&i2c_mutex) != MQX_OK)

  {

  printf("Mutex lock failed.\n");

  _task_block();

  }

  temperatureRead( globalVariable );

  _mutex_unlock(&i2c_mutex); // Unlock the I2C MUTEX so that other tasks can use the I2C bus

  // Clear the event

  if (_event_clear(event_ptr, TEMP_EVENT_MASK) != MQX_OK)

  {

  //TODO error message

  } // end if( _event ...

  } // end while(TRUE)

} // end Temperature_task

Thanks,

Mitty

标记 (4)
0 项奖励
回复
1 回复

498 次查看
Carlos_Musich
NXP Employee
NXP Employee

Hi Mitty,

the Mutex example shows how to pass parameters form a task to another. Please refer to the attached file.

If you have any question please let me know.

Regards,

Carlos

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复