Advertisement
Nanne118

Main + Setup

May 22nd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "arm_const_structs.h"
  3. #include "core_cm4.h"
  4. #include "math.h"
  5. #include "arm_math.h"
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include "main.h"
  9.  
  10. ADC_HandleTypeDef hadc1;
  11. DMA_HandleTypeDef hdma_adc1;
  12. UART_HandleTypeDef huart2;
  13.  
  14. void SystemClock_Config(void);
  15. static void MX_GPIO_Init(void);
  16. static void MX_DMA_Init(void);
  17. static void MX_ADC1_Init(void);
  18. static void MX_USART2_UART_Init(void);
  19.  
  20.  
  21. #define buffer_length 9000 // Has to be a multiple of 3, for 3 channels of ADC
  22. #define samples (buffer_length/3)
  23. #define halfsamples (samples/2) // Nyquist
  24.  
  25. volatile bool conversionPaused = 0; // Start off with conversion running, pause it after conversion, resume it after DSP
  26. volatile uint32_t adc_buffer[buffer_length];
  27.  
  28. int32_t x_cnt, y_cnt, z_cnt = 0;
  29. float Xin[samples], Yin[samples], Zin[samples];
  30. float Xdsp[samples+2], Ydsp[samples+2], Zdsp[samples+2];
  31. float Xmax, Ymax, Zmax;
  32. uint32_t Xind, Yind, Zind;
  33.  
  34. uint8_t msg1[15] = "ADC_end \n\r";
  35. uint8_t msg2[15] = "CONV_end \n]r";
  36. uint8_t msg3[15] = "DSP_start \n\r";
  37. uint8_t msg4[15] = "DSP_end \n\r";
  38.  
  39.  
  40. void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  41. {
  42.     if (hadc->Instance == ADC1 && !conversionPaused)
  43.     {
  44.         conversionPaused = 1; // Pause the conversion so the DSP code can be run
  45.         for(int i = 0; i<buffer_length/3; i++) // 3 samples taken per iterative loop, so it only needs to run 1/3 of the length
  46.         {
  47.             Xin[i] = ((3.3-0)/4096) * adc_buffer[0+3*i];
  48.             Yin[i] = ((3.3-0)/4096) * adc_buffer[1+3*i];
  49.             Zin[i] = ((3.3-0)/4096) * adc_buffer[2+3*i];
  50.         }
  51.         HAL_UART_Transmit(&huart2, msg1, 15, 100); // Conversion complete msg:  Transmit where, what data, how many characters, timeout)
  52.     }
  53. }
  54.  
  55.  
  56. int main(void)
  57. {
  58.     arm_rfft_fast_instance_f32 S;
  59.     arm_rfft_fast_init_f32(&S, samples);
  60.  
  61.     HAL_Init();
  62.     SystemClock_Config();
  63.     MX_GPIO_Init();
  64.     MX_DMA_Init();
  65.     MX_ADC1_Init();
  66.     MX_USART2_UART_Init();
  67.  
  68.     while (1)
  69.     {
  70.         while(conversionPaused)
  71.         {
  72.           HAL_UART_Transmit(&huart2, msg3, 15, 100);
  73.  
  74.           // some fft functions cause major issues if the input buffer is the same as the output array, notably the rfft
  75.           arm_rfft_fast_f32(&S, Xin, Xdsp, 0); // X FFT func (instance of FFT, x input, x dsp output, 0 for FFT or 1 for IFFT)
  76.           arm_rfft_fast_f32(&S, Yin, Ydsp, 0); // Y FFT func ()
  77.           arm_rfft_fast_f32(&S, Zin, Zdsp, 0); // Z FFT func ()
  78.  
  79.           Xdsp[samples] = Xdsp[1]; // copy Nyquist freqeuncy data to the end of the buffer
  80.           Ydsp[samples] = Ydsp[1];
  81.           Zdsp[samples] = Zdsp[1];
  82.  
  83.           Xdsp[1] = Xdsp[samples+1] = 0;
  84.           Ydsp[1] = Ydsp[samples+1] = 0;
  85.           Zdsp[1] = Zdsp[samples+1] = 0;
  86.  
  87.           arm_cmplx_mag_f32(Xdsp, Xdsp, halfsamples); // X magnitudes, only halfsamples because of Nyquist
  88.           arm_cmplx_mag_f32(Ydsp, Ydsp, halfsamples); // Y magnitudes
  89.           arm_cmplx_mag_f32(Zdsp, Zdsp, halfsamples); // Z magnitudes
  90.  
  91.           arm_max_f32(Xdsp+1, halfsamples, &Xmax, &Xind);
  92.           arm_max_f32(Ydsp+1, halfsamples, &Ymax, &Yind);
  93.           arm_max_f32(Zdsp+1, halfsamples, &Zmax, &Zind);
  94.  
  95.           Xind++, Yind++, Zind++; // Can I do this or do they need to be split into separate statements?
  96.  
  97.           printf("Max Value:[%ld]:%f Output=[",Xind,Xmax);
  98.           printf("Max Value:[%ld]:%f Output=[",Yind,Ymax);
  99.           printf("Max Value:[%ld]:%f Output=[",Zind,Zmax);
  100.  
  101.           for(int i=0; i<halfsamples; i++)
  102.           {
  103.               printf("%f,",Xdsp[i]);
  104.               printf("%f,",Ydsp[i]);
  105.               printf("%f,",Zdsp[i]);
  106.           }
  107.  
  108.           printf("]\r\n");
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement