You are on page 1of 39

Sensored FOC Control of PMSM

Using GPM32F011xB USER’S MANUAL


V1.0 –Jun. 05, 2023
Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Generalplus Technology Inc. reserves the right to change this documentation without prior notice.
Information provided by Generalplus Technology Inc. is believed to be accurate and reliable.
However, no responsibility is assumed by Generalplus Technology Inc. for errors, omissions or any
loss of profit resulting from the use of information contained in this documentation. In addition,
Generalplus products are not authorized for use as critical components in life support
devices/systems or aviation devices/systems, where a malfunction or failure of the product may
reasonably be expected to result in significant injury to the user, without the express written
approval of Generalplus.

Generalplus Technology
No.19, Industry E. Rd. IV, Hsinchu Science Park, Hsinchu City, 30077, Taiwan, R.O.C.
Tel: 886-3-666-2118
Fax: 886-3-666-2117
www.generalplus.com

 Generalplus Technology Inc. 2 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

REVISION HISTORY

Revision Date By Remark


st
V1.0 2023.06.05 Jim 1 Edition

 Generalplus Technology Inc. 3 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

TABLE OF CONTENT

1. Permanent-Magnet Synchronous Motor (PMSM) Driver Introduction ...................................................... 5


1.1. General Description.................................................................................................................................... 5
2. Field-Oriented Control (FOC) ................................................................................................................... 7
2.1. General Description.................................................................................................................................... 7
2.2. CLARKE Coordinate Transform ................................................................................................................... 8
2.3. PARK Coordinate Transform ....................................................................................................................... 8
2.4. PI Controller ............................................................................................................................................... 9
2.5. PARK Coordinate Inverse ............................................................................................................................ 9
2.6. CLARKE Coordinate Inverse ...................................................................................................................... 10
2.7. Space Vector Modulation (SVPWM) ......................................................................................................... 10
2.8. Phase Current Sensoring .......................................................................................................................... 14
3. GPM32F011xb Introduction .................................................................................................................. 16
3.1. GPM32F011xB Motor Control Module General Description.................................................................... 16
3.2. GPM32F011xBMotor Control Module Introduction ................................................................................ 16
3.2.1. PWM Module ................................................................................................................................. 16
3.2.2. ADC Module ................................................................................................................................... 20
3.2.3. Hardware MATH Divider Module ................................................................................................... 22
3.2.4. Over Current Protection Module ................................................................................................... 23
3.2.5. Built-In Current Measurement Operational Amplifier ................................................................... 24
4. Sample Code Description ...................................................................................................................... 25
4.1. Hardware General Description ................................................................................................................. 25
4.2. Sample Code File Description ................................................................................................................... 26
4.3. Q Format Calculation................................................................................................................................ 26
4.4. Overall Program Structure ........................................................................................................................ 26
4.5. Main Program Flow Description ............................................................................................................... 27
4.6. PWM Interrupt Program Flow .................................................................................................................. 27
4.6.1. Motor Angle Calculation ................................................................................................................ 28
4.6.2. Current Calculation ........................................................................................................................ 29
4.6.3. FOC Sample Code ........................................................................................................................... 30
4.6.4. FOC Library Usage Description ....................................................................................................... 33
4.7. OVC Overcurrent Interrupt Program Flow ................................................................................................ 37
5. Appendix: Related Documents .............................................................................................................. 39

 Generalplus Technology Inc. 4 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

1. Introduction to Permanent-Magnet Synchronous Motor (PMSM) Driver

1.1. General Description


For motor whose counter EMF is sine wave, or motor driven by sine wave, it’s called PMSM. Like
BLDC, PMSM usually has 3-phase and Hall sensor, which is used to detect rotor position and
determine the timing of changing phases.
Figure 1.1 is PMSM Driver Circuit Diagram. By 6 triodes, Q0~Q5, 3-phase winding will input PWM
driving current to generate rotating magnetic field in order to drive the motor. Hall sensor is used to
determine the timing of Q0~Q5 outputting PWM.

Q1 Q3 Q5 Winding U

Q0 Q2 Winding V
Q4

Winding W

Figure 1.1 PMSM Driver Circuit Diagram

 Generalplus Technology Inc. 5 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Figure 1.2 is the relationship of PMSM Hall Signal and Back EMF. The driving timing generally
corresponds to counter EMF.

Hall U
Bemf U

Hall V
Bemf V

Hall W
Bemf W

Hall
5 1 3 2 6 4 5 1
State
Phase back EMF of PMSM

Figure 1.2 Relationship of PMSM Hall Signal and Back EMF

 Generalplus Technology Inc. 6 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2. Field-Oriented Control (FOC)

2.1. General Description


As the progress of Digital Signal Controller (DSC), the large computation of Field-Oriented Control
(FOC) is no longer a limitation. In medium and low cost application, FOC is more efficient and more
popular than controlling voltage sine wave and square wave. FOC is suitable for environment which is
low-noise, low-torque-ripple, and good-torque-control.
By transforming coordinate, FOC first transforms 3-phase sine wave current to two direct currents,
and then transforms the two direct voltages to 3-phase voltage in order to drive the motor. The block
diagram of FOC is show as Figure 2.1. 3-phase currents ia , ib and ic will become two direct

signals
id and iq after coordinate transform. Two direct signals, Vd and Vq , will be output

through the controller, and then be inverted to get 3-phase sine wave voltages Va , Vb and Vc .
Finally, the voltages go through SVPWM and become six inverter PWM signals which can be used to
drive the motor.

Figure 2.1 FOC Block Diagram

 Generalplus Technology Inc. 7 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2.2. CLARKE Coordinate Transform


