[STM32H750] Fun with ART-Pi (7)-TouchGFX reads pictures from the file system

table of Contents

[STM32H750] Fun with ART-Pi (1)-Use STM32CUBMX to generate TouchGFX project
[STM32H750] Fun with ART-Pi (2)-Make MDK's external QSPI-FLASH burning algorithm
[STM32H750] Fun with ART-Pi ( Three)-How to create a TouchGFX project on ART-PI
[STM32H750] Fun ART-Pi (4)
-Add file system [STM32H750] Fun ART-Pi (5)
-Add network function [STM32H750] Fun ART-Pi (6) -Add FTP server
[STM32H750] Fun with ART-Pi (7)-TouchGFX reads pictures from the file system

experiment platform:

Hardware: RT-Thread official ART-PI H750 development version
software: RT-Thread Studio 1.1.5

Contact the author:

Add me on WeChat, note "add group", join the technical exchange group
Insert picture description here

Experimental ideas

Official document introduction: Use non-memory mapped flash memory to store images

For performance reasons, TouchGFX requires all graphics data stored directly in external flash memory (via pointers) without passing through the driver layer. This means that TouchGFX cannot directly render from non-memory mapped flash memory (such as an SD card). To overcome this limitation, the bitmap cache provides a mechanism to cache part or all of the bitmap data in RAM during power-up. Therefore, when you need to store bitmaps on slow external storage (such as USB disks or SD cards), bitmap caching is very useful.

Read image resources from the file system

1. Modify the link script

By default, all bitmaps in TouchGFX are put into ExtFlashSection, and the standard linker script (here GCC) puts other read-only data into flash memory. In this example, we put the image data into ExtFlashSection at address 0x9A000000. You can choose any other unused address (not part of the code or data address space).
First, in addition to the normal internal flash memory and RAM area, we also define a new storage area (USB flash memory with address 0x9A000000):
Insert picture description here
Then, instruct the linker to put ExtFlashSection into the USB area:
Insert picture description here
after linking, we can check the mapping File (application.map) to check the address of the bitmap. This is the relevant part:
Insert picture description here
now, suppose we want the bitmap data to enter the SD card. We can use a simple objcopy command to extract the binary data of the bitmap from the .elf file:

arm-none-eabi-objcopy -O binary -j ExtFlashSection "${BuildArtifactFileBaseName}.elf" "images.bin"

Insert picture description here
Compiling the code at this time will generate a file (images.bin) containing only the image byte array. The file can be copied to USB flash memory, SD card, and even programmed to flash memory chip.

Next, when TouchGFX requests data above address 0x9A000000, we get the data from the images.bin file on the SD card.

2. Modify the BlockCopy function

When you cache the bitmap to RAM, TouchGFX will call HAL::BlockCopy to get the data.
In order to link it to the data on the SD card or nor flash, we can implement a new BlockCopy in your specific HAL class. Here, we assume that this class is called TouchGFXHAL (generated by TouchGFX Generator).
First look for images.bin in the SD card. If the SD card is not mounted, look for nor flash:

bool TouchGFXHAL::blockCopy(void* RESTRICT dest, const void* RESTRICT src, uint32_t numBytes)
{
    
    
    uint32_t dataOffset = (uint32_t)src;
    if (dataOffset >= 0x9A000000 && dataOffset < 0xA0000000)
    {
    
    
            int fd;
            struct statfs buffer;
            if(rt_device_find("sd0") != RT_NULL)
            {
    
    
                if ((dfs_statfs("/sdcard",&buffer) == RT_EOK) | (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK))
                {
    
    
                    fd = open("/sdcard/images.bin", O_RDONLY, 0);
                    if (fd < 0)
                    {
    
    
                        rt_kprintf("open file for read failed\n");
                        return false;
                    }
                    dataOffset =  dataOffset - 0x9A000000;
                    lseek(fd, dataOffset, SEEK_SET);
                    // for copying data from there.
                    read(fd, (uint8_t *)dest, numBytes);
                    close(fd);
                }
                return true;
            }
            else if(dfs_statfs("/flash",&buffer) == RT_EOK)
            {
    
    
                fd = open("/flash/images.bin", O_RDONLY, 0);
                if (fd < 0)
                {
    
    
                    rt_kprintf("open file for read failed\n");
                    return false;
                }
                dataOffset =  dataOffset - 0x9A000000;
                lseek(fd, dataOffset, SEEK_SET);
                // for copying data from there.
                read(fd, (uint8_t *)dest, numBytes);
                close(fd);
                return true;
            }
            else
            {
    
    
                return false;
            }
    }
    else
    {
    
    
        // For all other addresses, just use the default implementation.
        // This is important, as blockCopy is also used for other things in the core framework.
        return HAL::blockCopy(dest, src, numBytes);
    }
}

3. Copy bitmap data from flash memory to cache

Enable and configure the bitmap cache in TouchGFXHAL.cpp. First, you need to delete the bitmap cache database created by default, and then set a new cache based on the storage area provided.
Insert picture description here

4. Upload images.bin to SD card or nor flash via fileziia

If you have SD, you can copy images.bin to the card through a card reader or fileziia. By default, the program first searches for the images.bin file from the SD card.
Insert picture description here
If there is no SD card, upload it to NOR FLASH. After the program cannot find the SD, it will look for the images.bin file from NOR FLASH.
Insert picture description here

5. Demo presentation

Import the touchgfx demo.
Insert picture description here
Demo code download: https://download.csdn.net/download/sinat_31039061/13139312
Insert picture description here

Guess you like

Origin blog.csdn.net/sinat_31039061/article/details/109763842