240 likes | 253 Views
Learn about the challenges and solutions in embedded programming, including real-time issues, concurrency, and interaction with hardware.
E N D
IN2305-IIEmbedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.
Arjan van Gemund? • BS Physics Engineering • MS, PhD Computer Science • DSM (Embedded Systems HW + SW) • TNO (High-Performance Computing) • TUD (EE, Computer Architecture) • TUD (CS, Software Technology, 0.4 fte Prof Emb SW) • 2 kids, dog, cat, living in Ravenswoud (Fr) • http://www.st.ewi.tudelft.nl/~gemund/
In2305-ii? Info: viahttp://www.st.ewi.tudelft.nl/~gemund/
printer printing system user I/F netw I/F Embedded Systems? ES = computer system (HW+SW) embedded within another system largely determining its functionality
Telegraph Telegraph • out-of-order data • negotiate with multiple clients (print jobs) • service status requests • adapt to different printers • response time to certain requests • data throughput / buffering network printer
Underground Tank Monitoring System • guard levels, detect leaks • extremely low-cost design (proc) • simple arithmetic - CPU hog - response time problem Emb Sys level CH level H2O temperature . . . tank 1 buttons LCD disp printer tank N
speedo meter gas pedal buttons SSD Cruise Control System (Lab Project) • stabilize car speed when engaged • extremely low processor cycle budget • small control loop jitter due to other activities • reliable operation Emb Sys
Embedded Systems Boom • provides functionality (intelligence) of almost everything • annual growth 25-60% (Emb Linux > 60%) • 100 x PC market • accounts for 25-40% costs in automotive • very large societal dependence • very high performance demands
Embedded Software Crisis • functionality migrates from HW to SW • existing cores combined with FPGA’s, rather than ASICs • programming-centered design (incl. HDLs) • TV, mobile, car, .. 10+ MLOC code, expon. growth! • despite SW engineering: 1 – 10 bug / KLOC • 100 G$ / yr on bugs (Mars Polar Lander, Mars Climate Orbiter, Ariane 5, Patriot, USS Yorktown, Therac-25, ... )
Embedded Programming • more difficult than “classical” programming • interaction with hardware • real-time issues (timing) • concurrency (multiple threads, scheduling, deadlock) • need to understand underlying RTOS principles • event-driven programming (interrupts) • lots of (novice) errors (hence the crisis) • so that’s why we have this course already in 2nd year
Example • naïve automatic door task (thread): for (;;) { while (inp(sensor) != 1) ; // wait to open out(door,OPEN); while (inp(sensor) == 0) ; // wait to close sleep(1000); out(door,CLOSE); // close after timeout } • what are the (many) issues? • (discussed on the next slides)
s’ s s’ s s’ 0 1 2 s t.s’ Specification: FSM (Moore) 0: door’, timer_enable’ 1: door, timer_enable’ 2: door, timer_enable s = sensor, t = timeout e tmr t clk • red transition absent in example code • -> door can slam into your face!
How to Program? • VHDL: FSM in entitydoor_controller • pros: • separate hardware: no problems sharing a processor with other tasks (scheduling, priorities) • fast and synchronous programming model: high frequency clocked process with simple polling for s and t • cons: • VHDL to cumbersome / prohibitive for large applications • lots of legacy code written in, e.g., C
A VHDL Solution process -- fsm begin wait until rising_edge(clk); case state is when S0 => if (s = ‘1’) then state <= S1; when S1 => if (s = ‘0’) then state <= S2; when S2 => if (s = ‘1’) then – red arc in FSM state <= S1; if (t = ‘1’ and s = ‘0’) then state <= S0; end case; door <= ‘1’ when (state != S0) else ‘0’; timer_enable <= ‘1’ when (state = S2) else ‘0’; end process;
What if C? • C: FSM in a taskdoor_controller • pros: • simple (sequential) programming model • cons: • can’t be invoked periodically by a high-frequency clock (timer) because of polling overhead • busy waiting (polling) is not an option (see above) -> concurrent (event) programming (e.g., using interrupts and semaphores) • so the while loops in the example code are wrong • only use a delay which is not based on busy wait • ergo: interrupt programming, using an RTOS
A C Solution void isr_sensor(void) // process sensor IRQ { OS_Post(semaphore_event_on_s); // signal s changed } void task_door_controller(void) { for (;;) { OS_Pend(semaphore_event_on_s); // wait for s = 1 out(door,OPEN); do { OS_Pend(semaphore_event_on_s); // wait for s = 0 OS_Delay(1000); } while (inp(sensor) != 0); // timeout out(door,CLOSE); } }
Issues • efficient, no busy waiting any more (OS_Pend, OS_Delay) • still, code is not correct: interrupts (entering/leaving persons within delay period are not properly handled, and are only accumulated in semaphore (wrong) • cannot afford to just “sit” in a delay, need control flow flexibility of a “real” FSM, i.e., to jump, AND .. • the ability to simultaneously wait for two events (s or t): void isr_sensor_and_timer(void) { // handle both IRQs OS_Post(s_or_t); // either s or t changed }
Alternative C Solution void task_door_controller(void) { for (;;) { switch (state) { STDBY: OS_Pend(s_or_t); // wait for 0-1 out(door,OPEN); state = OPEN; OPEN: OS_Pend(s_or_t); // wait for 1-0 timer_enable(); state = TIMING; TIMING: OS_Pend(s_or_t); // wait for 0-1 or t if (inp(sensor) == 0) { // timeout out(door,CLOSE); timer_disable(); state = STDBY; } else state = OPEN; }}}
Conclusion • Embedded programming is not so easy • Nor in C nor VHDL • C: • Concurrency needed (seq. prog. model): RTOS support • Event programming needed: interrupts + RTOS support • Learn the basics of interrupt programming and using an RTOS (in C) • Learning is (lots of) programming! • Lab: simple Cruise Control subsystem • Hardware: FPGA board with 32 bit soft core + C tools • In2305 via: http://www.st.ewi.tudelft.nl/~gemund/
engage inc/dec throttle controller vehicle speed Lab Assignment: Cruise Control • http://auto.howstuffworks.com/cruise-control.htm • engage button: engage cruise control • inc button: increment throttle or cruising speed • dec button: decrement throttle or cruising speed • speed and throttle on SSD • monitor link to PC terminal (status, logging, ..)
Cruise Control Setup (throttle) m PC host (Linux) Embed. Syst. a b (FPGA board) (speed) encoder (vehicle) DC motor
32 32 32 32 controller ES Setup throttle engage inc PWM m dec setpoint a decoder speed b count m a b
Demo Demo ..
Finally .. • Grade = f (MC exam, Quiz, Lab) • Without lab NO grade • Lab: presence mandatory, AWOL -> no grade! • Lab enrollment: email TA ASAP • In2305-ii first time edition! Accept glitches .. • Response group (3 student volunteers – via email) • In2305 via: http://www.st.ewi.tudelft.nl/~gemund/ • Credits: Mark Dufour, Sijmen Woutersen