Clarke coordinate transform is to transform the motor three-axis coordinates with 120° difference
into two-axis coordinates with 90° difference. As show in Figure 2.2, ia , ib and ic are motor
3-phase currents with 120° difference, and they’re transformed into i and i , 2-phase currents
with 90° difference.

ia  ib  ic  0
i  ia
i  (ia  2  ib ) / 3

2.3. PARK Coordinate Transform


Park coordinate transform is to make two-axis coordinates synchronously rotate as rotor magnetic
flux, and  is the angle of the rotor. i and i , sine waves with 90° difference, can be
transformed to 2-phase direct signals, id and iq . d axis is the motor magnetic flux; q axis is
motor current or torque, and it’s easier to control id and iq if user follows the description above.

id  i  cos   i  sin 
iq  i  sin   i  cos 

 Generalplus Technology Inc. 8 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2.4. PI Controller
PI controller has a simple and easy-to-use design. FOC uses two PI controllers, which control motor
magnetic flux id and motor torque iq respectively. Figure 2.4 is the block diagram of PI controller,
and it shows the Proportional (P) and the Integral (I). Proportional (P) is signal error multiplies Kp
gain. It can make the signal error as close to 0 as possible but not convergence. There will be little
steady-state error. Integral (I) is the integral result of signal error multiplying Ki gain. The
accumulated error signal is multiplied by an Integral (I) gain factor and becomes the Integral (I)
output term of the PI controller. PI controller is the sum of Proportional (P) and Integral (I) output.
Kc*Excess can be added to PI controller in order to limit the integral windup.

Err = Ref - FB
Up = Kp * Err
Ui = Ui + Ki * Err + Kc * Excess
U = Up + Ui
IF ( U > OutMax)
Out = OutMax
ELSE IF ( U < OutMin)
Out = OutMin
ELSE
Out = U
Excess = U – OUT

2.5. PARK Coordinate Inverse


Park coordinate inverse will make the 2-phase direct signals Vd and Vq become V and V ,
2-phase sine waves with 90° difference, by transforming the two-axis synchronous rotation
coordination to two-axis fixed coordination.

V  Vd  cos   Vq  sin 
V  Vd  sin   Vq  cos 

 Generalplus Technology Inc. 9 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2.6. CLARKE Coordinate Inverse


Clarke coordinate inverse will transform the two-axis fixed coordination to three-axis fixed
coordination.

Va  V
Vb  (V  3  V ) / 2
Vc  (V  3  V ) / 2

2.7. Space Vector Modulation (SVPWM)


The 3-phase full-wave rectification inverter consists of 6 MOS switches. The switches can generate
8 states according to different combination, and 2 of the states are invalid 0 voltage vectors which
are also called null states. If the inverter only outputs 6 basic voltage space vectors, the motion
track of the motor stator flux linkage will be a hexagon, as shown in Figure 2.7.

Figure 2.7 Space Vectors of Six Base Voltages

 Generalplus Technology Inc. 10 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

If user needs a round rotating magnetic field, please use linear time combination as shown in Figure
2.8. U x and U x 60 are the two basic space vectors that are adjacent to the output vector U out .
U out is the linear time combination of U x and U x 60 , which equals the vector addition of U x
times t1 / TPWM and U x 60 times t 2 / TPWM . t1 and t 2 are the function time of U x and
U x 60 ; TPWM is the function time of U out . As the method mentioned above, in the next TPWM
period, the linear time combination of U x and U x 60 is still applied but the function time t1 and
t 2 are different from the last time. Therefore, if TPWM lasts for long enough, several hours for
example, the trail of the voltage space vector will be approximately a circular.

Figure 2.8 Linear Time Combination of Two Basic Vectors

U out can be shown as the vector addition of U x times t1 / TPWM and U x 60 times t 2 / TPWM :

t1 t2
U out  Ux  U x60
TPWM TPWM

With the formula above and trigonometric functions, user will know that:
t1
Ux
TPWM U out

sin(60   ) sin120 

t2
U x60
TPWM U out

sin  sin120 

With the two formulas, user will get the following results:

2U out
t1  TPWM sin(60   )
3U x

 Generalplus Technology Inc. 11 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2U out
t2  TPWM sin 
3U x60

When user knows the 2-phase adjacent basic space vectors U x and U x 60 , t1 and t 2 can be
confirmed by the formulas above. If the inverter outputs zero vector, the motor stator flux linkage
will not rotate. With this feature, it’s able to insert zero vector t 0 between TPWM and make
TPWM  t1  t2  t0 . By doing so, user can adjust the angular frequency, the frequency can thus be
switched, and the motor torque ripple will be reduced.

The actual formula of the program is shown in the following section.

PWM Period  t1  t 2
tc 
2
tb  t c  t1
t a  tb  t 2

With Clarke coordinate transform, user is able to transform V and V to Va , Vb and Vc ,


which are used to determine the Sector value. The programming codes are listed below. The t1 and
t 2 every Sector corresponds to are listed in Table 2.1, and user can get t a , t b and t c by the
formulas above.

if(Va>0) Sector = 1;
if(Vb>0) Sector += 2;
if(Vc>0) Sector += 4;

Table 2.1 Sector and t1 , t 2 Corresponding Table


Sector t1 t2
1 Vc Vb
2 Vb - Va
3 - Vc Va
4 - Va Vc
5 Va - Vb
6 - Vb - Vc

 Generalplus Technology Inc. 12 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

With the combination of Sector and t a , t b , t c , user can get the 3-phase PWM cycle value, as
shown in Table 2.2.

Table 2.2 Sector and


t a , t b , t c Corresponding Table

Sector 1 2 3 4 5 6
PWM_A tb ta ta tc tb tc
PWM_B ta tc tb tb tc ta
PWM_C tc tb tc ta ta tb

 Generalplus Technology Inc. 13 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

