ESP32 missing some main while cycles

Thread Starter

Vilius_Zalenas

Joined Jul 24, 2022
173
Hi, not sure if its the best article name or even forum for this thread... Since I am not sure where the problem lies, I hope you wont mind this post here, I can only guess the problem direction...

I am using ESP32 ethernet devboard (ESP32 WROOVER E) with ADC12020 adc to sample Gaussian shape pulses (10 us duration) from a very specific sensor... My ADC12020 is configured for single ended operation at 12 bits of resolution, 0-4V analog voltage range (it has dedicated digital supply of 3.3V, so no worries about the voltage levels), so a value of 1 depicts 1mV.

In order not to waist precious MCU timing and delays, I am not using gpio_set/get_level commands. Instead I am using a direct register manipulation commands with parallel reading. This way I am able to get around 10 samples per pulse. I am generating the clock for the ADC from the same ESP as well:

toggle clock gpio output
read all 12 bits of the ADC
toggle clock gpio output again
.
.
.
I am storing my data in circular buffer - I am always sampling, and only the interrupt pulse tells me when I have captured a pulse and the sampled data packet should be saved in the memory for further processing... Interrupt pulse is generated by some analog comparator (cant remember the exact MPR). It generates a digital signal which fully covers the Gaussian pulse, the ESP gets the interrupt by the falling edge of that pulse - its a sign that X last values contain valuable information and should be copied to the memory... Until here, everything is tested and verified, my comparator works like a clock, ESP32 gets the interrupts at the right moment.

Finally my problem: when I print out my circular buffer data after the pulse, I do get some values indicating voltage spikes (values over 1000) but they are really messy and inconsistent... Pulse form is always the same, but here are some of my logs (there is a 200 mV offset on the signal, so background reading of 200 is normal) :

-----------------------------------------------------------
Cycle 1: 201
Cycle 2: 205
Cycle 3: 205
Cycle 4: 202
Cycle 5: 207
Cycle 6: 201
Cycle 7: 207
Cycle 8: 203
Cycle 9: 201
Cycle 10: 203
Cycle 11: 197
Cycle 12: 205
Cycle 13: 206
Cycle 14: 205
Cycle 15: 201
Cycle 16: 206
Cycle 17: 195
Cycle 18: 201
Cycle 19: 204
Cycle 20: 202
Cycle 21: 202
Cycle 22: 202
Cycle 23: 201
Cycle 24: 200
Cycle 25: 207
Cycle 26: 201
Cycle 27: 204
Cycle 28: 193
Cycle 29: 197
Cycle 30: 196
Cycle 31: 202
Cycle 32: 205
Cycle 33: 202
Cycle 34: 203
Cycle 35: 197
Cycle 36: 196
Cycle 37: 203
Cycle 38: 204
Cycle 39: 202
Cycle 40: 204
Cycle 41: 388
Cycle 42: 1094
Cycle 43: 1998
Cycle 44: 655
Cycle 45: 781
Cycle 46: 202
Cycle 47: 201
Cycle 48: 197
Cycle 49: 201
Cycle 50: 206
Cycle 51: 202
Cycle 52: 197
Cycle 53: 202
Cycle 54: 198
Cycle 55: 202
Cycle 56: 203
Cycle 57: 207
Cycle 58: 201
Cycle 59: 204
Cycle 60: 195
Cycle 61: 200
Cycle 62: 192
Cycle 63: 203
Cycle 64: 202
Cycle 65: 201
Cycle 66: 200
Cycle 67: 201
Cycle 68: 203
Cycle 69: 205
Cycle 70: 202
Cycle 71: 203
Cycle 72: 200
Cycle 73: 194
Cycle 74: 203
Cycle 75: 192
Cycle 76: 202
Cycle 77: 199
Cycle 78: 202
Cycle 79: 197
Cycle 80: 202
-----------------------------------------------------------
Cycle 1: 193
Cycle 2: 199
Cycle 3: 198
Cycle 4: 198
Cycle 5: 201
Cycle 6: 203
Cycle 7: 207
Cycle 8: 202
Cycle 9: 202
Cycle 10: 206
Cycle 11: 198
Cycle 12: 324
Cycle 13: 971
Cycle 14: 1928
Cycle 15: 579
Cycle 16: 771
Cycle 17: 202
Cycle 18: 195
Cycle 19: 201
Cycle 20: 205
Cycle 21: 201
Cycle 22: 196
Cycle 23: 201
Cycle 24: 206
Cycle 25: 200
Cycle 26: 203
Cycle 27: 192
Cycle 28: 201
Cycle 29: 197
Cycle 30: 204
Cycle 31: 204
Cycle 32: 201
Cycle 33: 202
Cycle 34: 200
Cycle 35: 195
Cycle 36: 203
Cycle 37: 205
Cycle 38: 204
Cycle 39: 197
Cycle 40: 200
Cycle 41: 199
Cycle 42: 192
Cycle 43: 204
Cycle 44: 198
Cycle 45: 198
Cycle 46: 205
Cycle 47: 202
Cycle 48: 196
Cycle 49: 202
Cycle 50: 205
Cycle 51: 200
Cycle 52: 200
Cycle 53: 197
Cycle 54: 204
Cycle 55: 207
Cycle 56: 192
Cycle 57: 195
Cycle 58: 200
Cycle 59: 200
Cycle 60: 198
Cycle 61: 202
Cycle 62: 192
Cycle 63: 205
Cycle 64: 197
Cycle 65: 199
Cycle 66: 198
Cycle 67: 204
Cycle 68: 205
Cycle 69: 201
Cycle 70: 199
Cycle 71: 204
Cycle 72: 200
Cycle 73: 198
Cycle 74: 206
Cycle 75: 192
Cycle 76: 205
Cycle 77: 200
Cycle 78: 195
Cycle 79: 196
Cycle 80: 204
-----------------------------------------------------------
Cycle 1: 206
Cycle 2: 206
Cycle 3: 205
Cycle 4: 202
Cycle 5: 204
Cycle 6: 201
Cycle 7: 204
Cycle 8: 200
Cycle 9: 201
Cycle 10: 205
Cycle 11: 205
Cycle 12: 197
Cycle 13: 202
Cycle 14: 202
Cycle 15: 205
Cycle 16: 204
Cycle 17: 199
Cycle 18: 199
Cycle 19: 193
Cycle 20: 200
Cycle 21: 201
Cycle 22: 200
Cycle 23: 206
Cycle 24: 204
Cycle 25: 205
Cycle 26: 200
Cycle 27: 201
Cycle 28: 201
Cycle 29: 202
Cycle 30: 201
Cycle 31: 204
Cycle 32: 204
Cycle 33: 204
Cycle 34: 200
Cycle 35: 199
Cycle 36: 200
Cycle 37: 193
Cycle 38: 202
Cycle 39: 198
Cycle 40: 201
Cycle 41: 203
Cycle 42: 202
Cycle 43: 205
Cycle 44: 201
Cycle 45: 207
Cycle 46: 203
Cycle 47: 205
Cycle 48: 201
Cycle 49: 207
Cycle 50: 206
Cycle 51: 205
Cycle 52: 200
Cycle 53: 202
Cycle 54: 201
Cycle 55: 203
Cycle 56: 205
Cycle 57: 202
Cycle 58: 203
Cycle 59: 198
Cycle 60: 200
Cycle 61: 205
Cycle 62: 204
Cycle 63: 205
Cycle 64: 207
Cycle 65: 580
Cycle 66: 1414
Cycle 67: 259
Cycle 68: 773
Cycle 69: 781
Cycle 70: 202
Cycle 71: 199
Cycle 72: 200
Cycle 73: 204
Cycle 74: 203
Cycle 75: 203
Cycle 76: 202
Cycle 77: 205
Cycle 78: 203
Cycle 79: 201
Cycle 80: 198
-----------------------------------------------------------

