Esp32/Core/i2s.h
Go to the documentation of this file.
1 /*
2  i2s.h - Software I2S library for esp8266
3 
4  (Sming Arduino compatility layer)
5 
6  Copyright (c) 2015 Hristo Gochkov. All rights reserved.
7  This file is part of the esp8266 core for Arduino environment.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #ifndef I2S_h
24 #define I2S_h
25 
26 #include "sdkconfig.h"
27 #include "esp_system.h"
28 #include "esp_spi_flash.h"
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/task.h"
31 
32 #include <driver/i2s.h>
33 
34 /*
35 How does this work? Basically, to get sound, you need to:
36 - Connect an I2S codec to the I2S pins on the ESP.
37 - Start up a thread that's going to do the sound output
38 - Call i2s_begin()
39 - Call i2s_set_rate() with the sample rate you want.
40 - Generate sound and call i2s_write_sample() with 32-bit samples.
41 The 32bit samples basically are 2 16-bit signed values (the analog values for
42 the left and right channel) concatenated as (Rout<<16)+Lout
43 
44 i2s_write_sample will block when you're sending data too quickly, so you can just
45 generate and push data as fast as you can and i2s_write_sample will regulate the
46 speed.
47 */
48 
49 #include <stdint.h>
50 #include <stdbool.h>
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 // Blocking calls are time-limited (in milliseconds)
57 #define I2S_BLOCKING_TIMEOUT 100
58 
62 bool i2s_rxtx_begin(bool enableRx, bool enableTx);
63 
67 inline bool i2s_begin()
68 {
69  return i2s_rxtx_begin(false, true);
70 }
71 
75 inline void i2s_end()
76 {
77  // TODO:
78  // i2s_driver_uninstall(PORT);
79 }
80 
84 inline bool i2s_set_rate(uint32_t rate)
85 {
86  // TODO:
87  // return i2s_set_sample_rates(PORT, rate);
88  return false;
89 }
90 
91 /*
92  * These functions are implemented in the driver itself
93  */
94 // bool i2s_set_dividers(uint8_t bck_div, uint8_t mclk_div);
95 // float i2s_get_real_rate();
96 
97 bool i2s_is_full();
98 
99 bool i2s_rx_is_full();
100 
101 bool i2s_is_empty();
102 
103 bool i2s_rx_is_empty();
104 
106 
108 
112 inline bool i2s_write_sample(uint32_t sample)
113 {
114  // return i2s_write(PORT, &sample, sizeof(sample), I2S_BLOCKING_TIMEOUT) == sizeof(sample);
115  return false;
116 }
117 
121 inline bool i2s_write_sample_nb(uint32_t sample)
122 {
123  // return i2s_write(PORT, &sample, sizeof(sample), 0) == sizeof(sample);
124  return false;
125 }
126 
131 bool i2s_write_lr(int16_t left, int16_t right);
132 
137 bool i2s_read_sample(int16_t* left, int16_t* right, bool blocking);
138 
143 bool i2s_is_full();
144 
149 bool i2s_is_empty();
150 
154 bool i2s_rx_is_full();
155 
159 bool i2s_rx_is_empty();
160 
166 
172 
176 void i2s_set_callback(void (*callback)(void));
177 
181 void i2s_rx_set_callback(void (*callback)(void));
182 
187 uint16_t i2s_write_buffer_mono(int16_t* frames, uint16_t frame_count);
188 
192 uint16_t i2s_write_buffer_mono_nb(int16_t* frames, uint16_t frame_count);
193 
197 uint16_t i2s_write_buffer(int16_t* frames, uint16_t frame_count);
198 
202 uint16_t i2s_write_buffer_nb(int16_t* frames, uint16_t frame_count);
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif
bool i2s_begin()
Enable TX only, for compatibility.
Definition: Esp32/Core/i2s.h:67
void i2s_rx_set_callback(void(*callback)(void))
bool i2s_is_empty()
uint16_t i2s_write_buffer(int16_t *frames, uint16_t frame_count)
bool i2s_set_rate(uint32_t rate)
Definition: Esp32/Core/i2s.h:84
uint16_t i2s_available()
bool i2s_is_full()
uint16_t i2s_write_buffer_nb(int16_t *frames, uint16_t frame_count)
bool i2s_write_sample_nb(uint32_t sample)
Same as above but does not block when DMA is full and returns false instead.
Definition: Esp32/Core/i2s.h:121
uint16_t i2s_write_buffer_mono_nb(int16_t *frames, uint16_t frame_count)
bool i2s_write_sample(uint32_t sample)
32bit sample with channels being upper and lower 16 bits (blocking when DMA is full) ...
Definition: Esp32/Core/i2s.h:112
bool i2s_read_sample(int16_t *left, int16_t *right, bool blocking)
void i2s_end()
Definition: Esp32/Core/i2s.h:75
bool i2s_rxtx_begin(bool enableRx, bool enableTx)
bool i2s_rx_is_empty()
void i2s_set_callback(void(*callback)(void))
uint16_t i2s_write_buffer_mono(int16_t *frames, uint16_t frame_count)
Writes a buffer of frames into the DMA memory, returns the amount of frames written.
bool i2s_write_lr(int16_t left, int16_t right)
bool i2s_rx_is_full()
uint16_t i2s_rx_available()