ADC glitchs - I2S - ESP32
Posted: Mon Aug 06, 2018 2:57 pm
Hello
I'm working on a project with the ESP32.
I have to send data by wifi using TCP/IP protocol.
I tested with simple signal but I have a problem, they're noisy with inexplicable glitchs.
Tested with 40KHz sampling rate. Wanted to work fine even at 100Hz but problem with clkmdiv is too large, don't know how to solve this... (Sorry I'm a frenchy )
Do you have any idea ? I think about a problem with the ADC like in other forums but I'm a little bit blocked
Thanks !
I'm working on a project with the ESP32.
I have to send data by wifi using TCP/IP protocol.
I tested with simple signal but I have a problem, they're noisy with inexplicable glitchs.
Tested with 40KHz sampling rate. Wanted to work fine even at 100Hz but problem with clkmdiv is too large, don't know how to solve this... (Sorry I'm a frenchy )
Code: Select all
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "driver/i2s.h"
#include "soc/syscon_reg.h"
#include "driver/adc.h"
#include "esp_event_loop.h"
#define BUF_LEN 512
#define TAG1 "bytes read"
#define SSID "PCDylanR"
#define PASSPHARSE "motdepasse"
#define TCPServerIP "192.168.137.1"
//#define SAMPLERATE 40000
//PORT3010
static QueueHandle_t i2s_event_queue;
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static const char *TAG="tcp_client";
void wifi_connect(){
wifi_config_t cfg = {
.sta = {
.ssid = SSID,
.password = PASSPHARSE,
},
};
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg) );
ESP_ERROR_CHECK( esp_wifi_connect() );
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
esp_log_level_set("wifi", ESP_LOG_NONE);
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
void app_main()
{
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_event_group = xEventGroupCreate();
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
initialise_wifi();
// Configuration de l'I2S
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
.sample_rate = 40000,
.bits_per_sample = 16,
.use_apll = true,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.intr_alloc_flags =0,
.dma_buf_count =2,
.dma_buf_len = BUF_LEN
i2s_driver_install(I2S_NUM_0, &i2s_config, 1, &i2s_event_queue);
i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
i2s_set_sample_rates(I2S_NUM_0, 40000); //test utilité
SET_PERI_REG_MASK(SYSCON_SARADC_CTRL2_REG, SYSCON_SARADC_SAR1_INV);
adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_11);
vTaskDelay(5000/portTICK_RATE_MS);
i2s_adc_enable(I2S_NUM_0);
ESP_LOGI(TAG,"démarage du mode TCP \n");
struct sockaddr_in tcpServerAddr;
tcpServerAddr.sin_addr.s_addr = inet_addr(TCPServerIP);
tcpServerAddr.sin_family = AF_INET;
tcpServerAddr.sin_port = htons( 3010 ); //Port sur lequel le serveur écoute
int s;
int i2s_read_len = BUF_LEN*2;
size_t bytes_read;
char* i2s_read_buff = (char*) calloc(i2s_read_len,sizeof(char));
i2s_event_t evt;
while(1){
xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY);
s=socket(AF_INET, SOCK_STREAM, 0);
if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS)
{
if (evt.type==I2S_EVENT_RX_DONE)
{
i2s_read(I2S_NUM_0, (void*)i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY);
if(s < 0)
{
ESP_LOGE(TAG, "... Impossible d'alouer un socket.\n");
vTaskDelay(2000 / portTICK_PERIOD_MS);
continue;
}
//ESP_LOGI(TAG, "... socket alloué\n");
if(connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)) != 0)
{
ESP_LOGE(TAG, "... problème de connexion errno=%d \n", errno);
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... connecté \n");
if( send(s , i2s_read_buff , i2s_read_len,0) < 0)
{
ESP_LOGE(TAG, "... Send failed \n");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
//ESP_LOGI(TAG, "... paquet envoyé");
//ESP_LOGI(TAG1,"%d \n", bytes_read);
ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16);
close(s);
}
}
}
}
Thanks !