Top Banner
IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.
24

IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

Dec 14, 2015

Download

Documents

Trey Prince
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

IN2305-IIEmbedded Programming

prof.dr.ir. Arjan J.C. van Gemund

Embedded Software LabSoftware Technology Dept.

Page 2: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 2

Arjan van Gemund?

BS Physics EngineeringMS, 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/

Page 3: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 3

In2305-ii?

Info: via http://www.st.ewi.tudelft.nl/~gemund/

Page 4: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 4

Embedded Systems?

ES = computer system (HW+SW) embedded within another system largely determining its functionality

printingsystem

user I/F

printer

netw I/F

Page 5: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 5

Telegraph

out-of-order datanegotiate with multiple clients (print jobs)service status requestsadapt to different printersresponse time to certain requestsdata throughput / buffering

Telegraphnetwork printer

Page 6: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 6

Underground Tank Monitoring System

guard levels, detect leaksextremely low-cost design (proc)simple arithmetic - CPU hog - response time problem

tank 1 tank N

Emb Sys

buttons LCDdisp

printer. . .

level CHlevel H2Otemperature

Page 7: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 7

Cruise Control System (Lab Project)

stabilize car speed when engagedextremely low processor cycle budgetsmall control loop jitter due to other activitiesreliable operation

speedometer

gas pedal

Emb Sys

buttons SSD

Page 8: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 8

Embedded Systems Boom

provides functionality (intelligence) of almost everythingannual growth 25-60% (Emb Linux > 60%)100 x PC marketaccounts for 25-40% costs in automotivevery large societal dependencevery high performance demands

Page 9: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 9

Embedded Software Crisisfunctionality migrates from HW to SWexisting cores combined with FPGA’s, rather than ASICsprogramming-centered design (incl. HDLs)TV, mobile, car, .. 10+ MLOC code, expon. growth!despite SW engineering: 1 – 10 bug / KLOC100 G$ / yr on bugs (Mars Polar Lander, Mars Climate Orbiter, Ariane 5, Patriot, USS Yorktown, Therac-25, ... )

Page 10: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 10

Embedded Programming

more difficult than “classical” programming interaction with hardware real-time issues (timing)concurrency (multiple threads, scheduling, deadlock)need to understand underlying RTOS principlesevent-driven programming (interrupts)

lots of (novice) errors (hence the crisis)so that’s why we have this course already in 2nd year

Page 11: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 11

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)

Page 12: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 12

Specification: FSM (Moore)

0: door’, timer_enable’1: door, timer_enable’2: door, timer_enable

s = sensor, t = timeout

s’

10

s

2

ss’

s

s’

t.s’

red transition absent in example code-> door can slam into your face!

tmre t

clk

Page 13: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 13

How to Program?

VHDL: FSM in entity door_controllerpros:

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

Page 14: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 14

A VHDL Solutionprocess -- fsmbegin 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;

Page 15: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 15

What if C?

C: FSM in a task door_controllerpros:

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 wrongonly use a delay which is not based on busy waitergo: interrupt programming, using an RTOS

Page 16: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 16

A C Solutionvoid 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); }}

Page 17: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 17

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}

Page 18: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 18

Alternative C Solutionvoid 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;}}}

Page 19: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 19

ConclusionEmbedded programming is not so easyNor in C nor VHDLC:

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 subsystemHardware: FPGA board with 32 bit soft core + C tools

In2305 via: http://www.st.ewi.tudelft.nl/~gemund/

Page 20: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 20

Lab Assignment: Cruise Controlhttp://auto.howstuffworks.com/cruise-control.htmengage button: engage cruise controlinc button: increment throttle or cruising speeddec button: decrement throttle or cruising speedspeed and throttle on SSDmonitor link to PC terminal (status, logging, ..)

throttle

speed

vehiclecontroller

inc/dec

engage

Page 21: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 21

Cruise Control Setup

m

(speed)

(vehicle)

Embed. Syst.

(FPGA board)

ab

(throttle)

PC host(Linux)

encoder

DC motor

Page 22: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 22

ES Setup

controller

inc

engage

dec 32

32

PWM

decoder

m

a

b

setpoint

speed

count

throttle

32

32

m

ab

Page 23: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 23

Demo

Demo ..

Page 24: IN2305-II Embedded Programming prof.dr.ir. Arjan J.C. van Gemund Embedded Software Lab Software Technology Dept.

in2305-II: L1 24

Finally ..

Grade = f (MC exam, Quiz, Lab)Without lab NO gradeLab: 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