This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F280025C: RFFT code calculations giving all zeros and lots of warnings

Part Number: TMS320F280025C
Other Parts Discussed in Thread: C2000WARE

I have modified RFFT example code for calculating 64 point RFFT in TMS320F280025C. The complete main code file that implements RFFT is shown below:

//#############################################################################
//
// FILE:   main.c
//
//
//
//#############################################################################
// $TI Release: F28002x Support Library v3.04.00.00 $
// $Release Date: Fri Feb 12 18:58:34 IST 2021 $
// $Copyright:
// Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################

//
// Included Files
//
// Select the global Q value to use:
#define GLOBAL_Q    17
#define FFT_SIZE    64
//long GlobalQ = GLOBAL_Q;      // Used for legacy GEL & Graph Debug.

#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "IQmathLib.h"
#include <stdio.h>
#include <file.h>
#include "sci_io_driverlib.h"
#include "fft.h"
#include "fft_hamming_Q31.h"
#include "math.h"

//
// Globals
//
uint16_t sData[32];                // Send data buffer for DMA transmission
uint16_t rData[32];                // Receive data buffer for DMA reception

//Defining FFT sections
#pragma DATA_SECTION(ipcb, "FFTipcb");
int32_t ipcb[FFT_SIZE+2];         //Calculations buffer
#pragma DATA_SECTION(ipcbsrc, "FFTipcbsrc");
int32_t ipcbsrc[FFT_SIZE];        //Sampled input signal
//Defining real fft object for 64 point calculations
RFFT32  rfft = RFFT32_64P_DEFAULTS;

// Place buffers in GSRAM so that DMA can access these
#pragma DATA_SECTION(sData, "ramgs0");
#pragma DATA_SECTION(rData, "ramgs0");
//
// Function Prototypes
void init_SPI_FIFO(void);
//
// Function Prototypes for Interrupt
//
extern __interrupt void controlISR(void);
extern __interrupt void jemADCISR(void);
extern __interrupt void cpuTimer0ISR(void);
extern __interrupt void dmaCh5ISR(void);
extern __interrupt void dmaCh6ISR(void);
void initCPUTimers(void);
void configCPUTimer(uint32_t, float, float);