As you see, the voltages spikes are depicted in maximum of 3-4 samples (something like 500-1300-300 and the numbers wander every time so much...) I never hit the value of 2000, but I can clearly see my signal coming all the way up to 3V However, the oscilloscope shows I am indeed getting at least 10 samples per pulse, so my logs should depict the pulses with more evenly distributed values (something like 200 - 500 - 1200 - 2000 - 2700 - 2700 - 2000 - 1200 - 500 - 200 or similar). I have done many test with my ADC on DC, the ADC works fine it really shifts its output bits according to the voltage level. Noise and PCB design can not compensate for this great of an error. I cant say that this design does not work at all, but thats way too far from what I am really after.... Any observations on this strange behavior?

ESP32 code:


C:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "soc/soc.h"
#include "soc/gpio_struct.h"
#include "soc/gpio_reg.h"
#include "esp32/rom/ets_sys.h"
#include "soc/rtc.h"

#define INTERRUPT_INPUT 15
#define GPIO_CLOCK 14
#define NUM_GPIO_PINS 12
#define NUM_CYCLES 80

#define GPIO_PIN_12 12   //LSB
#define GPIO_PIN_13 13
#define GPIO_PIN_2 2
#define GPIO_PIN_4 4
#define GPIO_PIN_16 16
#define GPIO_PIN_17 17
#define GPIO_PIN_32 32
#define GPIO_PIN_33 33
#define GPIO_PIN_34 34
#define GPIO_PIN_35 35
#define GPIO_PIN_36 36
#define GPIO_PIN_39 39   //MSB

const uint32_t gpio_pins[NUM_GPIO_PINS] = {
    12, 13, 2, 4, 16, 17, 32, 33, 34, 35, 36, 39
};

