User`s guide
Implementing Device Drivers
10-17
All S-functions call the mdlOutputs function to calculate block outputs. For a
device driver block, this function contains the code that reads from or writes to
the hardware.
Accessing the Hardware
The mechanism you use to access the I/O device depends on your particular
implementation. One possibility is to use low level hardware calls that are part
of your compiler’s C library. For example, on the PC a device driver could use
the following technique. These S-functions define access routines for both of the
supported compilers:
/* compiler dependent low level hardware calls */
#include <conio.h>
#define hw_outportb(portid, value) _outp(portid, value)
#define hw_inportb(portid) _inp(portid)
#define hw_outport(portid, value) _outpw(portid, value)
#define hw_inport(portid ) _inpw(portid)
Another technique is to use C callable functions provided with the I/O
hardware.
ADC Outputs
InthecaseofanADC,mdlOutputs must:
• Initiate a conversion for each channel.
• Read the board’s A/D converter output for each channel (and perhaps apply
scaling to the values read).
• Set these values in the output vector
y for use by the model.
DAC Outputs
InthecaseofaDAC,mdlOutputs must:
• Read the input
u from the upstream block.
• Set the board’s D/A converter output for each channel (and apply scaling to
the input values if necessary).
• Initiate a conversion for each channel.
For examples of device drivers, see the S-functions supplied in
matlabroot/
rtw/c/tornado/devices
and matlabroot/rtw/c/dos/devices.