#include <Wire.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <utility/imxrt_hw.h>
class AK4619 {
public:
TwoWire* wire;
uint8_t addr;
void Init(uint8_t i2cAddress=0x10) {
Wire.begin();
addr = i2cAddress;
}
uint8_t writeReg(uint8_t deviceReg, uint8_t regVal) {
Wire.beginTransmission(addr);
Wire.write(deviceReg);
Wire.write(regVal);
return(Wire.endTransmission(true));
}
uint8_t readReg(int8_t deviceReg, uint8_t * regVal) {
Wire.beginTransmission(addr);
Wire.write(deviceReg);
Wire.endTransmission(true);
uint8_t numbytes = 0;
numbytes = Wire.requestFrom(addr, (uint8_t)1, (uint8_t)false);
if((bool)numbytes){
Wire.readBytes(regVal, numbytes);
}
return(Wire.endTransmission(true)); //Send STOP
}
};
void setI2SFreq(int freq) {
// PLL between 27*24 = 648MHz und 54*24=1296MHz
int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
int n2 = 1 + (24000000 * 27) / (freq * 256 * n1);
double C = ((double)freq * 256 * n1 * n2) / 24000000;
int c0 = C;
int c2 = 10000;
int c1 = C * c2 - (c0 * c2);
set_audioClock(c0, c1, c2, true);
CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
| CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
| CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
//Serial.printf("SetI2SFreq(%d)\n",freq);
}
// GUItool: begin automatically generated code
AudioInputTDM tdm1; //xy=326,427
AudioOutputTDM tdm2; //xy=843,411
AudioSynthWaveform waveform1; //xy=347,179
AudioSynthWaveform waveform2; //xy=347,179
AudioSynthWaveform waveform3; //xy=347,179
AudioSynthWaveformDc dc1; //xy=575,252
AudioAmplifier amp1; //xy=540,355
AudioConnection patchCord5(waveform1, 0, tdm2, 0);
AudioConnection patchCord6(waveform2, 0, tdm2, 2);
AudioConnection patchCord7(waveform3, 0, tdm2, 4);
AudioConnection patchCord8(waveform1, 0, tdm2, 6);
// -------------------------------------------------------
// UNCOMMENT ME TO CAUSE ISSUES WITH DAC1R and DAC2L!!!! -
// -------------------------------------------------------
// AudioConnection patchCord1(tdm1, 0, amp1, 0);
// AudioConnection patchCord2(amp1, 0, tdm2, 8);
AK4619 codec;
uint8_t regval = 0;
uint8_t error = 0;
int count = 0;
void setup() {
Serial.begin(9600);
Serial.println("hi");
AudioMemory(20);
setI2SFreq(20000);
Serial.println("begin");
// AK4619VN startup sequence
pinMode(41, OUTPUT);
digitalWrite(41, LOW);
delay(200);
digitalWrite(41, HIGH);
delay(200);
codec.Init();
delay(200);
//if(codec.writeReg(0x00, 0b00110111)) Serial.println("Error."); // set last but repeated here for address consistency
if(codec.writeReg(0x01, 0b11111110)) Serial.println("Error."); // set AK4619 regs
if(codec.writeReg(0x02, 0b00011100)) Serial.println("Error.");
if(codec.writeReg(0x03, 0b00000011)) Serial.println("Error.");
if(codec.writeReg(0x04, 0b00100010)) Serial.println("Error.");
if(codec.writeReg(0x05, 0b00100010)) Serial.println("Error.");
if(codec.writeReg(0x06, 0b00110000)) Serial.println("Error.");
if(codec.writeReg(0x07, 0b00110000)) Serial.println("Error.");
if(codec.writeReg(0x08, 0b00110000)) Serial.println("Error.");
if(codec.writeReg(0x09, 0b00110000)) Serial.println("Error.");
if(codec.writeReg(0x0A, 0b00000000)) Serial.println("Error.");
if(codec.writeReg(0x0B, 0b01010101)) Serial.println("Error.");
//if(codec.writeReg(0x0C, 0b00000000)) Serial.println("Error."); //reserved
if(codec.writeReg(0x0D, 0b00000000)) Serial.println("Error.");
if(codec.writeReg(0x0E, 0b00011000)) Serial.println("Error.");
if(codec.writeReg(0x0F, 0b00011000)) Serial.println("Error.");
if(codec.writeReg(0x10, 0b00011000)) Serial.println("Error.");
if(codec.writeReg(0x11, 0b00011000)) Serial.println("Error.");
if(codec.writeReg(0x12, 0b00000100)) Serial.println("Error.");
if(codec.writeReg(0x13, 0b00000101)) Serial.println("Error.");
if(codec.writeReg(0x14, 0b00001010)) Serial.println("Error.");
if(codec.writeReg(0x00, 0b00110111)) Serial.println("Error."); // turn AK4619 on
// read back AK4619 register settings
for(int i=0;i<=0x14;i++) {
if(codec.readReg(i, ®val)) {
Serial.println("READ ERROR!");
}
Serial.print(i, HEX);
Serial.print(": \t");
for (int8_t i = 7; i >= 0; i--) {
uint8_t bit = (regval >> i) & 1;
Serial.print(bit);
if (i % 4 == 0)
Serial.print(" ");
}
Serial.print("\t");
Serial.print(regval, HEX);
Serial.println();
}
Serial.println("Setup done");
// set up waveforms
waveform1.begin(WAVEFORM_SINE);
waveform1.frequency(30);
waveform1.amplitude(1.0);
waveform2.begin(WAVEFORM_SAWTOOTH);
waveform2.frequency(30);
waveform2.amplitude(1.0);
waveform3.begin(WAVEFORM_TRIANGLE);
waveform3.frequency(30);
waveform3.amplitude(1.0);
amp1.gain(0.0);
}
void loop() {
// sample-and-hold DC test signal
//dc1.amplitude(random(0,100)/50.0 - 1.0);
dc1.amplitude(-1.0/2.0);
// print a counter so we know we're running
Serial.println(count++);
delay(100);
}