24 Commits

Author SHA1 Message Date
e2eaa1b5b1 readme update 2025-06-17 12:10:54 +02:00
3b63123c76 cpu overloading playing 8 streams at once 2025-06-16 01:31:09 +02:00
ecbd6ab3ee better audio with only a few glitches left 2025-06-15 02:52:54 +02:00
76f2e0ec28 waves playing again, still xruns 2025-06-13 02:35:31 +02:00
b78060aac2 debugging 2025-06-12 19:21:24 +02:00
c1e204953f dac working, wave playback broken WIP 2025-06-11 01:59:23 +02:00
ac9d4b5e6b more wave file parsing WIP -> weiter in OF 2025-06-06 11:41:35 +02:00
db463c5b8c wave file parser WIP 2025-06-06 10:22:29 +02:00
55bcb2c389 volume control added 2025-06-06 02:07:30 +02:00
8cd690edbd speaker 2025-06-05 17:49:04 +02:00
120a802840 speaker function added 2025-06-05 12:30:51 +02:00
e58b4ff968 pcb speakers working, turned off again 2025-06-04 10:22:59 +02:00
76ff73791a 8 simultaneous streams working, but playback has artifacts 2025-06-04 10:20:37 +02:00
15733e8b36 streaming max 4 stereo wave files at the same time 2025-06-04 02:24:28 +02:00
c3adf740bf ringbuffer skeleton added 2025-06-03 16:02:52 +02:00
a1efe3cc10 ui and sound together is almost ok but not quite there. occasional xruns 2025-06-03 01:16:31 +02:00
ef52d50b7c structuring code 2025-06-02 01:31:56 +02:00
c1ca96793c feierabend 2025-06-01 02:33:01 +02:00
a3a4fd4fb4 The beginnings of codec.h 2025-06-01 01:09:35 +02:00
a17cefb4ba Codec ok, transients are quite rounded. Maybe hardware, maybe codec settings. 2025-05-30 01:54:45 +02:00
4da1c8fa9d Codec ADC gain and DAC volume settings optimized, but not perfect yet 2025-05-29 18:47:41 +02:00
2ffd1997b9 I2S loopback working! 2025-05-29 18:06:39 +02:00
549fa21d1a Codec working in bypass mode. I2S not working yet. 2025-05-29 03:19:54 +02:00
cd1b0934aa introduced lookup table for led ring cw 2025-05-28 01:41:12 +02:00
11 changed files with 1418 additions and 144 deletions

View File

