CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with a multidimensional matrix and pointer
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 11, 2012 3:22 am     Reply with quote

just use the keyword 'rom', where you would otherwise use the keyword 'const'. So:
Code:

rom unsigned char matrix[2][2] = {1,2,3,4};


The difference is that historically you couldn't construct a pointer to the PIC ROM. Most of the early chips didn't support an ability to access a specific 'cell' in ROM, so the constant array was simulated, by creating a program, which returned the required byte, when given an index. However later chips do allow a ROM byte to be accessed this way, so CCS added the ability with the 'rom' keyword. Data stored as 'rom', is basically exactly the same as const, but in the former case the extraction code is still added as a header in front of the data, while in the latter case, a slightly more bulky extraction code is used, which supports the handling of pointers.

However your pointer also needs to be told it is to the ROM. Remember that with the PIC, the ROM is in a completely separate memory space to the RAM. So the pointer declaration needs to be:
Code:

rom int* pointer;

Which says that 'pointer' is a pointer to an integer stored in the rom space.

Best Wishes
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Fri May 11, 2012 12:32 pm     Reply with quote

I did:

Code:

rom unsigned char matrix[] = {1,2,3,4};
rom int *pointer;

pointer = &matrix;
printf("%d %d", *pointer, *(pointer+1));


and doesnt work with PIC18F4520... Sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 11, 2012 12:53 pm     Reply with quote

Much more important, what compiler version?.
Also try 'dereferencing' the value yourself, so declare an int variable, read the value into this, and then print it. It does work, but has to be used 'with care'.

Best Wishes
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

ROM or CONST Pointer
PostPosted: Sat May 12, 2012 8:20 am     Reply with quote

Is possible use memcpy with a ROM POINTER ?

For example:

rom unsigned char matrix[4] = {1,2,3,4};
rom char *pointer;
char aux[2];

