forked from W4D/soundcube-firmware
feierabend
This commit is contained in:
@ -1,29 +1,43 @@
|
|||||||
#include <cstdint>
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
struct CodecSettings{
|
#ifndef DEBUG
|
||||||
public:
|
#define DEBUG false
|
||||||
|
#endif
|
||||||
|
|
||||||
virtual void get() = 0;
|
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(){
|
void write(){
|
||||||
selectPage(page);
|
selectPage(page);
|
||||||
cw(reg, get());
|
cw(reg, get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read(uint8_t result[]){
|
||||||
|
selectPage(page);
|
||||||
|
cr(reg, result, len);
|
||||||
|
}
|
||||||
|
|
||||||
void selectPage(int page){
|
void selectPage(int page){
|
||||||
cw(0x00, page);
|
cw(0x00, page);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cw(unsigned char first, unsigned char second){
|
void cw(unsigned char first, unsigned char second){
|
||||||
Wire1.beginTransmission(i2c_address);
|
Wire1.beginTransmission(i2cAddress);
|
||||||
Wire1.write(first);
|
Wire1.write(first);
|
||||||
Wire1.write(second);
|
Wire1.write(second);
|
||||||
int result = Wire1.endTransmission();
|
int result = Wire1.endTransmission();
|
||||||
if(debug){
|
if(DEBUG){
|
||||||
Serial.print(i2c_address, HEX);
|
Serial.print(i2cAddress, HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.print(first, HEX);
|
Serial.print(first, HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
@ -39,16 +53,16 @@ struct CodecSettings{
|
|||||||
delay(5);
|
delay(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cr(unsigned char first, uint8_t &result, size_t len){
|
void cr(unsigned char first, uint8_t result[], size_t len){
|
||||||
Wire1.beginTransmission(i2c_address);
|
Wire1.beginTransmission(i2cAddress);
|
||||||
Wire1.write(first); // set register for read
|
Wire1.write(first); // set register for read
|
||||||
Wire1.endTransmission(false); // false to not release the line
|
Wire1.endTransmission(false); // false to not release the line
|
||||||
|
|
||||||
Wire1.requestFrom(i2c_address, len, true);
|
Wire1.requestFrom(i2cAddress, len, true);
|
||||||
|
|
||||||
Wire1.readBytes(result, len);
|
Wire1.readBytes(result, len);
|
||||||
|
|
||||||
if(debug){
|
if(DEBUG){
|
||||||
Serial.print(first, HEX);
|
Serial.print(first, HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
|
|
||||||
@ -62,8 +76,11 @@ struct CodecSettings{
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ClockSettings1 : public CodecSettings{
|
struct ClockSettings1 : public CodecSettings{
|
||||||
|
ClockSettings1(uint8_t i2c_address, TwoWire *wire) : CodecSettings(i2c_address, wire) {}
|
||||||
|
|
||||||
uint8_t page = 0x00;
|
uint8_t page = 0x00;
|
||||||
uint8_t reg = 0x04;
|
uint8_t reg = 0x04;
|
||||||
|
uint8_t len = 1;
|
||||||
|
|
||||||
enum PLLRange{
|
enum PLLRange{
|
||||||
PLL_HIGH = 0b01000000,
|
PLL_HIGH = 0b01000000,
|
||||||
@ -96,25 +113,30 @@ struct ClockSettings1 : public CodecSettings{
|
|||||||
class TLV320AIC3204_Settings{
|
class TLV320AIC3204_Settings{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
TwoWire *wire;
|
||||||
|
uint8_t i2cAddress = 0x18;
|
||||||
|
|
||||||
static ClockSettings1 clock_settings_1;
|
ClockSettings1 clock_settings_1 = ClockSettings1(i2cAddress, wire);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class TLV320AIC3204{
|
class TLV320AIC3204{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TLV320AIC3204(){i2c = &Wire}
|
TLV320AIC3204(){settings.wire = &Wire;}
|
||||||
TLV320AIC3204(TwoWire &wire) : i2c(&wire) {}
|
TLV320AIC3204(TwoWire *wire) {settings.wire = wire;}
|
||||||
|
|
||||||
uint8_t i2c_address = 0x18;
|
void begin(){};
|
||||||
|
void begin(TwoWire *wire) {settings.wire = wire;}
|
||||||
|
void begin(uint8_t i2cAddress, TwoWire *wire) {
|
||||||
|
settings.i2cAddress = i2cAddress;
|
||||||
|
settings.wire = wire;
|
||||||
|
}
|
||||||
|
|
||||||
void init();
|
void softReset(){}; // 0x00 0x01
|
||||||
|
void hardReset(){}; // reset pin
|
||||||
|
|
||||||
void softReset(); // 0x00 0x01
|
void powerUp(){}; // power up
|
||||||
void hardReset(); // reset pin
|
|
||||||
|
|
||||||
void powerUp(); // power up
|
|
||||||
|
|
||||||
void setClockMultiplexer(ClockSettings1::PLLRange range, ClockSettings1::PLLInputClock pll_input, ClockSettings1::CodecInputClock codec_input) {
|
void setClockMultiplexer(ClockSettings1::PLLRange range, ClockSettings1::PLLInputClock pll_input, ClockSettings1::CodecInputClock codec_input) {
|
||||||
settings.clock_settings_1.pll_range = range;
|
settings.clock_settings_1.pll_range = range;
|
||||||
@ -134,10 +156,7 @@ class TLV320AIC3204{
|
|||||||
void setLineOutVolumeL(int volume);
|
void setLineOutVolumeL(int volume);
|
||||||
void setLineOutVolumeR(int volume);
|
void setLineOutVolumeR(int volume);
|
||||||
|
|
||||||
bool debug = false;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TwoWire *i2c;
|
|
||||||
|
|
||||||
TLV320AIC3204_Settings settings;
|
TLV320AIC3204_Settings settings;
|
||||||
|
|
||||||
|
|||||||
@ -4,9 +4,12 @@
|
|||||||
*/
|
*/
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <I2S.h>
|
#include <I2S.h>
|
||||||
|
#include "codec.h"
|
||||||
|
|
||||||
I2S i2s(INPUT_PULLUP);
|
I2S i2s(INPUT_PULLUP);
|
||||||
|
|
||||||
|
TLV320AIC3204 codec;
|
||||||
|
|
||||||
#define SIZE 16
|
#define SIZE 16
|
||||||
#define ECHO 192000
|
#define ECHO 192000
|
||||||
int16_t buffer[SIZE];
|
int16_t buffer[SIZE];
|
||||||
@ -36,45 +39,6 @@ void codec_receive(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cw(unsigned char first, unsigned char second){
|
|
||||||
Wire1.beginTransmission(0x18);
|
|
||||||
Wire1.write(first);
|
|
||||||
Wire1.write(second);
|
|
||||||
int result = Wire1.endTransmission();
|
|
||||||
Serial.print(0x18, 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, size_t len){
|
|
||||||
Wire1.beginTransmission(0x18);
|
|
||||||
Wire1.write(first); // set register for read
|
|
||||||
Wire1.endTransmission(false); // false to not release the line
|
|
||||||
|
|
||||||
Wire1.requestFrom(0x18, len, true); // request bytes from register XY
|
|
||||||
|
|
||||||
Serial.print(first, HEX);
|
|
||||||
Serial.print(" ");
|
|
||||||
|
|
||||||
byte buff[len];
|
|
||||||
Wire1.readBytes(buff, len);
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
Serial.print(buff[i], HEX);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.println(buff[i], BIN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
i2s.setSysClk(48000);
|
i2s.setSysClk(48000);
|
||||||
|
|
||||||
@ -94,7 +58,8 @@ void setup() {
|
|||||||
pinMode(20, OUTPUT); // CODEC reset
|
pinMode(20, OUTPUT); // CODEC reset
|
||||||
digitalWrite(20, HIGH);
|
digitalWrite(20, HIGH);
|
||||||
|
|
||||||
|
codec.begin(&Wire1);
|
||||||
|
/*
|
||||||
// GENERAL
|
// GENERAL
|
||||||
cw(0x00, 0x00); // select page 0
|
cw(0x00, 0x00); // select page 0
|
||||||
cw(0x01, 0x01); // soft reset
|
cw(0x01, 0x01); // soft reset
|
||||||
@ -192,6 +157,8 @@ void setup() {
|
|||||||
Serial.println("P0_42 - Sticky Flags");
|
Serial.println("P0_42 - Sticky Flags");
|
||||||
cr(0x2A, 1);
|
cr(0x2A, 1);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
i2s.onTransmit(codec_transmit);
|
i2s.onTransmit(codec_transmit);
|
||||||
i2s.onReceive(codec_receive);
|
i2s.onReceive(codec_receive);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user