5#if defined(USE_ANALOG) && (defined(IS_ZEPHYR))
10#include <zephyr/kernel.h>
11#include <zephyr/device.h>
12#include <zephyr/drivers/adc.h>
13#include <zephyr/drivers/dac.h>
37 LOGE(
"Invalid config");
42 if (frame_size <= 0) {
43 LOGE(
"Invalid frame size");
48 if (!setupDac())
return false;
52 if (!setupAdc())
return false;
74 size_t bytes = (size_bytes / frame_size) * frame_size;
75 if (bytes == 0)
return 0;
87 size_t bytes = (size_bytes / frame_size) * frame_size;
88 if (bytes == 0)
return 0;
111 int configured_channels = 0;
118 RingBuffer<uint8_t> tx_buffer{0};
130 const struct device* dac_dev =
nullptr;
131 const struct device* adc_dev =
nullptr;
137 bool setupBuffer(RingBuffer<uint8_t>& buffer) {
142 return buffer.address() !=
nullptr;
150#if IS_ENABLED(CONFIG_DAC)
152 if (config.dac.empty()) {
153 LOGE(
"No DAC configured");
157 if (!setupBuffer(tx_buffer)){
162 configured_channels = std::min<int>(config.
channels, config.dac.size());
164 for (
int ch = 0;
ch < configured_channels;
ch++) {
166 LOGE(
"DAC not ready");
172 LOGE(
"DAC setup failed ch=%d rc=%d",
ch,
rc);
188#if IS_ENABLED(CONFIG_ADC)
190 if (config.adc.empty()) {
191 LOGE(
"No ADC configured");
195 if (!setupBuffer(rx_buffer)){
200 configured_channels = std::min<int>(config.
channels, config.adc.size());
202 for (
int ch = 0;
ch < configured_channels;
ch++) {
204 LOGE(
"ADC not ready");
210 LOGE(
"ADC setup failed ch=%d rc=%d",
ch,
rc);
226 timer.setCallbackParameter(
this);
230 static void timerCallback(
void*
arg) {
232 if (!self || !self->active)
return;
234 switch (self->config.rx_tx_mode) {
248 LOGE(
"Undefined rx_tx_mode: %d", (
int)self->config.rx_tx_mode)
257#if IS_ENABLED(CONFIG_DAC)
259 if (tx_buffer.
available() < frame_size) {
268 for (
int ch = 0;
ch < configured_channels;
ch++) {
269 int32_t sample = extractSample(frame,
ch);
278 void writeSilence() {
279#if IS_ENABLED(CONFIG_DAC)
283 for (
int ch = 0;
ch < configured_channels;
ch++) {
295#if IS_ENABLED(CONFIG_ADC)
301 for (
int ch = 0;
ch < configured_channels;
ch++) {
307 .buffer_size =
sizeof(io),
308 .resolution = config.adc[
ch].resolution,
311 int32_t sample = adcToSigned(io);
313 writeSample(frame,
ch, sample);
330 case 8:
return (
int8_t)p[0];
332 case 24:
return (p[0] | (p[1] << 8) | (p[2] << 16));
349 p[0] = sample & 0xFF;
350 p[1] = (sample >> 8) & 0xFF;
351 p[2] = (sample >> 16) & 0xFF;
363 uint32_t max_out = (1u << dacBits()) - 1;
372 int dacBits()
const {
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGE(...)
Definition AudioLoggerIDF.h:30
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26
@ HZ
Definition AudioTypes.h:44