Hot!FFT on dsPIC33EP never works but in FJ yes

Page: 12 > Showing page 1 of 2
Author
David Felipe
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2018/04/06 11:32:53
  • Location: Colombia
  • Status: offline
2018/04/16 04:10:33 (permalink)
0

FFT on dsPIC33EP never works but in FJ yes

Hello comunity!
I developed a "signal analyzer" on a dsPIC33FJ128GP802 based on Microchip dsp libraries for the spectrum, then I write a code for show data in a ILI9341 display in a graphical mode and the code works very well with a maximum FFT size of 1024 points (due to RAM limitation) this code shows time domain and frequency domain without problems at the same time, but due to the 30 extra MIPS (and flash/ram capabilities) of the dsPIC33EP512GP502, I decided to migrate the code without success, all the routines works very well but the FFT not.
 
The first issue was the memory mapping, the compiler does not allow atribbutes like "space(ymemory)" or similar for EP devices throwing an error "could not allocate..." (I dont know why) so I address manually like :
 
fractcomplex vector_complejo[Muestras_FFT] __attribute__((address(0x9000),far,aligned(Muestras_FFT*2*2)));
fractcomplex twiddleFactors[Muestras_FFT/2] __attribute__((address(0x1000),near,aligned(Muestras_FFT*2)));

 
Doing this there are not address trap interrupts and the code flows without problems but NOT calculate the FFT using the same code that works on dsPIC33FJ like:
 p_real = &vector_complejo[0].real ; //Inicializa puntero
p_complejo = &vector_complejo[0]; //Inicializa puntero
//===================SE MUEVE EL BUFFER DE ENTRADA AL VECTOR COMPLEJO===========
for ( i = 0; i < Muestras_FFT; i++ )
{
vector_complejo[i].real = Senal_Entrada [i] ;
vector_complejo[i].imag = 0 ;
}
//==========================CALCULO DE LA FFT===========
FFTComplexIP (E_Mariposa, &vector_complejo[0], (fractcomplex *)&twiddleFactors[0],COEFFS_IN_DATA);
BitReverseComplex (E_Mariposa, &vector_complejo[0]);
SquareMagnitudeCplx(Muestras_FFT, &vector_complejo[0], &vector_complejo[0].real);

"Senal_Entrada" contains the data sampled by timer3 ADC triggering at 48KHz, the screen and the debug always show 17000 Hz and the data stored in the result of the FFT vector never changes. Anyone have an idea why the same algorithm that works on FJ doesn't in EP? I stopped the develoment because I dont know what is happening. Any help will be very hopefull.
#1

