This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Fatfs example essential libraries

Hello.

I just started using nRF52840-DK board with SEGGER Embedded Studio and I'm trying to use it to save data in a micro SD through an adapter. I tested it with the Fatfs example provided by Nordic and it works as intended. My next step is to reduce the code to the minimun needed to keep it working, removing not essential libraries and functions.

This example contains the following libraries:

#include "nrf.h"
#include "bsp.h"
#include "ff.h"
#include "diskio_blkdev.h"
#include "nrf_block_dev_sdc.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

By now, I removed all the log ones, and I'm testing the first five ones, but they are really large. I assume that nrf.h is required because of the board and also ff.h because of the micro SD, but there are many functions on the ff.h one and probably most of them aren't required to just write on the SD. I don't know about the other three ones yet.

Has anyone tried this before? If yes, could you provide me with some help or guidance on which functions/libraries can I safely remove or reduce as much as possible?

Thanks in advance

  • Hi,

    It sounds like you are looking for "dead code elimination across compilation units", but before you search for that I have to warn you this sounds like premature optimization. There is a lot of flash space available on the nRF52840 (1 MB) and the entire fatfs example weighs in at around 20 kB. That is at 2 % of available flash on the SoC. Your fatfs filesystem is on an external device, not on the remaining 98 % of internal flash, so you do not need space in flash for the filesystem. If you get too little flash left on the nRF52840 it is due to other code or other storage, and that is where to look for savings (it would be 50 times bigger, so a much larger potential to save space there.)

    If you still want to remove code from the fatfs project:

    Note that merely not including the header files is not enough for removing the code from the final build. A rough description of the build process is that first all the .c files are compiled as separate units (by a compiler), and then they are all merged into one executable (by a linker). Each .c file being compiled is known as a "compilation unit", and the .c file needs to include header files (.h files) that describe functionality used from other compilation units. This is needed for "tagging" function calls for the finished compiled code, so that the linker knows how to put it all together. The linker puts everything from all the compilation units into the final executable, regardless of what is called from where.

    In any case, removing the logging should be easy (and safe), although you do lose functionality that may be very useful for tracing what the application does during development (and it is also useful for debugging.) I would not recommend removing the logging.

    nrf.h is for defining constants, structures for where the registers of peripherals are located, etc., and does not have any single accompanying .c file. Most of it (probably all of it) is required, so nothing to remove there.

    bsp.h belongs to the Board Support Package (BSP). It does have some functionality for handling buttons which adds some (but not much) code (including handling debouncing). If you do not include anything from BSP, you should probably include boards.h (which usually gets included through bsp.h), as that file (and further includes from within it) contain defines that are used throughout the SDK.

    The other .h files that you mentioned belong to .c files used for implementing fatfs. What is used and what is not used there depend on the usage scenario, and I do not have an overview for what is required for the fatfs example.

    All in all for this particular case there doesn't seem to be much to remove, and it may require a lot of work to remove it, for virtually no gain.

    Regards,
    Terje

  • Hello tesc,

    First of all, thanks for your answer. I know it is hard to achieve my goal because of the complexity. The fact is that I only want to use the fatfs functionality, not everything else that is included on the example folder. It's not a matter of space, but of simplicity, as I don't need most of it.

    I mean, I have already designed my own SPI libraries and I want to use them to implement the fatfs, using the minimal code from the example. To be exact, I only want to implement the folder C:\nRF5SDK160098a08e2\external\fatfs from the example. I assume that it also uses another libraries from the folder, as I saw them on the code.

    So, from you answer I get that nrf.h is required, bsp.h is not (I've already found some way to reduce it), logs can be easily removed although for debugging purposes I'll keep them for now, and the other three are the ones that I should keep for the fatfs functionality.

    By the way, as I said, I've already designed an SPI library for another purposes, I'll search for it, but maybe you could point me to where the drivers between SPI and fatfs are.

    Thanks for you help.

  • Hi,

    I am sorry, but I did not quite understand what you meant by "the drivers between SPI and fatfs". If you elaborate I might be able to find something, but if you know what you are looking for you would probably have found it in code by now.

    In any case, drivers for external devices are found under <sdk root>/components/drivers_ext/, a few drivers for on-chip peripherals under <sdk root>/components/drivers_nrf/, and then you have higher level libraries under <sdk root>/components/libraries/. Low-level drivers and HALs, are found under <sdk root>/modules/nrfx. See also the documentation for Hardware Drivers on our Infocenter.

    Regards,
    Terje

Related