User`s guide

10 Targeting Custom Hardware
10-16
Initializing the I/O Device
Device driver S-functions use the mdlInitializeConditions function to:
Read parameters from the block’s dialog box
Save parameter values in the
SimStruct
Initialize the I/O device
When reading parameter values, you must be careful to handle data types
correctly. The next two examples illustrate how to read two different types of
data from the dialog box.
Reading the Base Address. The following code reads the base address parameter
from the dialog box. While this parameter is actually a hexadecimal number, it
is specified as a character string and is ultimately stored as an integer:
static void mdlInitializeConditions(SimStruct S)
{
int base_addr_str_len = 128;
char base_addr_str[128];
mxGetString(ssGetSFcnParams(S, 0), base_addr_str,
base_addr_str_len);
sscanf(base_addr_str, "%lx", &base_addr);
}
In this example, the base address is the first argument in the dialog box. You
obtain the parameter with
ssGetSFcnParam andthenusemxGetString to
convert the argument (which, like all MEX-file arguments, is actually a type
mxArray *) to a string. The parameter is passed as a string because you cannot
specify hexadecimal numbers in the dialog box.
The conversion from string to hexadecimal is accomplished by
sscanf,which
returns the integer equivalent of the hexadecimal number.
Calculating Block Outputs
The basic purpose of a device driver block is to allow your program to
communicate with I/O hardware. Typically, you can accomplish this using low
level hardware calls that are part of your compiler’s C library or using
C-callable functions provided with your I/O hardware. This section provides
examples of both techniques.