├── .gitattributes ├── README.md └── soft ├── control_cs4272.h └── control_cs4272.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 4HP usb eurorack audio interface 2 | === 3 | 4 | ![My image](https://c2.staticflickr.com/2/1956/43531137870_a26c21bc7c_h.jpg) 5 | 6 | ### hardware: 7 | - MK20 / CS4272 / INA134 / THS4521 / OPA1662 8 | - w/ (optional) USB isolator 9 | - DC-coupled 10 | -------------------------------------------------------------------------------- /soft/control_cs4272.h: -------------------------------------------------------------------------------- 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 | 30 | #ifndef control_cs4272_h_ 31 | #define control_cs4272_h_ 32 | 33 | #include "AudioControl.h" 34 | 35 | class AudioControlCS4272 : public AudioControl 36 | { 37 | public: 38 | bool enable(void); 39 | bool disable(void) { return false; } 40 | bool volume(float n) { return volumeInteger(n * 127 + 0.499); } 41 | bool inputLevel(float n) { return false; } 42 | bool inputSelect(int n) { return false; } 43 | 44 | bool volume(float left, float right); 45 | bool dacVolume(float n) { return volumeInteger(n * 127 + 0.499); } 46 | bool dacVolume(float left, float right); 47 | 48 | bool muteOutput(void); 49 | bool unmuteOutput(void); 50 | 51 | bool muteInput(void); 52 | bool unmuteInput(void); 53 | 54 | bool enableDither(void); 55 | bool disableDither(void); 56 | 57 | protected: 58 | bool write(unsigned int reg, unsigned int val); 59 | bool volumeInteger(unsigned int n); // range: 0x0 to 0x7F 60 | 61 | uint8_t regLocal[8]; 62 | 63 | void initLocalRegs(void); 64 | }; 65 | 66 | // For sample rate ratio select (only single speed tested) 67 | #define CS4272_RATIO_SINGLE 0 68 | #define CS4272_RATIO_DOUBLE 2 69 | #define CS4272_RATIO_QUAD 3 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /soft/control_cs4272.cpp: -------------------------------------------------------------------------------- 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 | 30 | #include 31 | #include "control_cs4272.h" 32 | #include "i2c_t3.h" 33 | 34 | #define CS4272_ADDR 0x10 35 | 36 | // Section 8.1 Mode Control 37 | #define CS4272_MODE_CONTROL (uint8_t)0x01 38 | #define CS4272_MC_FUNC_MODE(x) (uint8_t)(((x) & 0x03) << 6) 39 | #define CS4272_MC_RATIO_SEL(x) (uint8_t)(((x) & 0x03) << 4) 40 | #define CS4272_MC_MASTER_SLAVE (uint8_t)0x00 // (uint8_t)0x08 // 0x08 = Master 41 | #define CS4272_MC_SERIAL_FORMAT(x) (uint8_t)(((x) & 0x07) << 0) 42 | 43 | // Section 8.2 DAC Control 44 | #define CS4272_DAC_CONTROL (uint8_t)0x02 45 | #define CS4272_DAC_CTRL_AUTO_MUTE (uint8_t)0x80 46 | #define CS4272_DAC_CTRL_FILTER_SEL (uint8_t)0x40 47 | #define CS4272_DAC_CTRL_DE_EMPHASIS(x) (uint8_t)(((x) & 0x03) << 4) 48 | #define CS4272_DAC_CTRL_VOL_RAMP_UP (uint8_t)0x08 49 | #define CS4272_DAC_CTRL_VOL_RAMP_DN (uint8_t)0x04 50 | #define CS4272_DAC_CTRL_INV_POL(x) (uint8_t)(((x) & 0x03) << 0) 51 | 52 | // Section 8.3 DAC Volume and Mixing 53 | #define CS4272_DAC_VOL (uint8_t)0x03 54 | #define CS4272_DAC_VOL_CH_VOL_TRACKING (uint8_t)0x40 55 | #define CS4272_DAC_VOL_SOFT_RAMP(x) (uint8_t)(((x) & 0x03) << 4) 56 | #define CS4272_DAC_VOL_ATAPI(x) (uint8_t)(((x) & 0x0F) << 0) 57 | 58 | // Section 8.4 DAC Channel A volume 59 | #define CS4272_DAC_CHA_VOL (uint8_t)0x04 60 | #define CS4272_DAC_CHA_VOL_MUTE (uint8_t)0x80 61 | #define CS4272_DAC_CHA_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0) 62 | 63 | // Section 8.5 DAC Channel B volume 64 | #define CS4272_DAC_CHB_VOL (uint8_t)0x05 65 | #define CS4272_DAC_CHB_VOL_MUTE (uint8_t)0x80 66 | #define CS4272_DAC_CHB_VOL_VOLUME(x) (uint8_t)(((x) & 0x7F) << 0) 67 | 68 | // Section 8.6 ADC Control 69 | #define CS4272_ADC_CTRL (uint8_t)0x06 70 | #define CS4272_ADC_CTRL_DITHER (uint8_t)0x20 71 | #define CS4272_ADC_CTRL_SER_FORMAT (uint8_t)0x10 72 | #define CS4272_ADC_CTRL_MUTE(x) (uint8_t)(((x) & 0x03) << 2) 73 | #define CS4272_ADC_CTRL_HPF(x) (uint8_t)(((x) & 0x03) << 0) 74 | 75 | // Section 8.7 Mode Control 2 76 | #define CS4272_MODE_CTRL2 (uint8_t)0x07 77 | #define CS4272_MODE_CTRL2_LOOP (uint8_t)0x10 78 | #define CS4272_MODE_CTRL2_MUTE_TRACK (uint8_t)0x08 79 | #define CS4272_MODE_CTRL2_CTRL_FREEZE (uint8_t)0x04 80 | #define CS4272_MODE_CTRL2_CTRL_PORT_EN (uint8_t)0x02 81 | #define CS4272_MODE_CTRL2_POWER_DOWN (uint8_t)0x01 82 | 83 | // Section 8.8 Chip ID 84 | #define CS4272_CHIP_ID (uint8_t)0x08 85 | #define CS4272_CHIP_ID_PART(x) (uint8_t)(((x) & 0x0F) << 4) 86 | #define CS4272_CHIP_ID_REV(x) (uint8_t)(((x) & 0x0F) << 0) 87 | 88 | // using different reset pin ... 89 | #define CS4272_RESET_PIN 10 90 | 91 | bool AudioControlCS4272::enable(void) 92 | { 93 | delay(5); 94 | Wire1.begin(); 95 | delay(25); 96 | initLocalRegs(); 97 | 98 | // Setup Reset pin 99 | pinMode(CS4272_RESET_PIN, OUTPUT); 100 | 101 | // Drive pin low 102 | digitalWriteFast(CS4272_RESET_PIN, LOW); 103 | delay(1); 104 | 105 | // Release Reset 106 | digitalWriteFast(CS4272_RESET_PIN, HIGH); 107 | delay(2); 108 | 109 | // Set power down and control port enable as spec'd in the 110 | // datasheet for control port mode 111 | write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_POWER_DOWN 112 | | CS4272_MODE_CTRL2_CTRL_PORT_EN); 113 | 114 | // Wait for further setup 115 | delay(1); 116 | 117 | // Set ratio select for MCLK=512*LRCLK (BCLK = 64*LRCLK), and master mode 118 | //write(CS4272_MODE_CONTROL, CS4272_MC_RATIO_SEL(3) | CS4272_MC_MASTER_SLAVE); 119 | 120 | delay(10); 121 | 122 | // Release power down bit to start up codec 123 | // TODO: May need other bits set in this reg 124 | write(CS4272_MODE_CTRL2, CS4272_MODE_CTRL2_CTRL_PORT_EN); 125 | 126 | // Wait for everything to come up 127 | delay(10); 128 | 129 | 130 | return true; 131 | } 132 | 133 | bool AudioControlCS4272::volumeInteger(unsigned int n) 134 | { 135 | unsigned int val = 0x7F - (n & 0x7F); 136 | write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val)); 137 | write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val)); 138 | return true; 139 | } 140 | 141 | bool AudioControlCS4272::volume(float left, float right) 142 | { 143 | unsigned int leftInt,rightInt; 144 | 145 | leftInt = left*127 + 0.499; 146 | rightInt = right*127 + 0.499; 147 | 148 | unsigned int val = 0x7F - (leftInt & 0x7F); 149 | write(CS4272_DAC_CHA_VOL,CS4272_DAC_CHA_VOL_VOLUME(val)); 150 | 151 | val = 0x7F - (rightInt & 0x7F); 152 | write(CS4272_DAC_CHB_VOL,CS4272_DAC_CHB_VOL_VOLUME(val)); 153 | 154 | return true; 155 | } 156 | 157 | bool AudioControlCS4272::dacVolume(float left, float right) 158 | { 159 | return volume(left,right); 160 | } 161 | 162 | bool AudioControlCS4272::muteOutput(void) 163 | { 164 | write(CS4272_DAC_CHA_VOL, 165 | regLocal[CS4272_DAC_CHA_VOL] | CS4272_DAC_CHA_VOL_MUTE); 166 | 167 | write(CS4272_DAC_CHB_VOL, 168 | regLocal[CS4272_DAC_CHB_VOL] | CS4272_DAC_CHB_VOL_MUTE); 169 | 170 | return true; 171 | } 172 | 173 | bool AudioControlCS4272::unmuteOutput(void) 174 | { 175 | write(CS4272_DAC_CHA_VOL, 176 | regLocal[CS4272_DAC_CHA_VOL] & ~CS4272_DAC_CHA_VOL_MUTE); 177 | 178 | write(CS4272_DAC_CHB_VOL, 179 | regLocal[CS4272_DAC_CHB_VOL] & ~CS4272_DAC_CHB_VOL_MUTE); 180 | 181 | return true; 182 | } 183 | 184 | bool AudioControlCS4272::muteInput(void) 185 | { 186 | uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_MUTE(3); 187 | write(CS4272_ADC_CTRL,val); 188 | return true; 189 | } 190 | 191 | bool AudioControlCS4272::unmuteInput(void) 192 | { 193 | uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_MUTE(3); 194 | write(CS4272_ADC_CTRL,val); 195 | return true; 196 | } 197 | 198 | bool AudioControlCS4272::enableDither(void) 199 | { 200 | uint8_t val = regLocal[CS4272_ADC_CTRL] | CS4272_ADC_CTRL_DITHER; 201 | write(CS4272_ADC_CTRL,val); 202 | return true; 203 | } 204 | 205 | bool AudioControlCS4272::disableDither(void) 206 | { 207 | uint8_t val = regLocal[CS4272_ADC_CTRL] & ~CS4272_ADC_CTRL_DITHER; 208 | write(CS4272_ADC_CTRL,val); 209 | return true; 210 | } 211 | 212 | 213 | bool AudioControlCS4272::write(unsigned int reg, unsigned int val) 214 | { 215 | // Write local copy first 216 | if(reg > 7) 217 | return false; 218 | 219 | regLocal[reg] = val; 220 | 221 | Wire1.beginTransmission(CS4272_ADDR); 222 | Wire1.write(reg & 0xFF); 223 | Wire1.write(val & 0xFF); 224 | Wire1.endTransmission(); 225 | return true; 226 | } 227 | 228 | 229 | // Initialize local registers to CS4272 reset status 230 | void AudioControlCS4272::initLocalRegs(void) 231 | { 232 | regLocal[CS4272_MODE_CONTROL] = 0x00; 233 | regLocal[CS4272_DAC_CONTROL] = CS4272_DAC_CTRL_AUTO_MUTE; 234 | regLocal[CS4272_DAC_VOL] = CS4272_DAC_VOL_SOFT_RAMP(2) | CS4272_DAC_VOL_ATAPI(9); 235 | regLocal[CS4272_DAC_CHA_VOL] = 0x00; 236 | regLocal[CS4272_DAC_CHB_VOL] = 0x00; 237 | regLocal[CS4272_ADC_CTRL] = 0x00; 238 | regLocal[CS4272_MODE_CTRL2] = 0x00; 239 | } 240 | 241 | 242 | 243 | --------------------------------------------------------------------------------