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.

116 rindas
4.9KB

  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::readInitConfig(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]);
  26. }
  27. }
  28. bool AudioControlAK4558::write(unsigned int reg, unsigned int val)
  29. {
  30. Wire.beginTransmission(AK4558_I2C_ADDR);
  31. Wire.write(reg);
  32. Wire.write(val);
  33. return (Wire.endTransmission()==0);
  34. }
  35. bool AudioControlAK4558::enable(void)
  36. {
  37. // Power Up and Reset
  38. // Clock Setup (datasheet page 72)
  39. digitalWrite(PIN_PDN, LOW);
  40. delay(1);
  41. digitalWrite(PIN_PDN, HIGH);
  42. // After Power Up: PDN pin “L” → “H”
  43. // “L” time of 150ns or more is needed to reset the AK4558.
  44. delay(20);
  45. Serial.println("PDN is HIGH");
  46. // Control register settings become available in 10ms (min.) when LDOE pin = “H”
  47. Wire.begin();
  48. readInitConfig();
  49. // access all registers to store locally their default values
  50. // DIF2-0, DFS1-0 and ACKS bits must be set before MCKI, LRCK and BICK are supplied
  51. // PMPLL = 0 (EXT Slave Mode; disables internal PLL and uses ext. clock) (by DEFAULT)
  52. // ACKS = 0 (Manual Setting Mode; disables automatic clock selection) (by DEFAULT)
  53. // DFS1-0 = 00 (Sampling Speed = Normal Speed Mode) (by DEFAULT)
  54. // TDM1-0 = 00 (Time Division Multiplexing mode OFF) (by DEFAULT)
  55. registers[AK4558_CTRL_1] &= ~AK4558_DIF2;
  56. registers[AK4558_CTRL_1] |= AK4558_DIF1 | AK4558_DIF0;
  57. // DIF2-1-0 = 011 ( 16 bit I2S compatible when BICK = 32fs)
  58. registers[AK4558_CTRL_2] &= ~AK4558_MCKS1;
  59. // MCKS1-0 = 00 (Master Clock Input Frequency Select, set 256fs for Normal Speed Mode -> 11.2896 MHz)
  60. registers[AK4558_MODE_CTRL] &= ~AK4558_BCKO0;
  61. // BCKO1-0 = 00 (BICK Output Frequency at Master Mode = 32fs = 1.4112 MHz)
  62. Wire.beginTransmission(AK4558_I2C_ADDR);
  63. Wire.write(AK4558_CTRL_1);
  64. Wire.write(registers[AK4558_CTRL_1]);
  65. Wire.write(registers[AK4558_CTRL_2]);
  66. Wire.write(registers[AK4558_MODE_CTRL]);
  67. Wire.endTransmission();
  68. // Write configuration registers in a single write operation (datasheet page 81):
  69. // The AK4558 can perform more than one byte write operation per sequence. After receipt of the third byte
  70. // the AK4558 generates an acknowledge and awaits the next data. The master can transmit more than
  71. // one byte instead of terminating the write cycle after the first data byte is transferred. After receiving each
  72. // data packet the internal address counter is incremented by one, and the next data is automatically taken
  73. // into the next address.
  74. // ADC/DAC Output setup (datasheet pages 74, 75)
  75. registers[AK4558_PLL_CTRL] |= AK4558_PLL2;
  76. registers[AK4558_PLL_CTRL] &= ~AK4558_PLL1;
  77. write(AK4558_I2C_ADDR, registers[AK4558_PLL_CTRL]);
  78. delay(10);
  79. // as per table 16, set PLL_CTRL.PLL3-2-1-0 to 0101 for MICK as PLL Reference, 11.2896 MHz
  80. // also, wait 10 ms for PLL lock
  81. // TODO: IS IT NEEDED?
  82. // Set the DAC output to power-save mode: LOPS bit “0” → “1”
  83. registers[AK4558_MODE_CTRL] |= AK4558_FS1 | AK4558_LOPS;
  84. write(AK4558_I2C_ADDR, registers[AK4558_MODE_CTRL]);
  85. // Set up the sampling frequency (FS3-0 bits). The ADC must be powered-up in consideration of PLL
  86. // lock time. (in this case (ref. table 17): Set clock to mode 5 / 44.100 KHz)
  87. // Set up the audio format (Addr=03H). (in this case: TDM1-0 = 00 (Time Division Multiplexing mode OFF) by default)
  88. // ignore this, leaving default values - ADC: Set up the de-emphasis filter (Addr = 07H).
  89. // ignore this, leaving default values - DAC: Set up the digital filter mode.
  90. // ignore this, leaving default values - Set up the digital output volume (Address = 08H, 09H).
  91. registers[AK4558_PWR_MNGT] |= AK4558_PMADR | AK4558_PMADL | AK4558_PMDAR | AK4558_PMDAL;
  92. delay(300);
  93. // Power up the ADC: PMADL = PMADR bits = “0” → “1”
  94. // Initialization cycle of the ADC is 5200/fs @Normal mode. The SDTO pin outputs “L” during initialization.
  95. // Power up the DAC: PMDAL = PMDAR bits = “0” → “1”
  96. // Outputs of the LOUT and ROUT pins start rising. Rise time is 300ms (max.) when C = 1μF.
  97. registers[AK4558_MODE_CTRL] &= ~AK4558_LOPS;
  98. write(AK4558_I2C_ADDR, registers[AK4558_MODE_CTRL]);
  99. // Release power-save mode of the DAC output: LOPS bit = “1” → “0”
  100. // Set LOPS bit to “0” after the LOUT and ROUT pins output “H”. Sound data will be output from the
  101. // LOUT and ROUT pins after this setting.
  102. Serial.println("Setup ended");
  103. return true;
  104. }