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.

773 lines
28KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. #include "Wire.h"
  4. #define CHIP_ID 0x0000
  5. // 15:8 PARTID 0xA0 - 8 bit identifier for SGTL5000
  6. // 7:0 REVID 0x00 - revision number for SGTL5000.
  7. #define CHIP_DIG_POWER 0x0002
  8. // 6 ADC_POWERUP 1=Enable, 0=disable the ADC block, both digital & analog,
  9. // 5 DAC_POWERUP 1=Enable, 0=disable the DAC block, both analog and digital
  10. // 4 DAP_POWERUP 1=Enable, 0=disable the DAP block
  11. // 1 I2S_OUT_POWERUP 1=Enable, 0=disable the I2S data output
  12. // 0 I2S_IN_POWERUP 1=Enable, 0=disable the I2S data input
  13. #define CHIP_CLK_CTRL 0x0004
  14. // 5:4 RATE_MODE Sets the sample rate mode. MCLK_FREQ is still specified
  15. // relative to the rate in SYS_FS
  16. // 0x0 = SYS_FS specifies the rate
  17. // 0x1 = Rate is 1/2 of the SYS_FS rate
  18. // 0x2 = Rate is 1/4 of the SYS_FS rate
  19. // 0x3 = Rate is 1/6 of the SYS_FS rate
  20. // 3:2 SYS_FS Sets the internal system sample rate (default=2)
  21. // 0x0 = 32 kHz
  22. // 0x1 = 44.1 kHz
  23. // 0x2 = 48 kHz
  24. // 0x3 = 96 kHz
  25. // 1:0 MCLK_FREQ Identifies incoming SYS_MCLK frequency and if the PLL should be used
  26. // 0x0 = 256*Fs
  27. // 0x1 = 384*Fs
  28. // 0x2 = 512*Fs
  29. // 0x3 = Use PLL
  30. // The 0x3 (Use PLL) setting must be used if the SYS_MCLK is not
  31. // a standard multiple of Fs (256, 384, or 512). This setting can
  32. // also be used if SYS_MCLK is a standard multiple of Fs.
  33. // Before this field is set to 0x3 (Use PLL), the PLL must be
  34. // powered up by setting CHIP_ANA_POWER->PLL_POWERUP and
  35. // CHIP_ANA_POWER->VCOAMP_POWERUP. Also, the PLL dividers must
  36. // be calculated based on the external MCLK rate and
  37. // CHIP_PLL_CTRL register must be set (see CHIP_PLL_CTRL register
  38. // description details on how to calculate the divisors).
  39. #define CHIP_I2S_CTRL 0x0006
  40. // 8 SCLKFREQ Sets frequency of I2S_SCLK when in master mode (MS=1). When in slave
  41. // mode (MS=0), this field must be set appropriately to match SCLK input
  42. // rate.
  43. // 0x0 = 64Fs
  44. // 0x1 = 32Fs - Not supported for RJ mode (I2S_MODE = 1)
  45. // 7 MS Configures master or slave of I2S_LRCLK and I2S_SCLK.
  46. // 0x0 = Slave: I2S_LRCLK an I2S_SCLK are inputs
  47. // 0x1 = Master: I2S_LRCLK and I2S_SCLK are outputs
  48. // NOTE: If the PLL is used (CHIP_CLK_CTRL->MCLK_FREQ==0x3),
  49. // the SGTL5000 must be a master of the I2S port (MS==1)
  50. // 6 SCLK_INV Sets the edge that data (input and output) is clocked in on for I2S_SCLK
  51. // 0x0 = data is valid on rising edge of I2S_SCLK
  52. // 0x1 = data is valid on falling edge of I2S_SCLK
  53. // 5:4 DLEN I2S data length (default=1)
  54. // 0x0 = 32 bits (only valid when SCLKFREQ=0),
  55. // not valid for Right Justified Mode
  56. // 0x1 = 24 bits (only valid when SCLKFREQ=0)
  57. // 0x2 = 20 bits
  58. // 0x3 = 16 bits
  59. // 3:2 I2S_MODE Sets the mode for the I2S port
  60. // 0x0 = I2S mode or Left Justified (Use LRALIGN to select)
  61. // 0x1 = Right Justified Mode
  62. // 0x2 = PCM Format A/B
  63. // 0x3 = RESERVED
  64. // 1 LRALIGN I2S_LRCLK Alignment to data word. Not used for Right Justified mode
  65. // 0x0 = Data word starts 1 I2S_SCLK delay after I2S_LRCLK
  66. // transition (I2S format, PCM format A)
  67. // 0x1 = Data word starts after I2S_LRCLK transition (left
  68. // justified format, PCM format B)
  69. // 0 LRPOL I2S_LRCLK Polarity when data is presented.
  70. // 0x0 = I2S_LRCLK = 0 - Left, 1 - Right
  71. // 1x0 = I2S_LRCLK = 0 - Right, 1 - Left
  72. // The left subframe should be presented first regardless of
  73. // the setting of LRPOL.
  74. #define CHIP_SSS_CTRL 0x000A
  75. // 14 DAP_MIX_LRSWAP DAP Mixer Input Swap
  76. // 0x0 = Normal Operation
  77. // 0x1 = Left and Right channels for the DAP MIXER Input are swapped.
  78. // 13 DAP_LRSWAP DAP Mixer Input Swap
  79. // 0x0 = Normal Operation
  80. // 0x1 = Left and Right channels for the DAP Input are swapped
  81. // 12 DAC_LRSWAP DAC Input Swap
  82. // 0x0 = Normal Operation
  83. // 0x1 = Left and Right channels for the DAC are swapped
  84. // 10 I2S_LRSWAP I2S_DOUT Swap
  85. // 0x0 = Normal Operation
  86. // 0x1 = Left and Right channels for the I2S_DOUT are swapped
  87. // 9:8 DAP_MIX_SELECT Select data source for DAP mixer
  88. // 0x0 = ADC
  89. // 0x1 = I2S_IN
  90. // 0x2 = Reserved
  91. // 0x3 = Reserved
  92. // 7:6 DAP_SELECT Select data source for DAP
  93. // 0x0 = ADC
  94. // 0x1 = I2S_IN
  95. // 0x2 = Reserved
  96. // 0x3 = Reserved
  97. // 5:4 DAC_SELECT Select data source for DAC (default=1)
  98. // 0x0 = ADC
  99. // 0x1 = I2S_IN
  100. // 0x2 = Reserved
  101. // 0x3 = DAP
  102. // 1:0 I2S_SELECT Select data source for I2S_DOUT
  103. // 0x0 = ADC
  104. // 0x1 = I2S_IN
  105. // 0x2 = Reserved
  106. // 0x3 = DAP
  107. #define CHIP_ADCDAC_CTRL 0x000E
  108. // 13 VOL_BUSY_DAC_RIGHT Volume Busy DAC Right
  109. // 0x0 = Ready
  110. // 0x1 = Busy - This indicates the channel has not reached its
  111. // programmed volume/mute level
  112. // 12 VOL_BUSY_DAC_LEFT Volume Busy DAC Left
  113. // 0x0 = Ready
  114. // 0x1 = Busy - This indicates the channel has not reached its
  115. // programmed volume/mute level
  116. // 9 VOL_RAMP_EN Volume Ramp Enable (default=1)
  117. // 0x0 = Disables volume ramp. New volume settings take immediate
  118. // effect without a ramp
  119. // 0x1 = Enables volume ramp
  120. // This field affects DAC_VOL. The volume ramp effects both
  121. // volume settings and mute When set to 1 a soft mute is enabled.
  122. // 8 VOL_EXPO_RAMP Exponential Volume Ramp Enable
  123. // 0x0 = Linear ramp over top 4 volume octaves
  124. // 0x1 = Exponential ramp over full volume range
  125. // This bit only takes effect if VOL_RAMP_EN is 1.
  126. // 3 DAC_MUTE_RIGHT DAC Right Mute (default=1)
  127. // 0x0 = Unmute
  128. // 0x1 = Muted
  129. // If VOL_RAMP_EN = 1, this is a soft mute.
  130. // 2 DAC_MUTE_LEFT DAC Left Mute (default=1)
  131. // 0x0 = Unmute
  132. // 0x1 = Muted
  133. // If VOL_RAMP_EN = 1, this is a soft mute.
  134. // 1 ADC_HPF_FREEZE ADC High Pass Filter Freeze
  135. // 0x0 = Normal operation
  136. // 0x1 = Freeze the ADC high-pass filter offset register. The
  137. // offset continues to be subtracted from the ADC data stream.
  138. // 0 ADC_HPF_BYPASS ADC High Pass Filter Bypass
  139. // 0x0 = Normal operation
  140. // 0x1 = Bypassed and offset not updated
  141. #define CHIP_DAC_VOL 0x0010
  142. // 15:8 DAC_VOL_RIGHT DAC Right Channel Volume. Set the Right channel DAC volume
  143. // with 0.5017 dB steps from 0 to -90 dB
  144. // 0x3B and less = Reserved
  145. // 0x3C = 0 dB
  146. // 0x3D = -0.5 dB
  147. // 0xF0 = -90 dB
  148. // 0xFC and greater = Muted
  149. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  150. // new volume setting.
  151. // 7:0 DAC_VOL_LEFT DAC Left Channel Volume. Set the Left channel DAC volume
  152. // with 0.5017 dB steps from 0 to -90 dB
  153. // 0x3B and less = Reserved
  154. // 0x3C = 0 dB
  155. // 0x3D = -0.5 dB
  156. // 0xF0 = -90 dB
  157. // 0xFC and greater = Muted
  158. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  159. // new volume setting.
  160. #define CHIP_PAD_STRENGTH 0x0014
  161. // 9:8 I2S_LRCLK I2S LRCLK Pad Drive Strength (default=1)
  162. // Sets drive strength for output pads per the table below.
  163. // VDDIO 1.8 V 2.5 V 3.3 V
  164. // 0x0 = Disable
  165. // 0x1 = 1.66 mA 2.87 mA 4.02 mA
  166. // 0x2 = 3.33 mA 5.74 mA 8.03 mA
  167. // 0x3 = 4.99 mA 8.61 mA 12.05 mA
  168. // 7:6 I2S_SCLK I2S SCLK Pad Drive Strength (default=1)
  169. // 5:4 I2S_DOUT I2S DOUT Pad Drive Strength (default=1)
  170. // 3:2 CTRL_DATA I2C DATA Pad Drive Strength (default=3)
  171. // 1:0 CTRL_CLK I2C CLK Pad Drive Strength (default=3)
  172. // (all use same table as I2S_LRCLK)
  173. #define CHIP_ANA_ADC_CTRL 0x0020
  174. // 8 ADC_VOL_M6DB ADC Volume Range Reduction
  175. // This bit shifts both right and left analog ADC volume
  176. // range down by 6.0 dB.
  177. // 0x0 = No change in ADC range
  178. // 0x1 = ADC range reduced by 6.0 dB
  179. // 7:4 ADC_VOL_RIGHT ADC Right Channel Volume
  180. // Right channel analog ADC volume control in 1.5 dB steps.
  181. // 0x0 = 0 dB
  182. // 0x1 = +1.5 dB
  183. // ...
  184. // 0xF = +22.5 dB
  185. // This range is -6.0 dB to +16.5 dB if ADC_VOL_M6DB is set to 1.
  186. // 3:0 ADC_VOL_LEFT ADC Left Channel Volume
  187. // (same scale as ADC_VOL_RIGHT)
  188. #define CHIP_ANA_HP_CTRL 0x0022
  189. // 14:8 HP_VOL_RIGHT Headphone Right Channel Volume (default 0x18)
  190. // Right channel headphone volume control with 0.5 dB steps.
  191. // 0x00 = +12 dB
  192. // 0x01 = +11.5 dB
  193. // 0x18 = 0 dB
  194. // ...
  195. // 0x7F = -51.5 dB
  196. // 6:0 HP_VOL_LEFT Headphone Left Channel Volume (default 0x18)
  197. // (same scale as HP_VOL_RIGHT)
  198. #define CHIP_ANA_CTRL 0x0024
  199. // 8 MUTE_LO LINEOUT Mute, 0 = Unmute, 1 = Mute (default 1)
  200. // 6 SELECT_HP Select the headphone input, 0 = DAC, 1 = LINEIN
  201. // 5 EN_ZCD_HP Enable the headphone zero cross detector (ZCD)
  202. // 0x0 = HP ZCD disabled
  203. // 0x1 = HP ZCD enabled
  204. // 4 MUTE_HP Mute the headphone outputs, 0 = Unmute, 1 = Mute (default)
  205. // 2 SELECT_ADC Select the ADC input, 0 = Microphone, 1 = LINEIN
  206. // 1 EN_ZCD_ADC Enable the ADC analog zero cross detector (ZCD)
  207. // 0x0 = ADC ZCD disabled
  208. // 0x1 = ADC ZCD enabled
  209. // 0 MUTE_ADC Mute the ADC analog volume, 0 = Unmute, 1 = Mute (default)
  210. #define CHIP_LINREG_CTRL 0x0026
  211. // 6 VDDC_MAN_ASSN Determines chargepump source when VDDC_ASSN_OVRD is set.
  212. // 0x0 = VDDA
  213. // 0x1 = VDDIO
  214. // 5 VDDC_ASSN_OVRD Charge pump Source Assignment Override
  215. // 0x0 = Charge pump source is automatically assigned based
  216. // on higher of VDDA and VDDIO
  217. // 0x1 = the source of charge pump is manually assigned by
  218. // VDDC_MAN_ASSN If VDDIO and VDDA are both the same
  219. // and greater than 3.1 V, VDDC_ASSN_OVRD and
  220. // VDDC_MAN_ASSN should be used to manually assign
  221. // VDDIO as the source for charge pump.
  222. // 3:0 D_PROGRAMMING Sets the VDDD linear regulator output voltage in 50 mV steps.
  223. // Must clear the LINREG_SIMPLE_POWERUP and STARTUP_POWERUP bits
  224. // in the 0x0030 (CHIP_ANA_POWER) register after power-up, for
  225. // this setting to produce the proper VDDD voltage.
  226. // 0x0 = 1.60
  227. // 0xF = 0.85
  228. #define CHIP_REF_CTRL 0x0028 // bandgap reference bias voltage and currents
  229. // 8:4 VAG_VAL Analog Ground Voltage Control
  230. // These bits control the analog ground voltage in 25 mV steps.
  231. // This should usually be set to VDDA/2 or lower for best
  232. // performance (maximum output swing at minimum THD). This VAG
  233. // reference is also used for the DAC and ADC voltage reference.
  234. // So changing this voltage scales the output swing of the DAC
  235. // and the output signal of the ADC.
  236. // 0x00 = 0.800 V
  237. // 0x1F = 1.575 V
  238. // 3:1 BIAS_CTRL Bias control
  239. // These bits adjust the bias currents for all of the analog
  240. // blocks. By lowering the bias current a lower quiescent power
  241. // is achieved. It should be noted that this mode can affect
  242. // performance by 3-4 dB.
  243. // 0x0 = Nominal
  244. // 0x1-0x3=+12.5%
  245. // 0x4=-12.5%
  246. // 0x5=-25%
  247. // 0x6=-37.5%
  248. // 0x7=-50%
  249. // 0 SMALL_POP VAG Ramp Control
  250. // Setting this bit slows down the VAG ramp from ~200 to ~400 ms
  251. // to reduce the startup pop, but increases the turn on/off time.
  252. // 0x0 = Normal VAG ramp
  253. // 0x1 = Slow down VAG ramp
  254. #define CHIP_MIC_CTRL 0x002A // microphone gain & internal microphone bias
  255. // 9:8 BIAS_RESISTOR MIC Bias Output Impedance Adjustment
  256. // Controls an adjustable output impedance for the microphone bias.
  257. // If this is set to zero the micbias block is powered off and
  258. // the output is highZ.
  259. // 0x0 = Powered off
  260. // 0x1 = 2.0 kohm
  261. // 0x2 = 4.0 kohm
  262. // 0x3 = 8.0 kohm
  263. // 6:4 BIAS_VOLT MIC Bias Voltage Adjustment
  264. // Controls an adjustable bias voltage for the microphone bias
  265. // amp in 250 mV steps. This bias voltage setting should be no
  266. // more than VDDA-200 mV for adequate power supply rejection.
  267. // 0x0 = 1.25 V
  268. // ...
  269. // 0x7 = 3.00 V
  270. // 1:0 GAIN MIC Amplifier Gain
  271. // Sets the microphone amplifier gain. At 0 dB setting the THD
  272. // can be slightly higher than other paths- typically around
  273. // ~65 dB. At other gain settings the THD are better.
  274. // 0x0 = 0 dB
  275. // 0x1 = +20 dB
  276. // 0x2 = +30 dB
  277. // 0x3 = +40 dB
  278. #define CHIP_LINE_OUT_CTRL 0x002C
  279. // 11:8 OUT_CURRENT Controls the output bias current for the LINEOUT amplifiers. The
  280. // nominal recommended setting for a 10 kohm load with 1.0 nF load cap
  281. // is 0x3. There are only 5 valid settings.
  282. // 0x0=0.18 mA
  283. // 0x1=0.27 mA
  284. // 0x3=0.36 mA
  285. // 0x7=0.45 mA
  286. // 0xF=0.54 mA
  287. // 5:0 LO_VAGCNTRL LINEOUT Amplifier Analog Ground Voltage
  288. // Controls the analog ground voltage for the LINEOUT amplifiers
  289. // in 25 mV steps. This should usually be set to VDDIO/2.
  290. // 0x00 = 0.800 V
  291. // ...
  292. // 0x1F = 1.575 V
  293. // ...
  294. // 0x23 = 1.675 V
  295. // 0x24-0x3F are invalid
  296. #define CHIP_LINE_OUT_VOL 0x002E
  297. // 12:8 LO_VOL_RIGHT LINEOUT Right Channel Volume (default=4)
  298. // Controls the right channel LINEOUT volume in 0.5 dB steps.
  299. // Higher codes have more attenuation.
  300. // 4:0 LO_VOL_LEFT LINEOUT Left Channel Output Level (default=4)
  301. // Used to normalize the output level of the left line output
  302. // to full scale based on the values used to set
  303. // LINE_OUT_CTRL->LO_VAGCNTRL and CHIP_REF_CTRL->VAG_VAL.
  304. // In general this field should be set to:
  305. // 40*log((VAG_VAL)/(LO_VAGCNTRL)) + 15
  306. // Suggested values based on typical VDDIO and VDDA voltages.
  307. // VDDA VAG_VAL VDDIO LO_VAGCNTRL LO_VOL_*
  308. // 1.8 V 0.9 3.3 V 1.55 0x06
  309. // 1.8 V 0.9 1.8 V 0.9 0x0F
  310. // 3.3 V 1.55 1.8 V 0.9 0x19
  311. // 3.3 V 1.55 3.3 V 1.55 0x0F
  312. // After setting to the nominal voltage, this field can be used
  313. // to adjust the output level in +/-0.5 dB increments by using
  314. // values higher or lower than the nominal setting.
  315. #define CHIP_ANA_POWER 0x0030 // power down controls for the analog blocks.
  316. // The only other power-down controls are BIAS_RESISTOR in the MIC_CTRL register
  317. // and the EN_ZCD control bits in ANA_CTRL.
  318. // 14 DAC_MONO While DAC_POWERUP is set, this allows the DAC to be put into left only
  319. // mono operation for power savings. 0=mono, 1=stereo (default)
  320. // 13 LINREG_SIMPLE_POWERUP Power up the simple (low power) digital supply regulator.
  321. // After reset, this bit can be cleared IF VDDD is driven
  322. // externally OR the primary digital linreg is enabled with
  323. // LINREG_D_POWERUP
  324. // 12 STARTUP_POWERUP Power up the circuitry needed during the power up ramp and reset.
  325. // After reset this bit can be cleared if VDDD is coming from
  326. // an external source.
  327. // 11 VDDC_CHRGPMP_POWERUP Power up the VDDC charge pump block. If neither VDDA or VDDIO
  328. // is 3.0 V or larger this bit should be cleared before analog
  329. // blocks are powered up.
  330. // 10 PLL_POWERUP PLL Power Up, 0 = Power down, 1 = Power up
  331. // When cleared, the PLL is turned off. This must be set before
  332. // CHIP_CLK_CTRL->MCLK_FREQ is programmed to 0x3. The
  333. // CHIP_PLL_CTRL register must be configured correctly before
  334. // setting this bit.
  335. // 9 LINREG_D_POWERUP Power up the primary VDDD linear regulator, 0 = Power down, 1 = Power up
  336. // 8 VCOAMP_POWERUP Power up the PLL VCO amplifier, 0 = Power down, 1 = Power up
  337. // 7 VAG_POWERUP Power up the VAG reference buffer.
  338. // Setting this bit starts the power up ramp for the headphone
  339. // and LINEOUT. The headphone (and/or LINEOUT) powerup should
  340. // be set BEFORE clearing this bit. When this bit is cleared
  341. // the power-down ramp is started. The headphone (and/or LINEOUT)
  342. // powerup should stay set until the VAG is fully ramped down
  343. // (200 to 400 ms after clearing this bit).
  344. // 0x0 = Power down, 0x1 = Power up
  345. // 6 ADC_MONO While ADC_POWERUP is set, this allows the ADC to be put into left only
  346. // mono operation for power savings. This mode is useful when
  347. // only using the microphone input.
  348. // 0x0 = Mono (left only), 0x1 = Stereo
  349. // 5 REFTOP_POWERUP Power up the reference bias currents
  350. // 0x0 = Power down, 0x1 = Power up
  351. // This bit can be cleared when the part is a sleep state
  352. // to minimize analog power.
  353. // 4 HEADPHONE_POWERUP Power up the headphone amplifiers
  354. // 0x0 = Power down, 0x1 = Power up
  355. // 3 DAC_POWERUP Power up the DACs
  356. // 0x0 = Power down, 0x1 = Power up
  357. // 2 CAPLESS_HEADPHONE_POWERUP Power up the capless headphone mode
  358. // 0x0 = Power down, 0x1 = Power up
  359. // 1 ADC_POWERUP Power up the ADCs
  360. // 0x0 = Power down, 0x1 = Power up
  361. // 0 LINEOUT_POWERUP Power up the LINEOUT amplifiers
  362. // 0x0 = Power down, 0x1 = Power up
  363. #define CHIP_PLL_CTRL 0x0032
  364. // 15:11 INT_DIVISOR
  365. // 10:0 FRAC_DIVISOR
  366. #define CHIP_CLK_TOP_CTRL 0x0034
  367. // 11 ENABLE_INT_OSC Setting this bit enables an internal oscillator to be used for the
  368. // zero cross detectors, the short detect recovery, and the
  369. // charge pump. This allows the I2S clock to be shut off while
  370. // still operating an analog signal path. This bit can be kept
  371. // on when the I2S clock is enabled, but the I2S clock is more
  372. // accurate so it is preferred to clear this bit when I2S is present.
  373. // 3 INPUT_FREQ_DIV2 SYS_MCLK divider before PLL input
  374. // 0x0 = pass through
  375. // 0x1 = SYS_MCLK is divided by 2 before entering PLL
  376. // This must be set when the input clock is above 17 Mhz. This
  377. // has no effect when the PLL is powered down.
  378. #define CHIP_ANA_STATUS 0x0036
  379. // 9 LRSHORT_STS This bit is high whenever a short is detected on the left or right
  380. // channel headphone drivers.
  381. // 8 CSHORT_STS This bit is high whenever a short is detected on the capless headphone
  382. // common/center channel driver.
  383. // 4 PLL_IS_LOCKED This bit goes high after the PLL is locked.
  384. #define CHIP_ANA_TEST1 0x0038 // intended only for debug.
  385. #define CHIP_ANA_TEST2 0x003A // intended only for debug.
  386. #define CHIP_SHORT_CTRL 0x003C
  387. // 14:12 LVLADJR Right channel headphone short detector in 25 mA steps.
  388. // 0x3=25 mA
  389. // 0x2=50 mA
  390. // 0x1=75 mA
  391. // 0x0=100 mA
  392. // 0x4=125 mA
  393. // 0x5=150 mA
  394. // 0x6=175 mA
  395. // 0x7=200 mA
  396. // This trip point can vary by ~30% over process so leave plenty
  397. // of guard band to avoid false trips. This short detect trip
  398. // point is also effected by the bias current adjustments made
  399. // by CHIP_REF_CTRL->BIAS_CTRL and by CHIP_ANA_TEST1->HP_IALL_ADJ.
  400. // 10:8 LVLADJL Left channel headphone short detector in 25 mA steps.
  401. // (same scale as LVLADJR)
  402. // 6:4 LVLADJC Capless headphone center channel short detector in 50 mA steps.
  403. // 0x3=50 mA
  404. // 0x2=100 mA
  405. // 0x1=150 mA
  406. // 0x0=200 mA
  407. // 0x4=250 mA
  408. // 0x5=300 mA
  409. // 0x6=350 mA
  410. // 0x7=400 mA
  411. // 3:2 MODE_LR Behavior of left/right short detection
  412. // 0x0 = Disable short detector, reset short detect latch,
  413. // software view non-latched short signal
  414. // 0x1 = Enable short detector and reset the latch at timeout
  415. // (every ~50 ms)
  416. // 0x2 = This mode is not used/invalid
  417. // 0x3 = Enable short detector with only manual reset (have
  418. // to return to 0x0 to reset the latch)
  419. // 1:0 MODE_CM Behavior of capless headphone central short detection
  420. // (same settings as MODE_LR)
  421. #define DAP_CONTROL 0x0100
  422. #define DAP_PEQ 0x0102
  423. #define DAP_BASS_ENHANCE 0x0104
  424. #define DAP_BASS_ENHANCE_CTRL 0x0106
  425. #define DAP_AUDIO_EQ 0x0108
  426. #define DAP_SGTL_SURROUND 0x010A
  427. #define DAP_FILTER_COEF_ACCESS 0x010C
  428. #define DAP_COEF_WR_B0_MSB 0x010E
  429. #define DAP_COEF_WR_B0_LSB 0x0110
  430. #define DAP_AUDIO_EQ_BASS_BAND0 0x0116 // 115 Hz
  431. #define DAP_AUDIO_EQ_BAND1 0x0118 // 330 Hz
  432. #define DAP_AUDIO_EQ_BAND2 0x011A // 990 Hz
  433. #define DAP_AUDIO_EQ_BAND3 0x011C // 3000 Hz
  434. #define DAP_AUDIO_EQ_TREBLE_BAND4 0x011E // 9900 Hz
  435. #define DAP_MAIN_CHAN 0x0120
  436. #define DAP_MIX_CHAN 0x0122
  437. #define DAP_AVC_CTRL 0x0124
  438. #define DAP_AVC_THRESHOLD 0x0126
  439. #define DAP_AVC_ATTACK 0x0128
  440. #define DAP_AVC_DECAY 0x012A
  441. #define DAP_COEF_WR_B1_MSB 0x012C
  442. #define DAP_COEF_WR_B1_LSB 0x012E
  443. #define DAP_COEF_WR_B2_MSB 0x0130
  444. #define DAP_COEF_WR_B2_LSB 0x0132
  445. #define DAP_COEF_WR_A1_MSB 0x0134
  446. #define DAP_COEF_WR_A1_LSB 0x0136
  447. #define DAP_COEF_WR_A2_MSB 0x0138
  448. #define DAP_COEF_WR_A2_LSB 0x013A
  449. #define SGTL5000_I2C_ADDR 0x0A // CTRL_ADR0_CS pin low (normal configuration)
  450. //#define SGTL5000_I2C_ADDR 0x2A // CTRL_ADR0_CS pin high
  451. bool AudioControlSGTL5000::enable(void)
  452. {
  453. //unsigned int n;
  454. muted = true;
  455. Wire.begin();
  456. delay(5);
  457. //Serial.print("chip ID = ");
  458. //delay(5);
  459. //n = read(CHIP_ID);
  460. //Serial.println(n, HEX);
  461. write(CHIP_ANA_POWER, 0x4060); // VDDD is externally driven with 1.8V
  462. write(CHIP_LINREG_CTRL, 0x006C); // VDDA & VDDIO both over 3.1V
  463. write(CHIP_REF_CTRL, 0x01F1); // VAG=1.575 slow ramp, normal bias current
  464. write(CHIP_LINE_OUT_CTRL, 0x0322); // LO_VAGCNTRL=1.65V, OUT_CURRENT=0.36mA
  465. write(CHIP_SHORT_CTRL, 0x4446); // allow up to 125mA
  466. write(CHIP_ANA_CTRL, 0x0137); // enable zero cross detectors
  467. write(CHIP_ANA_POWER, 0x40FF); // power up: lineout, hp, adc, dac
  468. write(CHIP_DIG_POWER, 0x0073); // power up all digital stuff
  469. delay(400);
  470. // 40*log((1.575)/(1.65)) + 15 = 13.1391993746043 but it seems wrong, 5 is better...
  471. write(CHIP_LINE_OUT_VOL, 0x0505); // TODO: correct value for 3.3V
  472. write(CHIP_CLK_CTRL, 0x0004); // 44.1 kHz, 256*Fs
  473. write(CHIP_I2S_CTRL, 0x0130); // SCLK=32*Fs, 16bit, I2S format
  474. // default signal routing is ok?
  475. write(CHIP_SSS_CTRL, 0x0010); // ADC->I2S, I2S->DAC
  476. write(CHIP_ADCDAC_CTRL, 0x0000); // disable dac mute
  477. write(CHIP_DAC_VOL, 0x3C3C); // digital gain, 0dB
  478. write(CHIP_ANA_HP_CTRL, 0x7F7F); // set volume (lowest level)
  479. write(CHIP_ANA_CTRL, 0x0136); // enable zero cross detectors
  480. //mute = false;
  481. return true;
  482. }
  483. unsigned int AudioControlSGTL5000::read(unsigned int reg)
  484. {
  485. unsigned int val;
  486. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  487. Wire.write(reg >> 8);
  488. Wire.write(reg);
  489. if (Wire.endTransmission(false) != 0) return 0;
  490. if (Wire.requestFrom(SGTL5000_I2C_ADDR, 2) < 2) return 0;
  491. val = Wire.read() << 8;
  492. val |= Wire.read();
  493. return val;
  494. }
  495. bool AudioControlSGTL5000::write(unsigned int reg, unsigned int val)
  496. {
  497. if (reg == CHIP_ANA_CTRL) ana_ctrl = val;
  498. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  499. Wire.write(reg >> 8);
  500. Wire.write(reg);
  501. Wire.write(val >> 8);
  502. Wire.write(val);
  503. if (Wire.endTransmission() == 0) return true;
  504. return false;
  505. }
  506. unsigned int AudioControlSGTL5000::modify(unsigned int reg, unsigned int val, unsigned int iMask)
  507. {
  508. unsigned int val1 = (read(reg)&(~iMask))|val;
  509. if(!write(reg,val1)) return 0;
  510. return val1;
  511. }
  512. bool AudioControlSGTL5000::volumeInteger(unsigned int n)
  513. {
  514. if (n == 0) {
  515. muted = true;
  516. write(CHIP_ANA_HP_CTRL, 0x7F7F);
  517. return muteHeadphone();
  518. } else if (n > 0x80) {
  519. n = 0;
  520. } else {
  521. n = 0x80 - n;
  522. }
  523. if (muted) {
  524. muted = false;
  525. unmuteHeadphone();
  526. }
  527. n = n | (n << 8);
  528. return write(CHIP_ANA_HP_CTRL, n); // set volume
  529. }
  530. bool AudioControlSGTL5000::volume(float left, float right)
  531. {
  532. unsigned short m=((0x7F-calcVol(right,0x7F))<<8)|(0x7F-calcVol(left,0x7F));
  533. return write(CHIP_ANA_HP_CTRL, m);
  534. }
  535. // CHIP_LINE_OUT_VOL
  536. unsigned short AudioControlSGTL5000::lo_lvl(uint8_t n)
  537. {
  538. n&=31;
  539. return modify(CHIP_LINE_OUT_VOL,(n<<8)|n,(31<<8)|31);
  540. }
  541. unsigned short AudioControlSGTL5000::lo_lvl(uint8_t left, uint8_t right)
  542. {
  543. left&=31;
  544. right&=31;
  545. return modify(CHIP_LINE_OUT_VOL,(right<<8)|left,(31<<8)|31);
  546. }
  547. unsigned short AudioControlSGTL5000::dac_vol(float n) // set both directly
  548. {
  549. if(read(CHIP_ADCDAC_CTRL)&(3<<2)!=((n>0 ? 0:3)<<2)) modify(CHIP_ADCDAC_CTRL,(n>0 ? 0:3)<<2,3<<2);
  550. unsigned char m=calcVol(n,0xC0);
  551. return modify(CHIP_DAC_VOL,((0xFC-m)<<8)|(0xFC-m),65535);
  552. }
  553. unsigned short AudioControlSGTL5000::dac_vol(float left, float right)
  554. {
  555. unsigned short adcdac=((right>0 ? 0:2)|(left>0 ? 0:1))<<2;
  556. if(read(CHIP_ADCDAC_CTRL)&(3<<2)!=adcdac) modify(CHIP_ADCDAC_CTRL,adcdac,1<<2);
  557. unsigned short m=(0xFC-calcVol(right,0xC0))<<8|(0xFC-calcVol(left,0xC0));
  558. return modify(CHIP_DAC_VOL,m,65535);
  559. }
  560. // DAP_CONTROL
  561. unsigned short AudioControlSGTL5000::dap_mix_enable(uint8_t n)
  562. {
  563. return modify(DAP_CONTROL,(n&1)<<4,1<<4);
  564. }
  565. unsigned short AudioControlSGTL5000::dap_enable(uint8_t n)
  566. {
  567. if(n) n=1;
  568. unsigned char DAC=1+(2*n); // I2S_IN if n==0 else DAP
  569. modify(DAP_CONTROL,n,1);
  570. return modify(CHIP_SSS_CTRL,(0<<6)|(DAC<<4),(3<<6)|(3<<4));
  571. }
  572. unsigned short AudioControlSGTL5000::dap_enable(void)
  573. {
  574. return dap_enable(1);
  575. }
  576. // DAP_PEQ
  577. unsigned short AudioControlSGTL5000::dap_peqs(uint8_t n) // valid to n&7, 0 thru 7 filters enabled.
  578. {
  579. return modify(DAP_PEQ,(n&7),7);
  580. }
  581. // DAP_AUDIO_EQ
  582. unsigned short AudioControlSGTL5000::dap_audio_eq(uint8_t n) // 0=NONE, 1=PEQ (7 IIR Biquad filters), 2=TONE (tone), 3=GEQ (5 band EQ)
  583. {
  584. return modify(DAP_AUDIO_EQ,n&3,3);
  585. }
  586. // DAP_AUDIO_EQ_BASS_BAND0 & DAP_AUDIO_EQ_BAND1 & DAP_AUDIO_EQ_BAND2 etc etc
  587. unsigned short AudioControlSGTL5000::dap_audio_eq_band(uint8_t bandNum, float n) // by signed percentage -100/+100; dap_audio_eq(3);
  588. { // 0x00==-12dB, 0x2F==0dB, 0x5F==12dB
  589. n=((n/100)*48)+0.499;
  590. if(n<-47) n=-47;
  591. if(n>48) n=48;
  592. n+=47;
  593. return modify(DAP_AUDIO_EQ_BASS_BAND0+(bandNum*2),(unsigned int)n,127);
  594. }
  595. void AudioControlSGTL5000::dap_audio_eq_geq(float bass, float mid_bass, float midrange, float mid_treble, float treble)
  596. {
  597. dap_audio_eq_band(0,bass);
  598. dap_audio_eq_band(1,mid_bass);
  599. dap_audio_eq_band(2,midrange);
  600. dap_audio_eq_band(3,mid_treble);
  601. dap_audio_eq_band(4,treble);
  602. }
  603. void AudioControlSGTL5000::dap_audio_eq_tone(float bass, float treble) // dap_audio_eq(2);
  604. {
  605. dap_audio_eq_band(0,bass);
  606. dap_audio_eq_band(4,treble);
  607. }
  608. // SGTL5000 PEQ Coefficient loader
  609. void AudioControlSGTL5000::load_peq(uint8_t filterNum, int *filterParameters)
  610. {
  611. // 1111 11111111 11111111
  612. write(DAP_COEF_WR_B0_MSB,(*filterParameters>>4)&65535);
  613. write(DAP_COEF_WR_B0_LSB,(*filterParameters++)&15);
  614. write(DAP_COEF_WR_B1_MSB,(*filterParameters>>4)&65535);
  615. write(DAP_COEF_WR_B1_LSB,(*filterParameters++)&15);
  616. write(DAP_COEF_WR_B2_MSB,(*filterParameters>>4)&65535);
  617. write(DAP_COEF_WR_B2_LSB,(*filterParameters++)&15);
  618. write(DAP_COEF_WR_A1_MSB,(*filterParameters>>4)&65535);
  619. write(DAP_COEF_WR_A1_LSB,(*filterParameters++)&15);
  620. write(DAP_COEF_WR_A2_MSB,(*filterParameters>>4)&65535);
  621. write(DAP_COEF_WR_A2_LSB,(*filterParameters++)&15);
  622. write(DAP_FILTER_COEF_ACCESS,(uint16_t)0x100|filterNum);
  623. delay(10); // seems necessary, didn't work for 1ms.
  624. modify(DAP_FILTER_COEF_ACCESS,(uint16_t)filterNum,15);
  625. }
  626. unsigned char AudioControlSGTL5000::calcVol(float n, unsigned char range)
  627. {
  628. n=(n*(((float)range)/100))+0.499;
  629. if ((unsigned char)n>range) n=range;
  630. return (unsigned char)n;
  631. }
  632. // if(SGTL5000_PEQ) quantization_unit=524288; if(AudioFilterBiquad) quantization_unit=2147483648;
  633. void calcBiquad(uint8_t filtertype, float fC, float dB_Gain, float Q, uint32_t quantization_unit, uint32_t fS, int *coef)
  634. {
  635. // I used resources like http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  636. // to make this routine, I tested most of the filter types and they worked. Such filters have limits and
  637. // before calling this routine with varying values the end user should check that those values are limited
  638. // to valid results.
  639. float A;
  640. if(filtertype<FILTER_PARAEQ) A=pow(10,dB_Gain/20); else A=pow(10,dB_Gain/40);
  641. float W0 = 2*3.14159265358979323846*fC/fS;
  642. float cosw=cos(W0);
  643. float sinw=sin(W0);
  644. //float alpha = sinw*sinh((log(2)/2)*BW*W0/sinw);
  645. //float beta = sqrt(2*A);
  646. float alpha = sinw / (2 * Q);
  647. float beta = sqrt(A)/Q;
  648. float b0,b1,b2,a0,a1,a2;
  649. switch(filtertype) {
  650. case FILTER_LOPASS:
  651. b0 = (1.0F - cosw) * 0.5F; // =(1-COS($H$2))/2
  652. b1 = 1.0F - cosw;
  653. b2 = (1.0F - cosw) * 0.5F;
  654. a0 = 1.0F + alpha;
  655. a1 = 2.0F * cosw;
  656. a2 = alpha - 1.0F;
  657. break;
  658. case FILTER_HIPASS:
  659. b0 = (1.0F + cosw) * 0.5F;
  660. b1 = -(cosw + 1.0F);
  661. b2 = (1.0F + cosw) * 0.5F;
  662. a0 = 1.0F + alpha;
  663. a1 = 2.0F * cosw;
  664. a2 = alpha - 1.0F;
  665. break;
  666. case FILTER_BANDPASS:
  667. b0 = alpha;
  668. b1 = 0.0F;
  669. b2 = -alpha;
  670. a0 = 1.0F + alpha;
  671. a1 = 2.0F * cosw;
  672. a2 = alpha - 1.0F;
  673. break;
  674. case FILTER_NOTCH:
  675. b0=1;
  676. b1=-2*cosw;
  677. b2=1;
  678. a0=1+alpha;
  679. a1=2*cosw;
  680. a2=-(1-alpha);
  681. break;
  682. case FILTER_PARAEQ:
  683. b0 = 1 + (alpha*A);
  684. b1 =-2 * cosw;
  685. b2 = 1 - (alpha*A);
  686. a0 = 1 + (alpha/A);
  687. a1 = 2 * cosw;
  688. a2 =-(1-(alpha/A));
  689. break;
  690. case FILTER_LOSHELF:
  691. b0 = A * ((A+1.0F) - ((A-1.0F)*cosw) + (beta*sinw));
  692. b1 = 2.0F * A * ((A-1.0F) - ((A+1.0F)*cosw));
  693. b2 = A * ((A+1.0F) - ((A-1.0F)*cosw) - (beta*sinw));
  694. a0 = (A+1.0F) + ((A-1.0F)*cosw) + (beta*sinw);
  695. a1 = 2.0F * ((A-1.0F) + ((A+1.0F)*cosw));
  696. a2 = -((A+1.0F) + ((A-1.0F)*cosw) - (beta*sinw));
  697. break;
  698. case FILTER_HISHELF:
  699. b0 = A * ((A+1.0F) + ((A-1.0F)*cosw) + (beta*sinw));
  700. b1 = -2.0F * A * ((A-1.0F) + ((A+1.0F)*cosw));
  701. b2 = A * ((A+1.0F) + ((A-1.0F)*cosw) - (beta*sinw));
  702. a0 = (A+1.0F) - ((A-1.0F)*cosw) + (beta*sinw);
  703. a1 = -2.0F * ((A-1.0F) - ((A+1.0F)*cosw));
  704. a2 = -((A+1.0F) - ((A-1.0F)*cosw) - (beta*sinw));
  705. }
  706. a0=(a0*2)/(float)quantization_unit; // once here instead of five times there...
  707. b0/=a0;
  708. *coef++=(int)(b0+0.499);
  709. b1/=a0;
  710. *coef++=(int)(b1+0.499);
  711. b2/=a0;
  712. *coef++=(int)(b2+0.499);
  713. a1/=a0;
  714. *coef++=(int)(a1+0.499);
  715. a2/=a0;
  716. *coef++=(int)(a2+0.499);
  717. }