MKM34Z256VLx7 Bare Metal Software Drivers  R4.1.6
Reference Manual

This example shows typical use of the MMAU module for sine(x) computing. The sine(x) function is implemented using a 13th order polynomial approximation derived using a Taylor Series. Refer to matlab script for more details.

Matlab script:
function y = sine(x)
% Input arguments: x = -1 to 1, corresponds to the angle -pi to pi.
% Returns sine of input angle in range -1 to 1.
% Example: k=1;for i=-1:0.01:1 e(k)=sin(i*pi)-sine(i);k=k+1; end;plot((-1:0.01:1).*pi,floor(e*2^31));xlabel('Angle [rad]');ylabel('LSB');title ('Function Error - y=sine(x)'); grid
% Freescale Semiconductor, 2015
a = [1/2 -pi^2/FACTORIAL(3)/2 pi^4/FACTORIAL(5)/2 -pi^6/FACTORIAL(7)/2 pi^8/FACTORIAL(9)/2 -pi^10/FACTORIAL(11)/2 pi^12/FACTORIAL(13)/2];
if x > 0.5
x = 1.0-x;
elseif x < -0.5
x = -1.0-x;
end
y = pi*2*(((((((((((((a(7)*x)*x+a(6))*x)*x+a(5))*x)*x+a(4))*x)*x+a(3))*x)*x+a(2))*x)*x+a(1))*x);

The precision of the sine(x) matlab script has been compared with standard sin(x*pi) matlab function. The precision of approximated sine(x) function is in range +/-6.8e-10 as shown in the following figure.

mmau_test_sine.bmp

The matlab script has been rewritten using 64-bit mul_dl, maca_dl and d_mula_l operations supported by the MMAU module.

Source code:
/******************************************************************************
* (c) Copyright 2010-2015, Freescale Semiconductor Inc.
* ALL RIGHTS RESERVED.
******************************************************************************
* mmau_test.c
******************************************************************************/
#include "drivers.h"
/* mmau callback prototype (optional) */
static void mmau_callback (MMAU_CALLBACK_TYPE type);
static frac32 volatile result;
static const frac64 sin_coef[] =
{
FRAC64( 0.50000000000000), FRAC64(-0.82246703342411), FRAC64( 0.40587121264168),
FRAC64(-0.09537591206104), FRAC64( 0.01307392390883), FRAC64(-0.00117304051773),
FRAC64( 0.00006930000000) /* 0.00007421439652 */
};
/****************************************************************************/
static frac32 sin (frac32 x)
{
if (x > FRAC32( 0.5)) { x = FRAC32( 1.0)-x; }
else if (x < FRAC32(-0.5)) { x = FRAC32(-1.0)-x; }
mul_dl (sin_coef[6],x); /* acc= x*sin_coef[6] */
maca_dl(sin_coef[5],x); /* acc=acc*x+sin_coef[5] */
mula_l ( x); /* acc=acc*x */
maca_dl(sin_coef[4],x); /* acc=acc*x+sin_coef[4] */
mula_l ( x); /* acc=acc*x */
maca_dl(sin_coef[3],x); /* acc=acc*x+sin_coef[3] */
mula_l ( x); /* acc=acc*x */
maca_dl(sin_coef[2],x); /* acc=acc*x+sin_coef[2] */
mula_l ( x); /* acc=acc*x */
maca_dl(sin_coef[1],x); /* acc=acc*x+sin_coef[1] */
mula_l ( x); /* acc=acc*x */
maca_dl(sin_coef[0],x); /* acc=acc*x+sin_coef[0] */
mula_l ( x); /* acc=acc*x */
return l_mula_l (FRAC32(0.78539816339745))<<3; /* acc=acc*2*pi */
}
static volatile uint32 ticks;
void main (void)
{
/* initialize mmau module (optional) */
/* enable interrupts */
result = sin (FRAC32( 1.0));
result = sin (FRAC32( 0.8));
result = sin (FRAC32( 0.6));
result = sin (FRAC32( 0.4));
result = sin (FRAC32( 0.2));
result = sin (FRAC32( 0.0));
result = sin (FRAC32(-0.2));
result = sin (FRAC32(-0.4));
result = sin (FRAC32(-0.6));
result = sin (FRAC32(-0.8));
result = sin (FRAC32(-1.0));
ticks = SYST_GetCntrVal ();
while(1);
}
/* mmau callback definition */
static void mmau_callback (MMAU_CALLBACK_TYPE type)
{
if (type == DZIF_CALLBACK) { __asm volatile ("nop"); }
if (type == VIF_CALLBACK) { __asm volatile ("nop"); }
if (type == QIF_CALLBACK) { __asm volatile ("nop"); }
}

The MMAU module can provide a several-fold increase of computation performance for high-dynamic range calculations versus the most common microcontroller cores, for example ARM Cortex®-M0+.

Toolchain support:
IAR EWARM 7.40.7KEIL uVision 5.15CrossWorks 3.6ATOLLIC TrueStudio 5.3.0Kinetis Design Studio 3.0.0