Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

152 rindas
6.1KB

  1. /*
  2. * HiFi Audio Codec Module support library for Teensy 3.x
  3. *
  4. * Copyright 2015, Michele Perla
  5. *
  6. */
  7. #include "control_ak4558.h"
  8. #include "Wire.h"
  9. void AudioControlAK4558::initConfig(void)
  10. {
  11. // puts all default registers values inside an array
  12. // this allows us to modify registers locally using annotation like follows:
  13. //
  14. // registers[AK4558_CTRL_1] &= ~AK4558_DIF2;
  15. // registers[AK4558_CTRL_1] |= AK4558_DIF1 | AK4558_DIF0;
  16. //
  17. // after manipulation, we can write the entire register value on the CODEC
  18. unsigned int n = 0;
  19. Wire.requestFrom(AK4558_I2C_ADDR,10);
  20. while(Wire.available()) {
  21. Serial.print("Register ");
  22. Serial.print(n);
  23. Serial.print(" = ");
  24. registers[n++] = Wire.read();
  25. Serial.println(registers[n-1], BIN);
  26. }
  27. }
  28. void AudioControlAK4558::readConfig(void)
  29. {
  30. // reads registers values
  31. unsigned int n = 0;
  32. char c = 0;
  33. Wire.requestFrom(AK4558_I2C_ADDR, 10);
  34. while(Wire.available()) {
  35. Serial.print("Register ");
  36. Serial.print(n++);
  37. Serial.print(" = ");
  38. c = Wire.read();
  39. Serial.println(c, BIN);
  40. }
  41. }
  42. bool AudioControlAK4558::write(unsigned int reg, unsigned int val)
  43. {
  44. Wire.beginTransmission(AK4558_I2C_ADDR);
  45. Wire.write(reg);
  46. Wire.write(val);
  47. return (Wire.endTransmission(true)==0);
  48. }
  49. bool AudioControlAK4558::enable(void)
  50. {
  51. Serial.println("Starting setup...");
  52. delay(1000);
  53. // Power Up and Reset
  54. // Clock Setup (datasheet page 72)
  55. pinMode(PIN_PDN, OUTPUT);
  56. digitalWrite(0, LOW);
  57. delay(1);
  58. digitalWrite(0, HIGH);
  59. // After Power Up: PDN pin “L” → “H”
  60. // “L” time of 150ns or more is needed to reset the AK4558.
  61. delay(20);
  62. Serial.println("PDN is HIGH");
  63. // Control register settings become available in 10ms (min.) when LDOE pin = “H”
  64. Wire.begin();
  65. initConfig();
  66. // access all registers to store locally their default values
  67. // DIF2-0, DFS1-0 and ACKS bits must be set before MCKI, LRCK and BICK are supplied
  68. // PMPLL = 0 (EXT Slave Mode; disables internal PLL and uses ext. clock) (by DEFAULT)
  69. // ACKS = 0 (Manual Setting Mode; disables automatic clock selection) (by DEFAULT)
  70. // DFS1-0 = 00 (Sampling Speed = Normal Speed Mode) (by DEFAULT)
  71. // TDM1-0 = 00 (Time Division Multiplexing mode OFF) (by DEFAULT)
  72. registers[AK4558_CTRL_1] &= ~AK4558_DIF2;
  73. registers[AK4558_CTRL_1] |= AK4558_DIF1 | AK4558_DIF0;
  74. Serial.print("CTRL_1 set to ");
  75. Serial.println(registers[AK4558_CTRL_1], BIN);
  76. // DIF2-1-0 = 011 ( 16 bit I2S compatible when BICK = 32fs)
  77. registers[AK4558_CTRL_2] &= ~AK4558_MCKS1;
  78. Serial.print("CTRL_2 set to ");
  79. Serial.println(registers[AK4558_CTRL_2], BIN);
  80. // MCKS1-0 = 00 (Master Clock Input Frequency Select, set 256fs for Normal Speed Mode -> 11.2896 MHz)
  81. registers[AK4558_MODE_CTRL] &= ~AK4558_BCKO0;
  82. registers[AK4558_MODE_CTRL] |= AK4558_LOPS; //| AK4558_FS1;
  83. // Set the DAC output to power-save mode: LOPS bit “0” → “1”
  84. Serial.print("MODE_CTRL set to ");
  85. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  86. // BCKO1-0 = 00 (BICK Output Frequency at Master Mode = 32fs = 1.4112 MHz)
  87. Wire.beginTransmission(AK4558_I2C_ADDR);
  88. Wire.write(AK4558_CTRL_1);
  89. Wire.write(registers[AK4558_CTRL_1]);
  90. Wire.write(registers[AK4558_CTRL_2]);
  91. Wire.write(registers[AK4558_MODE_CTRL]);
  92. Wire.endTransmission();
  93. // Write configuration registers in a single write operation (datasheet page 81):
  94. // The AK4558 can perform more than one byte write operation per sequence. After receipt of the third byte
  95. // the AK4558 generates an acknowledge and awaits the next data. The master can transmit more than
  96. // one byte instead of terminating the write cycle after the first data byte is transferred. After receiving each
  97. // data packet the internal address counter is incremented by one, and the next data is automatically taken
  98. // into the next address.
  99. // ADC/DAC Output setup (datasheet pages 74, 75)
  100. // registers[AK4558_PLL_CTRL] |= AK4558_PLL2;
  101. // registers[AK4558_PLL_CTRL] &= ~AK4558_PLL1;
  102. // write(AK4558_I2C_ADDR, registers[AK4558_PLL_CTRL]);
  103. // Serial.print("PLL_CTRL set to ");
  104. // Serial.println(registers[AK4558_PLL_CTRL], BIN);
  105. // delay(10);
  106. // as per table 16, set PLL_CTRL.PLL3-2-1-0 to 0101 for MICK as PLL Reference, 11.2896 MHz
  107. // also, wait 10 ms for PLL lock
  108. // TODO: IS IT NEEDED?
  109. // Set the DAC output to power-save mode: LOPS bit “0” → “1”
  110. // registers[AK4558_MODE_CTRL] |= AK4558_LOPS; //| AK4558_FS1;
  111. // write(AK4558_I2C_ADDR, registers[AK4558_MODE_CTRL]);
  112. // Serial.print("MODE_CTRL set to ");
  113. // Serial.println(registers[AK4558_MODE_CTRL], BIN);
  114. // Set up the sampling frequency (FS3-0 bits). The ADC must be powered-up in consideration of PLL
  115. // lock time. (in this case (ref. table 17): Set clock to mode 5 / 44.100 KHz)
  116. // Set up the audio format (Addr=03H). (in this case: TDM1-0 = 00 (Time Division Multiplexing mode OFF) by default)
  117. // ignore this, leaving default values - ADC: Set up the de-emphasis filter (Addr = 07H).
  118. // ignore this, leaving default values - DAC: Set up the digital filter mode.
  119. // ignore this, leaving default values - Set up the digital output volume (Address = 08H, 09H).
  120. registers[AK4558_PWR_MNGT] |= AK4558_PMDAR | AK4558_PMDAL | AK4558_PMADR | AK4558_PMADL;
  121. write(AK4558_PWR_MNGT, registers[AK4558_PWR_MNGT]);
  122. Serial.print("PWR_MNGT set to ");
  123. Serial.println(registers[AK4558_PWR_MNGT], BIN);
  124. delay(300);
  125. // Power up the ADC: PMADL = PMADR bits = “0” → “1”
  126. // Initialization cycle of the ADC is 5200/fs @Normal mode. The SDTO pin outputs “L” during initialization.
  127. // Power up the DAC: PMDAL = PMDAR bits = “0” → “1”
  128. // Outputs of the LOUT and ROUT pins start rising. Rise time is 300ms (max.) when C = 1μF.
  129. registers[AK4558_MODE_CTRL] &= ~AK4558_LOPS;
  130. write(AK4558_MODE_CTRL, registers[AK4558_MODE_CTRL]);
  131. Serial.print("MODE_CTRL set to ");
  132. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  133. // Release power-save mode of the DAC output: LOPS bit = “1” → “0”
  134. // Set LOPS bit to “0” after the LOUT and ROUT pins output “H”. Sound data will be output from the
  135. // LOUT and ROUT pins after this setting.
  136. Serial.println("Setup ended");
  137. write(AK4558_ROUT_VOL,255); //dummy write to last register to reset address counter to 0
  138. return true;
  139. }