Real-Time Workshop    

Writing an Inlined S-Function Device Driver

To inline a device driver, you must provide:

Example: An Inlined ADC Driver

As an aid to understanding the process of inlining a device driver, this section describes an example driver block for an ADC device. Source Code for Inlined ADC Driver lists code for:

The driver S-Function block is masked and has an icon. Figure 14-8 shows a model using the driver S-Function block. Figure 14-9 shows the block's dialog box.

Figure 14-8: ADC S-function Driver Block in a Model

The dialog box lets the user enter:

Figure 14-9: ADC Driver Dialog Box

Simulation Code..   adc.c consists almost entirely of functions to be executed during simulation. (The sole exception is mdlRTW, which executes during code generation.) Most of these functions are similar to the examples of non-real-time code given in Writing a Noninlined S-Function Device Driver. The S-function implements the following functions:

Since adc.c contains only simulation code, it uses a single test of MATLAB_MEX_FILE to ensure that it is compiled as a C MEX-file.

For the same reason, adc.c unconditionally includes simulink.c.

mdlRTW and Code Generation.   mdlRTW is a mechanism by which an S-function can generate and write data structures to the model.rtw file. The Target Language Compiler, in turn, uses these data structures when generating code. Unlike the other functions in the driver, mdlRTW executes at code generation time.

In this example, mdlRTW calls the ssWriteRTWParamSettings function to generate a structure that contains both user-entered parameter values (base address, hardware gain) and values computed from user-entered values (resolution, offset).

The structure defined in model.rtw is

(The actual values of SFcnParamSettings derive from data entered by the user.)

Values stored in the SFcnParamSettings structure are referenced in driver.tlc, as in the following assignment statement.

The Target Language Compiler uses variables such as baseAddr to generate parameters in real-time code files such as model.c and model.h. This is discussed in the next section.

The TLC File

adc.tlc contains three TLC functions. The BlockTypeSetup function generates the statement

in the model.h file. The other two functions, Start and Outputs, generate code within the MdlStart and MdlOutputs functions of model.c.

Statements in adc.tlc, and in the generated code, employ macros and symbols defined in device.h, and parameter values in the SFcnParamSettings structure. The following code uses the values from the SFcnParamSettings structure above to generate code containing constant values:

The TLC code above generates this statement in the MdlOutputs function of model.c.

adcSetHardwareGain and adcGetGainMask are macros that expand to low-level hardware calls.

S-Function Wrappers

Another technique for integrating driver code into your target system is to use S-function wrappers. In this approach, you write:

See Writing S-Functions for a full description of how to use wrapper S-functions.


  Writing a Noninlined S-Function Device Driver Building the MEX-File and the Driver Block