選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

134 行
4.1KB

  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 "control_wm8731.h"
  27. #include "Wire.h"
  28. #define WM8731_I2C_ADDR 0x1A
  29. //#define WM8731_I2C_ADDR 0x1B
  30. #define WM8731_REG_LLINEIN 0
  31. #define WM8731_REG_RLINEIN 1
  32. #define WM8731_REG_LHEADOUT 2
  33. #define WM8731_REG_RHEADOUT 3
  34. #define WM8731_REG_ANALOG 4
  35. #define WM8731_REG_DIGITAL 5
  36. #define WM8731_REG_POWERDOWN 6
  37. #define WM8731_REG_INTERFACE 7
  38. #define WM8731_REG_SAMPLING 8
  39. #define WM8731_REG_ACTIVE 9
  40. #define WM8731_REG_RESET 15
  41. bool AudioControlWM8731::enable(void)
  42. {
  43. Wire.begin();
  44. delay(5);
  45. //write(WM8731_REG_RESET, 0);
  46. write(WM8731_REG_INTERFACE, 0x02); // I2S, 16 bit, MCLK slave
  47. write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
  48. // In order to prevent pops, the DAC should first be soft-muted (DACMU),
  49. // the output should then be de-selected from the line and headphone output
  50. // (DACSEL), then the DAC powered down (DACPD).
  51. write(WM8731_REG_DIGITAL, 0x08); // DAC soft mute
  52. write(WM8731_REG_ANALOG, 0x00); // disable all
  53. write(WM8731_REG_POWERDOWN, 0x00); // codec powerdown
  54. write(WM8731_REG_LHEADOUT, 0x80); // volume off
  55. write(WM8731_REG_RHEADOUT, 0x80);
  56. delay(100); // how long to power up?
  57. write(WM8731_REG_ACTIVE, 1);
  58. delay(5);
  59. write(WM8731_REG_DIGITAL, 0x00); // DAC unmuted
  60. write(WM8731_REG_ANALOG, 0x10); // DAC selected
  61. return true;
  62. }
  63. bool AudioControlWM8731::write(unsigned int reg, unsigned int val)
  64. {
  65. Wire.beginTransmission(WM8731_I2C_ADDR);
  66. Wire.write((reg << 1) | ((val >> 8) & 1));
  67. Wire.write(val & 0xFF);
  68. Wire.endTransmission();
  69. return true;
  70. }
  71. bool AudioControlWM8731::volumeInteger(unsigned int n)
  72. {
  73. if (n > 127) n = 127;
  74. //Serial.print("volumeInteger, n = ");
  75. //Serial.println(n);
  76. write(WM8731_REG_LHEADOUT, n | 0x180);
  77. write(WM8731_REG_RHEADOUT, n | 0x80);
  78. return true;
  79. }
  80. /******************************************************************/
  81. bool AudioControlWM8731master::enable(void)
  82. {
  83. Wire.begin();
  84. delay(5);
  85. //write(WM8731_REG_RESET, 0);
  86. write(WM8731_REG_INTERFACE, 0x42); // I2S, 16 bit, MCLK master
  87. write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
  88. // In order to prevent pops, the DAC should first be soft-muted (DACMU),
  89. // the output should then be de-selected from the line and headphone output
  90. // (DACSEL), then the DAC powered down (DACPD).
  91. write(WM8731_REG_DIGITAL, 0x08); // DAC soft mute
  92. write(WM8731_REG_ANALOG, 0x00); // disable all
  93. write(WM8731_REG_POWERDOWN, 0x00); // codec powerdown
  94. write(WM8731_REG_LHEADOUT, 0x80); // volume off
  95. write(WM8731_REG_RHEADOUT, 0x80);
  96. delay(100); // how long to power up?
  97. write(WM8731_REG_ACTIVE, 1);
  98. delay(5);
  99. write(WM8731_REG_DIGITAL, 0x00); // DAC unmuted
  100. write(WM8731_REG_ANALOG, 0x10); // DAC selected
  101. return true;
  102. }