Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: How do I "bake" a file into the firmware

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    3

    How do I "bake" a file into the firmware

    I need read-only access to a binary data file (essentially a lookup table). I don't need to change it without flashing the firmware and I don't need to change it or write to it. I am completely new to teensy/arduino programming and I haven't touched C/C++ since college so I'm at a complete loss. Does anyone know how to do this?

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,830
    The simplest way is just to declare it as a 'const' array with static initialization (assuming the index is a simple non-negative number).

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    3
    It does ultimately map to an array but I believe there are about 20k entries. Also, the file is generated so even though it doesn't change I need a way to update it during development.

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,047
    You can use bin2hex which should be available for all OS. (e.g. https://tomeko.net/online_tools/file_to_hex.php?lang=en)

    This will generate an ascii file (lets call it data.hex) containing the hex codes of your binary file.
    E.g.
    Code:
    0x25, 0x50, 0x44, 0x46, 0x2D, 0x31, 0x2E, 0x35, 0x0D, 0x0A, 0x31, 0x20,
    0x30, 0x20, 0x6F, 0x62, 0x6A, 0x0D, 0x0A, 0x3C, 0x3C, 0x0D, 0x0A, 0x2F,
    0x54, 0x79, 0x70, 0x65, 0x20, 0x2F, 0x50, 0x61, 0x67, 0x65, 0x73, 0x0D,
    0x0A, 0x2F, 0x4B, 0x69, 0x64, 0x73, 0x20, 0x5B, 0x32, 0x20, 0x30, 0x20,
    0x52, 0x20, 0x5D, 0x0D, 0x0A, 0x2F, 0x43, 0x6F, 0x75, 0x6E, 0x74, 0x20,
    0x31, 0x0D, 0x0A, 0x2F, 0x52, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65,
    0x73, 0x20, 0x33, 0x20, 0x30, 0x20, 0x52, 0x0D, 0x0A, 0x3E, 0x3E, 0x0D,
    0x0A, 0x65, 0x6E, 0x64, 0x6F, 0x62, 0x6A, 0x0D, 0x0A, 0x32, 0x20, 0x30,
    0x20, 0x6F, 0x62, 0x6A, 0x0D, 0x0A, 0x3C, 0x3C, 0x0D, 0x0A, 0x2F, 0x54,
    ....
    Then do the following data.h and data.cpp files

    data.h
    Code:
    #include <cstdint>
    extern const uint8_t data[];
    data.cpp
    Code:
    #include <data.h>
    
    const uint8_t data[] =
    {
        #include "data.hex"
    };
    Whenever your binary changes all you have to do is to run bin2hex again, recompile and you are done. Depending on your build system you can easily automate the bin2hex step (makefile target, prebuild step, adopting platform.txt? ...)

    bin2hex also allows for 16 and 32bit output if you need that.

  5. #5
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,830
    Quote Originally Posted by sasha1sum View Post
    It does ultimately map to an array but I believe there are about 20k entries. Also, the file is generated so even though it doesn't change I need a way to update it during development.
    Well, I'm sure there are ways, but deep diving into figuring out how to modify the file to incorporate an extra file, may involve learning many things about the Teensy environment.

    It is simpler if you have your generator just rewrite a C++ file in the same directory as the .ino file.

  6. #6
    Junior Member
    Join Date
    Jul 2020
    Posts
    3
    Thanks @luni, this is exactly what I was looking for

  7. #7
    You can put the table in an "include" file and do a #include. Use: static rom name_of_your_table [] = {,,,}

    You should double check the word rom. I think the compiler should know what it means.

    When you compile it will be picked up and added to the flash.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •