STMicroelectronics AN5325 User manual

AN5325

Application note

Getting started with the CORDIC accelerator using STM32CubeG4 MCU Package

Introduction

This document applies to STM32CubeG4 MCU Package, for use with STM32G4 Series microcontrollers.

The CORDIC is a hardware accelerator designed to speed up the calculation of certain mathematical functions, notably trigonometric and hyperbolic, compared to a software implementation.

The accelerator is particularly useful in motor control and related applications, where algorithms require frequent and rapid conversions between rectangular (x, y) and angular (amplitude, phase) co-ordinates.

This application note describes how the CORDIC accelerator works on STM32G4 Series microcontrollers, its capabilities and limitations, and evaluates the speed of execution for certain calculations compared with equivalent software implementations.

The example code to accompany this application note is included in the STM32CubeG4 MCU Package available on www.st.com. The examples run on the NUCLEO-G474RE board.

AN5325 - Rev 2 - March 2021

www.st.com

For further information contact your local STMicroelectronics sales office.

 

 

 

AN5325

General information

1General information

 

The STM32CubeG4 MCU Package runs on STM32G4 Series microncontrollers, based on Arm® Cortex®-M4

 

processors.

Note:

Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.

AN5325 - Rev 2

page 2/20

 

 

AN5325

CORDIC introduction

2CORDIC introduction

The CORDIC (coordinate rotation digital computer) is a low-cost successive approximation algorithm for evaluating trigonometric and hyperbolic functions.

Originally presented by Jack Volder in 1959, it was widely used inθearly calculators.

In trigonometric (circular) mode, the sine and cosine of an angle are determined by rotating the vector [0.61,

0] through decreasing angles tan-1(2-n) (n = 0, 1, 2,...) until the cumulative sum of the rotation angles equals the input angle.θThe. x and y cartesian components of the rotated vector then correspond respectively to the cosine and sine of This is illustrated in Figure 1 for an angle of 60 deg. The vector undergoes a scaling by 1/0.61 (~1.65) over the course of the calculation.

 

Figure 1. CORDIC circular mode operation

90°

 

 

3

 

60°

y (sin q)

 

0.8

 

0.6

2

 

30°

0.4

 

 

1

0.2

 

 

q

 

 

x (cos q)

Inversely, the angle of a vector [x, y] is determined by rotating 0.61[x, y] through successively decreasing angles to obtain the unit vector [1, 0]. The cumulative sum of the rotation angles gives the angle of the original vector, corresponding to arctangent (y/x).

The CORDIC algorithm can also be used for calculating hyperbolic functions (sinh, cosh, atanh) by replacing the circular rotations by hyperbolic angles tanh-1(2-j) (j = 1, 2, 3...), see Figure 2. CORDIC hyperbolic mode operation.

 

2.tanh −1

x −

1

The natural logarithm is a special case of the inverse hyperbolic tangent, obtained from the identity:

ln x =

 

+

 

The square root function is also a special case of the inverse hyperbolic tangent. When calculating the atanh the

2 2 1 2 1 2

CORDIC also calculates √(cosh t - sinh t) as a by-product. So the square root is obtained from: x = x + 4x − 4

Additional functions are calculated from the above using appropriate identities:

AN5325 - Rev 2

page 3/20

 

 

AN5325

Limitations

 

tanh x =

sinh x

 

cosh x

log 2 x =

ex = sin x + cos x

log 2 e

ln x

= 1.442695041 ln x

log 10 x =

log 10 e

ln x

= 0.434294482 ln x

The CORDIC algorithm lends itself to hardware implementation since there are no multiplications involved (the fixed scaling factor 0.61 is pre-loaded). Only add and shift operations are performed. This also means that it is ideally suited to integer arithmetic.

Figure 2. CORDIC hyperbolic mode operation

1.0

hyperbola: x2 - y2 = 1

 

0.5

y(sinh t)

3

2 1

 

t/2

0.5

1.0

x(cosh t)

2.1Limitations

In circular mode, the CORDIC converges for all angles in the range -π to π radians. The use of fixed point representation means that input and output values must be in the range -1 to +1. Input angles in radians must be multiplied by 1/π and output angles must be multiplied by π to convert back to radians.

The modulus must be in the range 0 to 1, whether converting from polar to rectangular or rectangular to polar. This means that phase(1.0, 1.0) gives false results since the modulus (√2) is out of range and saturates the CORDIC engine, even if only the phase is needed.

