README.md
1# I2S ES8311 Example
2
3(See the README.md file in the upper level 'examples' directory for more information about examples.)
4
5In this example, you can choose music mode or echo mode in menuconfig. In music mode, the example will play a piece of music in 'canon.pcm', and in echo mode, you can hear what you said in earphone.
6
7## ES8311 brief
8
9ES8311 low power mono audio codec features:
10
11- High performance and low power multi-bit delta-sigma audio ADC and DAC
12- I2S/PCM master or slave serial data port
13- I2C interface for configuration
14- ADC: 24-bit, 8 to 96 kHz sampling frequency
15- ADC: 100 dB signal to noise ratio, -93 dB THD+N
16- DAC: 24-bit, 8 to 96 kHz sampling frequency
17- DAC: 110 dB signal to noise ratio, -80 dB THD+N
18
19For more details, see [ES8311 datasheet](http://www.everest-semi.com/pdf/ES8311%20PB.pdf)
20
21## How to Use Example
22
23### Hardware Required
24
25* An ESP development board that support I2S.
26* A USB cable for power supply and programming.
27* A board with ES8311 codec, mic and earphone interface(e.g. ESP-LyraT-8311A extension board).
28
29### Connection
30```
31┌─────────────────┐ ┌──────────────────────────┐
32│ ESP │ │ ES8311 │
33│ │ │ │
34│ MCLK-GPIO 0 ├──────────►│PIN2-MCLK │
35│ │ │ │ ┌─────────┐
36│ BCLK-GPIO 4 ├──────────►│PIN6-BCLK PIN12-OUTP├───────────┤ │
37│ │ │ │ │ EARPHONE│
38│ WS-GPIO 5 ├──────────►│PIN8-LRCK PIN13-OUTN├───────────┤ │
39│ │ │ │ └─────────┘
40│ SDOUT-GPIO 18├──────────►│PIN9-SDIN │
41│ │ │ │
42│ SDIN-GPIO 19│◄──────────┤PIN7-SDOUT │
43│ │ │ │ ┌─────────┐
44│ │ │ PIN18-MIC1P├───────────┤ │
45│ SCL-GPIO 16├──────────►│PIN1 -CCLK │ │ MIC │
46│ (GPIO 7)│ │ PIN17-MIC1N├───────────┤ │
47│ SDA-GPIO 17│◄─────────►│PIN19-CDATA │ └─────────┘
48│ (GPIO 8)│ │ │
49│ VCC 3.3├───────────┤VCC │
50│ │ │ │
51│ GND├───────────┤GND │
52└─────────────────┘ └──────────────────────────┘
53```
54Note: Since ESP32-C3 board does not have GPIO 16/17, you can use other available GPIOs instead. In this example, we set GPIO 7/8 as I2C pins for ESP32-C3 and GPIO 16/17 for other chips.
55
56### Dependency
57
58This example is based on [es8311 component](https://components.espressif.com/component/espressif/es8311)
59
60The component can be installed by esp component manager. Since this example already installed it, no need to re-installed it again, but if you want to install this component in your own project, you can input the following command:
61```
62idf.py add-dependency espressif/es8311==0.0.2-alpha
63```
64
65If dependency is added, you can check `idf_component.yml` for more detail. When building this example or other project with managed-component in it, the component manager will search the componet online and download it under `managed_componets` folder.
66
67### Configure the Project
68
69```
70idf.py menuconfig
71```
72You can find configurations for this example in 'Example Configutation' tag.
73
74* In 'Example mode' subtag, you can set the example mode to 'music' or 'echo'. You can hear a piece of music in 'music' mode and echo the sound sampled by mic in 'echo' mode. You can also customize you own music to play as shown below.
75
76* In 'Set MIC gain' subtag, you can set the mic gain for echo mode.
77
78* In 'Voice volume', you can set the volum between 0 to 100.
79
80### Build and Flash
81
82Build the project and flash it to the board, then run monitor tool to view serial output:
83
84```
85idf.py -p PORT flash monitor
86```
87
88(To exit the serial monitor, type ``Ctrl-]``.)
89
90See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
91
92## Example Output
93
94Running this example in music mode, you can hear a piece of music (canon), the log is shown as follow:
95
96```
97I (348) I2S: DMA Malloc info, datalen=blocksize=1200, dma_buf_count=6
98I (348) I2S: DMA Malloc info, datalen=blocksize=1200, dma_buf_count=6
99I (358) I2S: I2S0, MCLK output by GPIO0
100I (368) DRV8311: ES8311 in Slave mode
101I (378) gpio: GPIO[10]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
102I (3718) i2s_es8311: I2S music played, 213996 bytes are written.
103I (7948) i2s_es8311: I2S music played, 213996 bytes are written.
104......
105```
106
107Running this example in echo mode, you can hear the sound in earphone that collected by mic.
108```
109I (312) I2S: DMA Malloc info, datalen=blocksize=1200, dma_buf_count=6
110I (312) I2S: DMA Malloc info, datalen=blocksize=1200, dma_buf_count=6
111I (322) I2S: I2S0, MCLK output by GPIO0
112I (332) DRV8311: ES8311 in Slave mode
113I (342) gpio: GPIO[10]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
114```
115
116If you have a logic analyzer, you can use a logic analyzer to grab GPIO signal directly. The following table describes the pins we use by default (Note that you can also use other pins for the same purpose).
117
118| pin name| function | gpio_num |
119|:---:|:---:|:---:|
120| MCLK |module clock | GPIO_NUM_0|
121| BCLK |bit clock | GPIO_NUM_4 |
122| WS |word select | GPIO_NUM_5 |
123| SDOUT |serial data out| GPIO_NUM_18 |
124| SDIN |serial data in | GPIO_NUM_19 |
125
126### Customize your own music
127
128The example have contained a piece of music in canon.pcm, if you want to play your own music, you can follow these steps:
129
1301. Choose the music in any format you want to play (e.g. a.mp3)
1312. Install 'ffmpeg' tool
1323. Check your music format using ```ffprobe a.mp3```, you can get the stream format (e.g. Stream #0.0: Audio: mp3, 44100Hz, stereo, s16p, 64kb/s)
1334. Cut your music since there is no enough space for the whole piece of music. ```ffmpeg -i a.mp3 -ss 00:00:00 -t 00:00:20 a_cut.mp3```
1345. Transfer the music format into .pcm. ```ffmpeg -i a_cut.mp3 -f s16ls -ar 16000 -ac -1 -acodec pcm_s16le a.pcm```
1356. Move 'a.pcm' under 'main' directory
1367. Replace 'canon.pcm' with 'a.pcm' in 'CMakeLists.txt' under 'main' directory
1378. Replace '_binary_canon_pcm_start' and '_binary_canon_pcm_end' with '_binary_a_pcm_start' and '_binary_a_pcm_end' in `i2s_es9311_example.c`(line 46/47)
1389. Download the example and enjoy your own music
139
140## Troubleshooting
141
142* Program upload failure
143
144 * Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
145 * The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
146
147For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
148