Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with FAR pointers in code library for multiple embedded platforms

Tags:

c

embedded

I'm trying to maintain a library of code, with samples, for multiple embedded platforms. I need to support the concept of "far" (non 16-bit) pointers for some function parameters.

I thought I had a good solution with defining the macro FAR to be __far on some platforms and nothing on platforms with 32-bit pointers (embedded Linux, Win32, etc.). With that macro, I could easily define pointers as somestruct_t FAR *foo.

But then I started working with Freescale processors, and their compiler requires the FAR to go between the asterisk and variable name. (somestruct_t * __far foo).

The best solution I've come up with to handle this case is to define a macro FARPTR as either __far *, * __far or just * depending on the platform. This allows for somestruct_t FARPTR foo.

Are there cleaner solutions out there? In particular, I don't like that there isn't a * visible to someone reading that code. I'm also worried that I will run into problems when it comes to function declarations. Get a load of this syntax from the Freescale compiler help:

int __far *f();          // __far function returning a pointer to int
int * __far f();         // Function returning a __far pointer to int
int __near * __far f();  // __near function returning a __far pointer to int

That last one kills me -- a qualifier inside of the return type indicates a near function?! And I've recently learned that adding the __near isn't enough to actually compile a function to near memory -- I need to wrap it in pragmas.

So, has anyone seen a nicer solution than my FARPTR macro idea?

like image 463
tomlogic Avatar asked Jun 09 '11 18:06

tomlogic


People also ask

What is the difference between far pointer and huge pointer?

Huge pointer has the same size of 32-bit to that of a far pointer, and it can also access bits that are located outside the sector. Far pointer which is fixed and hence that part of the sector in which they are located cannot be modified in any way; huge pointers can be.

What is the difference between far and near pointer?

Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time. A far pointer is typically 32 bit that can access memory outside current segment.

What is a far pointer where we use it?

In a segmented architecture computer, a far pointer is a pointer which includes a segment selector, making it possible to point to addresses outside of the default segment.

What is function pointer in embedded C?

Function pointers are identical to any other pointer type except that they point to a function instead of a variable type. The pointer can then be used to call the function that is being pointed to. This allows a whole range of techniques to be used when developing an embedded system.


2 Answers

The freescale processor's usage is more consistent with standard type qualifiers such as const, that placement causes it to refer to the pointer not the data being pointed to. That said, since "far data" rather than a "far pointer" would be meaningless, you'd have thought that it would not matter, but greater consistency presumably makes for a simpler compiler parser.

You could use something kludgey such as:

#if defined __SOME_ARCHITECTURE__

    #define DECLARE_FARPTR( type, identifier ) type __far * identifier

#if defined __SOME_OTHER_ARRCHITECTURE__

    #define DECLARE_FARPTR( type, identifier ) type * __far identifier

#else

    #define DECLARE_FARPTR( type, identifier )

#endif

Then your declarations would look like:

DECLARE_FARPTR( somestruct_t, foo ) ;

or in a function prototype taking a pointer parameter:

void fn( DECLARE_FARPTR( somestruct_t, foo )  ) ;

or a function returning a far pointer:

DECLARE_FARPTR( somestruct_t, fn( void ) ) ;

As you can see it quickly gets hard to read and a declarative function-like macro is generally a thing best avoided.

like image 98
Clifford Avatar answered Sep 30 '22 15:09

Clifford


I don't have a specific better solution for you. However, having dealt with the same problem on several occasions, I recommend a review of the AUTOSAR Specification of Compiler Abstraction (PDF).

It includes a detailed approach to dealing with multiple compilers.

like image 45
Tim Henigan Avatar answered Sep 30 '22 15:09

Tim Henigan