/*************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=247401]@brief[/url] Display how to use DSP Convolution function
* and compare with calcultion without DSP
*
* @note
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "arm_math.h"
/*---------------------------------------------------------------------------*/
/* Define */
/*---------------------------------------------------------------------------*/
#define PLL_CLOCK 192000000
#define TEST_LENGTH_SAMPLES 320
#define BLOCK_SIZE 32
#define NUM_TAPS 29
extern float32_t testInput_f32_1kHz_15kHz[TEST_LENGTH_SAMPLES];
/*---------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------*/
float32_t firCoeffs32[NUM_TAPS] = {
-0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f,
-0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f,
+0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f,
+0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
};
float32_t conoutput[400], CONoutput[320], CONLOutput, DSPCalTime, CalTime;
uint32_t numBlocks = TEST_LENGTH_SAMPLES / BLOCK_SIZE, i = 0, j = 0, k = 0;
/*---------------------------------------------------------------------------*/
/* Functions */
/*---------------------------------------------------------------------------*/
/* Convolution (length of the first input vector, length of the second input vector) */
void Convolution(int n, int m)
{
CONLOutput = TEST_LENGTH_SAMPLES + NUM_TAPS - 1;
for (i = 0; i < CONLOutput; ++i) {
CONoutput[i] = 0;
}
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
CONoutput[i + j] += testInput_f32_1kHz_15kHz[i] * firCoeffs32[j];
}
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
/* Enable External XTAL (4~24 MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Waiting for 12MHz clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Set PCLK0/PCLK1 to HCLK/2 */
CLK->PCLKDIV = (CLK_PCLKDIV_PCLK0DIV2 | CLK_PCLKDIV_PCLK1DIV2);
/* Enable UART clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(TMR0_MODULE);
/* Select UART clock source from HXT */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/* Lock protected registers */
SYS_LockReg();
}
void UART_Init(void)
{
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int main()
{
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Init UART for printf */
UART_Init();
printf("+-----------------------------------------+\n");
printf("| DSP Convolution Sample Code |\n");
printf("+-----------------------------------------+\n\n");
/* Init Timer */
TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
/* Let TIMER0 start to count */
TIMER_Start(TIMER0);
/* Calculate convoltion with M4 DSP instruction */
arm_conv_f32(testInput_f32_1kHz_15kHz, TEST_LENGTH_SAMPLES, firCoeffs32, NUM_TAPS, conoutput);
/* Read Timer counter */
DSPCalTime = TIMER_GetCounter(TIMER0);
/* Reset Timer counter */
TIMER_ResetCounter(TIMER0);
/* Calculate convoltion */
Convolution(TEST_LENGTH_SAMPLES, NUM_TAPS);
TIMER_Close(TIMER0);
/* Read Timer counter */
CalTime = TIMER_GetCounter(TIMER0);
/* Calculate the time, timer clock source is 12M, unit is ms */
DSPCalTime = (DSPCalTime / 12000000) * 1000;
CalTime = (CalTime / 12000000) * 1000;
printf("Calculating time with DSP instruction is %f ms\n\n", DSPCalTime);
printf("Calculating time without DSP instruction is %f ms\n\n", CalTime);
printf("Efficiency increase rate is %.2f \n", CalTime / DSPCalTime);
while (1);
}