Skip to content

Commit 39a6be3

Browse files
committed
2 parents a4e9e19 + c7a1a10 commit 39a6be3

File tree

5 files changed

+214
-72
lines changed

5 files changed

+214
-72
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Arduino STM32F411 I2S Library
1+
# Arduino STM32 I2S Library
22

33
I wanted to use __I2S__ in Arduino with my __STM32F411 Black Pill__ processor together with my [Arduino Audio Tools](https://github.com/pschatzmann/arduino-audio-tools)!
44

@@ -18,15 +18,11 @@ My first trials failed miserably using the DMA versions of the HAL API, so I dec
1818
- Only __16bit__ data is supported
1919
- I also incuded the __codec drivers__ that are part of some stm32 evaluation boards.
2020

21+
Subsequently, I have extended the functionality to support other variants.
22+
2123
## Pins for I2S3
2224

23-
FUNCTIONs | BlackP | Disco
24-
-----------|--------|------
25-
MCK | PB10 | PC7
26-
BCK | PB3 | PC10
27-
WS (LRC) | PA4 | PA4
28-
SD | PB5 | PC3
29-
ext_SD | PB4 | PC12
25+
See [src/stm32-config-i2s.h](https://github.com/pschatzmann/stm32-i2s/blob/main/src/stm32-config-i2s.h).
3026

3127
## Supported Sample Rates
3228

@@ -55,6 +51,7 @@ using namespace stm32_i2s;
5551
5652
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
5753
I2SSettingsSTM32 i2s_settings;
54+
Stm32I2sClass I2S;
5855
int sample_rate = 8000;
5956
int channels = 1;
6057
@@ -92,6 +89,7 @@ using namespace stm32_i2s;
9289
9390
CsvStream<int16_t> out(Serial, 2); // ASCII output stream
9491
I2SSettingsSTM32 i2s_settings;
92+
Stm32I2sClass I2S;
9593
9694
void writeFromReceive(uint8_t *buffer, uint16_t byteCount, void*){
9795
out.write(buffer, byteCount);
@@ -100,7 +98,7 @@ void writeFromReceive(uint8_t *buffer, uint16_t byteCount, void*){
10098
void setup() {
10199
Serial.begin(115200);
102100
i2s_settings.sample_rate = I2S_AUDIOFREQ_8K;
103-
if (!I2s.beginReadDMA(i2s_settings, writeFromReceive){
101+
if (!I2S.beginReadDMA(i2s_settings, writeFromReceive){
104102
Serial.println("I2S Error");
105103
}
106104
}
@@ -122,7 +120,7 @@ You can download the library as zip and call include Library -> zip library. Or
122120

123121
```
124122
cd ~/Documents/Arduino/libraries
125-
git clone https://github.com/pschatzmann/stm32f411-i2s.git
123+
git clone https://github.com/pschatzmann/stm32-i2s.git
126124
```
127125

128126
I recommend to use git because you can easily update to the latest version just by executing the ```git pull``` command in the project folder.

library.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name=stm32f411-i2s
1+
name=stm32-i2s
22
version=0.0.1
33
author=Phil Schatzmann
44
maintainer=Phil Schatzmann
5-
sentence=i2s for STM32F04 Black Pill
6-
paragraph=i2s for STM32F04
5+
sentence=i2s for STM32
6+
paragraph=i2s for STM32 Microcontroller
77
category=Signal Input/Output
8-
url=https://github.com/pschatzmann/stm32f411-i2s
8+
url=https://github.com/pschatzmann/stm32-i2s
99
architectures=stm32

src/stm32-config-i2s.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#pragma once
22

3-
#define I2S_BUFFER_SIZE 512
43
#define STM32_I2S_WITH_OBJECT
54
#define USE_FULL_ASSERT
65

6+
#ifndef I2S_FULLDUPLEXMODE_DISABLE
7+
# define I2S_FULLDUPLEXMODE_DISABLE (0x00000000U)
8+
#endif
9+
#ifndef I2S_FULLDUPLEXMODE_ENABLE
10+
# define I2S_FULLDUPLEXMODE_ENABLE (0x00000001U)
11+
#endif
12+
713
#ifdef ARDUINO_BLACKPILL_F411CE
814
#define SPI_INSTANCE_FOR_I2S SPI3
915
#define STM_I2S_PINS \
@@ -18,6 +24,8 @@
1824
#define PLLM 16
1925
#define PLLN 192
2026
#define PLLR 2
27+
#define IS_F4
28+
2129
#endif
2230

2331
#ifdef ARDUINO_GENERIC_F411VETX
@@ -34,5 +42,46 @@
3442
#define PLLM 16
3543
#define PLLN 100
3644
#define PLLR 2
45+
#define IS_F4
46+
47+
#endif
48+
49+
#ifdef STM32H750xx
50+
#define SPI_INSTANCE_FOR_I2S SPI3
51+
#define STM_I2S_PINS \
52+
{ \
53+
{mclk, PC_7, GPIO_AF6_SPI3},\
54+
{bck, PC_10, GPIO_AF6_SPI3},\
55+
{ws, PA_4, GPIO_AF6_SPI3},\
56+
{data_out, PB_2, GPIO_AF6_SPI3},\
57+
{data_in, PC_11, GPIO_AF6_SPI3}\
58+
};
59+
#define IS_H7
60+
#endif
61+
62+
#ifdef STM32H743xx
63+
#define SPI_INSTANCE_FOR_I2S SPI3
64+
#define STM_I2S_PINS \
65+
{ \
66+
{mclk, PC_7, GPIO_AF6_SPI3},\
67+
{bck, PC_10, GPIO_AF6_SPI3},\
68+
{ws, PA_4, GPIO_AF6_SPI3},\
69+
{data_out, PB_5, GPIO_AF6_SPI3},\
70+
{data_in, PB_4, GPIO_AF6_SPI3}\
71+
};
72+
#define IS_H7
3773
#endif
3874

75+
#ifdef STM32F723xx
76+
#define SPI_INSTANCE_FOR_I2S SPI3
77+
#define STM_I2S_PINS \
78+
{ \
79+
{mclk, PC_7, GPIO_AF6_SPI3},\
80+
{bck, PC_10, GPIO_AF6_SPI3},\
81+
{ws, PA_4, GPIO_AF6_SPI3},\
82+
{data_out, PC_12, GPIO_AF6_SPI3},\
83+
{data_in, PC_11, GPIO_AF6_SPI3}\
84+
};
85+
#define IS_F7
86+
#define SPI_CLOCK_SOURCE LL_RCC_SPI123_CLKSOURCE_PLL1Q
87+
#endif

src/stm32-i2s.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11

22
#include "stm32-i2s.h"
33

4+
#ifdef STM_I2S_PINS
5+
46
namespace stm32_i2s {
57

6-
Stm32I2sClass I2S;
8+
//Stm32I2sClass I2S;
9+
Stm32I2sClass *self_I2S = nullptr;
10+
711
bool stm32_i2s_is_error = false;
812

913
extern "C" void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
10-
I2S.cb_TxRxComplete(hi2s);
14+
self_I2S->cb_TxRxComplete(hi2s);
1115
}
1216

1317
extern "C" void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
14-
I2S.cb_TxRxHalfComplete(hi2s);
18+
self_I2S->cb_TxRxHalfComplete(hi2s);
1519
}
1620

1721
extern "C" void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) {
18-
I2S.cb_TxRxComplete(hi2s);
22+
self_I2S->cb_TxRxComplete(hi2s);
1923
}
2024

2125
extern "C" void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
22-
I2S.cb_TxRxHalfComplete(hi2s);
26+
self_I2S->cb_TxRxHalfComplete(hi2s);
2327
}
2428

25-
extern "C" void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) { Report_Error(); }
29+
extern "C" void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) { Report_Error(10); }
2630

2731
/**
2832
* @brief This function handles DMA1 stream0 global interrupt.
2933
*/
30-
extern "C" void DMA1_Stream0_IRQHandler(void) { I2S.cb_dmaIrqRx(); }
34+
extern "C" void DMA1_Stream0_IRQHandler(void) { self_I2S->cb_dmaIrqRx(); }
3135

3236
/**
3337
* @brief This function handles DMA1 stream5 global interrupt.
3438
*/
35-
extern "C" void DMA1_Stream5_IRQHandler(void) { I2S.cb_dmaIrqTx(); }
39+
extern "C" void DMA1_Stream5_IRQHandler(void) { self_I2S->cb_dmaIrqTx(); }
3640

3741
/**
3842
* @brief I2S MSP Initialization
3943
* This function configures the hardware resources used in this example
4044
* @param hi2s: I2S handle pointer
4145
* @retval None
4246
*/
43-
extern "C" void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s) { I2S.cb_HAL_I2S_MspInit(hi2s); }
47+
extern "C" void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s) { self_I2S->cb_HAL_I2S_MspInit(hi2s); }
4448

4549
/**
4650
* @brief I2S MSP De-Initialization
4751
* This function freeze the hardware resources used in this example
4852
* @param hi2s: I2S handle pointer
4953
* @retval None
5054
*/
51-
extern "C" void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s) { I2S.cb_HAL_I2S_MspDeInit(hi2s); }
55+
extern "C" void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s) { self_I2S->cb_HAL_I2S_MspDeInit(hi2s); }
5256
/**
5357
* @brief This function is executed in case of error occurrence.
5458
* @retval None
5559
*/
56-
void Report_Error() {
60+
void Report_Error(int no) {
5761
stm32_i2s_is_error = true;
58-
STM32_LOG("%s", "stm32-i2s: Error");
62+
STM32_LOG("%s %d", "stm32-i2s: Error", no);
5963
}
6064

6165
/**
@@ -68,8 +72,8 @@ void STM32_LOG(const char *fmt, ...) {
6872
va_start(arg, fmt);
6973
int len = vsnprintf(log_buffer + 7, 200, fmt, arg);
7074
va_end(arg);
71-
Serial.println(log_buffer);
72-
Serial.flush();
75+
self_I2S->STM32_LOG(log_buffer);
76+
va_end(arg);
7377
}
7478

7579
#ifdef USE_FULL_ASSERT
@@ -90,3 +94,6 @@ extern "C" void assert_failed(uint8_t *file, uint32_t line) {
9094
#endif /* USE_FULL_ASSERT */
9195

9296
}
97+
98+
#endif // STM_I2S_PINS
99+

0 commit comments

Comments
 (0)