a2dp source with internal i2s adc

DonIdaGreat
Posts: 3
Joined: Mon Jul 08, 2019 3:08 pm

a2dp source with internal i2s adc

Postby DonIdaGreat » Sat Jul 27, 2019 2:36 pm

Dear all,
I am trying to change the a2dp source example to send measured i2s analog signal (e.g. from mic) to the sink device via bluetooth instead of the default noise.
Currently I am trying to change the bt_app_a2d_data_cb() function. Is there some more meaningful way to do this?

My i2s code:

Code: Select all

        i2s_config_t i2s_config = {

           		 .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
       		        .sample_rate = 44100,
       		        .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
       		        .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,                           //1-channel
       		        .communication_format = I2S_COMM_FORMAT_I2S_MSB,
       		        .dma_buf_count = 32,
       			.use_apll = true,
       		        .dma_buf_len = BUF_SIZE,
       		        .intr_alloc_flags = 0,                                                  //Default interrupt priority

           };


           i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
           i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
           i2s_set_sample_rates(I2S_NUM_0, 44100); //test utilité
           adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_6);
           vTaskDelay(5000/portTICK_RATE_MS);

           ESP_LOGI(BT_AV_TAG, "I2S initialized");
And my bt_app_a2d_data_cb() function:

Code: Select all

static int32_t bt_app_a2d_data_cb(uint8_t *data, int32_t i2s_read_len)
{
    if (i2s_read_len < 0 || data == NULL) {
        return 0;
    }

	char* i2s_read_buff = (char*) calloc(i2s_read_len, sizeof(char));

	bytes_read = 0;
	i2s_adc_enable(I2S_NUM_0);
	while(bytes_read == 0)
	{
	i2s_read(I2S_NUM_0,  i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY);
	}
	i2s_adc_disable(I2S_NUM_0);

// taking care of the watchdog//
	TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
	TIMERG0.wdt_feed=1;
	TIMERG0.wdt_wprotect=0;

	uint32_t j = 0;
	uint16_t dac_value = 0;

// change 16bit input signal to 8bit 
    for (int i = 0; i < i2s_read_len; i += 2) {
        dac_value = ((((uint16_t) (i2s_read_buff[i + 1] & 0xf) << 8) | ((i2s_read_buff[i + 0]))));
        data[j] = (uint8_t) dac_value * 256 / 4096;
        j++;
    }

// testing for loop
	//uint8_t da = 0;
		//for (int i = 0; i < i2s_read_len; i++) {
		//	data[i] = (uint8_t) (i2s_read_buff[i] >> 8);//  & 0xff;
		    //   da++;
		   //  if(da>254) da=0;
		//}

	free(i2s_read_buff);
	i2s_read_buff = NULL;


    return i2s_read_len;
}
This does not work however....
If I uncomment the for loop related to da variable, I can hear the sawtooth sound from the sink device.
Any ideas what to do?

Thanks!


Output from monitor:

Code: Select all

MONITOR
--- idf_monitor on COM11 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6460
load:0x40078000,len:11356
ho 0 tail 12 room 4
load:0x40080400,len:6716
entry 0x40080768
I (30) boot: ESP-IDF v4.0-dev-728-g826ff7186-dirty 2nd stage bootloader
I (30) boot: compile time 12:46:34
I (31) boot: Enabling RNG early entropy source...
I (36) boot: SPI Speed      : 40MHz
I (41) boot: SPI Mode       : DIO
I (45) boot: SPI Flash Size : 4MB
I (49) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset   Length
I (60) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (74) boot:  2 factory          factory app      00 00 00010000 00100000
I (82) boot: End of partition table
I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x29534 (169268) map
I (154) esp_image: segment 1: paddr=0x0003955c vaddr=0x3ffbdb60 size=0x02ab0 ( 10928) load
I (159) esp_image: segment 2: paddr=0x0003c014 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _WindowOverflow4 at C:/msys32/home/lk/esp/esp-idf/components/freertos/xtensa_vectors.S:1778

