Skip to content

Commit

Permalink
Merge pull request #1 from toniebox-reverse-engineering/develop
Browse files Browse the repository at this point in the history
nightly
  • Loading branch information
SciLor authored Jun 16, 2020
2 parents e1b00d3 + e3f1ecd commit c2f5337
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 22 deletions.
2 changes: 1 addition & 1 deletion BoxConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ConfigStructures.h"
#include "BoxSD.h"

#define BOXCONFIG_JSON_SIZE 382 //Size from https://arduinojson.org/v6/assistant/
#define BOXCONFIG_JSON_SIZE 447 //Size from https://arduinojson.org/v6/assistant/

class BoxConfig {
public:
Expand Down
179 changes: 179 additions & 0 deletions BoxDAC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include "BoxDAC.h"
#include <Wire.h>

void BoxDAC::begin() {
Log.info("Initialize DAC...");
//RESET
pinMode(62, OUTPUT);
digitalWrite(62, HIGH);
delayMicroseconds(1); //Be sure

Wire.begin();


//Extracted from logic analyzer capture of box
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::SOFTWARE_RESET, 0x01); //Self-clearing software reset for control register
send(ADDR_P0_SERIAL::CLOCKGEN_MUX, 0x07);
send(ADDR_P0_SERIAL::PLL_J_VAL, 0x20);
send(ADDR_P0_SERIAL::PLL_D_VAL_MSB, 0x00);
send(ADDR_P0_SERIAL::PLL_D_VAL_LSB, 0x00);
send(ADDR_P0_SERIAL::PLL_P_R_VAL, 0x96);
send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x84);
send(ADDR_P0_SERIAL::DAC_MDAC_VAL, 0x86);
send(ADDR_P0_SERIAL::DAC_DOSR_VAL_MSB, 0x01);
send(ADDR_P0_SERIAL::DAC_DOSR_VAL_LSB, 0x00);
send(ADDR_P0_SERIAL::CODEC_INT_CTRL1, 0x00);
send(ADDR_P0_SERIAL::DAC_PROC_BLOCK_SEL, 0x19);

send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);
send(ADDR_P1_DAC_OUT::HP_OUT_POP_REM_SET, 0x4E);
send(ADDR_P1_DAC_OUT::OUT_PGA_RAMP_DOWN_PER_CTRL, 0x70);
send(ADDR_P1_DAC_OUT::DAC_LR_OUT_MIX_ROUTING, 0x44);
send(ADDR_P1_DAC_OUT::MICBIAS, 0x0B);
send(ADDR_P1_DAC_OUT::HP_DRIVER_CTRL, 0xE0);

send(ADDR::PAGE_CONTROL, PAGE::MCLK_DIVIDER);
send(ADDR_P3_MCLK::TIMER_CLK_MCLK_DIV, 0x01);

send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::HEADSET_DETECT, 0x8C);
send(ADDR_P0_SERIAL::INT1_CTRL_REG, 0x80);
send(ADDR_P0_SERIAL::GPIO1_INOUT_CTRL, 0x14);
//send(0x2E); Excel 161
//read 0x18 addr

send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);
send(ADDR_P1_DAC_OUT::L_VOL_TO_HPL, 0x7F);
send(ADDR_P1_DAC_OUT::R_VOL_TO_HPR, 0x7F);
send(ADDR_P1_DAC_OUT::L_VOL_TO_SPK, 0x7F);
send(ADDR_P1_DAC_OUT::HPL_DRIVER, 0x00);
send(ADDR_P1_DAC_OUT::HPR_DRIVER, 0x00);
send(ADDR_P1_DAC_OUT::SPK_DRIVER, 0x00);
send(ADDR_P1_DAC_OUT::HP_DRIVERS, 0x00);
send(ADDR_P1_DAC_OUT::SPK_AMP, 0x00);
send(ADDR_P1_DAC_OUT::HPL_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::HPR_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::HP_DRIVERS, 0xC2);
send(ADDR_P1_DAC_OUT::L_VOL_TO_HPL, 0x92);
send(ADDR_P1_DAC_OUT::R_VOL_TO_HPR, 0x92);

