OVMS3-idf/examples/peripherals/i2s/main/i2s_example_main.c

114 lines
3.8 KiB
C
Raw Normal View History

2016-12-29 09:29:14 +00:00
/* I2S Example
2018-04-03 10:32:59 +00:00
2016-12-29 09:29:14 +00:00
This example code will output 100Hz sine wave and triangle wave to 2-channel of I2S driver
2018-04-03 10:32:59 +00:00
Every 5 seconds, it will change bits_per_sample [16, 24, 32] for i2s data
2016-12-29 09:29:14 +00:00
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s.h"
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
#include "esp_system.h"
2016-12-29 09:29:14 +00:00
#include <math.h>
#define SAMPLE_RATE (36000)
#define I2S_NUM (0)
#define WAVE_FREQ_HZ (100)
#define PI 3.14159265
#define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
static void setup_triangle_sine_waves(int bits)
2016-12-29 09:29:14 +00:00
{
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
int *samples_data = malloc(((bits+8)/16)*SAMPLE_PER_CYCLE*4);
2016-12-29 09:29:14 +00:00
unsigned int i, sample_val;
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
double sin_float, triangle_float, triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
2018-04-19 08:21:50 +00:00
size_t i2s_bytes_write = 0;
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
printf("\r\nTest bits=%d free mem=%d, written data=%d\n", bits, esp_get_free_heap_size(), ((bits+8)/16)*SAMPLE_PER_CYCLE*4);
triangle_float = -(pow(2, bits)/2 - 1);
2018-04-03 10:32:59 +00:00
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
sin_float = sin(i * PI / 180.0);
if(sin_float >= 0)
triangle_float += triangle_step;
else
triangle_float -= triangle_step;
sin_float *= (pow(2, bits)/2 - 1);
if (bits == 16) {
sample_val = 0;
sample_val += (short)triangle_float;
sample_val = sample_val << 16;
sample_val += (short) sin_float;
samples_data[i] = sample_val;
} else if (bits == 24) { //1-bytes unused
samples_data[i*2] = ((int) triangle_float) << 8;
samples_data[i*2 + 1] = ((int) sin_float) << 8;
} else {
samples_data[i*2] = ((int) triangle_float);
samples_data[i*2 + 1] = ((int) sin_float);
}
2018-04-03 10:32:59 +00:00
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
}
i2s_set_clk(I2S_NUM, SAMPLE_RATE, bits, 2);
//Using push
// for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
// if (bits == 16)
// i2s_push_sample(0, &samples_data[i], 100);
// else
// i2s_push_sample(0, &samples_data[i*2], 100);
// }
// or write
2018-04-19 08:21:50 +00:00
i2s_write(I2S_NUM, samples_data, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, &i2s_bytes_write, 100);
2018-04-03 10:32:59 +00:00
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
free(samples_data);
}
void app_main()
2018-04-03 10:32:59 +00:00
{
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
//for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample)
//depend on bits_per_sample
2016-12-29 09:29:14 +00:00
//using 6 buffers, we need 60-samples per buffer
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
//if 2-channels, 16-bit each channel, total buffer is 360*4 = 1440 bytes
//if 2-channels, 24/32-bit each channel, total buffer is 360*8 = 2880 bytes
2016-12-29 09:29:14 +00:00
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
.sample_rate = SAMPLE_RATE,
2018-04-03 10:32:59 +00:00
.bits_per_sample = 16,
2016-12-29 09:29:14 +00:00
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.dma_buf_count = 6,
.dma_buf_len = 60,
.use_apll = 0,
2016-12-29 09:29:14 +00:00
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
};
i2s_pin_config_t pin_config = {
.bck_io_num = 26,
.ws_io_num = 25,
.data_out_num = 22,
.data_in_num = -1 //Not used
};
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
I2S: fixed support for 24 and 32-bits, fixed tx/rx at the same time and others in log: Add support 24, 32 bits @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 Add support 24, 32 bits reduce push function Modify sample can test with 24, 32-bits Add i2s sample README Fixed i2s_set_clk got reset sometime can changes bit-wide while running Fix sample rate calculation for DAC mode from @jeroen modify example can change bits per sample every 5 seconds cleanup add comment for bytes_per_sample update as @angus suggestions Add i2s_set_clk api I2S driver bug fixed as following log: @jxmao optimize i2s_zero_buffer @jxmao correct config support slave mode @jxmao correct setup pin support tx/rx at the same time Enhance pin power @jxmao add dynamic change number of channels remove nvs_init remove start log and enhance power pin setup using enum for i2s_channel and i2s_bits_per_sample, return ESP_ERR_INVALID_ARG when check params validate i2s_bits_per_sample and channel check tx & rx buffer before fill zero remove checking channel as @jxmao suggest limit dma buffer modify i2s pin back to gpio mux before configure gpio matrix, resolve github issue #512 limit dma buffer to 4092 change gpio_matrix_*_check to inline function, remove some comments and rename test_i2s function to setup_triangle_sine_waves for change commit title
2017-02-06 06:11:11 +00:00
int test_bits = 16;
while (1) {
setup_triangle_sine_waves(test_bits);
vTaskDelay(5000/portTICK_RATE_MS);
test_bits += 8;
if(test_bits > 32)
test_bits = 16;
2018-04-03 10:32:59 +00:00
}
2016-12-29 09:29:14 +00:00
}