How do I call normal functions with page memory

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I call normal functions with page memory

1,724 Views
cornonly
Contributor II

hello,

my Project.prm:

PAGED_RAM_SCIBUFF1 INTO  RAM_F1;

...

my SCI.c:

#pragma DATA_SEG __GPAGE_SEG PAGED_RAM_SCIBUFF1
unsigned char Buffer_SCI_Receive[2048] = {0};
#pragma DATA_SEG DEFAULT

if I want to use a function, the function :

Fun(unsigned char * __far buf);

I can use it this way :

Fun(Buffer_SCI_Receive);

but,

if I use some else  .c file , Maybe it's from a colleague or Maybe it was downloaded from the Internet.

Their function most is FUN1(unsigned char * buf);

I can't just use it like that : FUN1(Buffer_SCI_Receive);

it will Pointer to the loss.

How do I use it.

Labels (1)
0 Kudos
5 Replies

1,619 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

I hope I will not confuse you a lot because the topic is "a little bit" complex.

If I understand you well then I am not sure you correctly understand pointer (memory addressing) possibilities. C language is high level and it is good to step short test codes via asm instructions to see what the compiler uses and how the accesses are processed.

The RAM memory can by addressed by:

  • Near pointer (no page is necessary, direct space, 16 bit address is used)
  • Far pointer in global memory space (GLD,GST,…global asm instructions are used with 24bit global address from global address space )
  • Far pointer in RPAGE paged memory space(local space) (The address from window 1000-1FFF is used together with RPAGE register setup )

Some of the spaces are addressable with all three options, see attached file for XEP100 or XDP512 device.

So, it is not possible to use pointer of different types as a parameter of the function. Each address space has its own principle of an access.

Of course, there is a possibility to combine near space with local space when we create 24bit pointer and use common addressing as to local space.

0x004000 is the same as 0x4000 and will be processed as a near address access (RPAGE has no meaning because internal mechanizm recognizes access to near space)

0xFC1000 will set RPAGE=0xFC because access to a RAM memory window is recognized.

(both lines access the same address as you can see in the attached memory map)

But,….the same address in a global address space is 0x0FC000 and global address access instructions are used by compiler.

Because of this, if you create some code with other programmers then you have to define joint point. Or, there is possibility to set agreement that all shared data will be placed in near memory space (2000~5FFF) or the same space but linearly ordered to 4000~7FFF if RAMHM=1 and ROMHM=1. (16kB should be enough if you also reorder data from access and importance point of view)

 

There is also another solution where you can pass the pointer as a number and then inside the function process it on the basis of used address or convert incoming address to suitable (one of them selected for addressing) for next data processing.

(In order to avoid redundant issues I suggest to use near space as described above.)

 

Moreover; If you use far data then do not forget to add to compiler command line command “-D__FAR_DATA” (Alt F7-> Compiler for HCS12) because of possible issues described in the file start12.c on the top.

A simple example code is also attached.

Best regards,

Ladislav

0 Kudos

1,619 Views
cornonly
Contributor II

First of all, thank you very much for your answer.

I am currently at the entry level. I have No 9S12XEP100 development experience.I read the instructions in some of the chip manuals and looked them up on the Internet.My project USES large arrays(2K or 4K), so I'm using paged memory. I added command “-D__FAR_DATA” and 

my Project.prm:

PAGED_RAM_SCIBUFF1 INTO  RAM_F1;

#pragma DATA_SEG __GPAGE_SEG PAGED_RAM_SCIBUFF1
unsigned char Buffer_SCI_Receive[2048] = {0};
#pragma DATA_SEG DEFAULT

I'm even a little skeptical right now that I'm using paging memory correctly.But it seems to be working.Because,

my Project.map:

Section Name                    Size  Type     From       To       Segment

---------------------------------------------------------------------------------------------

PAGED_RAM_SCIBUFF1              2048   R/W   0xF11000   0xF117FF   RAM_F1