pointer = &matrix;
memcpy( aux, &pointer[ 2 ], 2 );
printf( %02X %02X\n\r", aux[ 0 ], aux[ 1 ] );

I test this and dont work.

I use the last version of compiler.

Best Regards,
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sat May 12, 2012 11:05 am     Reply with quote

I am using the CCS 4.078 and PCD 4.068...
I tryed:

Code:

int a,b,c;

a = *pointer; b=*(pointer+1); c=*(pointer+2);
printf("%d %d %d",a,b,c);


and doesnt work too...
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Sat May 12, 2012 11:26 am     Reply with quote

My problem is that I have a large ROM Array and I need put in RAM for modify some values.

That's all.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat May 12, 2012 11:32 am     Reply with quote

With PIC24, ROM can be mapped to data space by using the PSV (program storage visibility) feature. In CCS PCD, it's supported starting with V4.120.

PCD V4.068 is a very old and very buggy version that can't be suggested for serious programming purposes.
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sat May 12, 2012 11:36 am     Reply with quote

FvM thanks a lot!
I will update my CCS...

After, do you think doing this I will can acess the ROM, and the code will works?
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Sat May 12, 2012 11:40 am     Reply with quote

I have the last version and not work this feature.

And in PIC24 not work this

Code:
char s[5];

memcpy( s, "\x01\x02", 2 );


and in PIC18 yes!!!

Is very crazy......
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun May 13, 2012 3:58 am     Reply with quote

I checked in PCD V4.132 and can't confirm your observations.

All below memcpy actions are working correctly. Not actually expectable, string constants embedded in the code, either with memcpy(), strcpy() or printf() are still using legacy table read instructions. You need to separate the constant string from the copy instruction to take advantage of the more effective PSV methode.
Code:
#device psv=16

char s[5];
rom char c[2] = {"\x01\x02"};
rom char *pr = c;

memcpy( s, "\x01\x02", 2 ); // Is using slow tlbrd instead of PSV!
memcpy( s, c, 2 );
memcpy( s, pr, 2 );


Code that should work with PSV according to the syntax in readme.txt, but actually doesn't.
Code:
#device psv=16
char s[5];
rom char c[2] = {"\x01\x02"};
rom char * rom rpr = c;
memcpy( s, rpr, 2 );
pr = rpr;
memcpy( s, pr, 2 );


Without PSV in effect, memcpy(s,c,2) and memcpy(s,pr,2) are causing address error traps, as expectable.

Apart from the missing support for embedded constant strings, PSV is working as expected. It's usage is strongly suggested for effective utilisation of the PIC 24 processor. It's mostly abandoning the endless confusion with const strings in PCD.

For the present implementation, see below the PSV related documentation in readme.txt. Unfortunately the feature still isn't listed in the online help more than one year after it has been implemented.

Code:
PSV support for 16-bit PICs has been added:
   #device PSV=16
When used, all 'rom' and 'const' keywords are fit into a PSV page of the
program memory, and the contents of the 'rom and 'const' structure/array
are made PSV friendly.  'const' or 'rom' pointers can be assigned to RAM
pointers and still allow access to the data in the program memory.
Example:
   #device PSV=16
   const char constString[] = "Hello";
   const char *constPtr = constString; //constPtr points to program memory in PSV
   char *ramPtr = constString;   //ramPtr points to program memory in PSV
   printf("%s", constPtr);
   printf("%s", ramPtr);


Users of other embedded compilers (e.g. from Microchip) may possibly expect the below construct to work, but it's causing an AE trap with PCD.
Code:
rom char *str = {"Hello new world"};
strcpy(s,str2);


Best regards,
Frank
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sun May 13, 2012 6:57 pm     Reply with quote

I update my CCS and now I can read ROM memory without problems, but now I am having other strange problem:

Code:

   rom int matrix[] = {0x00,0x00,0x60,0x10,0x70,0x90,0x78,0x00};
   rom int *pFonte;
   int i;
   
   pFonte = &matrix;

   for(i=0; i<8; i++){
      printf("\f%d) %d\n%d %ld", i, *(pFonte+i), i, (pFonte+i));
      delay_ms(2000);
   }


And the printf() is printing: 0, 0, 96, 16, 112, -112, 120, 0

The 0x90 = 144 is printing -112, why????

Thanks a lot!
jeremiah



Joined: 20 Jul 2010
Posts: 1346

View user's profile Send private message

PostPosted: Sun May 13, 2012 7:10 pm     Reply with quote

In printf, %d is a signed value. 0x90 is -112 in 2's compliment signed values. Try %u if you want unsigned.
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sun May 13, 2012 7:32 pm     Reply with quote

Thanks jeremiah, it works now!
But if I need to use this value with one variable, I just need to create one unsigned int, and the value will be 144?
Code:

unsigned int tmp;
tmp = *pFonte;

because I test like this, using the printf() with %d to show the tmp variable, and shows -112 again, but using %u shows 144!

But I need use this value with another process... Using unsigned int it will work, right?
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sun May 13, 2012 8:04 pm     Reply with quote

Thanks everybody!
I finally finished the driver for display Nokia 6100...

I will put to download...

Thanks a lot!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon May 14, 2012 1:36 am     Reply with quote

There seems to be a new bug in PCD V4.132, which is apparently confused by the rom attribute of pFonte. It's not actually required with PSV, so simply omit it. The bug is not present in V4.128.

Regards,
Frank

Code:
#include "24FJ32GA002.h"
#device PSV=16
#use delay(clock=8000000)
#pin_select U1RX=PIN_B4
#pin_select U1TX=PIN_B5
#use RS232(baud=9600, UART1)

   rom int matrix[] = {0x00,0x1234,0x60,0x10,0x70,0x90,0x78,0x00};
   rom int *pFonte;
   int *pFonte2;
   int i;
   int x;
   
void main(void)
{
   printf("\r\nPCD Bug with printf() and rom pointer\r\n");
   printf("PCD Version %5.3f\r\n",getenv("VERSION")+0.0001);
 
   pFonte = &matrix;
   pFonte2 = &matrix;
   
   for(i=0; i<8; i++){
      printf("%d ",*(pFonte+i));
      printf("%d ",*(pFonte2+i));
      x=*(pFonte+i);
      printf(" %d\r\n",x);
   }   
   while(1);
}


Result:
    PCD Bug with printf() and rom pointer
    PCD Version 4.132
    0 0 0
    -10281 4660 4660
    -10281 96 96
    -10281 16 16
    -10281 112 112
    -10281 144 144
    -10281 120 120
    -10281 0 0
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group