S32K396 MBDT based motor control

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

S32K396 MBDT based motor control

S32K396 MBDT based motor control

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.

Zhongling_Lang_0-1711007492379.png

 

Zhongling_Lang_1-1711006836953.png

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

 

 

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:

Zhongling_Lang_2-1711007063940.png
  1. Connect the PMSM’s three-phase winding to J4 on the pre-driver and connect the resolver signal to J8 on the pre-driver.
  2. Connect motor control signals from J14 on the pre-driver to J44 on the EVB board.
  3. Connect the debugger between JTAG J20 and the computer.
  4. Connect the USB cable between J15 and the computer, then the EVB LED D30 will be solid green.
  5. Plug in the 12V power supply port J1 on the EVB board, then the EVB LED D4 will be solid green.
  6. 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.
Zhongling_Lang_1-1711007739117.png

 

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.
Zhongling_Lang_2-1711007843820.pngZhongling_Lang_3-1711007882976.png

 

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.
Zhongling_Lang_0-1711008215839.png

 

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.

Zhongling_Lang_1-1711008325777.png

 

Zhongling_Lang_2-1711008325812.png

 

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.

Zhongling_Lang_3-1711008325825.png

 

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.

Zhongling_Lang_0-1711435752753.png

 

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.

Zhongling_Lang_5-1711008847468.jpeg

 

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.

Zhongling_Lang_6-1711008847486.png

 

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.

Zhongling_Lang_7-1711008847501.png

 

6. State machine

 

This is a critical part of models with motor control, each state has a dedicated block to handle the related functions.

Zhongling_Lang_1-1711435815507.png

 

The state machine block controls the workflow of the motor control application, it’s developed based on the stateflow tool in MATLAB.

Zhongling_Lang_9-1711009169451.png

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:
Zhongling_Lang_0-1711009757777.png
  • Disable Outputs subsystem:
Zhongling_Lang_1-1711009757802.png

 

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:
Zhongling_Lang_2-1711009757815.png

 

  • PWM update peripherals:
Zhongling_Lang_3-1711009757845.png

 

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.

Zhongling_Lang_4-1711010057507.png

 

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.

Zhongling_Lang_5-1711010436750.png

 

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.

Zhongling_Lang_6-1711010535793.png

 

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.
Zhongling_Lang_1-1711012141040.png

 

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”.

Zhongling_Lang_2-1711012216169.png

 

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

 

Labels (1)
Attachments
No ratings
Version history
Last update:
‎03-29-2024 02:53 AM
Updated by: