• AVR Freaks

Error in dsPIC30F FFT Code. Please help!

Author
indula
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/19 06:35:00
  • Location: 0
  • Status: offline
2019/11/14 23:55:58 (permalink)
0

Error in dsPIC30F FFT Code. Please help!

I'm new to micro-controllers. I've found the following code to calculate FFT on a signal given to dsPIC30F6012A. But there is a error that fails the build. Following is the code that I used. Please help me to know how to proceed. 
#include <xc.h>
#include <p30f6012a.h>
#include <dsp.h>

#define FFT_BLOCK_LENGTH 256
#define LOG2_BLOCK_LENGTH 8
#define SAMPLING_RATE 10000 

unsigned long counter;
fractcomplex sigCmpx[FFT_BLOCK_LENGTH] __attribute__ ((space(ymemory)));
fractcomplex twiddleFactors[FFT_BLOCK_LENGTH/2] __attribute__ ((space(xmemory)));
fractional SquareMagnitude [FFT_BLOCK_LENGTH/2];

int peakFrequencyBin = 0; /* Declare post-FFT variables to compute the */

unsigned long peakFrequency = 0; /* frequency of the largest spectral component */

void start_timer(unsigned int time_value){
    T1CONbits.TON = 0;
    TMR1 = 0;
    PR1 = time_value;
    T1CONbits.TON = 1;
    return;
}

fractional getADCValue(){
    unsigned int ADCValue;
    ADCON1bits.SAMP = 1; 
    while(!ADCON1bits.DONE); //checks if conversion is done
    ADCValue = ADCBUF0;
    return ADCValue - 16384;
}

void __attribute__((interrupt, no_auto_psv))_T1Interrupt(void){
    if (counter < FFT_BLOCK_LENGTH){
        sigCmpx[counter].real = getADCValue();
        sigCmpx[counter].imag = 0;
    }else{
        int i = 0;
        fractional *p_real = &sigCmpx[0].real ;
        fractcomplex *p_cmpx = &sigCmpx[0] ;
        fractional *p_fract = &sigCmpx[0].real ;

        for ( i = 0; i < FFT_BLOCK_LENGTH; i++ )/* The FFT function requires input data */
        { /* to be in the fractional fixed-point range [-0.5, +0.5]*/
            *p_real = *p_real >>1 ; /* So, we shift all data samples by 1 bit to the right. */
            *p_real++; /* Should you desire to optimize this process, perform */
        } /* data scaling when first obtaining the time samples */
      /* Or within the BitReverseComplex function source code */

        p_real = &sigCmpx[(FFT_BLOCK_LENGTH/2)-1].real ; /* Set up pointers to convert real array */
        p_cmpx = &sigCmpx[FFT_BLOCK_LENGTH-1] ; /* to a complex array. The input array initially has all */
      /* the real input samples followed by a series of zeros */
        for ( i = FFT_BLOCK_LENGTH; i > 0; i-- ) /* Convert the Real input sample array */
        { /* to a Complex input sample array */
            (*p_cmpx).real = (*p_real--); /* We will simpy zero out the imaginary */
            (*p_cmpx--).imag = 0x0000; /* part of each data sample */
        }

 /* Perform FFT operation */
        FFTComplexIP (LOG2_BLOCK_LENGTH, &sigCmpx[0], &twiddleFactors[0], COEFFS_IN_DATA);
        
 /* Store output samples in bit-reversed order of their addresses */
        BitReverseComplex(LOG2_BLOCK_LENGTH, &sigCmpx[0]);

 /* Compute the square magnitude of the complex FFT output array so we have a Real output vetor */
        SquareMagnitudeCplx(FFT_BLOCK_LENGTH, &sigCmpx[0], &sigCmpx[0].real);
        sigCmpx[0].real = 0; 
        sigCmpx[0].imag = 0;
        p_fract = &sigCmpx[0].real ; //inicia ponteiro
        for( i = 0; i < FFT_BLOCK_LENGTH/2; i++ ) 
        {
            SquareMagnitude [i] = *p_fract; 
            *p_fract++; //incrementa o ponteiro 
        }
 /* Find the frequency Bin ( = index into the SigCmpx[] array) that has the largest energy*/
 /* i.e., the largest spectral component */
        VectorMax(FFT_BLOCK_LENGTH/2, &SquareMagnitude[0], &peakFrequencyBin);
 /* Compute the frequency (in Hz) of the largest spectral component */
        peakFrequency = peakFrequencyBin*(SAMPLING_RATE/FFT_BLOCK_LENGTH);
        
        //PWM(peakFrequency*0.00465);
        if(peakFrequency < 1500){
            LATD = 0x0001;
        }else if(peakFrequency > 6000){
            LATD = 0x0004;
        }else{
            LATD = 0x0002;
        } 
        counter = 0;
    }
    counter++;
    IFS0bits.T1IF = 0;
    return;
}

 
void time1_init(void){
    T1CONbits.TCS = 0;
    T1CONbits.TGATE = 0;
    T1CONbits.TCKPS = 0;
    IPC0bits.T1IP = 5;
    IFS0bits.T1IF = 0;
    IEC0bits.T1IE = 1;
    start_timer(800);
    return;
}

void init_ADC(void){
    ADCON1bits.ADSIDL = 0; //continues operation while microcontroller in idle(), 0 so it doesn't
    ADCON1bits.FORM = 3; //integer form, 1 for signed integer, 2 for fractional, 3 for signed fractional (sddd dddd dddd 0000)
    ADCON1bits.SSRC = 7; //7 for auto-convert, 0 for manual convert
    
    ADCON1bits.ASAM = 0; //0 for sampling begin when SAMP bit set, 1 for samp bit auto-step (start conversion after last one is finished)
    
    ADCON2bits.VCFG = 0; //which voltage references use for A/D
    ADCON2bits.CSCNA = 1; //0 to not scan inputs, 1 to scan inputs
    ADCON2bits.SMPI = 0; //
    ADCON2bits.BUFM = 0;
    ADCON2bits.ALTS = 0;
    
    ADCON3bits.SAMC = 1;
    ADCON3bits.ADRC = 0;
    ADCON3bits.ADCS = 21; 
    
    ADCHS = 0;
    ADCSSL = 0x0001;
    ADPCFG = 0;
    
    IPC2bits.ADIP = 3;
    IEC0bits.ADIE = 0;
    IFS0bits.ADIF = 0;
    
    ADCON1bits.ADON = 1; //A/D module on, 0 for off
    
    return;
}

int main(void) {
    TwidFactorInit (LOG2_BLOCK_LENGTH, &twiddleFactors[0], 0);
    init_ADC();
    time1_init();
    //time2_init();
    //PWM_setup();
    TRISB = 0x0001; //definir que portas sao input e output
    TRISD = 0x0000;
    counter = 0;
    while(1){
        Idle();
    }
    return 0;
}

 
Following is the error that I get. I'm pasting the whole build log file. The error is at the end of the log. 
 
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/irmun/MPLABXProjects/FFT_learn.X'
make -f nbproject/Makefile-default.mk dist/default/debug/FFT_learn.X.debug.elf
make[2]: Entering directory 'C:/Users/irmun/MPLABXProjects/FFT_learn.X'
"C:\Program Files (x86)\Microchip\xc16\v1.41\bin\xc16-gcc.exe" main.c -o build/default/debug/main.o -c -mcpu=30F6012A -MMD -MF "build/default/debug/main.o.d" -g -D__DEBUG -D__MPLAB_DEBUGGER_SIMULATOR=1 -omf=elf -DXPRJ_default=default -legacy-libc -O0 -msmart-io=1 -Wall -msfr-warn=off
main.c: In function '_T1Interrupt':
main.c:47:13: warning: value computed is not used
main.c:74:13: warning: value computed is not used
"C:\Program Files (x86)\Microchip\xc16\v1.41\bin\xc16-gcc.exe" -o dist/default/debug/FFT_learn.X.debug.elf build/default/debug/main.o -mcpu=30F6012A -D__DEBUG=__DEBUG -D__MPLAB_DEBUGGER_SIMULATOR=1 -omf=elf -DXPRJ_default=default -legacy-libc -Wl,,,--defsym=__MPLAB_BUILD=1,--defsym=__MPLAB_DEBUG=1,--defsym=__DEBUG=1,-D__DEBUG=__DEBUG,--defsym=__MPLAB_DEBUGGER_SIMULATOR=1,,--script=p30F6012A.gld,--stack=16,--check-sections,--data-init,--pack-data,--handles,--isr,--no-gc-sections,--fill-upper=0,--stackguard=16,--no-force-link,--smart-io,-Map="dist/default/debug/FFT_learn.X.debug.map",--report-mem,--memorysummary,dist/default/debug/memoryfile.xml


