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
For further information contact your local STMicroelectronics sales office.
www.st.com
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
General information
AN5325 - Rev 2
page 2/20
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
(~1.65) over the course of the calculation.
θ.
This is illustrated in Figure 1 for an angle of 60 deg. The vector undergoes a scaling by 1/0.61
90°
y (sin q)
0.8
AN5325
CORDIC introduction
Figure 1. CORDIC circular mode operation
3
60°
0.6
0.4
2
30°
1
0.2
q
x (cos q)
0°
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.
The natural logarithm is a special case of the inverse hyperbolic tangent, obtained from the identity:
x + 1
ln x = 2.tanh
−1
x − 1
The square root function is also a special case of the inverse hyperbolic tangent. When calculating the atanh the
CORDIC also calculates √(cosh2 t - sinh2 t) as a by-product. So the square root is obtained from:
x =x +
1
4
2
− x −
2
1
4
AN5325 - Rev 2
Additional functions are calculated from the above using appropriate identities:
page 3/20
AN5325
Limitations
tanh x =
ex= sinℎ x + cosℎ x
log2x = log2e ln x = 1.442695041 ln x
log10x = log10e 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
0.5
y (sinh t)
sinh x
cosh x
3
2
hyperbola: x
1
2
- y
2
= 1
2.1
t/2
0.5
1.0
x (cosh t)
Limitations
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:
∞
tanh−12
j = 1
This is illustrated in Figure 3. Hyperbolic convergence limit.
− j
= 1.118
AN5325 - Rev 2
page 4/20
Figure 3. Hyperbolic convergence limit
AN5325
Limitations
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.
As previously stated, the natural log uses the identity:
ln x =
x − 1
x + 1
−1
2.tanh
Since the limit for the atanh input magnitude is 0.806, then:
x + 1
< 0.806x < 9.35
x − 1
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:
tanh t =
sinh t
cosh t
≤ 0.806
and the inputs to the atanh function are sinh t and cosh t, then the input to the square root function must satisfy
the expression:
x − 0.25
x + 0.25
=
sinh t
cosh t
≤ 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
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
-16
(2
= 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.
AN5325
Convergence rate and precision
Figure 4. CORDIC convergence (circular mode)
AN5325 - Rev 2
page 6/20
Loading...
+ 14 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.