2.8. Phase Current Sensoring


Phase current sensoring is controlled by 6 MOS switches, and the circuit architecture diagram is shown
as Figure 2.9. In the interval 1 between PWM waves, if MOS Q1, Q3 and Q4 are ON and Q0, Q2 and Q5
are OFF, the current will be input to the motor from phase A and be output by phase B and C.

Figure 2.9 Motor Variable-Frequency Drive Current Diagram

 Generalplus Technology Inc. 14 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

In Figure 2.10, if PWM switches to interval 2, MOS Q0, Q2 and Q4 are OFF and Q1, Q3 and Q5 are ON.
Since the motor is inductance, the motor current will not change immediately, which means it will
still be input from phase A and be output by phase B and C. User can convert the phase current at
interval 2.

Figure 2.10 Motor Variable-Frequency Drive Current Diagram

 Generalplus Technology Inc. 15 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

3. GPM32F011xb Introduction

3.1. GPM32F011xB Motor Control Module General Description


GPM32F011xB is a dedicated IC design for PMSM driven by FOC. Its features are listed as follows:
■ 6 channels independent PWM or 3 pairs of complementary PWM output.
■ 2 sets of 16 channels 12-bit ADC input with 1M SPS, which can sample two channels
simultaneously. It can sample at any PWM point at the same time as well.
■ 32/32bit Fast Divider
■ CORDIC Arctan Calculation
■ 2 Built-In Over Current Protection Comparators
■ 4 Built-In Current Sensoring Operational Amplifiers
■ 3 types of serial communication function provided: I2C, SPI and UART.

3.2. GPM32F011xBMotor Control Module Introduction

3.2.1. PWM Module


GPM32F011xB eCCU6 provides two PWM output modes, independent and complementary. User can
configure PWM according to actual application: eCCU6_PWM_CTRL0 [10,9,8] = 0 is for independent
mode (Figure 3.1 below) and eCCU6_PWM_CTRL0 [10,9,8] = 1 is for complementary mode (Figure
3.2 below). For PWM carrier wave comparison signal, user is able to select sawtooth wave
(eCCU6_TMR[14,13] = 0, Figure 3.1 below), or triangle wave (eCCU6_TMR[14,13] = 1, Figure 3.3
below). With appropriate eCCU6_TMR_PLOADS configuration, user can obtain the needed PWM
frequency. When sawtooth wave is applied as carrier wave, PWM frequency is determined by
Formula (1) below.

eCCU 6CLK
PWMCLK  ………….. Formula (1)
eCCU 6 _ TMR _ CK _ SRC * eCCU 6 _ TMR _ PLOADS

eCCU6CLK = eCCU6 Clock Frequency (92MHz)


eCCU6_TMR_CK_SRC (PWM Clock Divider) is at eCCU6_TMR_CTRL[11:8].

When triangle wave is applied as carrier wave, PWM frequency is determined by Formula (2) below.

eCCU 6CLK
PWMCLK  ……….. Formula (2)
eCCU 6 _ TMR _ CK _ SRC * eCCU 6 _ TMR _ PLOADS * 2

 Generalplus Technology Inc. 16 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

TMR_PLOADS

CCPm
CCPn

Time

eCCU6_PWMm

Time

eCCU6_PWMn

Time

Figure 3.1 GPM32F011xB PWM Module, Sawtooth Wave Independent Mode

TMR_PLOADS

CCPm

Time

eCCU6_PWMm

Time

eCCU6_PWMm+1

Time

Figure 3.2 GPM32F011xB PWM Module, Sawtooth Wave Complementary Mode

PWM duty is determined by eCCU6_CCPS0~5; while eCCU6_CCPS0 determines PWM0 duty,


eCCU6_CCPS 1 determines PWM1 duty, and so on. Figure 3.1 and Figure 3.2 show the configurable
range of eCCU6_CCPS 0~5 is 0 ~ eCCU6_TMR_PLOADS.

 Generalplus Technology Inc. 17 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

TMR_PLOADS

CCPm
CCPn

Time

eCCU6_PWMm

Time

eCCU6_PWMn

Time

Figure 3.3 GPM32F011xB PWM Module, Triangle Wave Independent Mode

According to user’s needs, it’s able to configure the appropriate dead time to avoid upper and lower
MOS being turned on at the same time during the process of switching. If the value of
eCCU6_PWM_DTIMEx is not 0, dead time mode will be triggered, and the dead time is
DTR
T  . If eCCU6_PWM_DTIMEx = 0, there won’t be any dead time.
eCCU 6 xCLK

TMR_PLOADS

CCPm

Time

eCCU6_PWMm

Time
DTIME_0 DTIME_1

eCCU6_PWMm+1

Time

Figure 3.4 GPM32F011xB PWM Module, Complementary Mode Dead Time

 Generalplus Technology Inc. 18 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

To avoid high-side and low-side being turned on at the same time, the controller will configure the
high-side and low-side as complementary, which means the high-side will be turned on at high level,
the low-side will be turned on at low level, and vice versa. For various controller design,
GPM32F011xB provides register eCCU6_PWM_CTRL0 [21:16] for user to configure the polarity
setting of PWM output. With the configuration of eCCU6_PWM_CTRL0, it’s able to apply a
well-developed program application when user uses a different controller.

While using single shunt to sample 3-phase current, user has to shift PWM. GPM32F011xB provides
register eCCU6_PWM_CTRL0 [24] to configure PWM offset function. For PWM duty, not only register
eCCU6_CCPm but also eCCU6_CCPn has to be configured, as shown in Figure 3.5, and PWM will then
be shifted.

eCCU6_PLOADS

CCPm

CCPn

Time

eCCU6_PWMm

Time

eCCU6_PWMm+1

Time

