Linker Errors Encountered while Building dmic

I'm encountering linker errors while attempting to build dmic (PDM mic for data logging) using the Nordic SDK.  However, during compilation, I'm facing linker errors related to standard C library functions such as closeread, write, lseek, and open.

Below are the details of my setup and the errors encountered:

This code will write the received audio data to a file named "audio.pcm" in the current directory in PCM format.

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/audio/dmic.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(dmic_sample);

#define MAX_SAMPLE_RATE 16000
#define SAMPLE_BIT_WIDTH 16
#define BYTES_PER_SAMPLE sizeof(int16_t)
/* Milliseconds to wait for a block to be read. */
#define READ_TIMEOUT 1000

/* Size of a block for 100 ms of audio data. */
#define BLOCK_SIZE(_sample_rate, _number_of_channels) \
(BYTES_PER_SAMPLE * (_sample_rate / 10) * _number_of_channels)

/* Driver will allocate blocks from this slab to receive audio data into them.
* Application, after getting a given block from the driver and processing its
* data, needs to free that block.
*/
#define MAX_BLOCK_SIZE BLOCK_SIZE(MAX_SAMPLE_RATE, 2)
#define BLOCK_COUNT 4
K_MEM_SLAB_DEFINE_STATIC(mem_slab, MAX_BLOCK_SIZE, BLOCK_COUNT, 4);

static int do_pdm_transfer(const struct device *dmic_dev,
struct dmic_cfg *cfg,
size_t block_count)
{
int ret;

LOG_INF("PCM output rate: %u, channels: %u",
cfg->streams[0].pcm_rate, cfg->channel.req_num_chan);

ret = dmic_configure(dmic_dev, cfg);
if (ret < 0) {
LOG_ERR("Failed to configure the driver: %d", ret);
return ret;
}

ret = dmic_trigger(dmic_dev, DMIC_TRIGGER_START);
if (ret < 0) {
LOG_ERR("START trigger failed: %d", ret);
return ret;
}

FILE *pcm_file = fopen("audio.pcm", "wb");
if (!pcm_file) {
LOG_ERR("Failed to open PCM file for writing");
return -1;
}

for (int i = 0; i < block_count; ++i) {
void *buffer;
uint32_t size;

ret = dmic_read(dmic_dev, 0, &buffer, &size, READ_TIMEOUT);
if (ret < 0) {
LOG_ERR("%d - read failed: %d", i, ret);
fclose(pcm_file);
return ret;
}

// Write audio data to PCM file
fwrite(buffer, 1, size, pcm_file);

k_mem_slab_free(&mem_slab, buffer);
}

fclose(pcm_file);

ret = dmic_trigger(dmic_dev, DMIC_TRIGGER_STOP);
if (ret < 0) {
LOG_ERR("STOP trigger failed: %d", ret);
return ret;
}

return ret;
}

int main(void)
{
const struct device *const dmic_dev = DEVICE_DT_GET(DT_NODELABEL(dmic_dev));
int ret;

LOG_INF("DMIC sample");

if (!device_is_ready(dmic_dev)) {
LOG_ERR("%s is not ready", dmic_dev->name);
return 0;
}

struct pcm_stream_cfg stream = {
.pcm_width = SAMPLE_BIT_WIDTH,
.mem_slab = &mem_slab,
};
struct dmic_cfg cfg = {
.io = {
.min_pdm_clk_freq = 1000000,
.max_pdm_clk_freq = 3500000,
.min_pdm_clk_dc = 40,
.max_pdm_clk_dc = 60,
},
.streams = &stream,
.channel = {
.req_num_streams = 1,
},

};

cfg.channel.req_num_chan = 1;
cfg.channel.req_chan_map_lo =
dmic_build_channel_map(0, 0, PDM_CHAN_LEFT);
cfg.streams[0].pcm_rate = MAX_SAMPLE_RATE;
cfg.streams[0].block_size =
BLOCK_SIZE(cfg.streams[0].pcm_rate, cfg.channel.req_num_chan);

ret = do_pdm_transfer(dmic_dev, &cfg, 2 * BLOCK_COUNT);
if (ret < 0) {
return 0;
}

cfg.channel.req_num_chan = 2;
cfg.channel.req_chan_map_lo =
dmic_build_channel_map(0, 0, PDM_CHAN_LEFT) |
dmic_build_channel_map(1, 0, PDM_CHAN_RIGHT);
cfg.streams[0].pcm_rate = MAX_SAMPLE_RATE;
cfg.streams[0].block_size =
BLOCK_SIZE(cfg.streams[0].pcm_rate, cfg.channel.req_num_chan);

ret = do_pdm_transfer(dmic_dev, &cfg, 2 * BLOCK_COUNT);
if (ret < 0) {
return 0;
}

LOG_INF("Exiting");
return 0;
}

Error-

s\ANUSHKA\dmic\build\zephyr && D:\ncs\toolchains\c57af46cb7\opt\bin\cmake.exe -E true""

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: d:\ncs\toolchains\c57af46cb7\opt\zephyr-sdk\arm-zephyr-eabi\bin\../lib/gcc/../../picolibc/arm-zephyr-eabi/lib/thumb/v8-m.main/nofp\libc.a(libc_tinystdio_fdopen.c.o): in function `fdopen':

fdopen.c:(.text.fdopen+0x20): undefined reference to `close'

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: fdopen.c:(.text.fdopen+0xa4): undefined reference to `read'

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: fdopen.c:(.text.fdopen+0xa8): undefined reference to `write'

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: fdopen.c:(.text.fdopen+0xac): undefined reference to `lseek'

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: fdopen.c:(.text.fdopen+0xb0): undefined reference to `close'

