forked from W4D/soundcube-firmware
Compare commits
52 Commits
v0.0.1
...
sampler-si
| Author | SHA1 | Date | |
|---|---|---|---|
| 962c299e80 | |||
| cef092f494 | |||
| d44210ce59 | |||
| 7418cdfd35 | |||
| ad035e69e2 | |||
| d5ea2377f9 | |||
| b9130dc0ae | |||
| b4f504b21d | |||
| 6bcc37d3ee | |||
| 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 | |||
| 8691bc05eb | |||
| 6ceefa6705 | |||
| ab0dbe98a1 | |||
| 202b862280 | |||
| 4b728fffd0 | |||
| 559d92e526 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build
|
||||
137
README.md
Normal file
137
README.md
Normal file
@ -0,0 +1,137 @@
|
||||
# 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
|
||||
|
||||
1. Download [Arduino IDE](https://www.arduino.cc/en/software/)
|
||||
2. Install and open "Preferences"
|
||||
3. Follow these [instructions](https://github.com/earlephilhower/arduino-pico?tab=readme-ov-file#installation)
|
||||
- No need to change checkboxes, only paste the URL and hit OK.
|
||||
4. Install **Raspberry Pi Pico** boards ([HowTo install boards](https://support.arduino.cc/hc/en-us/articles/360016119519-Add-boards-to-Arduino-IDE))
|
||||
5. Install libraries ([HowTo install libraries](https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-installing-a-library/)): Adafruit Neopixel, AS5600 (Rob Tillaart), TCA9555 (Rob Tillaart)
|
||||
6. Choose **Generic RP2350** as board and the correct port (Mac: /dev/cu.usbmodemXXXX, PC: COMXX Serial Port)
|
||||
7. Go to **Tools -> Flash Size** and choose **16MB (no FS)**
|
||||
8. Make your changes in the code
|
||||
9. Hit Upload Button (Arduino IDE compiles and uploads it automatically)
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
106
soundcube-firmware/ringbuffer.h
Normal file
106
soundcube-firmware/ringbuffer.h
Normal file
@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
class RingBuffer{
|
||||
public:
|
||||
RingBuffer() {}
|
||||
RingBuffer(size_t size) : bufferSize(size) {}
|
||||
|
||||
void setSize(size_t size) {
|
||||
bufferSize = size;
|
||||
}
|
||||
|
||||
void begin(){
|
||||
buffer = new int16_t[bufferSize];
|
||||
}
|
||||
|
||||
bool push(int16_t data){
|
||||
if(counter < bufferSize){
|
||||
buffer[write] = data;
|
||||
write++; // % bufferSize;
|
||||
if(write == bufferSize) write = 0;
|
||||
counter++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int16_t pop(){
|
||||
int16_t retval = 0;
|
||||
if(counter > 0) {
|
||||
counter--;
|
||||
retval = buffer[read];
|
||||
read++;// % bufferSize;
|
||||
if(read == bufferSize) read = 0;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int16_t* getReadPointer(){
|
||||
return &buffer[read];
|
||||
}
|
||||
|
||||
void pointerPop(int nbytes){
|
||||
for(int i=0; i<nbytes / 2; ++i){
|
||||
if(counter > 0) {
|
||||
counter--;
|
||||
read++;// % bufferSize;
|
||||
if(read == bufferSize) read = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pushDMA(int32_t *source){
|
||||
if(counter < bufferSize){
|
||||
rp2040.memcpyDMA(&buffer[write], source, 4);
|
||||
write += 2; // % bufferSize;
|
||||
if(write == bufferSize) write = 0;
|
||||
counter += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void* getWritePointer(){
|
||||
return &buffer[write];
|
||||
}
|
||||
|
||||
void advance(int nbytes){
|
||||
for(int i = 0; i < nbytes/2; ++i){
|
||||
if(counter < (bufferSize-1)){
|
||||
write++; // % bufferSize;
|
||||
if(write == bufferSize) write = 0;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void popDMA(int32_t *target){
|
||||
if(counter > 1) {
|
||||
counter -= 2;
|
||||
rp2040.memcpyDMA(target, &buffer[read], 4);
|
||||
read += 2;
|
||||
if(read >= bufferSize) read = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool isEmpty(){
|
||||
return counter == 0;
|
||||
}
|
||||
|
||||
bool isFull(){
|
||||
return counter == (bufferSize-2);
|
||||
}
|
||||
|
||||
int size(){
|
||||
return counter;
|
||||
}
|
||||
|
||||
int remains(){
|
||||
return (bufferSize-2) - counter;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t bufferSize = 0;
|
||||
int counter = 0;
|
||||
int write = 0;
|
||||
int read = 0;
|
||||
int16_t *buffer;
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
222
soundcube-firmware/wavestream.h
Normal file
222
soundcube-firmware/wavestream.h
Normal file
@ -0,0 +1,222 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
bool readblockDMA(){
|
||||
void *bufferStart = buffer.getWritePointer();
|
||||
|
||||
file.read((uint8_t*)bufferStart, 4);
|
||||
buffer.advance(4);
|
||||
|
||||
if(!file.available() && loop) file.seek(44, SeekSet);
|
||||
if(!file.available() && !loop) {
|
||||
file.seek(44, SeekSet);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readblockSD(){
|
||||
void *bufferStart = buffer.getWritePointer();
|
||||
remains = buffer.remains();
|
||||
|
||||
if(remains > 512){
|
||||
adv = file.readBytes((char*)bufferStart, 512);
|
||||
buffer.advance(adv);
|
||||
|
||||
if(!file.available() && loop) file.seek(44, SeekSet);
|
||||
if(!file.available() && !loop) {
|
||||
file.seek(44, SeekSet);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
int adv = 0;
|
||||
int remains = 0;
|
||||
|
||||
RingBuffer buffer;
|
||||
};
|
||||
|
||||
class WaveStream{
|
||||
public:
|
||||
WaveStream(){}
|
||||
|
||||
void begin(){
|
||||
wavefile.buffer.setSize(2048);
|
||||
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(){
|
||||
int cnt = 0;
|
||||
while (!wavefile.buffer.isFull() && cnt < 1024) {
|
||||
bool ok = wavefile.readblockDMA();
|
||||
if(!ok) playing = false;
|
||||
cnt += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void streamChunk(){
|
||||
if(!wavefile.buffer.isFull()){
|
||||
bool ok = wavefile.readblockSD();
|
||||
if(!ok) playing = false;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t get(){
|
||||
return playing ? wavefile.get() : 0;
|
||||
}
|
||||
|
||||
int16_t* getPointer(){
|
||||
int16_t* p = wavefile.buffer.getReadPointer();
|
||||
wavefile.buffer.pointerPop(2);
|
||||
return p;
|
||||
}
|
||||
|
||||
void getDMA(int32_t *samples){
|
||||
if(playing) wavefile.buffer.popDMA(samples);
|
||||
}
|
||||
|
||||
bool isPlaying(){
|
||||
return playing;
|
||||
}
|
||||
|
||||
//private:
|
||||
|
||||
WaveFile wavefile;
|
||||
uint32_t cuelist[16];
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
@ -1,125 +1,90 @@
|
||||
/*
|
||||
I2S bi-directional input and output buffered loopback example
|
||||
Released to the Public Domain by Cooper Dalrymple
|
||||
*/
|
||||
#include <Wire.h>
|
||||
#include <I2S.h>
|
||||
|
||||
I2S i2s(INPUT_PULLUP);
|
||||
|
||||
#define SIZE 256
|
||||
int16_t buffer[SIZE];
|
||||
|
||||
void cw(char first, char second){
|
||||
Wire1.beginTransmission(0x18);
|
||||
Wire1.write(first);
|
||||
Wire1.write(second);
|
||||
Wire1.endTransmission();
|
||||
delay(5);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(1000);
|
||||
|
||||
Wire1.setSDA(2);
|
||||
Wire1.setSCL(3);
|
||||
Wire1.begin();
|
||||
|
||||
delay(1000);
|
||||
|
||||
// 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(20, OUTPUT); // CODEC reset
|
||||
|
||||
i2s.setSysClk(48000);
|
||||
|
||||
i2s.setDOUT(14);
|
||||
i2s.setDIN(15);
|
||||
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
|
||||
i2s.setMCLK(18);
|
||||
i2s.swapClocks();
|
||||
i2s.setBitsPerSample(16);
|
||||
i2s.setFrequency(48000);
|
||||
i2s.setMCLKmult(128); // 6144000Hz 6.144MHz
|
||||
|
||||
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
|
||||
|
||||
digitalWrite(19, HIGH); // enable MCLK
|
||||
digitalWrite(20, HIGH);
|
||||
i2s.begin();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
/* Nothing here */
|
||||
}
|
||||
/*
|
||||
I2S bi-directional input and output buffered loopback example
|
||||
Released to the Public Domain by Cooper Dalrymple
|
||||
*/
|
||||
#include <Wire.h>
|
||||
#include <I2S.h>
|
||||
#include "codec.h"
|
||||
|
||||
I2S i2s(INPUT_PULLUP);
|
||||
|
||||
TLV320AIC3204 codec;
|
||||
|
||||
#define SIZE 16
|
||||
#define ECHO 192000
|
||||
int16_t buffer[SIZE];
|
||||
int16_t buffer2[ECHO];
|
||||
|
||||
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() {
|
||||
i2s.setSysClk(48000);
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(2000);
|
||||
|
||||
Wire1.setSDA(2);
|
||||
Wire1.setSCL(3);
|
||||
Wire1.begin();
|
||||
|
||||
delay(100);
|
||||
|
||||
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(256); // 12.288.000Hz
|
||||
|
||||
i2s.swapClocks();
|
||||
i2s.setBitsPerSample(16);
|
||||
|
||||
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
|
||||
|
||||
if(!i2s.begin(48000)){
|
||||
Serial.println("I2S error!");
|
||||
while(100);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int32_t last = 0;
|
||||
int frame = 0;
|
||||
|
||||
void loop() {
|
||||
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