| Contents | Index |
The following is a generic procedure for how to create a custom device driver. For additional notes on writing custom xPC Target drivers, see Custom xPC Target Driver Notes. For a description of a tool that helps you create simple custom drivers, see Using the xPC Target Driver Authoring Tool. A simple custom device driver is one which performs no DMA or interrupt handling.
Note You might need administrative or write privileges to add a custom device driver to the xPC Target system. Otherwise, see Creating Custom Blocks in the Simulink User's Guide. This topic describes how to add custom blocks to a library. |
Write your driver in C using the approved I/O functions (see I/O Structures — Alphabetical List and I/O Functions — Alphabetical List). An example device driver for the analog inputs of the Diamond MM-32 board is available at
matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\addiamondmm32.c
As you write your device driver, you will want to compile and link the driver, then test it. Compile and link the driver into a MEX-file. For example:
mex driver.c
This step creates the MEX-file executable, driver.mexw32.
Create a file of MATLAB code to supplement the main C driver and support the block mask. For an example of this file, see
matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\diamondmm32.m
Open the Simulink Library Browser and create a new library, for example, your_company_namelib.mdl (see Creating Block Libraries in the Working with Block Libraries in the chapter of Simulink User's Guide). Use a unique library name to prevent conflicts with other libraries.
In the new library, create an S-function block. From the Simulink Library Browser, drag an S-Function block to the new library.
Configure the S-Function block.
In the new library, right-click the S-Function block and select S-Function Parameters.
In S-function name, enter the name, without extension, of the driver. For example, addiamondmm32. (This is the driver C-file you created in step 1).
In S-function parameters, enter the parameters you defined for the driver. The parameter names you enter here must match the names you will later enter for the driver block mask (through the Parameters and Initialization panes of the Mask Editor dialog box). For example, firstChan, numChans, range, sampleTime, baseDec. Step 7 describes the block mask creation.
Leave the S-function modules parameter with the default value, unless you need to separate your driver C-file into multiple files. If that is the case, see SpecifyAdditional Source Files for an S-Function in the Insert S-Function Code chapter of the Simulink Coder User's Guide.

Double-click the S-Function block and create a block mask (see Masking Drivers).
At the bottom of the S-Function block, enter a block name. For example, MM-32.

To make your new library visible in the Simulink Library Browser, move it to
matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers
Copy and paste sample_xpcblocks.m in
matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers
Rename this file your_company_namelib_xpcblocks.m and edit this file as follows:
Set out.Library to your new library.
out.Library = 'your_company_namelib';
Set out.Name to a string, such as the library name.
out.Name = 'your_company_namelib Blockset';
This string will appear in the Simulink Library Browser.
Set out.IsFlat to 0.
out.IsFlat = 0;
(Optional for PCI boards) To enable the xpctarget.xpc.getxpcpci function to account for your new board, copy sample_supported.m to a unique file name. For example:
your_company_namelib_supported.m
Edit your copy of the file. For each board for which you add a device driver:
Copy one of the commented structures in the file.
Remove the comment symbols (%).
Starting with 1, update the ID number.
Replace the field entries with your equivalents.
A structure entry might look like:
boards(1).VendorID = '18f7'; boards(1).DeviceID = '0004'; boards(1).SubVendorID = '-1'; boards(1).SubDeviceID = '-1'; boards(1).DeviceName = '422/2-PCI-335'; boards(1).VendorName = 'Commtech'; boards(1).DeviceType = 'Serial Ports';
Save and close the file.
To confirm your entries, type getxpcpci('all') in the MATLAB Command Window.
Place all your driver files, including include files, in the directory:
matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers
To update the directories that you added, at the MATLAB Command Window, type
rehash toolbox
When you are done, your library will appear in the Simulink Library Browser with xPC Target: added to the beginning of the library name.
xPC Target: your_company_namelib Blockset
![]() | Before You Start | Debugging Notes | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |