I was using the adc with i2s dma. My code looks like this:
I2S config:
Code: Select all
void configure_i2s(){
//pinMode(36,INPUT);
i2s_config_t i2s_config =
{
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN), // I2S receive mode with ADC
.sample_rate = 144444, // sample rate
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // 16 bit I2S
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, //I2S_CHANNEL_FMT_ALL_LEFT, // only the left channel
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), // I2S format
.intr_alloc_flags = 0, // none
.dma_buf_count = 2, // number of DMA buffers
.dma_buf_len = 1024, // number of samples
.use_apll = 0, // no Audio PLL
};
adc1_config_channel_atten(ADC_CHANNEL, ADC_ATTEN_11db);
adc1_config_width(ADC_WIDTH_12Bit);
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_adc_mode(ADC_UNIT_1, ADC_CHANNEL);
SET_PERI_REG_MASK(SYSCON_SARADC_CTRL2_REG, SYSCON_SARADC_SAR1_INV);
i2s_adc_enable(I2S_NUM_0);
vTaskDelay(3000/portTICK_RATE_MS);
}
Read from DMA BUFFER and plot the results to web:
Code: Select all
static const inline void Plot_DMA_Buffer(){
int Vmax = 0;
int Vmin = 4095;
uint16_t Buffer[NUM_SAMPLES];
for(int i = 0;i<NUM_SAMPLES;i++){
Buffer[i] = i2s_read_buff[i];
if(i != NUM_SAMPLES)
{
if((i2s_read_buff[i]<i2s_read_buff[i+1])&&i2s_read_buff[i]<Vmin){
if(constrain(i2s_read_buff[i],Vmin-300,Vmin+300)){
Vmin = i2s_read_buff[i];
}
}else if((i2s_read_buff[i]>i2s_read_buff[i+1])&&i2s_read_buff[i]>Vmax){
if(constrain(i2s_read_buff[i],Vmax-300,Vmax+300)){
Vmax = i2s_read_buff[i];
}
}
}
}
int Vamp = (Vmax) - (Vmin);
if(millis() - Start_Millis >= 1000){
Start_Millis = millis();
Send_Amplitude(Vamp);
if(olvasunk_e){
for(int k = 0;k<NUM_SAMPLES;k++){
if(!olvasunk_e){
break;
}else{
String data = (String)Buffer[k]+"}";
webSocket.broadcastTXT(data.c_str(), data.length());
}
}
}
}
}
Code: Select all
static const inline void Base_Setups_Core1(){
Serial.begin(115200);
SPIFFS.begin() ? Serial.println("SPIFFS.OK") : Serial.println("SPIFFS.FAIL");
configure_i2s();
/** Ethernet wifi stack **/
WiFi.onEvent(WiFiEvent);
ETH_begin();
ethernet.config(ip1, nm1, gw1);
TCP_Requests();
server.begin();
ws.onEvent(onWsEvent);
server.addHandler(&ws);
External_Interrupts();
xTaskCreatePinnedToCore ( loop0, "v_getIMU0", 1024, NULL, 0, &TaskHandle_1, 1 );
xTaskCreatePinnedToCore ( loop1, "v_getIMU1", 15048, NULL, 0, &TaskHandle_2, 0 );
}
static const inline void Main_Loop(){
if(!Ota_is_Started){
Plot_DMA_Buffer(); // RETRIVE DATA FROM i2s_read_buff FOR CALCULATION AND VISUALIZATION / COMPUTE THE AVERAGE AMPLITUDE
}
}
static void loop0(void * pvParameters){
for( ;; ){
i2s_read(I2S_NUM_0, (char*)i2s_read_buff,NUM_SAMPLES * sizeof(uint16_t), &bytes_read, portMAX_DELAY);
}
}
The problem with this is that i can not do anything on that core. And the results are noisy.
I don't know how the task semaphores or the task events are working.