Event Compile.AddVar

From Flowcode Help
Jump to navigationJump to search

<sidebar>Event Contents</sidebar> Sent to a component to allow dynamic amendment of global constant declarations

This event is part of the Compile class

Parameters

STRING VarName

The original Flowcode variable

STRING ExpandName

The expanded variable name

STRING Expansion

The expanded string to inline to the code
This parameter is returned back to Flowcode


Return value

ULONG Non-zero to include definition - initialised on call


Detailed description

No additional information


Examples

On some PIC devices there are sometimes 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


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 AddVar 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


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


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.


Total data memory used (bytes): 0x1116 (4374) 15%

...

HEX file creation successful!


Here is the file as a demonstration.

FC6 Icon.png Far Mem demo