patchAVR C Runtime Library - Patches: patch #6352, Far pointer library


You are not allowed to post comments on this tracker with your current authentication level.

patch #6352: Far pointer library

Submitter:  Carlos Lamas <carloslamas>
Submitted:  Fri 21 Dec 2007 02:40:29 PM UTC
Category:  None Priority:  9 - Immediate
Status:  Ready For Test Privacy:  Public
Assigned to:  arcanum Open/Closed:  Open

Jump to the original submission

Mon 26 Sep 2011 09:29:49 PM UTC, comment #9: 

You seem to have misunderstood my comment.

Carlos's library is fully adequate for its purpose and has been used for that purpose by several users months or even years before it has been accepted in 1.7.0; the problem is that it is incorrectly compiled within avr-libc.

My comment thus describes an identical problem to (I wasn't aware of at the time when this problem was brought to my attention in the linked avrfreaks thread).


Jan Waclawek <wek>
Mon 26 Sep 2011 08:43:32 PM UTC, comment #8: 

> They don't use the ELPM instruction rather, incorrectly,
> the LPM instruction.

The very problem is that AVR with > 64k flash is a segmented architecture and it's not easy to handle them correctly.

For example, ELPM Z+ might change RAMPZ so that this instruction is not of much use.

The root problem is the ABI and that it does not disallow crossing section boundaries, i.e. 64k boundaries.  No data must cross such boundaries and if ELPM Z+ shall be used to get efficient code (users like small/fast/low-ISR-overhead code) the last byte of each segment must be kept free.

Any other approach will lead to very inefficent code and/or strage artifacts.

The alternative is to take away RAMPZ from the compiler altogether and let the user set it appropriately, but the fact that crossing section boundaries does not work smooth won't go away with it, either.

Georg-Johann Lay <gjlayde>
Wed 03 Aug 2011 09:06:02 PM UTC, comment #7: 

The _PF functions in the respective binary libraries are broken, and they ever were, since the've been added to avr-libc in 1.7.0.

They don't use the ELPM instruction rather, incorrectly, the LPM instruction.

The reason is, that these functions rely on LPM_R0_ZPLUS_NEXT macro in common/, which uses the BIG_CODE macro defined in the same file to determine whether LPM or ELPM is appropriate.

BIG_CODE is defined according to FLASHEND macro which comes from the individual files' headers (additionally, the RAMPZ register's address might be needed for some of the files). However, these files are compiled with the "generic" -mavrN switch, so the FLASHEND macro is unavailable thus considered zero by preprocessor.

This bug was originally reported here

Jan Waclawek <wek>
Wed 30 Jun 2010 12:50:06 PM UTC, comment #6: 

The xxx_PF() functions modify the RAMPZ register, but on XMegas with EBI, RAMPZ influences also LD/LDD/ST/STD, making code after using xxx_PF() to access unintended data memory "pages".

In the attached file, in, macro LPM_R0_ZPLUS_INIT is modified and macro LPM_R0_ZPLUS_FINI added, storing and restoring RAMPZ, for targets with _AVR_HAVE_RAMPD_ defined (which IMHO is unique indication of presence of EBI). Only LPM_R0_ZPLUS_FINI needs to be added to xxx_PF() functions (and documentation modified accordingly), as shown in the attached memcpy_PF(). All the xxx_PF() functions are affected and should be modified accordingly after consensus happens.

As a cheaper alternative, RAMPZ could be simply zeroed as all "standard" RAM access in current avr-gcc is restricted to 16-bit addresses; whether this is a better option I leave to decide for the more experienced developers on this forum.

This resolves which thus could be closed as duplicate (the issue with RAMPZ mods being non-interrupt safe is resolved by saving RAMPZ in the standard interrupt prologue/epilogue as of avr-gcc version 4.3+).

