Codec working in bypass mode. I2S not working yet.

This commit is contained in:
Sebastian
2025-05-29 03:19:54 +02:00
parent cd1b0934aa
commit 549fa21d1a
2 changed files with 126 additions and 63 deletions

View File

@ -23,6 +23,8 @@ PWMAudio ui_snd(8);
enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
int lut_ring_cw[48] = {39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,50,49,48,47,46,45,44,43,42,41,40};
int lut_ring_ccw[48] = {40,41,42,43,44,45,46,47,48,49,50,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
int lut_matrix[13] = {56,55,57,58,59,62,61,60,63,64,65,67,66};
CRGB ui_leds[74];
CRGB edge_leds[11];
@ -188,8 +190,8 @@ void loop() {
position = ENC.getCumulativePosition();
sd_card_detected = !digitalRead(21);
if(sd_card_detected) edge_leds[8] = CRGB(0,25,0);
if(!sd_card_detected) edge_leds[8] = CRGB(25,0,0);
if(sd_card_detected) edge_leds[8] = CRGB(0,10,0);
if(!sd_card_detected) edge_leds[8] = CRGB(10,0,0);
// EDGE LEDs
for (int i = 0; i < 8; i++) {
@ -227,11 +229,6 @@ void loop() {
click = true;
}
// Flash led ring
// for (int i = 0; i < 48; i++) {
// ui_leds[i + 3] = CRGB(0, 15, 0);
// }
// Flash encoder leds
ui_leds[0] = CRGB(0, 100, 50);
ui_leds[1] = CRGB(0, 100, 50);
@ -246,25 +243,23 @@ void loop() {
flag = false;
}
// empty LED matrix
for (int i = 0; i < 13; i++) {
ui_leds[i + 3 + 48 + 4] = CRGB(0, 0, 0);
ui_leds[lut_matrix[i]] = CRGB(0, 0, 0);
}
// set active LED matrix LED
ui_leds[active + 3 + 48 + 4] = CRGB(255, 255, 255);
ui_leds[lut_matrix[active]] = CRGB(100, 100, 100);
if(position < 0) position += 4096;
active_led_ring = (position / 32) % 48;
// set active LED ring LED
for(int i = 0; i < active_led_ring; i++){
ui_leds[lut_ring_cw[i]] = CRGB(50, 0, 25);
ui_leds[lut_ring_ccw[i]] = CRGB(50, 0, 25);
}
FastLED.show();
delay(1); // wait 1ms
}

View File

@ -10,59 +10,126 @@ I2S i2s(INPUT_PULLUP);
#define SIZE 256
int16_t buffer[SIZE];
void cw(char first, char second){
int16_t volume = 0;
size_t count;
void codec_transmit() {
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);
size_t index = 0;
volume = 0;
while (index < count) {
volume += buffer[index];
buffer[index] = 0;
index++;
}
}
void cw(unsigned char first, unsigned char second){
Wire1.beginTransmission(0x18);
Wire1.write(first);
Wire1.write(second);
Wire1.endTransmission();
int result = Wire1.endTransmission();
Serial.print(0x18, HEX);
Serial.print(" ");
Serial.print(first, HEX);
Serial.print(" ");
Serial.print(second, HEX);
Serial.print(" : ");
Serial.println(result);
delay(5);
}
void cr(unsigned char first, size_t len){
Wire1.beginTransmission(0x18);
Wire1.write(first); // set register for read
Wire1.endTransmission(false); // false to not release the line
Wire1.requestFrom(0x18, len, true); // request bytes from register XY
byte buff[len];
Wire1.readBytes(buff, len);
for (int i = 0; i < len; i++) {
Serial.println(buff[i], HEX);
}
Serial.println();
}
void setup() {
i2s.setSysClk(48000);
Serial.begin(115200);
delay(1000);
delay(2000);
Wire1.setSDA(2);
Wire1.setSCL(3);
Wire1.begin();
delay(1000);
delay(100);
pinMode(19, OUTPUT); // MCLK enable
digitalWrite(19, LOW); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset
digitalWrite(20, HIGH);
// GENERAL
cw(0x00, 0x00);
cw(0x01, 0x01);
cw(0x1b, 0x10); // select I2S
cw(0x00, 0x00); // select page 0
cw(0x01, 0x01); // soft reset
cw(0x1b, 0x30); // select I2S with 32 bit word length
cw(0x1d, 0b00000000); // disable loopback
// POWER and CM
cw(0x00, 0x01); // select page 1
cw(0x01, 0x08); // disable weak (crude) AVdd connection to DVdd
cw(0x02, 0x01); // enable internal AVdd LDO and enable analog blocks
cw(0x09, 0b00001100); // power up LOL, LOR, power down MAL, MAR, HPL, HPR
cw(0x0a, 0b00000000); // set full chip CM to 0.9V
cw(0x47, 0b00110010); // analog input quick charge time 6.4ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x34, 0b11000000); // route IN1L to LEFT_P with 20k input impedance
cw(0x36, 0b11000000); // route CM to LEFT_M with 20k input impedance
cw(0x37, 0b11000000); // route IN1R to RIGHT_P with 20k input impedance
cw(0x39, 0b11000000); // route CM to RIGHT_M with 20k input impedance
cw(0x3a, 0b00111100); // connect IN2, IN3 weakly to CM
// GAIN
cw(0x00, 0x01); // select page 1
cw(0x3b, 0b00000000); // unmute left MICPGA, set gain to > 0db
cw(0x3c, 0b00000000); // unmute right MICPGA, set gain to > 0db
// VOLUME
cw(0x00, 0x01); // select page 1
cw(0x16, 0b01110101); // MUTE IN1L to HPL
cw(0x17, 0b01110101); // MUTE IN1R to HPR
// ADC
cw(0x00, 0x00); // select page 0
//cw(0x01, 0x01); // soft reset
cw(0x12, 0x87); // NADC 7
cw(0x12, 0x81); // NADC 1
cw(0x13, 0x82); // MADC 2
cw(0x14, 0x80); // OSR ADC 128
cw(0x3d, 0x01); // ADC PRB_R1
cw(0x00, 0x01); // select page 1
cw(0x01, 0x08); // disable crude AVdd
cw(0x02, 0x01); // enable internal AVdd LDO
cw(0x0a, 0x0B); // set input CM to 0.9V and LO to 1.65V
cw(0x3d, 0x00); // ADC PTM_R4
cw(0x34, 0x80); // route IN1L to LEFT_P with 20k input impedance
cw(0x36, 0x80); // route CM to LEFT_M with 20k input impedance
cw(0x37, 0x80); // route IN1R to RIGHT_P with 20k input impedance
cw(0x39, 0x80); // route CM to RIGHT_M with 20k input impedance
cw(0x3b, 0x0c); // unmute left MICPGA
cw(0x3c, 0x0c); // unmute right MICPGA
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x00, 0x00); // select page 0
cw(0x51, 0xc0); // power up ADC
cw(0x51, 0x00); // unmute ADC digital volume control
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x53, 0b00000000); // LEFT
cw(0x54, 0b00000000); // RIGHT
// DAC
cw(0x00, 0x00); // select page 0
//cw(0x01, 0x01); // software reset
cw(0x0b, 0x82); // NDAC 2
cw(0x0c, 0x87); // MDAC 7
cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0x10); // world length 20bits PTM_P4 (highest performance)
@ -74,52 +141,53 @@ void setup() {
cw(0x7b, 0x01); // set REF charging time to 40ms
//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(0x0e, 0x08); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0x08); // right DAC reconstruction filter routed to LOR
// ROUTING
cw(0x00, 0x01);
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0x00); // DAC PTM_P3/4
cw(0x04, 0x00); // DAC PTM_P3/4
cw(0x12, 0x00); // LOL gain 0dB
cw(0x13, 0x00); // LOR gain 0dB
delay(1000);
// LO GAIN
cw(0x00, 0x01);
cw(0x12, 0b00000010); // LOL gain 0dB
cw(0x13, 0b00000010); // LOR gain 0dB
cw(0x00, 0x00); // select page 0
cw(0x3f, 0xd6); // 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
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x41, 0b00000000); // LEFT
cw(0x42, 0b00000000); // RIGHT
pinMode(19, OUTPUT); // MCLK enable
pinMode(20, OUTPUT); // CODEC reset
i2s.setSysClk(48000);
//i2s.onTransmit(codec_transmit);
//i2s.onReceive(codec_receive);
i2s.setDOUT(14);
i2s.setDIN(15);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18);
i2s.setMCLKmult(256); // 12.288.000Hz
i2s.swapClocks();
i2s.setBitsPerSample(16);
i2s.setFrequency(48000);
i2s.setMCLKmult(128); // 6144000Hz 6.144MHz
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
digitalWrite(19, HIGH); // enable MCLK
digitalWrite(20, HIGH);
i2s.begin();
size_t count, index;
while (1) {
count = i2s.read((uint8_t *)&buffer, SIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
index = 0;
while (index < count) {
// Reduce volume by half
buffer[index++] >>= 1; // right
buffer[index++] >>= 1; // left
}
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
if(!i2s.begin(48000)){
Serial.println("I2S error!");
while(100);
}
}
void loop() {
/* Nothing here */
//Serial.println(volume);
//delay(10);
int16_t l, r;
i2s.read16(&l, &r);
i2s.write16(l, r);
}