-
Notifications
You must be signed in to change notification settings - Fork 107
/
esp32_s3_eye_idf5.c
137 lines (118 loc) · 4.42 KB
/
esp32_s3_eye_idf5.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_err.h"
#include "bsp/esp32_s3_eye.h"
#include "bsp_err_check.h"
#include "esp_codec_dev_defaults.h"
static const char *TAG = "S3-EYE";
static i2s_chan_handle_t i2s_tx_chan = NULL;
static i2s_chan_handle_t i2s_rx_chan = NULL;
static const audio_codec_data_if_t *i2s_data_if = NULL; /* Codec data interface */
static adc_oneshot_unit_handle_t bsp_adc_handle = NULL;
/* Sample rate of MSM261S4030H0 */
#define BSP_MIC_SAMPLE_RATE (48000u)
/*
* ESP32-S3-EYE I2S pinout
* Can be used for i2s_std_gpio_config_t and/or i2s_std_config_t initialization
*/
#define BSP_I2S_GPIO_CFG() \
{ \
.mclk = GPIO_NUM_NC, \
.bclk = BSP_I2S_SCLK, \
.ws = BSP_I2S_LCLK, \
.dout = GPIO_NUM_NC, \
.din = BSP_I2S_DIN, \
.invert_flags = { \
.mclk_inv = false, \
.bclk_inv = false, \
.ws_inv = false, \
}, \
}
/* This configuration is used by default in bsp_audio_init() */
#define BSP_I2S_SIMPLEX_MONO_CFG() \
{ \
.clk_cfg = { \
.sample_rate_hz = BSP_MIC_SAMPLE_RATE, \
.clk_src = I2S_CLK_SRC_DEFAULT, \
.mclk_multiple = I2S_MCLK_MULTIPLE_384, \
}, \
.slot_cfg = { \
.data_bit_width = I2S_DATA_BIT_WIDTH_24BIT, \
.slot_bit_width = I2S_SLOT_BIT_WIDTH_32BIT, \
.slot_mode = I2S_SLOT_MODE_MONO, \
.slot_mask = I2S_STD_SLOT_LEFT, \
.ws_width = 32, \
.ws_pol = false, \
.bit_shift = true, \
.left_align = true, \
.big_endian = false, \
.bit_order_lsb = false, \
}, \
.gpio_cfg = BSP_I2S_GPIO_CFG(), \
}
esp_err_t bsp_audio_init(const i2s_std_config_t *i2s_config)
{
esp_err_t ret = ESP_FAIL;
if (i2s_tx_chan && i2s_rx_chan) {
/* Audio was initialized before */
return ESP_OK;
}
/* Setup I2S peripheral */
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(CONFIG_BSP_I2S_NUM, I2S_ROLE_MASTER);
chan_cfg.auto_clear = true; // Auto clear the legacy data in the DMA buffer
BSP_ERROR_CHECK_RETURN_ERR(i2s_new_channel(&chan_cfg, &i2s_tx_chan, &i2s_rx_chan));
/* Setup I2S channels */
const i2s_std_config_t std_cfg_default = BSP_I2S_SIMPLEX_MONO_CFG();
const i2s_std_config_t *p_i2s_cfg = &std_cfg_default;
if (i2s_config != NULL) {
p_i2s_cfg = i2s_config;
}
if (i2s_tx_chan != NULL) {
ESP_GOTO_ON_ERROR(i2s_channel_init_std_mode(i2s_tx_chan, p_i2s_cfg), err, TAG, "I2S channel initialization failed");
ESP_GOTO_ON_ERROR(i2s_channel_enable(i2s_tx_chan), err, TAG, "I2S enabling failed");
}
if (i2s_rx_chan != NULL) {
ESP_GOTO_ON_ERROR(i2s_channel_init_std_mode(i2s_rx_chan, p_i2s_cfg), err, TAG, "I2S channel initialization failed");
ESP_GOTO_ON_ERROR(i2s_channel_enable(i2s_rx_chan), err, TAG, "I2S enabling failed");
}
audio_codec_i2s_cfg_t i2s_cfg = {
.port = CONFIG_BSP_I2S_NUM,
.rx_handle = i2s_rx_chan,
.tx_handle = i2s_tx_chan,
};
i2s_data_if = audio_codec_new_i2s_data(&i2s_cfg);
BSP_NULL_CHECK_GOTO(i2s_data_if, err);
return ESP_OK;
err:
if (i2s_tx_chan) {
i2s_del_channel(i2s_tx_chan);
}
if (i2s_rx_chan) {
i2s_del_channel(i2s_rx_chan);
}
return ret;
}
const audio_codec_data_if_t *bsp_audio_get_codec_itf(void)
{
return i2s_data_if;
}
esp_err_t bsp_adc_initialize(void)
{
/* ADC was initialized before */
if (bsp_adc_handle != NULL) {
return ESP_OK;
}
/* Initialize ADC */
const adc_oneshot_unit_init_cfg_t init_config1 = {
.unit_id = BSP_ADC_UNIT,
};
BSP_ERROR_CHECK_RETURN_ERR(adc_oneshot_new_unit(&init_config1, &bsp_adc_handle));
return ESP_OK;
}
adc_oneshot_unit_handle_t bsp_adc_get_handle(void)
{
return bsp_adc_handle;
}