Storing a variable into FAR memory

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Storing a variable into FAR memory

Post by Benj »

Hello,

On some dsPIC and PIC24 devices there are two types of RAM memory due to bus limitations, near and far. Near memory is fast to access but is limited by the size of the internal bus. Far memory is much less limited but means additional overhead when fetching or storing values.
FarMem.jpg
FarMem.jpg (62.3 KiB) Viewed 2900 times
When creating Flowcode variables these will default to being stored in near memory to optimise the time to fetch and store values. If you run out of room in near memory then you may get a message like this during compilation.
C:\Users\BenR\Desktop\FarMem.o: Link Error: Could not allocate section .nbss, size = 4118 bytes, attributes = bss near
Link Error: Could not allocate data memory
To tell the compiler to move the variable into FAR memory in C you would do something like this.
__attribute__((far)) unsigned char data[4096];
The Flowcode variable defaults to something like this.
MX_GLOBAL MX_UINT8 FCV_DATA[FCVsz_DATA]; // Large Data Array
The AdVar event is used during the compile to C process to take the variables from the variable manager and generate the necessary C code. By calling a macro to run on this event we can tailor the way the variables are added to the C and add in the cast to FAR memory.
AdVar.jpg
AdVar.jpg (26.6 KiB) Viewed 2905 times
The first thing we need to do in the macro is compare the name of the variable that is currently being processed with the name of the variable we want to store into FAR memory. The string compare function returns 0 if the two strings match up so in the No branch of the decision we then add the "__attribute__((far)) " string to the expansion string.
EvAddVar.jpg
EvAddVar.jpg (50.89 KiB) Viewed 2906 times
After saving the project and refreshing the C code window the variable now looks like this.
__attribute__((far)) MX_GLOBAL MX_UINT8 FCV_DATA[FCVsz_DATA]; // Large Data Array
And during compilation we now get this.
section address alignment gaps total length (dec)
------- ------- -------------- -------------------
.nbss 0x1000 0 0x16 (22)
.bss 0x1016 0 0x1000 (4096)
.heap 0x2016 0 0x100 (256)

Total data memory used (bytes): 0x1116 (4374) 15%
...
HEX file creation successful!
Here is the file as a demonstration.
FarMem.fcfx
(5.73 KiB) Downloaded 273 times
This info is also now available as an example for the AddVar event in the v6 Wiki: http://www.matrixmultimedia.com/wiki/in ... r#Examples

Post Reply