xc16-ld 1.41 (A)

Default Code Model: Small
Default Data Model: Large
Default Scalar Model: Small

"program" Memory [Origin = 0x100, Length = 0x17f00]

section address length (PC units) length (bytes) (dec)
------- ------- ----------------- --------------------
.text 0x100 0x220 0x330 (816)
.text 0x320 0xce 0x135 (309)
.dinit 0x3ee 0x1a 0x27 (39)

                 Total "program" memory used (bytes): 0x48c (1164) <1%


"data" Memory [Origin = 0x800, Length = 0x2000]

section address alignment gaps total length (dec)
------- ------- -------------- -------------------
.nbss 0x800 0 0xa (10)
.xbss 0x80a 0 0x200 (512)
.bss 0xa0a 0 0x100 (256)
.ybss 0x2400 0 0x400 (1024)

                 Total "data" memory used (bytes): 0x70a (1802) 21%


Dynamic Memory Usage

region address maximum length (dec)
------ ------- ---------------------
heap 0 0 (0)
stack 0xb0a 0x18f6 (6390)

                 Maximum dynamic memory (bytes): 0x18f6 (6390)

Note: Project is using a large data memory model when small data memory model is sufficient.

nbproject/Makefile-default.mk:131: recipe for target 'dist/default/debug/FFT_learn.X.debug.elf' failed
make[2]: Leaving directory 'C:/Users/irmun/MPLABXProjects/FFT_learn.X'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/irmun/MPLABXProjects/FFT_learn.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
build/default/debug/main.o(.isr.text+0xac): In function `.L9':
: undefined reference to `_FFTComplexIP'
build/default/debug/main.o(.isr.text+0xb2): In function `.L9':
: undefined reference to `_BitReverseComplex'
build/default/debug/main.o(.isr.text+0xba): In function `.L9':
: undefined reference to `_SquareMagnitudeCplx'
build/default/debug/main.o(.isr.text+0xf2): In function `.L11':
: undefined reference to `_VectorMax'
build/default/debug/main.o(.text+0xba): In function `main':
C:\Users\irmun\MPLABXProjects\FFT_learn.X/main.c:95: undefined reference to `_TwidFactorInit'
make[2]: *** [dist/default/debug/FFT_learn.X.debug.elf] Error 255
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)

 
Why does compiler says "undefined reference to" when I have included the dsp.h file. I'm using XC16 compiler. 
post edited by indula - 2019/11/14 23:58:43
#1

4 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 18060
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Error in dsPIC30F FFT Code. Please help! 2019/11/15 00:01:46 (permalink)
    0
    The header file only includes the prototypes.
    You need to add the c code to the project, or include the library file.
    #2
    indula
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2019/10/19 06:35:00
    • Location: 0
    • Status: offline
    Re: Error in dsPIC30F FFT Code. Please help! 2019/11/15 00:07:40 (permalink)
    0
    Hi Nkurzman, 
     
    Thanks for the reply.
    I've included the C functions in my code. My question is why does the compiler gives an error, when I have included the necessary header files ? 
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 18060
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Error in dsPIC30F FFT Code. Please help! 2019/11/15 01:00:14 (permalink)
    0
    The header is not the problem.
    The compliant is the functions are missing.
    #4
    indula
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2019/10/19 06:35:00
    • Location: 0
    • Status: offline
    Re: Error in dsPIC30F FFT Code. Please help! 2019/11/15 01:22:12 (permalink)
    0
    Yes, Thank you. 
    You're correct. I added the --library "dsp" and the library got linked.
    #5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5