Embedded Systems Design Europe - May 2008 - (Page 16) cover feature Listing 2 How processTasks() might be implemented. loop through tasks if (cooperative_Tasks[i].ready > 0) { returnVal = (*cooperative_Tasks[i].pTask) (cooperative_Tasks[i].param); cooperative_Tasks[i].ready--; if (cooperative_Tasks[i].reload == 0) cooperative_deleteTask (i); } Listing 3 Example of a very simple Protothreads “thread.” Static PT_THREAD(example(struct pt *pt)) { PT_BEGIN(pt); while(1) { PT_WAIT_UNTIL(pt, counter == 1000); printf(“Threshold reached\n”); counter = 0; } PT_END(pt); } Task execution is performed outside of ISRs, which means all threads run in the same context minimizing needs for concurrency control mechanisms. As all of the scheduler code is written in ANSI C and the only hardware dependency is the tick, one can quickly see how easy it would be to port this to a new target. Although an excellent explanation of Protothreads internals can be found on the Protothreads website at www. sics.se/~adam/pt/, minimal information is needed to get up and running quickly. Protothreads are stackless threads that provide conditional blocking. The conditional blocking is accomplished using what some have called a “dodgy” feature of C: many C compilers, including GNU, allow for a nonconventional switch/case construction; a while loop placed in the middle of the switch. Listing 3 and 4 shows the breakdown example. Notice how the PT_BEGIN simply maps to the beginning of the switch/case: #define PT_BEGIN(pt) switch(pt->lc) { case 0: switch/case, cleans up, and returns control to the scheduler. #define PT_END(pt) >lc = 0; return 2; } pt- (Source for Listing 3 and 4: www.sics.se/~adam/pt/expansion.html) Each time the function or “thread” is called, execution will resume exactly where the thread blocked. The location is determined from the line number at compilation time. See the expansion of PT_WAIT_UNTIL: #define PT_WAIT_UNIT(pt, c) pt->lc = __LINE__;case __LINE__:\ If (!(c)) return 0 Finishing up with PT_END closes the 16 Creation of a thread usListing 4 The simple thread with ing Protothreads is simple but macros expanded. requires a few steps as shown in Static the code example in Listing 5. char example(struct pt *pt) Each thread must be declared { switch(pt->lc) { case 0: using the PT_THREAD macro. Each thread must receive a while(1) { pt struct point as a parampt->lc = 12; case 12: if(!(counter == 1000)) return 0; eter. Each thread also requires printf(“Threshold reached\n”); PT_INIT(&pt) to be invoked to counter = 0; configure the context the first } time. } pt->lc = 0; return 2; A PT_BEGIN(pt) is re} quired at the beginning of the thread and a PT_END(pt) is required at the very end. In the virtual concurrency and the cooperamiddle of the thread the implementer tive scheduler used to allow task execuhas a variety of macros to choose from tion to be scheduled. to cause the thread to yield. Listing 6 shows a complete blendThe complete application proing of an application using Protogramming interface, examples, more threads and the cooperative scheduler. implementation details can all be The task control block in this case (pt) found on the Protothreads website. Several threads can be created and is defined by Protothreads and still executed. A simple main program for needs to be initialized before use, by the example above could be like the one invoking the PT_INIT macro. After shown in Listing 5. that, the tasks are created exactly as The infinite loop in main is continexplained above in the cooperative uously executing all of the threads. Each scheduler. time a thread is reentered from main, The tasks will run just fine using code execution will begin from where Protothreads alone, but by adding the the thread had previously yielded, just cooperative scheduler, higher prioras one would expect. Notice how the ity threads can be created that execute data is stored in global variables. more often than lower priority. Both the cooperative scheduler and Since scheduling is based and ocProtothreads can be useful for a variety curs on a tick, a device can even go into of jobs, with Protothreads providing sleep mode and conserve battery life. MAY 2008 | embedded systems design europe | www.embedded.com/europe http://www.sics.se/~adam/pt/expansion.html http://www.sics.se/~adam/pt/ http://www.sics.se/~adam/pt/ http://www.embedded.com/europe
Table of Contents Feed for the Digital Edition of Embedded Systems Design Europe - May 2008 Embedded Systems Design Europe - May 2008 Contents Microsoft Provides Embedded Roadmap Enea Buys Developers Irish Start-Up Raises Funds for Telecom FPGAs Kontron Promotes COM Express Nano Mentor Nucleus Platform Provides UI for Atmel Small Form Factor Boards Head for the SUMIT Proffibus Advances IO-Link Integration Embedded Developers Cautious on Multicore Auto Cooperation Improves Test Altera Launches DO-254 Partner Network Building an ‘Instant-Up’ Real-Time Operating Systems An Architecture for Reusable Embedded Systems Software Free up Bandwidth in PCI Express Evaluating Software in Medical Devices Circuit Sensitivity in Analog Circuits Choosing Flash Memory New Products Advertising Contacts Embedded Systems Design Europe - May 2008 Embedded Systems Design Europe - May 2008 - Embedded Systems Design Europe - May 2008 (Page 1) Embedded Systems Design Europe - May 2008 - Embedded Systems Design Europe - May 2008 (Page 2) Embedded Systems Design Europe - May 2008 - Contents (Page 3) Embedded Systems Design Europe - May 2008 - Contents (Page 4) Embedded Systems Design Europe - May 2008 - Contents (Page 5) Embedded Systems Design Europe - May 2008 - Microsoft Provides Embedded Roadmap (Page 6) Embedded Systems Design Europe - May 2008 - Microsoft Provides Embedded Roadmap (Page 7) Embedded Systems Design Europe - May 2008 - Irish Start-Up Raises Funds for Telecom FPGAs (Page 8) Embedded Systems Design Europe - May 2008 - Irish Start-Up Raises Funds for Telecom FPGAs (Page 9) Embedded Systems Design Europe - May 2008 - Mentor Nucleus Platform Provides UI for Atmel (Page 10) Embedded Systems Design Europe - May 2008 - Mentor Nucleus Platform Provides UI for Atmel (Page 11) Embedded Systems Design Europe - May 2008 - Proffibus Advances IO-Link Integration (Page 12) Embedded Systems Design Europe - May 2008 - Altera Launches DO-254 Partner Network (Page 13) Embedded Systems Design Europe - May 2008 - Building an ‘Instant-Up’ Real-Time Operating Systems (Page 14) Embedded Systems Design Europe - May 2008 - Building an ‘Instant-Up’ Real-Time Operating Systems (Page 15) Embedded Systems Design Europe - May 2008 - Building an ‘Instant-Up’ Real-Time Operating Systems (Page 16) Embedded Systems Design Europe - May 2008 - Building an ‘Instant-Up’ Real-Time Operating Systems (Page 17) Embedded Systems Design Europe - May 2008 - An Architecture for Reusable Embedded Systems Software (Page 18) Embedded Systems Design Europe - May 2008 - An Architecture for Reusable Embedded Systems Software (Page 19) Embedded Systems Design Europe - May 2008 - An Architecture for Reusable Embedded Systems Software (Page 20) Embedded Systems Design Europe - May 2008 - An Architecture for Reusable Embedded Systems Software (Page 21) Embedded Systems Design Europe - May 2008 - Free up Bandwidth in PCI Express (Page 22) Embedded Systems Design Europe - May 2008 - Free up Bandwidth in PCI Express (Page 23) Embedded Systems Design Europe - May 2008 - Free up Bandwidth in PCI Express (Page 24) Embedded Systems Design Europe - May 2008 - Free up Bandwidth in PCI Express (Page 25) Embedded Systems Design Europe - May 2008 - Evaluating Software in Medical Devices (Page 26) Embedded Systems Design Europe - May 2008 - Evaluating Software in Medical Devices (Page 27) Embedded Systems Design Europe - May 2008 - Evaluating Software in Medical Devices (Page 28) Embedded Systems Design Europe - May 2008 - Evaluating Software in Medical Devices (Page 29) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 30) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 31) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 32) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 33) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 34) Embedded Systems Design Europe - May 2008 - Circuit Sensitivity in Analog Circuits (Page 35) Embedded Systems Design Europe - May 2008 - Choosing Flash Memory (Page 36) Embedded Systems Design Europe - May 2008 - Choosing Flash Memory (Page 37) Embedded Systems Design Europe - May 2008 - Choosing Flash Memory (Page 38) Embedded Systems Design Europe - May 2008 - Choosing Flash Memory (Page 39) Embedded Systems Design Europe - May 2008 - New Products (Page 40) Embedded Systems Design Europe - May 2008 - New Products (Page 41) Embedded Systems Design Europe - May 2008 - New Products (Page 42) Embedded Systems Design Europe - May 2008 - Advertising Contacts (Page 43) Embedded Systems Design Europe - May 2008 - Advertising Contacts (Page 44)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.