Advertisement
Guest User

I2S ADC DMA Test Example

a guest
Apr 26th, 2018
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/event_groups.h"
  4. #include "esp_log.h"
  5. #include "driver/i2s.h"
  6. #include "soc/syscon_reg.h"
  7. #include "driver/adc.h"
  8. #include "esp_wifi.h"
  9. #include "esp_event_loop.h"
  10. #include "nvs_flash.h"
  11. #include "sr_gen_sine.h"
  12.  
  13. #define TAG "adc_i2s"
  14.  
  15. uint32_t    SAMPLE_RATE = 120E3;
  16. uint32_t    NUM_SAMPLES = 512;
  17. uint32_t    SIG_FREQ = 10E3;
  18.  
  19. static QueueHandle_t i2s_event_queue;
  20. static EventGroupHandle_t wifi_event_group;
  21. SigGen *SineWave          = nullptr;  
  22.  
  23. void setup(){
  24.  Serial.begin(921600);
  25.     i2s_config_t i2s_config ;
  26.     i2s_config.mode =  i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
  27.     i2s_config.sample_rate = SAMPLE_RATE;                           // 120 KHz
  28.     i2s_config.dma_buf_len = NUM_SAMPLES;                           // 512
  29.     i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;         // Should be mono but doesn't seem to be
  30.     i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
  31.     i2s_config.use_apll = false,
  32.     i2s_config.communication_format = I2S_COMM_FORMAT_I2S;
  33.     i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
  34.     i2s_config.dma_buf_count = 2;
  35.     //install and start i2s driver
  36.     i2s_driver_install(I2S_NUM_0, &i2s_config, 1, &i2s_event_queue);
  37.     // Connect ADC to I2S
  38.     i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_6);
  39.     //i2s_set_clk(I2S_NUM_0,SAMPLE_RATE,I2S_BITS_PER_SAMPLE_16BIT,I2S_CHANNEL_MONO);    // This doesn't work either
  40.    
  41.     // Generate test signal
  42.     SineWave                    = new SigGen();
  43.     SineWave->channel           = DAC_CHANNEL_1;
  44.     SineWave->waveform          = wfSINE;          
  45.     SineWave->frequency         = SIG_FREQ;
  46.     SineWave->enabled           = true;                           // Start signal generator
  47.  
  48.     i2s_adc_enable(I2S_NUM_0);                                      // Start ADC
  49. }
  50.  
  51. void loop(){
  52.     uint16_t i2s_read_buff[NUM_SAMPLES];
  53.         system_event_t evt;
  54.         if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS) {
  55.             if (evt.event_id==2) {
  56.                 i2s_read_bytes(I2S_NUM_0, (char*)i2s_read_buff,  NUM_SAMPLES * 2, portMAX_DELAY);
  57.                 for (int i=0;i<NUM_SAMPLES;i++){
  58.                 Serial.printf("%X, ", i2s_read_buff[i] & 0xFFF);
  59.                 }
  60.                 Serial.println();
  61.                 Serial.println();
  62.                 Serial.println();
  63.             }
  64.         }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement