Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

control_cs4272.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * https://hackaday.io/project/5912-teensy-super-audio-board
  5. * https://github.com/whollender/Audio/tree/SuperAudioBoard
  6. *
  7. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  8. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  9. * open source software by purchasing Teensy or other PJRC products.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice, development funding notice, and this permission
  19. * notice shall be included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #include "control_cs4272.h"
  30. #include "Wire.h"
  31. #define CS4272_ADDR 0x10 // TODO: need to double check
  32. // Section 8.1 Mode Control
  33. #define CS4272_MODE_CONTROL (uint8_t)0x01
  34. #define CS4272_MC_FUNC_MODE(x) (uint8_t)(((x) & 0x03) << 6)
  35. #define CS4272_MC_RATIO_SEL(x) (uint8_t)(((x) & 0x03) << 4)
  36. #define CS4272_MC_MASTER_SLAVE (uint8_t)0x08
  37. #define CS4272_MC_SERIAL_FORMAT(x) (uint8_t)(((x) & 0x07) << 0)
  38. // Section 8.2 DAC Control
  39. #define CS4272_DAC_CONTROL (uint8_t)0x02
  40. #define CS4272_DAC_CTRL_AUTO_MUTE (uint8_t)0x80
  41. #define CS4272_DAC_CTRL_FILTER_SEL (uint8_t)0x40
  42. #define CS4272_DAC_CTRL_DE_EMPHASIS(x) (uint8_t)(((x) & 0x03) << 4)
  43. #define CS4272_DAC_CTRL_VOL_RAMP_UP (uint8_t)0x08
  44. #define CS4272_DAC_CTRL_VOL_RAMP_DN (uint8_t)0x04
  45. #define CS4272_DAC_CTRL_INV_POL(x) (uint8_t)(((x) & 0x03) << 0)
  46. // Section 8.3 DAC Volume and Mixing
  47. #define CS4272_DAC_VOL (uint8_t)0x03
  48. #define CS4272_DAC_VOL_CH_VOL_TRACKING (uint8_t)0x40
  49. #define CS4272_DAC_VOL_SOFT_RAMP(x) (uint8_t)(((x) & 0x03) << 4)
  50. #define CS4272_DAC_VOL_ATAPI(x) (uint8_t)(((x) & 0x0F) << 0)
  51. // Section 8.4 DAC Channel A volume
  52. #define CS4272_DAC_CHA_VOL (uint8_t)0x04
  53. #define CS4272_DAC_CHA_VOL_MUTE (uint8_t)0x80
  54. #define CS4272_DAC_CHA_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0)
  55. // Section 8.5 DAC Channel B volume
  56. #define CS4272_DAC_CHB_VOL (uint8_t)0x05
  57. #define CS4272_DAC_CHB_VOL_MUTE (uint8_t)0x80
  58. #define CS4272_DAC_CHB_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0)
  59. // Section 8.6 ADC Control
  60. #define CS4272_ADC_CTRL (uint8_t)0x06
  61. #define CS4272_ADC_CTRL_DITHER (uint8_t)0x20
  62. #define CS4272_ADC_CTRL_SER_FORMAT (uint8_t)0x10
  63. #define CS4272_ADC_CTRL_MUTE(x) (uint8_t)(((x) & 0x03) << 2)
  64. #define CS4272_ADC_CTRL_HPF(x) (uint8_t)(((x) & 0x03) << 0)
  65. // Section 8.7 Mode Control 2
  66. #define CS4272_MODE_CTRL2 (uint8_t)0x07
  67. #define CS4272_MODE_CTRL2_LOOP (uint8_t)0x10
  68. #define CS4272_MODE_CTRL2_MUTE_TRACK (uint8_t)0x08
  69. #define CS4272_MODE_CTRL2_CTRL_FREEZE (uint8_t)0x04
  70. #define CS4272_MODE_CTRL2_CTRL_PORT_EN (uint8_t)0x02
  71. #define CS4272_MODE_CTRL2_POWER_DOWN (uint8_t)0x01
  72. // Section 8.8 Chip ID
  73. #define CS4272_CHIP_ID (uint8_t)0x08
  74. #define CS4272_CHIP_ID_PART(x) (uint8_t)(((x) & 0x0F) << 4)
  75. #define CS4272_CHIP_ID_REV(x) (uint8_t)(((x) & 0x0F) << 0)
  76. #define CS4272_RESET_PIN 2
  77. bool AudioControlCS4272::enable(void)
  78. {
  79. Wire.begin();
  80. delay(5);
  81. initLocalRegs();
  82. // Setup Reset pin
  83. pinMode(CS4272_RESET_PIN, OUTPUT);
  84. // Drive pin low
  85. digitalWriteFast(CS4272_RESET_PIN, LOW);
  86. delay(1);
  87. // Release Reset
  88. digitalWriteFast(CS4272_RESET_PIN, HIGH);
  89. delay(2);
  90. // Set power down and control port enable as spec'd in the
  91. // datasheet for control port mode
  92. write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_POWER_DOWN
  93. | CS4272_MODE_CTRL2_CTRL_PORT_EN);
  94. // Wait for further setup
  95. delay(1);
  96. // Set ratio select for MCLK=512*LRCLK (BCLK = 64*LRCLK), and master mode
  97. write(CS4272_MODE_CONTROL, CS4272_MC_RATIO_SEL(3) | CS4272_MC_MASTER_SLAVE);
  98. delay(10);
  99. // Release power down bit to start up codec
  100. // TODO: May need other bits set in this reg
  101. write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_CTRL_PORT_EN);
  102. // Wait for everything to come up
  103. delay(10);
  104. return true;
  105. }
  106. bool AudioControlCS4272::volumeInteger(unsigned int n)
  107. {
  108. unsigned int val = 0x7F - (n & 0x7F);
  109. write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val));
  110. write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val));
  111. return true;
  112. }
  113. bool AudioControlCS4272::volume(float left, float right)
  114. {
  115. unsigned int leftInt,rightInt;
  116. leftInt = left*127 + 0.499;
  117. rightInt = right*127 + 0.499;
  118. unsigned int val = 0x7F - (leftInt & 0x7F);
  119. write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val));
  120. val = 0x7F - (rightInt & 0x7F);
  121. write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val));
  122. return true;
  123. }
  124. bool AudioControlCS4272::dacVolume(float left, float right)
  125. {
  126. return volume(left,right);
  127. }
  128. bool AudioControlCS4272::muteOutput(void)
  129. {
  130. write(CS4272_DAC_CHA_VOL,
  131. regLocal[CS4272_DAC_CHA_VOL] | CS4272_DAC_CHA_VOL_MUTE);
  132. write(CS4272_DAC_CHB_VOL,
  133. regLocal[CS4272_DAC_CHB_VOL] | CS4272_DAC_CHB_VOL_MUTE);
  134. return true;
  135. }
  136. bool AudioControlCS4272::unmuteOutput(void)
  137. {
  138. write(CS4272_DAC_CHA_VOL,
  139. regLocal[CS4272_DAC_CHA_VOL] & ~CS4272_DAC_CHA_VOL_MUTE);
  140. write(CS4272_DAC_CHB_VOL,
  141. regLocal[CS4272_DAC_CHB_VOL] & ~CS4272_DAC_CHB_VOL_MUTE);
  142. return true;
  143. }
  144. bool AudioControlCS4272::muteInput(void)
  145. {
  146. uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_MUTE(3);
  147. write(CS4272_ADC_CTRL,val);
  148. return true;
  149. }
  150. bool AudioControlCS4272::unmuteInput(void)
  151. {
  152. uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_MUTE(3);
  153. write(CS4272_ADC_CTRL,val);
  154. return true;
  155. }
  156. bool AudioControlCS4272::enableDither(void)
  157. {
  158. uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_DITHER;
  159. write(CS4272_ADC_CTRL,val);
  160. return true;
  161. }
  162. bool AudioControlCS4272::disableDither(void)
  163. {
  164. uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_DITHER;
  165. write(CS4272_ADC_CTRL,val);
  166. return true;
  167. }
  168. bool AudioControlCS4272::write(unsigned int reg, unsigned int val)
  169. {
  170. // Write local copy first
  171. if(reg > 7)
  172. return false;
  173. regLocal[reg] = val;
  174. Wire.beginTransmission(CS4272_ADDR);
  175. Wire.write(reg & 0xFF);
  176. Wire.write(val & 0xFF);
  177. Wire.endTransmission();
  178. return true;
  179. }
  180. // Initialize local registers to CS4272 reset status
  181. void AudioControlCS4272::initLocalRegs(void)
  182. {
  183. regLocal[CS4272_MODE_CONTROL] = 0x00;
  184. regLocal[CS4272_DAC_CONTROL] = CS4272_DAC_CTRL_AUTO_MUTE;
  185. regLocal[CS4272_DAC_VOL] = CS4272_DAC_VOL_SOFT_RAMP(2) | CS4272_DAC_VOL_ATAPI(9);
  186. regLocal[CS4272_DAC_CHA_VOL] = 0x00;
  187. regLocal[CS4272_DAC_CHB_VOL] = 0x00;
  188. regLocal[CS4272_ADC_CTRL] = 0x00;
  189. regLocal[CS4272_MODE_CTRL2] = 0x00;
  190. }