Can Not Use I2S ADC with Wifi

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Can Not Use I2S ADC with Wifi

Postby physiii » Sun May 31, 2020 12:04 pm

I started with the i2s_adc_dac example then added the wifi station example. However, when I hit esp_wifi_start() my ADC reading goes to 0.

Documentation clearly states ADC2 can not be used with Wifi but I'm using ADC1.

Any idea's on what is going on?

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Re: Can Not Use I2S ADC with Wifi

Postby physiii » Mon Jun 01, 2020 11:32 am

I get data for the first couple executions of i2s_read then it outputs a single value, see below. The value it settles on is different each time I reset the esp32. Again, this only happens when running wifi at the same time.

I tried changing wifi settings in menuconfig like optimizations, cores, flash, but saw no change.

Is there something I can try in menuconfig or is this a more fundamental problem?

Code: Select all

======
0f 37 0f 37 f9 36 f9 36 
ff 36 ff 36 ff 36 ff 36 
13 37 13 37 d9 36 d9 36 
f7 36 f7 36 d9 36 d9 36 
3e 37 3e 37 d5 36 d5 36 
1a 37 1a 37 3f 37 3f 37 
e3 36 e3 36 e7 36 e7 36 
c7 36 c7 36 ff 36 ff 36 
======
I (2405) wifi:state: init -> auth (b0)
I (2415) wifi:state: auth -> assoc (0)
I (2425) wifi:state: assoc -> run (10)
I (2445) wifi:connected with Echo_42, aid = 2, channel 6, BW20, bssid = 80:2a:a8:41:64:cd
I (2445) wifi:security type: 4, phy: bgn, rssi: -45
I (2445) wifi:pm start, type: 1

I (2505) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (3105) esp_netif_handlers: sta ip: 192.168.1.66, mask: 255.255.255.0, gw: 192.168.1.1
I (3105) wifi station: got ip:192.168.1.66
I (3105) wifi station: connected to ap SSID:Echo_42 password:qscwdvpk
EC Reading: 4345	Value: 979
Free memory: 157392 bytes
======
1d 38 1d 38 c5 38 c5 38 
7f 38 7f 38 f0 38 f0 38 
e0 38 e0 38 7f 38 7f 38 
53 38 53 38 a7 37 a7 37 
60 37 60 37 a7 36 a7 36 
00 36 00 36 8e 35 8e 35 
ff 34 ff 34 ff 34 ff 34 
c7 34 c7 34 d8 34 d8 34 
======
EC Reading: 5105	Value: 587
Free memory: 157388 bytes
======
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
======
EC Reading: 5561	Value: 352
Free memory: 157388 bytes
======
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
======
EC Reading: 5834	Value: 211
Free memory: 157392 bytes
======
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
======
EC Reading: 5999	Value: 126
Free memory: 157392 bytes
======
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
7e 37 7e 37 7e 37 7e 37 
======


physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Re: Can Not Use I2S ADC with Wifi

Postby physiii » Tue Jun 02, 2020 5:13 pm

Also tried wrapping i2s_read() in i2s_adc_enable and i2s_adc_disable but that did not work.

Here is the entire striped down example file I am using. You can see where I launch the wifi station at the bottom of app_main

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_spi_flash.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_partition.h"
#include "driver/i2s.h"
#include "driver/adc.h"
#include "audio_example_file.h"
#include "esp_adc_cal.h"

static const char* TAG = "ad/da";
#include "station/station_example_main.c"

#define V_REF   1100
#define ADC1_TEST_CHANNEL (ADC1_CHANNEL_7)

#define EXAMPLE_I2S_NUM           (0)
#define EXAMPLE_I2S_SAMPLE_RATE   (16000)
#define EXAMPLE_I2S_SAMPLE_BITS   (16)
#define EXAMPLE_I2S_BUF_DEBUG     (1)
#define EXAMPLE_I2S_READ_LEN      (16 * 1024)
#define EXAMPLE_I2S_FORMAT        (I2S_CHANNEL_FMT_RIGHT_LEFT)
#define EXAMPLE_I2S_CHANNEL_NUM   ((EXAMPLE_I2S_FORMAT < I2S_CHANNEL_FMT_ONLY_RIGHT) ? (2) : (1))
#define I2S_ADC_UNIT              ADC_UNIT_1
#define I2S_ADC_CHANNEL           ADC1_CHANNEL_7

