Engineer's Notes | How to combine STM32CubeMX and MC Workbench to generate and modify the chip program not included
What should I do if the selected chip is not included in MC Workbench?
This article teaches you how to use STM32CubeMX combined with MC Workbench to generate FOC code
Currently, ST has a wide range of products, with more than 1,000 part numbers available for customers to choose from. At the same time, ST has launched the MC SDK V5.x motor control software system for motor FOC control . This software combines STM32CubeMX to generate motor control code. Since the chip selection of MC Workbench software is limited and it is impossible to include all ST product part numbers, some customers will find that the selected chip is not included in the workbench when they need to use the software . This article explains in detail how to combine STM32CubeMX with MC Workbench to generate a chip program that is not included in this scenario . Through this document, customers can use the full range of ST products for motor FOC control, which brings convenience to customers. This article uses Nucleo-STM32G474RET6 (control board) + X-Nucleo-IHM16M1 (power board) + GBM2804H-100T (motor) .
1.1 New Construction
If there is a demo board and a motor, select the corresponding demo board and motor model ; if not, directly select customer board . Here we see that there is no Nucleo-STM32G474RET6, so we select customer board.
1.2 Configure the motor and power board
Configure the power supply, voltage protection, motor, current sampling, and power tube respectively. Because we use the ready-made power board and motor, we can use the default parameters here . If you have your own motor and power board, please refer to ST's previous technical training.
1.3 Chip selection and configuration
First of all, we can see that there is no STM32G474RET6 chip model in MC workbench, so we need to choose a MCU that is similar to it . The principle is to give priority to the same Flash size, similar models second, and the same series last . Here we can choose STM32G474QE as the reference model.
1.4 Digital port configuration
The power device we use here is a single PWM and control enable mode, combined with the hardware connection as follows.
If PWM complementary mode is used, the lower bridge control pin also needs to be configured.
1.5 Analog port configuration
The same configuration is done for the analog ports associated with hardware sampling.
1.6 Generating Code
Generate initialization code for motor control peripherals based on STM32CubeMX
2.1 Create a new STM32 CubeMX project
Create a new STM32G474RET6 project
2.2 Configuring ADC
Configure ADC1 configuration pins
You can directly open the *.ioc file of STM32G474QE generated by MC Workbench as a reference .
Configure ADC1 function
Note that ADC1 is left-aligned, the trigger signal is Tim1 Trigger Out Event, and there is only Inject mode but no Regular mode .
Enable ADC1/2 interrupt
Configure ADC2 pins and functions
Here ADC2_IN14 is multiplexed on the pin of ADC1_IN14 .
Configure ADC2 function
ADC2 has two channels for voltage and temperature sensing sampling, using Regular mode . The Inject mode is used for current sampling, which is different from the configuration .
2.3 Configure Timer1
Configure TIM1 output pins
Only the PWM output of the upper bridge is used here, so only the upper bridge output PWM wave is configured, among which CH4 is used for ADC triggering, and there is no external PWM output .
Configuring TIM1 Functions
Here are some macro definitions. You can refer to the project generated by MC Workbench. Note that they are defined with the same name .
Enable TIM1 interrupt
2.4 Configure the debug serial port
2.5 Enable STM32G4-specific Cordic peripherals
2.6 Reassigning Pins
Define the pins as hardware-controlled pins and add input and output pins .
2.7 Pin Name Redefinition
Redefine the pin names to match the names .
2.8 Planning NVIC interrupt priority
Pay special attention to the setting of interrupt priority level, which will affect the program operation.
2.9 Do not generate motor-related interrupt programs
Because the motor-related interrupt program is already in the motor library, STM32CubeMX is not used to generate interrupt code .
2.10 Other system configurations
Configure RCC to external crystal, GPIO port default settings, and Sys debugging part respectively .
2.11 Clock Configuration
Here our external crystal oscillator is 24MHz and the System clock is configured to 170MH z.
2.12 Generate Project
3.1 Copy files
Copy MCSDK_v5.4.3 in the folder generated by MC Workbench to the G474_MC folder directly generated by STM32CubeMX. Also copy the Inc and Src files at the same time. You need to choose not to overwrite files with the same name.
3.2 Add motor FOC library
Open the project generated by STM32 Cube MX , add a Group to the project, and add files to the project .
3.3 Add API and control files to the project
3.4 Add header file include directory
3.5 Modify the Main.c file
Include header files and add motor control initialization functions .
After compiling and downloading, use Workbench to control the motor rotation .
- End -