/** * The first protothread function. A protothread function must always * return an integer, but must NEVER explicitly return - returning is * performed inside the protothread statements. * * The protothread function is driven by the main loop further down in * the code. */ static PT_THREAD (protothread1(struct pt *pt)) { // mark beginning of thread PT_BEGIN(pt); /* We loop forever here. */ while(1) { //stop until thread 2 signals PT_SEM_WAIT(pt, &control_t1); // put a 2 microsec pulse on the debug pin with amplitude 3 PT_DEBUG_VALUE(3, 2) ; // toggle a port pin mPORTAToggleBits(BIT_0); // tell thread 2 to go PT_SEM_SIGNAL(pt, &control_t2); // Allow thread 3 to control blinking // thru command interface PT_YIELD_UNTIL(pt, cntl_blink) ; // This is a locally written macro using timer5 to count millisec // to program a yield time PT_YIELD_TIME_msec(wait_t1) ; // NEVER exit while } // END WHILE(1) // mark end the thread PT_END(pt); } // thread 1
static PT_THREAD(producer(struct pt *pt)) { static int produced; PT_BEGIN(pt); for(produced = 0; produced < NUM_ITEMS; ++produced) { PT_SEM_WAIT(pt, &full); add_to_buffer(produce_item()); PT_SEM_SIGNAL(pt, &empty); } PT_END(pt); }
static PT_THREAD(consumer(struct pt *pt)) { static int consumed; PT_BEGIN(pt); for(consumed = 0; consumed < NUM_ITEMS; ++consumed) { PT_SEM_WAIT(pt, &empty); consume_item(get_from_buffer()); PT_SEM_SIGNAL(pt, &full); } PT_END(pt); }
// === Thread 2 ====================================================== // static PT_THREAD (protothread2(struct pt *pt)) { PT_BEGIN(pt); while(1) { //stop until thread 1 signals PT_SEM_WAIT(pt, &control_t2); // put a 5 microsec pulse on the debug pin with amplitude 6 PT_DEBUG_VALUE(6, 5) ; // toggle a port mPORTAToggleBits(BIT_1); // tell thread 1 to go PT_SEM_SIGNAL(pt, &control_t1); // NEVER exit while } // END WHILE(1) PT_END(pt); } // thread 2