void example_i2s_init(void)
{
     int i2s_num = EXAMPLE_I2S_NUM;
     i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN,
        .sample_rate =  EXAMPLE_I2S_SAMPLE_RATE,
        .bits_per_sample = EXAMPLE_I2S_SAMPLE_BITS,
        .communication_format = I2S_COMM_FORMAT_PCM,
        .channel_format = EXAMPLE_I2S_FORMAT,
        .intr_alloc_flags = 0,
        .dma_buf_count = 2,
        .dma_buf_len = 1024,
        .use_apll = 1,
     };
     //install and start i2s driver
     i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
     i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
     i2s_set_adc_mode(I2S_ADC_UNIT, I2S_ADC_CHANNEL);
}

void example_disp_buf(uint8_t* buf, int length)
{
#if EXAMPLE_I2S_BUF_DEBUG
    printf("======\n");
    for (int i = 0; i < length; i++) {
        printf("%02x ", buf[i]);
        if ((i + 1) % 8 == 0) {
            printf("\n");
        }
    }
    printf("======\n");
#endif
}

int example_i2s_dac_data_scale(uint8_t* d_buff, uint8_t* s_buff, uint32_t len)
{
    uint32_t j = 0;
#if (EXAMPLE_I2S_SAMPLE_BITS == 16)
    for (int i = 0; i < len; i++) {
        d_buff[j++] = 0;
        d_buff[j++] = s_buff[i];
    }
    return (len * 2);
#else
    for (int i = 0; i < len; i++) {
        d_buff[j++] = 0;
        d_buff[j++] = 0;
        d_buff[j++] = 0;
        d_buff[j++] = s_buff[i];
    }
    return (len * 4);
#endif
}

void example_i2s_adc_dac(void*arg)
{
    int i2s_read_len = EXAMPLE_I2S_READ_LEN;
    int flash_wr_size = 0;
    size_t bytes_read, bytes_written;

    uint8_t* flash_read_buff = (uint8_t*) calloc(i2s_read_len, sizeof(char));
    uint8_t* i2s_write_buff = (uint8_t*) calloc(i2s_read_len, sizeof(char));

    while (1) {
        printf("Playing file example: \n");
        int offset = 0;
        int tot_size = sizeof(audio_table);
        while (offset < tot_size) {
            int play_len = ((tot_size - offset) > (4 * 1024)) ? (4 * 1024) : (tot_size - offset);
            int i2s_wr_len = example_i2s_dac_data_scale(i2s_write_buff, (uint8_t*)(audio_table + offset), play_len);
            i2s_write(EXAMPLE_I2S_NUM, i2s_write_buff, i2s_wr_len, &bytes_written, portMAX_DELAY);
            offset += play_len;
				}
		}
}

void adc_read_task(void* arg)
{
    size_t bytes_read;
    int i2s_read_len = EXAMPLE_I2S_READ_LEN;
		char* i2s_read_buff = (char*) calloc(i2s_read_len, sizeof(char));
    while(1) {
				// i2s_adc_enable(EXAMPLE_I2S_NUM);
				i2s_read(EXAMPLE_I2S_NUM, (void*) i2s_read_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
				example_disp_buf((uint8_t*) i2s_read_buff, 64);
				// i2s_adc_disable(EXAMPLE_I2S_NUM);
    }
}

esp_err_t app_main(void)
{
    example_i2s_init();
    esp_log_level_set("I2S", ESP_LOG_INFO);
    xTaskCreate(example_i2s_adc_dac, "example_i2s_adc_dac", 1024 * 2, NULL, 5, NULL);
    xTaskCreate(adc_read_task, "ADC read task", 2048, NULL, 5, NULL);
    station_main();
    return ESP_OK;
}


WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Can Not Use I2S ADC with Wifi

Postby WiFive » Wed Jun 03, 2020 2:28 pm


Who is online

Users browsing this forum: No registered users and 115 guests