In hyperbolic mode, x = cosh t being defined only for x ≥ 1, all inputs and outputs are divided by 2 to remain in the fixed point numeric range. Hence the CORDIC without additional scaling supports values of x = cosh(t) in the range 1 to 2 (ie. 0.5 to 1 after division by 2), which corresponds to hyperbolic angle magnitude t in the range 0 to

1.317 (cosh-1 2).

It is possible to increase the range by additional scaling. However, the CORDIC algorithm stops converging for |t| > 1.118. This is because the successive approximation step size, |δtj| is equal to the magnitude of the hyperbolic

angle |tanh-1 2-j|, where j is the iteration number. So as j increases, the step size decreases. It so happens that as

j tends to infinity, the sum of |δtj| tends to 1.118:

j = 1tanh−1 2−j = 1.118

This is illustrated in Figure 3. Hyperbolic convergence limit.

AN5325 - Rev 2

page 4/20

 

 

AN5325

Limitations

Figure 3. Hyperbolic convergence limit

So 1.118 is the effective limit for the hyperbolic sine and cosine functions’ input magnitude. If the value of t is constrained to be smaller than this limit, then the CORDIC is used directly. Otherwise, the magnitude of t must be tested and if it is above the limit, an alternative software algorithm must be used, such as the math.h library functions coshf() and sinhf().

The magnitude of the atanh function output is similarly limited to 1.118, hence the input magnitude is limited to tanh 1.118 = 0.806.

 

2.tanh −1

x

1

As previously stated, the natural log uses the identity:

+

 

ln x =

 

 

x + 1 < 0.806 x < 9.35

Since the limit for the atanh input magnitude is 0.806, then:

Hence the limit for the natural log input is 9.35. Furthermore, the input must be scaled by the appropriate power of 2, such that 2-n.(x+1) < 1, to avoid overflowing the fixed point numerical format.

In a similar way the convergence limit for the sqrt function is calculated. Since the limit for the atanh function is

given by:

sinh t

tanh t = cosh t ≤ 0.806

and the inputs to the atanh function are sinh t and cosh

the expression: x − 0.25 = x + 0.25

t, then the input to the square root function must satisfy coshsinh tt ≤ 0.806

Hence x ≤ 2.34. Again, scaling must be applied such that 2-n.(x+0.25) < 1 to avoid saturation.

AN5325 - Rev 2

page 5/20

 

 

STMicroelectronics AN5325 User manual

AN5325

Convergence rate and precision

2.2Convergence rate and precision

In circular mode, the CORDIC algorithm converges at a rate of 1 binary digit per iteration. This means that 16 iterations are required to achieve 16-bit precision. The maximum achievable precision is limited by the number of bits in the CORDIC “engine” (the shifters and adders, as well as the table used to store the successive rotation angles), and of course in the input and output registers. The CORDIC unit in the STM32G4 Series supports 16-bit and 32-bit input and output data, and has an internal precision of 24 bits.

Figure 4. CORDIC convergence (circular mode) shows the rate of convergence for the CORDIC in circular mode. The curve labeled “q1.15” uses 16-bit input and output data. The curve labeled “q1.31” uses 32-bit data. In both cases the CORDIC “engine” is 24-bit. When the maximum residual error approaches the limit for 16-bit precision

(2-16 = 1.5 x 10-5), the quantization error caused by truncating the result to 16 bits becomes dominant, and the curve flattens out, even though the CORDIC engine continues to converge. For 32-bit data it is the quantization error of the CORDIC engine itself which starts to become significant after around 20 iterations. After 24 iterations, the successive rotation angle becomes zero and no more convergence is possible. The maximum residual error

in this case is 1.9 x 10-6, which corresponds to 19-bit precision.

In hyperbolic mode, a particularity of the algorithm requires that certain iterations must be performed twice in order to converge over the whole range. In a 24-bit CORDIC, the 4th and the 14th iterations are repeated, that is to say the same rotation angle is applied twice in a row instead of dividing by two. This means that the convergence is not linear, as seen in Figure 5. CORDIC convergence (hyperbolic mode). The error decreases

more gradually at the beginning and remains the same between iteration 14 and 15. Therefore the CORDIC takes two more iterations to achieve the same precision in hyperbolic mode than in circular mode.

Square root is an exception to the above. It converges approximately twice as fast as the other hyperbolic mode functions.

Figure 4. CORDIC convergence (circular mode)

AN5325 - Rev 2

page 6/20

 

 

Loading...
+ 14 hidden pages