NXP Model-Based Design Tools Knowledge Base

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

NXP Model-Based Design Tools Knowledge Base

Labels

Discussions

Sort by:
S32K396 - MBDT based motor control demo Examples were designed on XS32K396-BGA-DC EVB and 3-phase PMSM pre-driver board(with connection cable) MATLAB Simulink based project (s32k396_pmsm_mc_mbdt) is build using Model-Based Design Toolbox (MBDT) and can be downloaded from NXP Model-Based Design Toolbox for S32K3xx - version 1.4.0 or newer releases. For all models file and document, please find the attachment. 1. Introduction   The Model-Based Design Toolbox (MBDT) enables the Model-Based Design workflow targeting NXP processors via the MATLAB® and Simulink® environments. The NXP MBDT integrated the system and peripheral devices interface blocks and their Real-Time Drivers (RTD), Automotive Math and Motor Control Library (AMMCLIB), Compilers and Toolchain. It’s a Simulink ® -embedded target supporting NXP MCUs for direct rapid prototyping and built-in support for software-in-the-loop and processor-in-the-loop (SIL and PIL) development workflows. It also generates and deploys code automatically to start up the MCU and run complex applications, which enables control engineers and embedded developers to shorten project life cycles.   The S32K39 is an Arm Cortex-M7 based Microcontroller series, which contains an advanced motor control co-processor (eTPU), a high-resolution PWM, and runs up to 320 MHz. It is developed to meet the next generation SiC traction inverter requirements and to enable high efficiency and low latency features. Also, S32K39 is suitable for applications like Automotive Inverter, On-board Charger (OBC), and High-performance Battery Management System (BMS). This demo aims to give a quick start guide on building up a motor control system with NXP MBDT on S32K396. It contains the environment setup, module configuration, system initialization, and interruption structure, Permanent Magnet Synchronous Motor (PMSM) control algorithm, and FreeMASTER configuration. 2. Motor control demo requirements   The MBDT based motor control demo which supports the following features: 3-phase PMSM speed Field Oriented Control. Integrated eTPU software resolver functions for position and speed measurement. Develop PIL models for hardware simulation and PIL test. Application control user interface using FreeMASTER debugging tool. To install the MBDT plug-in for S32K3, please check the Model-Based Design Toolbox for S32K3xx - Quick Start Guide under NXP MBDT page.   2.1 Required software   S32 Design Studio for S32 Platform 3.5 EB tresos Studio 29.0 FreeMASTER 3.2 MATLAB R2023a with “Embedded Coder for ARM Cortex-M Processors” and “NXP_Support_Package_S32K3xx” NXP Model-Based Design Toolbox for S32K3xx version 1.4.0   2.2 Required hardware   The following is a list of hardware required. Boards: XS32K396-BGA-DC EVB and MC33937 MOSFETs pre-driver(with connection cable). Motor: 3-phase PMSM TGT2-0032-30-24. Debugger: Lauterbach for Cortex-M7 or Multilink PE micro debugger. Power: 12V power supply for EVB and 24V power supply for pre-driver board. PCIE Cable. Micro USB Cable.   2.3 Prepare the demo   The central controller EVB board runs the control algorithm, and the pre-driver drives the motor. Connect the hardware devices with the following steps: Connect the PMSM’s three-phase winding to J4 on the pre-driver and connect the resolver signal to J8 on the pre-driver. Connect motor control signals from J14 on the pre-driver to J44 on the EVB board. Connect the debugger between JTAG J20 and the computer. Connect the USB cable between J15 and the computer, then the EVB LED D30 will be solid green. Plug in the 12V power supply port J1 on the EVB board, then the EVB LED D4 will be solid green. Plug in the 24V driver power on port J13 of the pre-driver, and then the pre-driver LED D14 will be solid yellow.   2.4 Running the demo   Please refer to the Getting Started with the S32K396 MBDT Motor Control Demo.pdf  in project doc folder.   3. Demo blocks introduction   The MBD model overview with two main functions: initialization and interruption. These are the main blocks: Var Init contains the variables used by the model and can be displayed on FreeMASTER. Initialize contains Freemaster configuration, state machine and control mode initialization, and CTU hardware trigger enablement. Hardware Interrupt Callback calls the motor control block to run. Motor Control runs every time an ADC interrupt occurs. Within an interruption, the ADC hardware trigger is disabled until the PMSM control algorithm calculation is finished, The priority order of generated code is achieved by setting the “Priority” of block properties.   The motor control algorithm is a subsystem reference model. It collects the input sensor signals, calculates the PMSM control algorithm, and drives PWMs to generate the output voltage. Following figure shows the main blocks: Current Voltage Measurement reads BCTU FIFO data and calls the following blocks. Board Buttons control the motor with hardware pins on board. State Machine subsystem contains the stateflow of the motor control application and each state will call a dedicated function block. Enable Outputs enables the PWM output function. Disable Outputs disables the PWM output function. Green LED Toggle controls a green LED to blink. Update PWM calls the MBDT PWM block to update the duty cycle value.   3.1 Startup initialization   The startup initialization is a subsystem of the initialize function block. It’s used to config some basic functionality at the start of the application: FreeMASTER Config which configures the UART to communicate with FreeMASTER GUI. Gpt_startTimer enables one GPT channel for pre-driver MC33937 dead time configuration Adc_CtuEnableHwTrigger enables the BCTU hardware trigger feature. Set the event and state to reset values and set the default control mode as speed control.   4. Interrupt and measurements   This part will describe the configuration and usage of interrupt and ADC used in this project. BCTU FIFO notification is used to call the main control loop of the project, which ensures the ADC sampling is finished before processing.   4.1 Interrupt configuration and service   The following picture shows the configuration for the BCTU FIFO notification callback feature. The BCTU FIFO 1 is used to store the ADC results, the watermark value decides when to call the function configured in the notification. Also, enabling the BCTU IRQ in configuration is needed.     In the Simulink model, connect the Hardware Interrupt Callback block with the desired handler function. When the interrupt occurs, the block will call the connected subsystems.   4.2 Measurements of currents and voltage   This part describes the configuration for 3-phase currents and bus voltage measurement. BCTU is used to make sure the values are sampled at the same time, and measured values are stored in the BCTU FIFO. BCTU FIFO notification is used to call the main loop of the motor control, which ensures the sampling is finished before using them. For the configuration of BCTU and ADC, please refer to the application note, 3-phase Motor Control Kit with S32K396, for more details. This document focuses on how to get sampled values for processing. The following picture shows the steps and operations to transfer the current and voltage to their real physical values. Using the ADC submodel in the MBDT library with the function ‘Adc_CtuReadFifoData’ to get the current and voltage from BCTU FIFO. Then, according to the physical circuit restoring them to real values.   5. eTPU resolver   Software resolver is now widely used in the Inverter application, which can help to save the BOM cost. The eTPU on S32K39 supplied a function to the customer to implement a software resolver via a software package. It uses one eTPU channel to generate a 50% duty-cycle PWM output signal to be passed through an external low-pass filter and used as a resolver excitation signal. In the resolver position sensor, this excitation signal is modulated by the sine and cosine of the actual motor angle. The feedback Sine and Cosine signals are sampled by an SDADC and processed by a followed DSP. The conversion results can be transferred to eTPU DATA RAM by DMA. Then, the eTPU can process the digital samples of resolver output signals and output the position and speed for the FOC. For more details about the eTPU resolver, please refer to AN13038.   5.1 Resolver functions call in MATLAB   There is no MBDT block for resolver functions, using the System Outputs block to specify code for the code generator to add declaration, execution, and exit sections. It calls Etpu_Resolver_Ip_Sample (&resolver_instance_mc_0) at the beginning of the interrupt function. And then it calls getMotorControlResolver (&mbd_ebt_DW.FOC_one.Resolver_SW) to get resolver data.   Finally, the velocity and angle of the resolver are sent to the speed loop and current loop at the subsystem Resolver Angle. The sin and cos value of the angel is calculated then.   6. State machine   This is a critical part of models with motor control, each state has a dedicated block to handle the related functions.   The state machine block controls the workflow of the motor control application, it’s developed based on the stateflow tool in MATLAB. For each state model, please refer to the AN 3-Phase PMSM Motor Control with MBDT on S32K396.   7. PWM control and update   Subsystems Enable Outputs and Disable Outputs control the output of PWM. They are called by the state functions. In the subsystems, the flag PWM_enabled and gate driver output status is changed via the pwm_enable_output or the pwm_disable_output function. Enable Outputs subsystem: Disable Outputs subsystem:   Update PWM subsystem is used to generate the PWM duty cycle value according to the output from the control loop. This value is eventually set for peripheral FlexPWM to generate PWM voltages to drive the motor. The FlexPWM is configured to generate complementary signals for bridges and is updated synchronously according to a reload signal in the EB project, for more details please refer to the application note, 3-phase Motor Control Kit with S32K396. Here only needs to pass the duty cycle values to the Pwm function in the model. Update PWM subsystem:   PWM update peripherals:   8. Buttons   Buttons are used to control the running of the motor. It reads the value of the button from the board to change the running state or speed. The Dio block in the library is used to read the value of I/O. The I/O port of buttons has been configured in the EB project. Two buttons on the board are used to increase or decrease the running speed of the motor. Also, they are used to clear the fault information when pressed together. One button is used to control the start or stop of the motor. The following picture shows the subsystem for button control logic.   9. FreeMASTER GUI   FreeMASTER is a user-friendly real-time debug monitor and data visualization tool that enables runtime configuration and tuning of embedded software applications. To enable FreeMASTER in this project, the interface needs to be configured first. MBDT supplied blocks to support FreeMASTER’s configuration. In this project, LPUART_0 is used to transmit data between the GUI and the board.   10. PIL model   The PIL model is used to verify the model-generated code with microcontroller involved. The PIL top model contains two parts, the simulation model and the hardware model. For the hardware model, the control algorithm is the same as in the MBD model, together with the input and output signal processing blocks will generate code that runs on the hardware controller. While the simulation model simulates the pre-driver and PMSM on a laptop and monitors the model signals.   10.1 PIL Model introduction   The PIL top model runs the simulation model part on the laptop and exchanges signal data with the hardware controller through serial port.   10.1.1 Simulation model   The simulation model simulates the hardware of the pre-driver and PMSM with simscape blocks. Duty to PWM simulates the function of a central aligned FlexPWM with the frequency of 20kHz. Inverter simulates three-phase full-bridge circuit with the same parameters as in MC33937 MOSFETs pre-driver. PMSM is defined with the same parameters as the real motor. Resolver is simplified as an ideal rotational motion sensor. Phase Current Sensing, Bus Current Sensing and Bus Voltage Sensing are built up regarding MC33937 MOSFETs pre-driver. SARADC block samples signal simultaneously with the PWM module and generates a function call after data conversion is completed.   10.1.2 Hardware model   The hardware model is a referenced model that generates code and deploys it into the microcontroller. It’s called by ADC conversion completion signal. In the initialization process, it initializes the global variables and drivers. In the normal running process, when a function call comes, the model will calculate the input signal, run the state machine, and generate the output signal in the order defined in subsystem “pmsm_mc_algo”.   11. Conclusions   The NXP MBDT for S32K3xx integrates the commonly used features like RTD, AMMCLib, compiler, and toolchain in the Simulink environment, and allows to import peripheral driver configuration from EB Tresos. It allows converting the software-verified simulation models into reliable optimized hardware code with minimal effort and migrating the original module configurations to the newly created MBD model with minimal effort, accelerating project application development. This application note gives examples of the construction and environment setup process of motor control models for MBD and PIL. It introduces the method of calling the MBDT library predefined driver and embedding custom code. For more information about the newest MBDT application, please visit the MBDT Community Page. MBDT enables the NXP tools like Automotive Math and Motor Control Library (AMMCLib), Compilers, Configuration Tools, Real-Time Drivers (RTD), and Toolchain in the Simulink® environment. Together with the Embedded Coder®, it generates optimized code for the microcontroller, ensuring that the most advanced software loops can run on the NXP MCUs with maximum performance. You might also visit NXP.com for additional information on the development ecosystem that NXP offers.   12. References   3-phase Motor Control Kit with S32K396 The workshop “3-Phase PMSM Control Workshop with NXP's Model-Based Design Toolbox” from NXP Community Module 4: Space Vector Modulation from NXP Community MC33937: 3-Phase Field Effect Transistor Pre-driver. Automotive Math and Motor Control Library Set for NXP S32K3xx devices. FreeMASTER Run-Time Debugging Tool. Application Note: AN13902: 3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks
View full article
The content of this article is identical to the AN13902: 3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks
View full article
Introduction The application is based on an example of basic GPIO for S32K144 (gpio_s32k14_mbd_rtw). The application is extended to fit the needs of motor control application running a sensorless PMSM Field Oriented Control algorithm. Therefore, certain modes (states) and transitions (events) are implemented. NOTE: Theory of Finite state machines defines states and events (transitions). Following design uses approach which may seem to mix states and transitions at some point. Some states are run just once, thus they may be considered as transitions only. However, the design is considered as an example and may be extended in terms of additional checks for external events, making these "one-shot" states the actual states. The design has been well known from NXP Automotive Motor Control Development Kits and it has been well accepted by majority of customers. Therefore, the same concept is presented for MATLAB Simulink. Finite State Machine Design The application structure should introduce a systematic tool to handle all the tasks of motor control as well as hardware protection in case of failure. Therefore, a finite state machine is designed to control the application states using MATLAB Simulink Stateflow Chart (Stateflow library/Chart). The motor control application requires at least two states - "stop" represented by "ready" state and "run" represented by "run" state. However, some additional states need to be implemented to cover e.g. power-on situation, where the program waits for various auxiliary systems (system basis chip, DC-bus to be fully charged, memory checks, etc.). In addition, the motor control application and specifically the sensorless field oriented control requires additional states to calibrate the sensors and to start the motor with known position. Therefore, transition from READY state to RUN state is done through an initialization sequence of CALIB (sensors calibration) and ALIGN (initial rotor position alignment or detection) states. To stop the motor, the application goes back to the READY state via INIT (state variables initialization). While the INIT state is designed to clear all the internal accumulators and other variables (but the parameters can be changed in the run time and not reset to the default settings), the RESET state is introduced to enable power-on or "default configuration" or "soft reset" initialization in the case of the motor control parameters are changed using FreeMASTER or other user interface. All the states are linked with an output event which is traced out of the state machine chart block. These events can be used as trigger points for calling the handlers (state functions). The transitions are driven by the input value of the state machine, treated as an event using a simple comparison (e.g. [u==e_start]). To change the state, the event/input value should be changed. If the event has changed, the state machine changes the state only in case of there is an existing action linked with the current state and the event. The state machine is designed to be used in the application using Event input (to signal the event), State output (to indicate the current state) and Event triggers outputs (to call the state functions / handlers). Following application has been built to show an example of the state machine usage. Following tables show the nomenclature of the states and events: State Purpose Value Reset Power-on / Default settings / Soft-reset state. May include some HW checking sequence or fault detection. 1 Init Initialization of control state variables (integrators, ramps, accumulators, variables, etc.). May include fault detection 2 Ready Stand-by state, ready to be switched-on. Includes fault detection, e.g. DC-bus overvoltage or high temperature 3 Calib Calibration state to calibrate ADC channels (remove offsets). Includes fault detection 4 Align Alignment state to find the rotor position and to prepare to start. Includes fault detection 5 Run Motor is running either in open-loop or sensorless mode. Includes fault detection 6 Fault Fault state to switch off the power converter and all the peripherals to a safe state. 7 Input events are the triggers which initiate a change of current state. Input Event Purpose Value e_init_done Asserted when the Init state has finished the task with success 1 e_start Asserted when a user sends the switch-on command 2 e_calib_done Asserted when all the required ADC channels are calibrated 3 e_align_done Asserted when the initial rotor alignment / position detection is done 4 e_stop Asserted when a user sends the switch-off command 5 e_fault Asserted when a fault situation occurs 6 e_fault_clear Asserted when a user sends the "clear faults" command or when the situation allows this 7 e_reset Asserted when a user sends the "reset" command to start over with default settings 8 Output events are used to trigger the Motor Control State Handlers and correspond to actual state. These events are triggered with every state machine call. Therefore, the state machine shall be aligned with the control algorithm. For example, it shall be placed within the ADC "conversion completed" interrupt routine or PWM reload interrupt routine. Finite State Machine Usage The state machine shall be used in good alignment with the control algorithm. The usual way of controlling a motor is to have a periodic interrupt linked with the ADC conversion completed or with the PWM reload event (interrupt). The state machine shall be called within this event handler, right after all the external information is collected (voltages, currents, binary inputs, etc.) to let the state machine decide, which state should be called next. Internal event/state handling inside of the state machine is clearly described by the state machine block definition. Output event triggers are configured to provide clear function-based code interface to the state machine. That means, the output events shall be connected to a function designed to handle the state task. For example, in Run state, the run() output event is triggered with every state machine call, while within the Run state function the whole motor control algorithm is called. If an input information is supposed to switch the state, a simple condition shall be programmed to change the Event variable (defined as a global variable). For example, if a user sends the "stop" command, the Event is set to "e_stop" and the state machine will switch to the Init state. For more complex triggering of output functions, additional output events can be programmed within the state machine definition. Template state handler function is based on the function caller block. In general, the function blocks can work with global variables, thus there is no need for inputs or outputs. Thanks to global Event variable, event-driven state machine can react on events thrown inside a state handler function or outside of the state machine (e.g. based on other asynchronous interrupt). An example of a simple template is shown below. In this example, the function represents the Reset state, which is supposed to be run after the power-on reset or to set all the variables to its default settings. Therefore, the first part is dedicated to hardware-related settings, the second part is covering the application-related settings. The third part checks whether all the tasks are done. If yes, the e_reset_done event is thrown (stored into the Event variable). In this case, the ResetStatus variable is obviously always going from zero to two with no additional influence. Therefore, the final condition may be removed (even by the MATLAB optimization process during compilation). If there is an external condition, such as a waiting for an external pin to be set, then it makes sense to use such "status" variable as a green light for completing the state task and throwing the "done" event. Embedded C code Implementation In default settings, MATLAB Embedded Coder generates the state machine code in a fashion of switch-case structure. This might be not very useful for further code debugging or for manual editing. Therefore, the function call subsystem block parameters should be changed as shown below. The function packaging option is set to "Nonreusable function", other settings might be left at default values. This will keep the state machine code structure in switch-case, however the state handlers function calls will be generated as static functions (instead of putting the code inside the switch-case). Following code sample is a part of the state machine code generated in the stateMachineTest.c example code. The state machine decides based on the State variable, which is internally stored in the stateMachineTest_DW.is_c1_stateMachineTest. Based on the stateMachineTest_DW.Event, a transition is initiated. Finally, the state handler function is called, in this case stateMachineTest_Resetstate(). void stateMachineTest_step(void) { /* ...  */       switch (stateMachineTest_DW.is_c1_stateMachineTest) { /* ... */       case stateMachineTest_IN_Reset:             rtb_y = 1U;             /* During 'Reset': '<S5>:35' */             if (stateMachineTest_DW.Event == stateMachineTest_e_reset_done) {               /* Transition: '<S5>:37' */               stateMachineTest_DW.is_c1_stateMachineTest = stateMachineTest_IN_Init;               /* Entry 'Init': '<S5>:1' */               rtb_y = 2U;             } else if (stateMachineTest_DW.Event == stateMachineTest_e_fault) {               /* Transition: '<S5>:46' */               stateMachineTest_DW.is_c1_stateMachineTest = stateMachineTest_IN_Fault;               /* Entry 'Fault': '<S5>:18' */               rtb_y = 7U;             } else {               /* Outputs for Function Call SubSystem: '<Root>/Reset state' */               /* Event: '<S5>:49' */               stateMachineTest_Resetstate();               /* End of Outputs for SubSystem: '<Root>/Reset state' */             }             break; /* ... */       }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ /* ... */ }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ The reset state function is compiled based on priorities set in each block of the Simulink model. If no priorities are set, the default ones are based on the order of adding them to the model. Therefore, it is very important to verify and eventually change the priorities as requested by its logical sequence. This setting can be changed in the block properties as shown below. The priority settings is shown after the model is updated (Ctrl+D) as a number in the upper right corner of each block. State Machine Test Application Testing application is designed to test all the features of the state machine, targeting the S32K144EVB. To indicate active states, RGB LED diode is used in combination with flashing frequency. On-board buttons SW2 and SW3 are used to control the application. The application is running in 10 ms interrupt routine (given by the sample time). There is an independent LPIT interrupt controlling the LED flashing. After the power-on reset, the device is configured and the RESET state is entered, where additional HW and application settings are performed. Then, the application runs into the INIT state with a simulated delay of approx. 2 seconds, indicated by the blue LED diode flashing fast. After the delay, the READY state is entered automatically. Both buttons are handled by another state machine, which detects short and long button press. While the short press is not directly indicated, the long press is indicated by the red LED diode switched on. By a short pressing of the SW2, the application is started, entering the CALIB state first, followed by the ALIGN state and finally entering the RUN state. The CALIB and ALIGN states are indicated by higher frequency flashing of the blue LED, while the RUN state is indicated by the green LED being switched on. The application introduces a simulation of the speed command, which can be changed by long pressing of the SW2 (up) or SW3 (down) within the range of 0 to 10,000. Moreover, to simulate a fault situation, the e_fault event is thrown once the speed command reaches value of 5,000. The FAULT state is entered, indicated by the red LED flashing. To clear the fault, both SW2 and SW3 should be pressed simultaneously. The INIT state is entered, indicated by the blue LED diode flashing fast. Following tables show the functions and LED indication. Button Press lenght Function SW2 short press Start the application SW2 long press Increase the speed command (indicated by the red LED diode ON) SW3 short press Stop the application SW3 long press Decrease the speed command (indicated by the red LED diode ON) SW2+SW3 short press Clear faults State LED Flashing Reset - Init Blue period 50 Ready Blue period 500 Calib Blue period 250 Align Blue period 100 Run Green always on Fault Red period 100 any Red always on when a long press of SW2 or SW3 is detected Running the example The example can be built and run along with the Model Based Design Toolbox for S32K14x, v3.0.0. This version has been created using MATLAB R2017a. Please follow the instructions and courses on the NXP Community page prior to running this example. Usage of the S32K144EVB with no connected extension boards is recommended, however this example doesn't use any HW interfaces except of (please refer to the S32K144EVB documentation): S32K144 pin S32K144EVB connector Usage PTD15 J2.2 GPIO output / strength: High / Red LED PTD0 J2.6 GPIO output / strength: High / Blue LED PTD16 J2.4 GPIO output / strength: High / Green LED PTC12 J2.10 GPIO input / Button SW2 PTC13 J2.12 GPIO input / Button SW3 PTC6 J4.4 UART1 / RxD / FreeMASTER PTC7 J4.2 UART1 / TxD / FreeMASTER The application works also with the FreeMASTER application. Users can connect to the target and watch or control the application. The FreeMASTER project is attached as well, however, the ELF file location needs to be updated in the FreeMASTER project settings after the application is built and run.
View full article
BLDC Closed Loop Speed Control example for MPC574xP(Panther)+MotorGD Features: - Commutation based on HALL sensor transitions - Speed PI controller - Speed estimator based on HALL A transition time - Example made for Linix Motor (phA-white/phB-blue/phC-green) Copyright (c) 2017 NXP version 1.0.0 Model Based Design ToolBox
View full article
BLDC OpenLoop Voltage Control example for MPC574xP(Panther)+MotorGD Features: - Commutation based on HALL sensor transitions - Voltage read via SW1(++) and SW2(--) - Voltage can be read from POT if VoltageReqSource=0 - Motor can rotate CW (default) or CCW via SW1/SW2 Copyright (c) 2017 NXP version 1.0.1 Model Based Design ToolBox
View full article