- /*************************************************************************//**
- * [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 Fast math functions(sine, cosine)
- * 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 blockSize 32
- /*---------------------------------------------------------------------------*/
- /* Global variables */
- /*---------------------------------------------------------------------------*/
- const float32_t testInput_f32[blockSize] = {
- -1.244916875853235400, -4.793533929171324800, 0.360705030233248850, 0.827929644170887320, -3.299532218312426900, 3.427441903227623800, 3.422401784294607700, -0.108308165334010680,
- 0.941943896490312180, 0.502609575000365850, -0.537345278736373500, 2.088817392965764500, -1.693168684143455700, 6.283185307179590700, -0.392545884746175080, 0.327893095115825040,
- 3.070147440456292300, 0.170611405884662230, -0.275275082396073010, -2.395492805446796300, 0.847311163536506600, -3.845517018083148800, 2.055818378415868300, 4.672594161978930800,
- -1.990923030266425800, 2.469305197656249500, 3.609002606064021000, -4.586736582331667500, -4.147080139136136300, 1.643756718868359500, -1.150866392366494800, 1.985805026477433800
- };
- float32_t cosOutput[blockSize], cosOutput1[blockSize];
- float32_t sinOutput[blockSize], sinOutput1[blockSize];
- float cosine[blockSize], cosine1[blockSize], sine[blockSize], sine1[blockSize];
- float DSPCalTime, CalTime;
- uint32_t i = 0, j = 0;
- /*---------------------------------------------------------------------------*/
- /* Functions */
- /*---------------------------------------------------------------------------*/
- void sin_cos(float theta)
- {
- if (j < blockSize) {
- sine1[j] = sin(theta);
- cosine1[j] = cos(theta);
- 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 Fast math functions Sample Code |\n");
- printf("+-----------------------------------------+\n\n");
- /* Init Timer */
- TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
- /* Let TIMER0 start to count */
- TIMER_Start(TIMER0);
- /* Calculate sine and cosine with M4 DSP instruction */
- for (i = 0; i < blockSize; i++) {
- /* input is radian */
- cosOutput[i] = arm_cos_f32(testInput_f32[i]);
- sinOutput[i] = arm_sin_f32(testInput_f32[i]);
- /* input is degree */
- arm_sin_cos_f32(testInput_f32[i], &sinOutput1[i], &cosOutput1[i]);
- }
- /* Read Timer counter */
- DSPCalTime = TIMER_GetCounter(TIMER0);
- /* Reset Timer counter */
- TIMER_ResetCounter(TIMER0);
- /* Calculate sine and cosine */
- for (i = 0; i < blockSize; i++) {
- /* input is radian */
- cosine[i] = cos(testInput_f32[i]);
- sine[i] = sin(testInput_f32[i]);
- /* input is radian */
- sin_cos(testInput_f32[i]);
- }
- 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);
- }
- /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/