In this section we will discuss how to link all your images to a binary file that you can put in a non-memory mapped flash and how to use that file at runtime together with the bitmap cache. TouchGFX cannot draw bitmaps that are stored in non-memory mapped flash, but by caching the bitmaps in RAM we can make the bitmaps useable in the application.
See the article Caching Bitmaps for a general discussion on the bitmap cache.
In this article we assume that you have setup a bitmap cache, and that you want to store your bitmaps in non-memory mapped flash. This can be e.g. USB storage, NAND flash etc.
The goal is to link the images to a specific address, copy the images to a file, and help TouchGFX to copy from the file to the cache.
Copying bitmap data from flash to cache
Recall that when you cache a bitmap, TouchGFX copies the pixels from the original location to the bitmap cache.
This copying is done by calling this method from the HAL class:
If your bitmaps are stored in normal addressable flash (like internal flash or memory mapped external flash), then the normal blockCopy function provided in the TouchGFX library is fine, and you do not need to do anything.
On the other hand, if your bitmaps is stored in storage that is not addressable, e.g. a filesystem, then the normal implementation is not sufficient and you need to provide an updated version that is able to read from your specific flash storage.
But first we need to make sure that our bitmaps is linked to a fixed address.
The BitmapDatabase table
All bitmaps in TouchGFX is generated to .cpp files in the folder generated/images/src. Here the bitmaps are represented as byte arrays.
These arrays are compiled by the C++ compiler as any other source code file and are linked into the application.
Here is a screenshot of a simple application with a Button and a TextureMapper showing a rotating logo:
This application uses 3 images: Button_Pressed, Button_Released, and Logo.
These 3 bitmaps are converted to .cpp files and linked in as part of the application. The images are referenced in a table called the bitmap_database. This table is located in the file BitmapDatabase.cpp. Here is the table from the above example (some details removed):
The arrays declared first are the arrays containing the pixels of the individual bitmaps. These arrays are defined in other .cpp files. The bitmap_database array is holding the addresses of these arrays. TouchGFX uses this array to find the address of the pixels of a bitmap.
When the programmer requests a bitmap to be cached, TouchGFX finds the address of the bitmap in flash (in the bitmap_database array) and copies data from here.
Linker script modifications
The linker selects an address for the bitmaps. This selection is based on the section the bitmaps are placed in. All bitmaps in TouchGFX is by default put into the ExtFlashSection.
The standard linker scripts (here for GCC) puts this section into flash together with other read-only data.
In this example we will put the image data in the ExtFlashSection at address 0x24000000. You can select any address that is otherwise unused (not part of the code or data address space).
First we define a new memory area (USB-flash at address 0x24000000), in addition to the normal internal FLASH and RAM areas:
Then we instruct the linker to put the ExtFlashSection in the USB area:
After linking we can check the addresses of the bitmaps by inspecting the map file (application.map). Here is the relevant part:
We can see here that the total size of the images is 0x23ec0 = 147.136 bytes. The 3 arrays holding the bitmaps are located sequentially from address 0x24000000.
Let's now assume the you want the bitmap data to go to a SD-card. We can extract the binary data for the bitmaps from the .elf file with a simple objcopy command:
This gives us a file (images.bin) containing the image byte arrays only. This file can be copied to an USB flash, an SD-card, or even programmed to a flash chip.
The idea is now that when TouchGFX asks for data above address 0x24000000 we take the data from the images.bin file on the SD-card.
Modifying the BlockCopy function
Recall that when you cache a bitmap to RAM TouchGFX calls HAL::BlockCopy to get the data.
To get this linked to the data on your SD-card we can implement a new BlockCopy in your specific HAL class. Here we assume the class is called TouchGFXHAL (as generated by the TouchGFX Generator):
Now you can start caching bitmaps from the SD-card.
If TouchGFX tries to draw a bitmap that is not cached it will try to
read the pixels from the address found in the
table. This address will be in the range 0x24000000 - 0x24100000 and
the read will result in an exception.
Linking images to RAM
If your available RAM is big enough to hold all the images (in the above example that is more than 147.136 bytes) then you do not need to use the bitmap cache to copy the images.
The strategy is as follows:
- Select a fixed address and range in RAM for the images
- Remove that range from the RAM area in the linker script
- Create a new area IMAGES with the selected address and size
- Place the ExtFlashSection in IMAGES area
- Link the application and check the .map file
- Create the images.bin file from the application.elf
- Before TouchGFX is started, copy the whole images.bin file from the SD-card to the selected address in RAM
This solution is simple, but has some drawbacks:
- The available RAM must be big enough to hold all the images
- Start up time will be larger because of the copying from the SD-card (megabytes can take seconds)