Pointer to Constants

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Pointer to Constants

#1 Post by JimKueneman » 31 Oct 2014 05:57

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

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Pointer to Constants

#2 Post by yo2lio » 31 Oct 2014 13:24

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
  );
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: Pointer to Constants

#3 Post by JimKueneman » 31 Oct 2014 14:56

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

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: Pointer to Constants

#4 Post by JimKueneman » 31 Oct 2014 15:05

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

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Pointer to Constants

#5 Post by yo2lio » 31 Oct 2014 15:11

Hmmm, try with dword(@USER_VNODE_SUPPORTED_EVENTS_CONSUMED[EventIndex])
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: Pointer to Constants

#6 Post by JimKueneman » 31 Oct 2014 15:16

Hi Florin,

You mean instead of declaring the constant pointer?

Jim

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Pointer to Constants

#7 Post by yo2lio » 31 Oct 2014 15:21

JimKueneman wrote:Hi Florin,

You mean instead of declaring the constant pointer?

Jim
Please put a piece of code that I can compile...
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: Pointer to Constants

#8 Post by JimKueneman » 31 Oct 2014 16:24

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

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Pointer to Constants

#9 Post by yo2lio » 31 Oct 2014 16:36

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).
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Post Reply

Return to “mikroPascal PRO for PIC General”