Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/event_groups.h"
- #include "esp_log.h"
- #include "driver/i2s.h"
- #include "soc/syscon_reg.h"
- #include "driver/adc.h"
- #include "esp_wifi.h"
- #include "esp_event_loop.h"
- #include "nvs_flash.h"
- #include "sr_gen_sine.h"
- #define TAG "adc_i2s"
- uint32_t SAMPLE_RATE = 120E3;
- uint32_t NUM_SAMPLES = 512;
- uint32_t SIG_FREQ = 10E3;
- static QueueHandle_t i2s_event_queue;
- static EventGroupHandle_t wifi_event_group;
- SigGen *SineWave = nullptr;
- void setup(){
- Serial.begin(921600);
- i2s_config_t i2s_config ;
- i2s_config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
- i2s_config.sample_rate = SAMPLE_RATE; // 120 KHz
- i2s_config.dma_buf_len = NUM_SAMPLES; // 512
- i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT; // Should be mono but doesn't seem to be
- i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
- i2s_config.use_apll = false,
- i2s_config.communication_format = I2S_COMM_FORMAT_I2S;
- i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
- i2s_config.dma_buf_count = 2;
- //install and start i2s driver
- i2s_driver_install(I2S_NUM_0, &i2s_config, 1, &i2s_event_queue);
- // Connect ADC to I2S
- i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_6);
- //i2s_set_clk(I2S_NUM_0,SAMPLE_RATE,I2S_BITS_PER_SAMPLE_16BIT,I2S_CHANNEL_MONO); // This doesn't work either
- // Generate test signal
- SineWave = new SigGen();
- SineWave->channel = DAC_CHANNEL_1;
- SineWave->waveform = wfSINE;
- SineWave->frequency = SIG_FREQ;
- SineWave->enabled = true; // Start signal generator
- i2s_adc_enable(I2S_NUM_0); // Start ADC
- }
- void loop(){
- uint16_t i2s_read_buff[NUM_SAMPLES];
- system_event_t evt;
- if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS) {
- if (evt.event_id==2) {
- i2s_read_bytes(I2S_NUM_0, (char*)i2s_read_buff, NUM_SAMPLES * 2, portMAX_DELAY);
- for (int i=0;i<NUM_SAMPLES;i++){
- Serial.printf("%X, ", i2s_read_buff[i] & 0xFFF);
- }
- Serial.println();
- Serial.println();
- Serial.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement