arduino-audio-driver
Loading...
Searching...
No Matches
DriverCommon.h
Go to the documentation of this file.
1#pragma once
2
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
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
328enum class PinLogic {
332 Input,
333 Output,
334 Inactive,
336};
337
344enum class PinFunction {
345 UNDEFINED = 0,
348 PA,
349 POWER,
350 LED,
351 KEY,
352 SD,
353 CODEC,
354 CODEC_ADC,
355 LATCH,
356 RESET,
358 EXPANDER,
359};
360
367enum class AudioDriverKey {
368 KEY_REC = 0,
369 KEY_MODE,
370 KEY_PLAY,
371 KEY_SET,
374};
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 GPIO_UNDEFINED
Definition DriverCommon.h:131
Namespace for audio driver components.
Definition AudioBoard.h:5
AudioDriverKey
Definition DriverCommon.h:367
void * spi_bus_handle_t
Definition DriverCommon.h:156
int16_t GpioPin
Definition DriverCommon.h:153
PinLogic
Definition DriverCommon.h:328
input_device_t
Definition DriverCommon.h:170
@ 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
sample_bits_t
Definition DriverCommon.h:235
@ 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
PinFunction
Definition DriverCommon.h:344
int16_t ADCPin
Definition DriverCommon.h:154
codec_mode_t
Definition DriverCommon.h:290
@ 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
int error_t
Definition DriverCommon.h:150
output_device_t
Definition DriverCommon.h:186
@ 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
samplerate_t
Definition DriverCommon.h:213
@ 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
channels_t
Definition DriverCommon.h:274
@ CHANNELS2
Definition DriverCommon.h:275
@ CHANNELS16
Definition DriverCommon.h:278
@ CHANNELS8
Definition DriverCommon.h:277
@ CHANNELS4
Definition DriverCommon.h:276
es_mic_gain_t
Definition DriverCommon.h:305
@ 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_format_t
Definition DriverCommon.h:250
@ I2S_RIGHT
Definition DriverCommon.h:253
@ I2S_DSP
Definition DriverCommon.h:254
@ I2S_LEFT
Definition DriverCommon.h:252
@ I2S_NORMAL
Definition DriverCommon.h:251
i2s_master_slave_t
Definition DriverCommon.h:203
@ MODE_MASTER
Definition DriverCommon.h:205
@ MODE_SLAVE
Definition DriverCommon.h:204
signal_t
Definition DriverCommon.h:262
@ SIGNAL_DIGITAL
Definition DriverCommon.h:263
@ SIGNAL_TDM
Definition DriverCommon.h:266
@ SIGNAL_PDM
Definition DriverCommon.h:265
@ SIGNAL_AMALOG
Definition DriverCommon.h:264
void * i2c_bus_handle_t
Definition DriverCommon.h:155
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