Figure 3.5 GPM32F011xB PWM Module, PWM Shifting Diagram

The PWM of eCCU6 might happen due to OVC external trigger, and PWM may be turned off
immediately. It’s enabled by setting eCCU6_OVC_CTRL[9] as 1. Or user can set OVC as the trigger
source of the built-in comparator in register eCCU6_OVC_CTRL [13:12]. By doing so, the mechanism
of turning off PWM can also be triggered, and OVC interrupt will be issued as well.

 Generalplus Technology Inc. 19 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

3.2.2. ADC Module


GPM32F011xB provides 2 sets of 16 channels 12-bit resolution ADC module, which support program
configurable function sample & hold. Figure 3.6 is the module sequence diagram. ADC conversion
frequency (ADCCLK) can be configured in ADCx_CTRL0 [14:12], and the highest frequency is SYSCLK/5,
which equals 18.4MHz.

For each channel, the sample & hold time can be configured respectively in ADCx_SMPx [31:0]. The
shortest configurable time is 1T (the suggested time is 10T), and the highest conversion frequency of
each data is shown as follows:
SYSCLK 92MHz
  1.314MHz
ADCx _ CLK _ SEL * (SH _ time  Conv _ time) 5 * (1  13)

ADC_START

ADC_CLK

ADC_SMP 1T ~ 200T 13T

ADC_RDY

DATA ADC_DATA

Figure 3.6 GPM32F011xB ADC Module Sequence Diagram

■ Regular Mode (unsynchronized with PWM):


Before turning on ADC conversion, conversion frequency, conversion mode and sample & hold time
have to be configured first, and ADCx_CTRL1[0] has to be set as 1, which means to turn on Regular
mode. It’s able to configure channel selector in ADCx_REG_SEQx[31:0], and the number of channels
to be sampled is configured in ADCx_CTRL1[7:4]. After all the configuration, set ADCx_CTRL0[0] as 1
and ADC conversion will be turned on. When the conversion is completed, ADC conversion ready flag
(ADCx_INTF[0]) will be set as 1. The software has to make sure the ADC conversion ready flag is set
as 1 before reading the converted data. If the conversion is done, the data will be read in
ADCx_REG_DATA. With DMA function, ADC DATA can be moved and saved in RAM in order to
prevent the converted data being covered.

 Generalplus Technology Inc. 20 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Figure 3.7 GPM32F011xB ADC Module Block Diagram

■ Injected Mode (synchronized with PWM):


There are 4 channels that can be used independently in Injected mode. User can turn on injected
mode in ADCx_CTRL1[11:8], configure eCCU6 trigger source in ADCx_CTRL1[31:16], and configure
channel selector in ADCx_INJ_SEQ[16:0]. The difference between Injected and Regular conversion is
that in Injected mode, user has to configure the starting time of ADC conversion at any point of PWM
cycle (0 ~ eCCU6_TMR_PLOAD). The ADC conversion starting time can be triggered by configuring
eCCU6_ADC_TRG01S. For eCCU6, user also has to enable synchronized transfer mode, that is
eCCU6_PWM_CTRL0[12] = 1.

■ ADC Interrupt:
Injected and Regular interrupt is enabled by ADCx_INTE[11:8] and ADCx_INTE[0]. When ADC
conversion is completed, ADC transfer ready interrupt will be generated. Check the interrupt is
generated by Injected or Regular in ADCx_INTF[8:11] and ADCx_INTF[0], and writing 1 to
ADCx_INTFC[8:11] and ADCx_INTFC[0] will clear the flag. The converted data can be read in
ADCx_REG_DATA and ADCx_INJx_DATA.

 Generalplus Technology Inc. 21 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

3.2.3. Hardware MATH Divider Module


The hardware math divider is 32/32bit operation; it can be configured as shifting to left or right, and
the operation time takes 7 system cycles, as shown in Figure 3.8. Fill dividend and divisor in register
DIVIDEND and DIVISOR, configure it’s left-shifting or right-shifting in MATH_DIV_CTRL[13], and
configure the number of shifting bit in MATH_DIV_CTRL[12:8]. When MATH_DIV_CTRL[0] = 1, the
operation will start, and when MATH_DIV_INTF[0] is set as 1, it means the operation is completed.
Quotient and remainder can be read in register QUOTIENT and REMANDER.

Clock

DIV_STR

SFT_DIR “1”

SFT_CNT 0x00

DIVIDEND 0x08

DIVISOR 0x03

DIV_BUSY

DIV_INTF

QUOTIENT 0x02

REMAINDER 0x02

Figure 3.8 GPM32F0118B Hardware MATH Divider Sequence Diagram

For usage, please refer to the following instruction.


MATH->DIVIDEND = dividend;
MATH->DIVISOR = divisor;
MATH->DIV |= MATH_DIV_STR;
while(!(MATH->INTF & MATH_INTF_DIV));
MATH->INTFC = MATH_INTFC_DIV;
quotient = MATH->QUOTIENT;
remainder = MATH->REMAINDER;

 Generalplus Technology Inc. 22 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

3.2.4. Over Current Protection Module


There are 2 built-in comparators in GPM32F011xB. The comparators can perform upward or
downward comparison, and be used in over current detection. Take CMP2 as an example, as shown
in Figure 3.9, the comparator will be turned on when register ACMP2_CTRL[0] = 1. User is able to
configure the positive input or internal reference voltage in ACMP2_CTRL[10:8] and configure the
negative input pin or internal reference voltage in ACMP2_CTRL[14:12]register respectively. There
are 16 levels, Vcc*(0/16) ~ Vcc*(15/16), for the voltage configuration. The comparator output is
obtained in register ACMP2_STS[0]. The comparator output can be set as interrupt trigger source,
and it can also trigger the hardware to disable eCCU6 PWM.

