12-F746-Demo_Frq_Spectrum (STM32F746)

Mit diesem Demo-Projekt wird Mic/Line Input und LCD/FFT+EAR Output des STM32F746-Discovery-Board getestet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//--------------------------------------------------------------
// File     : main.c
// Datum    : 28.02.2016
// Version  : 1.0
// Autor    : UB
// EMail    : mc-4u(@)t-online.de
// Web      : www.mikrocontroller-4u.de
// CPU      : STM32F746
// Board    : STM32F746-Discovery-Board
// IDE      : OpenSTM32
// GCC      : 4.9 2015q2
// Module   : CubeHAL
// Funktion : Hauptprogramm
//--------------------------------------------------------------
 
 
#include "stm32_ub_system.h"
#include "stm32_ub_lcd_480x272.h"
#include "stm32_ub_graphic2d.h"
#include "stm32_ub_uart.h"
#include "stm32f7_audio.h"
 
// add global define : ARM_MATH_CM7
// add global define : __FPU_PRESENT=1
 
 
#include "arm_math.h"
#include "arm_const_structs.h"
 
 
#define FFT_DATA                    512  // n*(real+imaginary)  DON'T CHANGE !!
#define FFT_SIZE                FFT_DATA / 2
float32_t FFT_Input[FFT_DATA];
float32_t FFT_Output[FFT_SIZE];
#define FFT_LCD_DATA            FFT_SIZE / 2
uint16_t FFT_LCD[FFT_LCD_DATA];
 
 
#define AUDIO_BUFFER_SIZE  1024*2  // size in 16bit words (n * left+right)
uint16_t audio_buffer[AUDIO_BUFFER_SIZE];
 
 
#define AUDIO_WAVE_HEIGHT  130
#define FFT_WAVE_HEIGHT    200
 
 
 
arm_cfft_radix4_instance_f32 S;
 
 
void init_hardware(void)
{
  UB_LCD_Init();
  UB_LCD_LayerInit_Fullscreen();
  UB_LCD_SetLayer_2();
  UB_LCD_FillLayer(RGB_COL_BLUE);
  UB_LCD_Refresh();
 
  UB_Uart_Init();
  UB_Uart_SendString(COM1,"Frq-Spectrum",CRLF);
}
 
void init_audio(uint8_t mode)
{
  if(mode==0) {
    // input = mic, output = ear
    UB_AUDIO_IN_OUT_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2,OUTPUT_DEVICE_HEADPHONE,90,I2S_AUDIOFREQ_44K);
    UB_AUDIO_IN_Record_Array(audio_buffer, AUDIO_BUFFER_SIZE);
    UB_AUDIO_OUT_Play_Array(audio_buffer, AUDIO_BUFFER_SIZE*2);
  }
  else {
    // input = line, output = none
	UB_AUDIO_IN_Init(INPUT_DEVICE_INPUT_LINE_1,90,I2S_AUDIOFREQ_44K);
	UB_AUDIO_IN_Record_Array(audio_buffer, AUDIO_BUFFER_SIZE);
  }
}
 
void init_fft(void)
{
	arm_cfft_radix4_init_f32(&S, FFT_SIZE, 0, 1);
}
 
void fill_fft(void)
{
	uint32_t n;
	int16_t wert;
	int16_t min=audio_buffer[0];
	int16_t max=audio_buffer[0];
	int16_t delta;
	float faktor;
 
	// seach min and max audio value
	// to range fft_input from +2.0 to -2.0
	for(n=0;n<AUDIO_BUFFER_SIZE;n+=2) { wert=audio_buffer[n]; if(wert>max) max=wert;
		if(wert<min) min=wert;
	}
	delta=max-min;
	faktor=delta/4;
 
	// convert audio-data in real+imaginary
	for(n=0;n<FFT_DATA;n+=2) { wert=audio_buffer[n]; FFT_Input[n]=(float32_t)(wert/faktor); // real (+2.0 ... -2.0) FFT_Input[n+1]=0.0; // imaginary alway zero } } void calc_fft(unsigned char mode) { uint32_t maxIndex,n; float32_t maxValue; float32_t wert; // calculate fft (FFT_Input --> FFT_Output)
	arm_cfft_radix4_f32(&S, FFT_Input);
	arm_cmplx_mag_f32(FFT_Input, FFT_Output, FFT_SIZE);
	if(mode==0) {
	  // set variable limit
	  arm_max_f32(FFT_Output, FFT_SIZE, &maxValue, &maxIndex);
	}
	else {
	  // set fix limit
	  maxValue=100.0;
	}
 
	// calculate lcd values
	for(n=0;n<FFT_LCD_DATA;n++) {
		wert=FFT_Output[n];
		FFT_LCD[n]=(uint16_t)(wert/maxValue*(float)(FFT_WAVE_HEIGHT));
	}
}
 
 
 
void draw_fft(void)
{
	uint32_t n;
	uint16_t x,l;
 
	UB_Graphic2D_DrawStraightDMA(0,LCD_MAXY-1,FFT_SIZE,LCD_DIR_HORIZONTAL,RGB_COL_RED);
 
	// draw data on screen
	x=0;
	for(n=0;n<FFT_LCD_DATA;n++) {
		l=FFT_LCD[n];
		// draw two lines for each data value
		UB_Graphic2D_DrawStraightDMA(x,LCD_MAXY-l,l,LCD_DIR_VERTICAL,RGB_COL_RED);
		UB_Graphic2D_DrawStraightDMA(x+1,LCD_MAXY-l,l,LCD_DIR_VERTICAL,RGB_COL_RED);
		x+=2;
	}
}
 
void draw_audio(void)
{
	uint32_t n;
	int16_t wert,delta;
	int16_t min;
	float faktor;
	uint16_t x1,y1,y2,start=0;
	uint8_t status=0;
 
	delta=5000;
	min=-2500;
	faktor=(float)(AUDIO_WAVE_HEIGHT)/delta;
 
	// search trigger start (rising edge)
	for(n=0;n<(AUDIO_BUFFER_SIZE-(LCD_MAXX*2));n+=2) {
		wert=audio_buffer[n];
		if(status==0) {
			if(wert<(-100)) status=1; } else if(status==1) { if(wert>=0) {
				start=n;
				break;
			}
		}
	}
 
	// draw data on screen
	x1=0;
	y1=AUDIO_WAVE_HEIGHT/2;
	for(n=0;n<(LCD_MAXX*2);n+=2) { wert=audio_buffer[n+start]; y2=((wert-min)*faktor); UB_Graphic2D_DrawLineNormal(x1,y1,x1+1,y2,RGB_COL_WHITE); y1=y2; x1++; } } // called by audio dma interrupt void update_lcd(void) { static uint16_t display_delay=0; display_delay++; if(display_delay>2) {
		display_delay=0;
		fill_fft();
		calc_fft(0); // fft-height : 0=fix, 1=variable
		UB_Graphic2D_ClearSreenDMA(RGB_COL_BLUE);
		draw_audio();
		draw_fft();
		UB_LCD_Refresh();
	}
}
 
int main(void)
{
  // init vom System
  UB_System_Init();
 
 
  init_hardware();
  init_audio(0); // input : 0=mic, 1=line
  init_fft();
 
 
  while(1) {
	  // nothing to do
  }
}

Hier der komplette OpenSTM32-Projektordner zum Download :

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.