/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.0
* $Date: 15/09/02 10:04a $
* @brief
* Display how to use DSP Basic math functions.
* and compare with calcultion without DSP
* @note
* Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "M451Series.h"
#include "arm_math.h"
#define blockSize 32
#define USE_DSP
float32_t srcA_buf_f32[blockSize] =
{
-0.4325648115282207, -1.6655843782380970, 0.1253323064748307,
0.2876764203585489, -1.1464713506814637, 1.1909154656429988,
1.1891642016521031, -0.0376332765933176, 0.3272923614086541,
0.1746391428209245, -0.1867085776814394, 0.7257905482933027,
-0.5883165430141887, 2.1831858181971011, -0.1363958830865957,
0.1139313135208096, 1.0667682113591888, 0.0592814605236053,
-0.0956484054836690, -0.8323494636500225, 0.2944108163926404,
-1.3361818579378040, 0.7143245518189522, 1.6235620644462707,
-0.6917757017022868, 0.8579966728282626, 1.2540014216025324,
-1.5937295764474768, -1.4409644319010200, 0.5711476236581780,
-0.3998855777153632, 0.6899973754643451
};
/* ----------------------------------------------------------------------
** Test input data of srcB for blockSize 32
** ------------------------------------------------------------------- */
float32_t srcB_buf_f32[blockSize] =
{
1.7491401329284098, 0.1325982188803279, 0.3252281811989881,
-0.7938091410349637, 0.3149236145048914, -0.5272704888029532,
0.9322666565031119, 1.1646643544607362, -2.0456694357357357,
-0.6443728590041911, 1.7410657940825480, 0.4867684246821860,
1.0488288293660140, 1.4885752747099299, 1.2705014969484090,
-1.8561241921210170, 2.1343209047321410, 1.4358467535865909,
-0.9173023332875400, -1.1060770780029008, 0.8105708062681296,
0.6985430696369063, -0.4015827425012831, 1.2687512030669628,
-0.7836083053674872, 0.2132664971465569, 0.7878984786088954,
0.8966819356782295, -0.1869172943544062, 1.0131816724341454,
0.2484350696132857, 0.0596083377937976
};
float32_t dotoutput_f32,suboutput[blockSize],multoutput[blockSize],absoutput[blockSize];
float32_t scaleoutput[blockSize],offsetoutput[blockSize],addoutput[blockSize],negoutput[blockSize];
int CalTime,i;
float sum,subout[blockSize],multout[blockSize],absout[blockSize];
float scaleout[blockSize],offsetout[blockSize],addout[blockSize],negout[blockSize];
void DOT(int size)
{
sum=0;
for(i=0;i<size;i++)
{
sum=sum+(srcA_buf_f32[i]*srcB_buf_f32[i]);
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to HIRC */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable HXT */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
CLK_SetCoreClock(72000000);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
/* Enable peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(TMR0_MODULE);
/* Peripheral clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, CLK_CLKDIV0_UART(1));
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);
}
void UART_Init(void)
{
/* Set PD multi-function pins for UART0 RXD and TXD */
SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART for printf */
UART_Init();
TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
TIMER_Start(TIMER0);
#ifdef USE_DSP
/*Calculate dot product (32 sample) with DSP*/
arm_dot_prod_f32(srcA_buf_f32, srcB_buf_f32, blockSize, &dotoutput_f32);
/*Calculate Absolute value (32 sample) with DSP*/
arm_abs_f32(srcA_buf_f32, absoutput, blockSize );
/*Calculate Addition (32 sample) with DSP*/
arm_add_f32(srcA_buf_f32, srcB_buf_f32, addoutput, blockSize);
/*Calculate Subtraction (32 sample) with DSP*/
arm_sub_f32 (srcA_buf_f32, srcB_buf_f32, suboutput, blockSize);
/*Calculate Multiplication (32 sample) with DSP*/
arm_mult_f32 (srcA_buf_f32, srcB_buf_f32, multoutput, blockSize);
/*Calculate Scale (32 sample) with DSP*/
arm_scale_f32 (srcA_buf_f32, 10, scaleoutput, blockSize);
/*Calculate Offest (32 sample) with DSP*/
arm_offset_f32 (srcA_buf_f32, 10, offsetoutput, blockSize);
/*Calculate Negate (32 sample) with DSP*/
arm_negate_f32 (srcA_buf_f32, negoutput, blockSize);
#else
/*Calculate Dot Product (32 sample) without DSP*/
DOT(blockSize);
/*Calculate Absolute value (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
absout[i]=fabs(srcA_buf_f32[i]);
}
/*Calculate Addition (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
addout[i]=srcA_buf_f32[i]+srcB_buf_f32[i];
}
/*Calculate Subtraction (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
subout[i]=srcA_buf_f32[i]-srcB_buf_f32[i];
}
/*Calculate Multiplication (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
multout[i]=srcA_buf_f32[i]*srcB_buf_f32[i];
}
/*Calculate Scale (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
scaleout[i]=10*srcA_buf_f32[i];
}
/*Calculate Offest (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
offsetout[i]=10+srcA_buf_f32[i];
}
/*Calculate Negate (32 sample) without DSP*/
for(i=0;i<blockSize;i++){
negout[i]=-srcA_buf_f32[i];
}
#endif
TIMER_Close(TIMER0);
CalTime=TIMER_GetCounter(TIMER0);
/* Display function calculation time */
printf("time is %d \n",CalTime);
while(1);
}