Figure 3.9 GPM32F011xB Built-In Comparator ACMP2 Block Diagram

 Generalplus Technology Inc. 23 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

3.2.5. Built-In Current Measurement Operational Amplifier


There are 4 built-in operational amplifiers (OPA) in GPM32F011xB, as shown in Figure 3.10. In OPA,
user is able to select the internal gain in register OPAx_CTRL[6:4]. The amplifier can be used in ADC
conversion or be used as the comparator input.

Figure 3.10 GPM32F011xB Built-In Operational Amplifier Block Diagram

 Generalplus Technology Inc. 24 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

4. Sample Code Description

4.1. Hardware General Description


As shown in Figure 4.1, the 3-phase inverter bridge is driven by 6 PWM signals. With the HALL signal
of the motor, user can detect the position of the motor. The shunt resistor is used to measure the
motor phase current and total current, and it’s able to detect over current signal in order to protect
the system from the damage caused by the over current.

Figure 4.1 Hardware Structure

 Generalplus Technology Inc. 25 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

4.2. Sample Code File Description


Sample code structure (Sensored_FOC_3Shunt) contains the following files, and the description of
the files is listed below.

motor.h: Motor Controller Parameters


Header Files prodef.h: Functions to be used with other .c files.
foc_v01.h: Variables of FOC Control Algorithm
Main.c: Main Program
C Files Init.c: Configuration of IC initialization and variable initialization.
IRQ.c: Interrupt Service Functions
Library Files foc_v01.lib: FOC Control Algorithm

4.3. Q Format Calculation


In fixed-point arithmetic processor, the speed of processing floating point is fairly slow. User can save
CPU processing time by applying the transform of Q Format calculation. A floating point consists of
integer and decimal, so it’s important to select the appropriate Q Format.

Q Format is to multiply a floating point with decimal by several times, make the floating point an
integer first and then continue the calculation. The sample program uses Q15 format as an example.
For a piece of 32bit data, the lowest 15bit is decimal, the highest 16th bit is integer, and the highest
bit is plus-minus sign. The range of the value is -65536<x<65535.999.

To transform a floating point to Q15 format, please multiply the data by 2^15. For example,
0.333*2^15=0x2A9F, the calculation of 0.333 will be present as 0x2A9F. Multiplying one Q15 format
by another Q15 format equals Q30. To obtain Q15 with the same format, user has to right-shift Q30
for 15bit. For Q Format calculation, please note that:
1. Fixed Point Addition/Subtraction: The data has to be transformed into the same Q format.
2. Fixed Point Multiplication: After user multiplies two identical Q formats, the bit of Q format has to
be right-shifted to generate the same format. The following example is Q15 multiplication of the
program: #define _IQmpy(A,B) ((long)A * (long)B)>>15.

4.4. Overall Program Structure


As shown in Figure 4.2, the two interrupt sources used by the program are OVC over current external
interrupt and eCCU6 PWM interrupt. PWM interrupt calculates FOC algorithm and the system state
while OVC over current interrupt will shut down the system.

 Generalplus Technology Inc. 26 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Main Loop

eCCU6 Interrupt
OVC Interrupt
(PWM)

Figure 4.2 Program Structure

4.5. Main Program Flow Description


The main program is attached to main.c file, as shown in Figure 4.3. For the main program, user has
to configure the OPA, ACMP, GPIO, DMA, ADC, PWM and POSIF function in IC. The sub function of
the related default setting is attached to Init.c file.

Reset
Init.c
OPA_Init();
ACMP_Init();
GPIO_Init();
Initial Chip DMA_Init();
ADC_Init();
eCCU6_PWM_Init();
POSIF0_HALL_Init();

Initial Variables

Main Loop

Figure 4.3 Main Program Flow Chart

4.6. PWM Interrupt Program Flow


PWM interrupt sub program is attached to IRQ.c file, and the program flow chart is shown as Figure
4.4. The program is mainly used to calculate motor rotor angle, phase current, FOC algorithm and so
on. FOC algorithm program is attached to foc_v01.lib file, and it includes coordinate transfer, PI
control and SVPWM modulation. Part of the program will be illustrated in the following description.

 Generalplus Technology Inc. 27 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Figure 4.4 PWM Interrupt Program Flow Chart

4.6.1. Motor Angle Calculation


Sample program uses the 3-phase HALL signal of the motor to calculate angle, and the program
shows the angle 0~360 as 0~65535. With capture function of POSIF HALL mode, it’s able to capture
hall cycle count value. Set the capture timer as 250KHz, and the PWM interrupt frequency is 20KHz.
As shown in the following figure, if the counting time, T_HALL(CAPTV), of the HALL cycle is already
known, user can calculate the added cumulative angle every time the system enters PWM interrupt.
65535 * 250 K
The cumulative angle  , and the angle can be calculated.
20 K * CAPTV

 Generalplus Technology Inc. 28 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

CAPTV=0 CPATV=Timer

65535

HALL_U

T_HALL

Figure 4.5 Calculating the Rotor Position with Hall Signal

Sample code is shown as below:


#define Theta_constan (65535*Period_Count_Freq)/IRQ_Freq
if(POSIF0->INTF & POSIF_INTF_CAP)
{
POSIF0->INTFC = POSIF_INTFC_CAP;
Hall_period = POSIF0->HCAPTV;
MATH->DIVIDEND = Theta_constan;
MATH->DIVISOR = Hall_period;
MATH->DIV |= MATH_DIV_STR;
while(!(MATH->INTF & MATH_INTF_DIV));
MATH->INTFC = MATH_INTFC_DIV;
Theta_add = (unsigned long)(MATH->QUOTIENT);
if(Hall_U && ~Hall_U_old) Theta= 0;
}
if((65535 - Theta) > Theta_add) Theta += Theta_add;

