Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Printing full directory using FatFs and NRF_LOG_INFO via UART

I am working on project where I am storing and reading data from an SD card. During the initialization, I would like to print the full contents of the root directory  - "/". The issue that I am running into is that there is a built-in delay in the logging buffer and since a pointer to a string is used, by the time that the string actually prints, the memory that stored the directory gets overwritten and this is no longer correct. One solution is to create a static buffer and fill it with the directory names but this is not practical if the root contains a lot of folders. Also, I was under the impression that the printed data gets stored in the UART buffer, which is configurable in sdk_config.h, but it seems like instead it just stores the pointer to the data structure, which gets updated by the time it is ready to print. I've observed the same issue in the fatfs example included in the SDK. Any ideas on how I can fix this would be hugely helpful. Thank you!

  • Looks like I answered my own question:

    For example, the following way of logging a temporary string is not recommended:

    void foo(void)
    { 
        char string_on_stack[] = "stack";
        //WRONG! by the time the log is processed, variable content will be invalid
        NRF_LOG_INFO("%s",string_on_stack);
    }

    Instead, use the following way of logging a temporary string:

    void foo(void)
    { 
        char string_on_stack[] = "stack";
        //nrf_log_push() copies the string into the logger buffer and returns address from the logger buffer
        NRF_LOG_INFO("%s",nrf_log_push(string_on_stack));
    }

Related