Main Content

Activate the Processor-in-the-Loop Feature

Each target must have a connectivity configuration for the processor-in-the-loop (PIL) feature. By default, a connectivity configuration for your target is created based on the connectivity configuration of the reference target when you save your target. The target is stored in the ConnectivityConfig.m file of this folder:

<targetrootfolder>/matlabshared/+target/+<yourtargetpackage>

Note

The <yourtargetpackage> folder name is autogenerated from the name of your target by removing all spaces.

The connectivity configuration is registered with Simulink® via the rtwTargetInfo.m file. The rtwTargetInfo.m file gets created and stored in the <targetrootfolder> when you save your target.

The generated ConnectivityConfig.m file points to the connectivity configuration of the reference target. The connectivity configuration of the reference target was created for a special emulator interface used by the reference target and most likely not appropriate for the hardware supported by your target. To customize the connectivity configuration of your target, see Host-Target Communication for Simulink PIL Simulation.

Caution

Whenever you save a target, the connectivity configuration and the rtwTargetInfo.m file are overwritten by the default implementation described previously. Do not make any manual changes to the connectivity configuration and customization method until after you save the target for the final time.

  1. Define I/O interfaces for each hardware device that your target supports. The hardware can have multiple I/O interfaces of either Ethernet or serial type. Typically, ARM® Cortex®-R hardware uses a serial interface. I/O interfaces are used for the PIL and External mode features, which you add to your target in later steps.

  2. Add a Serial interface to the hardware by calling addNewSerialInterface with the name of the interface to be displayed in Simulink, for example, 'Serial'.

    ioInterfaceName = 'Serial'
    ioInterfaceObj = addNewSerialInterface(hw, ioInterfaceName);

    A similar command exists for the Ethernet interface. For more information, see addNewEthernetInterface.

  3. Set the default port according to your setup. The default port can be found using Windows® Device Manager under Ports(COM & LPT) and indicated by XDS110 Class Application/User UART. For example, if the device manager lists XDS110 Class Application/User UART(COM9) then:

    ioInterfaceObj.DefaultPort = 'COM9';
  4. View the properties of the SerialInterface object.

    ioInterfaceObj
    ioInterfaceObj = 
    
      SerialInterface with properties:
    
           DefaultBaudrate: 115200
        AvailableBaudrates: {[115200]}
               DefaultPort: 'COM9'
            AvailablePorts: {'COM1'}
                      Name: 'Serial'
    

    Set the properties as appropriate for your hardware.

  5. Create and add a new PIL object to your Target object, tgt, by calling addNewPIL with the name for the PIL connectivity configuration, for example, 'MyPIL'.

    pil = addNewPIL(tgt,'MyPIL');

    Do not delete the PIL object from the MATLAB® workspace before you save the target.

  6. Confirm that the PIL configuration 'MyPIL' is added to your target.

    show(tgt);
                          TIRM46Lx2 Launchpad      
    Display Name          TIRM46Lx2 Launchpad      
    RM46 TI Deployer               1               
    FreeRTOS                       1               
    MyPIL                          0

    The PIL configuration 'MyPIL' is added to the target. However, the 0 indicates that the PIL configuration is not used for the hardware 'TIRM46Lx2 Launchpad'.

  7. Map the PIL object to the Hardware object, hw, and to its I/O interface, for example, 'Serial'.

    map(tgt,hw,pil,'Serial');
  8. Confirm that the PIL configuration 'My PIL' is used for the hardware 'TIRM46Lx2 Launchpad' and its I/O interface 'Serial'.

    show(tgt);
                          TIRM46Lx2 Launchpad      
    Display Name          TIRM46Lx2 Launchpad      
    RM46 TI Deployer               1               
    FreeRTOS                       1               
    My PIL                        Serial

    The PIL configuration 'My PIL' is used for the hardware 'TIRM46Lx2 Launchpad', as indicated by 'Serial' in the corresponding position for the hardware.

  9. Save target description information to its framework.

    saveTarget(tgt);
  10. Copy the PIL connectivity configuration, source, and header files from the examples folder and save them in the target root folder.

    copyfile(fullfile(codertarget.arm_cortex_r.internal.getSpPkgRootDir(),...
         'arm_cortex_r_examples', 'pil'),...
         fullfile(tgt.Folder),'f');

    These files can be used as a guide to create your own PIL configuration.

  11. Open the connectivity configuration file, ConnectivityConfig.m, in your target folder.

    edit matlabshared.target.tirm46.ConnectivityConfig
  12. In the newCommunicator method, change the variable COMPort to match the DefaultPort property of the SerialInterface object. Save the file.

  13. At the MATLAB command line, test that the PIL works correctly.

    testTarget(tgt,'pil');

    Upon completion of the test, a summary result is displayed.

    Note

    Testing the PIL feature requires opening and deploying several models to your hardware board. This process requires some time to complete.

Confirm the Operation of the PIL Feature

  1. Create a blank Simulink model named test.

  2. On the Apps tab, click Run on Hardware Board. In the Run on Hardware Board dialog box, set Hardware board to the hardware you registered, for example, 'TIRM46Lx2 Launchpad'.

  3. In the Configuration Parameters dialog box, on the Solver pane.

  4. From the Type list, select Fixed-step. From the Solver list, select auto.

  5. Set Code Generation > Verification > Advanced Parameters > Create block to PIL. Then, click OK.

  6. Open the Simulink Library Browser and from the Sources library, add a Constant block to your model.

  7. From the Math Operations library, add the Gain block to your model. Connect the Constant and the Gain block.

  8. Add a Subtract block to your model and connect its first input port to the Gain block.

  9. In the Gain block, select Create Subsystem from Selection.

  10. In the Subsystem block, select C/C++ Code > Build this Subsystem. Click Build in the dialog box that opens. After the build completes, a library containing the PIL Subsystem block is created. This block is the gateway to the generated code that will run on the hardware.

  11. Copy the PIL Subsystem block from the library to your model. Connect its input to the Constant block and its output to the second input port of the Subtract block.

  12. In the Sinks library, add a Scope block to your model. Connect the Subtract and the Scope block.

  13. Open the Scope block, and then run the model. The PIL simulation runs for 10 seconds, but because the model is so simple, the simulation takes far less time to complete.

  14. After the PIL simulation completes, the Scope block displays the difference between the outputs of the two Subsystem blocks. The difference is constantly 0.

  15. In the original Subsystem block, open the Gain block. Set Gain to 2. Click OK.

  16. Run the model. After the PIL simulation completes, the Scope block displays the difference between the outputs of the two Subsystem blocks. Now, the difference is constantly 1 because the generated code reflects the original Gain block value of 1.