1 / 40

Embedded Software

dr. Stefan Dulman Embedded Software Group. Embedded Software. TI2720-C. 3 . Software architectures. Lec.2: Interrupts & data sharing. volatile static long int lSecondsToday ; void interrupt vUpdateTime () { ++ lSecondsToday ; } long lGetSeconds () { long lReturn ;

connie
Télécharger la présentation

Embedded Software

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. dr. Stefan Dulman Embedded Software Group Embedded Software TI2720-C 3. Software architectures

  2. Lec.2: Interrupts & data sharing volatile static long intlSecondsToday; void interrupt vUpdateTime() { ++lSecondsToday; } long lGetSeconds() { long lReturn; lReturn = lSecondsToday; while (lReturn!=lSecondsToday) lReturn = lSecondsToday; return (lReturn); } lSecondsToday - previous lSecondsToday - current lReturn - desired lReturn - bad

  3. Exercise static intcErrors; void interrupt testint(void) { ++cErrors; } void test(void) { ++cErrors; } Does this code suffer from the data-sharing bug?

  4. ++cErrors; • Intel 80x86 INC (cErrors) RET • 8051 controller MOVE DPTR, #cErrors+01H MOVX A, @DPTR INC A MOVX @DPTR, A JNZ noCarry MOVE DPTR, #cErrors MOVX A,@DPTR INC A MOVX @DPTR, A noCarry: RET

  5. Outline • Embedded system architectures • Examples of architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Task switching X32

  6. Achitectures – the book • Problem (in the book): system response time • Solution: software architectures • Simple: systems with low load and few constraints • Complex: systems with high load and many constraints • Complex architectures introduce additional costs • Additional information (compulsory reading) Edward A. Lee - Embedded software, Advances in computers, vol. 56, 2002, p. 55-95 http://ptolemy.eecs.berkeley.edu/publications/papers/02/embsoft/embsoftwre.pdf

  7. Embedded software • Just software on small computers? • Well… • Timeliness (software takes time, physical processes) • Concurrency (network of sensors and actuators, tools) • Liveness (Turing machines) • Interfaces (temporal properties, combination of objects) • Heterogeneity (programming languages) • Reactivity (data streaming processing, adaptive)

  8. Models of computation • Definition: the laws governing component interactions • Recommended model supporting concurrency = ? • Sequential computation – Von Neumann architecture • Successive transformation of the system state • Distributed systems – system state unknown • System/modules can be expressed in various languages • VHDL + FPGA – reconfigurable components • Java – portability + security • C – efficient execution

  9. Examples of models • Dataflow • LabView; synchronous, boolean, dynamic … • Time triggered • clock, time triggered architecture, discrete time models, SystemC • Synchronous/reactive • Signals can have no value at various moments • Token ring protocol for media access control • Discrete events • Events (value, time stamp) • Process networks • Components = processes, asynchronous communication • Publish/subscribe mechanisms • Connections are event streams • Others: finite state machines, rendevous, continuous time…

  10. Publish-subscribe mechanisms • Each component • Create data from a set of data types • Consume data from a set of data types • Central entity • Data type associations with components • Buffers and distributed data to them • Architectures can be dynamically changed C1 Blackboard C2 C3 … C4

  11. Round-Robin architecture void main(void) { while(TRUE) { if ( !! I/O device A needs attention ) { !! Take care of device A and handle its data } if ( !! I/O device B needs attention ) { !! Take care of device B and handle its data } … if (!! I/O device Z needs attention) { !! Take care of device Z and handle its data } }

  12. RR Examples • Digital multimeter • Measure: resistance, current and potential • For each situation select the right scale • Washing machine • Airbag controller • Anything that displays information • …

  13. Round-Robin Evaluation • Simplicity! • No priorities, no interrupts, no data-sharing bugs • Suited for systems with no latency concerns • Worst response time of task code = sum of all task code • Alternative design: interleaving devices • Fragile design: timing in future versions? A B C D A B A C A D

  14. Outline • Embedded system architectures • Architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Task switching X32

  15. Round-Robin with Interrupts boolflagA=FALSE, flagB=FALSE, …, flagZ=FALSE; void interrupt handleA (void) { !! Take care of the I/O device A flagA = TRUE; } … void interrupt handleZ (void) { !! Take care of the I/O device Z flagZ = TRUE; } void main(void) { while(TRUE) { if (flagA) { flagA = FALSE !! Handle data from device A } if (flagB) … } }

  16. RR-ISR Characteristics • Splits the work between interrupts and main • Interrupts handle I/O of devices • Main function deals with processing data • Why use interrupts? • Allow fast response time for handling I/O buffers(event-based design vs. polling design) • Set flags to indicate work has to be done • Take advantage of the interrupt-priority system • Often the most appropriate architecture!

  17. RR-ISR Examples • Systems with few components needing fast response time (limited processing needed) • Most likely to be found in: • Stopwatches • Modern washing machines • Coffee machines • Microwave ovens • Central heating units • Traffic light controllers • Other: data-bridge devices and barcode scanners

  18. RR-ISR Evaluation • Leads to a simple design (still…) • Data-sharing problem introduced • All task codes have the same priority - no long tasks!!! • Worst response time = sum of all task code + ISR times • Alternative designs: • Move code in interrupts • Interleaving devices in main() • Fragile design: timing in future versions? • Changing code or ISR priority for one device…

  19. Outline • Architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Task switching X32

  20. Function-Queue Architecture !! Queue of function pointers void interrupt handleA(void) { !! Take care of I/O device A !! Put functionA on queue of function pointers } !! ISR for devices B…Z void functionA(void) { !! Handle actions required by A } !! Functions for devices B…Z void main(void) { while(TRUE) { while (!! Queue of function pointers is empty) {}; !! Call first function on the queue } }

  21. FQ Evaluation • Splitting of responsibilities similar to RR-ISR • Interrupts handle I/O of devices • Main function deals with processing data (via functions) • Insert operation for the queue was not specified! • FIFO queue leads to RR-ISR • Priority queue leads to task priorities • FQ architecture gives the basis for “non-preemptive OSes” • Worst response time = longest task + tasks with higher priority + ISR routines

  22. Non-preemptive scheduling • First-in First-out scheduling algorithm • Shortest-Job-First scheduling • Shortest-Remaining-Time Scheduling • Priority scheduling • Multilevel queue scheduling

  23. Performance comparison RR RR-ISR FQS High priority Dev. A ISR Dev. A ISR Dev. B ISR Dev. B ISR … … Dev. Z ISR Dev. Z ISR everything Task code Task code A Task code B … Task code Z Low priority

  24. Outline • Embedded system architectures • Architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Task switching X32

  25. Real Time OS Architecture • Characteristics • Evolved from the FQ Architecture • Contains a set of ISR (dealing with fast events from devices) • Contains a set of tasks (one or more for each device) • Signaling between ISRs and tasks is part of the OS • No more while(1) loops in the main code –> OS scheduler • OS can stop a task to run another one (preemption) • Terminology!

  26. RTOS Evaluation • System response is relatively stable • Changing a piece of code has a reduced effect (changing lower-priority tasks does not matter) • RTOSes consume time themselves • Synchronization mechanisms • Task preemption • Better response time vs. throughput • Large number of RTOSes developed • They come with an increasing set of tools

  27. Task Synchronization • Global data ( data sharing problem) • Semaphores • pend (semaphore, WAIT_FOREVER, err)  block task • post (semaphore)  unblock task • Queues, Mailboxes, and Pipes • Synchronization + data communication • No shared data problems • unless you pass pointer to the original data in other task (cf. Fig. 7.4) • Same pitfalls as semaphores (deadlock, etc.)

  28. FIFO Queue • Must be initialized#define MSGQ_SIZE 10 // max no of messages OS_EVENT* myMsgQ; // msg-Queue void* myMsgQdata[MSGQ_SIZE]; // storage for msgs … myMsgQ = OSQCreate (myMsgQdata, MSGQ_SIZE); … • Can be used by the reading task msg = (char*) OSQPend(myMsgQ, WAIT_FOREVER, &err); • Can be used by the writing task OSQPost(myMsgQ, (void*)msg); (uC/OS, book pp. 333)

  29. Mailbox • Similar to queue • Create, write, read • But also check, destroy • Some RTOS provide priorities in mailboxes • But, … • Only 1 message (under X32) • No blocking on the reader side (Queue WAIT_FOREVER  timeout) void* OSMboxAccept (OSEvend* mbox); • Blocking OSMboxPend ( … )

  30. Pipe (not in X32 uC/OS) • Similar to queue and mailbox • But, … • Variable length messages

  31. Queue vs. Mailbox vs. Pipe • Queues for • FIFO communication • Mailboxes for • non-blocking communication • Communication according to priorities • Pipes for • Variable length messages • Huge overhead compared with global variables!

  32. Task Synchronization Pitfalls • Typically no protection of queues, mailboxes, pipes • Any task can use them any time • Is the right task writing to/reading from the right communication channel? • Pointer problems • Passing pointers to a communication channel can create data sharing problems • Type and length of messages • Write an integer/read a byte! • Potential memory leaks • Malloc/free • Space problems • What if queue is full?  some recovery mechanism!

  33. Choosing the right architecture • Select the simplest one! Think of the model first… • Writing code for ES is complicated enough • Remember: customers will want more features in version 2! • Use an RTOS if the system needs real time response • Make use of the offered tools rather than reinventing • Your system will most likely require a hybrid architecture

  34. Outline • Embedded system architectures • Architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Task switching X32

  35. Task switching • Switching from current context (PC, stack, registers) to another • Context • Thread identity -> multithreading • Needs two constructs: • Initialize a context • Switch to a context • Often used (standard C library): setjmp, longjmp • X32 void *stack[1024]init_stack(stack, (void*)task, (void *)0);context_switch(new_context, &old_context)

  36. Simple example – X32 void **thread_main, **thread_a; void *stack_a[1024]; int main(void) { thread_a = init_stack(stack_a, task_a); printf("now in thread_main\r\n"); context_switch(thread_a, &thread_main); printf("back in main thread\r\n"); } void task_a(void) { printf("now in thread_a\r\n"); context_switch(thread_main, &thread_a); }

  37. Time Slicing Example (1) void **thread_main, **thread_a; void *stack_a[1024]; intthread_id; void isr_timer(void) { if (thread_id==0) { thread_id = 1; context_switch(thread_a, &thread_main); } else { thread_id = 0; context_switch(thread_main, &thread_a); } }

  38. Time Slicing Example (2) int main(void) { thread_a = init_stack(stack_a, task_a); thread_id = 0; // now in main !! set timer to interrupt every 10ms while(true) printf("now in thread_main\r\n"); } void task_a(void) { while(TRUE) printf("now in thread_a\r\n"); }

  39. Conclusions • Architectures: • Round-Robin • Round-Robin with Interrupts • Function-Queue • Real-Time Operating Systems • Some task synchronization mechanisms • Task switching X32 • Conclusion: • Choose the simplest architecture for your system • Keep in mind that design requirements will change

  40. X32: Demo • Demo .. • (x32_projects.tgz, fqs.c, rrobin.c, rrobin2.c, rrobinISR.c, slicing.c)

More Related