Bodmer / JPEGDecoder

A JPEG decoder library
Other
220 stars 64 forks source link

Accessing JPG files in Mega2560 Flash memory #46

Closed prenticedavid closed 4 years ago

prenticedavid commented 4 years ago

The MEGA2560 has got a whole 256kB of Flash. You can store several JPG images but these need to live at the "top" of Flash Memory i.e. after the executable code.

Regular PROGMEM goes to the "bottom" of Flash (just after the vector table) This makes it easy for pgm_read_byte() to access 0-64kB of data. But pgm_read_byte_far() can access all 0-256kB Likewise, memcpy_PF() is the far version of memcpy_P()

This can put your JPG data after the code section.

#if defined(AVR)
#undef PROGMEM
#define PROGMEM  __attribute__((section(".fini2")))
#endif

and this can make your sketch work with ESP8266, ESP32, ...

#if !defined(AVR)
#define uint_farptr_t       uint8_t*
#define memcpy_PF           memcpy_P
#define pgm_get_far_address
#endif

Your sketch will use pgm_get_far_address(x) and cast to uint_farptr_t for the JPG array in Flash. And use memcpy_PF() to copy to SRAM.

JPEGs stored as byte arrays in Flash are limited to 32767 bytes by the AVR compiler. There is no size limit for ARM, ESP, ... Complex images will compress below 32kB. They render well. And you can fit several in Flash memory.

Yes, it makes Flash images practical for a Mega2560 or Xmega.

Redefining Preprocessor macros must be done with care. Most AVR programs only expect to use 0-64kB PROGMEM. ARM and ESP targets do not have these problems.

David.

Bodmer commented 4 years ago

Thanks, I have updated the library to support the Mega/Xmega change.

I researched this a few years ago but never found a solution so thanks again for solving this.