42 Real-time systems development
2.13 Lifecycle of a professional programmer
There is quite a common professional pattern to the way practising pro-
grammers deal with real-time systems. Their first project starts as a simple
sequential loop of procedures with extra interrupt routines and ad hoc global
status flags introduced as the development progresses. Unfortunately, the ini-
tial laboratory prototype often works reliably. But further modifications or
improvements to the production model induce a nightmare of unforeseen side-
effects. This is usually termed the Bodge method of system development,
bottom-up from beginning to end. The second project can be a disastrous
attempt to apply a more theoretical ideal to the practical lessons learnt from
the first experience. Second projects rarely get completed because the goal
of thorough perfection can never be attained within 3.5 person-months. The
code grows and grows, the design gets reworked many times, and the test
schedule never gets finished. Perhaps we can call this phase the Bloat phase.
After this, the only sane decision left for the third project is to reuse well-
proven software. Buy as much as possible! So beware the ‘Second System
Syndrome’.
Bodge
Bloat
Buy
Psychology of a systems programmer
2.14 Chapter summary
A simple program loop of tasks, running periodically at a fixed rate, with
help from some interrupt service routines, can satisfy many straightforward
embedded applications. However, the problem of inadvertently corrupting
shared data must be carefully considered. Using the printer port on a PC
demands running processes with root privilege, but with the Unix
setuid
facility, a process using direct I/O code can be launched with only normal
user rights. Until a programmer has had to interface with real-world devices,
the timing discrepancy between the computer and its surrounding physical
world will never be fully appreciated. The control of different kinds of electric
motors is a common requirement for real-time programmers.
/* Example driver routine for a dual, two phase stepper motor
*
*/
#include <stdio.h>
Implementing simple real-time systems 43
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "../PIO.h" //gcc keypad.c -L. -lfab -o keypad
#include <sys/time.h>
#include <ctype.h>
typedef unsigned char BYTE;
typedef unsigned int DWORD;
// alternative driving patterns for a dual, two phase stepper
// motor
const BYTE
steptab1[ ] = {8, 4, 2, 1, 8, 4, 2, 1, 8, 4, 2, 1, 8, 4, 2, 1};
const BYTE
steptab2[ ] = {9,12, 6, 3, 9,12, 6, 3, 9,12, 6, 3, 9,12, 6, 3};
const BYTE
steptab3[ ] = {8,12, 4, 6, 2, 3, 1, 9, 8,12, 4, 6, 2, 3, 1, 9};
int control, portA, portB, portC;
int portCstatus = 0; // motor attached to C0-C3 of 8255 PIO
void inithw(void) {
int x=0x90;
initFAB(&control, &portA, &portB, &portC);
write(control, &x, 1); // 8255 mode 0
}
void stepit(void) {
static int step;
int s;
step = (step++)&0x0F;
s = steptab2[step];
portCstatus = (s & 9) | ((s&2)<<1) | ((s&4)>>1);
write(portC, &portCstatus, 1);
}
int main () {
struct timeval tim;
BYTE key;
DWORD then, now, rround;
initFAB(&control, &portA, &portB, &portC);
gettimeofday(&tim, 0);
now = tim.tv
sec*1000000 + tim.tv
usec;
while (1) {
rround =0;
then = now+4000;
if (now > then) rround = 1; // roll round flag
while (rround ? (now > then):(then > now)) {

Get Real-Time Systems Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.