Main Content

STM32CubeMX based PIL Execution at the Command Line Using MATLAB Coder

PIL Execution at the Command Line Using MATLAB Coder for STM32 Based Boards

This example shows how to set up a processor-in-the-loop (PIL) execution to verify generated code from a MATLAB® function using MATLAB Coder™ at the command line for STM32CubeMX project.

You can use PIL executions to verify generated code that you deploy to the STMicroelectronics STM32 Processors by using a MATLAB Coder procedure. You can profile algorithm performance and speed of your generated code on the hardware board. To verify generated code, you must have MATLAB Coder and an Embedded Coder® license.

To set up a PIL execution:

  1. In the Command Window, select the hardware for PIL execution.

    hw = coder.hardware('STM32L4xx Based')
    Hardware with properties:
    
                     Name: 'STM32L4xx Based'
             CPUClockRate: 216
             PILInterface: 'Serial'
               AutoDetect: 1
                     Mode: 'Normal'
                ResetMode: 'Software reset'
               DeviceList: 'None'
                  COMPort: 'COM1'
        CubemXProjectPath: '<Specify the CubeMX Project>'
                    USART: 'USART3'
           ConnectionPort: 'SWD'
    
  2. Add the hardware to the MATLAB Coder configuration object.

    cfg = coder.config('lib','ecoder',true); 
    cfg.VerificationMode = 'PIL'; 
    cfg.Hardware = hw; 
    

    The memory of the hardware board is limited, and a default value of 20000 is beyond the stack size available in the hardware. We recommend you to set the StackUsageMax value to 512. However, specify an appropriate stack usage based on your algorithm.

    cfg.StackUsageMax = 512;
  3. Generate PIL code for a function, averaging_filter.

    codegen -config cfg averaging_filter -args {zeros(1,16)}

For another example of PIL verification, see the Processor-in-the-Loop Verification of MATLAB Functions example in the documentation of the Embedded Coder Support Package for STMicroelectronics® STM32 Processors.

To install the Embedded Coder Support Package for STMicroelectronics STM32 Processors, see Embedded Coder Supported Hardware.

Configure STM32 Processor-Based Boards with STM32CubeMX Using Command Line

This example shows how to configure the STM32 processor based boards with STM32CubeMX project using command line. Using this example, you will learn how to,

  • Get list of boards

  • Create STMCubeMX Project

  • Launch STMCubeMX Project

  • Get Connected Devices List

Before getting started, add the hardware to the MATLAB Coder configuration object.

cfg = coder.config('lib','ecoder',true); 
cfg.VerificationMode = 'PIL'; 
cfg.Hardware = hw; 

Get list of boards

Enter the following Command in the MATLAB command window.

stm32.getListOfBoards(cs)

For example,

stm32.getListOfBoards(cfg)
ans =

  1×13 cell array

  Columns 1 through 7

    {'NUCLEO_l433RC-P'}    {'NUCLEO-l432KC'}    {'NUCLEO-l412RB-P'}    {'NUCLEO-l412KB'}    
{'NUCLEO-L452RE'}    {'NUCLEO-L476RG'}    {'NUCLEO-L496ZG'}

  Columns 8 through 13

    {'NUCLEO-L4A6ZG'}    {'STM32L496G-DISCO'}    {'B-L475E-IOT01A1'}    {'B-L475E-IOT01A2'}    
{'B-L462E_CELL1'}    {'Custom STM32L4x…'}

Create STMCubeMX Project

Enter the following Command in the MATLAB command window.

stm32.createSTMCubeMXProject(projectPath, hardwareName, config)

Creates a new project processor family. Provide the Input Argument for

  • projectPath = Give the full pull path of the ioc file with name or just name of the ioc file to be created

  • harwdareNme = the board name

  • config = MATLAB config

For example,

stm32.createSTMCubeMXProject('ui.ioc','NUCLEO_l433RC-P', cfg)

The ui.ioc (ioc) file is created in the current working directory.

After creating the STM32 cubeMX project, set the project path. For example,

cfg.Hardware.CubemXProjectPath='C:\Users\<username>\IOC\ui\ui.ioc'

Launch STMCubeMX Project

Launch the STMCubeMX project processor family. Provide the input argument

  • projectPath = Give the full pull path of the ioc file with name or just name of the ioc file

stm32.launchSTMCubeMXProject('projectPath')

For example,

stm32.launchSTMCubeMXProject('C:\Users\<username>\IOC\ui\ui.ioc')

See Also

| |

Related Topics