Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "arm_const_structs.h"
- #include "core_cm4.h"
- #include "math.h"
- #include "arm_math.h"
- #include <stdio.h>
- #include <stdbool.h>
- #include "main.h"
- ADC_HandleTypeDef hadc1;
- DMA_HandleTypeDef hdma_adc1;
- UART_HandleTypeDef huart2;
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_ADC1_Init(void);
- static void MX_USART2_UART_Init(void);
- #define buffer_length 9000 // Has to be a multiple of 3, for 3 channels of ADC
- #define samples (buffer_length/3)
- #define halfsamples (samples/2) // Nyquist
- volatile bool conversionPaused = 0; // Start off with conversion running, pause it after conversion, resume it after DSP
- volatile uint32_t adc_buffer[buffer_length];
- int32_t x_cnt, y_cnt, z_cnt = 0;
- float Xin[samples], Yin[samples], Zin[samples];
- float Xdsp[samples+2], Ydsp[samples+2], Zdsp[samples+2];
- float Xmax, Ymax, Zmax;
- uint32_t Xind, Yind, Zind;
- uint8_t msg1[15] = "ADC_end \n\r";
- uint8_t msg2[15] = "CONV_end \n]r";
- uint8_t msg3[15] = "DSP_start \n\r";
- uint8_t msg4[15] = "DSP_end \n\r";
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
- {
- if (hadc->Instance == ADC1 && !conversionPaused)
- {
- conversionPaused = 1; // Pause the conversion so the DSP code can be run
- 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
- {
- Xin[i] = ((3.3-0)/4096) * adc_buffer[0+3*i];
- Yin[i] = ((3.3-0)/4096) * adc_buffer[1+3*i];
- Zin[i] = ((3.3-0)/4096) * adc_buffer[2+3*i];
- }
- HAL_UART_Transmit(&huart2, msg1, 15, 100); // Conversion complete msg: Transmit where, what data, how many characters, timeout)
- }
- }
- int main(void)
- {
- arm_rfft_fast_instance_f32 S;
- arm_rfft_fast_init_f32(&S, samples);
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- MX_USART2_UART_Init();
- while (1)
- {
- while(conversionPaused)
- {
- HAL_UART_Transmit(&huart2, msg3, 15, 100);
- // some fft functions cause major issues if the input buffer is the same as the output array, notably the rfft
- 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)
- arm_rfft_fast_f32(&S, Yin, Ydsp, 0); // Y FFT func ()
- arm_rfft_fast_f32(&S, Zin, Zdsp, 0); // Z FFT func ()
- Xdsp[samples] = Xdsp[1]; // copy Nyquist freqeuncy data to the end of the buffer
- Ydsp[samples] = Ydsp[1];
- Zdsp[samples] = Zdsp[1];
- Xdsp[1] = Xdsp[samples+1] = 0;
- Ydsp[1] = Ydsp[samples+1] = 0;
- Zdsp[1] = Zdsp[samples+1] = 0;
- arm_cmplx_mag_f32(Xdsp, Xdsp, halfsamples); // X magnitudes, only halfsamples because of Nyquist
- arm_cmplx_mag_f32(Ydsp, Ydsp, halfsamples); // Y magnitudes
- arm_cmplx_mag_f32(Zdsp, Zdsp, halfsamples); // Z magnitudes
- arm_max_f32(Xdsp+1, halfsamples, &Xmax, &Xind);
- arm_max_f32(Ydsp+1, halfsamples, &Ymax, &Yind);
- arm_max_f32(Zdsp+1, halfsamples, &Zmax, &Zind);
- Xind++, Yind++, Zind++; // Can I do this or do they need to be split into separate statements?
- printf("Max Value:[%ld]:%f Output=[",Xind,Xmax);
- printf("Max Value:[%ld]:%f Output=[",Yind,Ymax);
- printf("Max Value:[%ld]:%f Output=[",Zind,Zmax);
- for(int i=0; i<halfsamples; i++)
- {
- printf("%f,",Xdsp[i]);
- printf("%f,",Ydsp[i]);
- printf("%f,",Zdsp[i]);
- }
- printf("]\r\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement