Problems with const array of functionpointers (p16 only?)

Beta Testing discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Problems with const array of functionpointers (p16 only?)

#1 Post by Dany » 28 Jun 2012 14:41

v.5.6.1.

Hi, during testing on a P16F628A of the "StateMachine library" I encountered a peculiar phenomenon: the execution of the last entry in a "const array" of "pointers to functions" failed: the PIC resets.

In the code below I would expect an output (via uart) as here:
Started
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
From: 2, To: 2, Event: 3here
but I see
Started
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
etc...
Apparently when the last entry in the array gets executed something goes wrong, the PIC restarts.

This is the code:

Code: Select all

{$DEFINE DEBUG}

type TActionProc = procedure(From, Towards, Event: byte);

Procedure A_a(from, towards, event: byte);
begin
  {$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}  
end;

Procedure A_b(from, towards, event: byte);
begin
  {$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}  
end;

Procedure A_i_(from, towards, event: byte);
begin
  {$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}  
  Uart_write_Line('here');
end;

const SmallTable: array[3] of ^TActionProc =
      (@A_a,
       @A_b,
       @A_i_
      );
....
begin
  { Main program }

  CMCON  := 7;             // Disable Comparator module's
  PORTA  := 0;             // initialize portA
  TRISA  := %00110011;
  
{$IFDEF DEBUG}
  Uart1_init(9600);
  delay_ms(100);
  Uart_write_Line('Started');

  for TmpIndex := 0 to 2 do
  begin 
    SmallTable[TmpIndex]^(TmpIndex, 2, 3);  // call the routine, first parameter is the index in the table
    Uart_write_line('');
  end;
  Uart_write_line('');
  
  while true do ;
{$ENDIF}
end.
As you can see the only action done here is to call (indirectly) all entries in the table "SmallTable".

Only the last entry in the table shows the erroneous behaviour, so extending the table with one (not used) entry (e.g. with "nil" as value) makes the problem go away.

I have only see this with the P16F628A, not with e.g the P18F2550.

As far as I can tell the content in rom of "SmallTable" seems to be correct (tough I do not understand it).

Thanks in advance! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Problems with const array of functionpointers (p16 only?

#2 Post by Dany » 18 Aug 2012 19:46

Hi mE people, were you able to reproduce this phenomenon?

Thanks in advance.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problems with const array of functionpointers (p16 only?

#3 Post by janko.kaljevic » 20 Aug 2012 14:16

Hello Dany,

I apologize for the late reply.

I was able to reproduce this behavior on PIC16f controllers, and we will work on this issue.
As you have noticed at the moment, workaround is to use larger array for one element.

I will notice you, regarding our findings.

Best regards.

Post Reply

Return to “mikroPascal PRO for PIC Beta Testing”