From d8662d59a077dfcf88a2bbd1cff4b7db4ed67068 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 9 Jan 2019 21:28:41 -0700 Subject: [PATCH 1/2] An example to read high frequency analog data using i2s_adc --- .../examples/I2S/HiFreq_ADC/HiFreq_ADC.ino | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 libraries/ESP32/examples/I2S/HiFreq_ADC/HiFreq_ADC.ino diff --git a/libraries/ESP32/examples/I2S/HiFreq_ADC/HiFreq_ADC.ino b/libraries/ESP32/examples/I2S/HiFreq_ADC/HiFreq_ADC.ino new file mode 100644 index 00000000000..83538db59bb --- /dev/null +++ b/libraries/ESP32/examples/I2S/HiFreq_ADC/HiFreq_ADC.ino @@ -0,0 +1,71 @@ +/* + * This is an example to read analog data at high frequency using the I2S peripheral + * Run a wire between pins 27 & 32 + * The readings from the device will be 12bit (0-4096) + */ +#include + +#define I2S_SAMPLE_RATE 78125 +#define ADC_INPUT ADC1_CHANNEL_4 //pin 32 +#define OUTPUT_PIN 27 +#define OUTPUT_VALUE 3800 +#define READ_DELAY 10000 //microseconds + +void i2sInit() +{ + i2s_config_t i2s_config = { + .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN), + .sample_rate = I2S_SAMPLE_RATE, // The format of the signal using ADC_BUILT_IN + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB + .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, + .communication_format = I2S_COMM_FORMAT_I2S_MSB, + .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, + .dma_buf_count = 4, + .dma_buf_len = 8, + .use_apll = false, + .tx_desc_auto_clear = false, + .fixed_mclk = 0 + }; + i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); + i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT); + i2s_adc_enable(I2S_NUM_0); +} + +void reader(void *pvParameters) { + uint32_t read_counter = 0; + uint64_t read_sum = 0; + while(1){ + size_t bytes_read = 0; + uint16_t buffer = 0; + i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, portMAX_DELAY); + buffer = ~buffer; // The data is inverted + //Serial.println(buffer % 0x1000); + read_sum += buffer % 0x1000; // The 4 high bits are the channel + read_counter++; + if (bytes_read != sizeof(buffer)) Serial.println("buffer empty!"); + if (read_counter == I2S_SAMPLE_RATE) { + Serial.printf("avg: %d\n", read_sum/I2S_SAMPLE_RATE); + read_counter = 0; + read_sum = 0; + i2s_adc_disable(I2S_NUM_0); + delay(READ_DELAY); + i2s_adc_enable(I2S_NUM_0); + + } + } +} + +void setup() { + Serial.begin(115200); + // Put a signal out on pin + uint32_t freq = ledcSetup(0, I2S_SAMPLE_RATE, 10); + Serial.printf("Output frequency: %d\n", freq); + ledcWrite(0, OUTPUT_VALUE/4); + ledcAttachPin(OUTPUT_PIN, 0); + // Initialize the I2S peripheral + i2sInit(); + // Create a task that will read the data + xTaskCreatePinnedToCore(reader, "ADC_reader", 2048, NULL, 1, NULL, 1); +} + +void loop() {} From 8151b503441c621397895109c18da8d3c8b7e933 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 16 Feb 2019 12:17:42 -0700 Subject: [PATCH 2/2] Changed random() to use Lemire's method for bounding --- cores/esp32/WMath.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index b1099b67e66..891a737d7c9 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -37,10 +37,23 @@ void randomSeed(unsigned long seed) long random(long howbig) { - if(howbig == 0) { - return 0; + uint32_t x = esp_random(); + uint64_t m = uint64_t(x) * uint64_t(howbig); + uint32_t l = uint32_t(m); + if (l < howbig) { + uint32_t t = -howbig; + if (t >= howbig) { + t -= howbig; + if (t >= howbig) + t %= howbig; + } + while (l < t) { + x = esp_random(); + m = uint64_t(x) * uint64_t(howbig); + l = uint32_t(m); + } } - return esp_random() % howbig; + return m >> 32; } long random(long howsmall, long howbig)