send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::DAC_DATA_PATH_SETUP, 0xD5);
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, 0xDC);
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, 0xDC);
//Excel 219
// Extract END


//Testing custom code, recommened Initialization code by chip doc
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::SOFTWARE_RESET, 0x01); //Self-clearing software reset for control register
send(ADDR_P0_SERIAL::CLOCKGEN_MUX, 0x03);
send(ADDR_P0_SERIAL::PLL_J_VAL, 0x08);
send(ADDR_P0_SERIAL::PLL_D_VAL_MSB, 0x00); //30 07 00 00
send(ADDR_P0_SERIAL::PLL_D_VAL_LSB, 0x00);
send(ADDR_P0_SERIAL::PLL_P_R_VAL, 0x91);
send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x88);
send(ADDR_P0_SERIAL::DAC_MDAC_VAL, 0x82);
send(ADDR_P0_SERIAL::DAC_DOSR_VAL_MSB, 0x00); //30 0D 00 80
send(ADDR_P0_SERIAL::DAC_DOSR_VAL_LSB, 0x80);

send(ADDR_P0_SERIAL::CODEC_INT_CTRL1, 0x00);
send(ADDR_P0_SERIAL::DAC_PROC_BLOCK_SEL, 0x0B);
send(0x00, 0x08);//TODO!
send(0x01, 0x04);//TODO!

send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::VOL_MICDET_SAR_ADC, 0x00);

send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);
send(ADDR_P1_DAC_OUT::HP_DRIVERS, 0x04);
send(ADDR_P1_DAC_OUT::HP_OUT_POP_REM_SET, 0x4E);
send(ADDR_P1_DAC_OUT::DAC_LR_OUT_MIX_ROUTING, 0x44);
send(ADDR_P1_DAC_OUT::HPL_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::HPR_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::SPK_DRIVER, 0x1C);
send(ADDR_P1_DAC_OUT::HP_DRIVERS, 0xC2);
send(ADDR_P1_DAC_OUT::SPK_AMP, 0x86);
send(ADDR_P1_DAC_OUT::L_VOL_TO_HPL, 0x92);
send(ADDR_P1_DAC_OUT::R_VOL_TO_HPR, 0x92);
send(ADDR_P1_DAC_OUT::L_VOL_TO_SPK, 0x92);

send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::DAC_DATA_PATH_SETUP, 0xD4);
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, 0xD4);
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, 0xD4);
send(ADDR_P0_SERIAL::DAC_VOL_CTRL, 0x00);


/*
send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);
send(ADDR_P1_DAC_OUT::HPL_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::HPR_DRIVER, 0x06);
send(ADDR_P1_DAC_OUT::SPK_DRIVER, 0x1C);
send(ADDR_P1_DAC_OUT::SPK_AMP, 0x86);
send(ADDR_P1_DAC_OUT::L_VOL_TO_HPL, 0x92);
send(ADDR_P1_DAC_OUT::R_VOL_TO_HPR, 0x92);
send(ADDR_P1_DAC_OUT::L_VOL_TO_SPK, 0x92);
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::DAC_DATA_PATH_SETUP, 0xD4);
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, 0xD4);
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, 0xD4);
*/

//recommened cdoe for beep by chip doc
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::DAC_VOL_CTRL, 0x0C); //mute DACs
//f 30 26 xxx1xxx1 # wait for DAC gain flag to be set
delay(500);
send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x02); //power down NDAC divider
for (uint32_t i = 0; i<10; i++) {
send(ADDR_P0_SERIAL::BEEP_L_GEN, 0x80); //enable beep generator with left channel volume = 0dB,
send(ADDR_P0_SERIAL::BEEP_R_GEN, 0x00);
delay(50);
send(ADDR_P0_SERIAL::BEEP_L_GEN, 0x80); //enable beep generator with left channel volume = 0dB,
send(ADDR_P0_SERIAL::BEEP_R_GEN, 0x00);
delay(100);
}
send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x84); //power up NDAC divider
send(ADDR_P0_SERIAL::DAC_VOL_CTRL, 0x00); //unmute DACs



Log.info("...initialized");
}

void BoxDAC::loop() {
}

bool BoxDAC::send_raw(uint8_t data) {
if (!Wire.write(data)) {
Log.error("Could not write into I2C Buffer");
return false;
}
return true;
}
bool BoxDAC::send(uint8_t target_register, uint8_t data) {
//0x30 - 8bit / 0x18 - 7bit
Wire.beginTransmission(0x18);
if (!send_raw(target_register)) return false;
if (!send_raw(data)) return false;

uint8_t result = Wire.endTransmission(false);
if (!result) return true;
Log.error("Could not send I2C buffer, error=%i", result);
}
bool BoxDAC::send(ADDR target_register, PAGE data) {
return send((uint8_t)target_register, (uint8_t)data);
}
bool BoxDAC::send(ADDR_P0_SERIAL target_register, uint8_t data) {
return send((uint8_t)target_register, data);
}
bool BoxDAC::send(ADDR_P1_DAC_OUT target_register, uint8_t data) {
return send((uint8_t)target_register, data);
}
bool BoxDAC::send(ADDR_P3_MCLK target_register, uint8_t data) {
return send((uint8_t)target_register, data);
}
78 changes: 78 additions & 0 deletions BoxDAC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef BoxDAC_h
#define BoxDAC_h

#include "BaseHeader.h"
#include <EnhancedThread.h>

class BoxDAC : public EnhancedThread {
public:
void
begin(),
loop();

private:
enum class PAGE {
SERIAL_IO = 0x00,
DAC_OUT_VOL = 0x01,
MCLK_DIVIDER = 0x03,
DAC_FILTER_DRC_COE_1A = 0x08,
DAC_FILTER_DRC_COE_2A = 0x09,
DAC_FILTER_DRC_COE_1B = 0x0C,
DAC_FILTER_DRC_COE_2B = 0x0D,
};
enum class ADDR {
PAGE_CONTROL = 0x00,
};
enum class ADDR_P0_SERIAL {
SOFTWARE_RESET = 0x01,
CLOCKGEN_MUX = 0x04,
PLL_P_R_VAL = 0x05,
PLL_J_VAL = 0x06,
PLL_D_VAL_MSB = 0x07,
PLL_D_VAL_LSB = 0x08,
DAC_NDAC_VAL = 0x0B,
DAC_MDAC_VAL = 0x0C,
DAC_DOSR_VAL_MSB = 0x0D,
DAC_DOSR_VAL_LSB = 0x0E,
CODEC_INT_CTRL1 = 0x1B,
INT1_CTRL_REG = 0x30,
GPIO1_INOUT_CTRL = 0x33,
DAC_PROC_BLOCK_SEL = 0x3C,
DAC_DATA_PATH_SETUP = 0x3F,
DAC_VOL_CTRL = 0x40,
DAC_VOL_L_CTRL = 0x41,
DAC_VOL_R_CTRL = 0x42,
HEADSET_DETECT = 0x43,
BEEP_L_GEN = 0x47,
BEEP_R_GEN = 0x48,
VOL_MICDET_SAR_ADC = 0x74,
};
enum class ADDR_P1_DAC_OUT {
HP_DRIVERS = 0x1F,
SPK_AMP = 0x20,
HP_OUT_POP_REM_SET = 0x21,
OUT_PGA_RAMP_DOWN_PER_CTRL = 0x22,
DAC_LR_OUT_MIX_ROUTING = 0x23,
L_VOL_TO_HPL = 0x24,
R_VOL_TO_HPR = 0x25,
L_VOL_TO_SPK = 0x26,
HPL_DRIVER = 0x28,
HPR_DRIVER = 0x29,
SPK_DRIVER = 0x2A,
HP_DRIVER_CTRL = 0x2C,
MICBIAS = 0x2E,
};
enum class ADDR_P3_MCLK {
TIMER_CLK_MCLK_DIV = 0x10,
};

bool
send_raw(uint8_t data),
send(uint8_t target_register, uint8_t data),
send(ADDR target_register, PAGE data),
send(ADDR_P0_SERIAL target_register, uint8_t data),
send(ADDR_P1_DAC_OUT target_register, uint8_t data),
send(ADDR_P3_MCLK target_register, uint8_t data);
};

