This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
/
drv_mic.c
245 lines (190 loc) · 6.83 KB
/
drv_mic.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form, except as embedded into a Nordic
Semiconductor ASA integrated circuit in a product or a software update for
such product, must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of Nordic Semiconductor ASA nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
4. This software, with or without modification, must only be used with a
Nordic Semiconductor ASA integrated circuit.
5. Any software provided in binary form under this license must not be reverse
engineered, decompiled, modified and/or disassembled.
THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "drv_audio.h"
#include "drv_audio_anr.h"
#include "drv_audio_dsp.h"
#include "drv_audio_coder.h"
#include "app_scheduler.h"
#include "app_error.h"
#include "drv_ext_gpio.h"
#include "drv_mic.h"
#include "app_util_platform.h"
#define NRF_LOG_MODULE_NAME "drv_mic "
#include "nrf_log.h"
#include "macros_common.h"
STATIC_ASSERT(CONFIG_PDM_BUFFER_SIZE_SAMPLES == (1 * CONFIG_AUDIO_FRAME_SIZE_SAMPLES));
typedef struct
{
int16_t buf[CONFIG_PDM_BUFFER_SIZE_SAMPLES];
uint16_t samples;
bool free;
}pdm_buf_t;
#define PDM_BUF_NUM 6
static bool m_audio_enabled; ///< Audio enabled flag.
static drv_mic_data_handler_t m_data_handler;
static pdm_buf_t m_pdm_buf[PDM_BUF_NUM];
static void mic_power_on(void)
{
uint32_t err_code;
nrf_gpio_cfg_input(MIC_DOUT, NRF_GPIO_PIN_NOPULL);
#if defined(THINGY_HW_v0_7_0)
err_code = drv_ext_gpio_pin_clear(SX_MIC_PWR_CTRL);
#elif defined(THINGY_HW_v0_8_0)
err_code = drv_ext_gpio_pin_clear(SX_MIC_PWR_CTRL);
#elif defined(THINGY_HW_v0_9_0)
err_code = drv_ext_gpio_pin_clear(SX_MIC_PWR_CTRL);
#else
err_code = drv_ext_gpio_pin_set(SX_MIC_PWR_CTRL);
#endif
APP_ERROR_CHECK(err_code);
}
static void mic_power_off(void)
{
uint32_t err_code;
#if defined(THINGY_HW_v0_7_0)
err_code = drv_ext_gpio_pin_set(SX_MIC_PWR_CTRL);
#elif defined(THINGY_HW_v0_8_0)
err_code = drv_ext_gpio_pin_set(SX_MIC_PWR_CTRL);
#elif defined(THINGY_HW_v0_9_0)
err_code = drv_ext_gpio_pin_set(SX_MIC_PWR_CTRL);
#else
err_code = drv_ext_gpio_pin_clear(SX_MIC_PWR_CTRL);
#endif
nrf_gpio_cfg_input(MIC_DOUT, NRF_GPIO_PIN_PULLDOWN);
APP_ERROR_CHECK(err_code);
}
static void m_audio_process(void * p_event_data, uint16_t event_size)
{
int16_t * p_buffer;
ret_code_t status;
m_audio_frame_t frame_buf;
pdm_buf_t * p_pdm_buf = (pdm_buf_t *)(*(uint32_t *)p_event_data);
APP_ERROR_CHECK_BOOL(p_event_data != NULL);
APP_ERROR_CHECK_BOOL(event_size > 0);
p_buffer = p_pdm_buf->buf;
#if CONFIG_AUDIO_EQUALIZER_ENABLED
drv_audio_dsp_equalizer((q15_t *)p_buffer, CONFIG_AUDIO_FRAME_SIZE_SAMPLES);
#endif /* CONFIG_AUDIO_EQUALIZER_ENABLED */
#if CONFIG_AUDIO_GAIN_CONTROL_ENABLED
drv_audio_dsp_gain_control((q15_t *)p_buffer, CONFIG_AUDIO_FRAME_SIZE_SAMPLES);
#endif /* CONFIG_AUDIO_GAIN_CONTROL_ENABLED */
uint8_t nested;
app_util_critical_region_enter(&nested);
drv_audio_coder_encode(p_buffer, &frame_buf);
p_pdm_buf->free = true;
app_util_critical_region_exit(nested);
// Schedule audio transmission. It cannot be done from this context.
status = m_data_handler(&frame_buf);
if (status != NRF_SUCCESS)
{
NRF_LOG_WARNING("Cannot schedule audio frame transmission!\r\n");
/*
* Do not clear CONFIG_IO_DBG_PCM. This will make debugging pulse wider
* than expected and easier to spot on the logic analyzer.
*/
}
}
static void m_audio_buffer_handler(int16_t *p_buffer, uint16_t samples)
{
uint32_t err_code;
pdm_buf_t * p_pdm_buf = NULL;
uint32_t pdm_buf_addr;
for(uint32_t i = 0; i < PDM_BUF_NUM; i++)
{
if ( m_pdm_buf[i].free == true )
{
m_pdm_buf[i].free = false;
m_pdm_buf[i].samples = samples;
for (uint32_t j = 0; j < samples; j++)
{
m_pdm_buf[i].buf[j] = p_buffer[j];
}
p_pdm_buf = &m_pdm_buf[i];
pdm_buf_addr = (uint32_t)&m_pdm_buf[i];
break;
}
}
if (p_pdm_buf != NULL)
{
err_code = app_sched_event_put(&pdm_buf_addr, sizeof(pdm_buf_t *), m_audio_process);
APP_ERROR_CHECK(err_code);
}
else
{
NRF_LOG_WARNING("m_audio_buffer_handler: BUFFER FULL!!\r\n");
}
}
uint32_t drv_mic_start(void)
{
ret_code_t status;
NRF_LOG_DEBUG("m_audio: Enabled\r\n");
if(m_audio_enabled == true)
{
return NRF_SUCCESS;
}
mic_power_on();
status = drv_audio_enable();
if (status == NRF_SUCCESS)
{
m_audio_enabled = true;
}
return status;
}
uint32_t drv_mic_stop(void)
{
ret_code_t status;
NRF_LOG_DEBUG("m_audio: Disabled\r\n");
if(m_audio_enabled == false)
{
return NRF_SUCCESS;
}
status = drv_audio_disable();
if (status == NRF_SUCCESS)
{
m_audio_enabled = false;
}
mic_power_off();
return status;
}
uint32_t drv_mic_init(drv_mic_data_handler_t data_handler)
{
uint32_t err_code;
m_audio_enabled = false;
m_data_handler = data_handler;
for(uint32_t i = 0; i < PDM_BUF_NUM; i++)
{
m_pdm_buf[i].free = true;
}
err_code = drv_ext_gpio_cfg_output(SX_MIC_PWR_CTRL);
RETURN_IF_ERROR(err_code);
mic_power_off();
return drv_audio_init(m_audio_buffer_handler);
}