37 LOGE(
"Invalid config");
42 if (frame_size <= 0) {
43 LOGE(
"Invalid frame size");
48 if (!setupDac())
return false;
52 if (!setupAdc())
return false;
71 size_t write(
const uint8_t* src,
size_t size_bytes)
override {
74 size_t bytes = (size_bytes / frame_size) * frame_size;
75 if (bytes == 0)
return 0;
84 size_t readBytes(uint8_t* dst,
size_t size_bytes)
override {
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++) {
165 if (!device_is_ready(config.dac[ch].dev)) {
166 LOGE(
"DAC not ready");
170 int rc = dac_channel_setup_dt(&config.dac[ch]);
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++) {
203 if (!device_is_ready(config.adc[ch].dev)) {
204 LOGE(
"ADC not ready");
208 int rc = adc_channel_setup_dt(&config.adc[ch]);
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);
270 uint32_t value = toDac(sample);
272 dac_write_value_dt(&config.dac[ch], value);
278 void writeSilence() {
279#if IS_ENABLED(CONFIG_DAC)
281 uint32_t mid = (1u << dacBits()) / 2u;
283 for (
int ch = 0; ch < configured_channels; ch++) {
284 dac_write_value_dt(&config.dac[ch], mid);
295#if IS_ENABLED(CONFIG_ADC)
301 for (
int ch = 0; ch < configured_channels; ch++) {
305 struct adc_sequence seq = {
307 .buffer_size =
sizeof(io),
308 .resolution = config.adc[ch].resolution,
310 adc_read_dt(&config.adc[ch], &seq);
311 int32_t sample = adcToSigned(io);
313 writeSample(frame, ch, sample);
325 int32_t extractSample(
const uint8_t* frame,
int ch) {
327 const uint8_t* p = frame + ch * bytes;
330 case 8:
return (int8_t)p[0];
331 case 16:
return *(int16_t*)p;
332 case 24:
return (p[0] | (p[1] << 8) | (p[2] << 16));
333 default:
return *(int32_t*)p;
337 void writeSample(uint8_t* frame,
int ch, int32_t sample) {
339 uint8_t* p = frame + ch * bytes;
343 p[0] = (uint8_t)(sample >> 8);
346 *(int16_t*)p = (int16_t)sample;
349 p[0] = sample & 0xFF;
350 p[1] = (sample >> 8) & 0xFF;
351 p[2] = (sample >> 16) & 0xFF;
354 *(int32_t*)p = sample;
359 uint32_t toDac(int32_t sample) {
360 int64_t min_in = -(1LL << 15);
361 int64_t max_in = (1LL << 15) - 1;
363 uint32_t max_out = (1u << dacBits()) - 1;
365 return (uint32_t)((sample - min_in) * max_out / (max_in - min_in));
368 int32_t adcToSigned(int16_t raw) {
369 return (int32_t)raw - 2048;
372 int dacBits()
const {