Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2c readback and write command #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,58 @@ static void cmd_gain(BaseSequentialStream *chp, int argc, char *argv[])
tlv320aic3204_set_gain(lvalue, rvalue);
}

static int atoh(char *str){
int x = *str++;
if(x == 0) return 0;
if(x > 'f') return 0;
if(x >= 'a') x -= 'a' - 10;
if(x > 'F') return 0;
if(x >= 'A') x -= 'A' - 10;
if(x >= '0') x -= '0';
if(x >= 16) x = 0;
int y = *str;
if(y == 0) return x;
if(y > 'f') return x;
if(y >= 'a') y -= 'a' - 10;
if(y > 'F') return x;
if(y >= 'A') y -= 'A' - 10;
if(y >= '0') y -= '0';
if(y >= 16) return x;
return ((x << 4) | y);
}

extern void tlv320aic3204_bulk_write(const uint8_t *buf, int len);
extern int tlv320aic3204_read(uint8_t d0);

static void cmd_i2c(BaseSequentialStream *chp, int argc, char *argv[])
{
int i;
/*
* If you use this command, you may need to stop scanning,
* because of gain settings etc. during scanning.
*/

if (argc == 0) {
chprintf(chp, "usage: i2c reg val1 [val2 [val3 [val4]]] (write)\r\n i2c reg (read)\r\n");
return;
}
if (argc == 1) {
int x = atoh(*argv++);
x = tlv320aic3204_read(x);
chprintf(chp, "0x%02x\r\n", x);
return;
}
if (argc < 6) {
uint8_t buf[6];
uint8_t* p = buf;
for(i = 0; i < argc; i++){
*p++ = atoh(*argv++);
}
tlv320aic3204_bulk_write(buf, argc);
return;
}
}

static void cmd_port(BaseSequentialStream *chp, int argc, char *argv[])
{
int port;
Expand Down Expand Up @@ -1990,6 +2042,7 @@ static const ShellCommand commands[] =
{ "port", cmd_port },
{ "stat", cmd_stat },
{ "gain", cmd_gain },
{ "i2c", cmd_i2c },
{ "power", cmd_power },
{ "sample", cmd_sample },
//{ "gamma", cmd_gamma },
Expand Down
22 changes: 11 additions & 11 deletions tlv320aic3204.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ static const uint8_t conf_data_clk[] = {
//2, 0x3c, 25, /* Set the DAC Mode to PRB_P25 */
2, 0x1b, 0x0c, /* Set the BCLK,WCLK as output */
2, 0x1e, 0x80 + 28, /* Enable the BCLKN divider with value 28 */
2, 0x25, 0xee, /* DAC power up */

#if 0
2, 0x25, 0xee, /* DAC power up */ // read only. page0/reg0x3F for DAC power up.
#endif
2, 0x12, 0x82, /* Power up the NADC divider with value 2 */
2, 0x13, 0x87, /* Power up the MADC divider with value 7 */
2, 0x14, 0x80, /* Program the OSR of ADC to 128 */
2, 0x3d, 0x01, /* Select ADC PRB_R1 */
2, 0x00, 0x08,
2, 0x01, 0x04, /* Enable ADC adaptive mode */
0 // sentinel
};

Expand All @@ -74,8 +77,8 @@ static const uint8_t conf_data_routing[] = {
2, 0x36, 0x10, /* Route IN2R to LEFT_N with 10K */
2, 0x37, 0x04, /* Route IN3R to RIGHT_P with 10K */
2, 0x39, 0x04, /* Route IN3L to RIGHT_N with 10K */
2, 0x3b, 0, /* Unmute Left MICPGA, Gain selection of 32dB to make channel gain 0dB */
2, 0x3c, 0, /* Unmute Right MICPGA, Gain selection of 32dB to make channel gain 0dB */
2, 0x3b, 0, /* Unmute Left MICPGA, Gain selection of 47.5dB to make channel gain 0dB */
2, 0x3c, 0, /* Unmute Right MICPGA, Gain selection of 47.5dB to make channel gain 0dB */
0 // sentinel
};

Expand All @@ -86,27 +89,24 @@ const uint8_t conf_data_unmute[] = {
0 // sentinel
};

static void
tlv320aic3204_bulk_write(const uint8_t *buf, int len)
void tlv320aic3204_bulk_write(const uint8_t *buf, int len)
{
int addr = AIC3204_ADDR;
i2cAcquireBus(&I2CD1);
(void)i2cMasterTransmitTimeout(&I2CD1, addr, buf, len, NULL, 0, 1000);
i2cReleaseBus(&I2CD1);
}

#if 0
static int
tlv320aic3204_read(uint8_t d0)
int tlv320aic3204_read(uint8_t d0)
{
int addr = AIC3204_ADDR;
uint8_t buf[] = { d0 };
i2cAcquireBus(&I2CD1);
i2cMasterTransmitTimeout(&I2CD1, addr, buf, 1, buf, 1, 1000);
i2cMasterTransmitTimeout(&I2CD1, addr, buf, 1, NULL, 0, 1000);
i2cMasterReceiveTimeout(&I2CD1, addr, buf, 1, 1000);
i2cReleaseBus(&I2CD1);
return buf[0];
}
#endif

static void
tlv320aic3204_config(const uint8_t *data)
Expand Down