#endif
25 changes: 5 additions & 20 deletions Hackiebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void Hackiebox::setup() {
boxEars.begin();
boxAccel.begin();
boxRFID.begin();
boxDAC.begin();

boxWiFi = WrapperWiFi(config->wifi.ssid, config->wifi.password);
boxWiFi.begin();
Expand All @@ -46,6 +47,7 @@ void Hackiebox::setup() {
Log.info("Config: %s", Config.getAsJson().c_str());

boxAccel.onRun(ThreadCallbackHandler([&]() { boxAccel.loop(); }));
boxDAC.onRun(ThreadCallbackHandler([&]() { boxDAC.loop(); }));
boxRFID.onRun(ThreadCallbackHandler([&]() { boxRFID.loop(); }));
boxPower.onRun(ThreadCallbackHandler([&]() { boxPower.loop(); }));
boxLEDs.onRun(ThreadCallbackHandler([&]() { boxLEDs.loop(); }));
Expand Down Expand Up @@ -83,37 +85,20 @@ void watchdog_handler() {
}
bool Hackiebox::watchdog_start() {
watchdog_feed();
// Enable the peripherals used by this example.
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK);

// Unlock to be able to configure the registers
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK);
MAP_WatchdogUnlock(WDT_BASE);

//if (fpAppWDTCB != NULL) {
MAP_IntPrioritySet(INT_WDT, INT_PRIORITY_LVL_1);
MAP_WatchdogIntRegister(WDT_BASE, watchdog_handler);
//}


// Set the watchdog timer reload value
MAP_IntPrioritySet(INT_WDT, INT_PRIORITY_LVL_1);
MAP_WatchdogIntRegister(WDT_BASE, watchdog_handler);
MAP_WatchdogReloadSet(WDT_BASE, 80000000*15); //15s

// Start the timer. Once the timer is started, it cannot be disable.
MAP_WatchdogEnable(WDT_BASE);

return MAP_WatchdogRunning(WDT_BASE);
}
void Hackiebox::watchdog_stop() {
// Unlock to be able to configure the registers
MAP_WatchdogUnlock(WDT_BASE);

// Disable stalling of the watchdog timer during debug events
MAP_WatchdogStallDisable(WDT_BASE);

// Clear the interrupt
MAP_WatchdogIntClear(WDT_BASE);

// Unregister the interrupt
MAP_WatchdogIntUnregister(WDT_BASE);
}

4 changes: 3 additions & 1 deletion Hackiebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "BoxAccelerometer.h"
#include "BoxBattery.h"
#include "BoxButtonEars.h"
#include "BoxDAC.h"
#include "BoxEvents.h"
#include "BoxLEDs.h"
#include "BoxPower.h"
Expand Down Expand Up @@ -42,7 +43,8 @@ class Hackiebox {

BoxAccelerometer boxAccel;
BoxBattery boxBattery;
BoxButtonEars boxEars;
BoxButtonEars boxEars;
BoxDAC boxDAC;
BoxLEDs boxLEDs;
BoxPower boxPower;
BoxRFID boxRFID;
Expand Down
5 changes: 5 additions & 0 deletions web/hackiebox.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ <h2>Credits</h2>
<td>Creating the cc3200tool</td>
<td></td>
</tr>
<tr>
<td>bluenazgul</td>
<td>Testing</td>
<td></td>
</tr>
<tr>
<td>Telegram Toniebox Hacking Community</td>
<td>THX2YOU</td>
Expand Down

0 comments on commit c2f5337

Please sign in to comment.