CIMSIS FFT issue

Hello,

I am trying to do fft on square wave signal.

 For which I am using STM32F411 MCU. 

The task is:

1. Take 512 Samples of the input signal.

2. Perform FFT on that. and calculate the frequency.

3. Display the frequency on serial terminal.

Specification of Input Signal

1. Frequency Range 2000Hz-7000Hz.

2. ADC sampling frequency 33Khz

The issue I am facing is that the function:

"arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex); " returns 0 for maxIndex. No matter what the input frequency is.

Here is the Code:


  */
/* Includes ------------------------------------------------------------------*/

#include "main.h"
#include "stm32f4xx_hal.h"
#include "arm_math.h"
#include "arm_const_structs.h"
#include <stdio.h>
#include <stdlib.h>


#define SAMPLES        512
#define FFT_SIZE                SAMPLES / 2
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

float32_t f=4000.56;
float32_t Input[SAMPLES];
float32_t Output[FFT_SIZE];
uint32_t ifftFlag = 0;
uint32_t doBitReverse = 1;

char Data[20];

float32_t frequency=0;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART2_UART_Init(void);


/* Private function prototypes -----------------------------------------------*/
void convert(char *data,float a, int place);



int main(void)
{
    /* USER CODE BEGIN 1 */

  //arm_status status;
    arm_cfft_radix4_instance_f32 S;    /* ARM CFFT module */
    float32_t maxValue;                /* Max FFT value is stored here */
    uint32_t maxIndex;

  int count=0;
uint16_t i;
    /* USER CODE END 1 */
  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();


  /* Configure the system clock */
  SystemClock_Config();



  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_USART2_UART_Init();


 
     /* USER CODE BEGIN 2 */
 //convert(Data,f,2);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
         /* USER CODE END WHILE */
        
        /*USER CODE BEGIN3 */
 for(i =0;i<SAMPLES;i+=2){
 
 HAL_ADC_Start(&hadc1);
     if(HAL_ADC_PollForConversion(&hadc1,1)==HAL_OK){
     Input[(uint16_t)i]=(float32_t)HAL_ADC_GetValue(&hadc1);
     Input[(uint16_t)i+1]=0;
     count++;
         if(count>512){
        
         count=512;
         }
     }
 
 }
 HAL_ADC_Stop(&hadc1);
 /* Initialize the CFFT/CIFFT module, intFlag = 0, doBitReverse = 1 */
       arm_cfft_radix4_init_f32(&S, FFT_SIZE, 0, 1);
        
        /* Process the data through the CFFT/CIFFT module */
        arm_cfft_radix4_f32(&S, Input);
        
        /* Process the data through the Complex Magniture Module for calculating the magnitude at each bin */
       arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
        
        /* Calculates maxValue and returns corresponding value */
        arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
 
    frequency=maxIndex*33330.00/2048;
 frequency=frequency/1000;
 

 convert(Data,(float32_t)maxValue,2);//  To convert float32_t  to ASCII
 HAL_UART_Transmit(&huart2,(uint8_t*)Data,7,1000);
 
 
 

 
 /*
if(maxIndex>0){
     count=0;
    
     convert(Data,(float32_t)frequency,2);
 HAL_UART_Transmit(&huart2,(uint8_t*)Data,7,1000);
 }
 else{
 count=0;
     f=f+1;
     convert(Data,(float32_t)maxValue,2);
 HAL_UART_Transmit(&huart2,(uint8_t*)Data,7,1000);
 }*/
 HAL_Delay(2000);

  }
/* UESER CODE END3 */

}

void convert(char *data,float a, int place) //definition
{
     int temp=a;
     float x=0.0;
     int digits=0;
     int i=0,mu=1;
     int j=0;
     if(a<0)
     {
            a=a*-1;
            data[i]='-';
            i++;
      }
     //exponent component
     while(temp!=0)
     {
         temp=temp/10;
         digits++;          
     }
     while(digits!=0)
     {
         if(digits==1)mu=1;
         else  for(j=2;j<=digits;j++)mu=mu*10;
         
         x=a/mu;
         a=a-((int)x*mu);
         data[i]=0x30+((int)x);
         i++;
         digits--;
         mu=1;
     }
     //mantissa component
     data[i]='.';
     i++;
     digits=0;
     for(j=1;j<=place;j++)mu=mu*10;
     x=(a-(int)a)*mu; //shift places
     a=x;
     temp=a;
     x=0.0;
     mu=1;
     digits=place;
     while(digits!=0)
     {
         if(digits==1)mu=1;
         else  for(j=2;j<=digits;j++)mu=mu*10;
         
         x=a/mu;
         a=a-((int)x*mu);
         data[i]=0x30+((int)x);
         i++;
         digits--;
         mu=1;
     }   
     
    data[i]='\n';
}

Kindly guide me. If I am doing anything wrong??