d:/ncs/toolchains/c57af46cb7/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: d:\ncs\toolchains\c57af46cb7\opt\zephyr-sdk\arm-zephyr-eabi\bin\../lib/gcc/../../picolibc/arm-zephyr-eabi/lib/thumb/v8-m.main/nofp\libc.a(libc_tinystdio_fopen.c.o): in function `fopen':

fopen.c:(.text.fopen+0x1e): undefined reference to `open



Host operating system- Windows 11

nRF connect sdk toolchain- v2.5.2 

nrf sdk- v2.5.99-dev1

I am using the nRF Connect for VS Code extension.

 

  • Hello,

    Which Nordic chip or development kit are you using here? Could you point me to which sample from the SDK you are trying to build here? (Providing a link will work.)

    nRF connect sdk toolchain- v2.5.2 

    nrf sdk- v2.5.99-dev1

    I recommend you use the nRF Connect SDK version v2.5.2, as it is the latest stable release.

    Kind Regards,

    Abhijith

  • Which Nordic chip or development kit are you using here? Could you point me to which sample from the SDK you are trying to build here? (Providing a link will work.)

    I am using nrf5340 audio DK. I have already built the DMIC sample (github.com/.../dmic) from Zephyr. Now, I want to upgrade the code to collect audio data from the PDM microphone and store it as PCM data. For this purpose, I've made some changes in the code. I aim to store the PCM data in a text file. Could you please review the changes and ensure they are correct for achieving this?

    Code is encountering an error when trying to open the file. When I search for the error solutions, I find that there are linkage problems, and some are saying there are include path problems as well. However, I am not sure how to solve these. Could you please provide the solution and steps to resolve this?

     

    nRF Connect SDK version v2.5.2

    Yes I am using the same.

     

  • Hello,

    Anushikha said:
    Yes I am using the same.

    But in the details you shared (in the parent query), the version looks different. You are using the nRF Connect SDK Toolchain (v2.5.2) and the nRF SDK (v2.5.99-dev1). It is okay if you are using v2.5.2 of the SDK, but if not, I recommend switching to a stable version, i.e., v2.5.2.

    From the error message, my guess is that the compiler is unable to find a definition for the function you are trying here. I will take a detailed look at this tomorrow.

    Kind Regards,

    Abhijith

  • But in the details you shared (in the parent query), the version looks different.

     I switched to v2.5.2

    After that I made some changes in the code and the kconfig file. This is the log message I am getting- 

    *** Booting nRF Connect SDK v2.5.2 ***
    [00:00:00.254,913] <inf> dmic_sample: DMIC sample
    [00:00:00.254,943] <inf> dmic_sample: PCM output rate: 16000, channels: 1
    [00:00:00.254,943] <inf> dmic_nrfx_pdm: PDM clock frequency: 1024000, actual PCM rate: 16000
    [00:00:00.355,468] <inf> dmic_sample: 0 - got buffer 0x2002fed8 of 3200 bytes
    --- 10 messages dropped ---
    [00:00:00.455,841] <inf> dmic_sample: 1 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:00.556,182] <inf> dmic_sample: 2 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:00.656,524] <inf> dmic_sample: 3 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:00.756,866] <inf> dmic_sample: 4 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:00.857,208] <inf> dmic_sample: 5 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:00.957,550] <inf> dmic_sample: 6 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:01.057,891] <inf> dmic_sample: 7 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:01.158,233] <inf> dmic_sample: 8 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:01.258,575] <inf> dmic_sample: 9 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:01.358,947] <inf> dmic_sample: 10 - got buffer 0x2002e5d8 of 3200 bytes
    --- 10 messages dropped ---
    [00:00:01.459,289] <inf> dmic_sample: 11 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:01.559,631] <inf> dmic_sample: 12 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:01.659,973] <inf> dmic_sample: 13 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:01.760,314] <inf> dmic_sample: 14 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:01.860,656] <inf> dmic_sample: 15 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:01.960,998] <inf> dmic_sample: 16 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:02.061,340] <inf> dmic_sample: 17 - got buffer 0x2002ccd8 of 3200 bytes
    [00:00:02.161,682] <inf> dmic_sample: 18 - got buffer 0x2002fed8 of 3200 bytes
    [00:00:02.262,023] <inf> dmic_sample: 19 - got buffer 0x2002e5d8 of 3200 bytes
    [00:00:02.362,487] <err> dmic_sample: Error opening the file.

    I checked the files path and the permissions of directories but still there is no change.
    I also try file handling operation in same path by normal C code, and now the file is creating, I can write,  everything is working perfectly.
    I don't understand why only through nrf connect I am not able to create and open the file.

    There is some environment configuration setting that I am missing. this is my .conf file

    CONFIG_AUDIO=y
    CONFIG_AUDIO_DMIC=y
    CONFIG_LOG=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_FILE_SYSTEM=y

    Also I am attaching the zip file
    error.zip
  • Hello,

    Your overlay and configuration file looks good to me.

    You might also want to enable the specific file system you're using here See the configs CONFIG_FILE_SYSTEM_LITTLEFS. give it a shot and let me know if this helped.

    Kind Regards,

    Abhijith

Related