When the system just being started up or the rotational speed is fairly low, the HALL cycle cannot be
calculated accurately. In this situation, user can know the six angles from HALL signal, and the six
output states can be judged from the six angles.

4.6.2. Current Calculation


Under 0.7V, the output of an OPA which is not rail-to-rail is non linear, so user has to raise a level of
the offset while detecting the current of OPA circuit. When ADC sample is applied, the offset has to
be deducted to ensure the correctness of the sample values. As shown in the following sample code,
after IC power on, user has to perform 32 times of ADC sample to OPA output first, and then
calculate the average of the 32 pieces of ADC data to obtain the offset value.

 Generalplus Technology Inc. 29 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

ADC0->REG_SEQ0=ADC_REG_SEQ0_CH11;
ADC0->CTRL1 |= ADC_REG_EN | ADC_REG_CH_NUM1;
for(ADC0Sum=0,k=0; k<32; k++)
{
ADC0->CTRL0 |=ADC_SFT_STR;
while((ADC0->INTF & ADC_INTF_REG) != ADC_INTF_REG);
ADC0->INTFC = ADC_INTFC_REG;
ADC0Sum += ADC0->REG_DATA;
}
Ia_Offset = ADC0Sum>>5;

According to PWM switch state, when the 3 low-sides are on, ADC sample will be triggered to
measure the current of the two data, and the sample code is shown as follows. After ADC conversion
is completed, the measured current is the phase current. ADC0->INJ0_DATA is the ADC converted
data, and it’s in 16bit format. The result of ADC sample data minusing the offset value is Q15 format
data.

while((ADC0->INTF & ADC_INTF_INJ0) != ADC_INTF_INJ0);


ADC0->INTFC = ADC_INTFC_INJ0;
while((ADC1->INTF & ADC_INTF_INJ0) != ADC_INTF_INJ0);
ADC1->INTFC = ADC_INTFC_INJ0;
I_As = ((long)(ADC0->INJ0_DATA)) - (Ia_Offset);
I_Bs = ((long)(ADC1->INJ0_DATA)) - (Ib_Offset);

4.6.3. FOC Sample Code

void foc(FOC*v)
{
■ CLARKE Coordinate Transform:
v->I_Alpha = v->I_As;
v->I_Beta = (_IQmpy(v->I_Bs,K_IQ1divsqrt3) << 1) + (_IQmpy(v->I_As,K_IQ1divsqrt3));
■ PARK Coordinate Transform:
v->I_Ds = (_IQmpy(v->I_Alpha,v->Cosine)) + (_IQmpy(v->I_Beta,v->Sine));
v->I_Qs = (_IQmpy(v->I_Beta,v->Cosine)) - (_IQmpy(v->I_Alpha,v->Sine));
■ Q Axis PI Control:
v->I_QsErr = v->I_QsRef - v->I_Qs;
v->V_QsUp = _IQmpy(v->I_QsKp,v->I_QsErr);

 Generalplus Technology Inc. 30 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

v->V_QsUi = v->V_QsUi + (_IQmpy(v->I_QsKi,v->I_QsErr)) + (_IQmpy(v->I_QsKc,v->V_QsSatErr));

v->V_QsPreOut = v->V_QsUp + v->V_QsUi;


if(v->V_QsPreOut > v->V_QsOutMax) v->V_QsOut = v->V_QsOutMax;
else if(v->V_QsPreOut < v->V_QsOutMin) v->V_QsOut = v->V_QsOutMin;
else v->V_QsOut = v->V_QsPreOut;
v->V_QsSatErr = v->V_QsOut - v->V_QsPreOut;
■ D Axis PI Control:
v->I_DsErr = v->I_DsRef - v->I_Ds;
v->V_DsUp = _IQmpy(v->I_DsKp,v->I_DsErr);
v->V_DsUi = v->V_DsUi + (_IQmpy(v->I_DsKi,v->I_DsErr)) + (_IQmpy(v->I_DsKc,v->V_DsSatErr));
v->V_DsPreOut = v->V_DsUp + v->V_DsUi;
if(v->V_DsPreOut > v->V_DsOutMax) v->V_DsOut = v->V_DsOutMax;
else if(v->V_DsPreOut < v->V_DsOutMin) v->V_DsOut = v->V_DsOutMin;
else v->V_DsOut = v->V_DsPreOut;
v->V_DsSatErr = v->V_DsOut - v->V_DsPreOut;
■ PARK Coordinate Inverse:
v->V_Alpha = (_IQmpy(v->V_DsOut,v->Cosine)) - (_IQmpy(v->V_QsOut,v->Sine));
v->V_Beta = (_IQmpy(v->V_DsOut,v->Sine)) + (_IQmpy(v->V_QsOut,v->Cosine));
■ CLARKE Coordinate Inverse:
v->Va = v->V_Beta;
v->Vb = -(v->V_Beta >> 1) + (_IQmpy(v->V_Alpha,K_IQsqrt3div2));
v->Vc = -(v->V_Beta >> 1) - (_IQmpy(v->V_Alpha,K_IQsqrt3div2));
■ Space Vector Modulation (SVPWM):
v->Sector = 0;
if(v->Va>0) v->Sector = 1;
if(v->Vb>0) v->Sector += 2;
if(v->Vc>0) v->Sector += 4;
v->Va = v->V_Beta;
v->Vb = (v->V_Beta >> 1) + (_IQmpy(v->V_Alpha,K_IQsqrt3div2));
v->Vc = (v->V_Beta >> 1) - (_IQmpy(v->V_Alpha,K_IQsqrt3div2));
switch(v->Sector)
{
case 0:
v->Ta = K_IQ1div2;
v->Tb = K_IQ1div2;
v->Tc = K_IQ1div2;
break;
case 1:
v->t1 = v->Vc;
v->t2 = v->Vb;

 Generalplus Technology Inc. 31 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

v->Tb = (K_IQ1 - v->t1 - v->t2)>>1;


v->Ta = v->Tb + v->t1;
v->Tc = v->Ta + v->t2;

break;
case 2:
v->t1 = v->Vb;
v->t2 = -v->Va;
v->Ta = (K_IQ1 - v->t1 - v->t2)>>1;
v->Tc = v->Ta + v->t1;
v->Tb = v->Tc + v->t2;
break;
case 3:
v->t1 = -v->Vc;
v->t2 = v->Va;
v->Ta = (K_IQ1 - v->t1 - v->t2)>>1;
v->Tb = v->Ta + v->t1;
v->Tc = v->Tb + v->t2;
break;
case 4:
v->t1 = -v->Va;
v->t2 = v->Vc;
v->Tc = (K_IQ1 - v->t1 - v->t2)>>1;
v->Tb = v->Tc + v->t1;
v->Ta = v->Tb + v->t2;
break;
case 5:
v->t1 = v->Va;
v->t2 = -v->Vb;
v->Tb = (K_IQ1 - v->t1 - v->t2)>>1;
v->Tc = v->Tb + v->t1;
v->Ta = v->Tc + v->t2;
break;
case 6:
v->t1 = -v->Vb;
v->t2 = -v->Vc;
v->Tc = (K_IQ1 - v->t1 - v->t2)>>1;
v->Ta = v->Tc + v->t1;
v->Tb = v->Ta + v->t2;
break;
}
}

 Generalplus Technology Inc. 32 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

