I2S loopback working!

This commit is contained in:
Sebastian
2025-05-29 18:06:39 +02:00
parent 549fa21d1a
commit 2ffd1997b9

View File

@ -39,7 +39,12 @@ void cw(unsigned char first, unsigned char second){
Serial.print(" "); Serial.print(" ");
Serial.print(second, HEX); Serial.print(second, HEX);
Serial.print(" : "); Serial.print(" : ");
Serial.println(result); if(result == 0) {
Serial.println("OK");
} else {
Serial.print("ERROR: ");
Serial.println(result);
}
delay(5); delay(5);
} }
@ -50,13 +55,16 @@ void cr(unsigned char first, size_t len){
Wire1.requestFrom(0x18, len, true); // request bytes from register XY Wire1.requestFrom(0x18, len, true); // request bytes from register XY
Serial.print(first, HEX);
Serial.print(" ");
byte buff[len]; byte buff[len];
Wire1.readBytes(buff, len); Wire1.readBytes(buff, len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Serial.println(buff[i], HEX); Serial.print(buff[i], HEX);
Serial.print(" ");
Serial.println(buff[i], BIN);
} }
Serial.println();
} }
void setup() { void setup() {
@ -73,7 +81,7 @@ void setup() {
delay(100); delay(100);
pinMode(19, OUTPUT); // MCLK enable pinMode(19, OUTPUT); // MCLK enable
digitalWrite(19, LOW); // enable MCLK digitalWrite(19, HIGH); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset pinMode(20, OUTPUT); // CODEC reset
digitalWrite(20, HIGH); digitalWrite(20, HIGH);
@ -117,57 +125,69 @@ void setup() {
cw(0x13, 0x82); // MADC 2 cw(0x13, 0x82); // MADC 2
cw(0x14, 0x80); // OSR ADC 128 cw(0x14, 0x80); // OSR ADC 128
cw(0x3d, 0x01); // ADC PRB_R1 cw(0x3d, 0x01); // ADC PRB_R1
cw(0x3d, 0x00); // ADC PTM_R4
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB cw(0x00, 0x01); // select page 1
cw(0x53, 0b00000000); // LEFT cw(0x3d, 0b01100100); // ADC PTM_R3
cw(0x54, 0b00000000); // RIGHT
// DAC // DAC
cw(0x00, 0x00); // select page 0 cw(0x00, 0x00); // select page 0
//cw(0x01, 0x01); // software reset
cw(0x0b, 0x81); // NDAC 1 cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2 cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128 cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128 cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0x10); // world length 20bits PTM_P4 (highest performance) cw(0x1b, 0b00000000); // word length 16bits
cw(0x3c, 0x08); // PRB_P8 cw(0x3c, 0x08); // PRB_P8
cw(0x00, 0x01); // select page 1 cw(0x00, 0x01); // select page 1
//cw(0x01, 0x08); // disable internal crude avdd
//cw(0x02, 0x01); // enable AVdd LDO
cw(0x7b, 0x01); // set REF charging time to 40ms cw(0x7b, 0x01); // set REF charging time to 40ms
//cw(0x14, 0x25); // set HP soft stepping for anti pop //cw(0x14, 0x25); // set HP soft stepping for anti pop
//cw(0x0a, 0x0B); // set input CM to 0.9V and LO to 1.65V //cw(0x0a, 0x0B); // set input CM to 0.9V and LO to 1.65V
// ROUTING // ROUTING
cw(0x00, 0x01); cw(0x00, 0x01); // select page 1
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0x00); // DAC PTM_P3/4 cw(0x03, 0b00000100); // DAC PTM_P3/4
cw(0x04, 0x00); // DAC PTM_P3/4 cw(0x04, 0b00000100); // DAC PTM_P3/4
// LO GAIN // LO GAIN
cw(0x00, 0x01); cw(0x00, 0x01);
cw(0x12, 0b00000010); // LOL gain 0dB cw(0x12, 0b00000010); // LOL gain 0dB
cw(0x13, 0b00000010); // LOR gain 0dB cw(0x13, 0b00000010); // LOR gain 0dB
// POWER UP
// ADC
cw(0x00, 0x00); // select page 0 cw(0x00, 0x00); // select page 0
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume cw(0x40, 0x00); // unmute DAC digital volume
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB // DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x00, 0x00); // select page 0
cw(0x41, 0b00000000); // LEFT cw(0x41, 0b00000000); // LEFT
cw(0x42, 0b00000000); // RIGHT cw(0x42, 0b00000000); // RIGHT
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x00, 0x00); // select page 0
cw(0x53, 0b00000000); // LEFT
cw(0x54, 0b00000000); // RIGHT
// STATUS FLAGS
Serial.println("CODEC STATUS");
cw(0x00, 0x00); // select page 0
Serial.println("ADC Flags");
cr(0x24, 1);
Serial.println("DAC Flags");
cr(0x25, 1);
Serial.println("P0_42 - Sticky Flags");
cr(0x2A, 1);
//i2s.onTransmit(codec_transmit); //i2s.onTransmit(codec_transmit);
//i2s.onReceive(codec_receive); //i2s.onReceive(codec_receive);
i2s.setDOUT(14); i2s.setDOUT(15);
i2s.setDIN(15); i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1 i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18); i2s.setMCLK(18);
i2s.setMCLKmult(256); // 12.288.000Hz i2s.setMCLKmult(256); // 12.288.000Hz
@ -175,7 +195,7 @@ void setup() {
i2s.swapClocks(); i2s.swapClocks();
i2s.setBitsPerSample(16); i2s.setBitsPerSample(16);
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t)); //i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
if(!i2s.begin(48000)){ if(!i2s.begin(48000)){
Serial.println("I2S error!"); Serial.println("I2S error!");
@ -183,11 +203,39 @@ void setup() {
} }
} }
int32_t last = 0;
int frame = 0;
void loop() { void loop() {
//Serial.println(volume);
//delay(10);
int16_t l, r; int16_t l, r;
i2s.read16(&l, &r); i2s.read16(&l, &r);
// float sine_pos = (2.0f * M_PI * 1000.0f * (float)frame) / (float)48000;
// l = (int16_t)(sin(sine_pos) * 8192.0f);
// r = l;
i2s.write16(l, r); i2s.write16(l, r);
// volume += l + r;
// if(millis() - last > 1000){
// Serial.println(volume);
// volume = 0;
// last = millis();
// // STATUS FLAGS
// Serial.println("CODEC STATUS");
// cw(0x00, 0x00); // select page 0
// Serial.println("ADC Flags");
// cr(0x24, 1);
// Serial.println("DAC Flags");
// cr(0x25, 1);
// Serial.println("P0_42 - Sticky Flags");
// cr(0x2A, 1);
// Serial.println("P0_43 - Interrupt Flags");
// cr(0x2B, 1);
// Serial.println("P0_44 - Sticky Flags");
// cr(0x2C, 1);
// }
frame++;
if(frame == 48000) frame = 0;
} }