Look up table out of bounds

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

Moderator: Benj

Post Reply
howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Look up table out of bounds

Post by howard123 »

Hello all

I am populating an array with interger elements, using RAM, as I need values for a look up table. To do this I entered int FCV_LUT[101] = {0,5,1,17,23,....977}; in the supplementary code box.

I am not sure how the supplementary code functions and would be grateful for a pointer, having not found it. How does it differ from putting code in a C block as shown, disabled, on my example?

Thanks once again
Howard
FC6.0.10
Attachments
LUT test.fcfx
(34.93 KiB) Downloaded 191 times
Last edited by howard123 on Sat Sep 27, 2014 10:15 am, edited 1 time in total.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Look up table out of bounds

Post by medelec35 »

Hi Howard,
Life is made easier with FC6 since it has a dedicated LUT component built-in, so no C code required.
Just click search and enter lut.
What you need to do is select properties of LUT, copy and paste your comma separated numbers in the integers section.
when you want to retrieve LUT data, just as a component macro assign it to the LUT GetInt of course adding the index variable.
A LUT example can be downloaded from here
It can be found within the components section.

The reason why your simulation shows index out of bounds is because the LUT variable is assigned using C and simulator does not simulate C code.
Problem will be resolved using LUT component.


Martin
Martin

howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Re: Look up table out of bounds

Post by howard123 »

Hi Martin and thanks once again for your reply.

I had started off using the built in LUT component but it unfortunately only works with bytes and I need larger numbers.

I had not used the simulator and was testing it on hardware. My mistake was that I had declared [101]in C code and [3] in regular FC. Once I had corrected the latter, it worked but there remains a hole in my understanding and probably in my implementation as well.

To extract the integer in regular FC (not C code) I declare FCV_LUT[101]. You may see this in the project explorer as FCV_LUT[3], this being my mistake. In also declared it in C as int FCV_LUT[101]={0,5....}; . So it is declared in two places and that has used about 400 bytes of RAM. This is clearly wrong on my part and there should be a better way to address the C code directly.

The question is how do I extract something defined in C into regular FC. Defining FCV_LUT[101]={0,5....}; in C and then printing FCV_LUT[10] to the display does not work for me unless I declare FCV_LUT[101] in regular FC as well. An inefficient process.

In addition I could put the FCV_LUT[101]={0,5....}; C line either at the top of the code or in the supplemental code block. Both worked and both used the same memory space. I am not able to fathom the difference between these two methods.

I could put the complete thing in regular FC and am just being lazy as I have a few CSV strings which I don’t want to retype as LUT[0]= 0 LUT[1]=5 …. and so on.

Thanks

Howard

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Look up table out of bounds

Post by kersing »

Howard,

LUT allows you to store large datatypes as well. The GetInt macro returns an unsigned long value.

Regarding the C versus Flowcode issue, as C code is ignored in simulation your array will not be filled with values as far as simulation is concerned when it is declared and filled in C code. To be able to simulate there are only two options, use assignments for every element (costly on chip as every assignment takes a couple of bytes in ROM) or use the LUT component.

Regarding the difference between C code block and supplemental code, the first inserts the C code withing the function that is generated from your macro. Any variables declared in it will be local variables accessible in that function only (like the .name variables in Flowcode macros). Anything in the supplemental code is inserted at the 'top' level of the C code. Variables declared there are like globals in Flowcode. If at some point in time you need to add your own functions, that is only possible at C code top level, so in the supplemental code. Would you insert a function definition in a C code block, that would break your code as the generated C code would have a function definition within a function definition, something C does not allow.

Hoping this helps,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Re: Look up table out of bounds

Post by howard123 »

Hi Jac

Thanks for your reply

I could not get the LUT component to return anything greater than a byte. I had seen that it has a ULONG return but as far as I can determine this is wrong. I could not get any value greater than 255 from the LUT. This may be because the component is writing to ROM and this in my case is 8 bits. Please correct me if I am wrong.

I have not been using the simulator and the code is tested and running on hardware all-be it with an expensive RAM usage. You may not have seen this in my last post as we wrote at similar times.

My reason for wanting to use C is to be able to enter longish C strings which would allow for not having to enter an assignment for every element. My strings already exist as CSVs.

Defining 100 array integers in C uses 200 bytes. My question is how to then to access these integer elements. The way that I have found (clearly incorrect) is to define an unassigned FCV_LUT[100] in regular Flowcode. This used an additional 200 bytes which to my thinking should not be necessary.

What are your thoughts Jac..

Howard

howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Re: Look up table out of bounds

Post by howard123 »

Small update

I have tested the component LUT but putting an array of 100 elements in it and compiling. I then increased the amount of elements by 2 (integers of 1000) and again compiled.

The first result had RAM at 493 bytes and ROM at 2854 bytes. The second test had RAM at 493 bytes and ROM at 2856 bytes.

From this it seems that the component LUT stores in ROM and that when storing an integer the value gets truncated to a byte.

Howard

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Look up table out of bounds

Post by kersing »

The code generated for the LUT component contains a byte array for the values in the INT property (at least on 8 bit PICs), you are right all values are truncated to bytes. Perhaps Matrix can create a LUT component where the user can select the type of the array to be used?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Re: Look up table out of bounds

Post by howard123 »

Thanks Jac.

For my integer LUT problem here is a working solution. I am quite embarrassed.... :wink:

Thanks again guys.
Attachments
LUT test 2.fcfx
(15.5 KiB) Downloaded 197 times

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Look up table out of bounds

Post by kersing »

Hi Howard,

After a bit of investigation it turns out the Lookup Table component does support 16 and 32 bit integer types as well. To switch the data type you need to incorporate a call to the SetIntBits macro of the component. This can be done at the start of your main program for simulation purposes. For code generation at compilation time I use the 'Compile Start' event.

To create this event, select the "EV" icon at the top of the project explorer. Next double click on "Start" in the "Compile" category of the tree (top node). In the new dialog, double click on "Macro", this will open "Edit Macro Details". Click "OK". Now open the macro and add a call to lut::SetIntBits() with the appropriate size (use the down arrow in the expression to show the options. Byte = 8 bits, Word = 16 bits and Long = 32 bits)

Add an assignment icon to set .Return to zero to signal Flowcode to proceed. (Non zero prevents compilation according to the wiki)

The resulting macro could look like this:
lut-16bit.PNG
(23.47 KiB) Downloaded 3244 times
After hitting the compile to C button the generated code looks like this:

Code: Select all

ROMARRAY_(MX_UINT16) FCD_00fb1_lut1__INTLIST_LUT ROMARRAY_E =
{
// Property added elements
  2048, 4, 6, 8, 1024
// Dynamically added elements
 
};
Notice the MX_UINT16 :-)

Using this method allows simulation, the use of C code does not.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Re: Look up table out of bounds

Post by howard123 »

Hi Jac
Thanks for your detailed and interesting insight on this. I have done as you suggested and have got it to work in simulation using 16 bit words. I tried to target it to my 8 bit chip and got a message that the device ROM does not support more than 8 bits. I reread your explanation and now understand that this was for simulation only.

I had read the BoostC manual and have tried rom*=[] with similar results; a limitation of 8 bits.

I will likely change to a 16 bit device such as 30F4011 to solve this. Fingers crossed...
regards
Howard

Post Reply