forked from W4D/soundcube-firmware
Compare commits
37 Commits
v0.0.2
...
v0.1.0-sam
| Author | SHA1 | Date | |
|---|---|---|---|
| 8858d41806 | |||
| ffdbe9fa6d | |||
| 6ef94390a5 | |||
| 6dd3acac2f | |||
| 4a04e95757 | |||
| 06642bd570 | |||
| 07323ec068 | |||
| e2eaa1b5b1 | |||
| 3b63123c76 | |||
| ecbd6ab3ee | |||
| 76f2e0ec28 | |||
| b78060aac2 | |||
| c1e204953f | |||
| ac9d4b5e6b | |||
| db463c5b8c | |||
| 55bcb2c389 | |||
| 8cd690edbd | |||
| 120a802840 | |||
| e58b4ff968 | |||
| 76ff73791a | |||
| 15733e8b36 | |||
| c3adf740bf | |||
| a1efe3cc10 | |||
| ef52d50b7c | |||
| c1ca96793c | |||
| a3a4fd4fb4 | |||
| a17cefb4ba | |||
| 4da1c8fa9d | |||
| 2ffd1997b9 | |||
| 549fa21d1a | |||
| cd1b0934aa | |||
| 686f76b0fd | |||
| 499d5f2c63 | |||
| c9b6220449 | |||
| 235603d47a | |||
| a16ac73d9b | |||
| ad9c71363b |
122
README.md
122
README.md
@ -1,5 +1,127 @@
|
|||||||
# Soundcube Firmware
|
# Soundcube Firmware
|
||||||
|
|
||||||
|
## Install new firmware
|
||||||
|
|
||||||
|
1. Go to [Releases](https://code.w4d.dev/W4D/soundcube-firmware/releases)
|
||||||
|
2. Download latest `soundcube-firmware.ino.uf2` file
|
||||||
|
3. On your Soundcube Board: Press and hold the outer most white button as seen from the USB-C socket
|
||||||
|
4. While holding the button press the inner most white button once
|
||||||
|
- 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
|
||||||
|
|
||||||
|
|
||||||
|
## SD Card contents
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
config.txt
|
||||||
|
sound/
|
||||||
|
├─ 1.wav
|
||||||
|
├─ 2.wav
|
||||||
|
├─ 3.wav
|
||||||
|
├─ 4.wav
|
||||||
|
├─ 5.wav
|
||||||
|
├─ 6.wav
|
||||||
|
├─ 7.wav
|
||||||
|
├─ 8.wav
|
||||||
|
ui/
|
||||||
|
├─ click.wav
|
||||||
|
├─ beep.wav
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### config.txt
|
||||||
|
|
||||||
|
Json formatted config file. For now only edge led color working.
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"boxid":"",
|
||||||
|
"edge": {
|
||||||
|
"color":{
|
||||||
|
"idle":{
|
||||||
|
"r":50,
|
||||||
|
"g":0,
|
||||||
|
"b":50
|
||||||
|
},
|
||||||
|
"active":{
|
||||||
|
"r":0,
|
||||||
|
"g":50,
|
||||||
|
"b":50
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ring": {
|
||||||
|
"color":{
|
||||||
|
"idle":{
|
||||||
|
"r":0,
|
||||||
|
"g":50,
|
||||||
|
"b":50
|
||||||
|
},
|
||||||
|
"active":{
|
||||||
|
"r":0,
|
||||||
|
"g":80,
|
||||||
|
"b":50
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sound
|
||||||
|
|
||||||
|
### UI
|
||||||
|
|
||||||
|
There are two UI sounds - `click.wav` and `beep.wav`.
|
||||||
|
|
||||||
|
- `click.wav` is played when a button is pressed other than the select button
|
||||||
|
- `beep.wav` is played when the rotary encoder select button is pressed
|
||||||
|
|
||||||
|
Put these files in the subfolder `ui` on the SD card.
|
||||||
|
|
||||||
|
#### Wave File Format for UI Sounds
|
||||||
|
|
||||||
|
`click.wav` needs to be **22050Hz (22kHz) 16bit Mono**
|
||||||
|
|
||||||
|
|
||||||
|
### Audio Out or Speakers
|
||||||
|
|
||||||
|
Put all sounds into the `/sound` subfolder. Name them `1.wav, 2.wav, 3.wav...`
|
||||||
|
|
||||||
|
|
||||||
|
#### Wave File Format for Audio Out and Speakers
|
||||||
|
|
||||||
|
Export all sounds as **48000Hz (48kHz) 16bit Stereo**.
|
||||||
|
|
||||||
|
|
||||||
|
### Audacity
|
||||||
|
|
||||||
|
You can use Audacity to export all soundfiles to WAV format.
|
||||||
|
|
||||||
|
Download it here [Github](https://github.com/audacity/audacity/releases)
|
||||||
|
|
||||||
|
1. Load file into Audacity
|
||||||
|
2. Select Track
|
||||||
|
3. File -> Export Audio
|
||||||
|
|
||||||
|
#### Format options in Audacity
|
||||||
|
|
||||||
|
**Audio Out**
|
||||||
|
|
||||||
|
- WAV(Microsoft)
|
||||||
|
- Chanels: Stereo
|
||||||
|
- Samplerate: 48000Hz
|
||||||
|
- Encoding: Signed 16-bit PCM
|
||||||
|
|
||||||
|
**UI**
|
||||||
|
|
||||||
|
- WAV(Microsoft)
|
||||||
|
- Channels: Mono
|
||||||
|
- Samplerate: 22050Hz
|
||||||
|
- 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
386
soundcube-firmware/codec.h
Normal 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
|
||||||
|
|
||||||
|
*/
|
||||||
57
soundcube-firmware/ringbuffer.h
Normal file
57
soundcube-firmware/ringbuffer.h
Normal 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;
|
||||||
|
};
|
||||||
@ -1,60 +1,352 @@
|
|||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <ArduinoJson.hpp>
|
||||||
|
|
||||||
#include <TCA9555.h>
|
#include <TCA9555.h>
|
||||||
#include <AS5600.h>
|
#include <AS5600.h>
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <DAC8552.h>
|
||||||
|
|
||||||
|
#include <FastLED.h>
|
||||||
#include <PWMAudio.h>
|
#include <PWMAudio.h>
|
||||||
#include <I2S.h>
|
#include <I2S.h>
|
||||||
|
#include <SPI.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 16
|
||||||
|
|
||||||
|
int16_t buffer[BUFFERSIZE];
|
||||||
|
|
||||||
|
WaveStream stream[NSTREAMS];
|
||||||
|
|
||||||
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 STATE {BANK, SAMPLE, SEQUENCE, CTEMPO};
|
||||||
|
STATE state = BANK;
|
||||||
|
|
||||||
Adafruit_NeoPixel edge_pixels(8, 4, NEO_GRB + NEO_KHZ800);
|
//enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
|
||||||
Adafruit_NeoPixel ui_pixels(72, 5, NEO_GRB + NEO_KHZ800);
|
enum BUTTON {MODE, LOOP, INL, INR, OUTR, OUTL, TEMPO, RESET, BACK, POWER, SELECT, DEBUG1, DEBUG2, DEBUG3};
|
||||||
|
|
||||||
volatile bool flag = false;
|
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};
|
||||||
volatile bool click = false;
|
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};
|
||||||
volatile bool amp = false;
|
int lut_matrix[13] = {56,55,57,58,59,62,61,60,63,64,65,67,66};
|
||||||
|
|
||||||
|
int lut_banks[4] = {56,55,67,66};
|
||||||
|
int lut_samples[4] = {62,58,60,64};
|
||||||
|
|
||||||
|
int active = 0;
|
||||||
|
int active_led_ring = 0;
|
||||||
|
|
||||||
|
int selected_bank = 0;
|
||||||
|
int selected_sample = 0;
|
||||||
|
|
||||||
|
uint32_t lastTime = 0;
|
||||||
|
int32_t position = 0;
|
||||||
|
|
||||||
|
CRGB ui_leds[74];
|
||||||
|
CRGB edge_leds[11];
|
||||||
|
|
||||||
|
volatile bool buttonChanged = false;
|
||||||
|
|
||||||
|
bool ui_click = false;
|
||||||
|
bool ui_beep = false;
|
||||||
|
bool amp = false;
|
||||||
|
bool streams_loaded = false;
|
||||||
|
bool speakerToggle = false;
|
||||||
|
bool sd_card_detected = false;
|
||||||
|
|
||||||
|
bool buttons[16] = {false};
|
||||||
|
int buttonsDir[16] = {0};
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
||||||
bool buttons[16] = {false};
|
int16_t ui_click_snd[UI_SAMPLERATE];
|
||||||
|
uint16_t click_length = 0;
|
||||||
|
|
||||||
int16_t beep[UI_SAMPLERATE];
|
int16_t ui_beep_snd[UI_SAMPLERATE];
|
||||||
|
uint16_t beep_length = 0;
|
||||||
|
|
||||||
|
struct Color {
|
||||||
|
int r;
|
||||||
|
int g;
|
||||||
|
int b;
|
||||||
|
int r_active;
|
||||||
|
int g_active;
|
||||||
|
int b_active;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
String boxid = "";
|
||||||
|
Color edge_color = {0,0,50,0,50,0};
|
||||||
|
Color ring_color = {0,50,50,0,80,50};
|
||||||
|
String sampleFiles[16];
|
||||||
|
int bpm = 90;
|
||||||
|
};
|
||||||
|
|
||||||
|
Config config;
|
||||||
|
|
||||||
|
struct Sample{
|
||||||
|
float volume = 1.0;
|
||||||
|
WaveStream *file;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sample samples[NSTREAMS];
|
||||||
|
|
||||||
|
struct SequenceStep{
|
||||||
|
int len = 0;
|
||||||
|
Sample *samples[4];
|
||||||
|
void trigger(){
|
||||||
|
for(int i = 0; i < len; i++){
|
||||||
|
samples[i]->file->play(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SequenceStep steps[16];
|
||||||
|
|
||||||
|
void loadConfiguration(Config& config) {
|
||||||
|
File file = SD.open("/config.txt");
|
||||||
|
JsonDocument doc;
|
||||||
|
|
||||||
|
DeserializationError error = deserializeJson(doc, file);
|
||||||
|
if(error) Serial.println(F("Failed to read file, using default configuration"));
|
||||||
|
|
||||||
|
config.edge_color.r = doc["edge"]["color"]["idle"]["r"] | 0;
|
||||||
|
config.edge_color.g = doc["edge"]["color"]["idle"]["g"] | 0;
|
||||||
|
config.edge_color.b = doc["edge"]["color"]["idle"]["b"] | 50;
|
||||||
|
config.edge_color.r_active = doc["edge"]["color"]["active"]["r"] | 0;
|
||||||
|
config.edge_color.g_active = doc["edge"]["color"]["active"]["g"] | 50;
|
||||||
|
config.edge_color.b_active = doc["edge"]["color"]["active"]["b"] | 0;
|
||||||
|
|
||||||
|
config.ring_color.r = doc["ring"]["color"]["idle"]["r"] | 0;
|
||||||
|
config.ring_color.g = doc["ring"]["color"]["idle"]["g"] | 50;
|
||||||
|
config.ring_color.b = doc["ring"]["color"]["idle"]["b"] | 50;
|
||||||
|
config.ring_color.r_active = doc["ring"]["color"]["active"]["r"] | 0;
|
||||||
|
config.ring_color.g_active = doc["ring"]["color"]["active"]["g"] | 80;
|
||||||
|
config.ring_color.b_active = doc["ring"]["color"]["active"]["b"] | 50;
|
||||||
|
|
||||||
|
JsonArray _sampleFiles = doc["samples"];
|
||||||
|
config.boxid = doc["boxid"].as<String>();
|
||||||
|
int i = 0;
|
||||||
|
for(JsonVariant item : _sampleFiles){
|
||||||
|
config.sampleFiles[i] = item.as<String>();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
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(ui_click || ui_beep) {
|
||||||
ui_snd.write(beep[counter]);
|
if(ui_beep) {
|
||||||
} else {
|
ui_snd.write(ui_beep_snd[counter]);
|
||||||
ui_snd.write(0);
|
|
||||||
}
|
|
||||||
counter++;
|
counter++;
|
||||||
if(counter == UI_SAMPLERATE) counter = 0;
|
if(counter == beep_length) {
|
||||||
|
counter = 0;
|
||||||
|
ui_beep = false;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(ui_click) {
|
||||||
|
ui_snd.write(ui_click_snd[counter]);
|
||||||
|
counter++;
|
||||||
|
if(counter == click_length) {
|
||||||
|
counter = 0;
|
||||||
|
ui_click = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
digitalWrite(7, LOW);
|
||||||
|
ui_snd.write(0);
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_ui_sounds(const char* file, int16_t *buffer, uint16_t &length){
|
||||||
|
if(SD.exists(file)) {
|
||||||
|
File f_click = SD.open(file);
|
||||||
|
f_click.seek(44, SeekSet);
|
||||||
|
int click_bytes = 0;
|
||||||
|
while (f_click.available() && click_bytes < UI_SAMPLERATE) {
|
||||||
|
uint8_t samplebyte[2];
|
||||||
|
f_click.read(samplebyte, 2);
|
||||||
|
int16_t sample = (samplebyte[1] << 8) + samplebyte[0];
|
||||||
|
buffer[click_bytes] = sample;
|
||||||
|
click_bytes++;
|
||||||
|
}
|
||||||
|
f_click.close();
|
||||||
|
length = click_bytes;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
for(int i = 0; i < UI_SAMPLERATE; i++){
|
||||||
|
float sine_pos = (2.0f * M_PI * 3000.0f * (float)i) / (float)UI_SAMPLERATE;
|
||||||
|
buffer[i] = (int16_t)(sin(sine_pos) * 8000.0f);
|
||||||
|
}
|
||||||
|
Serial.println("Synthesized 8kHz sine into beep array.");
|
||||||
|
length = UI_SAMPLERATE;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_samples(){
|
||||||
|
for(int i = 0; i < NSTREAMS; i++){
|
||||||
|
stream[i].begin();
|
||||||
|
char filename[64];
|
||||||
|
sprintf(filename, "/sound/%s/%s", config.boxid.c_str(), config.sampleFiles[i].c_str());
|
||||||
|
Serial.println(filename);
|
||||||
|
|
||||||
|
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].wavefile.loop = true;
|
||||||
|
} else {
|
||||||
|
Serial.println("file loading error");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(int k = 0; k < 3; k++){
|
||||||
|
digitalWrite(6, HIGH);
|
||||||
|
delay(20);
|
||||||
|
digitalWrite(6, LOW);
|
||||||
|
delay(25);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tca_irq() {
|
void tca_irq() {
|
||||||
flag = true;
|
buttonChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void speaker(bool state){
|
||||||
|
digitalWrite(12, state ? HIGH : LOW);
|
||||||
|
digitalWrite(13, state ? HIGH : LOW);
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < UI_SAMPLERATE; i++){
|
struct Vibration{
|
||||||
float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE;
|
int32_t last;
|
||||||
beep[i] = (int16_t)(sin(sine_pos) * 8000.0f);
|
bool start = false;
|
||||||
|
bool wait = false;
|
||||||
|
uint16_t on = 0;
|
||||||
|
uint16_t off = 0;
|
||||||
|
int n = 1;
|
||||||
|
|
||||||
|
void update(){
|
||||||
|
int32_t delta = millis() - last;
|
||||||
|
|
||||||
|
if(start && !wait){
|
||||||
|
digitalWrite(6, HIGH);
|
||||||
|
start = false;
|
||||||
|
wait = true;
|
||||||
|
last = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(delta >= on && wait){
|
||||||
|
digitalWrite(6, LOW);
|
||||||
|
wait = false;
|
||||||
|
n--;
|
||||||
|
last = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(delta >= off && n > 0){
|
||||||
|
start = true;
|
||||||
|
last = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void trigger(int _n, int _on, int _off){
|
||||||
|
n = _n;
|
||||||
|
on = _on;
|
||||||
|
off = _off;
|
||||||
|
start = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Vibration vibration;
|
||||||
|
|
||||||
|
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, 5>(ui_leds, 74);
|
||||||
|
|
||||||
|
pinMode(12, OUTPUT);
|
||||||
|
pinMode(13, OUTPUT);
|
||||||
|
|
||||||
|
speaker(false);
|
||||||
|
|
||||||
|
pinMode(21, INPUT_PULLUP);
|
||||||
|
sd_card_detected = !digitalRead(21);
|
||||||
|
delay(500);
|
||||||
|
|
||||||
|
bool sdInitialized = SD.begin(22, 23, 24);
|
||||||
|
delay(100);
|
||||||
|
if(!sdInitialized) sdInitialized = SD.begin(22, 23, 24); // hack to prevent SD card from not initializing after soft reset
|
||||||
|
|
||||||
|
if(sdInitialized) loadConfiguration(config);
|
||||||
|
|
||||||
|
if(sdInitialized){
|
||||||
|
load_ui_sounds("/ui/click.wav", ui_click_snd, click_length);
|
||||||
|
load_ui_sounds("/ui/beep.wav", ui_beep_snd, beep_length);
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println("INIT WIRE");
|
Serial.println("INIT WIRE");
|
||||||
Wire1.setSDA(2);
|
Wire1.setSDA(2);
|
||||||
@ -75,7 +367,8 @@ void setup() {
|
|||||||
|
|
||||||
// Rotary Encoder
|
// Rotary Encoder
|
||||||
ENC.begin(); // set direction pin.
|
ENC.begin(); // set direction pin.
|
||||||
ENC.setDirection(AS5600_COUNTERCLOCK_WISE);
|
ENC.setDirection(AS5600_CLOCK_WISE);
|
||||||
|
ENC.resetCumulativePosition();
|
||||||
|
|
||||||
pinMode(6, OUTPUT); // Vibration Motor
|
pinMode(6, OUTPUT); // Vibration Motor
|
||||||
pinMode(7, OUTPUT); // UI Amp Enable
|
pinMode(7, OUTPUT); // UI Amp Enable
|
||||||
@ -83,118 +376,317 @@ void setup() {
|
|||||||
ui_snd.onTransmit(pwm_audio_callback);
|
ui_snd.onTransmit(pwm_audio_callback);
|
||||||
ui_snd.begin(UI_SAMPLERATE);
|
ui_snd.begin(UI_SAMPLERATE);
|
||||||
|
|
||||||
digitalWrite(7, LOW);
|
digitalWrite(7, LOW); // UI amp off
|
||||||
|
|
||||||
edge_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
pinMode(19, OUTPUT); // MCLK enable
|
||||||
ui_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
digitalWrite(19, HIGH); // enable MCLK
|
||||||
|
|
||||||
// i2s.setDOUT(14);
|
pinMode(20, OUTPUT); // CODEC reset
|
||||||
// i2s.setDIN(15);
|
digitalWrite(20, HIGH);
|
||||||
// i2s.setBCLK(16); //
|
|
||||||
// i2s.swapClocks();
|
|
||||||
// i2s.setMCLK(18);
|
|
||||||
// i2s.setBitsPerSample(16);
|
|
||||||
// i2s.setFrequency(48000);
|
|
||||||
// i2s.setSysClk(48000);
|
|
||||||
// i2s.begin();
|
|
||||||
|
|
||||||
// while (1) {
|
codec.begin(&Wire1);
|
||||||
// int16_t l, r;
|
|
||||||
// i2s.read16(&l, &r);
|
i2s.onTransmit(codec_transmit);
|
||||||
// i2s.write16(l, r);
|
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) {
|
||||||
|
streams_loaded = load_samples();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(streams_loaded) {
|
||||||
|
for(int i = 0; i < NSTREAMS; i++){
|
||||||
|
samples[i].file = &stream[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
digitalWrite(6, HIGH);
|
||||||
|
delay(25);
|
||||||
|
digitalWrite(6, LOW);
|
||||||
|
delay(50);
|
||||||
|
digitalWrite(6, HIGH);
|
||||||
|
delay(25);
|
||||||
|
digitalWrite(6, LOW);
|
||||||
}
|
}
|
||||||
int active = 0;
|
|
||||||
int active_led_ring = 0;
|
|
||||||
|
|
||||||
uint32_t lastTime = 0;
|
uint32_t last = 0;
|
||||||
int32_t position = 0;
|
int bar = 0;
|
||||||
|
int bar_old = -1;
|
||||||
|
int set_bar = 0;
|
||||||
|
int16_t angle = 0;
|
||||||
|
int32_t position_last = 0;
|
||||||
|
|
||||||
|
int16_t encdelta_raw = 0;
|
||||||
|
int16_t encdeltadiv = 512;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
edge_pixels.clear(); // Set all pixel colors to 'off'
|
|
||||||
ui_pixels.clear(); // Set all pixel colors to 'off'
|
|
||||||
|
|
||||||
position = ENC.getCumulativePosition();
|
position = ENC.getCumulativePosition();
|
||||||
|
angle = ENC.readAngle();
|
||||||
|
encdelta_raw += (position - position_last);
|
||||||
|
|
||||||
// if (millis() - lastTime >= 100)
|
// Serial.print(encdelta_raw);
|
||||||
// {
|
// Serial.print(" \t");
|
||||||
// lastTime = millis();
|
|
||||||
// Serial.print(ENC.getCumulativePosition());
|
|
||||||
// Serial.print("\t");
|
|
||||||
// Serial.println(ENC.getRevolutions());
|
|
||||||
// }
|
|
||||||
|
|
||||||
// LED Ring leeren
|
int encdelta = 0;
|
||||||
for (int i = 0; i < 48; i++) {
|
if(abs(encdelta_raw) > encdeltadiv) {
|
||||||
ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 0, 0));
|
encdelta = encdelta_raw > 0 ? 1 : -1;
|
||||||
|
encdelta_raw = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// flag = true when a button is pushed
|
// Serial.print(angle);
|
||||||
if (flag) {
|
// Serial.print(" \t");
|
||||||
int val = TCA.read16();
|
// Serial.print(position);
|
||||||
Serial.println(val, BIN);
|
// Serial.print(" \t");
|
||||||
|
// Serial.print(position_last);
|
||||||
|
// Serial.print(" \t");
|
||||||
|
// Serial.print(encdelta_raw);
|
||||||
|
// Serial.print(" \t");
|
||||||
|
// Serial.println(encdelta);
|
||||||
|
|
||||||
for(int i = 0; i < 16; i++){
|
uint32_t delta_bpm = floor((60000 / config.bpm) / 16);
|
||||||
buttons[i] = ~(val >> i) & 0x01;
|
|
||||||
Serial.print(buttons[i]);
|
|
||||||
Serial.print(" ");
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
flag = false;
|
if(millis() - last >= delta_bpm) {
|
||||||
if (HAPTIC) {
|
bar = (bar + 1) % 16;
|
||||||
digitalWrite(6, HIGH);
|
last = millis();
|
||||||
}
|
|
||||||
// Make beep
|
|
||||||
if (AURAL) {
|
|
||||||
digitalWrite(7, HIGH);
|
|
||||||
click = true;
|
|
||||||
delay(50);
|
|
||||||
click = false;
|
|
||||||
digitalWrite(7, LOW);
|
|
||||||
}
|
|
||||||
// Make vibration
|
|
||||||
if(HAPTIC){
|
|
||||||
if(!AURAL) delay(50);
|
|
||||||
digitalWrite(6, LOW);
|
|
||||||
}
|
|
||||||
// Flash led ring
|
|
||||||
for (int i = 0; i < 48; i++) {
|
|
||||||
ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 15, 0));
|
|
||||||
}
|
|
||||||
// Switch through LED matrix
|
|
||||||
if(buttons[RIGHT]) active++;
|
|
||||||
if(buttons[LEFT]) active--;
|
|
||||||
if(active == 13) active = 0;
|
|
||||||
if(active == -1) active = 12;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sd_card_detected = !digitalRead(21);
|
||||||
|
edge_leds[8] = sd_card_detected ? CRGB(0,10,0) : CRGB(10,0,0);
|
||||||
|
|
||||||
// EDGE LEDs
|
// EDGE LEDs
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
edge_pixels.setPixelColor(i, edge_pixels.Color(15, 15, 15));
|
edge_leds[i] = CRGB(config.edge_color.r, config.edge_color.g, config.edge_color.b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// LED Ring leeren
|
||||||
|
for (int i = 0; i < 48; i++) {
|
||||||
|
ui_leds[i + 3] = CRGB(0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotary button LEDs
|
// Rotary button LEDs
|
||||||
ui_pixels.setPixelColor(0, edge_pixels.Color(0, 0, 15));
|
ui_leds[0] = CRGB(0, 0, 0);
|
||||||
ui_pixels.setPixelColor(1, edge_pixels.Color(0, 0, 15));
|
ui_leds[1] = CRGB(0, 0, 0);
|
||||||
ui_pixels.setPixelColor(2, edge_pixels.Color(0, 0, 15));
|
ui_leds[2] = CRGB(0, 0, 0);
|
||||||
|
|
||||||
|
// buttonChanged = true when a button is pushed
|
||||||
|
if (buttonChanged) {
|
||||||
|
int buttonValues = TCA.read16();
|
||||||
|
|
||||||
|
bool buttonsNew[16] = {false};
|
||||||
|
|
||||||
|
bool buttonUp = false;
|
||||||
|
bool buttonDown = false;
|
||||||
|
|
||||||
|
for(int i = 0; i < 16; i++){
|
||||||
|
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
|
||||||
|
if(HAPTIC && buttonDown) {
|
||||||
|
vibration.trigger(1, 25, 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make beep
|
||||||
|
if(AURAL && buttonDown) {
|
||||||
|
digitalWrite(7, HIGH);
|
||||||
|
ui_click = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttons[TEMPO] && buttonDown){
|
||||||
|
state = CTEMPO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttons[BACK] && buttonDown){
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(state){
|
||||||
|
case BANK:
|
||||||
|
encdeltadiv = 512;
|
||||||
|
encdelta_raw = 0;
|
||||||
|
encdelta = 0;
|
||||||
|
if(buttons[SELECT] && buttonDown){
|
||||||
|
state = SAMPLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SAMPLE:
|
||||||
|
encdeltadiv = 512;
|
||||||
|
encdelta_raw = 0;
|
||||||
|
encdelta = 0;
|
||||||
|
if(buttons[SELECT] && buttonDown){
|
||||||
|
state = SEQUENCE;
|
||||||
|
}
|
||||||
|
if(buttons[BACK] && buttonDown){
|
||||||
|
state = BANK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SEQUENCE:
|
||||||
|
encdeltadiv = 256;
|
||||||
|
encdelta_raw = 0;
|
||||||
|
encdelta = 0;
|
||||||
|
if(buttons[SELECT] && buttonDown){
|
||||||
|
int n = steps[set_bar].len;
|
||||||
|
steps[set_bar].samples[n] = &samples[selected_bank*4 + selected_sample];
|
||||||
|
steps[set_bar].len = (steps[set_bar].len + 1) % 4;
|
||||||
|
}
|
||||||
|
if(buttons[BACK] && buttonDown){
|
||||||
|
state = SAMPLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CTEMPO:
|
||||||
|
encdeltadiv = 64;
|
||||||
|
encdelta_raw = 0;
|
||||||
|
encdelta = 0;
|
||||||
|
if(buttons[SELECT] && buttonDown){
|
||||||
|
state = BANK;
|
||||||
|
}
|
||||||
|
if(buttons[BACK] && buttonDown){
|
||||||
|
state = BANK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set bar
|
||||||
|
// if(buttons[BACK]) set_bar++;
|
||||||
|
// if(buttons[POWER]) set_bar--;
|
||||||
|
|
||||||
|
if(buttons[SELECT]){
|
||||||
|
// Flash encoder leds
|
||||||
|
ui_leds[0] = CRGB(0, 100, 50);
|
||||||
|
ui_leds[1] = CRGB(0, 100, 50);
|
||||||
|
ui_leds[2] = CRGB(0, 100, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttons[DEBUG1]) {
|
||||||
|
Serial.println("vol down");
|
||||||
|
codec.volumeDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttons[DEBUG3]) {
|
||||||
|
Serial.println("vol up");
|
||||||
|
codec.volumeUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttons[DEBUG2]) {
|
||||||
|
speakerToggle = !speakerToggle;
|
||||||
|
speaker(speakerToggle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Serial.println(codec.getVolumeL());
|
||||||
|
|
||||||
|
buttonChanged = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(position < 0) position += 4096;
|
||||||
|
|
||||||
|
switch(state){
|
||||||
|
case BANK:
|
||||||
|
selected_bank += encdelta;
|
||||||
|
if(selected_bank == 4) selected_bank = 0;
|
||||||
|
if(selected_bank == -1) selected_bank = 3;
|
||||||
|
break;
|
||||||
|
case SAMPLE:
|
||||||
|
selected_sample += encdelta;
|
||||||
|
if(selected_sample == 4) selected_sample = 0;
|
||||||
|
if(selected_sample == -1) selected_sample = 3;
|
||||||
|
break;
|
||||||
|
case SEQUENCE:
|
||||||
|
set_bar += encdelta;
|
||||||
|
if(set_bar == 16) set_bar = 0;
|
||||||
|
if(set_bar == -1) set_bar = 15;
|
||||||
|
break;
|
||||||
|
case CTEMPO:
|
||||||
|
config.bpm += encdelta;
|
||||||
|
if(config.bpm < 15) config.bpm = 15;
|
||||||
|
if(config.bpm > 300) config.bpm = 300;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//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 < 4; i++) {
|
||||||
ui_pixels.setPixelColor(i + 3 + 48 + 4, edge_pixels.Color(0, 0, 0));
|
ui_leds[lut_banks[i]] = CRGB(0, 0, 0);
|
||||||
|
ui_leds[lut_samples[i]] = CRGB(0,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set active LED matrix LED
|
// set active LED matrix LED
|
||||||
ui_pixels.setPixelColor(active + 3 + 48 + 4, edge_pixels.Color(255, 255, 255));
|
ui_leds[lut_banks[selected_bank]] = CRGB(100, 50, 50);
|
||||||
|
ui_leds[lut_samples[selected_sample]] = CRGB(100, 0, 50);
|
||||||
|
|
||||||
if(position < 0) position += 4096;
|
for(int i = 0; i < 48; i++){
|
||||||
active_led_ring = (position / 32) % 48;
|
int step = floor(i/3);
|
||||||
|
if(step == set_bar){
|
||||||
|
ui_leds[lut_ring_cw[i]] = CRGB(config.ring_color.r_active, config.ring_color.g_active, config.ring_color.b_active);
|
||||||
|
}
|
||||||
|
if(step == bar){
|
||||||
|
ui_leds[lut_ring_cw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
|
||||||
|
}
|
||||||
|
if(steps[step].len > 0) {
|
||||||
|
ui_leds[lut_ring_cw[i]] = CRGB(0, 10, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// set active LED ring LED
|
}
|
||||||
ui_pixels.setPixelColor(active_led_ring + 3, edge_pixels.Color(255, 255, 255));
|
|
||||||
|
|
||||||
edge_pixels.show();
|
if(bar != bar_old){
|
||||||
ui_pixels.show();
|
if(steps[bar].len > 0) {
|
||||||
|
steps[bar].trigger();
|
||||||
|
Serial.print("trigger ");
|
||||||
|
Serial.println(bar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delay(1); // wait 1ms
|
|
||||||
|
// // set active LED ring LED
|
||||||
|
// for(int i = 0; i < active_led_ring; i++){
|
||||||
|
// ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
|
||||||
|
// }
|
||||||
|
|
||||||
|
FastLED.show();
|
||||||
|
if(streams_loaded) {
|
||||||
|
for(int i = 0; i < NSTREAMS; i++){
|
||||||
|
stream[i].stream();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(buttonChanged){
|
||||||
|
for(int i = 0; i < 16; i++){
|
||||||
|
if(buttonsDir[i] == 1) buttonsDir[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i2s.getOverflow()) Serial.println("overflow");
|
||||||
|
if(i2s.getUnderflow()) Serial.println("underflow");
|
||||||
|
|
||||||
|
//delay(20); // wait 1ms
|
||||||
|
bar_old = bar;
|
||||||
|
vibration.update();
|
||||||
|
position_last = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
171
soundcube-firmware/wavestream.h
Normal file
171
soundcube-firmware/wavestream.h
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readblock(){
|
||||||
|
uint8_t samplebyte[blockalign];
|
||||||
|
|
||||||
|
file.read(samplebyte, blockalign);
|
||||||
|
if(!file.available() && loop) file.seek(44, SeekSet);
|
||||||
|
if(!file.available() && !loop) {
|
||||||
|
file.seek(44, SeekSet);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < blockalign; i+=2){
|
||||||
|
int16_t sample = (samplebyte[i+1] << 8) + samplebyte[i];
|
||||||
|
buffer.push(sample);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t get(){
|
||||||
|
return buffer.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop(){
|
||||||
|
file.seek(44, SeekSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset(){
|
||||||
|
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(8000);
|
||||||
|
wavefile.buffer.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load(File _wavefile){
|
||||||
|
if(!wavefile.load(_wavefile)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toggle(){playing = !playing;}
|
||||||
|
|
||||||
|
void play(bool reset = false){
|
||||||
|
if(reset) wavefile.reset();
|
||||||
|
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) {
|
||||||
|
bool ok = wavefile.readblock();
|
||||||
|
if(!ok) playing = false;
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t get(){
|
||||||
|
return playing ? wavefile.get() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isPlaying(){
|
||||||
|
return playing;
|
||||||
|
}
|
||||||
|
|
||||||
|
//private:
|
||||||
|
|
||||||
|
WaveFile wavefile;
|
||||||
|
|
||||||
|
bool playing = false;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
110
soundcube-i2s-test/codec-registers.h
Normal file
110
soundcube-i2s-test/codec-registers.h
Normal 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
291
soundcube-i2s-test/codec.h
Normal 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
|
||||||
|
|
||||||
|
*/
|
||||||
@ -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
17
tools/index_luts.py
Normal 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
18
tools/ringbuffer-test.cpp
Normal 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
49
tools/ringbuffer.h
Normal 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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user