static const gpio_num_t GPIO_PINS[NUM_GPIO_PINS] = {
    GPIO_PIN_12, GPIO_PIN_13, GPIO_PIN_2, GPIO_PIN_4,
    GPIO_PIN_16, GPIO_PIN_17, GPIO_PIN_32, GPIO_PIN_33,
    GPIO_PIN_34, GPIO_PIN_35, GPIO_PIN_36, GPIO_PIN_39
};

bool trigger = false;
uint32_t data_array[NUM_CYCLES] = {0};

static void IRAM_ATTR gpio_interrupt_handler(void *args)
{
    trigger = true;
}

void disableWatchdog()
{
    WRITE_PERI_REG(RTC_CNTL_WDTCONFIG0_REG, 0);
    WRITE_PERI_REG(RTC_CNTL_WDTCONFIG1_REG, 0);
}

void app_main()
{
    vTaskDelay(200 / portTICK_PERIOD_MS);
    esp_rom_gpio_pad_select_gpio(INTERRUPT_INPUT);
    gpio_set_direction(INTERRUPT_INPUT, GPIO_MODE_INPUT);
    gpio_pulldown_en(INTERRUPT_INPUT);
    gpio_pullup_dis(INTERRUPT_INPUT);
    gpio_set_intr_type(INTERRUPT_INPUT, GPIO_INTR_NEGEDGE);

    gpio_install_isr_service(0);
    gpio_isr_handler_add(INTERRUPT_INPUT, gpio_interrupt_handler, (void *)INTERRUPT_INPUT);

    gpio_config_t io_conf_output = {
        .pin_bit_mask = (1ULL << GPIO_CLOCK),
        .mode = GPIO_MODE_OUTPUT,
    };
    gpio_config(&io_conf_output);

    gpio_config_t io_conf;
    for (int i = 0; i < NUM_GPIO_PINS; ++i)
    {
        io_conf = (gpio_config_t){
            .pin_bit_mask = (1ULL << GPIO_PINS[i]),
            .mode = GPIO_MODE_INPUT,
            .intr_type = GPIO_INTR_DISABLE,
            .pull_up_en = GPIO_PULLUP_DISABLE,
            .pull_down_en = GPIO_PULLDOWN_ENABLE,  // Enable pulldown resistors
        };
        gpio_config(&io_conf);
    }


    uint32_t reg_in = 0;
    uint32_t reg_in1 = 0;
    disableWatchdog();
    int k = 0;

    uint32_t pin12_mask = 1U << 12;
    uint32_t pin13_mask = 1U << 13;
    uint32_t pin2_mask = 1U << 2;
    uint32_t pin4_mask = 1U << 4;
    uint32_t pin16_mask = 1U << 16;
    uint32_t pin17_mask = 1U << 17;
    uint32_t pin32_mask = 1U << (32 - 32);
    uint32_t pin33_mask = 1U << (33 - 32);
    uint32_t pin34_mask = 1U << (34 - 32);
    uint32_t pin35_mask = 1U << (35 - 32);
    uint32_t pin36_mask = 1U << (36 - 32);
    uint32_t pin39_mask = 1U << (39 - 32);

    while (1)
    {
      
  
            REG_WRITE(GPIO_OUT_W1TS_REG, (1 << GPIO_CLOCK));

            reg_in = REG_READ(GPIO_IN_REG);
            reg_in1 = REG_READ(GPIO_IN1_REG);
            data_array[k] =((reg_in >> 12) & 0x01) | ((reg_in >> 13) & 0x01) << 1 | ((reg_in >> 2) & 0x01) << 2 | ((reg_in >> 4) & 0x01) << 3 | (((reg_in >> 16) & 0x01) ^ 1) << 4 | ((reg_in >> 17) & 0x01) << 5 | ((reg_in1 >> (32 - 32)) & 0x01) << 6 | ((reg_in1 >> (33 - 32)) & 0x01) << 7 | ((reg_in1 >> (34 - 32)) & 0x01) << 8 | ((reg_in1 >> (35 - 32)) & 0x01) << 9 | ((reg_in1 >> (36 - 32)) & 0x01) << 10 | ((reg_in1 >> (39 - 32)) & 0x01);

            REG_WRITE(GPIO_OUT_W1TC_REG, (1 << GPIO_CLOCK));
          
            k = (k + 1) % NUM_CYCLES;

        if (trigger)
        {
          
            for (int i=0; i < NUM_CYCLES; i++)
            {
                printf("Cycle %d: %ld", i + 1, data_array[i]);
                printf("\n");
            /* For printing data in binary
               printf("Cycle %d: ", k + 1);
               for (int i = 31; i >= 0; --i) {
               printf("%ld", (data_array[k] >> i) & 1);
               if (i % 4 == 0 && i > 0) {
                    printf(" ");
                }
                }
                printf("\n");
            */
            }

           printf("-----------------------------------------------------------\n");
           trigger = false;
        }
    }
}
 

Attachments

Last edited:
Top