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 <driver/i2s.h>
27 
28 /*
29 How does this work? Basically, to get sound, you need to:
30 - Connect an I2S codec to the I2S pins on the ESP.
31 - Start up a thread that's going to do the sound output
32 - Call i2s_begin()
33 - Call i2s_set_rate() with the sample rate you want.
34 - Generate sound and call i2s_write_sample() with 32-bit samples.
35 The 32bit samples basically are 2 16-bit signed values (the analog values for
36 the left and right channel) concatenated as (Rout<<16)+Lout
37 
38 i2s_write_sample will block when you're sending data too quickly, so you can just
39 generate and push data as fast as you can and i2s_write_sample will regulate the
40 speed.
41 */
42 
43 #include <stdint.h>
44 #include <stdbool.h>
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 // Blocking calls are time-limited (in milliseconds)
51 #define I2S_BLOCKING_TIMEOUT 100
52 
56 bool i2s_rxtx_begin(bool enableRx, bool enableTx);
57 
61 inline bool i2s_begin()
62 {
63  return i2s_rxtx_begin(false, true);
64 }
65 
69 inline void i2s_end()
70 {
72 }
73 
77 inline bool i2s_set_rate(uint32_t rate)
78 {
79  return i2s_set_sample_rates(rate);
80 }
81 
82 /*
83  * These functions are implemented in the driver itself
84  */
85 // bool i2s_set_dividers(uint8_t bck_div, uint8_t mclk_div);
86 // float i2s_get_real_rate();
87 
88 bool i2s_is_full();
89 
90 bool i2s_rx_is_full();
91 
92 bool i2s_is_empty();
93 
94 bool i2s_rx_is_empty();
95 
97 
99 
103 inline bool i2s_write_sample(uint32_t sample)
104 {
105  return i2s_write(&sample, sizeof(sample), I2S_BLOCKING_TIMEOUT) == sizeof(sample);
106 }
107 
111 inline bool i2s_write_sample_nb(uint32_t sample)
112 {
113  return i2s_write(&sample, sizeof(sample), 0) == sizeof(sample);
114 }
115 
120 bool i2s_write_lr(int16_t left, int16_t right);
121 
126 bool i2s_read_sample(int16_t* left, int16_t* right, bool blocking);
127 
132 bool i2s_is_full();
133 
138 bool i2s_is_empty();
139 
143 bool i2s_rx_is_full();
144 
148 bool i2s_rx_is_empty();
149 
155 
161 
165 void i2s_set_callback(void (*callback)(void));
166 
170 void i2s_rx_set_callback(void (*callback)(void));
171 
176 uint16_t i2s_write_buffer_mono(int16_t* frames, uint16_t frame_count);
177 
181 uint16_t i2s_write_buffer_mono_nb(int16_t* frames, uint16_t frame_count);
182 
186 uint16_t i2s_write_buffer(int16_t* frames, uint16_t frame_count);
187 
191 uint16_t i2s_write_buffer_nb(int16_t* frames, uint16_t frame_count);
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif
size_t i2s_write(const void *src, size_t size, TickType_t ticks_to_wait)
writes a buffer of frames into the DMA memory, returns the amount of frames written.
uint16_t i2s_write_buffer_mono_nb(int16_t *frames, uint16_t frame_count)
void i2s_end()
Definition: Core/i2s.h:69
void i2s_rx_set_callback(void(*callback)(void))
bool i2s_is_full()
#define I2S_BLOCKING_TIMEOUT
Definition: Core/i2s.h:51
bool i2s_begin()
Enable TX only, for compatibility.
Definition: Core/i2s.h:61
bool i2s_is_empty()
bool i2s_read_sample(int16_t *left, int16_t *right, bool blocking)
bool i2s_rx_is_full()
bool i2s_set_rate(uint32_t rate)
Definition: Core/i2s.h:77
bool i2s_rxtx_begin(bool enableRx, bool enableTx)
void i2s_set_callback(void(*callback)(void))
bool i2s_write_sample(uint32_t sample)
32bit sample with channels being upper and lower 16 bits (blocking when DMA is full) ...
Definition: Core/i2s.h:103
uint16_t i2s_write_buffer(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: Core/i2s.h:111
uint16_t i2s_write_buffer_nb(int16_t *frames, uint16_t frame_count)
bool i2s_set_sample_rates(uint32_t rate)
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.
uint16_t i2s_rx_available()
uint16_t i2s_available()
bool i2s_rx_is_empty()
bool i2s_write_lr(int16_t left, int16_t right)
void i2s_driver_uninstall()
Uninstall I2S driver.