Mozzi  version v2.0
sound synthesis library for Arduino
config_checks_esp32.h
1 /*
2  * config_checks_esp32.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2023-2024 Thomas Friedrichsmeier and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10 */
11 
12 #ifndef CONFIG_CHECK_ESP32_H
13 #define CONFIG_CHECK_ESP32_H
14 
15 /**
16  * @page hardware_esp32 Mozzi on ESP32-based boards.
17  *
18  * port by Dieter Vandoren and Thomas Friedrichsmeier
19  *
20  * @section esp32_status Port status and notes
21  * - Since flash memory is not built into the ESP32, but connected, externally, it is much too slow for keeping wave tables, audio samples, etc. Instead, these are kept in RAM on this platform.
22  * - Asynchronous analog reads are not implemented. `mozziAnalogRead()` relays to `analogRead()`. @ref MOZZI_AUDIO_INPUT is not implemented
23  * - twi_nonblock is not ported
24  * - WIFI-activity not yet tested, but likely the same notes as for ESP8266 apply, i.e. @em any Wifi activity is likely to introdcue considerable nose. Consider turning off WIFI.
25  * - The implementation of audioTicks() may be slightly inaccurate on this platform.
26  *
27  * @section esp32_output Output modes
28  * The following audio modes (see @ref MOZZI_AUDIO_MODE) are currently supported on this hardware:
29  * - MOZZI_OUTPUT_EXTERNAL_TIMED
30  * - MOZZI_OUTPUT_EXTERNAL_CUSTOM
31  * - MOZZI_OUTPUT_PDM_VIA_I2S
32  * - MOZZI_OUTPUT_I2S_DAC
33  * - MOZZI_OUTPUT_INTERNAL_DAC
34  *
35  * The default mode is @ref esp32_internal_dac .
36  *
37  * @section esp32_internal_dac MOZZI_OUTPUT_INTERNAL_DAC
38  * The internal DAC has 8 bit resolution, and outputs to GPIO pins 25 and 26 (non-configurable). For simplicity of code, both pins are always used.
39  * In a mono configuration, both pins output the same sample.
40  *
41  * TODO: We could really use this to hack in a 2 PIN mode!
42  *
43  * @note
44  * The number 25 refers to "GPIO 25" or sometimes labelled "D25". Confusingly, many boards come with an additional, totally different numbering scheme on top of that.
45  * Check a detailed pinout diagram, if in any doubt.
46  *
47  * Internally, the inbuilt DAC is connected via an I2S interface. Which interface number to use can be configured using:
48  *
49  * @code
50  * #define MOZZI_I2S_PORT ... // (default: I2S_NUM_0)
51  * @endcode
52  *
53  * @section esp32_i2s_dac MOZZI_OUTPUT_I2S_DAC
54  * This mode outputs to a PT8211 (or compatible) I2S DAC, which allows for very high quality (mono or stereo) output. Communication needs the BCK, WS, and DATA(out) pins
55  * of one I2S interface. Presumably, any pins qualify, and you can configure this using:
56  * @code
57  * #define MOZZI_I2S_PIN_BCK ... // (default: 26)
58  * #define MOZZI_I2S_PIN_WS ... // (default: 15)
59  * #define MOZZI_I2S_PIN_DATA ... // (default: 33)
60  * #define MOZZI_I2S_PORT ... // (default: I2S_NUM_0)
61  * @endcode
62  *
63  * See the note above (@ref esp_internal_dac) regarding pin numbering. Also, please always test the default pinout, should a custom setting fail!
64  *
65  * As a technical note, I2S support in the ESP32 SDK has been reworked since this was implemented in Mozzi, and Mozzi uses the "legacy" implementation "i2s.h".
66  * This should not be an issue, unless you want to connect additional I2S components, yourself. In which case contributions are certainly welcome!
67  *
68  * @section esp32_pdm_via_i2s MOZZI_OUTPUT_PDM_VIA_I2S
69  * This mode uses the same setup as @ref esp32_i2s_dac, but rather than using an external DAC, the communication signal itself is modulated in PDM
70  * (pulse density modulation) encoded form. Thus not extra hardware is needed, and the signal is output on the DATA pin (see above). The BCK and
71  * WS pins are also claimed, but should be left non-connected, and do not produce anything meaningful. This can only be used in mono mode.
72  *
73  * Output resolution may be adjusted by defining MOZZI_PDM_RESOLUTION , where the default value of 4 means that each audio sample is encoded into four 32 bit blocks
74  * of ones and zeros. Obviously, more is potentially better, but at the cost of considerable computation power.
75  *
76  * @section esp32_external MOZZI_OUTPUT_EXTERNAL_TIMED and MOZZI_OUTPUT_EXTERNAL_CUSTOM
77  * See @ref external_audio
78 */
79 
80 #if not IS_ESP32()
81 #error This header should be included for ESP32 architecture, only
82 #endif
83 
85 #if !defined(MOZZI_AUDIO_MODE)
86 #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC
87 #endif
88 MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM, MOZZI_OUTPUT_PDM_VIA_I2S, MOZZI_OUTPUT_I2S_DAC, MOZZI_OUTPUT_INTERNAL_DAC)
89 
90 #if !defined(MOZZI_AUDIO_RATE)
91 #define MOZZI_AUDIO_RATE 32768
92 #endif
93 
94 #if defined(MOZZI_PWM_RATE)
95 #error Configuration of MOZZI_PWM_RATE is not currently supported on this platform (always same as MOZZI_AUDIO_RATE)
96 #endif
97 
98 #if !defined(MOZZI_ANALOG_READ)
99 # define MOZZI_ANALOG_READ MOZZI_ANALOG_READ_NONE
100 #endif
101 
102 MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
103 MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE)
104 
105 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC, MOZZI_OUTPUT_PDM_VIA_I2S)
106 # if !defined(MOZZI_I2S_PIN_BCK)
107 # define MOZZI_I2S_PIN_BCK 26
108 # endif
109 # if !defined(MOZZI_I2S_PIN_WS)
110 # define MOZZI_I2S_PIN_WS 25
111 # endif
112 # if !defined(MOZZI_I2S_PIN_DATA)
113 # define MOZZI_I2S_PIN_DATA 33
114 # endif
115 #endif
116 
117 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_I2S_DAC, MOZZI_OUTPUT_PDM_VIA_I2S)
118 # include <driver/i2s.h>
119 # if !defined(MOZZI_IS2_PORT)
120 # define MOZZI_I2S_PORT I2S_NUM_0
121 # endif
122 #endif
123 
124 #if !defined(MOZZI_AUDIO_BITS)
125 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC)
126 # define MOZZI_AUDIO_BITS 8
127 # else
128 # define MOZZI_AUDIO_BITS 16
129 # endif
130 #endif
131 
132 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_I2S)
133 # if !defined(MOZZI_PDM_RESOLUTION)
134 # define MOZZI_PDM_RESOLUTION 8
135 # endif
136 #else
137 # define MOZZI_PDM_RESOLUTION 1 // unconditionally, no other value allowed
138 #endif
139 
140 // All modes besides timed external bypass the output buffer!
141 #if !MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
142 # define BYPASS_MOZZI_OUTPUT_BUFFER true
143 #endif
144 
145 #define MOZZI__INTERNAL_ANALOG_READ_RESOLUTION 12
146 
147 #endif // #ifndef CONFIG_CHECK_ESP32_H