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.

268 lines
9.0KB

  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. #if AK4558_SERIAL_DEBUG > 0
  22. Serial.print("Register ");
  23. Serial.print(n);
  24. Serial.print(" = ");
  25. #endif
  26. registers[n++] = Wire.read();
  27. #if AK4558_SERIAL_DEBUG > 0
  28. Serial.println(registers[n-1], BIN);
  29. #endif
  30. }
  31. }
  32. void AudioControlAK4558::readConfig(void)
  33. {
  34. // reads registers values
  35. unsigned int n = 0;
  36. char c = 0;
  37. Wire.requestFrom(AK4558_I2C_ADDR, 10);
  38. while(Wire.available()) {
  39. Serial.print("Register ");
  40. Serial.print(n++);
  41. Serial.print(" = ");
  42. c = Wire.read();
  43. Serial.println(c, BIN);
  44. }
  45. }
  46. bool AudioControlAK4558::write(unsigned int reg, unsigned int val)
  47. {
  48. Wire.beginTransmission(AK4558_I2C_ADDR);
  49. Wire.write(reg);
  50. Wire.write(val);
  51. return (Wire.endTransmission(true)==0);
  52. }
  53. bool AudioControlAK4558::enableIn(void)
  54. {
  55. // ADC setup (datasheet page 74
  56. #if AK4558_SERIAL_DEBUG > 0
  57. Serial.println("AK4558: Enable ADC");
  58. #endif
  59. // ignore this, leaving default values - ADC: Set up the de-emphasis filter (Addr = 07H).
  60. registers[AK4558_PWR_MNGT] |= AK4558_PMADR | AK4558_PMADL;
  61. write(AK4558_PWR_MNGT, registers[AK4558_PWR_MNGT]);
  62. #if AK4558_SERIAL_DEBUG > 0
  63. Serial.print("AK4558: PWR_MNGT set to ");
  64. Serial.println(registers[AK4558_PWR_MNGT], BIN);
  65. #endif
  66. delay(300);
  67. // Power up the ADC: PMADL = PMADR bits = “0” → “1”
  68. // Initialization cycle of the ADC is 5200/fs @Normal mode. The SDTO pin outputs “L” during initialization.
  69. #if AK4558_SERIAL_DEBUG > 0
  70. Serial.println("AK4558: Enable ADC - Done");
  71. #endif
  72. return true;
  73. }
  74. bool AudioControlAK4558::enableOut(void)
  75. {
  76. #if AK4558_SERIAL_DEBUG > 0
  77. Serial.println("AK4558: Enable DAC");
  78. #endif
  79. // DAC Output setup (datasheet page 75)
  80. registers[AK4558_MODE_CTRL] |= AK4558_LOPS;
  81. write(AK4558_MODE_CTRL, registers[AK4558_MODE_CTRL]);
  82. #if AK4558_SERIAL_DEBUG > 0
  83. Serial.print("AK4558: MODE_CTRL set to ");
  84. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  85. #endif
  86. // Set the DAC output to power-save mode: LOPS bit “0” → “1”
  87. // ignore this, leaving default values - DAC: Set up the digital filter mode.
  88. // ignore this, leaving default values - Set up the digital output volume (Address = 08H, 09H).
  89. registers[AK4558_PWR_MNGT] |= AK4558_PMDAR | AK4558_PMDAL;
  90. write(AK4558_PWR_MNGT, registers[AK4558_PWR_MNGT]);
  91. #if AK4558_SERIAL_DEBUG > 0
  92. Serial.print("AK4558: PWR_MNGT set to ");
  93. Serial.println(registers[AK4558_PWR_MNGT], BIN);
  94. #endif
  95. delay(300);
  96. // Power up the DAC: PMDAL = PMDAR bits = “0” → “1”
  97. // Outputs of the LOUT and ROUT pins start rising. Rise time is 300ms (max.) when C = 1μF.
  98. registers[AK4558_MODE_CTRL] &= ~AK4558_LOPS;
  99. write(AK4558_MODE_CTRL, registers[AK4558_MODE_CTRL]);
  100. #if AK4558_SERIAL_DEBUG > 0
  101. Serial.print("AK4558: MODE_CTRL set to ");
  102. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  103. #endif
  104. // Release power-save mode of the DAC output: LOPS bit = “1” → “0”
  105. // Set LOPS bit to “0” after the LOUT and ROUT pins output “H”. Sound data will be output from the
  106. // LOUT and ROUT pins after this setting.
  107. #if AK4558_SERIAL_DEBUG > 0
  108. Serial.println("AK4558: Enable DAC - Done");
  109. #endif
  110. return true;
  111. }
  112. bool AudioControlAK4558::enable(void)
  113. {
  114. #if AK4558_SERIAL_DEBUG > 0
  115. Serial.println("AK4558: Enable device");
  116. #endif
  117. // Power Up and Reset
  118. // Clock Setup (datasheet page 72)
  119. pinMode(PIN_PDN, OUTPUT);
  120. digitalWrite(0, LOW);
  121. delay(1);
  122. digitalWrite(0, HIGH);
  123. // After Power Up: PDN pin “L” → “H”
  124. // “L” time of 150ns or more is needed to reset the AK4558.
  125. delay(20);
  126. #if AK4558_SERIAL_DEBUG > 0
  127. Serial.println("AK4558: PDN is HIGH (device reset)");
  128. #endif
  129. // Control register settings become available in 10ms (min.) when LDOE pin = “H”
  130. Wire.begin();
  131. initConfig();
  132. // access all registers to store locally their default values
  133. // DIF2-0, DFS1-0 and ACKS bits must be set before MCKI, LRCK and BICK are supplied
  134. // PMPLL = 0 (EXT Slave Mode; disables internal PLL and uses ext. clock) (by DEFAULT)
  135. // ACKS = 0 (Manual Setting Mode; disables automatic clock selection) (by DEFAULT)
  136. // DFS1-0 = 00 (Sampling Speed = Normal Speed Mode) (by DEFAULT)
  137. // TDM1-0 = 00 (Time Division Multiplexing mode OFF) (by DEFAULT)
  138. registers[AK4558_CTRL_1] &= ~AK4558_DIF2;
  139. registers[AK4558_CTRL_1] |= AK4558_DIF1 | AK4558_DIF0;
  140. #if AK4558_SERIAL_DEBUG > 0
  141. Serial.print("AK4558: CTRL_1 set to ");
  142. Serial.println(registers[AK4558_CTRL_1], BIN);
  143. #endif
  144. // DIF2-1-0 = 011 ( 16 bit I2S compatible when BICK = 32fs)
  145. registers[AK4558_CTRL_2] &= ~AK4558_MCKS1;
  146. #if AK4558_SERIAL_DEBUG > 0
  147. Serial.print("AK4558: CTRL_2 set to ");
  148. Serial.println(registers[AK4558_CTRL_2], BIN);
  149. #endif
  150. // MCKS1-0 = 00 (Master Clock Input Frequency Select, set 256fs for Normal Speed Mode -> 11.2896 MHz)
  151. registers[AK4558_MODE_CTRL] &= ~AK4558_BCKO0;
  152. // BCKO1-0 = 00 (BICK Output Frequency at Master Mode = 32fs = 1.4112 MHz)
  153. registers[AK4558_MODE_CTRL] |= AK4558_FS1;
  154. // Set up the sampling frequency (FS3-0 bits). The ADC must be powered-up in consideration of PLL
  155. // lock time. (in this case (ref. table 17): Set clock to mode 5 / 44.100 KHz)
  156. #if AK4558_SERIAL_DEBUG > 0
  157. Serial.print("AK4558: MODE_CTRL set to ");
  158. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  159. #endif
  160. // BCKO1-0 = 00 (BICK Output Frequency at Master Mode = 32fs = 1.4112 MHz)
  161. Wire.beginTransmission(AK4558_I2C_ADDR);
  162. Wire.write(AK4558_CTRL_1);
  163. Wire.write(registers[AK4558_CTRL_1]);
  164. Wire.write(registers[AK4558_CTRL_2]);
  165. Wire.write(registers[AK4558_MODE_CTRL]);
  166. Wire.endTransmission();
  167. // Write configuration registers in a single write operation (datasheet page 81):
  168. // The AK4558 can perform more than one byte write operation per sequence. After receipt of the third byte
  169. // the AK4558 generates an acknowledge and awaits the next data. The master can transmit more than
  170. // one byte instead of terminating the write cycle after the first data byte is transferred. After receiving each
  171. // data packet the internal address counter is incremented by one, and the next data is automatically taken
  172. // into the next address.
  173. #if AK4558_SERIAL_DEBUG > 0
  174. Serial.println("AK4558: Enable device - Done");
  175. #endif
  176. write(AK4558_ROUT_VOL,255); //dummy write to last register to reset address counter to 0
  177. return true;
  178. }
  179. bool AudioControlAK4558::disableIn(void)
  180. {
  181. // ADC power-down (datasheet page 74
  182. registers[AK4558_PWR_MNGT] &= ~AK4558_PMADR | ~AK4558_PMADL;
  183. write(AK4558_PWR_MNGT, registers[AK4558_PWR_MNGT]);
  184. #if AK4558_SERIAL_DEBUG > 0
  185. Serial.print("AK4558: PWR_MNGT set to ");
  186. Serial.println(registers[AK4558_PWR_MNGT], BIN);
  187. #endif
  188. // Power down ADC: PMADL = PMADR bits = “1” → “0”
  189. #if AK4558_SERIAL_DEBUG > 0
  190. Serial.println("AK4558: Enable ADC - Done");
  191. #endif
  192. return true;
  193. }
  194. bool AudioControlAK4558::disableOut(void)
  195. {
  196. #if AK4558_SERIAL_DEBUG > 0
  197. Serial.println("AK4558: Disable DAC");
  198. #endif
  199. // DAC Output power-down (datasheet page 75)
  200. registers[AK4558_MODE_CTRL] |= AK4558_LOPS;
  201. write(AK4558_MODE_CTRL, registers[AK4558_MODE_CTRL]);
  202. #if AK4558_SERIAL_DEBUG > 0
  203. Serial.print("AK4558: MODE_CTRL set to ");
  204. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  205. #endif
  206. // Set the DAC output to power-save mode: LOPS bit “0” → “1”
  207. registers[AK4558_PWR_MNGT] &= ~AK4558_PMDAR | ~AK4558_PMDAL;
  208. write(AK4558_PWR_MNGT, registers[AK4558_PWR_MNGT]);
  209. #if AK4558_SERIAL_DEBUG > 0
  210. Serial.print("AK4558: PWR_MNGT set to ");
  211. Serial.println(registers[AK4558_PWR_MNGT], BIN);
  212. #endif
  213. delay(300);
  214. // Power down the DAC: PMDAL = PMDAR bits = “1” → “0”
  215. // Outputs of the LOUT and ROUT pins start falling. Rise time is 300ms (max.) when C = 1μF.
  216. registers[AK4558_MODE_CTRL] &= ~AK4558_LOPS;
  217. write(AK4558_MODE_CTRL, registers[AK4558_MODE_CTRL]);
  218. #if AK4558_SERIAL_DEBUG > 0
  219. Serial.print("AK4558: MODE_CTRL set to ");
  220. Serial.println(registers[AK4558_MODE_CTRL], BIN);
  221. #endif
  222. // Release power-save mode of the DAC output: LOPS bit = “1” → “0”
  223. // Set LOPS bit to “0” after outputs of the LOUT and ROUT pins fall to “L”.
  224. #if AK4558_SERIAL_DEBUG > 0
  225. Serial.println("AK4558: Disable DAC - Done");
  226. #endif
  227. return true;
  228. }
  229. bool AudioControlAK4558:volume(float n)
  230. {
  231. // Set DAC output volume
  232. registers[AK4558_LOUT_VOL] = 255;
  233. registers[AK4558_ROUT_VOL] = 255;
  234. Wire.beginTransmission(AK4558_I2C_ADDR);
  235. Wire.write(AK4558_LOUT_VOL);
  236. Wire.write(registers[AK4558_LOUT_VOL]);
  237. Wire.write(registers[AK4558_ROUT_VOL]);
  238. Wire.endTransmission();
  239. #if AK4558_SERIAL_DEBUG > 0
  240. Serial.print("AK4558: LOUT_VOL set to ");
  241. Serial.println(registers[AK4558_LOUT_VOL], BIN);
  242. #endif
  243. return (Wire.endTransmission(true)==0);
  244. }