const long win[FFT_SIZE/2]=HAMMING64;
void main(void)
{
    volatile int status = 0;
    volatile FILE *fid;
    int i = 0;
    //
    float RadStep = 0.1963495408494f;
    float Rad = 0.0f;
    float temp1;
    int temp2;

    //
    // Initialize device clock and peripherals
    //
    Device_init();

    //
    // Disable pin locks and enable internal pull-ups.
    //
    Device_initGPIO();

    //
    // Initialize PIE and clear PIE registers. Disables CPU interrupts.
    //
    Interrupt_initModule();

    //
    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    //
    Interrupt_initVectorTable();

    //
    // Interrupts that are used in this example are re-mapped to ISR functions
    // found within this file.
    //
    Interrupt_register(INT_TIMER0, &cpuTimer0ISR);
    Interrupt_register(INT_ADCA1, &controlISR);
    Interrupt_register(INT_ADCA2, &jemADCISR);
    //
    // Configuring interrupts for DMA transmit and receive channels
    Interrupt_register(INT_DMA_CH5, &dmaCh5ISR);
    Interrupt_register(INT_DMA_CH6, &dmaCh6ISR);

    //
    // Configure all peripherals and analog/digital GPIOs
    //
    Board_init();

    //Initialize ADCs. Not generated by the sysconfig tool.
    ADC_init();

    //
    //EPWM initialization code.Needs to be configured separately as current
    // tool version does not support this module
    //
    EPWM_init();

    // Set up DMA for SPI use, initialize the SPI for FIFO mode
    initDMA();
    //initSPI();

    // Enable interrupts required for DMA send and receive
    //
    Interrupt_enable(INT_DMA_CH5);
    Interrupt_enable(INT_DMA_CH6);

    //Initialize Timer
    initCPUTimers();

    // Configure CPU-Timer 0 to interrupt every 100000 u seconds:
    //
    configCPUTimer(CPUTIMER0_BASE, DEVICE_SYSCLK_FREQ, 100000);
    CPUTimer_enableInterrupt(CPUTIMER0_BASE);
    Interrupt_enable(INT_TIMER0);
    Interrupt_enable(INT_ADCA1);
    Interrupt_enable(INT_ADCA2);

    //
    // Enable Global Interrupt (INTM) and real time interrupt (DBGM)
    //
    EINT;
    ERTM;

    // Redirect STDOUT to SCI
    status = add_device("scia", _SSA, SCI_open, SCI_close, SCI_read, SCI_write,
                        SCI_lseek, SCI_unlink, SCI_rename);
    fid = fopen("scia", "w");
    freopen("scia:", "w", stdout);
    setvbuf(stdout, NULL, _IONBF, 0);

    //Prints welcome message on debug SCI
    printf("Skyelectric");
    DEVICE_DELAY_US(10000);

    init_SPI_FIFO();

    // Start the DMA channels
    //
    DMA_startChannel(DMA_CH6_BASE);
    DMA_startChannel(DMA_CH5_BASE);

    //------------------------------------
    CPUTimer_startTimer(CPUTIMER0_BASE);

    //Temporary code for implementing FFT
    for (i=0;i<FFT_SIZE;i++)
    {
        ipcbsrc[i]=(long)(2147483648*sinf(Rad));//Q31 format
        Rad=Rad+0.09817;
    }
    printf("\r\nSample Input:\r\n");
    for (i = 0; i < FFT_SIZE; i++)


        printf("%ld\r\n", ipcbsrc[i]);
    }

    RFFT32_brev(ipcbsrc, ipcb, FFT_SIZE); // real FFT bit reversing

    rfft.ipcbptr = ipcb;                  // FFT computation buffer
    rfft.magptr  = ipcbsrc;               // Magnitude output buffer
    rfft.winptr  = (long *)win;           // Window coefficient array
    rfft.init(&rfft);                     // Twiddle factor pointer initialization

    rfft.calc(&rfft);                     // Compute the FFT
    rfft.split(&rfft);                    // Post processing to get the correct spectrum
    rfft.mag(&rfft);                      // Q31 format (abs(ipcbsrc)/2^16).^2


    printf("\r\nFFT Result:\r\n");
    for (i = 0; i < FFT_SIZE; i++)
    {
         printf("%ld\r\n", ipcbsrc[i]);
    }
    for (;;)
    {

        DEVICE_DELAY_US(1000000);    //one second delay

    }
}

void init_SPI_FIFO(void)
{
    int i;
    for (i = 0; i < 32; i++)
        sData[i] = i;
}

The compilation process throws a lots of errors as below:

The linker settings for the projects are shown below:

The linker command file is shown below to check if the FFT sections are properly allocated.

MEMORY
{
   BEGIN           	: origin = 0x080000, length = 0x000002
   BOOT_RSVD		: origin = 0x00000002, length = 0x00000126
   RAMM0           	: origin = 0x00000128, length = 0x000002D8
   RAMM1            : origin = 0x00000400, length = 0x000003F8     /* on-chip RAM block M1 */
// RAMM1_RSVD       : origin = 0x000007F8, length = 0x00000008 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */

/* RAMLS4           : origin = 0x0000A000, length = 0x00000800
   RAMLS5           : origin = 0x0000A800, length = 0x00000800
   RAMLS6           : origin = 0x0000B000, length = 0x00000800
   RAMLS7           : origin = 0x0000B800, length = 0x00000800*/

   /* Combining all the LS RAMs */
   RAMLS4567        : origin = 0x0000A000, length = 0x00002000
   RAMGS0           : origin = 0x0000C000, length = 0x000007F8
// RAMGS0_RSVD      : origin = 0x0000C7F8, length = 0x00000008 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */


//  FLASHBANK1       : origin = 0x00080000, length = 0x0000FFF0
//  FLASH_BANK1_RSVD : origin = 0x0008FFF0, length = 0x00000010 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */
   BOOTROM          : origin = 0x003F0000, length = 0x00008000
   BOOTROM_EXT      : origin = 0x003F8000, length = 0x00007FC0
   RESET            : origin = 0x003FFFC0, length = 0x00000002
   /*IQMath table already exists in the BOOTROM section*/
   //IQTABLES 		: origin = 0x3F1402, length = 0x0166D
   /* Flash sectors */
   /* BANK 0 */
   FLASH_BANK0_SEC0  : origin = 0x080002, length = 0x000FFE	/* on-chip Flash */
   FLASH_BANK0_SEC1  : origin = 0x081000, length = 0x001000	/* on-chip Flash */
   FLASH_BANK0_SEC2  : origin = 0x082000, length = 0x001000	/* on-chip Flash */
   FLASH_BANK0_SEC3_9  : origin = 0x083000, length = 0x007000	/* on-chip Flash */
   FLASH_BANK0_SEC10_11 : origin = 0x08A000, length = 0x002000	/* on-chip Flash */
   FLASH_BANK0_SEC12_15 : origin = 0x08C000, length = 0x003FF0	/* on-chip Flash */
   // FLASH_BANK0_SEC15_RSVD     : origin = 0x08FFF0, length = 0x000010  /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */

}