If I define my own function, I'll use unsigned char * __far, Because I know I'm going to use the global address pointer.But if I call some function that somebody else wrote like Fun(unsigned char * buf) or some library function just like: memcpy(void *destin, void *source, unsigned int size);(<string.h>)

There will be problems.It's almost impossible for me to change a lot of other people's code from unsigned char *  to unsigned char * __far.So I wonder if there's a switch or some configuration,So I can call other functions directly.

I'm looking for a solution.

0 Kudos

1,619 Views
lama
NXP TechSupport
NXP TechSupport

Hi,
You have not mentioned the MCU you use but I suppose some of the derivative of S12XE family. If your colleagues have made functions with a near pointers and you use far space which has no near address alternative then compatibility is not possible. Of course, there are ideas for workarounds you could use but must be tested for each function to be sure there is no problem with it. However, this approach can cause issues in the future when the logic is forgotten and changes should be made or when person who does not know the application logic should add own code into it.

Workaround/Adjustment 1.
Rebuild all user function to *far input parameter and then adjust all processes in the function to this pointer.

Workaround/Adjustment 2.
Redefine each user function to contain two parameters RPAGE and near offset. (The is a condition the varable/string/ array is not allowed to go through more pages because RPAGE is not automatically incremented/decremented with offset overflow/underflow)
At the beginning of the function set RPAGE and the addrssing will be OK becaus internal mechanizm uses addressing mode on the basis of offset (window) address.

Workaroud/Adjustment 3.
Leave everything as it is just before function call with a near pointer as a parameter set the RPAGE to required value and then call the function with the offset value only. It must be ensured that nothing will change (some interrupt) the RPAGE value while function is not finished.

As I wrote, these are the ideas only and each of then reguires some access to original routine. And, which of them is the most suitable and the least dangerous. Hard to answer. It depend on thongs you do and you requirements.

Best regards,
Ladislav

0 Kudos

1,619 Views
cornonly
Contributor II
  • First of all, thank you very much for your words

    I have been waiting for your answer these days.The MCU I am using is S12XEP100.

    These days I'm also searching for solutions to the problem.As you said,If your colleagues create functions using near Pointers, and you use far Spaces with no near address replacement, compatibility will not be achieved.

    I used to use STM32 chip more,It doesn't have that problem.I understand the way I choose to define a variable to a segment and write my own function to call.

    Sometimes I want to use some open source library functions.

    For example, I use the FATFS file system.

    All of their interface functions are fun(unsigned char * ),This means I can't use FATFS directly if I don't have enough non-paging memory space unless I need to change each function to (unsigned char * __far),It's almost impossible.Because there are too many function (unsigned char *) calls.

  • On the other hand, if I use the RTOS,

    The task stack size appears to be directly used by non-paging memory,

    • Because their interface function is (unsigned char * or void *). As a result, the task stack size is limited and USES more non-paged memory, leaving less non-paged memory.There is almost no room to use FATFS anymore.

At present, I am still very troubled and have not found a good solution.I now understand the reasons for and use of paged memory and flash memory.

Currently, some external functions use almost all non-paged memory, as they may be based on some 32-bit microcontrollers.

  • I hope you can provide some ideas.

  • Thank you very much again!

    Best regards!

0 Kudos

1,619 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

I have one more idea.

Instead of placing the array into page space place it into global space.

For example global space for large array in RPAGE space F1 and F2 can be defined linearly by global space

 MY_GLOBAL_RAM = READ_WRITE  DATA_FAR           0x0F1000'G TO 0x0F2FFF'G; //

Of course remove it from paged space definitions.

Moreover, rebuilt the project to a LARGE memory model. In this case all pointers will be paged and access to the variables will be FAR and assembler instructions global store and global load will be used for access.

(Note that in the SMALL and LARGE memory model the default access to variables is near and variables with a far access must be explicitly defined.)

However, it would be good to test each function whether there is no exception or some hidden feature.

Best regards,

Ladislav

0 Kudos