Files
soundcube-firmware/soundcube-i2s-test/soundcube-i2s-test.ino

91 lines
1.8 KiB
Arduino
Raw Permalink Normal View History

2025-05-27 13:13:29 +02:00
/*
I2S bi-directional input and output buffered loopback example
Released to the Public Domain by Cooper Dalrymple
*/
#include <Wire.h>
#include <I2S.h>
2025-06-01 02:33:01 +02:00
#include "codec.h"
2025-05-27 13:13:29 +02:00
I2S i2s(INPUT_PULLUP);
TLV320AIC3204 codec;
2025-06-01 02:33:01 +02:00
2025-06-01 01:09:35 +02:00
#define SIZE 16
#define ECHO 192000
2025-05-27 13:13:29 +02:00
int16_t buffer[SIZE];
2025-06-01 01:09:35 +02:00
int16_t buffer2[ECHO];
2025-05-27 13:13:29 +02:00
int16_t volume = 0;
size_t count;
2025-06-01 01:09:35 +02:00
size_t tape_write = 0;
void codec_transmit() {
for(int i = 0; i < count; i++){
2025-06-01 01:09:35 +02:00
buffer2[tape_write] = buffer[i];
tape_write++;
if(tape_write == ECHO) tape_write = 0;
int tape_read = tape_write + 1;
if(tape_read < 0) tape_read += ECHO;
buffer[i] += buffer2[tape_read % ECHO];
}
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
}
void codec_receive(){
count = i2s.read((uint8_t *)&buffer, SIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
2025-06-01 01:09:35 +02:00
for(int i = 0; i < count; i++){
buffer[i] *= -1;
}
}
2025-05-27 13:13:29 +02:00
void setup() {
i2s.setSysClk(48000);
2025-05-27 13:13:29 +02:00
Serial.begin(115200);
delay(2000);
2025-05-27 13:13:29 +02:00
Wire1.setSDA(2);
Wire1.setSCL(3);
Wire1.begin();
delay(100);
pinMode(19, OUTPUT); // MCLK enable
2025-05-29 18:06:39 +02:00
digitalWrite(19, HIGH); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset
digitalWrite(20, HIGH);
2025-06-01 02:33:01 +02:00
codec.begin(&Wire1);
i2s.onTransmit(codec_transmit);
i2s.onReceive(codec_receive);
2025-05-27 13:13:29 +02:00
2025-05-29 18:06:39 +02:00
i2s.setDOUT(15);
i2s.setDIN(14);
2025-05-27 13:13:29 +02:00
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18);
i2s.setMCLKmult(256); // 12.288.000Hz
2025-05-27 13:13:29 +02:00
i2s.swapClocks();
i2s.setBitsPerSample(16);
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
2025-05-27 13:13:29 +02:00
if(!i2s.begin(48000)){
Serial.println("I2S error!");
while(100);
2025-05-27 13:13:29 +02:00
}
2025-05-27 13:13:29 +02:00
}
2025-06-01 01:09:35 +02:00
2025-05-29 18:06:39 +02:00
int32_t last = 0;
int frame = 0;
2025-06-01 01:09:35 +02:00
2025-05-27 13:13:29 +02:00
void loop() {
2025-05-29 18:06:39 +02:00
frame++;
if(frame == 48000) frame = 0;
2025-05-27 13:13:29 +02:00
}