1 / 30

Microcontrollers, Advanced Advanced Challenges With Real-Time Systems

Microcontrollers, Advanced Advanced Challenges With Real-Time Systems . January 30, 2012 Jack Ganssle. Design for Speed. Keep ISRs Short! Avoid loops Remove all unneeded code Understand the cost of a LOC. Hart 7.3 digit cos(x):.

nelly
Download Presentation

Microcontrollers, Advanced Advanced Challenges With Real-Time Systems

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. Microcontrollers, Advanced Advanced Challenges With Real-Time Systems January 30, 2012Jack Ganssle

  2. Design for Speed Keep ISRs Short! Avoid loops Remove all unneeded code Understand the cost of a LOC

  3. Hart 7.3 digit cos(x): a=.9999999523 - .4999990534 * b**2 + .04166358 * b**4 - .001385370 * b**6 + .000023233 * b**8 Computer Approximations, by John Hart

  4. A Commercial Product Micro Digital’s GoFast: www.smxrtos.com For NIOS-II processor at 24 MHz, times in microseconds: double precision single precision GoFast GCC GoFast GCC cos 16.0 120.9 4.0 38.4 acos 32.2 201.5 9.6 59.6 pow 43.5 542.0 10.6 163.6 Note GoFast is fully reentrant.

  5. Reentrancy Problems • A function is reentrant if: • If it uses all shared variables in an atomic way, • If it does not call non-reentrant functions • If it does not use the hardware in a non-atomic • way

  6. Shared Variable Perils Using statics or globals non-atomically makes the code non-reentrant. void function(int *data) { count=data*2; data=count; } int count;

  7. Shared Variable Perils Using statics or globals non-atomically makes the code non-reentrant. void function(int *data) { count=data*2; data=count; } static int count;

  8. Shared Variable Perils Atomic operations may not be atomic. int data; void function() { ++data; }

  9. Shared Variable Perils mov cx,[bx] add cx,1 mov [bx],cx Atomic alternative: inc [bx] lock

  10. The Danger of DI long i; void do_something(void) { disable_interrupts(); i+=0x1234; enable_interrupts(); } NO! long i; void do_something(void) { int key; key=disable_interrupts(); i+=0x1234; restore_interrupts(key); } Better

  11. Using a Handshake Flag while (in_use); //wait till resource free in_use=TRUE; //set resource busy Do non-reentrant stuff in_use=FALSE; //set resource available Bad Code! An interrupt between the first two statements may cause two sections of code to think they both have exclusive access to the shared resource.

  12. TSET Substitute loop: mov al,0 ; 0 means “in use” lock xchg al,variable cmp al,0 je loop ; loop if in use If al=0, we swapped 0 with zero; nothing changed but the code loops since someone else is using the resource. If al=1, we put a 0 into the “in use” variable, marking the resource as busy. We fall out of the loop, now having control of the resource. On some ARM processors, use LDREX/STREX

  13. Calling non-reentrant Routines Calling a non-reentrant function makes the caller non-reentrant. Be wary of runtime packages, and purchased code.

  14. Non-atomic Hardware Accesses If you can’t manage a hardware resource atomically, then the code is non-reentrant. /* swap peripheral modes*/ peripheral_reg_1=0xaa; peripheral_reg_2=0x55; peripheral_reg_2=0x22;

  15. Async Hardware/Software Variable timer_hi Hardware timer register High 16 bits Low 16 bits ++timer_hi Overflow of timer register ISR

  16. Async Hardware/Software int timer_hi; interrupt timer() { ++timer_hi; } long timer_read(void) { unsigned int low, high; low =inword(hardware_register); high=timer_hi; return (((ulong)high<<16) + (ulong)low); } Bad code! See next slide….

  17. Async Hardware/Software long timer_read(void) { unsigned int low, high; (hardware_register=ffff, timer_hi=0000) low =inword(hardware_register); (overflow; low=ffff, timer_hi=0001) high=timer_hi; return (((ulong)high<<16) + (ulong)low); (returns 0001ffff) } Bad code! As you can see, an interrupt may corrupt the result.

  18. long timer_read(void) { unsigned int low, high; push_interrupt_state; disable_interrupts; low=inword(hardware_register); high=timer_hi; if(timer_overflow) {++high; low=inword(hardware_register);} pop_interrupt_state; return (((ulong)high)<<16 + (ulong)low); }

  19. Input Capture Register 32 bit counter Register Bits 16-31 To CPU Bits 0-15 Data hold clock Metastable design! Will surely fail

  20. Speed Kills Required reading: High Speed Digital Design (a Handbook of Black Magic) by Howard Johnson and Martin Graham (1993 PTR Prentice Hall, NJ)

  21. Speed is a function of the edges, not clock rate.

  22. Power Spectrum Tr = signal’s rise time F = All frequencies higher than F are 40 dBV down in amplitude If Tr = 20 nsec, F= 25 MHz If Tr = 1 nsec, F= 500 MHz

  23. Bouncing in a 10 Inch wire TP2 - after 10” of wire TP1 - driving signal

  24. Now Terminated TP4 TP3

  25. The Tek TPP1000 Probe

  26. Common Impedance Problems • ALE • Edge sensitive interrupts (e.g., NMI) • All signals going off-board • Clock - particular problem as it goes all over the typical board. Few CPUs accept TTL clock signals; many won’t tolerate anything less than a perfect clock.

  27. Resources An Embedded Software Primer by David E. Simon 1999, Addison Wesley Longman ISBN 0-201-61569-X MicroC/OS-II by Jean J. LaBrosse 1999, Miller Freeman ISBN 0-87930-543-6 http://embedded.com/design/205203908 - Great multicore article MicroC/OS-III by Jean J. LaBrosse - MicroC/OS-III http://www.ecoscentric.com - ecos

  28. Questions?

More Related