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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*
* Copyright (c) 2022 HPMicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "pdm"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef BSP_USING_PDM
#include "board.h"
#include "drivers/dev_audio.h"
#include "hpm_i2s_drv.h"
#include "hpm_pdm_drv.h"
#include "drv_pdm.h"
#ifdef HPMSOC_HAS_HPMSDK_DMAV2
#include "hpm_dmav2_drv.h"
#else
#include "hpm_dma_drv.h"
#endif
#include "hpm_dmamux_drv.h"
#include "hpm_l1c_drv.h"
#include "hpm_dma_mgr.h"
/* PDM connect to I2S0 RX */
#define PDM_DMA_REQ HPM_DMA_SRC_I2S0_RX
#define PDM_I2S_DATA_LINE 0
struct hpm_pdm
{
struct rt_audio_device audio;
struct rt_audio_configure record_config;
rt_uint8_t* rx_fifo;
};
struct hpm_pdm hpm_pdm_dev = { 0 };
static dma_resource_t dma_resource = { 0 };
static rt_err_t hpm_pdm_dma_transmit();
void pdm_dma_tc_callback(DMA_Type *ptr, uint32_t channel, void *user_data)
{
rt_audio_rx_done(&hpm_pdm_dev.audio, hpm_pdm_dev.rx_fifo, PDM_FIFO_SIZE);
hpm_pdm_dma_transmit();
}
static rt_err_t hpm_pdm_getcaps(struct rt_audio_device* audio, struct rt_audio_caps* caps)
{
rt_err_t result = RT_EOK;
RT_ASSERT(audio != RT_NULL);
struct hpm_pdm* hpm_audio = (struct hpm_pdm*)audio->parent.user_data;
switch(caps->main_type)
{
case AUDIO_TYPE_INPUT:
{
switch(caps->sub_type)
{
case AUDIO_DSP_PARAM:
caps->udata.config.samplerate = hpm_audio->record_config.samplerate;
caps->udata.config.channels = hpm_audio->record_config.channels;
caps->udata.config.samplebits = hpm_audio->record_config.samplebits;
break;
case AUDIO_DSP_SAMPLERATE:
caps->udata.config.samplerate = hpm_audio->record_config.samplerate;
break;
case AUDIO_DSP_CHANNELS:
caps->udata.config.channels = hpm_audio->record_config.channels;
break;
case AUDIO_DSP_SAMPLEBITS:
caps->udata.config.samplebits = hpm_audio->record_config.samplebits;
break;
default:
result = -RT_ERROR;
break;
}
break;
}
default:
result = -RT_ERROR;
break;
}
return result;
}
static bool i2s_is_enabled(I2S_Type *ptr)
{
return ((ptr->CTRL & I2S_CTRL_I2S_EN_MASK) != 0);
}
static rt_err_t hpm_pdm_set_channels(uint32_t channel)
{
uint32_t mclk_hz;
i2s_transfer_config_t transfer;
mclk_hz = clock_get_frequency(clock_i2s0);
i2s_get_default_transfer_config_for_pdm(&transfer);
transfer.data_line = PDM_I2S_DATA_LINE;
if (channel == 1) {
transfer.channel_slot_mask = BOARD_PDM_SINGLE_CHANNEL_MASK;
} else if(channel == 2) {
transfer.channel_slot_mask = BOARD_PDM_DUAL_CHANNEL_MASK;
} else {
LOG_E("PDM not support channels number %d.\n", channel);
return -RT_ERROR;
}
bool is_enabled = i2s_is_enabled(PDM_I2S);
if (is_enabled) {
dma_mgr_abort_chn_transfer(&dma_resource);
pdm_stop(HPM_PDM);
pdm_software_reset(HPM_PDM);
i2s_disable(PDM_I2S);
i2s_disable_rx_dma_request(PDM_I2S);
if (status_success != i2s_reset_rx(PDM_I2S)) {
LOG_E("pdm_i2s reset rx failed\n");
return -RT_ERROR;
}
}
if (status_success != i2s_config_rx(PDM_I2S, mclk_hz, &transfer))
{
LOG_E("pdm_i2s configure transfer failed\n");
return -RT_ERROR;
}
if (is_enabled)
{
hpm_pdm_dma_transmit();
pdm_start(HPM_PDM);
i2s_enable_rx_dma_request(PDM_I2S);
i2s_start(PDM_I2S);
}
return RT_EOK;
}
static rt_err_t hpm_pdm_configure(struct rt_audio_device* audio, struct rt_audio_caps* caps)
{
rt_err_t result = RT_EOK;
RT_ASSERT(audio != RT_NULL);
struct hpm_pdm* hpm_audio = (struct hpm_pdm*)audio->parent.user_data;
switch(caps->main_type)
{
case AUDIO_TYPE_INPUT:
{
switch(caps->sub_type)
{
case AUDIO_DSP_CHANNELS:
{
hpm_audio->record_config.channels = caps->udata.config.channels;
hpm_pdm_set_channels(caps->udata.config.channels);
break;
}
default:
result = -RT_ERROR;
break;
}
break;
}
default:
result = -RT_ERROR;
break;
}
return result;
}
static rt_err_t hpm_pdm_init(struct rt_audio_device* audio)
{
RT_ASSERT(audio != RT_NULL);
i2s_config_t i2s_config;
i2s_transfer_config_t transfer;
pdm_config_t pdm_config;
init_pdm_pins();
board_init_pdm_clock();
i2s_get_default_config(PDM_I2S, &i2s_config);
i2s_enable_rx_dma_request(PDM_I2S);
if (status_success != i2s_init(PDM_I2S, &i2s_config)) {
LOG_E("pdm_i2s init failed\n");
return -RT_ERROR;
}
i2s_get_default_transfer_config_for_pdm(&transfer);
transfer.data_line = PDM_I2S_DATA_LINE;
transfer.channel_slot_mask = BOARD_PDM_SINGLE_CHANNEL_MASK;
if (status_success != i2s_config_rx(PDM_I2S, clock_get_frequency(clock_i2s0), &transfer))
{
LOG_E("pdm_i2s configure receive failed\n");
return -RT_ERROR;
}
/* init audio configure */
hpm_pdm_dev.record_config.channels = 1U;
hpm_pdm_dev.record_config.samplebits = 32U; /* the actual significant bit is 24 bits, and the low bit is 0 */
hpm_pdm_dev.record_config.samplerate = PDM_SOC_SAMPLE_RATE_IN_HZ; /* fix 16KHz */
pdm_get_default_config(HPM_PDM, &pdm_config);
if (status_success != pdm_init(HPM_PDM, &pdm_config)) {
LOG_E("pdm init failed\n");
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t hpm_pdm_start(struct rt_audio_device* audio, int stream)
{
RT_ASSERT(audio != RT_NULL);
struct hpm_pdm* hpm_audio = (struct hpm_pdm*)audio->parent.user_data;
if (stream == AUDIO_STREAM_RECORD)
{
i2s_disable(PDM_I2S);
i2s_disable_rx_dma_request(PDM_I2S);
pdm_stop(HPM_PDM);
pdm_software_reset(HPM_PDM);
if (dma_mgr_request_resource(&dma_resource) == status_success) {
uint8_t dmamux_ch;
dma_mgr_install_chn_tc_callback(&dma_resource, pdm_dma_tc_callback, NULL);
dma_mgr_enable_dma_irq_with_priority(&dma_resource, 1);
dmamux_ch = DMA_SOC_CHN_TO_DMAMUX_CHN(dma_resource.base, dma_resource.channel);
dmamux_config(HPM_DMAMUX, dmamux_ch, PDM_DMA_REQ, true);
} else {
LOG_E("no dma resource available for PDM transfer.\n");
return -RT_ERROR;
}
if (status_success != i2s_reset_rx(PDM_I2S)) {
LOG_E("pdm_i2s reset rx failed\n");
return -RT_ERROR;
}
i2s_enable_rx_line(PDM_I2S, PDM_I2S_DATA_LINE);
if (RT_EOK != hpm_pdm_dma_transmit()) {
return -RT_ERROR;
}
pdm_start(HPM_PDM);
i2s_enable_rx_dma_request(PDM_I2S);
i2s_start(PDM_I2S);
}
return RT_EOK;
}
static rt_err_t hpm_pdm_stop(struct rt_audio_device* audio, int stream)
{
RT_ASSERT(audio != RT_NULL);
if (stream == AUDIO_STREAM_RECORD) {
pdm_stop(HPM_PDM);
i2s_stop(PDM_I2S);
dma_mgr_abort_chn_transfer(&dma_resource);
dma_mgr_release_resource(&dma_resource);
}
return RT_EOK;
}
static rt_err_t hpm_pdm_dma_transmit(void)
{
dma_channel_config_t ch_config = {0};
dma_default_channel_config(dma_resource.base, &ch_config);
ch_config.src_addr = (uint32_t)&PDM_I2S->RXD[PDM_I2S_DATA_LINE];
ch_config.dst_addr = core_local_mem_to_sys_address(HPM_CORE0, (uint32_t)hpm_pdm_dev.rx_fifo);
ch_config.src_width = DMA_TRANSFER_WIDTH_WORD;
ch_config.dst_width = DMA_TRANSFER_WIDTH_WORD;
ch_config.src_addr_ctrl = DMA_ADDRESS_CONTROL_FIXED;
ch_config.dst_addr_ctrl = DMA_ADDRESS_CONTROL_INCREMENT;
ch_config.size_in_byte = PDM_FIFO_SIZE;
ch_config.src_mode = DMA_HANDSHAKE_MODE_HANDSHAKE;
ch_config.src_burst_size = DMA_NUM_TRANSFER_PER_BURST_1T;
ch_config.interrupt_mask = DMA_INTERRUPT_MASK_ABORT;
if (status_success != dma_setup_channel(dma_resource.base, dma_resource.channel, &ch_config, true)) {
LOG_E("dma setup channel failed\n");
return -RT_ERROR;
}
if (l1c_dc_is_enabled()) {
/* cache invalidate for receive buff */
l1c_dc_invalidate((uint32_t)hpm_pdm_dev.rx_fifo, PDM_FIFO_SIZE);
}
return RT_EOK;
}
static struct rt_audio_ops hpm_pdm_ops =
{
.getcaps = hpm_pdm_getcaps,
.configure = hpm_pdm_configure,
.init = hpm_pdm_init,
.start = hpm_pdm_start,
.stop = hpm_pdm_stop,
.transmit = NULL,
.buffer_info = NULL,
};
ATTR_ALIGN(HPM_L1C_CACHELINE_SIZE) uint8_t pdm_rx_fifo[PDM_FIFO_SIZE];
int rt_hw_pdm_init(void)
{
hpm_pdm_dev.rx_fifo = pdm_rx_fifo;
hpm_pdm_dev.audio.ops = &hpm_pdm_ops;
LOG_I("audio pdm registered.\n");
LOG_I("!!!Note: pdm depends on i2s0, they share clock.\n");
rt_audio_register(&hpm_pdm_dev.audio, "pdm", RT_DEVICE_FLAG_RDONLY, &hpm_pdm_dev);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_pdm_init);
#endif /* BSP_USING_PDM */
|