22. CORDIC coprocessor (CORDIC)

22.1 CORDIC introduction

The CORDIC coprocessor provides hardware acceleration of mathematical functions (mainly trigonometric ones) commonly used in motor control, metering, signal processing, and many other applications.

It speeds up the calculation of these functions compared to a software implementation, making possible the use of a lower operating frequency, or freeing up processor cycles to perform other tasks.

22.2 CORDIC main features

22.3 CORDIC functional description

22.3.1 General description

The CORDIC is a cost-efficient successive approximation algorithm for evaluating trigonometric and hyperbolic functions.

In trigonometric (circular) mode, the sine and cosine of an angle \( \theta \) are determined by rotating the unit vector \( [1, 0] \) through decreasing angles until the cumulative sum of the rotation angles equals the input angle \( \theta \) . The x and y cartesian components of the rotated vector then correspond, respectively, to the cosine and sine of \( \theta \) . Inversely, the angle of a vector \( [x, y] \) corresponding to arctangent ( \( y / x \) ), is determined by rotating \( [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.

The CORDIC algorithm can also be used for calculating hyperbolic functions (sinh, cosh, atanh), by replacing the successive circular rotations by steps along a hyperbole.

Other functions can be derived from the basic functions described above.

22.3.2 CORDIC functions

The first step when using the coprocessor is to select the required function, by programming the FUNC field of the CORDIC_CR register.

Table 149 lists the functions supported by the CORDIC coprocessor.

Table 149. CORDIC functions

FunctionPrimary argument (ARG1)Secondary argument (ARG2)Primary result (RES1)Secondary result (RES2)
Cosineangle \( \theta \)modulus m\( m \cdot \cos \theta \)\( m \cdot \sin \theta \)
Sineangle \( \theta \)modulus m\( m \cdot \sin \theta \)\( m \cdot \cos \theta \)
Phasexy\( \text{atan2}(y,x) \)\( \sqrt{x^2 + y^2} \)
Modulusxy\( \sqrt{x^2 + y^2} \)\( \text{atan2}(y,x) \)
Arctangentxnone\( \tan^{-1} x \)none
Hyperbolic cosinexnone\( \cosh x \)\( \sinh x \)
Hyperbolic sinexnone\( \sinh x \)\( \cosh x \)
Hyperbolic arctangentxnone\( \tanh^{-1} x \)none
Natural logarithmxnone\( \ln x \)none
Square rootxnone\( \sqrt{x} \)none

Several functions take two input arguments (ARG1 and ARG2), and some generate two results (RES1 and RES2) simultaneously. This is a side-effect of the algorithm and means that only one operation is needed to obtain two values. This is the case, for example, when performing polar-to-rectangular conversion: \( \sin \theta \) also generates \( \cos \theta \) , and \( \cos \theta \) also generates \( \sin \theta \) . Similarly for rectangular-to-polar conversion ( \( \text{phase}(x,y) \) , \( \text{modulus}(x,y) \) ) and for hyperbolic functions ( \( \cosh \theta \) , \( \sinh \theta \) ).

Note: The exponential function, \( \exp x \) , can be obtained as the sum of \( \sinh x \) and \( \cosh x \) . Furthermore, base N logarithms, \( \log_N x \) , can be derived by multiplying \( \ln x \) by a constant K, where \( K = 1/\ln N \) .

For certain functions (atan, log, sqrt) a scaling factor (see Section 22.3.4 ) can be applied to extend the range beyond the maximum [-1, 1] supported by the q1.31 fixed point format. The scaling factor must be set to 0 for all other circular functions, and to 1 for hyperbolic functions.

Cosine

Table 150. Cosine parameters

ParameterDescriptionRange
ARG1Angle \( \theta \) in radians, divided by \( \pi \)[-1, 1]
ARG2Modulus m[0, 1]
RES1\( m \cdot \cos \theta \)[-1, 1]
Table 150. Cosine parameters (continued)
ParameterDescriptionRange
RES2\( m \cdot \sin \theta \)[-1, 1]
SCALENot applicable0

This function calculates the cosine of an angle in the range \( -\pi \) to \( \pi \) . It can also be used to perform polar to rectangular conversion.

The primary argument is the angle \( \theta \) in radians. It must be divided by \( \pi \) before programming ARG1.

The secondary argument is the modulus \( m \) . If \( m \) is greater than 1, a scaling must be applied in software to adapt it to the q1.31 range of ARG2.

The primary result, RES1, is the cosine of the angle, multiplied by the modulus.

The secondary result, RES2, is the sine of the angle, multiplied by the modulus.

Sine

Table 151. Sine parameters
ParameterDescriptionRange
ARG1Angle \( \theta \) in radians, divided by \( \pi \)[-1, 1]
ARG2Modulus \( m \)[0, 1]
RES1\( m \cdot \sin \theta \)[-1, 1]
RES2\( m \cdot \cos \theta \)[-1, 1]
SCALENot applicable0

This function calculates the sine of an angle in the range \( -\pi \) to \( \pi \) . It can also be used to perform polar to rectangular conversion.

The primary argument is the angle \( \theta \) in radians. It must be divided by \( \pi \) before programming ARG1.

The secondary argument is the modulus \( m \) . If \( m \) is greater than 1, a scaling must be applied in software to adapt it to the q1.31 range of ARG2.

The primary result, RES1, is the sine of the angle, multiplied by the modulus.

The secondary result, RES2, is the cosine of the angle, multiplied by the modulus.

Phase

Table 152. Phase parameters
ParameterDescriptionRange
ARG1x coordinate[-1, 1]
ARG2y coordinate[-1, 1]
RES1Phase angle \( \theta \) in radians, divided by \( \pi \)[-1, 1]

Table 152. Phase parameters (continued)

ParameterDescriptionRange
RES2Modulus m[0, 1]
SCALENot applicable0

This function calculates the phase angle in the range \( -\pi \) to \( \pi \) of a vector \( \mathbf{v} = [x \ y] \) (also known as \( \text{atan2}(y,x) \) ). It can also be used to perform rectangular to polar conversion.

The primary argument is the x coordinate, that is, the magnitude of the vector in the direction of the x axis. If \( |x| > 1 \) , a scaling must be applied in software to adapt it to the q1.31 range of ARG1.

The secondary argument is the y coordinate, that is, the magnitude of the vector in the direction of the y axis. If \( |y| > 1 \) , a scaling must be applied in software to adapt it to the q1.31 range of ARG2.

The primary result, RES1, is the phase angle \( \theta \) of the vector \( \mathbf{v} \) . RES1 must be multiplied by \( \pi \) to obtain the angle in radians. Note that values close to \( \pi \) may sometimes wrap to \( -\pi \) due to the circular nature of the phase angle.

The secondary result, RES2, is the modulus, given by: \( |\mathbf{v}| = \sqrt{x^2 + y^2} \) . If \( |\mathbf{v}| > 1 \) the result in RES2 is saturated to 1.

Modulus

Table 153. Modulus parameters

ParameterDescriptionRange
ARG1x coordinate[-1, 1]
ARG2y coordinate[-1, 1]
RES1Modulus m[0, 1]
RES2Phase angle \( \theta \)[-1, 1]
SCALENot applicable0

This function calculates the magnitude, or modulus, of a vector \( \mathbf{v} = [x \ y] \) . It can also be used to perform rectangular to polar conversion.

The primary argument is the x coordinate, that is, the magnitude of the vector in the direction of the x axis. If \( |x| > 1 \) , a scaling must be applied in software to adapt it to the q1.31 range of ARG1.

The secondary argument is the y coordinate, that is, the magnitude of the vector in the direction of the y axis. If \( |y| > 1 \) , a scaling must be applied in software to adapt it to the q1.31 range of ARG2.

The primary result, RES1, is the modulus, given by: \( |\mathbf{v}| = \sqrt{x^2 + y^2} \) . If \( |\mathbf{v}| > 1 \) the result in RES1 is saturated to 1.

The secondary result, RES2, is the phase angle \( \theta \) of the vector \( \mathbf{v} \) . RES2 must be multiplied by \( \pi \) to obtain the angle in radians. Note that values close to \( \pi \) may sometimes wrap to \( -\pi \) due to the circular nature of the phase angle.

Arctangent

Table 154. Arctangent parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[-1, 1]
ARG2Not applicable-
RES1\( 2^{-n} \cdot \tan^{-1} x \) , in radians, divided by \( \pi \)[-1, 1]
RES2Not applicable-
SCALEn[0 7]

This function calculates the arctangent, or inverse tangent, of the input argument x.

The primary argument, ARG1, is the input value, \( x = \tan \theta \) . If \( |x| > 1 \) , a scaling factor of \( 2^{-n} \) must be applied in software such that \( -1 < x \cdot 2^{-n} < 1 \) . The scaled value \( x \cdot 2^{-n} \) is programmed in ARG1 and the scale factor n must be programmed in the SCALE parameter.

Note that the maximum input value allowed is \( \tan \theta = 128 \) , which corresponds to an angle \( \theta = 89.55 \) degrees. For \( |x| > 128 \) , a software method must be used to find \( \tan^{-1} x \) .

The secondary argument, ARG2, is unused.

The primary result, RES1, is the angle \( \theta = \tan^{-1} x \) . RES1 must be multiplied by \( 2^n \cdot \pi \) to obtain the angle in radians.

The secondary result, RES2, is unused.

Hyperbolic cosine

Table 155. Hyperbolic cosine parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[-0.559 0.559]
ARG2Not applicable-
RES1\( 2^{-n} \cdot \cosh x \)[0.5 0.846]
RES2\( 2^{-n} \cdot \sinh x \)[-0.683 0.683]
SCALEn1

This function calculates the hyperbolic cosine of a hyperbolic angle x. It can also be used to calculate the exponential functions \( e^x = \cosh x + \sinh x \) , and \( e^{-x} = \cosh x - \sinh x \) .

The primary argument is the hyperbolic angle x. Only values of x in the range -1.118 to +1.118 are supported. Since the minimum value of \( \cosh x \) is 1, which is beyond the range of the q1.31 format, a scaling factor of \( 2^{-n} \) must be applied in software. The factor n = 1 must be programmed in the SCALE parameter.

The secondary argument is not used.

The primary result, RES1, is the hyperbolic cosine, \( \cosh x \) . RES1 must be multiplied by 2 to obtain the correct result.

The secondary result, RES2, is the hyperbolic sine, \( \sinh x \) . RES2 must be multiplied by 2 to obtain the correct result.

Hyperbolic sine

Table 156. Hyperbolic sine parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[-0.559, 0.559]
ARG2Not applicable-
RES1\( 2^{-n} \cdot \sinh x \)[-0.683, 0.683]
RES2\( 2^{-n} \cdot \cosh x \)[0.5, 0.846]
SCALEn1

This function calculates the hyperbolic sine of a hyperbolic angle \( x \) . It can also be used to calculate the exponential functions \( e^x = \cosh x + \sinh x \) , and \( e^{-x} = \cosh x - \sinh x \) .

The primary argument is the hyperbolic angle \( x \) . Only values of \( x \) in the range -1.118 to +1.118 are supported. For all input values, a scaling factor of \( 2^{-n} \) must be applied in software, where \( n = 1 \) . The scaled value \( x \cdot 0.5 \) is programmed in ARG1 and the factor \( n = 1 \) must be programmed in the SCALE parameter.

The secondary argument is not used.

The primary result, RES1, is the hyperbolic sine, \( \sinh x \) . RES1 must be multiplied by 2 to obtain the correct result.

The secondary result, RES2, is the hyperbolic cosine, \( \cosh x \) . RES2 must be multiplied by 2 to obtain the correct result.

Hyperbolic arctangent

Table 157. Hyperbolic arctangent parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[-0.403, 0.403]
ARG2Not applicable-
RES1\( 2^{-n} \cdot \operatorname{atanh} x \)[-0.559, 0.559]
RES2Not applicable-
SCALEn1

This function calculates the hyperbolic arctangent of the input argument \( x \) .

The primary argument is the input value \( x \) . Only values of \( x \) in the -0.806 to +0.806 range are supported. The value \( x \) must be scaled by a factor \( 2^{-n} \) , where \( n = 1 \) . The scaled value

\( x \cdot 0.5 \) is programmed in ARG1 and the factor \( n = 1 \) must be programmed in the SCALE parameter.

The secondary argument is not used.

The primary result is the hyperbolic arctangent, \( \operatorname{atanh} x \) . RES1 must be multiplied by 2 to obtain the correct value.

The secondary result is not used.

Natural logarithm

Table 158. Natural logarithm parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[0.054 0.875]
ARG2Not applicable-
RES1\( 2^{-(n+1)} \cdot \ln x \)[-0.279 0.137]
RES2Not applicable-
SCALEn[1 4]

This function calculates the natural logarithm of the input argument x.

The primary argument is the input value x. Only values of x in the range 0.107 to 9.35 are supported. The value x must be scaled by a factor \( 2^{-n} \) , such that \( x \cdot 2^{-n} < 1 - 2^{-n} \) . The scaled value \( x \cdot 2^{-n} \) is programmed in ARG1 and the factor n must be programmed in the SCALE parameter.

Table 159 lists the valid scaling factors, n, and the corresponding ranges of x and ARG1.

Table 159. Natural log scaling factors and corresponding ranges

nx rangeARG1 range
1\( 0.107 \leq x < 1 \)\( 0.0535 \leq \text{ARG1} < 0.5 \)
2\( 1 \leq x < 3 \)\( 0.25 \leq \text{ARG1} < 0.75 \)
3\( 3 \leq x < 7 \)\( 0.375 \leq \text{ARG1} < 0.875 \)
4\( 7 \leq x \leq 9.35 \)\( 0.4375 \leq \text{ARG1} < 0.584 \)

The secondary argument is not used.

The primary result is the natural logarithm, \( \ln x \) . RES1 must be multiplied by \( 2^{(n+1)} \) to obtain the correct value.

The secondary result is not used.

Square root

Table 160. Square root parameters

ParameterDescriptionRange
ARG1\( x \cdot 2^{-n} \)[0.027 0.875]
ARG2Not applicable-
RES1\( 2^{-n} \sqrt{x} \)[0.04 1]
RES2Not applicable-
SCALEn[0 2]

This function calculates the square root of the input argument x.

The primary argument is the input value x. Only values of x in the range 0.027 to 2.34 are supported. The value x must be scaled by a factor \( 2^{-n} \) , such that \( x \cdot 2^{-n} < (1 - 2^{-(n-2)}) \) .

The scaled value \( x \cdot 2^{-n} \) is programmed in ARG1 and the factor n must be programmed in the SCALE parameter.

Table 161 lists the valid scaling factors, n, and the corresponding ranges of x and ARG1.

Table 161. Square root scaling factors and corresponding ranges

nx rangeARG1 range
0\( 0.027 \leq x < 0.75 \)\( 0.027 \leq \text{ARG1} < 0.75 \)
1\( 0.75 \leq x < 1.75 \)\( 0.375 \leq \text{ARG1} < 0.875 \)
2\( 1.75 \leq x \leq 2.341 \)\( 0.4375 \leq \text{ARG1} \leq 0.585 \)

The secondary argument is not used.

The primary result is the square root of x. RES1 must be multiplied by \( 2^n \) to obtain the correct value.

The secondary result is not used.

22.3.3 Fixed point representation

The CORDIC operates in fixed point signed integer format. Input and output values can be either q1.31 or q1.15.

In q1.31 format, numbers are represented by one sign bit and 31 fractional bits (binary decimal places). The numeric range is therefore -1 (0x80000000) to \( 1 - 2^{-31} \) (0x7FFFFFFF).

In q1.15 format, the numeric range is 1 (0x8000) to \( 1 - 2^{-15} \) (0x7FFF). This format has the advantage that two input arguments can be packed into a single 32-bit write, and two results can be fetched in one 32-bit read.

22.3.4 Scaling factor

Several of the functions listed in Section 22.3.2 specify a scaling factor, SCALE. This allows the function input range to be extended to cover the full range of values supported by the CORDIC, without saturating the input, output, or internal registers. If the scaling factor is

required, it must be calculated by software and programmed into the SCALE field of the CORDIC_CSR register. The input arguments must be scaled accordingly before programming the scaled values in the CORDIC_WDATA register. The scaling must also be undone on the results read from the CORDIC_RDATA register.

Note: The scaling factor entails a loss of precision due to truncation of the scaled value.

22.3.5 Precision

The precision of the result is dependent on the number of CORDIC iterations. The algorithm converges at a constant rate of one binary digit per iteration for trigonometric functions (sine, cosine, phase, modulus), see Figure 159 .

For hyperbolic functions (hyperbolic sine, hyperbolic cosine, natural logarithm), the convergence rate is less constant due to the peculiarities of the CORDIC algorithm (see Figure 160 ). The square root function converges at roughly twice the speed of the hyperbolic functions (see Figure 161 ).

Figure 159. CORDIC convergence for trigonometric functions

Figure 159: CORDIC convergence for trigonometric functions. A log-linear plot showing Max absolute error vs Iterations for Sine/Cosine convergence of q1.23 fixed point Cordic. The y-axis is logarithmic from 10^0 to 10^-8. The x-axis is linear from 0 to 24 iterations. Two curves are shown: q1.15 (green) and q1.31 (blue).

The graph titled "Sine/Cosine Convergence of q1.23 fixed point Cordic" shows the maximum absolute error decreasing as the number of iterations increases. The y-axis is logarithmic, ranging from \( 10^0 \) down to \( 10^{-8} \) . The x-axis shows iterations from 0 to 24. Two data series are plotted: a green line labeled 'q1.15' and a blue line labeled 'q1.31'. Both follow a similar downward slope until approximately 14 iterations. The q1.15 curve levels off at an error of approximately \( 10^{-5} \) after 20 iterations, while the q1.31 curve continues to decrease, reaching approximately \( 10^{-7} \) at 24 iterations.

Iterationsq1.15 Max absolute error (approx)q1.31 Max absolute error (approx)
0\( 10^0 \)\( 10^0 \)
4\( 10^{-1} \)\( 10^{-1} \)
8\( 10^{-2} \)\( 10^{-2} \)
12\( 10^{-3} \)\( 10^{-3} \)
16\( 4 \times 10^{-5} \)\( 10^{-4} \)
20\( 2 \times 10^{-5} \)\( 10^{-6} \)
24\( 2 \times 10^{-5} \)\( 10^{-7} \)
Figure 159: CORDIC convergence for trigonometric functions. A log-linear plot showing Max absolute error vs Iterations for Sine/Cosine convergence of q1.23 fixed point Cordic. The y-axis is logarithmic from 10^0 to 10^-8. The x-axis is linear from 0 to 24 iterations. Two curves are shown: q1.15 (green) and q1.31 (blue).

Figure 160. CORDIC convergence for hyperbolic functions

Line graph titled 'Hyperbolic Sine/Cosine Convergence of q1.23 fixed point Cordic' showing Max absolute error vs Iterations for q1.15 and q1.31 formats.

The graph illustrates the convergence of hyperbolic sine and cosine functions using a CORDIC algorithm. The y-axis represents the 'Max absolute error' on a logarithmic scale from \( 10^0 \) to \( 10^{-8} \) . The x-axis represents 'Iterations' from 0 to 24. Two data series are plotted: a green line for the q1.15 fixed-point format and a blue line for the q1.31 format. Both series show a rapid decrease in error as iterations increase. The q1.31 series continues to decrease more rapidly than the q1.15 series after approximately 15 iterations, reaching a lower final error of about \( 10^{-7} \) compared to the q1.15 series' error of about \( 10^{-5} \) .

Iterationsq1.15 Max absolute errorq1.31 Max absolute error
0\( 10^0 \)\( 10^0 \)
4\( 10^{-1} \)\( 10^{-1} \)
8\( 10^{-2} \)\( 10^{-2} \)
12\( 10^{-3} \)\( 10^{-3} \)
16\( 10^{-4} \)\( 10^{-4} \)
20\( 10^{-5} \)\( 10^{-6} \)
24\( 10^{-5} \)\( 10^{-7} \)
Line graph titled 'Hyperbolic Sine/Cosine Convergence of q1.23 fixed point Cordic' showing Max absolute error vs Iterations for q1.15 and q1.31 formats.

Figure 161. CORDIC convergence for square root

Figure 161. CORDIC convergence for square root. A log-linear plot titled 'Square Root Convergence of q1.23 fixed point Cordic'. The y-axis is 'Max absolute error' on a logarithmic scale from 10^0 to 10^-8. The x-axis is 'Iterations' from 0 to 12. Two curves are shown: 'q1.15' (green) and 'q1.31' (blue). Both curves show a linear decrease in error until approximately iteration 8, after which the error levels off due to quantization. The q1.31 curve reaches a lower final error (around 10^-7) compared to the q1.15 curve (around 10^-5).
Figure 161. CORDIC convergence for square root. A log-linear plot titled 'Square Root Convergence of q1.23 fixed point Cordic'. The y-axis is 'Max absolute error' on a logarithmic scale from 10^0 to 10^-8. The x-axis is 'Iterations' from 0 to 12. Two curves are shown: 'q1.15' (green) and 'q1.31' (blue). Both curves show a linear decrease in error until approximately iteration 8, after which the error levels off due to quantization. The q1.31 curve reaches a lower final error (around 10^-7) compared to the q1.15 curve (around 10^-5).

Note: The convergence rate decreases as the quantization error starts to become significant.

The CORDIC can perform four iterations per clock cycle. For each function, the maximum error remaining after every four iterations is shown in Table 162 , together with the number of clock cycles required to reach that precision. From this table, the desired number of cycles can be determined and programmed in the PRECISION field of the CORDIC_CR register. The coprocessor stops as soon as the programmed number of iterations is completed, and the result can be read immediately.

Table 162. Precision vs. number of iterations

FunctionNumber of iterationsNumber of cyclesMax residual error (1)
q1.31 formatq1.15 format
Sin, Cos,
Phase (2) , Mod,
Atan (4)
41\( 2^{-3} \)\( 2^{-3} \)
82\( 2^{-7} \)\( 2^{-7} \)
123\( 2^{-11} \)\( 2^{-11} \)
164\( 2^{-15} \)\( 2^{-15} \)
205\( 2^{-18} \)\( 2^{-16} \)
246\( 2^{-19} \)\( 2^{-16} \)

Table 162. Precision vs. number of iterations (continued)

FunctionNumber of iterationsNumber of cyclesMax residual error (1)
q1.31 formatq1.15 format
Sinh, Cosh, Atanh, Ln (3)41\( 2^{-2} \)\( 2^{-2} \)
82\( 2^{-6} \)\( 2^{-6} \)
123\( 2^{-10} \)\( 2^{-10} \)
164\( 2^{-13} \)\( 2^{-13} \)
205\( 2^{-17} \)\( 2^{-15} \)
246\( 2^{-18} \)\( 2^{-15} \)
Sqrt (4)41\( 2^{-7} \)\( 2^{-7} \)
82\( 2^{-14} \)\( 2^{-14} \)
123\( 2^{-19} \)\( 2^{-15} \)
  1. 1. Max residual error is the maximum error remaining after the given number of iterations, compared to the identical calculation performed in double precision floating point. An additional rounding error may be incurred, of up to \( 2^{-16} \) for q15 format or \( 2^{-20} \) for q31 format.
  2. 2. For modulus \( > 0.5 \) . The achievable precision reduces proportionally to the magnitude of the modulus, as quantization error becomes significant.
  3. 3. SCALE = 1. If a higher scaling factor is used, the achievable precision is reduced proportionally.
  4. 4. SCALE = 0. If a higher scaling factor is used, the achievable precision is reduced proportionally.

22.3.6 Zero-overhead mode

The fastest way to use the coprocessor is to preprogram the CORDIC_CSR register with the function to be performed (FUNC), the desired number of clock cycles (PRECISION), the size of the input and output values (ARGSIZE, RESSIZE), the number of input arguments (NARGS) and/or results (NRES), and the scaling factor (SCALE), if applicable.

The calculation is triggered by writing the input arguments to the CORDIC_WDATA register. As soon as the correct number of input arguments has been written (and any ongoing calculation has finished), a new calculation is launched using these input arguments and the current CORDIC_CSR settings. There is no need to reprogram the CORDIC_CSR register if there is no change.

If a dual 32-bit input argument is needed (ARGSIZE = 0, NARGS = 1), the primary input argument (ARG1) must be written first, followed by the secondary argument (ARG2). If the secondary argument remains unchanged for a series of calculations, the second write can be avoided, by reprogramming the number of arguments to one (NARGS = 0), once the first calculation has started. The secondary argument retains its programmed value as long as the function is not changed.

Note: ARG2 is set to +1 (0x7FFFFFFF) after a reset.

If two 16-bit arguments are used (ARGSIZE = 1) they must be packed into a 32-bit word, with ARG1 in the least significant half-word and ARG2 in the most significant half-word. The packed 32-bit word is then written to the CORDIC_WDATA register. Only one write is needed in this case (NARGS = 0).

For functions taking only one input argument, ARG1, it is recommended to set NARGS = 0. If NARGS = 1, a second write to CORDIC_WDATA must be performed to trigger the calculation. The ARG2 data in this case is not used.

Once the calculation starts, any attempt to read the CORDIC_RDATA register inserts bus wait-states until the calculation is completed, before returning the result. It is then possible for the software to write the input and immediately read the result without polling to see if it is valid. Alternatively, the processor can wait for the appropriate number of clock cycles before reading the result. This time can be used to program the CORDIC_CSR register for the next calculation, and prepare the next input data, if needed. The CORDIC_CSR register can be reprogrammed while a calculation is in progress, without affecting the result of the ongoing calculation. In the same way, the CORDIC_WDATA register can be updated with the next argument(s) once the previous ones have been taken into account. The next arguments and settings remain pending until the previous calculation has completed.

When a calculation is finished, the result(s) can be read from the CORDIC_RDATA register. If two 32-bit results are expected (NRES = 1, RESSIZE = 0), the primary result (RES1) is read out first, followed by the secondary result (RES2). If only one 32-bit result is expected (NRES = 0, RESSIZE = 0), then RES1 is output on the first read.

If 16-bit results are expected (RESSIZE = 1), a single read to CORDIC_RDATA fetches both results packed into a 32-bit word. RES1 is in the lower half-word, and RES2 in the upper half-word. In this case, it is recommended to program NRES = 0. IF NRES = 1, a second read of CORDIC_RDATA must be performed to free up the CORDIC for the next operation. The data from this second read must be discarded.

The next calculation starts when the expected number of results has been read, provided the expected number of arguments has been written. This means that at any time, there can be a calculation in progress, or waiting for the results to be read, and an operation pending. Any further access to CORDIC_WDATA while an operation is pending cancels it and overwrites the data.

The following sequence summarizes the use of the CORDIC_IP in zero-overhead mode:

  1. 1. Program the CORDIC_CSR register with the appropriate settings
  2. 2. Program the argument(s) for the first calculation in the CORDIC_WDATA register. This launches the first calculation.
  3. 3. If needed, update the CORDIC_CSR register settings for the next calculation.
  4. 4. Program the argument(s) for the next calculation in the CORDIC_WDATA register.
  5. 5. Read the result(s) from the CORDIC_RDATA register. This triggers the next calculation.
  6. 6. Go to step 3.

22.3.7 Polling mode

When a new result is available in the CORDIC_RDATA register, the RRDY flag is set in the CORDIC_CSR register. The flag can be polled by reading the register. It is reset by reading the CORDIC_RDATA register (once or twice, depending on the NRES field of the CORDIC_CSR register).

Polling the RRDY flag takes slightly longer than reading the CORDIC_RDATA register directly, since the result is not read as soon as it is available. The processor and bus interface are not stalled while reading the CORDIC_CSR register, so this mode may be of interest if stalling the processor is not acceptable (for example, if low latency interrupts must be serviced).

22.3.8 Interrupt mode

By setting the interrupt enable (IE) bit in the CORDIC_CSR register, an interrupt is generated whenever the RRDY flag is set. The interrupt is cleared when the flag is reset.

This mode allows the result of the calculation to be read under interrupt service routine, and hence given a priority relative to other tasks. However, it is slower than directly reading the result, or polling the flag, due to the interrupt handling delays.

22.3.9 DMA mode

If the DMA write enable (DMAWEN) bit is set in the CORDIC_CSR register, and no operation is pending, a DMA write channel request is generated. The DMA controller can transfer a primary input argument (ARG1) from memory into the CORDIC_WDATA register. Writing into the register deasserts the DMA request. If NARGS = 1 in the CORDIC_CSR register, a second DMA write channel request is generated to transfer the secondary input argument (ARG2) into the CORDIC_WDATA register. When all input arguments have been written, and any ongoing calculation has been completed (by reading the results), a new calculation is started and another DMA write channel request is generated.

If the DMA read enable (DMAREN) bit is set in the CORDIC_CSR register, the RRDY flag going active generates a DMA read channel request. The DMA controller can then transfer the primary result (RES1) from the CORDIC_RDATA register to memory. Reading the register deasserts the DMA request. If NRES = 1 in the CORDIC_CSR register, a second DMA request is generated to read out the secondary result (RES2). When all results have been read, the RRDY flag is deasserted.

The DMA read and write channels can be enabled separately. If both channels are enabled, the CORDIC can autonomously perform repeated calculations on a buffer of data without processor intervention. This allows the processor to perform other tasks. The DMA controller is operating in memory-to-peripheral mode for the write channel, and peripheral-to-memory mode for the read channel. The sequence is started by the processor setting the DMAWEN flag, the DMA read and write requests are generated as fast as the CORDIC can process the data.

In some cases, the input data may be stored in memory, and the output is transferred at regular intervals to another peripheral, such as a digital-to-analog converter. In this case, the destination peripheral generates a DMA request each time it needs a new data. The DMA controller can directly fetch the next sample from the CORDIC_RDATA register (in this case the DMA controller is operating in memory-to-peripheral mode, even though the source is a peripheral register). The act of reading the result allows the CORDIC to start a new calculation, which in turn generates a DMA write channel request, and the DMA controller transfers the next input value to the CORDIC_WDATA register. The DMA write channel is enabled (DMAWEN = 1), but the read channel must not be enabled.

In a similar way, data coming from another peripheral, such as an ADC, can be transferred directly to the CORDIC_WDATA register (in peripheral-to-memory mode). The DMA write channel must not be enabled. The CORDIC processes the input data and generates a DMA read request when complete, if DMAREN = 1. The DMA controller then transfers the result from CORDIC_RDATA register to memory (peripheral-to-memory mode).

Note: No DMA request is generated to program the CORDIC_CSR register. DMA mode is therefore useful only when repeatedly performing the same function with the same settings. The scale factor cannot be changed during a series of DMA transfers.

Note: Each DMA request must be acknowledged, as a result of the DMA performing an access to the CORDIC_WDATA or CORDIC_RDATA register. If an extraneous access to the relevant register occurs before this, the acknowledge is asserted prematurely, and may block the DMA channel. Therefore, when the DMA read channel is enabled, CPU access to the CORDIC_RDATA register must be avoided. Similarly, the processor must avoid accessing the CORDIC_WDATA register when the DMA write channel is enabled.

22.4 CORDIC registers

The CORDIC registers can be accessed only in 32-bit word format

22.4.1 CORDIC control/status register (CORDIC_CSR)

Address offset: 0x00

Reset value: 0x0000 0050

31302928272625242322212019181716
RRDYRes.Res.Res.Res.Res.Res.Res.Res.ARG
SIZE
RES
SIZE
N
ARGS
NRESDMA
WEN
DMA
REN
IEN
rrwrwrwrwrwrwrw

1514131211109876543210
Res.Res.Res.Res.Res.SCALE[2:0]PRECISION[3:0]FUNC[3:0]
rwrwrwrwrwrwrwrwrwrwrw

Bit 31 RRDY : Result ready flag

0: No new data in output register

1: CORDIC_RDATA register contains new data.

This bit is set by hardware when a CORDIC operation completes. It is reset by hardware when the CORDIC_RDATA register is read (NRES+1) times.

When this bit is set, if the IEN bit is also set, the CORDIC interrupt is asserted. If the DMAREN bit is set, a DMA read channel request is generated. While this bit is set, no new calculation is started.

Bits 30:23 Reserved, must be kept at reset value.

Bit 22 ARGSIZE : Width of input data

0: 32-bit

1: 16-bit

ARGSIZE selects the number of bits used to represent input data.

If 32-bit data is selected, the CORDIC_WDATA register expects arguments in q1.31 format.

If 16-bit data is selected, the CORDIC_WDATA register expects arguments in q1.15 format.

The primary argument (ARG1) is written to the least significant half-word, and the secondary argument (ARG2) to the most significant half-word.

Bit 21 RESSIZE : Width of output data

0: 32-bit

1: 16-bit

RESSIZE selects the number of bits used to represent output data.

If 32-bit data is selected, the CORDIC_RDATA register contains results in q1.31 format.

If 16-bit data is selected, the least significant half-word of CORDIC_RDATA contains the primary result (RES1) in q1.15 format, and the most significant half-word contains the secondary result (RES2), also in q1.15 format.

Bit 20 NARGS : Number of arguments expected by the CORDIC_WDATA register

0: Only one 32-bit write (or two 16-bit values if ARGSIZE = 1) is needed for the next calculation.

1: Two 32-bit values must be written to the CORDIC_WDATA register to trigger the next calculation.

Reads return the current state of the bit.

Bit 19 NRES : Number of results in the CORDIC_RDATA register

0: Only one 32-bit value (or two 16-bit values if RESSIZE = 1) is transferred to the CORDIC_RDATA register on completion of the next calculation. One read from CORDIC_RDATA resets the RRDY flag.

1: Two 32-bit values are transferred to the CORDIC_RDATA register on completion of the next calculation. Two reads from CORDIC_RDATA are necessary to reset the RRDY flag.

Reads return the current state of the bit.

Bit 18 DMAWEN : Enable DMA write channel

0: Disabled. No DMA write requests are generated.

1: Enabled. Requests are generated on the DMA write channel whenever no operation is pending

This bit is set and cleared by software. A read returns the current state of the bit.

Bit 17 DMAREN : Enable DMA read channel

0: Disabled. No DMA read requests are generated.

1: Enabled. Requests are generated on the DMA read channel whenever the RRDY flag is set.

This bit is set and cleared by software. A read returns the current state of the bit.

Bit 16 IEN : Enable interrupt.

0: Disabled. No interrupt requests are generated.

1: Enabled. An interrupt request is generated whenever the RRDY flag is set.

This bit is set and cleared by software. A read returns the current state of the bit.

Bits 15:11 Reserved, must be kept at reset value.

Bits 10:8 SCALE[2:0] : Scaling factor

The value of this field indicates the scaling factor applied to the arguments and/or results. A value n implies that the arguments have been multiplied by a factor \( 2^{-n} \) , and/or the results need to be multiplied by \( 2^n \) . Refer to Section 22.3.2 for the applicability of the scaling factor for each function and the appropriate range.

Bits 7:4 PRECISION[3:0] : Precision required (number of iterations)

0: reserved

1 to 15: (Number of iterations)/4

To determine the number of iterations needed for a given accuracy refer to Table 162 .

Note that for most functions, the recommended range for this field is 3 to 6.

Bits 3:0 FUNC[3:0] : Function

22.4.2 CORDIC argument register (CORDIC_WDATA)

Address offset: 0x04

Reset value: 0xXXXX XXXX

31302928272625242322212019181716
ARG[31:16]
wwwwwwwwwwwwwwww
1514131211109876543210
ARG[15:0]
wwwwwwwwwwwwwwww

Bits 31:0 ARG[31:0] : Function input arguments

This register is programmed with the input arguments for the function selected in the CORDIC_CSR register FUNC field.

If 32-bit format is selected (CORDIC_CSR.ARGSIZE = 0) and two input arguments are required (CORDIC_CSR.NARGS = 1), two successive writes are required to this register. The first writes the primary argument (ARG1), the second writes the secondary argument (ARG2).

If 32-bit format is selected and only one input argument is required (NARGS = 0), only one write is required to this register, containing the primary argument (ARG1).

If 16-bit format is selected (CORDIC_CSR.ARGSIZE = 1), one write to this register contains both arguments. The primary argument (ARG1) is in the lower half, ARG[15:0], and the secondary argument (ARG2) is in the upper half, ARG[31:16]. In this case, NARGS must be set to 0.

Refer to Section 22.3.2 for the arguments required by each function, and their permitted range.

When the required number of arguments has been written, the CORDIC evaluates the function designated by CORDIC_CSR.FUNC using the supplied input arguments, provided any previous calculation has completed. If a calculation is ongoing, the ARG1 and ARG 2 values are held pending until the calculation is completed and the results read. During this time, a write to the register cancels the pending operation and overwrite the argument data.

22.4.3 CORDIC result register (CORDIC_RDATA)

Address offset: 0x08

Reset value: 0x0000 0000

31302928272625242322212019181716
RES[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
RES[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 RES[31:0] : Function result

If 32-bit format is selected (CORDIC_CSR.RESSIZE = 0) and two output values are expected (CORDIC_CSR.NRES = 1), this register must be read twice when the RRDY flag is set. The first read fetches the primary result (RES1). The second read fetches the secondary result (RES2) and resets RRDY.

If 32-bit format is selected and only one output value is expected (NRES = 0), only one read of this register is required to fetch the primary result (RES1) and reset the RRDY flag.

If 16-bit format is selected (CORDIC_CSR.RESSIZE = 1), this register contains the primary result (RES1) in the lower half, RES[15:0], and the secondary result (RES2) in the upper half, RES[31:16]. In this case, NRES must be set to 0, and only one read performed.

A read from this register resets the RRDY flag in the CORDIC_CSR register.

22.4.4 CORDIC register map

Table 163. CORDIC register map and reset value

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00CORDIC_CSRRRDYResResResResResResResResARGSIZERESSIZENARGSNRESDMAWENDMARENIENResResResResSCALE [2:0]PRECISION [3:0]FUNC [3:0]
Reset value00000000000001010000
0x04CORDIC_WDATAARG[31:0]
Reset valuexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
0x08CORDIC_RDATARES[31:0]
Reset value0000000000000000000000000000000

Refer to Section 2.3: Memory organization for the register boundary addresses.