Help with Pointers to program memory

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
Malcolm_M
Posts: 184
Joined: 12 May 2007 13:01
Location: Bristol, UK

Help with Pointers to program memory

#1 Post by Malcolm_M » 19 Apr 2018 11:48

mikroPascal Pro for PIC V6.0.1 and PIC18F46K22

I need to read arrays stored in program memory and output the contents on SPI.

The compiler issues warnings, listed in the code, but the code simulates correctly on the software simulator.

Code: Select all

var 
Cmd_Array_Pnt: ^const byte; // ptr is pointer to program memory space

const
//
// Si446X_Boot_Commands
//
// 30MHz crystal osc
//
Si446X_Wake_Up_Cmd : array[7] of byte = (0x02,0x01,0x00,0x01,0xC9,0xC3,0x80);


//
// Si446X_Cmd_Write_SPI;
//
Si446X_Cmd_Write_SPI(Cmd_Array : byte; Num_Bytes : byte);

   var Cmd_Array_Num_Bytes : byte;
   var Cmd_Array_Byte_Counter : byte;
   var SPI_Data : byte;
   
begin

   Cmd_Array_Pnt := Cmd_Array;       <<-- Warning: Implicit typecast of integral value to pointer
   Cmd_Array_Num_Bytes := Num_Bytes;
   
   Cmd_Array_Byte_Counter := 1;

   while Cmd_Array_Byte_Counter <= Cmd_Array_Num_Bytes do
      begin
         SPI_Data := Cmd_Array_Pnt^;
         SPI1_Write(SPI_Data);
         INC(Cmd_Array_Pnt);
         INC(Cmd_Array_Byte_Counter);
      end;
end;


Main

Si446X_Cmd_Write_SPI(@Si446X_Wake_Up_Cmd, 7);  <<-- Warning: Suspicious pointer conversion and
                                               <<-- Warning: Implicit typecast performed from "pointer" to "integral type"
What is the correct way of writing this code?

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Help with Pointers to program memory

#2 Post by janni » 19 Apr 2018 12:30

The warnings have merit and this code will (accidentally) work only if the array starts within first 256 code words. The type of parameter in Si446X_Cmd_Write_SPI routine should reflect type of the passed value

Code: Select all

procedure Si446X_Cmd_Write_SPI(Cmd_Array : ^const byte; Num_Bytes : byte);

Malcolm_M
Posts: 184
Joined: 12 May 2007 13:01
Location: Bristol, UK

Re: Help with Pointers to program memory

#3 Post by Malcolm_M » 19 Apr 2018 15:32

Many thanks janni :D

Declaring Cmd_Array_Pnt: ^const byte; as a byte means that the array being pointed to can have a maximum of 256 elements? Secondly, if the pointer is of type byte the compiler uses this pointer value as an offset from the array start? In other words the pointer does not contain the address in program memory of the start of the array?

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Help with Pointers to program memory

#4 Post by janni » 19 Apr 2018 16:32

Malcolm_M wrote:Declaring Cmd_Array_Pnt: ^const byte; as a byte means that the array being pointed to can have a maximum of 256 elements?
Notation ^const byte does not declare byte, but pointer to byte in flash. Internal representation of pointer depends on the size of memory it may point to. (When you previously declared the parameter as byte, pointer was converted to byte and thus could cover only the first 256 addresses. If the array started at zero address, such 'pointer' could indeed point to one of 256 bytes.)
Secondly, if the pointer is of type byte the compiler uses this pointer value as an offset from the array start?
No. Array index may be used to calculate offset. Array address is in fact pointer to first array element. Pointer contains whole address of object, whether it's an array or element of array. Pointer to object in program memory internally consists of three bytes covering whole possible flash range. (For most 8 bit PIC processors the highest byte is always zero but compiler uses all three bytes.)

Malcolm_M
Posts: 184
Joined: 12 May 2007 13:01
Location: Bristol, UK

Re: Help with Pointers to program memory

#5 Post by Malcolm_M » 20 Apr 2018 13:07

Many thanks janni and I now have a better understanding of pointers, plus code which compiles without warnings :D

Post Reply

Return to “mikroPascal PRO for PIC General”