pointers use in Composer studio

Technical questions regarding the XTC tools and programming with XMOS.
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

pointers use in Composer studio

Post by genap »

I have a problem which seems to be related to pointers use.
I thoughts that pointers are allowed in Composer studio v13.
BTW, are there any documents on a difference in programming in versions 12 and 13,
and on programming in xc for v13 ?

Anyway, I have a piece of code

Code: Select all

  unsigned int size, i;
  unsigned char *start_ptr, *end_ptr;
  unsigned char const *dptr;
  unsigned char rbuf[4]; // buffer to read data from flash, [3] for debug

1   Read_mem(0x10000, 2, rbuf);       // read version to rbuf
2   Read_mem(0x10003, 1, &rbuf[2]);   // read verification byte to rbuf

3   if ((rbuf[2] == 0x55) && (rbuf[0] == dprof.version_major))    // valid verification
4   {
5     Read_mem(0x10000, size, &rprof.version_major);  // read flash profile to ram
6     rbuf[3] = 1;
7   }
8   else  // invalid, load default to ram and store to flash
9   {
10      unsigned char *start_ptr = &rprof.version_major;
11      start_ptr = (unsigned char *)&rprof.version_major;
12      memcpy(start_ptr,dptr,size);  // copy default profile to ram profile
13      /* erase sector 1 and write ram array to it (2 pages are written) */
14      Erase_mem_sector(1);
15      Write_mem(0x10000,256,&rprof.version_major);
16      Write_mem(0x10100,size-256,&rprof.version_major + 256);
17      rbuf[3] = 0;
18  }
where I extensively use passing pointers to a structure to various functions.
It compiles fine, but on debugging I constantly get an exception
- Suspended: Signal 'ET_ECALL' received. Description: Application exception.

And seems like it's related to pointers use.
When I comment out some the functions, i get this exception in a different place.

So is this exception related to the pointers use ?
And if yes, what are the rules for pointers use in XC v13 ?
And how can I pass a structure to a function without using pointers ?

Thank you,
Gennady


User avatar
davelacey
Experienced Member
Posts: 104
Joined: Fri Dec 11, 2009 8:29 pm

Post by davelacey »

Pointers have always been available in pure C code but from version 13 you have safe pointers in xC. The details are in section 7 of this doc:

https://www.xmos.com/download/public/XM ... o-C(D).pdf

When compiling xC the compiler puts runtime checks that could trap for either:

1) out of bounds pointer access
2) null pointer access.
3) passing of two pointers to a function that alias each other without adding the alias keyword to the function arguments

Unfortunately the debugger does not currently tell you which of these problems is occurring (this is something we are working on for a future release to help exactly your situation).

Is the code you posted the whole code segment? If so, then I would expect a trap on the following line:

Code: Select all

  memcpy(start_ptr,dptr,size);  // copy default profile to ram profile
Since dptr is uninitialized. If this is not the problem, does the debugger tell you which line of source code is causing the trap?

Dave
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

Post by genap »

Dave, thank you for your reply.
Unfortunately I can't access the file you referred to - it tells me that access is restricted :-(

Sorry, I din't copy the correct version of a code - this is the corrected one:

Code: Select all

