CS452 - Real-Time Programming - Spring 2013
Lecture 14 - AwaitEvent, Task Structure
Pubilc Service Annoucements
- Due date for kernel 3: 12 June.
- No class Friday, 14 June.
- Questions about kernel 3?
Serial I/O
See pdf.
FIFO
Why do FIFOs exist in UARTS?
The Big Blunder
To use the FIFO effectively you must be able to turn off the transmitter
& receiver independently.
But look at UARTE in UARTxCtrl
- UART Enable.
- If this bit is set to 1, the UART is enabled.
- Data transmission and reception occurs for UART signals.
The Little Blunder
`It is assumed that various configuration registers for the UART are not
written more than once in quick succession, in order to insure proper
synchronization of configuration information across the implementation. Such
registers include UART1Ctrl and UART1LinCtrlHigh. ... In between the two
writes, at least two UARTCLK periods must occur. Under worst case conditions,
at least 55 HCLK periods must separate the two writes. The simplest way to
due [sic] this is separate the two writes by 55 NOPs.'
Why does this occur?
- CPU clocked by CPU clock
- System buses clocked by several different clocks
- UART clocked by its own clock
- The clocks were not suitably synchronized
Why doesn't anybody care?
- UARTs are used at the beginning of the development process
- Once other I/O (ethernet, USB, etc.) is working, UARTs are no longer
used, except by the boot loader
Interrupts
Five interrupts in the device
These interrupts are separately enabled and disabled.
- Transmit
- FIFO enabled
- Asserted when transmit FIFO is less than half full.
- Cleared when transmit FIFO is more than half full.
- FIFO disabled
- Asserted when holding register is empty
- Cleared on write to the holding register
- Not conditioned by enable.
- Receive
- FIFO enabled
- Asserted when receive FIFO is half full
- Cleared when receive FIFO is read to less than half full.
- FIFO disabled
- Asserted when receive buffer is full
- Cleared when receive buffer is read
- Modem status
- Asserted when hardware flow control bits change
- Cleared when the modem status register is written
- Receive timeout
- Asserted when receive FIFO is not empty and 32 bit periods pass
with no new data
- Cleared when all data has been read from FIFO
- Combined
- OR of the four above interrupts
- Asserted when at least one of the above interrupts is asserted
- Cleared when all the above interrupts are not asserted.
Three inputs to the PIC
- Transmit
- Receive
- Combined
Easy way to use interrupts
Enable only combined; read UART registers to decide what to do.
Think of the receive and transmit parts of the UART as separate state
machines
- Base the state machine on bits in the status registers
- Make a separate state machine for flow control
Implementation of Serial I/O
FIFO is assumed to be turned off; buffers are assumed to be in the
server.
The simplest way to handle the interrupts is to turn on only the combined
interrupt and then look at the registers of the device.
To identify the current interrupt
Read UARTxIntIDIntClr at 0x800[bc]001c
- One of eight registers for interacting with the UART
- Four bits, one for each interrupt
- (Write anything to this register to clear the modem status
interrupt)
Providing I/O for the terminal
Initialization
- Enable RcvRdy, XmitRdy interrupts in UART.
- Enable combined interrupt in ICU
Receiving
Notifier RCV_BL on RcvRdy event
- Interrupt occurs, AwaitEvent returns with byte.
- Notifier sends byte to server
- Server replies, Notifier returns to initial state
- Server checks GetQ
- If ( ! GetQ_Empty )
- Extract client from GetQ
- Reply( client, byte )
- Else insert byte in RcvQ
When server receives Get request from client
- Server checks RcvQ
- If ( ! RcvQ_Empty )
- Extract byte from RcvQ
- Reply( client, byte )
- Else insert client in GetQ
Transmitting
Transmitting is a little more tricky because two conditions must be
met.
- Byte available to transmit
- Transmit holding register empty
Assume we put conjunction detection in the server
When server receives Put request from client
- Is Notifier ready?
- If ( Ready ) Reply( notifier, byte ); Reply( client, ... )
- Else insert byte in XmitQ; Reply( client, ... )
When server receives Ready request from Notifier
- Is XmitQ empty?
- If ( Empty ) Mark notifier ready
- Else Extract byte from XmitQ; Reply( notifier, byte ); Reply( client
)
The Notifier
- Enable XmitRdy interrupt
- byte = AwaitEvt( XmitRdy )
- Disable XmitRdy interrupt in UART
- Send( server, ready, byte )
- Write byte on UART
This procedure assumes that the transmit bit never resets once it is set.
Is this true?
Conjunction detection and buffer could just as easily be in the
Notifier
Providing I/O for the train controller
Receiving
As Above
Transmitting
For transmitting three conditions must be set
- Byte available to transmit
- Holding register empty
- CTS asserted
Task Structure
We are supposed to support
int Get( int port )
and
int Put( int port, char c )
These are wrappers for sends to one or more serial servers.
- On Get the server blocks the client until data is available
- Does a non-blocking TryGet give you anything you cannot do with
task structure?
- Put is non-blocking. The only guarantee is that the character has been
added to a buffer of characters to be transmitted
You will probably want something more complex in addition to Get and
Put.
How many servers and notifiers?
|
one server |
two servers |
four servers |
| one
notifier
|
likely queue
congestion in
server
|
|
likely queue
congestion
in notifer
|
| two
notifiers
|
one per channel?
one per direction?
|
how should they
be paired?
|
|
| four
notifiers
|
certain queue
congestion
in server
|
likely queue
congestion in
server
|
best performance,
most tasks
|
How should we handle a terminal?
Issues
- Line editing
- undefined intermediate states
- well-defined trigger
- Echo -- Either
- one server per channel,
- passing characters from one notifier to the other
Or
- courier connecting receive server to transmit server
- What characteristics does the terminal have? termcap follies
- Many other issues come up below as we consider possible task
structures.
Debugging Real-time Programs
The most common set of debugging tools used by experienced programmers is
the oldest: printf, grep & stack trace.
- The power of these tools is greatly enhanced by strong conventions in
code formatting.
Debugging real-time programs, at its base, is just the same as any other
debugging, and just the same as empirical science.
- Gather data.
- Create a model that explains the data
- Test the model
- If the model is not correct, go to 1.
- Remember that the model is ALWAYS provisional: data collected later may
invalidate it, no matter how much data has confirmed it.
But real-time programs are harder to debug. Very few programs are entirely
free of critical races, which are the worst type of bug, lurking for weeks
months or years in seemingly correct code, then appearing when innocuous,
believed to be independent code changes occur.
Critical Races
There is no known method for eliminating critical races.
- Synchronizing everything, which seems to be an obvious solution, kills
performance because it removes flexibility from the execution.
It is, in principle, impossible to test away critical races. Why?
- When three trains run continuously for ten minutes, how many events
occur in the real world?
- How many possible orders are there for these events?
- Re-ordering isn't even necessary for a critical race to occur, just
getting too close in time.
RedBoot
The memory contents are not wiped by reset. Some of the most difficult
errors can be detected only by using the contents of memory after a reset.
Produce useful results by inserting
str pc, <magic location>
many places in your code. Then, with the assistance of a load map, you can
find out where you were in which code when the problem occurred.
You can often find out this information by looking at the garbage output
that often appears at the bottom of the screen. One set of eight characters
is the hex address of the instruction that could not be executed. With a load
map and assembly listings you can find the instruction on which the program
died.
In RedBoot you can, in principle, trace any of the kernel data structures,
but it's extremely arduous to do it.
Breakpoint
What does it do?
- snapshot of the system
- This means that computation, including respose to interrupts, must
stop, or it isn't a snapshot.
- It's easy to make a mistake and have some parts of the program
continuing to run while you are interrogating the system.
- provides interactive tools for examining kernel data structures, such
as
- task descriptors
- lists and queues
- stacks, including the program counter and local variables, of
individual tasks
- restart system immediately afterwards
- If you want to continue where processing stopped you must make
certain that all state is saved when you enter Beakpoint and restored
when you leave it. What about pending interrupts? You can't stop the
entire universe!
- Otherwise you can re-enter RedBoot.
How do you get it started?
- function call, which you insert in your code when compiling.
- The easiest and fastest form to implement.
- Having the call as part of ASSERT is common.
- Has to exit to RedBoot. (Jump to x00.)
- system call instead of function call, which respects the kernel/user
distinction.
- an exception triggered externally
- at initialization
- Set up the system so that the external event will generate an
exception
- E.g. attach a button to PDIO on the third connector, set up
ICU.
- at run-time
- Trigger the interrupt
- Switch to Breakpoint in the event handler
- Either exit to RedBoot,
- Or clean up pending interrupts and resume execution.
Breakpoint is a special case of a particular sort of tool that is very
common.
- condition occurs => information is made available
- breakpoint provides the information interactively (`interactively' =
`on the time scale of the user')
- it can stop the system completely. How?
- but it has limited ability to stop the real world
Getting information closer to real-time.
Stack Trace
In single-threaded programs this is often the most useful tool.
- Anything that terminates execution abnormally prints the set of active
stack frames
- Minimal version
- name of calling function
- line number of call
- Extreme version
- values of arguments
- values of local variables
What is the equivalent of a stack trace in a real-time multi-tasking
environment?
Return to: