From 43961851d3fd102487b917bcf3d63afa4d21f87c Mon Sep 17 00:00:00 2001 From: kamiki18 Date: Wed, 27 Nov 2019 00:24:19 +0900 Subject: [PATCH] i2c command --- main.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ tlv320aic3204.c | 22 ++++++++++---------- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 6d52c881..b3cf3042 100644 --- a/main.c +++ b/main.c @@ -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; @@ -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 }, diff --git a/tlv320aic3204.c b/tlv320aic3204.c index cb86ad59..f71add59 100644 --- a/tlv320aic3204.c +++ b/tlv320aic3204.c @@ -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 }; @@ -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 }; @@ -86,8 +89,7 @@ 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); @@ -95,18 +97,16 @@ tlv320aic3204_bulk_write(const uint8_t *buf, int len) 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)