P306_profile rprof;
 
     void Configure_profile(void)
     {
       unsigned int size;
       unsigned char *start_ptr, *end_ptr;
       const unsigned char *dptr;
       unsigned char rbuf[4]; // buffer to read data from flash, [3] for debug

       /* get profile size */
1     start_ptr = (unsigned char *)&rprof.version_major;
2     end_ptr = (unsigned char *)&rprof.checksum;
3     size = end_ptr - start_ptr + 1;

       /* read version - rbuf[0..2] and verification byte - rbuf[3] - 0x55 from the flash profile */
       /* offset of version 1st sector size (64K) - 0x10000 */
       /* offset of verification byte is (size - 2) + 1st sector size (64K) */
4     Read_mem(0x10000, 2, rbuf);       // read version to rbuf
5     Read_mem(0x10003, 1, &rbuf[2]);   // read verification byte to rbuf

6     if ((rbuf[2] == 0x55) && (rbuf[0] == dprof.version_major))    // valid verification
7     {
8       rbuf[3] = 1;
9       Read_mem(0x10000, size, &rprof.version_major);  // read flash profile to ram
10   }
11   else  // invalid, load default to ram and store to flash
12   {
13     rbuf[3] = 0;
14     dptr = (unsigned char const *)&dprof.version_major;
15     memcpy(start_ptr,dptr,size);  // copy default profile to ram profile
16     /* erase sector 1 and write ram array to it (2 pages are written) */
17     Erase_mem_sector(1);
18     Write_mem(0x10000,256,&rprof.version_major);
19     Write_mem(0x10100,size-256,&rprof.version_major + 256);
20   }
21   Print_buffer(rbuf,4);
22 }  
In this form an exception points to the line 13.
When I comment out line 15 (memcpy), exception shows at the line inside the Write_mem()
function, where pointer is used.
When commenting out either or both Write_mem() calls (L18,19), an exception shows again at line 13.
And only commenting out lines 15,18,19 removes exception.

I tried to use the initialized *start_ptr for passing:

Code: Select all

P306_profile rprof;
 
  void Configure_profile(void)
  {
      unsigned int size;
      unsigned char *start_ptr, *end_ptr;
      const unsigned char *dptr;
      unsigned char rbuf[4]; // buffer to read data from flash, [3] for debug

       /* get profile size */
1     start_ptr = (unsigned char *)&rprof.version_major;
2     end_ptr = (unsigned char *)&rprof.checksum;
3     size = end_ptr - start_ptr + 1;

       /* read version - rbuf[0..2] and verification byte - rbuf[3] - 0x55 from the flash profile */
       /* offset of version 1st sector size (64K) - 0x10000 */
       /* offset of verification byte is (size - 2) + 1st sector size (64K) */
4     Read_mem(0x10000, 2, rbuf);       // read version to rbuf
5     Read_mem(0x10003, 1, &rbuf[2]);   // read verification byte to rbuf

6     if ((rbuf[2] == 0x55) && (rbuf[0] == dprof.version_major))    // valid verification
7     {
8        rbuf[3] = 1;
9        Read_mem(0x10000, size, start_ptr);  // read flash profile to ram
10    }
11    else  // invalid, load default to ram and store to flash
12    {
13      rbuf[3] = 0;
14      dptr = (unsigned char const *)&dprof.version_major;
15      memcpy(start_ptr,dptr,size);  // copy default profile to ram profile
16      /* erase sector 1 and write ram array to it (2 pages are written) */
17      Erase_mem_sector(1);
18      Write_mem(0x10000,256,start_ptr);
19      Write_mem(0x10100,size-256,start_ptr + 256);
20    }
21    Print_buffer(rbuf,4);
22  }  
but now I get an error
error: passing non-local alias to function `Read_mem' which accesses a global variable

Very confusing.

Thank you for help,
Gennady
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

Post by genap »

I can't see the difference between

Code: Select all

Read_mem(0x10000, size, &prof.version_major);
and

Code: Select all

unsigned char *start_ptr;

start_ptr = (unsigned char *)&rprof.version_major;
Read_mem(0x10000, size, start_ptr);
User avatar
XenoPhoenix
Member
Posts: 13
Joined: Tue Mar 29, 2011 5:13 pm

Post by XenoPhoenix »

genap wrote: Dave, thank you for your reply.
Unfortunately I can't access the file you referred to - it tells me that access is restricted :-(
The link highlighting for that link appears to be incorrect try this one:

https://www.xmos.com/download/public/XM ... o-C(D).pdf
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

Post by genap »

thank you,
I've found it in this place.
User avatar
davelacey
Experienced Member
Posts: 104
Joined: Fri Dec 11, 2009 8:29 pm

Post by davelacey »

I think I know why you get the exception and have put a post on the Q&A in case others run into this:

http://www.xcore.com/questions/2388/why ... -struct-xc
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

Post by genap »

Thank you,
I'll try it.
genap
Experienced Member
Posts: 99
Joined: Sat Aug 31, 2013 11:23 pm

Post by genap »

Works great.
Thank you.

Used it also in other functions passing structures as arguments.