Upload
hilary-bond
View
218
Download
0
Embed Size (px)
Citation preview
CSE 466 – Fall 2000 - Introduction - 1
Reentrant Driver
open() {
if (count++) return(0);
else return(grab_resources());
}
release() {
if (--count) return(0);
else return(release_resources());
}
read() {
if (read_count++) sleep_on_interruptible(&wq);
else {
start = getTime();
INIT = 1;
sleep_on_interruptible(wq);
INIT = 0;
}
compute_distance();
copy_to_user();
if (--read_count) INIT == ;
return(length);
}
isr() {
stop = getTime();
wake_on_interruptible(wq);
}
read() {if (INIT) sleep_on(&wq);else {
start = getTime();INIT = 1;sleep_on(wq);
}compute_distance();copy_to_user();return;
}isr() {
stop = getTime();INIT = 0; compute_distance()wake_on(wq);
}
still: a new readthat occurs before the queue is emptied will overwrite “start”wakeINIT=0readblocked reader computes wrong distance
problem:processes added to wqafter wake_on may never get awakened!They have to wait untila read is attempted afterthe queue is emptied and INITis turned off.read after wake problem
CSE 466 – Fall 2000 - Introduction - 2
Use a Bottom Half!read() {
if (INIT) sleep_on(wq);
else {
start = getTime();
INIT = 1;
sleep_on(wq);
}
copy_to_user();
if (empty(wq)) INIT = 0;
return;
}
isr() { // reads will still pile up on the wait queue until the bh executes.
stop = getTime();
add_to_bh_queue(isr_bh);
}
isr_bh() { // make a new queue for all future reads, compute distance with correct start-stop data.
sonar_result = Compute_distance(); // do this once per interrupt
INIT = 0;
tmp = wq; // re-initialize queue to catch for read calls
wq = (wait_queue*) kmalloc(sizeof(wait_queue));
wake_on(tmp); // wake old queue
}
will each reader get the “correct” reading?
CSE 466 – Fall 2000 - Introduction - 3
FIFO’s, which are named pipes
Process 1void main() { mknod(“/tmp/myfifo”, S_IFIFO ); // create a FIFO file node
f = open(“/tmp/myfifo”, O_WRONLY);while (1) {generate_data(buf); write(q, buf, n);}
}
Process 2void main() {
f = open(“/tmp/myfifo”, O_RDONLY);while (1) {read(q, buf, n); process_data(buf);}
}
Works for “unrelated” processes
Multiple writers, Multiple readers Kernel ensures mutual exclusion Kernel does not control interleaving of writers, readers
CSE 466 – Fall 2000 - Introduction - 4
Multi-Processor Systems A Control Dominated Example
H2
Air
H20
Heat
controllersense:
•Temperature•H2
•Output Current•Output Voltage•Fan Speed
control•H2 valves•Output MOSFET•Fan Speed•Stack MOSFETS
not showing power supply circuitry for the controller…runs off fuel cell w/ backup battery.
CSE 466 – Fall 2000 - Introduction - 5
System level Modes (Logical Structure)
startup warmup online
offlineshtdwnSelf Check
Fail
WarmOn
Overload+Off
Self CheckPass Error
Error
Error
Error
off
CSE 466 – Fall 2000 - Introduction - 6
State Table (*error conditions are mode dependent)
Mode Outputs Tasks Signals
New Mode
Off Load Disabled Power Supply OffGas Valves ClosedPower Button Enabled
none Power Button Push Startup
Startup Load DisabledPower Supply OnGas Valves ClosedInitialize
Temp ControlH2 DetectionLoad MonitorUI Running
Initialize Complete
Warm Up
Error Condition Detected* or shutdown request
Shutdown
Warmup Load DisabledPower Supply OnGas Valves Open
Temp Control H2 DetectionLoad MonitorUI Running
Operating Temp Reached Off Line
Error Condition Detected* or shutdown request
Shutdown
Off Line Load DisabledPower Supply OnGas Valves Open
Temp ControlH2 DetectionLoad MonitorUI Running
On-Line Command Received (UI)
On Line
Error Condition or Shutdown request
Shutdown
On Line Load EnabledPower Supply OnGas Valves Open
Temp ControlH2 DetectionLoad MonitorUI Running
Off Line Command Off Line
Overload Off Line
Error Condition* or Shutdown Request
Shutdown
Shutdown Load DisabledPower Supply OnGas Valves ClosedFan On
Temp ControlSchedule transition to Off state
Timeout Off
CSE 466 – Fall 2000 - Introduction - 7
Examples of Mode Dependencies
Fan Speed Control: In startup or shutdown mode always run minimum speed otherwise attempt to maintain temperature set point If fan doesn’t turn, issue badfan signal
Hydrogen Detection: always close hydrogen valves and issue h2dectected signal if hydrogen detected, except in startup mode.
Load Monitoring If not online and output current > 0 or output voltage < min, then issue
mosfet failure signal If online and load current is > max allowed, or if output voltage is < min
then turn on another stack. If all stacks are on, then issue overload signal.
UI Process If “line” button pushed and online issue offline event, If offline issue online signal.
If “power” button pushed and not in off mode, then issue shutdown signal.
CSE 466 – Fall 2000 - Introduction - 8
Logical Decomposition
UINextStateLogic
signals
signals
Temp
Load
H2 Task
Output Logic
inputs
CSE 466 – Fall 2000 - Introduction - 9
Logical Implementation
task stateMgr _task_ 0 {while (1) {
recv_signal(&s); // blockingswitch(state)
startUp: switch(s) TOOHOT: state = SHTDWN;…
break;…
}
synchronized volatile state;
task tempControl _task_ 1 {
while(1) {
t = readTemp();if (t > MAXT) send_signal(TOOHOT); //blockingif (t < MINT) send_signal(TOOCOLD);if (t < setpoint)
increase_fan_speed();if (t > setpoint)
decrease_fan_speed();wait(ThermalTime);
}}
task loadMonitor _task_ 2 {
while(1){
if (state != ONLINE)
if (getLoad() > 0) send_signal(BADFET);
else if (getLoad() > MAXLOAD) send_signal(OVRLOAD);
wait(LoadTime);
}}
task H2Monitor _task_ 3 {
while (1) {
if (state() != startUp)
if ((getH2() < MINH) || (getH2() > MAXH)) send_signal(H2FAILURE);
}} // let this process fill in all unused cycles!
Issues: what are send_signal() and recv_signal()?How can synchronized state be implemented?
CSE 466 – Fall 2000 - Introduction - 10
Physical Decomposition -- Layers
StateMgr +UI
load
temp
H2
Socket could be implemented in shared memory, internet, or anything in between.
High level architecture can be independent of implementation choices.
Synthesis Problem:Map processes and sockets to processors and networks. Optimize performance,latency, shared mem.
Warning: usually not done this way for embedded systems…usually designer performs the physical decomposition andapp is written to thehardware…not a good idea!
state
signals
physical layer
network layer
applicationlayer
CSE 466 – Fall 2000 - Introduction - 11
Example of Physical Layer: SPI Bus
Master Slave
SCK
SDO
SDI
SCK
SDI
SDO
void isr() interrupt TIMER { SDR = S; while(!SPF); R = SDR;}
void isr() interrupt SPF{ R = SDR; SDR = S signal(RECV);}
1 0 0 1 1 1 1 0
shift reg
0 0 0 1 1 0 0 0
shift reg
CSE 466 – Fall 2000 - Introduction - 12
Multiple Slave Configuration
Master Slave
SCK
SDO
SDI
SCK
SDI
SDO
Slave
SCK
SDI
SDO
CSE 466 – Fall 2000 - Introduction - 13
ISO Layers Continued Transport Layer: responsible for end-to-end protocol of user data buffer transmissions.
Source and destination addresses are private – host to host. Maps application space channel (socket) name to network address. makes network packets w/ transport header and communicates w/ network layer.
Each layer has a peer-to-peer and an intra-stack protocol
Transport -- TCP
Network -- IP
Datalink -- Ether
Physical -- Ether ethernet fiber
Datalink -- Ether
Network -- IP
fiber ethernet
Datalink -- Ether
Network -- IP
Transport -- TCP
Network -- IP
Datalink -- Ether
Physical -- Ether
write(s, buf,n); read(s, buf,n );
Application Application
CSE 466 – Fall 2000 - Introduction - 14
Transport
Network -- IP Network -- IP
Transport
Network -- IP Network -- IP
Embedded Networking: Simplest Case Simple case: socket name is the same as physical address. No mapping, we just need
to break our message into frames…maybe
Physical Layer – typically low bandwidth, serial, byte oriented
Data link layer – read/write interface to the application
frames: destination address, data, checksum. No mapping from sockets to network address No mapping from network address to physical address (no routing)
Datalink
Physical ethernet fiber
Datalink -- Ether
fiber ethernet
Datalink -- Ether Datalink
Physical
write(s, buf,n); read(s, buf,n );
Application Application
CSE 466 – Fall 2000 - Introduction - 15
Master Slave Data Link Protocol
As an example frame is [destination address, command, data]
An acknowledgement frame is [address, data, checksum]
Master Slave
SCK
SDO
SDI
SCK
SDI
SDO
Slave
SCK
SDI
SDO
mux
dst cmd data dst type data
addr data sum type data sum
mux x x x 1 1 1 2 2 2
CSE 466 – Fall 2000 - Introduction - 16
Multi-master Systems: I2C
Multi-mastered
Send and receive
Two wire (plus ground)
Packet oriented (block send)
CSE 466 – Fall 2000 - Introduction - 17
Major Features of I2C
CSE 466 – Fall 2000 - Introduction - 18
Physical Layer
CSE 466 – Fall 2000 - Introduction - 19
Bit Transfer
Transmitter
Master
CSE 466 – Fall 2000 - Introduction - 20
Who gets to be master
The one who initiates a frame:
A frame is:<Start><addr><data>…<data><Stop> OR<Start><addr><data>…<data><R_Start><addr><data>…<Stop>
CSE 466 – Fall 2000 - Introduction - 21
An I2C Byte Transfer
Tx Device Rx Device
master
slave slave
Rx
MSB First
MSB……………….LSB
CSE 466 – Fall 2000 - Introduction - 22
A Complete Frame
MSB……..LSB
CSE 466 – Fall 2000 - Introduction - 23
Beauty of Layers
App doesn’t care if lower layer is SPI or I2C
How much can we isolate the physical differences?