Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

180 lines
5.3KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "control_wm8731.h"
  28. #include "Wire.h"
  29. #define WM8731_I2C_ADDR 0x1A
  30. //#define WM8731_I2C_ADDR 0x1B
  31. #define WM8731_REG_LLINEIN 0
  32. #define WM8731_REG_RLINEIN 1
  33. #define WM8731_REG_LHEADOUT 2
  34. #define WM8731_REG_RHEADOUT 3
  35. #define WM8731_REG_ANALOG 4
  36. #define WM8731_REG_DIGITAL 5
  37. #define WM8731_REG_POWERDOWN 6
  38. #define WM8731_REG_INTERFACE 7
  39. #define WM8731_REG_SAMPLING 8
  40. #define WM8731_REG_ACTIVE 9
  41. #define WM8731_REG_RESET 15
  42. bool AudioControlWM8731::enable(void)
  43. {
  44. Wire.begin();
  45. delay(5);
  46. #if 1
  47. if (!write(WM8731_REG_RESET, 0)) {
  48. return false; // no WM8731 chip responding
  49. }
  50. #endif
  51. write(WM8731_REG_INTERFACE, 0x02); // I2S, 16 bit, MCLK slave
  52. write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
  53. // In order to prevent pops, the DAC should first be soft-muted (DACMU),
  54. // the output should then be de-selected from the line and headphone output
  55. // (DACSEL), then the DAC powered down (DACPD).
  56. write(WM8731_REG_DIGITAL, 0x08); // DAC soft mute
  57. write(WM8731_REG_ANALOG, 0x00); // disable all
  58. write(WM8731_REG_POWERDOWN, 0x00); // codec powerdown
  59. write(WM8731_REG_LHEADOUT, 0x80); // volume off
  60. write(WM8731_REG_RHEADOUT, 0x80);
  61. delay(100); // how long to power up?
  62. write(WM8731_REG_ACTIVE, 1);
  63. delay(5);
  64. write(WM8731_REG_DIGITAL, 0x00); // DAC unmuted
  65. write(WM8731_REG_ANALOG, 0x10); // DAC selected
  66. return true;
  67. }
  68. // WM8731 has flaky I2C communication, especially when MCLK has ringing,
  69. // overshoot or other high-speed signal quality issues. Chips like
  70. // Teensy 3.6 with very high bandwidth I/O pins should be used with
  71. // caution. A resistor or low-pass circuit may be needed.
  72. // https://forum.pjrc.com/threads/55334?p=201494&viewfull=1#post201494
  73. bool AudioControlWM8731::write(unsigned int reg, unsigned int val)
  74. {
  75. int attempt=0;
  76. while (1) {
  77. attempt++;
  78. Wire.beginTransmission(WM8731_I2C_ADDR);
  79. Wire.write((reg << 1) | ((val >> 8) & 1));
  80. Wire.write(val & 0xFF);
  81. int status = Wire.endTransmission();
  82. if (status == 0) {
  83. //Serial.printf("WM8731 write ok, %d tries\n", attempt);
  84. return true;
  85. }
  86. if (attempt >= 12) {
  87. //Serial.printf("WM8731 write failed, %d tries\n", attempt);
  88. return false;
  89. }
  90. delayMicroseconds(80);
  91. }
  92. }
  93. bool AudioControlWM8731::volumeInteger(unsigned int n)
  94. {
  95. // n = 127 for max volume (+6 dB)
  96. // n = 48 for min volume (-73 dB)
  97. // n = 0 to 47 for mute
  98. if (n > 127) n = 127;
  99. //Serial.print("volumeInteger, n = ");
  100. //Serial.println(n);
  101. write(WM8731_REG_LHEADOUT, n | 0x180);
  102. write(WM8731_REG_RHEADOUT, n | 0x80);
  103. return true;
  104. }
  105. bool AudioControlWM8731::inputLevel(float n)
  106. {
  107. // range is 0x00 (min) - 0x1F (max)
  108. int _level = int(n * 31.f);
  109. _level = _level > 0x1F ? 0x1F : _level;
  110. write(WM8731_REG_LLINEIN, _level);
  111. write(WM8731_REG_RLINEIN, _level);
  112. return true;
  113. }
  114. bool AudioControlWM8731::inputSelect(int n)
  115. {
  116. if (n == AUDIO_INPUT_LINEIN) {
  117. write(WM8731_REG_ANALOG, 0x12);
  118. } else if (n == AUDIO_INPUT_MIC) {
  119. write(WM8731_REG_ANALOG, 0x15);
  120. } else {
  121. return false;
  122. }
  123. return true;
  124. }
  125. /******************************************************************/
  126. bool AudioControlWM8731master::enable(void)
  127. {
  128. Wire.begin();
  129. delay(5);
  130. //write(WM8731_REG_RESET, 0);
  131. write(WM8731_REG_INTERFACE, 0x42); // I2S, 16 bit, MCLK master
  132. write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
  133. // In order to prevent pops, the DAC should first be soft-muted (DACMU),
  134. // the output should then be de-selected from the line and headphone output
  135. // (DACSEL), then the DAC powered down (DACPD).
  136. write(WM8731_REG_DIGITAL, 0x08); // DAC soft mute
  137. write(WM8731_REG_ANALOG, 0x00); // disable all
  138. write(WM8731_REG_POWERDOWN, 0x00); // codec powerdown
  139. write(WM8731_REG_LHEADOUT, 0x80); // volume off
  140. write(WM8731_REG_RHEADOUT, 0x80);
  141. delay(100); // how long to power up?
  142. write(WM8731_REG_ACTIVE, 1);
  143. delay(5);
  144. write(WM8731_REG_DIGITAL, 0x00); // DAC unmuted
  145. write(WM8731_REG_ANALOG, 0x10); // DAC selected
  146. return true;
  147. }