How do I add an interrupt hook function in xPC Target 2.6.1 (R14SP1)?

1 view (last 30 days)
I would like a step-by-step example detailing how to add an interrupt hook function in xPC Target.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This enhancement has been incorporated in Release 2008a (R2008a). For previous product releases, read below for any possible workarounds:
Documentation describing how to add an interrupt hook function is missing from the Using xPC Target manual. Instructions for adding an interrupt hook function in xPC Target 2.6.1 (R14SP1) are provided here:
1. Access the following directory:
$MATLAB/toolbox/rtw/targets/xpc/target/build/xpcblocks/
(where $MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB command prompt)
Edit the file named getioirqhookstruct.m such that the variable 'list' contains the name of your board. This name must be unique and cannot include any spaces. Enter the name as the second-to-last element in the list, just before the following line:
list{end + 1} = 'None/Other';
2. In the file named mxpcinterrupt.m located in the same directory, change the value of 'bTypeMapping' from:
[1, 3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1];
to:
[1, 3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1];
Note that you are adding '15' as the penultimate element.
3. This same file, mxpcinterrupt.m, includes the lists 'vendidlist', 'devidlist', 'hooklist', and 'startstoplist'. Add an entry for the board to the end of each list as follows:
a. To the end of 'vendidlist' and 'devidlist', add the vendor and device ID for the board generating the interrupt. Use the following format:
hex2dec('ID'), % board name
b. To the end of 'hooklist', add an entry for the board that contains (as necessary) the 'pre-hook' and 'post-hook' function, the “include” file, and the name of the board. Use the following format:
{'pre-hook', 'post-hook', 'C file of hooks'}, % board name
In most cases, only a 'pre-hook' function is required. In these cases, enter 'NULL' for the 'post-hook' function. Supply the name of the C-file without the '.c' extension.
A board typically runs two hook functions, one just before the ISR ('pre-hook' function), and one just after the ISR ('post-hook' function). The 'hooklist' function is a list of cell arrays containing three elements. The first element is the name of the 'pre-hook' function, the second is the name of the 'post-hook' function, and the third is a C-file that contains the hook functions.
The ISR in this case can refer to a function-call subsystem (if the IRQ source block is used), or the entire model (if it is being run off an interrupt).
c. To the end of 'startstoplist', add an entry for the start and stop functions. The driver executes the start and stop functions at the beginning and end of an application. Use the following format:
{'start function', 'stop function'}, % board name
Replace the start and stop entries with 'NULL' if they are unnecessary.
4. Create a hooks file for the board in the following directory:
$MATLAB/toolbox/rtw/targets/xpc/target/build/src
(where $MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB command prompt)
We recommend that you open an existing *hooks.c file and save it for the new board. For instance, you can save xpc6804hooks.c as foohooks.c. You need to edit this file to customize it for your board as described here:
a. Open the new hooks file, e.g., foohooks.c.
b. Change all instances of 'xpc6804' to the appropriate string associated with your driver. For example, if the name of your prehook function is 'fooprehook', change the name appropriately. NOTE: Do not change the function signature or remove the '__cdecl' string.
c. Insert code for turning off the interrupt in the prehook function.
d. If you need to output words or double words, add macros OUTPW, OUTPD, INPW, INPD after the existing macros OUTP and INP.
5. If you want to execute the model off of an interrupt, then you are done. However, if you want to use this new configuration with the IRQ Source block, continue performing the following steps:
6. Make several backup copies of the following file:
$MATLAB/toolbox/rtw/targets/xpc/target/build/xpcblocks/xpclib.mdl
(where $MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB command prompt)
NOTE: You must perform this step. Failure to do so might corrupt your only copy of the xPC Target Block library, which is a pivotal file.
7. At the MATLAB prompt, type the following:
xpclib
8. Select the “Edit -> Unlock Library” menu item. Open the Asynchronous Event sublibrary, select the IRQ Source block, and select the “Edit -> Edit Mask…” menu item.
9. Select the Parameters pane of the Mask Editor dialog box, and highlight the third entry (the 'Variable' field of which contains 'b_type').
10. The lower left corner of the Mask Editor dialog contains numerous entries for the popup list. In this editbox, add an entry for your board as the penultimate element (just before 'None/Other'). The name of your board should be the same as that entered in getioirqhookstruct.m in Step 1. To add your board to the list, press the Enter key just before 'None/Other' and type its name.
11. Click the “OK” button, save xpclib.mdl, and restart MATLAB.
12. Open your model. Specify the board as 'I/O board generating the interrupt' in one of the following ways:
  • Select your board in the IRQ Source block from the 'I/O board generating the interrupt' dropdown list.
  • Select the “Simulation -> Configuration Parameters” menu item in your model. Select the “xPC Target Options” pane, and then select your board from the 'I/O board generating the interrupt' dropdown list.
13. Check in the compile log in MATLAB to ensure that the C-file containing the hooks is compiled when you build the model.

More Answers (0)

Products


Release

R14SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!