|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
-
-
- #include <Arduino.h>
- #include "control_cs4272.h"
- #include "Wire.h"
-
- #define CS4272_ADDR 0x10
-
-
- #define CS4272_MODE_CONTROL (uint8_t)0x01
- #define CS4272_MC_FUNC_MODE(x) (uint8_t)(((x) & 0x03) << 6)
- #define CS4272_MC_RATIO_SEL(x) (uint8_t)(((x) & 0x03) << 4)
- #define CS4272_MC_MASTER_SLAVE (uint8_t)0x08
- #define CS4272_MC_SERIAL_FORMAT(x) (uint8_t)(((x) & 0x07) << 0)
-
-
- #define CS4272_DAC_CONTROL (uint8_t)0x02
- #define CS4272_DAC_CTRL_AUTO_MUTE (uint8_t)0x80
- #define CS4272_DAC_CTRL_FILTER_SEL (uint8_t)0x40
- #define CS4272_DAC_CTRL_DE_EMPHASIS(x) (uint8_t)(((x) & 0x03) << 4)
- #define CS4272_DAC_CTRL_VOL_RAMP_UP (uint8_t)0x08
- #define CS4272_DAC_CTRL_VOL_RAMP_DN (uint8_t)0x04
- #define CS4272_DAC_CTRL_INV_POL(x) (uint8_t)(((x) & 0x03) << 0)
-
-
- #define CS4272_DAC_VOL (uint8_t)0x03
- #define CS4272_DAC_VOL_CH_VOL_TRACKING (uint8_t)0x40
- #define CS4272_DAC_VOL_SOFT_RAMP(x) (uint8_t)(((x) & 0x03) << 4)
- #define CS4272_DAC_VOL_ATAPI(x) (uint8_t)(((x) & 0x0F) << 0)
-
-
- #define CS4272_DAC_CHA_VOL (uint8_t)0x04
- #define CS4272_DAC_CHA_VOL_MUTE (uint8_t)0x80
- #define CS4272_DAC_CHA_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0)
-
-
- #define CS4272_DAC_CHB_VOL (uint8_t)0x05
- #define CS4272_DAC_CHB_VOL_MUTE (uint8_t)0x80
- #define CS4272_DAC_CHB_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0)
-
-
- #define CS4272_ADC_CTRL (uint8_t)0x06
- #define CS4272_ADC_CTRL_DITHER (uint8_t)0x20
- #define CS4272_ADC_CTRL_SER_FORMAT (uint8_t)0x10
- #define CS4272_ADC_CTRL_MUTE(x) (uint8_t)(((x) & 0x03) << 2)
- #define CS4272_ADC_CTRL_HPF(x) (uint8_t)(((x) & 0x03) << 0)
-
-
- #define CS4272_MODE_CTRL2 (uint8_t)0x07
- #define CS4272_MODE_CTRL2_LOOP (uint8_t)0x10
- #define CS4272_MODE_CTRL2_MUTE_TRACK (uint8_t)0x08
- #define CS4272_MODE_CTRL2_CTRL_FREEZE (uint8_t)0x04
- #define CS4272_MODE_CTRL2_CTRL_PORT_EN (uint8_t)0x02
- #define CS4272_MODE_CTRL2_POWER_DOWN (uint8_t)0x01
-
-
- #define CS4272_CHIP_ID (uint8_t)0x08
- #define CS4272_CHIP_ID_PART(x) (uint8_t)(((x) & 0x0F) << 4)
- #define CS4272_CHIP_ID_REV(x) (uint8_t)(((x) & 0x0F) << 0)
-
-
- #define CS4272_RESET_PIN 2
-
- bool AudioControlCS4272::enable(void)
- {
- Wire.begin();
- delay(5);
- initLocalRegs();
-
-
- pinMode(CS4272_RESET_PIN, OUTPUT);
-
-
- digitalWriteFast(CS4272_RESET_PIN, LOW);
- delay(1);
-
-
- digitalWriteFast(CS4272_RESET_PIN, HIGH);
- delay(2);
-
-
-
- write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_POWER_DOWN
- | CS4272_MODE_CTRL2_CTRL_PORT_EN);
-
-
- delay(1);
-
-
- write(CS4272_MODE_CONTROL, CS4272_MC_RATIO_SEL(3) | CS4272_MC_MASTER_SLAVE);
-
- delay(10);
-
-
-
- write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_CTRL_PORT_EN);
-
-
- delay(10);
-
-
- return true;
- }
-
- bool AudioControlCS4272::volumeInteger(unsigned int n)
- {
- unsigned int val = 0x7F - (n & 0x7F);
- write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val));
- write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val));
- return true;
- }
-
- bool AudioControlCS4272::volume(float left, float right)
- {
- unsigned int leftInt,rightInt;
-
- leftInt = left*127 + 0.499;
- rightInt = right*127 + 0.499;
-
- unsigned int val = 0x7F - (leftInt & 0x7F);
- write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val));
-
- val = 0x7F - (rightInt & 0x7F);
- write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val));
-
- return true;
- }
-
- bool AudioControlCS4272::dacVolume(float left, float right)
- {
- return volume(left,right);
- }
-
- bool AudioControlCS4272::muteOutput(void)
- {
- write(CS4272_DAC_CHA_VOL,
- regLocal[CS4272_DAC_CHA_VOL] | CS4272_DAC_CHA_VOL_MUTE);
-
- write(CS4272_DAC_CHB_VOL,
- regLocal[CS4272_DAC_CHB_VOL] | CS4272_DAC_CHB_VOL_MUTE);
-
- return true;
- }
-
- bool AudioControlCS4272::unmuteOutput(void)
- {
- write(CS4272_DAC_CHA_VOL,
- regLocal[CS4272_DAC_CHA_VOL] & ~CS4272_DAC_CHA_VOL_MUTE);
-
- write(CS4272_DAC_CHB_VOL,
- regLocal[CS4272_DAC_CHB_VOL] & ~CS4272_DAC_CHB_VOL_MUTE);
-
- return true;
- }
-
- bool AudioControlCS4272::muteInput(void)
- {
- uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_MUTE(3);
- write(CS4272_ADC_CTRL,val);
- return true;
- }
-
- bool AudioControlCS4272::unmuteInput(void)
- {
- uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_MUTE(3);
- write(CS4272_ADC_CTRL,val);
- return true;
- }
-
- bool AudioControlCS4272::enableDither(void)
- {
- uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_DITHER;
- write(CS4272_ADC_CTRL,val);
- return true;
- }
-
- bool AudioControlCS4272::disableDither(void)
- {
- uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_DITHER;
- write(CS4272_ADC_CTRL,val);
- return true;
- }
-
-
- bool AudioControlCS4272::write(unsigned int reg, unsigned int val)
- {
-
- if(reg > 7)
- return false;
-
- regLocal[reg] = val;
-
- Wire.beginTransmission(CS4272_ADDR);
- Wire.write(reg & 0xFF);
- Wire.write(val & 0xFF);
- Wire.endTransmission();
- return true;
- }
-
-
-
- void AudioControlCS4272::initLocalRegs(void)
- {
- regLocal[CS4272_MODE_CONTROL] = 0x00;
- regLocal[CS4272_DAC_CONTROL] = CS4272_DAC_CTRL_AUTO_MUTE;
- regLocal[CS4272_DAC_VOL] = CS4272_DAC_VOL_SOFT_RAMP(2) | CS4272_DAC_VOL_ATAPI(9);
- regLocal[CS4272_DAC_CHA_VOL] = 0x00;
- regLocal[CS4272_DAC_CHB_VOL] = 0x00;
- regLocal[CS4272_ADC_CTRL] = 0x00;
- regLocal[CS4272_MODE_CTRL2] = 0x00;
- }
-
-
-
|