(file #20859)

Jan Waclawek <wek>
Thu 03 Jun 2010 03:23:06 PM UTC, comment #5: 

Adding one more function, memcmp_PF().

Jan Waclawek

(file #20682)

Jan Waclawek <wek>
Wed 22 Oct 2008 04:50:15 AM UTC, comment #4: 


Could you create a patch to avr-libc? Otherwise we will have to manually merge your separate files and it will take longer to commit this.


Eric Weddington <arcanum>
Group administrator
Thu 07 Aug 2008 02:20:25 PM UTC, comment #3: 

Hi there

I'd suggest using '"p"  (var) '  instead of '"p"  (&var)'
This allows to define:

typedef uint_farptr_t PGM_PF;
#define PFSTR(str) ((PGM_PF)GET_FAR_ADDRESS(PSTR(str))) 

which again makes it easy to create new functions like printf_PF.

Just my two cent.


Moritz 'morty' StrĂ¼be <cmorty>
Thu 03 Jan 2008 12:54:07 AM UTC, comment #2: 

This is a good point. I've considered this at the time of writing the functions but then I've discarded it considering that the code for pgm_read_xxxx_far() macros in avr/pgmspace.h has also the same problems and EEPROM routines are also affected. RAMPZ modification is documented but I agree that the side effects could be explained more clearly.

Making these functions reentrant secured imposes a small penalty (just to save and restore RAMPZ in any of the free call-used registers, 2 instructions, 2 machine cycles) for each function, probably small enough to avoid having two different versions, one reentrant and the other not. pgm_read_xxxx_far() macros probably could live in two flavors, reentrant and not reentrant. These macros will suffer more (relative to their small size) overhead costs adding RAMPZ preserving code.

This is all referred to interrupt handlers only. Preemptive task execution will require to save and restore RAMPZ as part of the task state for each context swap unless interrupts were disabled in the whole function (absurd)

Maybe it's time to debate the creation of a reentrant avr-libc version...

> 3. In future add support of "far" pointer in avr-gcc.

This sounds a bit optimistic but... who knows?

Carlos Lamas <carloslamas>
Wed 02 Jan 2008 04:20:32 PM UTC, comment #1: 

  These functions modify RAMPZ register and is non-reenterable. They cannot be used in interrupt handlers and pre-emptive Real Time OS. It should be written in documentation.

Your comments on such offer:
  Add in AVR GCC toolchain architectures for 128KB devices. Define  "__AVR_HAVE_RAMPZ__" macro and save RAMPZ register in interrupt handlers for these devices.

1. Safely use more 64KB data stored in FLASH.
2. Add xxxx_PF string functions in avr-libc.
3. In future add support of "far" pointer in avr-gcc.

1. Longer prologue/epologue code in interrupt handlers (4 instructions + 1 byte stored in stack).


Anatoly Sokolov <aesok>
Group administrator
Fri 21 Dec 2007 02:40:29 PM UTC, original submission:  

This code is to intended to access the whole flash memory in devices with flash address space bigger than 64KB.

The macro GET_FAR_ADDRESS() is the best version I've got after testing several ones but is not always a direct replacement of the & operator for getting far pointers. It has not been tested on ATmega2560, not even in simulation.

The string and memory copy functions for far program memory xxxx-PF() are still in beta stage and need more tests (not known bugs but not extensively tested). They had been parked in my hard disk for a long time but the big ROM devices users claim for some standard way to access the data in avr-libc.

Comments are welcomed.

Carlos Lamas.

Carlos Lamas <carloslamas>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #20859: added by wek (5KiB - application/zip)
file #20682: added by wek (5KiB - application/zip)
file #14680:  extra added by carloslamas (26KiB - application/zip - Sources to access the flash beyond 64KB)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by gjlayde (Posted a comment)
  • -email is unavailable- added by wek (Updated the item)
  • -email is unavailable- added by arcanum (Posted a comment)
  • -email is unavailable- added by cmorty (Posted a comment)
  • -email is unavailable- added by aesok (Posted a comment)
  • -email is unavailable- added by carloslamas (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.


    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2010-06-30 wek Attached File- Added, #20859
    2010-06-13 arcanum Priority5 - Normal 9 - Immediate
    2010-06-13 arcanum StatusNone Ready For Test
        Assigned toNone arcanum
    2010-06-03 wek Attached File- Added, #20682
    2007-12-21 carloslamas Attached File- Added extra, #14680

    Back to the top

    Powered by Savane 3.12.
    Corresponding source code