You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.6KB

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