SECTIONS
{
   codestart        : > BEGIN, ALIGN(8)
   .text            : >> FLASH_BANK0_SEC2 | FLASH_BANK0_SEC3_9 ,   ALIGN(8)//executable code >> operator splits code
   .cinit           : > FLASH_BANK0_SEC1,  ALIGN(8)//table that initializes global variables
   .switch          : > FLASH_BANK0_SEC1,  ALIGN(8)
   .reset           : > RESET,                  TYPE = DSECT /* not used, Dummy Section*/

   .stack           : > RAMM1

   .init_array      : > FLASH_BANK0_SEC1,  ALIGN(8)
   .bss             : > RAMLS4567 //uninitialized global variables
   .bss:output      : > RAMLS4567
   .bss:cio         : > RAMGS0
   .const           : > FLASH_BANK0_SEC10_11,  ALIGN(8)
   .data            : > RAMLS4567 //initialized data
   .sysmem          : > RAMLS4567

    ramgs0 : > RAMGS0


   /* Allocate IQ math areas: */
   IQmath : > FLASH_BANK0_SEC1, PAGE = 0, ALIGN(4) /* Math Code */
   //IQmathTables : > FLASH_BANK0_SEC2, PAGE = 0, ALIGN(4)/*This was working*/
   IQmathTables : > FLASH_BANK0_SEC1, PAGE = 0, ALIGN(4)/*Moved to sector 1 to run from RAM.Need verification*/

   /* Allocating FFT areas: */
   FFTipcb 		: >> RAMGS0, ALIGN(256) //64 points FFT
   FFTipcbsrc	: >> RAMLS4567
   FFTtf        : > FLASH_BANK0_SEC12_15

  .TI.ramfunc      : LOAD = FLASH_BANK0_SEC1, //Flash bank0 sector 1 contaings time critical functions required to run from RAM
                  RUN = RAMGS0,
                  LOAD_START(RamfuncsLoadStart),
                  LOAD_SIZE(RamfuncsLoadSize),
                  LOAD_END(RamfuncsLoadEnd),
                  RUN_START(RamfuncsRunStart),
                  RUN_SIZE(RamfuncsRunSize),
                  RUN_END(RamfuncsRunEnd),
                  ALIGN(8)

}
/*
//===========================================================================
// End of file.
//===========================================================================
*/

