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.

111 lines
2.7KB

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