Audio System + Drivers (#562)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
|
||||
|
||||
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
|
||||
|
||||
tactility_add_module(es7210-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel audio-codec-module esp_codec_dev
|
||||
)
|
||||
@@ -0,0 +1,195 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# ES7210 microphone ADC
|
||||
|
||||
A driver for the `ES7210` 4-channel microphone ADC by Everest Semiconductor,
|
||||
wired as an input-only `AUDIO_CODEC_TYPE` device. The I2C bus is the device's
|
||||
parent; the I2S controller carrying the (4-slot TDM) audio data is referenced by
|
||||
name.
|
||||
|
||||
Always configures the hardware for the full 4-slot TDM frame regardless of how
|
||||
many mics are actually wired (`mic-mask`), then demuxes down to just the active
|
||||
mic slots in software -- `es7210_set_fs()` silently halves the configured bit
|
||||
depth when asked for 2 or fewer TDM channels, which corrupts audio if the mic
|
||||
count is passed straight through.
|
||||
|
||||
Wraps Espressif's `esp_codec_dev` ES7210 implementation.
|
||||
|
||||
See https://www.everest-semi.com/pdf/ES7210%20PB.pdf
|
||||
|
||||
License: [Apache v2.0](LICENSE-Apache-2.0.md)
|
||||
@@ -0,0 +1,20 @@
|
||||
description: Everest Semiconductor ES7210 4-channel microphone ADC
|
||||
|
||||
include: ["i2c-device.yaml"]
|
||||
|
||||
compatible: "everest,es7210"
|
||||
|
||||
properties:
|
||||
i2s:
|
||||
type: phandle
|
||||
required: true
|
||||
description: "I2S controller device that carries audio data"
|
||||
mic-mask:
|
||||
type: int
|
||||
required: false
|
||||
default: 15
|
||||
description: "Bitmask of microphones to enable (bit0=MIC1 .. bit3=MIC4); defaults to all four"
|
||||
input-gain-percent:
|
||||
type: int
|
||||
default: 100
|
||||
description: "Extra digital gain multiplier applied by audio_stream on top of the ES7210's own 30dB hardware ADC gain, as an integer percentage (100 = 1.0x / no extra boost). devicetree has no float type, hence x100."
|
||||
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
- TactilityKernel
|
||||
bindings: bindings
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/es7210.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(es7210, struct Es7210Config)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,42 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/drivers/audio_codec.h>
|
||||
|
||||
struct Device;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief ES7210 codec device configuration.
|
||||
*
|
||||
* The ES7210 is an input-only (microphone ADC) codec. The I2C bus is the
|
||||
* device's parent (per i2c-device.yaml), while the I2S controller carrying
|
||||
* the (typically 4-slot TDM) audio data is referenced via a devicetree phandle.
|
||||
*/
|
||||
struct Es7210Config {
|
||||
/** I2C address on the bus (typically 0x40) */
|
||||
uint8_t address;
|
||||
/** I2S controller device that carries audio data */
|
||||
struct Device* i2s_device;
|
||||
/** Bitmask of microphones to enable, e.g. ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ... */
|
||||
uint8_t mic_selected_mask;
|
||||
/**
|
||||
* Extra fixed digital gain multiplier applied by audio_stream on top of the ES7210's
|
||||
* own hardware ADC gain (already at a hard-coded 30dB default out of a 0..37.5dB
|
||||
* range), as an integer percentage (100 = 1.0x / no extra boost). Small MEMS mic
|
||||
* capsules can still sound quiet even near max hardware gain; this is for boards where
|
||||
* 30dB hardware gain alone isn't enough. devicetree has no float property type, hence
|
||||
* the x100 integer encoding.
|
||||
*/
|
||||
uint16_t input_gain_percent;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/module.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern struct Module es7210_module;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,435 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/es7210.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/audio_codec.h>
|
||||
#include <tactility/drivers/audio_codec_adapters.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/drivers/i2s_controller.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <es7210_adc.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#define TAG "ES7210"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t NATIVE_SAMPLE_RATE = 16000;
|
||||
constexpr float MAX_INPUT_GAIN_DB = 37.5f; // ES7210 mic gain range is roughly 0..37.5 dB
|
||||
|
||||
struct Es7210Data {
|
||||
const audio_codec_ctrl_if_t* ctrl_if = nullptr;
|
||||
const audio_codec_data_if_t* data_if = nullptr;
|
||||
const audio_codec_if_t* codec_if = nullptr;
|
||||
esp_codec_dev_handle_t codec_device = nullptr;
|
||||
bool is_open = false;
|
||||
uint8_t mic_count = 4;
|
||||
uint8_t mic_mask = 0x0F;
|
||||
uint8_t tdm_slot_count = 4;
|
||||
uint8_t open_bits_per_sample = 16;
|
||||
uint32_t open_sample_rate = 0;
|
||||
float input_gain = 1.0f;
|
||||
std::vector<uint8_t> raw_buffer;
|
||||
};
|
||||
|
||||
#define GET_CONFIG(device) (static_cast<const Es7210Config*>((device)->config))
|
||||
#define GET_DATA(device) (static_cast<Es7210Data*>(device_get_driver_data(device)))
|
||||
|
||||
// region AudioCodecApi
|
||||
|
||||
error_t open(Device* device, const struct AudioCodecStreamConfig* config) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (config->direction == AUDIO_CODEC_DIR_OUTPUT || config->direction == AUDIO_CODEC_DIR_BOTH) {
|
||||
LOG_E(TAG, "ES7210 is input-only");
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (data->is_open) {
|
||||
bool same_config = config->bits_per_sample == data->open_bits_per_sample
|
||||
&& config->sample_rate == data->open_sample_rate;
|
||||
return same_config ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// Open with tdm_slot_count channels, not the (possibly smaller) active mic count.
|
||||
// tdm_slot_count is set in start_device() to match whichever framing the vendor
|
||||
// driver will actually use: the full 4-slot TDM frame when TDM mode engages
|
||||
// (mic_count >= 3), or exactly mic_count when it doesn't (plain N-channel I2S --
|
||||
// see start_device()'s tdm_slot_count comment). Opening with the wrong channel
|
||||
// count here mismatches the I2S receive layout the codec actually produces,
|
||||
// corrupting every channel past the first. When TDM *is* engaged, using the full
|
||||
// slot count (rather than just the active mic count) also sidesteps a separate
|
||||
// vendor quirk: es7210_set_fs() silently halves the configured bit depth when asked
|
||||
// for <= 2 channels in TDM mode (channel_mask == 0). We demux down to the active
|
||||
// mic slots ourselves in read().
|
||||
uint8_t hw_channels = data->tdm_slot_count;
|
||||
|
||||
esp_codec_dev_sample_info_t sample_info = {
|
||||
.bits_per_sample = config->bits_per_sample,
|
||||
.channel = hw_channels,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = config->sample_rate,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
|
||||
if (esp_codec_dev_open(data->codec_device, &sample_info) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open codec device");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
data->open_bits_per_sample = config->bits_per_sample;
|
||||
data->open_sample_rate = config->sample_rate;
|
||||
data->is_open = true;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t close(Device* device) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (data->is_open) {
|
||||
esp_codec_dev_close(data->codec_device);
|
||||
data->is_open = false;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t read(Device* device, void* buffer, size_t size, size_t* bytes_read, TickType_t timeout) {
|
||||
(void) timeout;
|
||||
auto* data = GET_DATA(device);
|
||||
if (!data->is_open) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// The hardware always runs at tdm_slot_count channels (see open() for why); demux down
|
||||
// to just the active mic slots here so callers see exactly mic_count channels of real
|
||||
// signal -- never diluted by silent/AEC-reference TDM slots.
|
||||
if (data->mic_count == data->tdm_slot_count) {
|
||||
int result = esp_codec_dev_read(data->codec_device, buffer, (int) size);
|
||||
if (result < 0) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
*bytes_read = (size_t) result;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
uint8_t bytes_per_sample = (uint8_t) (data->open_bits_per_sample / 8);
|
||||
size_t out_frame_bytes = (size_t) data->mic_count * bytes_per_sample;
|
||||
size_t requested_frames = (out_frame_bytes != 0) ? (size / out_frame_bytes) : 0;
|
||||
if (requested_frames == 0) {
|
||||
*bytes_read = 0;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
size_t raw_frame_bytes = (size_t) data->tdm_slot_count * bytes_per_sample;
|
||||
size_t raw_bytes_needed = requested_frames * raw_frame_bytes;
|
||||
if (data->raw_buffer.size() < raw_bytes_needed) {
|
||||
data->raw_buffer.resize(raw_bytes_needed);
|
||||
}
|
||||
|
||||
int result = esp_codec_dev_read(data->codec_device, data->raw_buffer.data(), (int) raw_bytes_needed);
|
||||
if (result < 0) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
size_t raw_bytes_read = (size_t) result;
|
||||
size_t frames_read = raw_bytes_read / raw_frame_bytes;
|
||||
|
||||
// Demux: pick the bytes_per_sample-wide slot for each set bit in mic_mask, in ascending
|
||||
// slot order, e.g. mic_mask = MIC1|MIC3 -> slots 0 and 2.
|
||||
uint8_t slot_indices[8];
|
||||
uint8_t slot_count = 0;
|
||||
for (uint8_t slot = 0; slot < data->tdm_slot_count && slot_count < sizeof(slot_indices); slot++) {
|
||||
if ((data->mic_mask & (uint8_t) (1u << slot)) != 0) {
|
||||
slot_indices[slot_count++] = slot;
|
||||
}
|
||||
}
|
||||
|
||||
auto* out = static_cast<uint8_t*>(buffer);
|
||||
const uint8_t* raw = data->raw_buffer.data();
|
||||
for (size_t frame = 0; frame < frames_read; frame++) {
|
||||
const uint8_t* raw_frame = raw + frame * raw_frame_bytes;
|
||||
uint8_t* out_frame = out + frame * out_frame_bytes;
|
||||
for (uint8_t ch = 0; ch < slot_count && ch < data->mic_count; ch++) {
|
||||
std::memcpy(out_frame + (size_t) ch * bytes_per_sample, raw_frame + (size_t) slot_indices[ch] * bytes_per_sample, bytes_per_sample);
|
||||
}
|
||||
}
|
||||
|
||||
*bytes_read = frames_read * out_frame_bytes;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t write(Device* device, const void* buffer, size_t size, size_t* bytes_written, TickType_t timeout) {
|
||||
(void) device;
|
||||
(void) buffer;
|
||||
(void) size;
|
||||
(void) bytes_written;
|
||||
(void) timeout;
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
error_t set_volume(Device* device, AudioCodecDirection direction, float volume_percent) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
// Map 0..100% linearly onto the gain range.
|
||||
float db = (volume_percent / 100.0f) * MAX_INPUT_GAIN_DB;
|
||||
return (esp_codec_dev_set_in_gain(data->codec_device, db) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_volume(Device* device, AudioCodecDirection direction, float* volume_percent) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
float db = 0.0f;
|
||||
if (esp_codec_dev_get_in_gain(data->codec_device, &db) != ESP_CODEC_DEV_OK) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
*volume_percent = (db / MAX_INPUT_GAIN_DB) * 100.0f;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t set_mute(Device* device, AudioCodecDirection direction, bool muted) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return (esp_codec_dev_set_in_mute(data->codec_device, muted) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_mute(Device* device, AudioCodecDirection direction, bool* muted) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return (esp_codec_dev_get_in_mute(data->codec_device, muted) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_native_channels(Device* device, AudioCodecDirection direction, uint8_t* channels) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
*channels = data->mic_count;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t get_native_sample_rate(Device* device, AudioCodecDirection direction, uint32_t* rate_hz) {
|
||||
(void) device;
|
||||
if (direction != AUDIO_CODEC_DIR_INPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
*rate_hz = NATIVE_SAMPLE_RATE;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t get_capabilities(Device* device, AudioCodecDirection* supported_directions) {
|
||||
(void) device;
|
||||
*supported_directions = AUDIO_CODEC_DIR_INPUT;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t get_input_gain_multiplier(Device* device, float* gain) {
|
||||
auto* data = GET_DATA(device);
|
||||
*gain = data->input_gain;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static const struct AudioCodecApi API = {
|
||||
.open = open,
|
||||
.close = close,
|
||||
.read = read,
|
||||
.write = write,
|
||||
.set_volume = set_volume,
|
||||
.get_volume = get_volume,
|
||||
.set_mute = set_mute,
|
||||
.get_mute = get_mute,
|
||||
.get_native_sample_rate = get_native_sample_rate,
|
||||
.get_native_channels = get_native_channels,
|
||||
.get_capabilities = get_capabilities,
|
||||
.get_input_gain_multiplier = get_input_gain_multiplier,
|
||||
};
|
||||
|
||||
// endregion
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
error_t start_device(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
// devicetree's int property type has no min/max constraint support, so a bogus
|
||||
// negative literal would otherwise silently wrap to 65535 when narrowed to uint16_t.
|
||||
// Bound it here instead -- 2000 (20x) is already an extreme upper bound for
|
||||
// compensating a quiet mic capsule on top of the ES7210's own hardware gain.
|
||||
if (config->input_gain_percent > 2000) {
|
||||
LOG_E(TAG, "Invalid input_gain_percent %u (must be 0..2000)", config->input_gain_percent);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// Same rationale as input_gain_percent: devicetree ints aren't bit-constrained, so
|
||||
// reject any bit outside the four supported mic slots before it inflates mic_count
|
||||
// beyond the fixed 4-slot TDM frame that read() demuxes against.
|
||||
constexpr uint8_t supported_mic_mask = (uint8_t) (ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ES7210_SEL_MIC3 | ES7210_SEL_MIC4);
|
||||
if ((config->mic_selected_mask & ~supported_mic_mask) != 0) {
|
||||
LOG_E(TAG, "Invalid mic_selected_mask 0x%02X (must be subset of 0x%02X)", config->mic_selected_mask, supported_mic_mask);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* i2c_controller = device_get_parent(device);
|
||||
if (i2c_controller == nullptr || device_get_type(i2c_controller) != &I2C_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "Parent is not an I2C controller");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* i2s_controller = config->i2s_device;
|
||||
if (i2s_controller == nullptr || device_get_type(i2s_controller) != &I2S_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "I2S controller device is not valid");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* data = new Es7210Data();
|
||||
|
||||
uint8_t mic_mask = (config->mic_selected_mask != 0)
|
||||
? config->mic_selected_mask
|
||||
: (uint8_t) (ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ES7210_SEL_MIC3 | ES7210_SEL_MIC4);
|
||||
data->mic_mask = mic_mask;
|
||||
data->mic_count = (uint8_t) __builtin_popcount(mic_mask);
|
||||
// Mirrors the vendor driver's own TDM threshold (es7210_is_tdm_mode(): TDM only
|
||||
// engages when mic_num >= 3) -- below that it runs a plain N-channel I2S frame, not
|
||||
// a fixed 4-slot TDM frame. Forcing a 4-channel open (see open()'s hw_channels
|
||||
// comment) against a non-TDM 2-mic config mismatches the I2S receive layout the
|
||||
// codec is actually producing, corrupting every channel past the first.
|
||||
data->tdm_slot_count = (data->mic_count >= 3) ? 4 : data->mic_count;
|
||||
data->input_gain = (float) config->input_gain_percent / 100.0f;
|
||||
|
||||
data->ctrl_if = audio_codec_adapter_new_i2c_ctrl(i2c_controller, config->address);
|
||||
data->data_if = audio_codec_adapter_new_i2s_data(i2s_controller);
|
||||
if (data->ctrl_if == nullptr || data->data_if == nullptr) {
|
||||
LOG_E(TAG, "Failed to create adapters");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (data->ctrl_if->open(data->ctrl_if, nullptr, 0) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open control interface");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (data->data_if->open(data->data_if, nullptr, 0) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open data interface");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
{
|
||||
es7210_codec_cfg_t codec_config = {};
|
||||
codec_config.ctrl_if = data->ctrl_if;
|
||||
codec_config.master_mode = false;
|
||||
codec_config.mic_selected = mic_mask;
|
||||
codec_config.mclk_src = ES7210_MCLK_FROM_PAD;
|
||||
codec_config.mclk_div = 0;
|
||||
|
||||
data->codec_if = es7210_codec_new(&codec_config);
|
||||
if (data->codec_if == nullptr) {
|
||||
LOG_E(TAG, "Failed to create ES7210 codec interface");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
esp_codec_dev_cfg_t dev_config = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_IN,
|
||||
.codec_if = data->codec_if,
|
||||
.data_if = data->data_if,
|
||||
};
|
||||
|
||||
data->codec_device = esp_codec_dev_new(&dev_config);
|
||||
if (data->codec_device == nullptr) {
|
||||
LOG_E(TAG, "Failed to create codec device");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
device_set_driver_data(device, data);
|
||||
return ERROR_NONE;
|
||||
|
||||
cleanup:
|
||||
// Mirrors stop_device's teardown order -- delete_*_if() routines close their interface
|
||||
// first, so we don't need separate ->close() calls here.
|
||||
if (data->codec_device != nullptr) {
|
||||
esp_codec_dev_delete(data->codec_device);
|
||||
}
|
||||
if (data->codec_if != nullptr) {
|
||||
audio_codec_delete_codec_if(data->codec_if);
|
||||
}
|
||||
if (data->data_if != nullptr) {
|
||||
audio_codec_delete_data_if(data->data_if);
|
||||
}
|
||||
if (data->ctrl_if != nullptr) {
|
||||
audio_codec_delete_ctrl_if(data->ctrl_if);
|
||||
}
|
||||
delete data;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t stop_device(Device* device) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data == nullptr) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
if (data->is_open) {
|
||||
esp_codec_dev_close(data->codec_device);
|
||||
}
|
||||
|
||||
if (data->codec_device != nullptr) {
|
||||
esp_codec_dev_delete(data->codec_device);
|
||||
}
|
||||
if (data->codec_if != nullptr) {
|
||||
audio_codec_delete_codec_if(data->codec_if);
|
||||
}
|
||||
if (data->data_if != nullptr) {
|
||||
audio_codec_delete_data_if(data->data_if);
|
||||
}
|
||||
if (data->ctrl_if != nullptr) {
|
||||
audio_codec_delete_ctrl_if(data->ctrl_if);
|
||||
}
|
||||
|
||||
device_set_driver_data(device, nullptr);
|
||||
delete data;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
Driver es7210_driver = {
|
||||
.name = "es7210",
|
||||
.compatible = (const char*[]) { "everest,es7210", nullptr },
|
||||
.start_device = start_device,
|
||||
.stop_device = stop_device,
|
||||
.api = &API,
|
||||
.device_type = &AUDIO_CODEC_TYPE,
|
||||
.owner = nullptr,
|
||||
.internal = nullptr,
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver es7210_driver;
|
||||
|
||||
static error_t start() {
|
||||
check(driver_construct_add(&es7210_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
check(driver_remove_destruct(&es7210_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
extern const ModuleSymbol es7210_module_symbols[];
|
||||
|
||||
Module es7210_module = {
|
||||
.name = "es7210",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = es7210_module_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/module.h>
|
||||
|
||||
const struct ModuleSymbol es7210_module_symbols[] = {
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
Reference in New Issue
Block a user