@ -9,7 +9,26 @@
- the board will go into bootloader mode and appear as USB thumb drive - the board will go into bootloader mode and appear as USB thumb drive
6. Copy the `soundcube-firmware.ino.uf2` file to the thumb drive and wait for the board to restart 6. Copy the `soundcube-firmware.ino.uf2` file to the thumb drive and wait for the board to restart
## config.txt
## SD Card contents
´´´
config.txt
click.wav
sound/
├─ 1.wav
├─ 2.wav
├─ 3.wav
├─ 4.wav
├─ 5.wav
├─ 6.wav
├─ 7.wav
├─ 8.wav
´´´
### config.txt
Json formatted config file. For now only edge led color working. Json formatted config file. For now only edge led color working.
@ -25,11 +44,38 @@ Json formatted config file. For now only edge led color working.
} }
``` ```
## click.wav (not played correctly) ### click.wav
Put a file called `click.wav` (not longer than a few seconds) in the root of the SD card and it will play when you press a button. Put a file called `click.wav` (not longer than a few seconds) in the root of the SD card and it will play when you press a button.
### Sounds
Put all sounds into the ´/sound´ subfolder. Name them ´1.wav, 2.wav, 3.wav...´
## Wave File Format
Export all sounds except ´click.wav´ as **48000Hz (48kHz) 16bit Stereo**.
### Audacity
You can use Audacity to export all soundfiles to WAV format.
1. Load file into Audacity
2. Select Track
3. File -> Export Audio
#### Format options in Audacity
- WAV(Microsoft)
- Chanels: Stereo
- Samplerate: 48000Hz
- Encoding: Signed 16-bit PCM
----
## Code something yourself ## Code something yourself
1. Download [Arduino IDE](https://www.arduino.cc/en/software/) 1. Download [Arduino IDE](https://www.arduino.cc/en/software/)

386
soundcube-firmware/codec.h Normal file
View File

@ -0,0 +1,386 @@
#pragma once
#include <cstdint>
#include <Wire.h>
#ifndef DEBUG
#define DEBUG false
#endif
// class TLV320AIC3204_Settings{
// public:
// TwoWire *wire;
// uint8_t i2cAddress = 0x18;
// ClockSettings1 clock_settings_1 = ClockSettings1(i2cAddress, wire);
// };
uint8_t logain[36] = {58,59,60,61,62,63,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
class TLV320AIC3204{
public:
TLV320AIC3204() : wire(&Wire) {}
TLV320AIC3204(TwoWire *wire) : wire(wire) {}
void begin(){init();};
void begin(TwoWire *_wire) {
wire = _wire;
init();
}
void begin(uint8_t i2cAddress, TwoWire *_wire) {
i2cAddress = i2cAddress;
wire = _wire;
init();
}
void init(){
// GENERAL
cw(0x00, 0x00); // select page 0
cw(0x01, 0x01); // soft reset
cw(0x1b, 0b00000000); // select I2S with 16 bit word length
cw(0x1d, 0b00000000); // disable loopback
// POWER and CM
cw(0x00, 0x01); // select page 1
cw(0x01, 0b00001000); // disable weak (crude) AVdd connection to DVdd
cw(0x02, 0b01011001); // enable internal AVdd LDO and enable analog blocks
cw(0x09, 0b00001100); // power up LOL, LOR, power down MAL, MAR, HPL, HPR
cw(0x0a, 0b01000000); // set full chip CM to 0.75V
cw(0x47, 0b00110011); // analog input quick charge time 1.6ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x34, 0b10000000); // LEFT MICPGA P route IN1L to LEFT_P with 40k input impedance
cw(0x36, 0b10000000); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
//cw(0x36, 0b00000011); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
cw(0x37, 0b10000000); // RIGHT MICPGA P route IN1R to RIGHT_P with 20k input impedance
cw(0x39, 0b10000000); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
//cw(0x39, 0b00000011); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
cw(0x3a, 0b00111100); // connect IN2, IN3 weakly to CM
// GAIN
cw(0x00, 0x01); // select page 1
cw(0x3b, 0b00000000); // unmute left MICPGA, set gain to 0db
cw(0x3c, 0b00000000); // unmute right MICPGA, set gain to 0db
// VOLUME
cw(0x00, 0x01); // select page 1
cw(0x16, 0b01110101); // MUTE IN1L to HPL
cw(0x17, 0b01110101); // MUTE IN1R to HPR
// ADC
cw(0x00, 0x00); // select page 0
cw(0x12, 0x81); // NADC 1
cw(0x13, 0x82); // MADC 2
cw(0x14, 0b10000000); // OSR ADC 128
//cw(0x14, 0b01000000); // OSR ADC 128
cw(0x3d, 0b00000001); // ADC PRB_R3 = 11, PRB_R2 = 10, PRB_R1 = 01
cw(0x00, 0x01); // select page 1
cw(0x3d, 0b00000000); // ADC PTM_R4
// DAC
cw(0x00, 0x00); // select page 0
cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0b00000000); // word length 16bits
cw(0x3c, 0b00000001); // PRB_P3
cw(0x00, 0x01); // select page 1
cw(0x7b, 0b00000001); // set REF charging time to 40ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0b00000000); // DAC PTM_P3/4
cw(0x04, 0b00000000); // DAC PTM_P3/4
// LO GAIN
cw(0x00, 0x01);
cw(0x12, 0b00000000); // LOL gain 0dB
cw(0x13, 0b00000000); // LOR gain 0dB
// POWER UP
// ADC
cw(0x00, 0x00); // select page 0
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x00, 0x00); // select page 0
cw(0x41, 0b11110001); // LEFT
cw(0x42, 0b11110001); // RIGHT
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x00, 0x00); // select page 0
cw(0x53, 0b01110000); // LEFT
cw(0x54, 0b01110000); // RIGHT
// // STATUS FLAGS
// Serial.println("CODEC STATUS");
// cw(0x00, 0x00); // select page 0
// Serial.println("ADC Flags");
// cr(0x24, 1);
// Serial.println("DAC Flags");
// cr(0x25, 1);
// Serial.println("P0_42 - Sticky Flags");
// cr(0x2A, 1);
}
// void softReset(){}; // 0x00 0x01
// void hardReset(){}; // reset pin
// void powerUp(){}; // power up
void setMicPgaGain(int gainLeft, int gainRight); // 0 - 47.5dB in 0.5dB steps
void setMicPgaGainL(int gain);
void setMicPgaGainR(int gain);
void mute(bool muteState){
muteL(muteState);
muteR(muteState);
}
void muteL(bool muteState){
uint8_t mutevol = (muteState << 7) + logain[volumeL];
cw(0x00, 0x01);
cw(0x12, mutevol);
muteStateL = muteState;
}
void muteR(bool muteState){
uint8_t mutevol = (muteState << 7) + logain[volumeR];
cw(0x00, 0x01);
cw(0x13, mutevol);
muteStateR = muteState;
}
// set LOL and LOR gain from 0 (-6dB) to 35 (29dB)
void setLineOutVolume(int volume){
setLineOutVolumeL(volume);
setLineOutVolumeR(volume);
cw(0x00, 0x01);
crd(0x12, 1);
}
void setLineOutVolumeL(int volume){
volume = max(0, min(35, volume));
uint8_t mutevol = (muteStateL << 7) + logain[volume];
cw(0x00, 0x01);
cw(0x12, mutevol); // LOL gain 0dB
volumeL = volume;
}
void setLineOutVolumeR(int volume){
volume = max(0, min(35, volume));
uint8_t mutevol = (muteStateR << 7) + logain[volume];
cw(0x00, 0x01);
cw(0x13, mutevol); // LOR gain 0dB
volumeR = volume;
}
void volumeUp(){
volumeL++;
volumeR++;
setLineOutVolumeL(volumeL);
setLineOutVolumeR(volumeR);
}
void volumeDown(){
volumeL--;
volumeR--;
setLineOutVolumeL(volumeL);
setLineOutVolumeR(volumeR);
}
uint8_t getVolumeL(){
uint8_t vol[1];
cw(0x00, 0x01);
cr(0x12, vol, 1);
return vol[0];
}
uint8_t getVolumeR(){
uint8_t vol[1];
cw(0x00, 0x01);
cr(0x13, vol, 1);
return vol[0];
}
private:
TwoWire *wire;
uint8_t i2cAddress = 0x18;
bool muteStateL = 0;
bool muteStateR = 0;
uint8_t volumeL = 0;
uint8_t volumeR = 0;
void cw(unsigned char first, unsigned char second){
wire->beginTransmission(i2cAddress);
wire->write(first);
wire->write(second);
int result = wire->endTransmission();
if(DEBUG){
Serial.print(i2cAddress, HEX);
Serial.print(" ");
Serial.print(first, HEX);
Serial.print(" ");
Serial.print(second, HEX);
Serial.print(" : ");
if(result == 0) {
Serial.println("OK");
} else {
Serial.print("ERROR: ");
Serial.println(result);
}
}
delay(5);
}
void cr(unsigned char first, uint8_t result[], size_t len){
wire->beginTransmission(i2cAddress);
wire->write(first); // set register for read
wire->endTransmission(false); // false to not release the line
wire->requestFrom(i2cAddress, len, true);
wire->readBytes(result, len);
if(DEBUG){
Serial.print(first, HEX);
Serial.print(" ");
for (int i = 0; i < len; i++) {
Serial.print(result[i], HEX);
Serial.print(" ");
Serial.println(result[i], BIN);
}
}
}
void crd(unsigned char first, size_t len){
wire->beginTransmission(0x18);
wire->write(first); // set register for read
wire->endTransmission(false); // false to not release the line
wire->requestFrom(0x18, len, true); // request bytes from register XY
Serial.print(first, HEX);
Serial.print(" ");
byte buff[len];
wire->readBytes(buff, len);
for (int i = 0; i < len; i++) {
Serial.print(buff[i], HEX);
Serial.print(" ");
Serial.println(buff[i], BIN);
}
}
// TLV320AIC3204_Settings settings;
};
/*
// GENERAL
cw(0x00, 0x00); // select page 0
cw(0x01, 0x01); // soft reset
cw(0x1b, 0b00000000); // select I2S with 16 bit word length
cw(0x1d, 0b00000000); // disable loopback
// POWER and CM
cw(0x00, 0x01); // select page 1
cw(0x01, 0b00001000); // disable weak (crude) AVdd connection to DVdd
cw(0x02, 0b00000001); // enable internal AVdd LDO and enable analog blocks
cw(0x09, 0b00001100); // power up LOL, LOR, power down MAL, MAR, HPL, HPR
cw(0x0a, 0b00001000); // set full chip CM to 0.75V
cw(0x47, 0b00110011); // analog input quick charge time 1.6ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x34, 0b10000000); // LEFT MICPGA P route IN1L to LEFT_P with 40k input impedance
//cw(0x36, 0b11000000); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
//cw(0x36, 0b00000011); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
cw(0x37, 0b10000000); // RIGHT MICPGA P route IN1R to RIGHT_P with 20k input impedance
//cw(0x39, 0b11000000); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
//cw(0x39, 0b00000011); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
cw(0x3a, 0b00111100); // connect IN2, IN3 weakly to CM
// GAIN
cw(0x00, 0x01); // select page 1
cw(0x3b, 0b00000000); // unmute left MICPGA, set gain to 0db
cw(0x3c, 0b00000000); // unmute right MICPGA, set gain to 0db
// VOLUME
cw(0x00, 0x01); // select page 1
cw(0x16, 0b01110101); // MUTE IN1L to HPL
cw(0x17, 0b01110101); // MUTE IN1R to HPR
// ADC
cw(0x00, 0x00); // select page 0
cw(0x12, 0x81); // NADC 1
cw(0x13, 0x82); // MADC 2
cw(0x14, 0b10000000); // OSR ADC 128
//cw(0x14, 0b01000000); // OSR ADC 128
cw(0x3d, 0b00000001); // ADC PRB_R3 = 11, PRB_R2 = 10, PRB_R1 = 01
cw(0x00, 0x01); // select page 1
cw(0x3d, 0b00000000); // ADC PTM_R4
// DAC
cw(0x00, 0x00); // select page 0
cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0b00000000); // word length 16bits
cw(0x3c, 0b00000001); // PRB_P3
cw(0x00, 0x01); // select page 1
cw(0x7b, 0b00000001); // set REF charging time to 40ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0b00000000); // DAC PTM_P3/4
cw(0x04, 0b00000000); // DAC PTM_P3/4
// LO GAIN
cw(0x00, 0x01);
cw(0x12, 0b00000001); // LOL gain 0dB
cw(0x13, 0b00000001); // LOR gain 0dB
// POWER UP
// ADC
cw(0x00, 0x00); // select page 0
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x00, 0x00); // select page 0
cw(0x41, 0b11111001); // LEFT
cw(0x42, 0b11111001); // RIGHT
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x00, 0x00); // select page 0
cw(0x53, 0b01110000); // LEFT
cw(0x54, 0b01110000); // RIGHT
*/

