arduino-audio-driver
Loading...
Searching...
No Matches
DriverCommon.h
Go to the documentation of this file.
1#pragma once
2
11
12// ============================================================================
13// Standard Includes
14// ============================================================================
15
16#include <stdint.h>
17
18#include "ConfigAudioDriver.h"
20#include "Platforms/Logger.h"
21
22// ============================================================================
23// Platform Abstractions
24// ============================================================================
25
26#if defined(ARDUINO)
27#include "Arduino.h"
28#include "SPI.h"
29#include "Wire.h"
30#define DEFAULT_WIRE &Wire
31#else
32#define DEFAULT_WIRE nullptr
33#undef delay
34#ifndef HIGH
35#define HIGH 0x1
36#endif
37#ifndef LOW
38#define LOW 0x0
39#endif
40#ifndef INPUT
41#define INPUT 0x0
42#endif
43#ifndef OUTPUT
44#define OUTPUT 0x1
45#endif
46#ifndef INPUT_PULLUP
47#define INPUT_PULLUP 0x2
48#endif
49#ifndef INPUT_PULLDOWN
50#define INPUT_PULLDOWN 0x3
51#endif
52#endif
53
54// ============================================================================
55// Tuning Parameters
56// ============================================================================
57
58#ifndef TOUCH_LIMIT
59#define TOUCH_LIMIT 20
60#endif
61
62#ifndef LYRAT_MINI_RANGE
63#define LYRAT_MINI_RANGE 5
64#endif
65
66#ifndef LYRAT_MINI_DELAY_MS
67#define LYRAT_MINI_DELAY_MS 5
68#endif
69
70// ============================================================================
71// Error Codes
72// ============================================================================
73
74#define RESULT_OK 0
75#define RESULT_FAIL -1
76#define ERROR_INVALID_ARG 1
77
78// ============================================================================
79// Misc Definitions
80// ============================================================================
81
83#define I2C_END true
84
85// ============================================================================
86// GPIO & Bus Handle Abstractions
87// ============================================================================
88
89#ifdef __zephyr__
90
91#include <zephyr/device.h>
92#include <zephyr/drivers/adc.h>
93#include <zephyr/drivers/gpio.h>
94#include <zephyr/kernel.h>
95
96
97#ifndef IS_GPIO
98#define IS_GPIO(pin) (pin.port != nullptr)
99#endif
100
101#ifndef GPIO_TO_INT
102#define GPIO_TO_INT(pin) pin.pin
103#endif
104
105namespace audio_driver {
106
107// In Zephyr, GPIO pins are device-tree specifications.
108using GpioPin = ::gpio_dt_spec;
109using ADCPin = ::adc_dt_spec;
110using i2c_bus_handle_t = ::device*;
111using spi_bus_handle_t = ::device*;
112static GpioPin GPIO_UNDEFINED{nullptr, 0, 0};
113
114static inline bool operator==(audio_driver::GpioPin& a, audio_driver::GpioPin& b) {
115 return (a.port == b.port) && (a.pin == b.pin);
116}
117
119
120static inline bool operator!=(audio_driver::GpioPin& a, audio_driver::GpioPin& b) {
121 return !(a == b);
122}
123
124} // namespace audio_driver
125
126
127#else // Non-Zephyr platforms
128
129// Non-Zephyr platforms use simple integer pin numbers.
130#ifndef GPIO_UNDEFINED
131#define GPIO_UNDEFINED -1
132#endif
133
134#ifndef IS_GPIO
135#define IS_GPIO(pin) ((pin) != GPIO_UNDEFINED)
136#endif
137
138#ifndef GPIO_TO_INT
139#define GPIO_TO_INT(pin) (pin)
140#endif
141
142#endif // __zephyr__
143
144// ============================================================================
145// Common Data in Namespace
146// ============================================================================
147
148namespace audio_driver {
149
150using error_t = int;
151
152#ifndef __zephyr__
153using GpioPin = int16_t;
154using ADCPin = int16_t;
155using i2c_bus_handle_t = void*;
156using spi_bus_handle_t = void*;
157#endif
158
159// ----------------------------------------------------------------------------
160// ADC / DAC Selection
161// ----------------------------------------------------------------------------
162
178
192
193// ----------------------------------------------------------------------------
194// I2S Interface Configuration
195// ----------------------------------------------------------------------------
196
207
229
244
256
268
280
281// ----------------------------------------------------------------------------
282// Codec Operating Mode & Gain
283// ----------------------------------------------------------------------------
284
299
318
319// ----------------------------------------------------------------------------
320// GPIO Pin Abstractions
321// ----------------------------------------------------------------------------
322
337
360
375
376// ----------------------------------------------------------------------------
377// Configuration Structures
378// ----------------------------------------------------------------------------
379
392
402
403} // namespace audio_driver
404
405// ============================================================================
406// Optional Namespace Import
407// ============================================================================
408
409#if (defined(ARDUINO) && !defined(NO_USING_NAMESPACE_AUDIO_DRIVER)) || \
410 defined(USING_NAMESPACE_AUDIO_DRIVER)
411using namespace audio_driver;
412#endif
#define KEY(key)
Definition AudioKitEs8388v1.h:5
#define GPIO_UNDEFINED
Definition DriverCommon.h:131
AudioDriverKey
Logical key identifiers for hardware buttons.
Definition DriverCommon.h:367
input_device_t
Selects the ADC input source for the microphone signal. On single-ADC chips, this may select at the c...
Definition DriverCommon.h:170
PinFunction
Logical function assigned to a GPIO pin.
Definition DriverCommon.h:344
output_device_t
Selects the DAC output destination. On single-DAC devices, this may select at the channel level.
Definition DriverCommon.h:186
@ KEY_MODE
Definition DriverCommon.h:369
@ KEY_PLAY
Definition DriverCommon.h:370
@ KEY_SET
Definition DriverCommon.h:371
@ KEY_REC
Definition DriverCommon.h:368
@ KEY_VOLUME_DOWN
Definition DriverCommon.h:372
@ KEY_VOLUME_UP
Definition DriverCommon.h:373
@ MCLK_SOURCE
Definition DriverCommon.h:357
@ PA
Definition DriverCommon.h:348
@ UNDEFINED
Definition DriverCommon.h:345
@ SD
Definition DriverCommon.h:352
@ CODEC
Definition DriverCommon.h:353
@ LED
Definition DriverCommon.h:350
@ HEADPHONE_DETECT
Definition DriverCommon.h:346
@ EXPANDER
Definition DriverCommon.h:358
@ RESET
Definition DriverCommon.h:356
@ LATCH
Definition DriverCommon.h:355
@ POWER
Definition DriverCommon.h:349
@ CODEC_ADC
Definition DriverCommon.h:354
@ AUXIN_DETECT
Definition DriverCommon.h:347
PinLogic
Describes the electrical role and active level of a GPIO pin.
Definition DriverCommon.h:328
sample_bits_t
Number of bits per I2S sample.
Definition DriverCommon.h:235
codec_mode_t
Selects which codec path(s) are active.
Definition DriverCommon.h:290
samplerate_t
I2S sample rate in samples per second.
Definition DriverCommon.h:213
channels_t
Number of audio channels.
Definition DriverCommon.h:274
es_mic_gain_t
Microphone pre-amplifier gain.
Definition DriverCommon.h:305
i2s_format_t
I2S data format for the audio codec.
Definition DriverCommon.h:250
i2s_master_slave_t
I2S operating mode for the audio codec. Note: if the microcontroller is master, the codec must be sla...
Definition DriverCommon.h:203
signal_t
Signal type carried over the audio interface.
Definition DriverCommon.h:262
@ Output
Definition DriverCommon.h:333
@ Input
Definition DriverCommon.h:332
@ OutputActiveLow
Definition DriverCommon.h:335
@ Inactive
Definition DriverCommon.h:334
@ InputActiveTouch
Definition DriverCommon.h:331
@ InputActiveHigh
Definition DriverCommon.h:329
@ InputActiveLow
Definition DriverCommon.h:330
Namespace for audio driver components.
Definition AudioBoard.h:5
void * spi_bus_handle_t
Definition DriverCommon.h:156
int16_t GpioPin
Definition DriverCommon.h:153
int16_t ADCPin
Definition DriverCommon.h:154
int error_t
Definition DriverCommon.h:150
void * i2c_bus_handle_t
Definition DriverCommon.h:155
@ ADC_INPUT_ALL
Definition DriverCommon.h:175
@ ADC_INPUT_NONE
Definition DriverCommon.h:171
@ ADC_INPUT_LINE1
Definition DriverCommon.h:172
@ ADC_INPUT_LINE3
Definition DriverCommon.h:174
@ ADC_INPUT_LINE2
Definition DriverCommon.h:173
@ ADC_INPUT_DIFFERENCE
Definition DriverCommon.h:176
@ BIT_LENGTH_24BITS
Definition DriverCommon.h:240
@ BIT_LENGTH_18BITS
Definition DriverCommon.h:238
@ BIT_LENGTH_20BITS
Definition DriverCommon.h:239
@ BIT_LENGTH_32BITS
Definition DriverCommon.h:241
@ BIT_LENGTH_MAX
Definition DriverCommon.h:242
@ BIT_LENGTH_MIN
Definition DriverCommon.h:236
@ BIT_LENGTH_16BITS
Definition DriverCommon.h:237
@ CODEC_MODE_MIN
Definition DriverCommon.h:291
@ CODEC_MODE_ENCODE
Definition DriverCommon.h:293
@ CODEC_MODE_NONE
Definition DriverCommon.h:292
@ CODEC_MODE_BOTH
Definition DriverCommon.h:295
@ CODEC_MODE_MAX
Definition DriverCommon.h:297
@ CODEC_MODE_DECODE
Definition DriverCommon.h:294
@ CODEC_MODE_LINE_IN
Definition DriverCommon.h:296
@ DAC_OUTPUT_LINE1
Definition DriverCommon.h:188
@ DAC_OUTPUT_ALL
Definition DriverCommon.h:190
@ DAC_OUTPUT_NONE
Definition DriverCommon.h:187
@ DAC_OUTPUT_LINE2
Definition DriverCommon.h:189
@ RATE_44K
Definition DriverCommon.h:220
@ RATE_24K
Definition DriverCommon.h:218
@ RATE_22K
Definition DriverCommon.h:217
@ RATE_128K
Definition DriverCommon.h:225
@ RATE_8K
Definition DriverCommon.h:214
@ RATE_88K
Definition DriverCommon.h:223
@ RATE_176K
Definition DriverCommon.h:226
@ RATE_32K
Definition DriverCommon.h:219
@ RATE_96K
Definition DriverCommon.h:224
@ RATE_11K
Definition DriverCommon.h:215
@ RATE_64K
Definition DriverCommon.h:222
@ RATE_16K
Definition DriverCommon.h:216
@ RATE_48K
Definition DriverCommon.h:221
@ RATE_192K
Definition DriverCommon.h:227
@ CHANNELS2
Definition DriverCommon.h:275
@ CHANNELS16
Definition DriverCommon.h:278
@ CHANNELS8
Definition DriverCommon.h:277
@ CHANNELS4
Definition DriverCommon.h:276
@ MIC_GAIN_15DB
Definition DriverCommon.h:312
@ MIC_GAIN_MIN
Definition DriverCommon.h:306
@ MIC_GAIN_6DB
Definition DriverCommon.h:309
@ MIC_GAIN_9DB
Definition DriverCommon.h:310
@ MIC_GAIN_24DB
Definition DriverCommon.h:315
@ MIC_GAIN_MAX
Definition DriverCommon.h:316
@ MIC_GAIN_0DB
Definition DriverCommon.h:307
@ MIC_GAIN_12DB
Definition DriverCommon.h:311
@ MIC_GAIN_18DB
Definition DriverCommon.h:313
@ MIC_GAIN_21DB
Definition DriverCommon.h:314
@ MIC_GAIN_3DB
Definition DriverCommon.h:308
@ I2S_RIGHT
Definition DriverCommon.h:253
@ I2S_DSP
Definition DriverCommon.h:254
@ I2S_LEFT
Definition DriverCommon.h:252
@ I2S_NORMAL
Definition DriverCommon.h:251
@ MODE_MASTER
Definition DriverCommon.h:205
@ MODE_SLAVE
Definition DriverCommon.h:204
@ SIGNAL_DIGITAL
Definition DriverCommon.h:263
@ SIGNAL_TDM
Definition DriverCommon.h:266
@ SIGNAL_PDM
Definition DriverCommon.h:265
@ SIGNAL_AMALOG
Definition DriverCommon.h:264
I2S interface parameters for the audio codec.
Definition DriverCommon.h:384
i2s_format_t fmt
Definition DriverCommon.h:386
samplerate_t rate
Definition DriverCommon.h:387
i2s_master_slave_t mode
Definition DriverCommon.h:385
channels_t channels
Definition DriverCommon.h:389
signal_t signal_type
Definition DriverCommon.h:390
sample_bits_t bits
Definition DriverCommon.h:388
Top-level codec configuration passed to the audio driver at init.
Definition DriverCommon.h:397
output_device_t output_device
Definition DriverCommon.h:399
I2SDefinition i2s
Definition DriverCommon.h:400
input_device_t input_device
Definition DriverCommon.h:398