-
Notifications
You must be signed in to change notification settings - Fork 105
/
esp32_lyrat_idf4.c
81 lines (69 loc) · 2.7 KB
/
esp32_lyrat_idf4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_err.h"
#include "bsp/esp32_lyrat.h"
#include "bsp_err_check.h"
#include "esp_codec_dev_defaults.h"
/* This configuration is used by default in bsp_audio_init() */
#define BSP_I2S_DUPLEX_MONO_CFG(_sample_rate) \
{ \
.mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX, \
.sample_rate = _sample_rate, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, \
.communication_format = I2S_COMM_FORMAT_STAND_I2S, \
.dma_buf_count = 3, \
.dma_buf_len = 1024, \
.use_apll = true, \
.tx_desc_auto_clear = true, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM \
}
static const audio_codec_data_if_t *i2s_data_if = NULL; /* Codec data interface */
esp_err_t bsp_audio_init(const i2s_config_t *i2s_config)
{
if (i2s_data_if != NULL) {
/* Audio was initialized before */
return ESP_OK;
}
/* Setup I2S peripheral */
const i2s_pin_config_t i2s_pin_config = {
.mck_io_num = BSP_I2S_MCLK,
.bck_io_num = BSP_I2S_SCLK,
.ws_io_num = BSP_I2S_LCLK,
.data_out_num = BSP_I2S_DOUT,
.data_in_num = BSP_I2S_DSIN
};
/* Setup I2S channels */
const i2s_config_t std_cfg_default = BSP_I2S_DUPLEX_MONO_CFG(22050);
const i2s_config_t *p_i2s_cfg = &std_cfg_default;
if (i2s_config != NULL) {
p_i2s_cfg = i2s_config;
}
ESP_ERROR_CHECK(i2s_driver_install(CONFIG_BSP_I2S_NUM, p_i2s_cfg, 0, NULL));
ESP_ERROR_CHECK(i2s_set_pin(CONFIG_BSP_I2S_NUM, &i2s_pin_config));
audio_codec_i2s_cfg_t i2s_cfg = {
.port = CONFIG_BSP_I2S_NUM,
};
i2s_data_if = audio_codec_new_i2s_data(&i2s_cfg);
BSP_NULL_CHECK(i2s_data_if, NULL);
return ESP_OK;
}
void bsp_audio_deinit(void)
{
ESP_ERROR_CHECK_WITHOUT_ABORT(i2s_driver_uninstall(CONFIG_BSP_I2S_NUM));
const gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = BIT64(BSP_POWER_AMP_IO),
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLDOWN_DISABLE,
};
ESP_ERROR_CHECK_WITHOUT_ABORT(gpio_config(&io_conf));
}
const audio_codec_data_if_t *bsp_audio_get_codec_itf(void)
{
return i2s_data_if;
}