Page 1 of 1

Pointer to Constants

Posted: 31 Oct 2014 05:57
by JimKueneman
I am porting some code that work in the dsPIC and PIC32 compilers but won't compile in the PIC compiler:

Code: Select all

type
  TEventID = array[0..7] of Byte;                                              
  PEventID = ^TEventID;

const
  USER_VNODE_SUPPORTED_EVENTS_CONSUMED: array[0..USER_MAX_VNODE_SUPPORTED_EVENTS_CONSUMED-1] of TEventID = (
    ($01, $01, $00, $00, $00, $00, $FF, $FF),                                    // EVENT_EMERGENCY_STOP
    ($05, $02, $01, $02, $02, $00, $00, $00)                                    // TEST
  );

Code: Select all

function NMRAnetUtilities_EqualEventID(Event1, Event2: PEventID): Boolean;
var
  i: Integer;
begin
…...

Code: Select all

 while (EventIndex < USER_MAX_VNODE_SUPPORTED_EVENTS_CONSUMED) do
  begin
    if NMRAnetUtilities_EqualEventID(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex], DataBytes) then
    begin
      Result := T…..
51 304 Syntax error: Expected "pointer to data (Event1)" but "pointer to cdata (?T7)" found opstackcore_events.mpas
there seems to be a difference between a "data" and "cdata" in the PIC compiler. Can someone enlighten me?

How can I make this portable between all compilers?

Thanks,
Jim

Re: Pointer to Constants

Posted: 31 Oct 2014 13:24
by yo2lio
Hi,

Code: Select all

 while (EventIndex < USER_MAX_VNODE_SUPPORTED_EVENTS_CONSUMED) do
  begin
    if NMRAnetUtilities_EqualEventID(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex], DataBytes) then
    begin
      Result := T…..
When you call the function, both operands must have PEventID type... You put "DataBytes" there...

Code: Select all

function NMRAnetUtilities_EqualEventID(Event1, Event2: PEventID): Boolean;
var
  i: Integer;
begin
…...
must be in this way or similar...

Code: Select all

    if NMRAnetUtilities_EqualEventID(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex], @USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex]) then
Also, you must declare ^const type...

Code: Select all

type
  TEventID = array[0..7] of Byte;                                              
  PEventID = ^const TEventID;

const
  USER_VNODE_SUPPORTED_EVENTS_CONSUMED: array[0..USER_MAX_VNODE_SUPPORTED_EVENTS_CONSUMED-1] of TEventID = (
    ($01, $01, $00, $00, $00, $00, $FF, $FF),                                    // EVENT_EMERGENCY_STOP
    ($05, $02, $01, $02, $02, $00, $00, $00)                                    // TEST
  );

Re: Pointer to Constants

Posted: 31 Oct 2014 14:56
by JimKueneman
Hi Florin,

I did not show you all that was needed:

Code: Select all

function SupportsVNodeEventAsConsumer(Node: PNMRAnetNode; DataBytes: PEventID; var EventIndex: Integer): Boolean;
var
  Event: TEventID;
begin
  Result := False;
  {$IFDEF SUPPORT_AT_LEAST_ONE_VNODE_CONSUMED_EVENT}
  EventIndex := 0;
  while (EventIndex < USER_MAX_VNODE_SUPPORTED_EVENTS_CONSUMED) do
  begin
    if NMRAnetUtilities_EqualEventID(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex], DataBytes) then
    begin
      Result := Tr...
So DataBytes is a PEventID.

It was

Code: Select all

PEventID = ^const TEventID;
Thanks,
Jim

Re: Pointer to Constants

Posted: 31 Oct 2014 15:05
by JimKueneman
Celebrated to soon…..

The problem now is I also call this function with a variable the opposite problems occurs. Is this the reason I see some functions that one is "SomeFunction_Const" and "SomeFunction"? I am sure there is a reason for this in the PIC, is it because of the paging where the other parts have flat memory spaces?

Code: Select all

var
  Event: TEventID;

…..
 while (EventIndex < USER_MAX_VNODE_SUPPORTED_DYNAMIC_EVENTS_CONSUMED) do
  begin
    if AppCallback_DynamicVNodeConsumedEvent(Node, EventIndex, Event) then
      if NMRAnetUtilities_EqualEventID(@Event, DataBytes) then   <<<<<<<
      begin
63 304 Syntax error: Expected "pointer to cdata (Event1)" but "pointer to data (?T12)" found opstackcore_events.mpas
Jim

Re: Pointer to Constants

Posted: 31 Oct 2014 15:11
by yo2lio
Hmmm, try with dword(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex])

Re: Pointer to Constants

Posted: 31 Oct 2014 15:16
by JimKueneman
Hi Florin,

You mean instead of declaring the constant pointer?

Jim

Re: Pointer to Constants

Posted: 31 Oct 2014 15:21
by yo2lio
JimKueneman wrote:Hi Florin,

You mean instead of declaring the constant pointer?

Jim
Please put a piece of code that I can compile...

Re: Pointer to Constants

Posted: 31 Oct 2014 16:24
by JimKueneman
That is easier said than done Florin. This is a large custom network stack framework and the bits and pieces are spread across many files.

So I went back to what I originally had and tried the cast:

Code: Select all

dword(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex])
It now compiles…. The question is did it compile into code that works correctly. I need to finish updating my CAN library to work with PIC18 parts and get a part and test bench setup before I will know.

Do you have confidence this will work? I could zip up and send you the entire project and show you where to look for these definitions.

Jim

Re: Pointer to Constants

Posted: 31 Oct 2014 16:36
by yo2lio
JimKueneman wrote: Do you have confidence this will work? I could zip up and send you the entire project and show you where to look for these definitions.
Hmmm, I can not guaranty to you that will be OK in all your code. But I remember, that I was forced to use this approach when I used pointers in const(flash).