21 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 2081
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 04:24:43 (permalink)
    0
    Could be the alignment is ruining your day.
    fractcomplex vector_complejo[Muestras_FFT] __attribute__((address(0x9000),far,aligned(Muestras_FFT*2*2)));
    fractcomplex twiddleFactors[Muestras_FFT/2] __attribute__((address(0x1000),near,aligned(Muestras_FFT*2)));
     
    I't start trying out aligned(4) for the first and aligned(2) for the second term.
    (Not sure, but "aligned(4096)" resp. "aligned(2048)" makes little sense for me.)
    That is: provided the remainder of the code is really identical to what you had on the FJ (no further "adaptations").

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 04:34:30 (permalink)
    0
    We have found some potential bugs in the cplxFft32b.s, realFft32b.s files.
     
    The fix is swapping  (W8,W9) with (W10,W11).
    #3
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 04:47:25 (permalink)
    0
    Hello Du, thank you for aswering, in effect the alignment is correct according with the "TB3141" Note from Microchip:
    "The array containing the twiddle factors must be either in X-memory in RAM or in a PSV section if stored in Flash program memory, and the array containing the input data/FFT result, which is used in functions, FFTComplexIP(), BitReverseComplex() and SquareMagnitudeComplex(), must be in Y-memory, and aligned to a multiple of the array size in bytes." which means that TwiddleFactors not need to be aligned but it does not work.
     
    timijk
    We have found some potential bugs in the cplxFft32b.s, realFft32b.s files.
     
    The fix is swapping  (W8,W9) with (W10,W11).


    How can I do that? The FFTComplex routines are hidden and I dont know how, thank you!
     
     
    I dont understand why the same code works perfectly on FJ (even if twiddles are aligned or not) devices but no in EP.
    post edited by David Felipe - 2018/04/16 04:51:47
    #4
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 04:54:05 (permalink)
    0
    I dont understand why the same code works perfectly on FJ (even if twiddles are aligned or not) devices but no in EP.

    In some asm files(*.s), EP has different implementations.  For example:
    .ifndef __dsPIC33E

    What we have observed, the error comes from this type of command:
    MPY W4*W7, B, [W9]+=2,W5

    W9 has to be in X data space, but it seems the data is Y data space.
    So we changed it to W11:
    MPY W4*W7, B, [W11]+=2,W5

    #5
    du00000001
    Just Some Member
    • Total Posts : 2081
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:11:46 (permalink)
    0
    Oops, ssems I missed the somewhat limited addressing capabilities of the dsPICs.
    So your findings about addressing might be ok.
    Otherweise I would have suggested to have vector_complejo[] in the lower RAM area (below 0x8000) - which might solve the issue as well.

    The main problem seems to be that the library code is inflexible regarding memory ranges (far vs. near), so you have to match the library's assumptions by your memory allocation.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #6
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:15:01 (permalink)
    0
    I archived the fixed assembly files and the compiled library (used for dsPIC33E only).  You can give it a try.
    https://drive.google.com/...ho2Di9Bm8er4shWeU/view
     
    Here is the original post (in Chinese).
    http://www.microchip.com....ode=flat&order=ASC
     
    ++ fix hyperlink
    post edited by timijk - 2018/04/16 05:17:43
    #7
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:17:48 (permalink)
    0
    In some asm files(*.s), EP has different implementations.  For example:
    .ifndef __dsPIC33E
     

    What we have observed, the error comes from this type of command:
    MPY W4*W7, B, [W9]+=2,W5
     

    W9 has to be in X data space, but it seems the data is Y data space.
    So we changed it to W11:
    MPY W4*W7, B, [W11]+=2,W5
     





    Thank you for advice, but, Im unable to get access to the assembler files linked with the DSP routines, Can you explain me how to make that change from my code? I was searching in the xc16 folder for any DSP file .s but does not exist and I dont use assembler so its hard to me.
     
    du00000001
    I would have suggested to have vector_complejo[] in the lower RAM area (below 0x8000) - which might solve the issue as well.

    This is not allowed due to the dual data fetch that needs the complex vector in Y memory which begins at 0x9000.
    #8
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:27:51 (permalink)
    5 (1)
    The original source is in the XC16 folder, xc16\v1.xx\src\libdsp.zip
    #9
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:39:04 (permalink)
    0
    timijk, Im reading the original forum in chinese and You said that is necessary to rebuild the library from the .bat file which I found on the folder src\libdsp.zip but Im really confused, I downloaded the file lib33e.zip with a folders:
    "asm and lib.33e".
     
    How can I integrate this modified library to the project? I tried to set the library path to the folder lib33e and replace the dsp elf and coff from the project folder but it does not work, always the same frequency on the screen.
     
    Thank you for your replies.
    #10
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 05:54:44 (permalink)
    0
    David Felipe
    timijk, Im reading the original forum in chinese and You said that is necessary to rebuild the library from the .bat file which I found on the folder src\libdsp.zip but Im really confused, I downloaded the file lib33e.zip with a folders:
    "asm and lib.33e".

    The archived file only includes the fixes.  The original files are in the XC16 folder.
     
    How can I integrate this modified library to the project? I tried to set the library path to the folder lib33e and replace the dsp elf and coff from the project folder but it does not work, always the same frequency on the screen.

    I am not quite sure what went wrong... I did my tests with demo projects and found the issues in the simulator.  The other guy (who initiated the post) did his own tests.
     
    Maybe you can double check your setup for the signal data and those twiddleFactors.  See if you can debug it in the simulator.
     
    By the way, I don't quite remember the details... there seems a limitation on the number of the data points.  How many data points are you using?
     
    #11
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 06:17:43 (permalink)
    0
    Im using 512 to 1024 points and the migration to EP for 2048 points (if possible), I want to know the correct steps to follow in order to fix the library with the modified files that you show me, can you help me with a little step tutorial?, I need to download C30 and rebuild? or what I can do? I dont want to lose the compatibility with FJ, or I should surrender? I understand a little bit from the chinese forum (with google translator). Thank you.
    #12
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 06:39:20 (permalink)
    0
    I tried it with C30 evaluation version with optimization -O3.  (that seems the old setup),  I checked the new one in XC16 v1.33, it's using xc16-gcc in the batch file(makedsplib.bat).
     
    After you unzip the libdsp, and inside src, there is another zip file for the source codes.  You can take a look at the DSP Library file ( in the docs folder) to see how to build it.  My experience is with C30... you should be able to do it with xc16-gcc.
     
    I think at first, you have to Run "Command Prompt" as administrator, and create an obj folder (under libdsp), and backup the *.a file in the lib folder(if you want).  Then run the makedsplib.bat file, of course, you have to setup the path for those xc16-gcc, xc16-as exe files.  It's very straightforward, it can be done in Windows environment.
     
     
    #13
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 07:50:30 (permalink)
    0
    I checked my testing project, I was using only FFTReal32bIP and MagnitudeCplx32bIP to get the result.  It's different to yours, you are using complex vectors.  I didn't test my fix with complex input vectors.  Maybe there are some other issues in the other *.s files.
     
    I don't know if you can run a simple test with fewer data points and see if it can go through in the simulator.
     
    I test my project with dsPIC33EP512GP502, it runs OK, except that I have to change space(ymemory)
    to address(0x9000) due to the linker errata.
     
    ++ in TB3141, for some models, ymemory is above 0x7FFF, using "space(ymemory),eds"
     
    +++ I was thinking, if we don't fix the (W8,W9) issues, maybe the code can still work if we place the source vector (srcV) in xmemory.  Because the developer who wrote the code for dsPIC33E must have done some testing, from the assembly code, for dsPIC33E, the source vector is placed in xmemory... 
    post edited by timijk - 2018/04/16 08:12:15
    #14
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 14:48:52 (permalink)
    0
    Hello timijk, I was trying to put the source vector on xmemory and as I expected, the address trap jumps inmediately.
    I think that the only way to fix it is remaking the library, and I dont know how to do it at all. For clarifying, I dont use the FFTReal32bIP or MagnitudeCplx32bIP, only the FFTComplexIP for FFT and SquareMagnitude (16 bit I suppose)
    #15
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 15:32:03 (permalink)
    0
    Hello, now Im pretty sure that the FFT function is working on the EP device with FFTComplexIP, but SquareMagnitudeCmplx not! I was debugging with a smal FFT and step by step with ICD3 direct in hardware and all the steps are developing ok, the FFT Function returns diferent values for each computation on the ADC samples so I assume that is working ok, but SquareMagnitude always return the same result even if the complex vector is loaded with diferent values, always return the same, this is an advance.
    If I compute the SquareMagnitude manually like:
            for (i = 0; i < Muestras_FFT/2; i++)
            {
                SquareMagnitude[i] = 2*((vector_complejo[i].real^2) + (vector_complejo[i].imag^2));
            }

    The result is the same, and debugging, I noticed that all access to the complex vector, return always the same value, like Test = vector_complejo[1], no matter if the vector have diferent values Test is always the same but in the variable window, the complex vector changes the values according with the ADC samples computation. This is a lot strange.
    post edited by David Felipe - 2018/04/16 16:40:05
    #16
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 16:23:17 (permalink)
    0
    There is another bug mentioned in http://www.microchip.com/.../FindPost/776110  that might be related.
     
    In cplxsqrmag.s, 
    .ifdef __dsPIC33E
     
         push DSRPAG
         movpag #0x0001, DSRPAG
     
    .endif
     
         ....
     
     
     
    .ifdef __dsPIC33E
         pop DSRPAG
    .endif

    changed to
    .ifdef __dsPIC33E
     
         push DSRPAG
     
         push DSWPAG
     
         movpag #0x0001, DSRPAG
     
         movpag #0x0001, DSWPAG
     
    .endif
     
         ....
     
     
     
    .ifdef __dsPIC33E
     
         pop DSWPAG
         pop DSRPAG
    .endif
     

     
    ++ It seems it doesn't fix the problem... got to dig into it...
     
    post edited by timijk - 2018/04/16 16:49:19
    #17
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 17:02:37 (permalink)
    5 (1)
    Regarding the function SquareMagnitudeCplx(), I tried to use a different array for the destV, the original code works (in the simulator).
    #18
    timijk
    Super Member
    • Total Posts : 1216
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 17:21:37 (permalink)
    0
    David Felipe
     
            for (i = 0; i < Muestras_FFT/2; i++)
            {
                SquareMagnitude[i] = 2*((vector_complejo[i].real^2) + (vector_complejo[i].imag^2));
            }


     
    Do you have to multiply it by 2?  The definition in the assembly code is 
     
    dstV[n] = Real(srcV[n])^2 + Imag(srcV[n])^2
     
    ... I did some more testing, the original code, SquareMagnitudeCplx(Muestras_FFT, &vector_complejo[0], &vector_complejo[0].real); looks OK.  I am not sure why it did not work in my previous test.
    #19
    David Felipe
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2018/04/06 11:32:53
    • Location: Colombia
    • Status: offline
    Re: FFT on dsPIC33EP never works but in FJ yes 2018/04/16 17:24:28 (permalink)
    0
    timijk
    Regarding the function SquareMagnitudeCplx(), I tried to use a different array for the destV, the original code works (in the simulator).


    Thats worked like a charm! with the previous consideration now the FFT shows me coherent and variant values depending of the ADC samples directy in hardware, now I need to calibrate the times and sample frequency because there is a wrong measure but it is working! Thank you very much!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5