View File

@ -0,0 +1,57 @@
#pragma once
template<typename T>
class RingBuffer{
public:
RingBuffer() {}
RingBuffer(size_t size) : bufferSize(size) {}
void setSize(size_t size) {
bufferSize = size;
}
void begin(){
buffer = new T[bufferSize];
}
bool push(T data){
if(counter < bufferSize){
buffer[write] = data;
write++; // % bufferSize;
if(write == bufferSize) write = 0;
counter++;
return true;
}
return false;
}
T pop(){
T retval = 0;
if(counter > 0) {
counter--;
retval = buffer[read];
read++;// % bufferSize;
if(read == bufferSize) read = 0;
}
return retval;
}
bool isEmpty(){
return counter == 0;
}
bool isFull(){
return counter == bufferSize;
}
int size(){
return counter;
}
private:
size_t bufferSize = 0;
int counter = 0;
int write = 0;
int read = 0;
T *buffer;
};

View File

@ -1,31 +1,61 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ArduinoJson.hpp> #include <ArduinoJson.hpp>
#include <TCA9555.h> #include <TCA9555.h>
#include <AS5600.h> #include <AS5600.h>
#include <DAC8552.h>
#include <FastLED.h> #include <FastLED.h>
#include <PWMAudio.h> #include <PWMAudio.h>
#include <I2S.h> #include <I2S.h>
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include "codec.h"
#include "wavestream.h"
#define HAPTIC 1 #define HAPTIC 1
#define AURAL 1 #define AURAL 1
#define UI_SAMPLERATE 22050 #define UI_SAMPLERATE 22050
#define BUFFERSIZE 64
#define NSTREAMS 8
int16_t buffer[BUFFERSIZE];
WaveStream stream[NSTREAMS];
bool streams_loaded = false;
I2S i2s(INPUT_PULLUP); I2S i2s(INPUT_PULLUP);
TLV320AIC3204 codec;
TCA9555 TCA(0x20, &Wire1); TCA9555 TCA(0x20, &Wire1);
AS5600 ENC(&Wire1); AS5600 ENC(&Wire1);
DAC8552 dac(9, &SPI1);
PWMAudio ui_snd(8); PWMAudio ui_snd(8);
enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3}; enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
int lut_ring_cw[48] = {39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,50,49,48,47,46,45,44,43,42,41,40};
int lut_ring_ccw[48] = {40,41,42,43,44,45,46,47,48,49,50,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
int lut_matrix[13] = {56,55,57,58,59,62,61,60,63,64,65,67,66};
int active = 0;
int active_led_ring = 0;
bool speakerToggle = false;
uint32_t lastTime = 0;
int32_t position = 0;
CRGB ui_leds[74]; CRGB ui_leds[74];
CRGB edge_leds[11]; CRGB edge_leds[11];
volatile bool flag = false; volatile bool buttonChanged = false;
volatile bool click = false; volatile bool click = false;
volatile bool amp = false; volatile bool amp = false;
@ -62,6 +92,28 @@ void loadConfiguration(Config& config) {
file.close(); file.close();
} }
size_t count;
size_t tape_write = 0;
void codec_transmit() {
for(int i = 0; i < count; i++){
int j = active % NSTREAMS;
for(int k = 0; k < NSTREAMS; k++){
int16_t sample = 0;
sample = stream[k].get();
buffer[i] += sample >> 2;
}
}
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
}
void codec_receive(){
count = i2s.read((uint8_t *)&buffer, BUFFERSIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
for(int i = 0; i < count; i++){
buffer[i] *= -1;
}
}
void pwm_audio_callback() { void pwm_audio_callback() {
while (ui_snd.availableForWrite()) { while (ui_snd.availableForWrite()) {
if(click) { if(click) {
@ -80,16 +132,34 @@ void pwm_audio_callback() {
} }
void tca_irq() { void tca_irq() {
flag = true; buttonChanged = true;
}
void speaker(bool state){
digitalWrite(12, state ? HIGH : LOW);
digitalWrite(13, state ? HIGH : LOW);
} }
void setup() { void setup() {
Serial.begin(); Serial.begin();
delay(2000); delay(1000);
i2s.setFrequency(48000);
SPI1.setSCK(10);
SPI1.setTX(11);
SPI1.begin();
dac.begin();
FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11); FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11);
FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74); FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
speaker(false);
pinMode(21, INPUT_PULLUP); pinMode(21, INPUT_PULLUP);
sd_card_detected = !digitalRead(21); sd_card_detected = !digitalRead(21);
delay(500); delay(500);
@ -102,21 +172,18 @@ void setup() {
if(sdInitialized && SD.exists("/click.wav")) { if(sdInitialized && SD.exists("/click.wav")) {
File click = SD.open("/click.wav"); File click = SD.open("/click.wav");
click.seek(44, SeekSet);
int click_bytes = 0; int click_bytes = 0;
int16_t maxv = 0;
while (click.available() || click_bytes == UI_SAMPLERATE-1) { while (click.available() || click_bytes == UI_SAMPLERATE-1) {
int16_t sample = click.read() * 32; uint8_t samplebyte[2];
if(abs(sample) > maxv) maxv = sample; click.read(samplebyte, 2);
int16_t sample = (samplebyte[1] << 8) + samplebyte[0];
beep[click_bytes] = sample; beep[click_bytes] = sample;
click_bytes++; click_bytes++;
} }
click.close(); click.close();
beep_length = click_bytes; beep_length = click_bytes;
Serial.print("Read ");
Serial.print(beep_length);
Serial.print(" bytes from click.wav into beep array. maxV was ");
Serial.println(maxv);
} else { } else {
for(int i = 0; i < UI_SAMPLERATE; i++){ for(int i = 0; i < UI_SAMPLERATE; i++){
float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE; float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE;
@ -155,39 +222,89 @@ void setup() {
digitalWrite(7, LOW); digitalWrite(7, LOW);
pinMode(19, OUTPUT); // MCLK enable
digitalWrite(19, HIGH); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset
digitalWrite(20, HIGH);
codec.begin(&Wire1);
i2s.onTransmit(codec_transmit);
i2s.onReceive(codec_receive);
i2s.setDOUT(15);
i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18);
i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25Mhz
i2s.swapClocks();
i2s.setBitsPerSample(16);
i2s.setBuffers(6, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t));
if(!i2s.begin(48000)){
Serial.println("I2S error!");
while(100);
}
if(sdInitialized) {
for(int i = 0; i < NSTREAMS; i++){
stream[i].begin();
char filename[40];
sprintf(filename, "/sound/%d.wav", i+1);
if(SD.exists(filename)){
bool loaded = stream[i].load(SD.open(filename));
if(loaded) {
Serial.print("file read: ");
Serial.print(stream[i].wavefile.length);
Serial.print(" bytes | ");
Serial.print(stream[i].wavefile.samplerate);
Serial.print(" kHz | ");
Serial.print(stream[i].wavefile.bitspersample);
Serial.print(" bits | ");
Serial.print(stream[i].wavefile.channels);
Serial.print(" channels | ");
Serial.print(stream[i].wavefile.blockalign);
Serial.println(" bytes");
//stream[i].play();
stream[i].wavefile.loop = true;
} else {
Serial.println("file loading error");
}
} else {
for(int k = 0; k < 3; k++){
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(50); delay(50);
}
}
}
streams_loaded = true;
}
digitalWrite(6, HIGH);
delay(25);
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(25);
digitalWrite(6, LOW); digitalWrite(6, LOW);
// i2s.setDOUT(14);
// i2s.setDIN(15);
// i2s.setBCLK(16); //
// i2s.swapClocks();
// i2s.setMCLK(18);
// i2s.setBitsPerSample(16);
// i2s.setFrequency(48000);
// i2s.setSysClk(48000);
// i2s.begin();
// while (1) {
// int16_t l, r;
// i2s.read16(&l, &r);
// i2s.write16(l, r);
// }
} }
int active = 0; bool dactest = false;
int active_led_ring = 0;
uint32_t lastTime = 0;
int32_t position = 0;
void loop() { void loop() {
position = ENC.getCumulativePosition(); position = ENC.getCumulativePosition();
sd_card_detected = !digitalRead(21); sd_card_detected = !digitalRead(21);
if(sd_card_detected) edge_leds[8] = CRGB(0,25,0); if(sd_card_detected) edge_leds[8] = CRGB(0,10,0);
if(!sd_card_detected) edge_leds[8] = CRGB(25,0,0); if(!sd_card_detected) edge_leds[8] = CRGB(10,0,0);
// EDGE LEDs // EDGE LEDs
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -199,31 +316,50 @@ void loop() {
ui_leds[i + 3] = CRGB(0, 0, 0); ui_leds[i + 3] = CRGB(0, 0, 0);
} }
// flag = true when a button is pushed // Rotary button LEDs
if (flag) { ui_leds[0] = CRGB(0, 0, 0);
int val = TCA.read16(); ui_leds[1] = CRGB(0, 0, 0);
ui_leds[2] = CRGB(0, 0, 0);
// buttonChanged = true when a button is pushed
if (buttonChanged) {
int buttonValues = TCA.read16();
bool buttonsNew[16] = {false};
int buttonsDir[16] = {0};
bool buttonUp = false;
bool buttonDown = false;
for(int i = 0; i < 16; i++){ for(int i = 0; i < 16; i++){
buttons[i] = ~(val >> i) & 0x01; buttonsNew[i] = ~(buttonValues >> i) & 0x01;
if(buttonsNew[i] == true && buttons[i] == false) {
buttonsDir[i] = 1;
buttonDown = true;
}
if(buttonsNew[i] == false && buttons[i] == true) {
buttonsDir[i] = -1;
buttonUp = true;
}
buttons[i] = buttonsNew[i];
} }
// Make vibration // Make vibration
if (HAPTIC) { if (HAPTIC && buttonDown) {
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
delay(50); delay(50);
digitalWrite(6, LOW); digitalWrite(6, LOW);
} }
// Make beep // Make beep
if (AURAL) { if (AURAL && buttonDown) {
digitalWrite(7, HIGH); digitalWrite(7, HIGH);
click = true; click = true;
} }
// Flash led ring // Flash encoder leds
for (int i = 0; i < 48; i++) { ui_leds[0] = CRGB(0, 100, 50);
ui_leds[i + 3] = CRGB(0, 15, 0); ui_leds[1] = CRGB(0, 100, 50);
} ui_leds[2] = CRGB(0, 100, 50);
// Switch through LED matrix // Switch through LED matrix
if(buttons[RIGHT]) active++; if(buttons[RIGHT]) active++;
@ -231,29 +367,70 @@ void loop() {
if(active == 13) active = 0; if(active == 13) active = 0;
if(active == -1) active = 12; if(active == -1) active = 12;
flag = false; if(buttons[CVINL]) {
Serial.println("vol down");
codec.volumeDown();
} }
// Rotary button LEDs if(buttons[CVOUTL]) {
ui_leds[0] = CRGB(0, 0, 15); Serial.println("vol up");
ui_leds[1] = CRGB(0, 0, 15); codec.volumeUp();
ui_leds[2] = CRGB(0, 0, 15); }
if(buttons[DEBUG3]) {
speakerToggle = !speakerToggle;
speaker(speakerToggle);
}
if(buttons[DEBUG2]) {
dactest = !dactest;
dac.setValue(0, dactest ? 0 : 32768);
dac.setValue(1, !dactest ? 0 : 65535);
}
if(buttons[SELECT]){
stream[active % NSTREAMS].toggle();
}
Serial.println(codec.getVolumeL());
//for(int i = 0; i < NSTREAMS; i++){
// stream[i].pause();
// }
//stream[active % NSTREAMS].play();
buttonChanged = false;
}
//dac.setValue(0, dactest ? 0 : sin((float)millis() / 100.0f) * 32768 + 32768);
// empty LED matrix // empty LED matrix
for (int i = 0; i < 13; i++) { for (int i = 0; i < 13; i++) {
ui_leds[i + 3 + 48 + 4] = CRGB(0, 0, 0); ui_leds[lut_matrix[i]] = CRGB(0, 0, 0);
if(stream[i % NSTREAMS].isPlaying()) ui_leds[lut_matrix[i % NSTREAMS]] = CRGB(0, 50, 0);
} }
// set active LED matrix LED // set active LED matrix LED
ui_leds[active + 3 + 48 + 4] = CRGB(255, 255, 255); ui_leds[lut_matrix[active]] = CRGB(100, 100, 100);
if(position < 0) position += 4096; if(position < 0) position += 4096;
active_led_ring = (position / 32) % 48; active_led_ring = (position / 32) % 48;
// set active LED ring LED // set active LED ring LED
ui_leds[active_led_ring + 3] = CRGB(255, 255, 255); for(int i = 0; i < active_led_ring; i++){
ui_leds[lut_ring_ccw[i]] = CRGB(50, 0, 25);
}
FastLED.show(); FastLED.show();
if(streams_loaded) {
for(int i = 0; i < NSTREAMS; i++){
stream[i].stream();
}
}
if(i2s.getOverflow()) Serial.println("overflow");
if(i2s.getUnderflow()) Serial.println("underflow");
//delay(20); // wait 1ms
delay(1); // wait 1ms
} }

View File

@ -0,0 +1,158 @@
#pragma once
#include <SD.h>
#include "ringbuffer.h"
struct WaveFile{
File file;
char cstart[4];
char cwave[4];
char cfmt[4];
bool load(File _file){
file = _file;
// RIFF Header
uint8_t start[4];
uint8_t size[4];
uint8_t wave[4];
file.seek(0, SeekSet);
file.read(start, 4);
memcpy(cstart, start, 4);
if(strcmp("RIFF", cstart) != 0) return false;
file.seek(4, SeekSet);
file.read(size, 4);
length = (size[3] << 24) + (size[2] << 16) + (size[1] << 8) + size[3] - 8;
file.seek(8, SeekSet);
file.read(wave, 4);
memcpy(cwave, wave, 4);
if(strcmp("WAVE", cwave) != 0) return false;
// FORMAT
uint8_t fmt[4];
uint8_t fmtLen[4];
uint8_t fmtTag[2];
uint8_t fmtChannels[2];
uint8_t fmtSamplerate[4];
uint8_t fmtBytesPerSecond[4];
uint8_t fmtBlockalign[2];
uint8_t fmtBitsPerSample[2];
file.seek(12, SeekSet);
file.read(fmt, 4);
memcpy(cfmt, fmt, 4);
if(strcmp("fmt ", cfmt) != 0) return false;
file.seek(20, SeekSet);
file.read(fmtTag, 4);
format = (fmtTag[1] << 8) + fmtTag[0];
if(format != 0x0001) return false;
file.seek(22, SeekSet);
file.read(fmtChannels, 2);
channels = (fmtChannels[1] << 8) + fmtChannels[0];
file.seek(24, SeekSet);
file.read(fmtSamplerate, 4);
samplerate = (fmtSamplerate[3] << 24) + (fmtSamplerate[2] << 16) + (fmtSamplerate[1] << 8) + fmtSamplerate[0];
file.seek(32, SeekSet);
file.read(fmtBlockalign, 2);
blockalign = (fmtBlockalign[1] << 8) + fmtBlockalign[0];
file.seek(34, SeekSet);
file.read(fmtBitsPerSample, 2);
bitspersample = (fmtBitsPerSample[1] << 8) + fmtBitsPerSample[0];
return true;
}
void readblock(){
uint8_t samplebyte[blockalign];
file.read(samplebyte, blockalign);
if(!file.available() && loop) file.seek(44, SeekSet);
for(int i = 0; i < blockalign; i+=2){
int16_t sample = (samplebyte[i+1] << 8) + samplebyte[i];
buffer.push(sample);
}
}
int16_t get(){
return buffer.pop();
}
void stop(){
file.seek(44, SeekSet);
}
bool loop = false;
uint16_t format = 0;
uint32_t length = 0;
uint16_t channels = 0;
uint32_t samplerate = 0;
uint16_t blockalign = 0;
uint16_t bitspersample = 0;
RingBuffer<int16_t> buffer;
};
class WaveStream{
public:
WaveStream(){}
void begin(){
wavefile.buffer.setSize(24000);
wavefile.buffer.begin();
}
bool load(File _wavefile){
if(!wavefile.load(_wavefile)) return false;
return true;
}
void toggle(){playing = !playing;}
void play(){playing = true;}
void stop(){
playing = false;
wavefile.stop();
}
void pause(){playing = false;}
void stream(){
if(!wavefile.buffer.isFull() && playing){
int cnt = 0;
while (!wavefile.buffer.isFull() && cnt < 6000) {
wavefile.readblock();
cnt++;
}
}
}
int16_t get(){
return playing ? wavefile.get() : 0;
}
bool isPlaying(){
return playing;
}
//private:
WaveFile wavefile;
bool playing = false;
};

View File

@ -0,0 +1,110 @@
#pragma once
#ifndef DEBUG
#define DEBUG false
#endif
struct CodecSettings{
CodecSettings(uint8_t i2c_address, TwoWire *wire) : wire(wire), i2cAddress(i2c_address) {}
TwoWire *wire;
uint8_t i2cAddress = 0x18;
uint8_t page;
uint8_t reg;
uint8_t len;
virtual uint8_t get() = 0;
void write(){
selectPage(page);
cw(reg, get());
}
void read(uint8_t result[]){
selectPage(page);
cr(reg, result, len);
}
void selectPage(int page){
cw(0x00, page);
}
void cw(unsigned char first, unsigned char second){
Wire1.beginTransmission(i2cAddress);
Wire1.write(first);
Wire1.write(second);
int result = Wire1.endTransmission();
if(DEBUG){
Serial.print(i2cAddress, HEX);
Serial.print(" ");
Serial.print(first, HEX);
Serial.print(" ");
Serial.print(second, HEX);
Serial.print(" : ");
if(result == 0) {
Serial.println("OK");
} else {
Serial.print("ERROR: ");
Serial.println(result);
}
}
delay(5);
}
void cr(unsigned char first, uint8_t result[], size_t len){
Wire1.beginTransmission(i2cAddress);
Wire1.write(first); // set register for read
Wire1.endTransmission(false); // false to not release the line
Wire1.requestFrom(i2cAddress, len, true);
Wire1.readBytes(result, len);
if(DEBUG){
Serial.print(first, HEX);
Serial.print(" ");
for (int i = 0; i < len; i++) {
Serial.print(result[i], HEX);
Serial.print(" ");
Serial.println(result[i], BIN);
}
}
}
};
struct ClockSettings1 : public CodecSettings{
ClockSettings1(uint8_t i2c_address, TwoWire *wire) : CodecSettings(i2c_address, wire) {}
uint8_t page = 0x00;
uint8_t reg = 0x04;
uint8_t len = 1;
enum PLLRange{
PLL_HIGH = 0b01000000,
PLL_LOW = 0b00000000
};
enum PLLInputClock{
PLL_IN_MCLK = 0b00000000,
PLL_IN_BCLK = 0b00000100,
PLL_IN_GPIO = 0b00001000,
PLL_IN_DIN = 0b00001100
};
enum CodecInputClock{
CODEC_IN_MCLK = 0b00000000,
CODEC_IN_BCLK = 0b00000001,
CODEC_IN_GPIO = 0b00000010,
CODEC_IN_PLL = 0b00000011
};
PLLRange pll_range = PLL_LOW;
PLLInputClock pll_input_clock = PLL_IN_MCLK;
CodecInputClock codec_input_clock = CODEC_IN_MCLK;
uint8_t get(){
return pll_range | pll_input_clock | codec_input_clock;
}
};

291
soundcube-i2s-test/codec.h Normal file
View File

@ -0,0 +1,291 @@
#pragma once
#include <cstdint>
#include <Wire.h>
#ifndef DEBUG
#define DEBUG false
#endif
// class TLV320AIC3204_Settings{
// public:
// TwoWire *wire;
// uint8_t i2cAddress = 0x18;
// ClockSettings1 clock_settings_1 = ClockSettings1(i2cAddress, wire);
// };
class TLV320AIC3204{
public:
TLV320AIC3204() : wire(&Wire) {}
TLV320AIC3204(TwoWire *wire) : wire(wire) {}
void begin(){init();};
void begin(TwoWire *_wire) {
wire = _wire;
init();
}
void begin(uint8_t i2cAddress, TwoWire *_wire) {
i2cAddress = i2cAddress;
wire = _wire;
init();
}
void init(){
// GENERAL
cw(0x00, 0x00); // select page 0
cw(0x01, 0x01); // soft reset
cw(0x1b, 0b00000000); // select I2S with 16 bit word length
cw(0x1d, 0b00000000); // disable loopback
// POWER and CM
cw(0x00, 0x01); // select page 1
cw(0x01, 0b00001000); // disable weak (crude) AVdd connection to DVdd
cw(0x02, 0b00000001); // enable internal AVdd LDO and enable analog blocks
cw(0x09, 0b00001100); // power up LOL, LOR, power down MAL, MAR, HPL, HPR
cw(0x0a, 0b00001000); // set full chip CM to 0.75V
cw(0x47, 0b00110011); // analog input quick charge time 1.6ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x34, 0b10000000); // LEFT MICPGA P route IN1L to LEFT_P with 40k input impedance
cw(0x36, 0b10000000); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
//cw(0x36, 0b00000011); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
cw(0x37, 0b10000000); // RIGHT MICPGA P route IN1R to RIGHT_P with 20k input impedance
cw(0x39, 0b10000000); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
//cw(0x39, 0b00000011); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
cw(0x3a, 0b00111100); // connect IN2, IN3 weakly to CM
// GAIN
cw(0x00, 0x01); // select page 1
cw(0x3b, 0b00000000); // unmute left MICPGA, set gain to 0db
cw(0x3c, 0b00000000); // unmute right MICPGA, set gain to 0db
// VOLUME
cw(0x00, 0x01); // select page 1
cw(0x16, 0b01110101); // MUTE IN1L to HPL
cw(0x17, 0b01110101); // MUTE IN1R to HPR
// ADC
cw(0x00, 0x00); // select page 0
cw(0x12, 0x81); // NADC 1
cw(0x13, 0x82); // MADC 2
cw(0x14, 0b10000000); // OSR ADC 128
//cw(0x14, 0b01000000); // OSR ADC 128
cw(0x3d, 0b00000001); // ADC PRB_R3 = 11, PRB_R2 = 10, PRB_R1 = 01
cw(0x00, 0x01); // select page 1
cw(0x3d, 0b00000000); // ADC PTM_R4
// DAC
cw(0x00, 0x00); // select page 0
cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0b00000000); // word length 16bits
cw(0x3c, 0b00000001); // PRB_P3
cw(0x00, 0x01); // select page 1
cw(0x7b, 0b00000001); // set REF charging time to 40ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0b00000000); // DAC PTM_P3/4
cw(0x04, 0b00000000); // DAC PTM_P3/4
// LO GAIN
cw(0x00, 0x01);
cw(0x12, 0b00000001); // LOL gain 0dB
cw(0x13, 0b00000001); // LOR gain 0dB
// POWER UP
// ADC
cw(0x00, 0x00); // select page 0
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x00, 0x00); // select page 0
cw(0x41, 0b11111001); // LEFT
cw(0x42, 0b11111001); // RIGHT
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x00, 0x00); // select page 0
cw(0x53, 0b01110000); // LEFT
cw(0x54, 0b01110000); // RIGHT
// // STATUS FLAGS
// Serial.println("CODEC STATUS");
// cw(0x00, 0x00); // select page 0
// Serial.println("ADC Flags");
// cr(0x24, 1);
// Serial.println("DAC Flags");
// cr(0x25, 1);
// Serial.println("P0_42 - Sticky Flags");
// cr(0x2A, 1);
}
// void softReset(){}; // 0x00 0x01
// void hardReset(){}; // reset pin
// void powerUp(){}; // power up
void setMicPgaGain(int gainLeft, int gainRight); // 0 - 47.5dB in 0.5dB steps
void setMicPgaGainL(int gain);
void setMicPgaGainR(int gain);
void setLineOutVolume(int volumeLeft, int volumeRight);
void setLineOutVolumeL(int volume);
void setLineOutVolumeR(int volume);
private:
TwoWire *wire;
uint8_t i2cAddress = 0x18;
void cw(unsigned char first, unsigned char second){
wire->beginTransmission(i2cAddress);
wire->write(first);
wire->write(second);
int result = wire->endTransmission();
if(DEBUG){
Serial.print(i2cAddress, HEX);
Serial.print(" ");
Serial.print(first, HEX);
Serial.print(" ");
Serial.print(second, HEX);
Serial.print(" : ");
if(result == 0) {
Serial.println("OK");
} else {
Serial.print("ERROR: ");
Serial.println(result);
}
}
delay(5);
}
void cr(unsigned char first, uint8_t result[], size_t len){
wire->beginTransmission(i2cAddress);
wire->write(first); // set register for read
wire->endTransmission(false); // false to not release the line
wire->requestFrom(i2cAddress, len, true);
wire->readBytes(result, len);
if(DEBUG){
Serial.print(first, HEX);
Serial.print(" ");
for (int i = 0; i < len; i++) {
Serial.print(result[i], HEX);
Serial.print(" ");
Serial.println(result[i], BIN);
}
}
}
// TLV320AIC3204_Settings settings;
};
/*
// GENERAL
cw(0x00, 0x00); // select page 0
cw(0x01, 0x01); // soft reset
cw(0x1b, 0b00000000); // select I2S with 16 bit word length
cw(0x1d, 0b00000000); // disable loopback
// POWER and CM
cw(0x00, 0x01); // select page 1
cw(0x01, 0b00001000); // disable weak (crude) AVdd connection to DVdd
cw(0x02, 0b00000001); // enable internal AVdd LDO and enable analog blocks
cw(0x09, 0b00001100); // power up LOL, LOR, power down MAL, MAR, HPL, HPR
cw(0x0a, 0b00001000); // set full chip CM to 0.75V
cw(0x47, 0b00110011); // analog input quick charge time 1.6ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x34, 0b10000000); // LEFT MICPGA P route IN1L to LEFT_P with 40k input impedance
//cw(0x36, 0b11000000); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
//cw(0x36, 0b00000011); // LEFT MICPGA M route CM to LEFT_M with 20k input impedance
cw(0x37, 0b10000000); // RIGHT MICPGA P route IN1R to RIGHT_P with 20k input impedance
//cw(0x39, 0b11000000); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
//cw(0x39, 0b00000011); // RIGHT MICPGA M route CM to RIGHT_M with 20k input impedance
cw(0x3a, 0b00111100); // connect IN2, IN3 weakly to CM
// GAIN
cw(0x00, 0x01); // select page 1
cw(0x3b, 0b00000000); // unmute left MICPGA, set gain to 0db
cw(0x3c, 0b00000000); // unmute right MICPGA, set gain to 0db
// VOLUME
cw(0x00, 0x01); // select page 1
cw(0x16, 0b01110101); // MUTE IN1L to HPL
cw(0x17, 0b01110101); // MUTE IN1R to HPR
// ADC
cw(0x00, 0x00); // select page 0
cw(0x12, 0x81); // NADC 1
cw(0x13, 0x82); // MADC 2
cw(0x14, 0b10000000); // OSR ADC 128
//cw(0x14, 0b01000000); // OSR ADC 128
cw(0x3d, 0b00000001); // ADC PRB_R3 = 11, PRB_R2 = 10, PRB_R1 = 01
cw(0x00, 0x01); // select page 1
cw(0x3d, 0b00000000); // ADC PTM_R4
// DAC
cw(0x00, 0x00); // select page 0
cw(0x0b, 0x81); // NDAC 1
cw(0x0c, 0x82); // MDAC 2
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0b00000000); // word length 16bits
cw(0x3c, 0b00000001); // PRB_P3
cw(0x00, 0x01); // select page 1
cw(0x7b, 0b00000001); // set REF charging time to 40ms
// ROUTING
cw(0x00, 0x01); // select page 1
cw(0x0e, 0b00001000); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0b00001000); // right DAC reconstruction filter routed to LOR
cw(0x03, 0b00000000); // DAC PTM_P3/4
cw(0x04, 0b00000000); // DAC PTM_P3/4
// LO GAIN
cw(0x00, 0x01);
cw(0x12, 0b00000001); // LOL gain 0dB
cw(0x13, 0b00000001); // LOR gain 0dB
// POWER UP
// ADC
cw(0x00, 0x00); // select page 0
cw(0x51, 0b11000000); // power up ADC
cw(0x52, 0b00000000); // unmute ADC
cw(0x3f, 0b11010100); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume
// DAC VOLUME 0b00000000 = 0dB, 10000001 = -63.5dB, 0b00110000 = +24dB
cw(0x00, 0x00); // select page 0
cw(0x41, 0b11111001); // LEFT
cw(0x42, 0b11111001); // RIGHT
// ADC VOLUME 0b1101000 = -12dB, 0b00000000 = 0dB, 0b0101000 = +20dB
cw(0x00, 0x00); // select page 0
cw(0x53, 0b01110000); // LEFT
cw(0x54, 0b01110000); // RIGHT
*/

View File

@ -4,122 +4,87 @@
*/ */
#include <Wire.h> #include <Wire.h>
#include <I2S.h> #include <I2S.h>
#include "codec.h"
I2S i2s(INPUT_PULLUP); I2S i2s(INPUT_PULLUP);
#define SIZE 256 TLV320AIC3204 codec;
int16_t buffer[SIZE];
void cw(char first, char second){ #define SIZE 16
Wire1.beginTransmission(0x18); #define ECHO 192000
Wire1.write(first); int16_t buffer[SIZE];
Wire1.write(second); int16_t buffer2[ECHO];
Wire1.endTransmission();
delay(5); int16_t volume = 0;
size_t count;
size_t tape_write = 0;
void codec_transmit() {
for(int i = 0; i < count; i++){
buffer2[tape_write] = buffer[i];
tape_write++;
if(tape_write == ECHO) tape_write = 0;
int tape_read = tape_write + 1;
if(tape_read < 0) tape_read += ECHO;
buffer[i] += buffer2[tape_read % ECHO];
}
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
}
void codec_receive(){
count = i2s.read((uint8_t *)&buffer, SIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
for(int i = 0; i < count; i++){
buffer[i] *= -1;
}
} }
void setup() { void setup() {
i2s.setSysClk(48000);
Serial.begin(115200); Serial.begin(115200);
delay(1000); delay(2000);
Wire1.setSDA(2); Wire1.setSDA(2);
Wire1.setSCL(3); Wire1.setSCL(3);
Wire1.begin(); Wire1.begin();
delay(1000); delay(100);
// GENERAL
cw(0x00, 0x00);
cw(0x01, 0x01);
cw(0x1b, 0x10); // select I2S
// ADC
cw(0x00, 0x00); // select page 0
//cw(0x01, 0x01); // soft reset
cw(0x12, 0x87); // NADC 7
cw(0x13, 0x82); // MADC 2
cw(0x14, 0x80); // OSR ADC 128
cw(0x3d, 0x01); // ADC PRB_R1
cw(0x00, 0x01); // select page 1
cw(0x01, 0x08); // disable crude AVdd
cw(0x02, 0x01); // enable internal AVdd LDO
cw(0x0a, 0x0B); // set input CM to 0.9V and LO to 1.65V
cw(0x3d, 0x00); // ADC PTM_R4
cw(0x34, 0x80); // route IN1L to LEFT_P with 20k input impedance
cw(0x36, 0x80); // route CM to LEFT_M with 20k input impedance
cw(0x37, 0x80); // route IN1R to RIGHT_P with 20k input impedance
cw(0x39, 0x80); // route CM to RIGHT_M with 20k input impedance
cw(0x3b, 0x0c); // unmute left MICPGA
cw(0x3c, 0x0c); // unmute right MICPGA
cw(0x00, 0x00); // select page 0
cw(0x51, 0xc0); // power up ADC
cw(0x51, 0x00); // unmute ADC digital volume control
// DAC
cw(0x00, 0x00); // select page 0
//cw(0x01, 0x01); // software reset
cw(0x0b, 0x82); // NDAC 2
cw(0x0c, 0x87); // MDAC 7
cw(0x0d, 0x00); // OSR DAC 128
cw(0x0e, 0x80); // OSR DAC 128
cw(0x1b, 0x10); // world length 20bits PTM_P4 (highest performance)
cw(0x3c, 0x08); // PRB_P8
cw(0x00, 0x01); // select page 1
//cw(0x01, 0x08); // disable internal crude avdd
//cw(0x02, 0x01); // enable AVdd LDO
cw(0x7b, 0x01); // set REF charging time to 40ms
//cw(0x14, 0x25); // set HP soft stepping for anti pop
//cw(0x0a, 0x0B); // set input CM to 0.9V and LO to 1.65V
cw(0x0e, 0x08); // left DAC reconstruction filter routed to LOL
cw(0x0f, 0x08); // right DAC reconstruction filter routed to LOR
cw(0x03, 0x00); // DAC PTM_P3/4
cw(0x04, 0x00); // DAC PTM_P3/4
cw(0x12, 0x00); // LOL gain 0dB
cw(0x13, 0x00); // LOR gain 0dB
delay(1000);
cw(0x00, 0x00); // select page 0
cw(0x3f, 0xd6); // power up and route left digital audio to left dac channel and right to right
cw(0x40, 0x00); // unmute DAC digital volume
pinMode(19, OUTPUT); // MCLK enable pinMode(19, OUTPUT); // MCLK enable
digitalWrite(19, HIGH); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset pinMode(20, OUTPUT); // CODEC reset
digitalWrite(20, HIGH);
i2s.setSysClk(48000); codec.begin(&Wire1);
i2s.setDOUT(14); i2s.onTransmit(codec_transmit);
i2s.setDIN(15); i2s.onReceive(codec_receive);
i2s.setDOUT(15);
i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1 i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18); i2s.setMCLK(18);
i2s.setMCLKmult(256); // 12.288.000Hz
i2s.swapClocks(); i2s.swapClocks();
i2s.setBitsPerSample(16); i2s.setBitsPerSample(16);
i2s.setFrequency(48000);
i2s.setMCLKmult(128); // 6144000Hz 6.144MHz
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t)); i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
digitalWrite(19, HIGH); // enable MCLK if(!i2s.begin(48000)){
digitalWrite(20, HIGH); Serial.println("I2S error!");
i2s.begin(); while(100);
}
size_t count, index;
while (1) {
count = i2s.read((uint8_t *)&buffer, SIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
index = 0;
while (index < count) {
// Reduce volume by half
buffer[index++] >>= 1; // right
buffer[index++] >>= 1; // left
}
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
}
} }
int32_t last = 0;
int frame = 0;
void loop() { void loop() {
/* Nothing here */ frame++;
if(frame == 48000) frame = 0;
} }

17
tools/index_luts.py Normal file
View File

@ -0,0 +1,17 @@
leds = []
for i in range(3,51):
leds.append(i)
leds_first = leds[0:37]
leds_first.reverse()
leds_last = leds[37:48]
leds_last.reverse()
leds_yeah = leds_first + leds_last
print("int lut_ring_cw[48] = {", end="")
for i in range(0,48):
if i < 47:
print(leds_yeah[i], end=",")
else:
print(leds_yeah[i], end="")
print("};", end="")
print()

18
tools/ringbuffer-test.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
#include "ringbuffer.h"
RingBuffer<int16_t> ringbuffer(25);
int main(){
ringbuffer.begin();
for(int j = 0; j < 3; j++){
for(int i = 0; i < 15; i++){
if(!ringbuffer.isFull()) ringbuffer.push(i);
}
std::cout << "---" << std::endl;
for(int i = 0; i < 25; i++){
if(!ringbuffer.isEmpty()) ringbuffer.pop();
}
}
}

49
tools/ringbuffer.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
#include <iostream>
template<typename T>
class RingBuffer{
public:
RingBuffer(size_t size) : bufferSize(size) {}
void begin(){
buffer = new T[bufferSize];
}
bool push(T data){
std::cout << "write: " << data << std::endl;
if(counter < bufferSize){
buffer[write] = data;
write = (write+1) % bufferSize;
counter++;
return true;
}
return false;
}
T pop(){
T retval;
if(counter > 0) {
counter--;
retval = buffer[read];
read = (read+1) % bufferSize;
}
std::cout << "read: " << retval << std::endl;
return retval;
}
bool isEmpty(){
return counter == 0;
}
bool isFull(){
return counter == bufferSize;
}
private:
size_t bufferSize = 0;
int counter = 0;
int write = 0;
int read = 0;
T *buffer;
};