I (162) esp_image: segment 3: paddr=0x0003c41c vaddr=0x40080400 size=0x03bf4 ( 15348) load
I (176) esp_image: segment 4: paddr=0x00040018 vaddr=0x400d0018 size=0x86df4 (552436) map
0x400d0018: _flash_cache_start at ??:?

I (372) esp_image: segment 5: paddr=0x000c6e14 vaddr=0x40083ff4 size=0x0ddbc ( 56764) load
0x40083ff4: set_chan_dig_gain at ??:?

I (407) boot: Loaded app from partition at offset 0x10000
I (407) boot: Disabling RNG early entropy source...
I (407) cpu_start: Pro cpu up.
I (411) cpu_start: Application information:
I (416) cpu_start: Project name:     a2dp_source
I (421) cpu_start: App version:      1
I (425) cpu_start: Compile time:     Jul 16 2019 12:45:57
I (431) cpu_start: ELF file SHA256:  80700eecd38b273e...
I (437) cpu_start: ESP-IDF:          v4.0-dev-728-g826ff7186-dirty
I (444) cpu_start: Starting app cpu, entry point is 0x4008105c
0x4008105c: call_start_cpu1 at C:/msys32/home/lk/esp/esp-idf/components/esp32/cpu_start.c:265

I (0) cpu_start: App cpu up.
I (455) heap_init: Initializing. RAM available for dynamic allocation:
I (461) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (468) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (474) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (480) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (486) heap_init: At 3FFCE630 len 000119D0 (70 KiB): DRAM
I (492) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (498) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (505) heap_init: At 40091DB0 len 0000E250 (56 KiB): IRAM
I (511) cpu_start: Pro cpu start user code
I (529) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (552) BTDM_INIT: BT controller compile version [aa078a0]
I (552) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (642) phy: phy_version: 4100, 2a5dd04, Jan 23 2019, 21:00:07, 0, 0
I (1032) BT_AV: Starting device discovery...
I (1032) I2S: DMA Malloc info, datalen=blocksize=1024, dma_buf_count=32
I (1032) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (1052) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (1062) BT_AV: Discovery started.
I (5082) BT_AV: Scanned device: c4:4f:33:17:06:13
I (5082) BT_AV: --Class of Device: 0x240414
I (5082) BT_AV: --RSSI: -20
I (5092) BT_AV: Found a target device, address c4:4f:33:17:06:13, name ESP_SPEAKER
I (5092) BT_AV: Cancel device discovery ...
I (5102) BT_AV: Device discovery stopped.
I (5102) BT_AV: a2dp connecting to peer: ESP_SPEAKER
E (5112) BT_APPL: reset flags
E (5842) BT_APPL: bta_av_rc_create ACP handle exist for shdl:0
I (6062) BT_AV: I2S initialized
W (6072) BT_APPL: new conn_srvc id:18, app_id:0
I (6082) BT_AV: a2dp connected
E (6282) BT_APPL: bta_dm_pm_btm_status hci_status=12
W (6442) BT_APPL: bta_av_proc_meta_cmd unhandled RC vendor PDU: 0x20
I (16062) BT_AV: a2dp media ready checking ...
I (16062) BT_AV: a2dp media ready, starting ...
W (16062) BT_APPL: new conn_srvc id:18, app_id:1
I (16092) BT_LOG: bta_av_link_role_ok hndl:x41 role:0 conn_audio:x1 bits:1 features:x824b

I (16092) BT_AV: a2dp media start successfully.
I (16102) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (16112) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (16122) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (16132) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (16152) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0
I (16162) I2S: APLL: Req RATE: 44100, real rate: 22049.982, BITS: 16, CLKM: 1, BCK_M: 8, MCLK: 5644795.500, SCLK: 705599.437500, diva: 1, divb: 0

Who is online

Users browsing this forum: No registered users and 97 guests