CS452 - Real-Time Programming - Spring 2009

Lecture 3 - Polling Loops

Practical Details


TS-7200

`COM' ports

Connected to UARTs

Only really two

Ethernet port

Reset switch

Timers

Software

Compiler

GNU tool chain

RedBoot

Partial implementation

Returns when program terminates

Busy-wait IO

COM2 uses monitor; COM1 goes to train

  1. initialization
  2. output
  3. input

Polling Loops

What is important for real-time?

  1. Throughput
  2. Response time

Busy Waiting

This is used to synchronize with an external event, with minimal response time.

#define FOREVER for( ; ; )
FOREVER {
   while( !ready( ) ) ;
   do-it( );
}

or in another form

FOREVER {
   if ( ready( ) ) do-it( );
}

Worst case response time

From the time that the ready bit sets until the next byte is in the data register

Aside: What happens to this data?

  1. The memory must be dual ported, and
  2. another processor gets the data from the dual ported memory and uses it.

The NOP technique

Maybe this isn't fast enough for you. Here's an "improvement", for your amusement

L10000000:
  nop
....
L2:
  nop
L1:
  nop
L0:
  ldb  r0, DATA-ADDRESS // get the next byte
  inc  r3
  stb  r0, [r3]
  br   r5       // r5 contains the address to which we branch

In the initialization code we determine, by experiment, exactly how many nops we need in order to land on the stb instruction just after the ready bit sets.

Worst case response time

The lesson

There's a lesson for everybody in this extreme example. (Actually there are more extreme examples, and they are used also.) Wherever there is a nicely structured solution with response time x seconds there is a disgustingly-structured example with reponse time less than x/10.

The problem with busy-waiting

What if the CPU has to both things at once?

E.g.,

  1. collect bytes coming in a serial port
  2. maintain a clock

Unless the rate of bytes coming in and rate of clock ticks are identical

Polling Loops

Polling loops allow you to manage more than one condition/activity pair at the same time.

The basic polling loop

FOREVER {
  if( c1 ) a1;
  if( c2 ) a2;
  ...
  if( cN ) aN;
}

Worst case response time

Testing more than once

Suppose you want a better response time for a1. Then try the loop

FOREVER {
  if( c1 ) a1;
  if( c2 ) a2;
  if( c1 ) a1;
  if( c3 ) a3;
  ...
  if( c1 ) a1;
  if( cN ) aN;
}

Worst case response time for a1

Breaking into pieces

Suppose the response time is still too long, because the execution of one action, say a2, is too long. Then you can break a2 into two parts

FOREVER {
  if( c1 ) a1;
  if( c2 ) { a2.1; half-done = TRUE }
  if( c1 ) a1;
  if( half-done ) a2.2;
  ...
}

This is strarting to get a little complicated and we haven't said anything about inter-action communication

TS-7200

`COM' ports

Connected to UARTs

Only really two

Ethernet port

Reset switch

Timers

Software

Compiler

GNU tool chain

RedBoot

Partial implementation

Returns when program terminates

Busy-wait IO

COM2 uses monitor; COM1 goes to train

  1. initialization
  2. output
  3. input

Return to: