NXP MCU - High Currents during align

Hello, I’m trying to adapt SimpleFOC library to work on a NXP microcontroller.

When the FOC is trying to initialise, I’m trying to find the sensor direction as it is not defined . So it calls “int BLDCMotor::alignSensor()”

During this step when the PWM is computed using SVM "setPhaseVoltage(voltage_sensor_align, 0, angle); "

I get high current’s (Around 20A) on each phase (have shunt resistors on each phase & current amplifiers). I have a fault logic which would disable the PWM when it detects high currents. Any idea on what could cause this ?

Wat voltage have you set for aligning? Also what is your phase resistance? Those values would tell you what current you should expect during the alignment phase, I think.

Alignment voltage of 10V, Input voltage is 48V

Phase resistance is around 1500 milliOhm

So that gives around 10/1.5 = 6.6 A ?

Hi, and welcome @Cheerudeep_Chintha !!

You’re working on a new MCU architecture? That’s so cool! If you get a PWM driver working for it, we would be very interested to see the code… perhaps you would consider contributing it?

Yeah, that should be an upper bound. If you have set the voltage_limits, then the current should stay within that limit. With 1.5Ω phase resistance the current should also not be rising sooo fast that it overloads in-cycle.

Some advice on creating a new PWM driver:

  • start out without a motor. Just use a logic analyser on the PWM pins.
  • Don’t test it via BLDCMotor, just use the BLDCDriver3PWM and _setPWM(). Then you can set specific values to the phases (25%,50%,75%) and check the output on the logic analyser.
  • Once your PWM output is perfect as you want it, then start testing via BLDCMotor and with a real motor attached

Maybe you already did it this way, in which case please excuse my redundant advice.

Hi @runger , I got the PWM drivers working. But I had to modify the FOC library heavily. Once I get it working, I will see how I can fit into existing FOC architecture.

Thanks for the advice

Isn’t there already code for NXP under the Teensy branch?

Teensy 4.0 uses NXP’s IMXRT1060CEC, for example.

Teensy 4.1 uses the 1062 MCU.


Of course, this may be a different NXP MCU.

@Cheerudeep_Chintha ,

Which MCU exactly is this, thank you? Also, thank you for advancing the code, this is really good.


Okay, NXP KV3x series. Using KV30 & KV31

Huh, ok, but those are the Cortex M4 ones… then I’m with @Valentine that they should be supported by the teensy driver.
Are you using Arduino Framework?

Thank you. Are you implementing 6pwm or 3pwm?

Which mosfet driver / board are you using?

It’s a custom board that I made , it will use 6 PWM.
Using IR2101SPBF Gate drivers, might change it in later stage

Did not know about teensy driver. Will look into it

@runger - No not using Arduino framework, Was using MCU Xpresso.

1 Like

Ok, I understand much better what is going on now.

Porting all of SimpleFOC to a new framework will be a bigger effort, I understand now why you “had to modify the FOC library heavily”.

One option might be to see if you can make Arduino Framework work for your board based on the Teensy support. If there is a close match between the MCU you’re using and the one on some teensy board version, then it is usually just a matter of adapting a few pin-definitions in the variant.h files.

Or you can of course continue in Xpresso, but be aware that people here won’t be able to help you as much, because SimpleFOC is really intended for use within Arduino framework… but please mention that fact when you post, otherwise people will be confused. :slight_smile:

Okay, Did not come across Teensy before. Will see if adapting to Arduino framework will be simpler

Thanks for the input :slight_smile:

I have been looking into the teensy 4.0/4.1 form factor and it does have a nice deadtime insertion function but this is not possible on the T4.0. (See edit below, I was wrong) Since the right pins are not available, I think. It might be on the T4.1. In order to set up 6 pin pwm switching with deadtime insertion you need 6 pins on the same FlexPWM with complimentary pairs A & B pins.

FYI I just found out that the FlexIO controller can go insanely fast using a 480Mhz clock.

There is the option to do a custom design with the T4 MCU and get the bootloader IC from Paul, but the one have to do via in pad I believe, which makes it a custom-custom job. Do you know where to do affordable via-in-pad prototyping?

Can someone explain to me the advantages of using 6 pin pwm switching, besides from the obvious fact that many half-bridge drivers expect two inputs? In which scenario would you turn both mosfets off. During regen braking?

Edit: just discovered it is possible to use the T4.0 for 6 pin pwm switching. Apparently you can use pins on multiple FlexPWM’s as long as they are paired (A and B pin). Do I need a Mosfet Driver? - Page 2

Both Teensy 4.0 and 4.1 use the 1062 MCU


Wow RT1170 coming out soon… 1Ghz + 240Mhz dual core beast. Would be awesome for BLDC control + peripheral. If it wasn’t for the chip shortage I think it would have been out already.

6-PWM gives you full control, depending on the driver / hardware used. So you have control over the dead-time insertion, optimising for your MOSFETs. And you can tie events/interrupts to the PWM generation to sync current sensing or voltage sensing to the commutation.
Both FETs off puts the half-bridge into “Hi-Z mode”, and is used for example in 6-step commutation and back-EMF sensing, IIRC…

1 Like

Regarding Regen Braking. Here is a good writeup about regen-braking by Microchip.

Microsoft PowerPoint - Regenerative Braking of BLDC Motors.ppt (microchip.com)

If I understand it correctly (se page 13) only the lower side mosfets are switched to create a boost circuit, using the energy in the coils, to boost above charging voltage, by PWM duty cycle [only on low side]. ?

The idea behind this, corect me if im wrong, is the ability to raise voltage, when the energy field in the coil collapses [When current can´t flow to gnd]. That “spike” in voltage will then run through the body-diode of the high-side mosfets and back to the battery.

Hence, in order to do this, we need to have all the high side FETs turned off during regen braking, and we need to control the low-side FETs independently. Naturally it will be of great importance to measure the actual voltage on the bus, in order to boost as efficiently as possible. The voltage spike will accumulate in the bulk capacitors before returning to source, completing the boost circuit.

As a safety precaution, we might want to have a large resistor (brake resistor) in place, if the generated current is greater then the battery is able to charge, as to not raise the temperature to much or otherwise damage the battery. Ideally it should not be necessary, but it can also be used to brake the motor in dynamic braking mode. BLDC MOTOR BRAKING TECHNIQUES (Part-1) (thesixtech.com)

Edit: Since the BLDC has a constant KV, we can implement a PID control loop dependent on the speed of the motor, to control the regen switching/boosting. That is, the voltage being generated by the motor when in generator mode, to low to charge the battery. Naturally all this requires the setup to be a direct drive setup eg. a bike that does not freewheel when the motor stops driving.

so, did you manage to work with the 6 PWM mode with the NXP IC?
Because of the STM32 supply shortage, I am considering the lpc54114.
It has a dual core M4 + M0