Search code examples
c++embeddedkeilfreertos

How to store text file on embedded systems flash memory and read from it


I'm trying to do the following: Storing a text file (7kB) in the flash memory of a STEVAL-MKI109V2 (running with freeRTOS) board and read this text file and doing some computation with it on the device itself. I have 2 problems regarding that:

1) Storing the text file Is it enough to just add the text file to my keil project? Can I access it after compiling?

2) Accessing the data That's where I failed until now. At first I tried using fopen() from stdio.h but I got some errors on compilation. I found out that my project compiles by using microLib which seems it doesn't include file I/O. After compiling with standard C - library it was successful but as soon as I reach the fopen part in my code the system crashes.

Now I don't know if the reason is that the text file is not found or if I cannot use fopen() on my embedded system. I didn't find further information inside the STM documents or forums except the FLASH_Unlock(); function but it seems it's used for writing.

Do I need to store my text file in another way and access by memory address instead of just filename? I'm confused and cannot find any information online.

Thanks in advance for any help!


Solution

  • If you just want the contents of the file as a char-string, you can convert the file to C source code e.g. using a small Python program (or any other language, I just use Python for that as it is simple to do that in Python than in C or C++ for instance). Just create something like:

    const char my_text[] = {
    
        ... here goes the text
    
    };
    

    Most simply, just embrace each line with ".

    Then either add that file to the project (you'll require an extern declaration from where you use it) or #include it and make the declaration static (thanks @clifford).