4.6.4. FOC Library Usage Description


FOC Library is shown as Figure 4.6. After user inputs 2-phase current (I_As and I_Bs), Sine, Cosine
and reference values of DQ axis current (I_QsRef and I_DsRef), 3-phase PWM duty (Ta, Tb and Tc) can
be output. The file name of FOC Library is foc_v01.lib.

I_As pu
I_Bs pu
pu Ta
Sine pu
FOC pu Tb
Cosine pu pu Tc
I_QsRef pu
I_DsRef pu

Figure 4.6 FOC Library Diagram

The Library variable is operated in Q15 format. All the variables used by the Library form a struct FOC,
and a variable name has to be declared when user is going to use it. Variables in the struct are listed
as Table 4.1. Variable struct definition is in foc_v01.h file, and it includes the configurations of
controller parameters and so on.

■ The content of foc_v01.h is shown as follows:


typedef long _iq;
#define K_IQsqrt3div2 _IQ(0.866025403)
#define K_IQ1div2 _IQ(0.500000000)
#define K_IQ1 _IQ(1.000000000)
#define K_IQ1divsqrt3 _IQ(0.577350269)
#define _IQ(A) (long)(A * 32768)
#define _IQmpy(A,B) ((long)A * (long)B)>>15
typedef struct { _iq I_As;
_iq I_Bs;
_iq I_Cs;
_iq I_Alpha;
_iq I_Beta;
_iq I_Ds;
_iq I_Qs;
_iq Sine;
_iq Cosine;
_iq I_QsRef;
_iq I_QsErr;
_iq I_QsKp;
_iq I_QsKi;
 Generalplus Technology Inc. 33 V1.0 – Jun. 05, 2023
Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

_iq V_QsUp;
_iq V_QsUi;
_iq V_QsOutMax;
_iq V_QsOutMin;
_iq V_QsOut;
_iq I_DsRef;

_iq I_DsErr;
_iq I_DsKp;
_iq I_DsKi;
_iq V_DsUp;
_iq V_DsUi;
_iq V_DsOutMax;
_iq V_DsOutMin;
_iq V_DsOut;
_iq V_Alpha;
_iq V_Beta;
_iq Va;
_iq Vb;
_iq Vc;
_iq t1;
_iq t2;
_iq Ta;
_iq Tb;
_iq Tc;
_iq V_QsPreOut;
_iq V_DsPreOut;
_iq V_QsSatErr;
_iq V_DsSatErr;
_iq I_QsKc;
_iq I_DsKc;
unsigned char Sector;
unsigned char Closeloop;
}FOC;
void foc(FOC *v);

Table 4.1 Struct FOC Variable


Name Description Format Range (hex)
I_As A Axis Current Q15 80000000-7FFFFFFF
I_Bs B Axis Current Q15 80000000-7FFFFFFF
Input I_Cs C Axis Current Q15 80000000-7FFFFFFF
Sine Sine Value Q15 80000000-7FFFFFFF
Cosine Cosine Value Q15 80000000-7FFFFFFF

 Generalplus Technology Inc. 34 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Name Description Format Range (hex)


