void task_3(void) { uint32 status; OS_bin_sem_prop_t bin_sem_prop; int counter = 0; OS_printf("Starting task 3\n"); OS_TaskRegister(); OS_printf("TASK 3: Waiting on the semaphore\n"); status = OS_BinSemTake(bin_sem_id); if ( status != OS_SUCCESS ) { OS_printf("TASK 3: Error calling OS_BinSemTake\n"); } else { status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); OS_printf("TASK 3: out of BinSemTake: %d\n",(int)bin_sem_prop.value); } while(1) { OS_TaskDelay(1000); OS_printf("TASK 3: Doing some work: %d\n", (int)counter++); } }
static void test_004_004_execute(void) { /* [4.4.1] OS_BinSemTake() is invoked with sem_id set to -1, an error is expected.*/ test_set_step(1); { int32 err; err = OS_BinSemTake((uint32)-1); test_assert(err == OS_ERR_INVALID_ID, "invalid sem_id not detected"); } }
void task_1(void) { uint32 status; OS_bin_sem_prop_t bin_sem_prop; int printf_counter = 0; OS_printf("Starting task 1\n"); OS_TaskRegister(); OS_printf("Delay for 1 second before starting\n"); OS_TaskDelay(1000); while(1) { status = OS_BinSemTake(bin_sem_id); if ( status != OS_SUCCESS ) { OS_printf("TASK 1:Error calling OS_BinSemTake\n"); exit(-1); } else { printf_counter++; counter++; if ( printf_counter > 100 ) { OS_printf("TASK 1: counter:%d timer_counter:%d\n", (int)counter,(int)timer_counter); printf_counter = 0; } status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); if ( bin_sem_prop.value > 1 ) { OS_printf("Error: Binary sem value > 1 ( in task):%d !\n",(int)bin_sem_prop.value); exit(-1); } else if ( bin_sem_prop.value < -1 ) { OS_printf("Error: Binary sem value < -1 ( in task):%d !\n",(int)bin_sem_prop.value); exit(-1); } } } }
/* ** Main function */ void OS_Application_Startup(void) { uint32 status; OS_bin_sem_prop_t bin_sem_prop; OS_API_Init(); OS_printf("OS Application Startup\n"); /* ** Create the binary semaphore */ status = OS_BinSemCreate( &bin_sem_id, "BinSem1", 1, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Binary Sem\n"); } else { status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); OS_printf("Binary Sem ID = %d, value = %d\n", (int)bin_sem_id, (int)bin_sem_prop.value); } /* ** Take the semaphore so the value is 0 and the next SemTake call should block */ status = OS_BinSemTake(bin_sem_id); if ( status != OS_SUCCESS ) { OS_printf("Error calling OS_BinSemTake with bin_sem_id = %d\n",(int)bin_sem_id); } else { status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); OS_printf("Initial Binary Sem Take: value = %d\n", (int)bin_sem_prop.value); } /* ** Create the tasks */ status = OS_TaskCreate( &task_1_id, "Task 1", task_1, task_1_stack, TASK_STACK_SIZE, TASK_1_PRIORITY, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Task 1\n"); } else { OS_printf("Created Task 1\n"); } status = OS_TaskCreate( &task_2_id, "Task 2", task_2, task_2_stack, TASK_STACK_SIZE, TASK_2_PRIORITY, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Task 2\n"); } else { OS_printf("Created Task 2\n"); } status = OS_TaskCreate( &task_3_id, "Task 3", task_3, task_3_stack, TASK_STACK_SIZE, TASK_3_PRIORITY, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Task 3\n"); } else { OS_printf("Created Task 3\n"); } /* ** Delay, then release the semaphore */ OS_TaskDelay(2000); status = OS_BinSemFlush(bin_sem_id); OS_printf("Main done!\n"); }
void OS_Application_Startup(void) { uint32 status; OS_bin_sem_prop_t bin_sem_prop; OS_printf("OS Application Startup\n"); /* ** Create the binary semaphore */ status = OS_BinSemCreate( &bin_sem_id, "BinSem1", 1, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Binary Sem\n"); } else { status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); OS_printf("Binary Sem ID = %d, value = %d\n", (int)bin_sem_id, (int)bin_sem_prop.value); } /* ** Create a timer */ status = OS_TimerCreate(&timer_id, "Timer 1", &timer_accuracy, &(TimerFunction)); if ( status != OS_SUCCESS ) { OS_printf("Error creating OS Timer\n"); } else { OS_printf("Timer ID = %d\n", (int)bin_sem_id); OS_printf("Timer Accuracy = %d microseconds \n",(int)timer_accuracy); } /* ** Take the semaphore so the value is 0 and the next SemTake call should block */ status = OS_BinSemTake(bin_sem_id); if ( status != OS_SUCCESS ) { OS_printf("Error calling OS_BinSemTake with bin_sem_id = %d\n",(int)bin_sem_id); } else { status = OS_BinSemGetInfo (bin_sem_id, &bin_sem_prop); OS_printf("Initial Binary Sem Take: value = %d\n", (int)bin_sem_prop.value); } /* ** Create the "consumer" task. */ status = OS_TaskCreate( &task_1_id, "Task 1", task_1, task_1_stack, TASK_1_STACK_SIZE, TASK_1_PRIORITY, 0); if ( status != OS_SUCCESS ) { OS_printf("Error creating Task 1\n"); } else { OS_printf("Created Task 1\n"); } /* ** Start the timer */ status = OS_TimerSet(timer_id, timer_start, timer_interval); if ( status != OS_SUCCESS ) { OS_printf("Error calling OS_TimerSet: ID = %d\n", (int)timer_id); } else { OS_printf("Timer programmed\n"); } }