Skip to content

Commit 95f6aba

Browse files
committed
hw/drivers/i2s: Add I2S driver for STM32F7 family
Code implements I2S driver for STM32F7xx MCUs that have it. Signed-off-by: Jerzy Kasenberg <[email protected]>
1 parent 87ae9ee commit 95f6aba

File tree

5 files changed

+1406
-0
lines changed

5 files changed

+1406
-0
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef _I2S_STM32_H
21+
#define _I2S_STM32_H
22+
23+
#include <stm32f7xx_hal.h>
24+
#include <i2s_stm32f7/stm32_pin_cfg.h>
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
struct i2s;
31+
struct i2s_cfg;
32+
struct stm32_spi_cfg;
33+
34+
struct stm32_i2s_pins {
35+
stm32_pin_cfg_t ck_pin;
36+
stm32_pin_cfg_t ws_pin;
37+
stm32_pin_cfg_t sd_pin;
38+
stm32_pin_cfg_t mck_pin;
39+
};
40+
41+
#define I2S_PIN(n, port, pin) &(I2S ## n ## _P ## port ## pin)
42+
#define I2S_CK_PIN(n, port, pin) &(I2S ## n ## _CK_P ## port ## pin)
43+
#define I2S_WS_PIN(n, port, pin) &(I2S ## n ## _WS_P ## port ## pin)
44+
#define I2S_SD_PIN(n, port, pin) &(I2S ## n ## _SD_P ## port ## pin)
45+
46+
struct stm32_dma_cfg {
47+
uint8_t dma_num;
48+
IRQn_Type dma_stream_irq;
49+
DMA_Stream_TypeDef *dma_stream;
50+
uint32_t dma_channel;
51+
};
52+
53+
struct i2s_cfg {
54+
uint32_t mode;
55+
uint32_t standard;
56+
uint32_t data_format;
57+
uint32_t sample_rate;
58+
59+
struct i2s_buffer_pool *pool;
60+
const struct stm32_spi_cfg *spi_cfg;
61+
const struct stm32_dma_cfg *dma_cfg;
62+
struct stm32_i2s_pins pins;
63+
};
64+
65+
#define SPI_CFG(n) &(spi ## n ## _cfg)
66+
67+
struct stm32_i2s {
68+
I2S_HandleTypeDef hi2s;
69+
DMA_HandleTypeDef *hdma_spi;
70+
71+
struct i2s *i2s;
72+
struct i2s_sample_buffer *dma_buffers[2];
73+
uint8_t dma_buffer_count;
74+
};
75+
76+
#define DMA_CFG(dma, ch, st, name) &(name ## _stream ## st ## _channel ## ch)
77+
78+
#define DMA_STREAM_DECLARE(dma, ch, st, name) \
79+
extern const struct stm32_dma_cfg name ## _stream ## st ## _channel ## ch
80+
81+
DMA_STREAM_DECLARE(1, 0, 0, spi3_rx);
82+
DMA_STREAM_DECLARE(1, 0, 1, spdifrx_dt);
83+
DMA_STREAM_DECLARE(1, 0, 2, spi3_rx);
84+
DMA_STREAM_DECLARE(1, 0, 3, spi2_rx);
85+
DMA_STREAM_DECLARE(1, 0, 4, spi2_tx);
86+
DMA_STREAM_DECLARE(1, 0, 5, spi3_tx);
87+
DMA_STREAM_DECLARE(1, 0, 6, spdifrx_cs);
88+
DMA_STREAM_DECLARE(1, 0, 7, spi3_tx);
89+
90+
DMA_STREAM_DECLARE(1, 1, 0, i2c1_rx);
91+
DMA_STREAM_DECLARE(1, 1, 1, i2c3_rx);
92+
DMA_STREAM_DECLARE(1, 1, 2, tim7_up);
93+
DMA_STREAM_DECLARE(1, 1, 4, tim7_up);
94+
DMA_STREAM_DECLARE(1, 1, 5, i2c1_rx);
95+
DMA_STREAM_DECLARE(1, 1, 6, i2c1_tx);
96+
DMA_STREAM_DECLARE(1, 1, 7, i2c1_tx);
97+
98+
DMA_STREAM_DECLARE(1, 2, 0, tim4_ch1);
99+
DMA_STREAM_DECLARE(1, 2, 2, i2c4_rx);
100+
DMA_STREAM_DECLARE(1, 2, 3, tim4_ch2);
101+
DMA_STREAM_DECLARE(1, 2, 5, i2c4_rx);
102+
DMA_STREAM_DECLARE(1, 2, 6, tim4_up);
103+
DMA_STREAM_DECLARE(1, 2, 7, tim4_ch3);
104+
105+
DMA_STREAM_DECLARE(1, 3, 1, tim2_up);
106+
DMA_STREAM_DECLARE(1, 3, 1, tim2_ch3);
107+
DMA_STREAM_DECLARE(1, 3, 2, i2c3_rx);
108+
DMA_STREAM_DECLARE(1, 3, 4, i2c3_tx);
109+
DMA_STREAM_DECLARE(1, 3, 5, tim2_ch1);
110+
DMA_STREAM_DECLARE(1, 3, 6, tim2_ch2);
111+
DMA_STREAM_DECLARE(1, 3, 6, tim2_ch4);
112+
DMA_STREAM_DECLARE(1, 3, 7, tim2_up);
113+
DMA_STREAM_DECLARE(1, 3, 7, tim2_ch4);
114+
115+
DMA_STREAM_DECLARE(1, 4, 0, uart5_rx);
116+
DMA_STREAM_DECLARE(1, 4, 1, usart3_rx);
117+
DMA_STREAM_DECLARE(1, 4, 2, uart4_rx);
118+
DMA_STREAM_DECLARE(1, 4, 3, usart3_tx);
119+
DMA_STREAM_DECLARE(1, 4, 4, uart4_tx);
120+
DMA_STREAM_DECLARE(1, 4, 5, usart2_rx);
121+
DMA_STREAM_DECLARE(1, 4, 6, usart2_tx);
122+
DMA_STREAM_DECLARE(1, 4, 7, uart5_tx);
123+
124+
DMA_STREAM_DECLARE(1, 5, 0, uart8_tx);
125+
DMA_STREAM_DECLARE(1, 5, 1, uart7_tx);
126+
DMA_STREAM_DECLARE(1, 5, 2, tim3_ch4);
127+
DMA_STREAM_DECLARE(1, 5, 2, tim3_up);
128+
DMA_STREAM_DECLARE(1, 5, 3, uart7_rx);
129+
DMA_STREAM_DECLARE(1, 5, 4, tim3_ch1);
130+
DMA_STREAM_DECLARE(1, 5, 4, tim3_trig);
131+
DMA_STREAM_DECLARE(1, 5, 5, tim3_ch2);
132+
DMA_STREAM_DECLARE(1, 5, 6, uart8_rx);
133+
DMA_STREAM_DECLARE(1, 5, 7, tim3_ch3);
134+
135+
DMA_STREAM_DECLARE(1, 6, 0, tim5_ch3);
136+
DMA_STREAM_DECLARE(1, 6, 0, tim5_up);
137+
DMA_STREAM_DECLARE(1, 6, 1, tim5_ch4);
138+
DMA_STREAM_DECLARE(1, 6, 1, tim5_trig);
139+
DMA_STREAM_DECLARE(1, 6, 2, tim5_ch1);
140+
DMA_STREAM_DECLARE(1, 6, 2, tim3_up);
141+
DMA_STREAM_DECLARE(1, 6, 3, tim5_ch4);
142+
DMA_STREAM_DECLARE(1, 6, 3, tim5_trig);
143+
DMA_STREAM_DECLARE(1, 6, 4, tim5_ch2);
144+
DMA_STREAM_DECLARE(1, 6, 6, tim5_up);
145+
146+
DMA_STREAM_DECLARE(1, 7, 1, tim6_up);
147+
DMA_STREAM_DECLARE(1, 7, 2, i2c2_rx);
148+
DMA_STREAM_DECLARE(1, 7, 3, i2c2_rx);
149+
DMA_STREAM_DECLARE(1, 7, 4, usart3_tx);
150+
DMA_STREAM_DECLARE(1, 7, 5, dac1);
151+
DMA_STREAM_DECLARE(1, 7, 6, dac2);
152+
DMA_STREAM_DECLARE(1, 7, 7, i2c2_tx);
153+
154+
DMA_STREAM_DECLARE(1, 8, 0, i2c3_tx);
155+
DMA_STREAM_DECLARE(1, 8, 1, i2c4_rx);
156+
DMA_STREAM_DECLARE(1, 8, 4, i2c2_tx);
157+
DMA_STREAM_DECLARE(1, 8, 6, i2c4_tx);
158+
159+
DMA_STREAM_DECLARE(1, 9, 1, i2c2_rx);
160+
DMA_STREAM_DECLARE(1, 9, 6, i2c2_tx);
161+
162+
DMA_STREAM_DECLARE(2, 0, 0, adc1);
163+
DMA_STREAM_DECLARE(2, 0, 1, sai1_a);
164+
DMA_STREAM_DECLARE(2, 0, 2, tim8_ch1);
165+
DMA_STREAM_DECLARE(2, 0, 2, tim8_ch2);
166+
DMA_STREAM_DECLARE(2, 0, 2, tim8_ch3);
167+
DMA_STREAM_DECLARE(2, 0, 3, sai1_a);
168+
DMA_STREAM_DECLARE(2, 0, 4, adc1);
169+
DMA_STREAM_DECLARE(2, 0, 5, sai1_b);
170+
DMA_STREAM_DECLARE(2, 0, 6, tim1_ch1);
171+
DMA_STREAM_DECLARE(2, 0, 6, tim1_ch2);
172+
DMA_STREAM_DECLARE(2, 0, 6, tim1_ch3);
173+
DMA_STREAM_DECLARE(2, 0, 7, sai1_b);
174+
175+
DMA_STREAM_DECLARE(2, 1, 1, dcmi);
176+
DMA_STREAM_DECLARE(2, 1, 2, adc2);
177+
DMA_STREAM_DECLARE(2, 1, 3, adc2);
178+
DMA_STREAM_DECLARE(2, 1, 4, sai1_b);
179+
DMA_STREAM_DECLARE(2, 1, 5, spi6_tx);
180+
DMA_STREAM_DECLARE(2, 1, 6, spi6_rx);
181+
DMA_STREAM_DECLARE(2, 1, 7, dcmi);
182+
183+
DMA_STREAM_DECLARE(2, 2, 0, adc3);
184+
DMA_STREAM_DECLARE(2, 2, 1, adc3);
185+
DMA_STREAM_DECLARE(2, 2, 3, spi5_rx);
186+
DMA_STREAM_DECLARE(2, 2, 4, spi5_tx);
187+
DMA_STREAM_DECLARE(2, 2, 5, cryp_out);
188+
DMA_STREAM_DECLARE(2, 2, 6, cryp_in);
189+
DMA_STREAM_DECLARE(2, 2, 7, hash_in);
190+
191+
DMA_STREAM_DECLARE(2, 3, 0, spi1_rx);
192+
DMA_STREAM_DECLARE(2, 3, 2, spi1_rx);
193+
DMA_STREAM_DECLARE(2, 3, 3, spi1_tx);
194+
DMA_STREAM_DECLARE(2, 3, 4, sai2_a);
195+
DMA_STREAM_DECLARE(2, 3, 5, spi1_tx);
196+
DMA_STREAM_DECLARE(2, 3, 6, sai2_b);
197+
DMA_STREAM_DECLARE(2, 3, 7, quadspi);
198+
199+
DMA_STREAM_DECLARE(2, 4, 0, spi4_rx);
200+
DMA_STREAM_DECLARE(2, 4, 1, spi4_tx);
201+
DMA_STREAM_DECLARE(2, 4, 2, usart1_rx);
202+
DMA_STREAM_DECLARE(2, 4, 3, sdmmc1);
203+
DMA_STREAM_DECLARE(2, 4, 5, usart1_rx);
204+
DMA_STREAM_DECLARE(2, 4, 6, sdmmc1);
205+
DMA_STREAM_DECLARE(2, 4, 7, usart1_tx);
206+
207+
DMA_STREAM_DECLARE(2, 5, 1, usart6_rx);
208+
DMA_STREAM_DECLARE(2, 5, 2, usart6_rx);
209+
DMA_STREAM_DECLARE(2, 5, 3, spi4_rx);
210+
DMA_STREAM_DECLARE(2, 5, 4, spi4_tx);
211+
DMA_STREAM_DECLARE(2, 5, 5, spi5_tx);
212+
DMA_STREAM_DECLARE(2, 5, 6, usart6_tx);
213+
DMA_STREAM_DECLARE(2, 5, 7, usart6_tx);
214+
215+
DMA_STREAM_DECLARE(2, 6, 0, tim1_trig);
216+
DMA_STREAM_DECLARE(2, 6, 1, tim1_ch1);
217+
DMA_STREAM_DECLARE(2, 6, 2, tim1_ch2);
218+
DMA_STREAM_DECLARE(2, 6, 3, tim1_ch1);
219+
DMA_STREAM_DECLARE(2, 6, 4, tim1_ch4);
220+
DMA_STREAM_DECLARE(2, 6, 4, tim1_trig);
221+
DMA_STREAM_DECLARE(2, 6, 4, tim1_com);
222+
DMA_STREAM_DECLARE(2, 6, 5, tim1_up);
223+
DMA_STREAM_DECLARE(2, 6, 6, tim1_ch3);
224+
225+
DMA_STREAM_DECLARE(2, 7, 1, tim8_up);
226+
DMA_STREAM_DECLARE(2, 7, 2, tim8_ch1);
227+
DMA_STREAM_DECLARE(2, 7, 3, tim8_ch2);
228+
DMA_STREAM_DECLARE(2, 7, 4, tim8_ch3);
229+
DMA_STREAM_DECLARE(2, 7, 5, spi5_rx);
230+
DMA_STREAM_DECLARE(2, 7, 6, spi5_tx);
231+
DMA_STREAM_DECLARE(2, 7, 7, tim8_ch4);
232+
DMA_STREAM_DECLARE(2, 7, 7, tim8_trig);
233+
DMA_STREAM_DECLARE(2, 7, 7, tim8_com);
234+
235+
DMA_STREAM_DECLARE(2, 8, 0, dfsdm1_flt0);
236+
DMA_STREAM_DECLARE(2, 8, 1, dfsdm1_flt1);
237+
DMA_STREAM_DECLARE(2, 8, 2, dfsdm1_flt2);
238+
DMA_STREAM_DECLARE(2, 8, 3, dfsdm1_flt3);
239+
DMA_STREAM_DECLARE(2, 8, 4, dfsdm1_flt0);
240+
DMA_STREAM_DECLARE(2, 8, 5, dfsdm1_flt1);
241+
DMA_STREAM_DECLARE(2, 8, 6, dfsdm1_flt2);
242+
DMA_STREAM_DECLARE(2, 8, 7, dfsdm1_flt3);
243+
244+
DMA_STREAM_DECLARE(2, 9, 0, jpeg_in);
245+
DMA_STREAM_DECLARE(2, 9, 1, jpeg_out);
246+
DMA_STREAM_DECLARE(2, 9, 2, spi4_tx);
247+
DMA_STREAM_DECLARE(2, 9, 3, jpeg_in);
248+
DMA_STREAM_DECLARE(2, 9, 4, jpeg_out);
249+
DMA_STREAM_DECLARE(2, 9, 5, spi5_rx);
250+
251+
DMA_STREAM_DECLARE(2, 10, 0, sai1_b);
252+
DMA_STREAM_DECLARE(2, 10, 1, sai2_b);
253+
DMA_STREAM_DECLARE(2, 10, 2, sai2_a);
254+
DMA_STREAM_DECLARE(2, 10, 6, sai1_a);
255+
256+
DMA_STREAM_DECLARE(2, 11, 0, sdmmc2);
257+
DMA_STREAM_DECLARE(2, 11, 2, suadspi);
258+
DMA_STREAM_DECLARE(2, 11, 5, sdmmc2);
259+
260+
#define SPI_CFG_DECLARE(n) \
261+
extern struct stm32_spi_cfg spi ## n ## _cfg;
262+
263+
SPI_CFG_DECLARE(1);
264+
SPI_CFG_DECLARE(2);
265+
SPI_CFG_DECLARE(3);
266+
SPI_CFG_DECLARE(4);
267+
SPI_CFG_DECLARE(5);
268+
269+
#define I2S_PIN_DECLARE(n, port, pin) \
270+
extern const struct stm32_pin_cfg I2S ## n ## _P ## port ## pin;
271+
#define I2S_CK_PIN_DECLARE(n, port, pin) \
272+
extern const struct stm32_pin_cfg I2S ## n ## _CK_P ## port ## pin;
273+
#define I2S_WS_PIN_DECLARE(n, port, pin) \
274+
extern const struct stm32_pin_cfg I2S ## n ## _WS_P ## port ## pin;
275+
#define I2S_SD_PIN_DECLARE(n, port, pin) \
276+
extern const struct stm32_pin_cfg I2S ## n ## _SD_P ## port ## pin;
277+
278+
279+
/* I2S1 Possible CK pins */
280+
I2S_CK_PIN_DECLARE(1, A, 5);
281+
I2S_CK_PIN_DECLARE(1, B, 3);
282+
I2S_CK_PIN_DECLARE(1, G, 11);
283+
284+
/* I2S1 possible WS pins */
285+
I2S_WS_PIN_DECLARE(1, A, 4);
286+
I2S_WS_PIN_DECLARE(1, A, 15);
287+
I2S_WS_PIN_DECLARE(1, G, 10);
288+
289+
/* I2S1 possible SD pins */
290+
I2S_SD_PIN_DECLARE(1, B, 5);
291+
I2S_SD_PIN_DECLARE(1, A, 7);
292+
I2S_SD_PIN_DECLARE(1, D, 7);
293+
294+
/* I2S1 possible MCK pins */
295+
I2S_SD_PIN_DECLARE(1, C, 4);
296+
297+
/* I2S2 Possible CKIN pins */
298+
I2S_PIN_DECLARE(2, C, 9);
299+
300+
/* I2S2 Possible MCK pins */
301+
#ifdef GPIO_AF5_SPI2
302+
I2S_PIN_DECLARE(2, C, 6);
303+
#endif
304+
305+
/* I2S2 Possible CK pins */
306+
I2S_CK_PIN_DECLARE(2, A, 9);
307+
I2S_CK_PIN_DECLARE(2, A, 12);
308+
I2S_CK_PIN_DECLARE(2, B, 10);
309+
I2S_CK_PIN_DECLARE(2, B, 13);
310+
I2S_CK_PIN_DECLARE(2, D, 3);
311+
I2S_CK_PIN_DECLARE(2, I, 1);
312+
313+
/* I2S2 possible WS pins */
314+
I2S_WS_PIN_DECLARE(2, A, 11);
315+
I2S_WS_PIN_DECLARE(2, B, 4);
316+
I2S_WS_PIN_DECLARE(2, B, 9);
317+
I2S_WS_PIN_DECLARE(2, B, 12);
318+
I2S_WS_PIN_DECLARE(2, I, 0);
319+
320+
/* I2S2 possible SD pins */
321+
I2S_SD_PIN_DECLARE(2, B, 15);
322+
I2S_SD_PIN_DECLARE(2, C, 1);
323+
I2S_SD_PIN_DECLARE(2, C, 3);
324+
I2S_SD_PIN_DECLARE(2, I, 3);
325+
326+
/* I2S3 possible CK pins */
327+
I2S_CK_PIN_DECLARE(3, B, 3);
328+
I2S_CK_PIN_DECLARE(3, C, 10);
329+
330+
/* I2S3 possible WS pins */
331+
I2S_WS_PIN_DECLARE(3, A, 4);
332+
I2S_WS_PIN_DECLARE(3, A, 15);
333+
334+
/* I2S3 possible SD pins */
335+
I2S_SD_PIN_DECLARE(3, B, 2);
336+
I2S_SD_PIN_DECLARE(3, B, 5);
337+
I2S_SD_PIN_DECLARE(3, C, 12);
338+
#ifdef GPIO_AF5_SPI3
339+
I2S_SD_PIN_DECLARE(3, D, 6);
340+
#endif
341+
342+
#ifdef __cplusplus
343+
}
344+
#endif
345+
346+
#endif /* _I2S_STM32_H */
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef _STM32_PIN_CFG_H
21+
#define _STM32_PIN_CFG_H
22+
23+
#include <stdint.h>
24+
#include <mcu/stm32_hal.h>
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
struct stm32_pin_cfg {
31+
int8_t pin;
32+
GPIO_InitTypeDef hal_init;
33+
};
34+
35+
typedef const struct stm32_pin_cfg *stm32_pin_cfg_t;
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
#endif /* _STM32_PIN_CFG_H */

0 commit comments

Comments
 (0)