I_QsRef Q Axis Reference Input Q15 80000000-7FFFFFFF
I_DsRef D Axis Reference Input Q15 80000000-7FFFFFFF
Ta A Phase PWM Q15 80000000-7FFFFFFF
Output Tb B Phase PWM Q15 80000000-7FFFFFFF
Tc C Phase PWM Q15 80000000-7FFFFFFF
Q Axis Proportional
I_QsKp Q15 80000000-7FFFFFFF
Gain
I_QsKi Q Axis Integral Gain Q15 80000000-7FFFFFFF
Q Axis Integral
I_QsKc Q15 80000000-7FFFFFFF
Compensation Gain
Q Axis Output
V_QsOutMax Q15 80000000-7FFFFFFF
Maximum Value
Q Axis Output Minimum
V_QsOutMin Q15 80000000-7FFFFFFF
Controller Value
Parameter D Axis Proportional
I_DsKp Q15 80000000-7FFFFFFF
Gain
I_DsKi D Axis Integral Gain Q15 80000000-7FFFFFFF
D Axis Integral
I_DsKc Q15 80000000-7FFFFFFF
Compensation Gain
D Axis Output
V_DsOutMax Q15 80000000-7FFFFFFF
Maximum Value
D Axis Output Minimum
V_DsOutMin Q15 80000000-7FFFFFFF
Value
I_Alpha  Axis Current Q15 80000000-7FFFFFFF
I_Beta  Axis Current Q15 80000000-7FFFFFFF
I_Qs Q Axis Current Q15 80000000-7FFFFFFF
I_Ds D Axis Current Q15 80000000-7FFFFFFF
Q Axis PI Controller
I_QsErr Q15 80000000-7FFFFFFF
Error
Q Axis Proportional
V_QsUp Q15 80000000-7FFFFFFF
Output
V_QsUi Q Axis Integral Output Q15 80000000-7FFFFFFF
Internal
V_Qs Q Axis Output Voltage Q15 80000000-7FFFFFFF
Variable
D Axis PI Controller
I_DsErr Q15 80000000-7FFFFFFF
Error
D Axis Proportional
V_DsUp Q15 80000000-7FFFFFFF
Output
V_DsUi D Axis Integral Output Q15 80000000-7FFFFFFF
V_Ds D Axis Output Voltage Q15 80000000-7FFFFFFF
V_QsSatErr Q Axis Saturation Error Q15 80000000-7FFFFFFF
Q Axis Pre-Saturation
V_QsPreOut Q15 80000000-7FFFFFFF
Output

 Generalplus Technology Inc. 35 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

Name Description Format Range (hex)


V_DsSatErr D Axis Saturation Error Q15 80000000-7FFFFFFF
D Axis Pre-Saturation
V_DsPreOut Q15 80000000-7FFFFFFF
Output
V_Alpha  Axis Output Voltage Q15 80000000-7FFFFFFF
V_Beta  Axis Output Voltage Q15 80000000-7FFFFFFF
Va A Phase Output Voltage Q15 80000000-7FFFFFFF
Vb B Phase Output Voltage Q15 80000000-7FFFFFFF
Vc C Phase Output Voltage Q15 80000000-7FFFFFFF
t1 Vector Output Time 1 Q15 80000000-7FFFFFFF
t2 Vector Output Time 2 Q15 80000000-7FFFFFFF
Unsigned
Sector Sector 0-6
Char
FOC Closed-Loop Unsigned
Closeloop 0-1
Setting Char
Internal K_IQsqrt3div2 3/2 Q15 6ED9
Constant K_IQ1div2 1/ 2 Q15 4000
K_IQ1 1.0 Q15 8000
K_IQ1divsqrt3 1/ 3 Q15 49E6

While using FOC Library, after the variables, 2-phase current (I_As and I_Bs), Sine, Cosine and DQ axis
current reference values (I_QsRef and I_DsRef), are input, user will get output 3-phase PWM duty
cycle Ta, Tb and Tc.

FOC Library sample code is shown as below:


#include "foc_v01.h"
FOC foc1 = FOC_DEFAULTS;
void periodic interrupt IRQ()
{
/************************Calculate motor rotor position.**************************/
-----As described in Section 4.6, Motor Angle Calculation.------
/***************************Calculate motor phase current.***************************/
----- As described in Section 4.6, Current Calculation.------
foc1.I_As = current_A;
foc1.I_Bs = current_B;
/************************Provide DQ axis reference current.************************/
foc1.I_QsRef = Iq_reference;
foc1.I_DsRef = Id_reference;
foc1.Sine= Sine_Tab[sin_theta];

 Generalplus Technology Inc. 36 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

foc1.Cosine = Sine_Tab[cos_theta];
/************************FOC Library Calculation**************************/
foc(&foc1);

/************************Set the PWM duty cycle.**************************/


eCCU61->CCPS0 = ((unsigned long)(_IQmpy(foc1.Ta,eCCU61->TMR_PLOADS)));
eCCU61->CCPS2 = ((unsigned long)(_IQmpy(foc1.Tb,eCCU61->TMR_PLOADS)));
eCCU61->CCPS4 = ((unsigned long)(_IQmpy(foc1.Tc,eCCU61->TMR_PLOADS)));}

In developing process, if user doesn’t need PI control, please set the variable “Closeloop” as 0. The
program will make I_QsRef equal V_Qs and I_DsRef equal V_Ds. For developing experiment, please
refer to the following description of FOC library.
if(v->Closeloop == 0)
{
v->V_QsOut = v->I_QsRef;
v->V_DsOut = v->I_DsRef;
}

4.7. OVC Overcurrent Interrupt Program Flow


Overcurrent interrupt means when an external overcurrent signal triggers this interrupt, MCU will
automatically disable PWM function to protect the system from damages. This interrupt program is
attached to IRQ.c file. This interrupt will inform the system the happening of overcurrent and disable
the PWM, so the system will also be shut down.

 Generalplus Technology Inc. 37 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

OVC IRQ

System
shoutdown

END

Figure 4.7 Overcurrent Interrupt Program Flow Chart

 Generalplus Technology Inc. 38 V1.0 – Jun. 05, 2023


Sensored FOC Control of PMSM Using GPM32F011xB
User’s Manual

5. Appendix: Related Documents

1. Description Document: Sensored FOC Control of PMSM Using GPM32F011xB_V1.0.pdf


2. Circuit Reference: E-BIKE GPM32F0118B DB V1.0.pdf
3. Sample Code: C:\ Generalplus\GPM32F011xB_BSP\E-BIKE
GPM32F0118B_DB_V10\Projects\Sensored_FOC_3Shunt

 Generalplus Technology Inc. 39 V1.0 – Jun. 05, 2023

You might also like