2021-08-18 07:45:51 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
|
|
*/
|
|
|
|
/* I2S Example
|
|
|
|
* This example code will output 100Hz sine wave and triangle wave to 2-channel of I2S driver
|
|
|
|
* Every 5 seconds, it will change bits_per_sample [16, 24, 32] for i2s data
|
|
|
|
*/
|
2016-12-29 04:29:14 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
2022-04-07 03:32:46 -04:00
|
|
|
#include "freertos/queue.h"
|
2016-12-29 04:29:14 -05:00
|
|
|
#include "freertos/task.h"
|
2022-04-07 03:32:46 -04:00
|
|
|
#include "driver/i2s_std.h"
|
2020-04-10 04:44:56 -04:00
|
|
|
#include "driver/gpio.h"
|
2017-02-06 01:11:11 -05:00
|
|
|
#include "esp_system.h"
|
2021-06-15 03:43:03 -04:00
|
|
|
#include "esp_log.h"
|
2022-04-07 03:32:46 -04:00
|
|
|
#include "esp_attr.h"
|
2016-12-29 04:29:14 -05:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
#define EXAMPLE_SAMPLE_RATE (36000)
|
|
|
|
#define EXAMPLE_DATA_BIT_WIDTH (I2S_DATA_BIT_WIDTH_16BIT)
|
2016-12-29 04:29:14 -05:00
|
|
|
|
|
|
|
#define I2S_NUM (0)
|
|
|
|
#define WAVE_FREQ_HZ (100)
|
2018-06-29 04:21:31 -04:00
|
|
|
#define PI (3.14159265)
|
2021-06-15 03:43:03 -04:00
|
|
|
#define I2S_BCK_IO (GPIO_NUM_4)
|
|
|
|
#define I2S_WS_IO (GPIO_NUM_5)
|
|
|
|
#define I2S_DO_IO (GPIO_NUM_18)
|
2022-04-07 03:32:46 -04:00
|
|
|
#define I2S_DI_IO (GPIO_NUM_18) /// Loopback internally if data_out and data_in signal are bound to a same GPIO
|
2016-12-29 04:29:14 -05:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
#define SAMPLE_PER_CYCLE (EXAMPLE_SAMPLE_RATE/WAVE_FREQ_HZ)
|
2016-12-29 04:29:14 -05:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
static i2s_chan_handle_t tx_handle = NULL;
|
|
|
|
static i2s_chan_handle_t rx_handle = NULL;
|
2021-06-15 03:43:03 -04:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
static volatile int is_overflow = 0;
|
2017-02-06 01:11:11 -05:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
static uint32_t* example_generate_triangle_sine_waves(int bits, uint32_t *buf_len)
|
|
|
|
{
|
|
|
|
uint32_t len = ((bits + 8) / 16)*SAMPLE_PER_CYCLE * 4;
|
|
|
|
uint32_t *samples_data = malloc(len);
|
2017-02-06 01:11:11 -05:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
double triangle_float = -(pow(2, bits) / 2 - 1);
|
|
|
|
double triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
|
2018-04-11 07:37:31 -04:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
for (int i = 0; i < SAMPLE_PER_CYCLE; i++) {
|
|
|
|
double sin_float = sin(i * 2 * PI / SAMPLE_PER_CYCLE);
|
|
|
|
if (sin_float >= 0) {
|
2017-02-06 01:11:11 -05:00
|
|
|
triangle_float += triangle_step;
|
2022-04-07 03:32:46 -04:00
|
|
|
} else {
|
2017-02-06 01:11:11 -05:00
|
|
|
triangle_float -= triangle_step;
|
2022-04-07 03:32:46 -04:00
|
|
|
}
|
|
|
|
sin_float *= (pow(2, bits) / 2 - 1);
|
2017-02-06 01:11:11 -05:00
|
|
|
if (bits == 16) {
|
2022-04-07 03:32:46 -04:00
|
|
|
samples_data[i] = ((short)triangle_float << 16) | (short)sin_float;
|
2017-02-06 01:11:11 -05:00
|
|
|
} else if (bits == 24) { //1-bytes unused
|
2022-04-07 03:32:46 -04:00
|
|
|
samples_data[i * 2] = ((int) triangle_float) << 8;
|
|
|
|
samples_data[i * 2 + 1] = ((int) sin_float) << 8;
|
2017-02-06 01:11:11 -05:00
|
|
|
} else {
|
2022-04-07 03:32:46 -04:00
|
|
|
samples_data[i * 2] = ((int) triangle_float);
|
|
|
|
samples_data[i * 2 + 1] = ((int) sin_float);
|
2017-02-06 01:11:11 -05:00
|
|
|
}
|
2018-04-11 07:37:31 -04:00
|
|
|
|
2017-02-06 01:11:11 -05:00
|
|
|
}
|
2022-04-07 03:32:46 -04:00
|
|
|
*buf_len = len;
|
|
|
|
return samples_data;
|
2017-02-06 01:11:11 -05:00
|
|
|
}
|
2020-03-24 13:33:05 -04:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
static IRAM_ATTR bool i2s_rx_queue_overflow_callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
|
2018-04-11 07:37:31 -04:00
|
|
|
{
|
2022-04-07 03:32:46 -04:00
|
|
|
is_overflow++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void example_i2s_read_task(void * args)
|
|
|
|
{
|
|
|
|
uint32_t *rx_buf = calloc(1, 8192);
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
uint32_t cnt = 0;
|
2016-12-29 04:29:14 -05:00
|
|
|
|
2017-02-06 01:11:11 -05:00
|
|
|
while (1) {
|
2022-04-07 03:32:46 -04:00
|
|
|
if (i2s_channel_read(rx_handle, rx_buf, 8192, &bytes_read, 1000) == ESP_OK) {
|
|
|
|
if (cnt == 0) {
|
|
|
|
printf("\n[i2s read] %d bytes are read successfully\n", bytes_read);
|
|
|
|
printf("----------------------------------------------\n");
|
|
|
|
printf("[0] %4x [1] %4x [2] %4x [3] %4x\n\n", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
cnt %= 10;
|
|
|
|
/* If the polling time is too long, there will be data dropped event */
|
|
|
|
// vTaskDelay(10);
|
|
|
|
} else {
|
|
|
|
printf("[i2s read] %d bytes are read, timeout triggered\n\n", bytes_read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void example_i2s_write_task(void * args)
|
|
|
|
{
|
|
|
|
uint32_t buf_len = 0;
|
|
|
|
uint32_t *tx_buf = example_generate_triangle_sine_waves(EXAMPLE_DATA_BIT_WIDTH, &buf_len);
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
uint32_t cnt = 0;
|
2022-04-02 09:31:35 -04:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
while (1) {
|
|
|
|
if (i2s_channel_write(tx_handle, tx_buf, buf_len, &bytes_written, 1000) == ESP_OK) {
|
|
|
|
if (cnt == 0) {
|
|
|
|
printf("[i2s write] %d bytes are written successfully\n", bytes_written);
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
cnt %= 20;
|
|
|
|
} else {
|
|
|
|
printf("[i2s write] %d bytes are written, timeout triggered\n", bytes_written);
|
|
|
|
}
|
2018-04-11 07:37:31 -04:00
|
|
|
}
|
2022-04-07 03:32:46 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void example_i2s_init_std_duplex(void)
|
|
|
|
{
|
|
|
|
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
|
|
|
|
/* Giving both tx and rx handle will make the i2s works in full-duplex mode and can share the bclk and ws signal */
|
|
|
|
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle));
|
|
|
|
i2s_std_config_t std_cfg = {
|
|
|
|
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(EXAMPLE_SAMPLE_RATE),
|
|
|
|
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(EXAMPLE_DATA_BIT_WIDTH, I2S_SLOT_MODE_STEREO),
|
|
|
|
.gpio_cfg = {
|
|
|
|
.mclk = I2S_GPIO_UNUSED,
|
|
|
|
.bclk = I2S_BCK_IO,
|
|
|
|
.ws = I2S_WS_IO,
|
|
|
|
.dout = I2S_DO_IO,
|
|
|
|
.din = I2S_DI_IO,
|
|
|
|
.invert_flags = {
|
|
|
|
.mclk_inv = false,
|
|
|
|
.bclk_inv = false,
|
|
|
|
.ws_inv = false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
#if SOC_I2S_SUPPORTS_APLL
|
|
|
|
// APLL clock is more accurate when sample rate is high
|
|
|
|
std_cfg.clk_cfg.clk_src = I2S_CLK_SRC_APLL;
|
|
|
|
#endif
|
|
|
|
/* Initialize the tx channel handle to standard mode */
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
|
|
|
|
/* Initialize the rx channel handle to standard mode */
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle, &std_cfg));
|
|
|
|
|
|
|
|
printf("I2S tx and rx channels have been initialized to standard duplex mode\n");
|
|
|
|
|
|
|
|
i2s_event_callbacks_t cbs = {
|
|
|
|
.on_recv = NULL,
|
|
|
|
.on_recv_q_ovf = i2s_rx_queue_overflow_callback,
|
|
|
|
.on_sent = NULL,
|
|
|
|
.on_send_q_ovf = NULL,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_register_event_callback(rx_handle, &cbs, NULL));
|
2022-04-02 09:31:35 -04:00
|
|
|
|
2022-04-07 03:32:46 -04:00
|
|
|
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle));
|
|
|
|
printf("I2S tx and rx channels enabled\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
//for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample)
|
|
|
|
example_i2s_init_std_duplex();
|
|
|
|
xTaskCreate(example_i2s_write_task, "i2s write task", 4096, NULL, 5, NULL);
|
|
|
|
xTaskCreate(example_i2s_read_task, "i2s read task", 8192, NULL, 5, NULL);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (is_overflow > 0) {
|
|
|
|
printf("[i2s monitor] RX message Queue overflowed\n");
|
|
|
|
is_overflow--;
|
|
|
|
}
|
|
|
|
vTaskDelay(1);
|
|
|
|
}
|
2016-12-29 04:29:14 -05:00
|
|
|
}
|