Hi,
I've modified the mutex example for MQX 3.8.1 to have 4 instances of the Print_task(). I'm trying to add a time delay to the task depending on
the "tag" I specified to be the first character of the string and depending on that value it would create a time delay for that instance before sending out.
Is this a code problem or is it a conceptual problem because I'm unable to see the variable data while stepping through the code for both "initial_data" and "tag" variables. I've attached some screenshots to show what I mean.
The reason I'm doing this is because I plan on creating a code with GPIOS that send the data from each GPIO with a time delay depending on which GPIO it is coming from. Any advice on this subject would be greatly helpful in my project.
Thanks,
Khoa Nguyen
* $FileName: main.c$
* $Version : 3.8.11.0$
* $Date : Oct-4-2011$
*
* Comments:
*
* This file contains the source for the mutex example program.
*
*END************************************************************************/
#include <mqx.h>
#include <bsp.h>
#include <mutex.h>
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
#if ! MQX_HAS_TIME_SLICE
#error This application requires MQX_HAS_TIME_SLICE defined non-zero in user_config.h. Please recompile kernel with this option.
#endif
/* Task IDs */
#define MAIN_TASK 5
#define PRINT_TASK 6
extern void main_task(uint_32 initial_data);
extern void print_task(uint_32 initial_data);
const TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
/* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice */
{ MAIN_TASK, main_task, 1000, 8, "main", MQX_AUTO_START_TASK, 0, 0 },
{ PRINT_TASK, print_task, 1000, 9, "print", MQX_TIME_SLICE_TASK, 0, 3 },
{ 0 }
};
MUTEX_STRUCT print_mutex;
/*TASK*--------------------------------------------------------
*
* Task Name : main_task
* Comments : This task creates a mutex and then two
* instances of the print task.
*END*--------------------------------------------------------*/
void main_task
(
uint_32 initial_data
)
{
MUTEX_ATTR_STRUCT mutexattr;
char* string1 = "1 ONE task\n";
char* string2 = "2 TWO task\n";
char* string3 = "3 THREE task\n";
char* string4 = "4 FOUR task\n";
/* Initialize mutex attributes */
if (_mutatr_init(&mutexattr) != MQX_OK) {
printf("Initialize mutex attributes failed.\n");
_task_block();
}
/* Initialize the mutex */
if (_mutex_init(&print_mutex, &mutexattr) != MQX_OK) {
printf("Initialize print mutex failed.\n");
_task_block();
}
/* Create the print tasks */
_task_create(0, PRINT_TASK, (uint_32)string1);
_task_create(0, PRINT_TASK, (uint_32)string2);
_task_create(0, PRINT_TASK, (uint_32)string3);
_task_create(0, PRINT_TASK, (uint_32)string4);
_task_block();
}
/*TASK*--------------------------------------------------------
*
* Task Name : print_task
* Comments : This task prints a message. It uses a mutex to
* ensure I/O is not interleaved.
*END*--------------------------------------------------------*/
void print_task
(
uint_32 initial_data
)
{
int tag;
while(TRUE) {
if (_mutex_lock(&print_mutex) != MQX_OK) {
printf("Mutex lock failed.\n");
_task_block();
}
tag = (int)initial_data; // <--------------------------------------------------------------------------------- unable to see tag or initial_data (in screenshots)
switch(tag){
case 5:
_time_delay(1000);
break;
case 2:
_time_delay(2000);
break;
case 3:
_time_delay(3000);
break;
case 4:
_time_delay(4000);
break;
}
_io_puts((char *)initial_data);
_mutex_unlock(&print_mutex);
}
}
/* EOF */