|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/audio/dmic.h> |
| 10 | + |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | +LOG_MODULE_REGISTER(dmic_sample); |
| 13 | + |
| 14 | +#define SAMPLE_RATE 16000 |
| 15 | +#define SAMPLE_BIT_WIDTH 16 |
| 16 | +#define BYTES_PER_SAMPLE (SAMPLE_BIT_WIDTH / 8) |
| 17 | +#define NO_OF_CHANNELS 1 |
| 18 | + |
| 19 | +/* Milliseconds to wait for a block to be captured by PCM peripheral. */ |
| 20 | +#define READ_TIMEOUT 1200 |
| 21 | + |
| 22 | +/* Driver will allocate blocks from this slab to receive audio data into them. |
| 23 | + * Application, after getting a given block from the driver and processing its |
| 24 | + * data, needs to free that block. |
| 25 | + */ |
| 26 | +#define AUDIO_BLOCK_SIZE (BYTES_PER_SAMPLE * SAMPLE_RATE * NO_OF_CHANNELS / 10) |
| 27 | +/* Driver allocates memory "in advance" therefore 2 blocks may be not enough. */ |
| 28 | +#define BLOCK_COUNT 4 |
| 29 | +K_MEM_SLAB_DEFINE_STATIC(mem_slab, AUDIO_BLOCK_SIZE, BLOCK_COUNT, 4); |
| 30 | + |
| 31 | +int main(void) |
| 32 | +{ |
| 33 | + const struct device *const dmic_dev = DEVICE_DT_GET(DT_NODELABEL(dmic_dev)); |
| 34 | + int ret; |
| 35 | + int loop_counter = 1; |
| 36 | + void *buffer; |
| 37 | + uint32_t size; |
| 38 | + |
| 39 | + if (!device_is_ready(dmic_dev)) { |
| 40 | + LOG_ERR("%s is not ready", dmic_dev->name); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + struct pcm_stream_cfg stream = { |
| 45 | + .pcm_rate = SAMPLE_RATE, |
| 46 | + .pcm_width = SAMPLE_BIT_WIDTH, |
| 47 | + .block_size = AUDIO_BLOCK_SIZE, |
| 48 | + .mem_slab = &mem_slab, |
| 49 | + }; |
| 50 | + |
| 51 | + struct dmic_cfg cfg = { |
| 52 | + .io = { |
| 53 | + /* These fields can be used to limit the PDM clock |
| 54 | + * configurations that the driver is allowed to use |
| 55 | + * to those supported by the microphone. |
| 56 | + */ |
| 57 | + .min_pdm_clk_freq = 1000000, |
| 58 | + .max_pdm_clk_freq = 3250000, |
| 59 | + .min_pdm_clk_dc = 40, |
| 60 | + .max_pdm_clk_dc = 60, |
| 61 | + }, |
| 62 | + .streams = &stream, |
| 63 | + .channel = { |
| 64 | + .req_num_chan = 1, |
| 65 | + .req_num_streams = 1, |
| 66 | + .req_chan_map_lo = dmic_build_channel_map(0, 0, PDM_CHAN_LEFT), |
| 67 | + }, |
| 68 | + }; |
| 69 | + |
| 70 | + ret = dmic_configure(dmic_dev, &cfg); |
| 71 | + if (ret < 0) { |
| 72 | + LOG_ERR("Failed to configure the driver: %d", ret); |
| 73 | + return ret; |
| 74 | + } |
| 75 | + |
| 76 | + ret = dmic_trigger(dmic_dev, DMIC_TRIGGER_START); |
| 77 | + if (ret < 0) { |
| 78 | + LOG_ERR("START trigger failed: %d", ret); |
| 79 | + return ret; |
| 80 | + } |
| 81 | + |
| 82 | + while (1) { |
| 83 | + ret = dmic_read(dmic_dev, 0, &buffer, &size, READ_TIMEOUT); |
| 84 | + if (ret < 0) { |
| 85 | + LOG_ERR("%d - read failed: %d", loop_counter, ret); |
| 86 | + return ret; |
| 87 | + } |
| 88 | + |
| 89 | + /* Print buffer on serial port (in binary mode). */ |
| 90 | + unsigned char pcm_l, pcm_h; |
| 91 | + int j; |
| 92 | + |
| 93 | + uint16_t *pcm_out = buffer; |
| 94 | + |
| 95 | + for (j = 0; j < size/2; j++) { |
| 96 | + pcm_l = (char)(pcm_out[j] & 0xFF); |
| 97 | + pcm_h = (char)((pcm_out[j] >> 8) & 0xFF); |
| 98 | + |
| 99 | + z_impl_k_str_out(&pcm_l, 1); |
| 100 | + z_impl_k_str_out(&pcm_h, 1); |
| 101 | + } |
| 102 | + |
| 103 | + k_mem_slab_free(&mem_slab, buffer); |
| 104 | + |
| 105 | + loop_counter++; |
| 106 | + } |
| 107 | + |
| 108 | + /* Dead code; left for reference. */ |
| 109 | + ret = dmic_trigger(dmic_dev, DMIC_TRIGGER_STOP); |
| 110 | + if (ret < 0) { |
| 111 | + LOG_ERR("STOP trigger failed: %d", ret); |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
0 commit comments