Please note that I have started working with the TI controllers only one month ago and I am not well versed with linked command file so kindly verify if all sections are allocated correctly. I will highly appreciate if these allocations can be revised  for better processing time and more efficient memory usage. Meanwhile I am looking into the "Fixed Point DSP Software Library" document from the C2000Ware directory to troubleshoot at my end. I have taken reference from "FixedPoint_RFFT" project written for f2838x controllers.

  • Asad,

    The compilation process throws a lots of errors as below:

    After a lot of debugging, I noticed the lack of '{' at line 207. Can you add that and see if it persists?

    The linker command file is shown below to check if the FFT sections are properly allocated.

    You cmd file looks correct. Make sure to memcpy the FFTtf and any table in FLASH to  RAM in the code by doing memcpy((uint32_t *)&FFTtfRunStart, (uint32_t *)&FFTtfLoadStart,
    (uint32_t)&FFTtfLoadSize); Also, in the cmd file, you should specifiy the following:

    FFTtf : LOAD = FLASHC, PAGE = 0
    RUN = RAML7, PAGE = 1
    LOAD_START(_FFTtfLoadStart),
    LOAD_SIZE(_FFTtfLoadSize),
    RUN_START(_FFTtfRunStart)

    Hope this helps

    -Shantanu

  • Dear Shanty,

       Thankyou very much for a very valuable feedback. Just a small query regarding your reply: How does compiler recognize FLASHC and RAML7?. These sections are not defined in my linker command file.

  • The compilation issue was in-fact due to missing curly bracket as pointed by you. However for the cmd file addition text: compiler does not recognize "FLASHC" and "RAML7". I have modified your proposed cmd file addition part according to the sections and memory definitions in my linker command file as shown below. 
    FFTtf : LOAD = FLASH_BANK0_SEC12_15,
    RUN = RAMLS4567,
    LOAD_START(_FFTtfLoadStart),
    LOAD_SIZE(_FFTtfLoadSize),
    RUN_START(_FFTtfRunStart)

    Please check if  the above following modification  is Okay to be included in linker command file:

  • Asad,

    Yes memory ranges like 'FLASHC' need to be defined in the "MEMORY" section of the cmd file. You can change the ranges and names as you like but the same nomenclature needs to be followed in the "SECTIONS" part.

    Your FFTtf looks okay. Just try to match the examples as far as possible. Let me know if you face any issues.

    -Shantanu

  • Dear Shanty,
        I have done some basic testing of FFT calculations on a sine wave as define below:

    for (i=0;i<FFT_SIZE;i++)
    {
    ipcbsrc[i]=(long)(2147483648*((sinf(2*3.14167*50*Ts*i))+sinf(2*3.14167*150*Ts*i))/2); //50Hz and 150Hz components.
    }

    where Ts=1/3200 i.e. sampling frequency is 3200 Hz. The sampling length is 64 points so frequency bin is 50 Hz. The FFT tables are defined in FLASH at the moment and are not loaded to RAM since I am having some issues but I think running from RAM should have performance gain only. Correct me if i am wrong.

    The generated Sine wave is shown below:

    After FFT calculation the following result is obtained:
    0
    67109714
    0
    67104820
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    So I am correctly getting two harmonics in first frequency bin (50 Hz) and third frequency bin (150 Hz). The magnitude of each component should be 2147483648/2=1073741824 in Q31 format. Please let me know how to correlate FFT results of RFFT32 (67109714) with the expected results(1073741824)?.

  • Asad,

    Your result seems to be scaled by 1/16. This may be a function of the fixed point implement. I haven't gotten a chance to look at the issue in depth right now. Let me get back to you by the end of this week. 

    One thing you can try is to make sure that the magnitude calculated is of the same Q format. The documentation states that the magnitude result is in Q30. 

    Apologies for the delay. 

    -Shantanu

  • Dear Shanty,

        I will wait for your reply expected by weekend. There is one more small query: I need to perform RFFT on ADC readings. The ADC resolution is 12- bits. So is it necessary to multiply the ADC readings by 2^19 to convert into Q31 format to copy into ipcbsrc array for subsequent FFT calculations?. 

  • Hi Asad,

    Thanks for understanding.
    Regarding the ADC, yes you should convert to Q31 by multiplying by  2^19. 

    -Shantanu

  • Hi Asad,

    I am able to reproduce the scaling issue. We are looking into why that's happening. I will let you know once we figure it out.

    Do you have any other questions?

    -Shantanu

  • Dear Shanty,

       Thanks a lot for the update. I don't have more questions relating to RFFT at the moment. I will be waiting for your reply to scaling issue.

  • Hi,

    From the documentation in fpu_cfft.h, the fixed point FFT does a magnitude scaling to avoid overflow. the factor is:

    1/[2^{FFT\_STAGES-1}]

    -Shantanu

  • Kindly explain the term in the denominator: "FFT\_STAGES-1".  Does it come out to be 1/16 as mentioned in your previous post?.