1

I'm trying to compute a CRC32 for the application code on an 8052 embedded platform.

The following is given as a guide to calculating the CRC :

void TestChecksum()
{
     unsigned short calc = 0;

     /* Run the checksum algorithm */
     calc = slow_crc16(0, (unsigned char *) ChecksumStart, (ChecksumEnd - ChecksumStart+1));

     /* Rotate out the answer */
     unsigned char zeros[2] = {0, 0};
     calc = slow_crc16(calc, zeros, 2);

     etc...
}

where

unsigned short slow_crc16(unsinged short sum, unsigned char *p, unsigned int len)
{
     while (len--)
     {
         int i;
         unsigned char byte = *(p++);

         for (i = 0; i < 8; ++i)
         {
          etc...
         }
}

The problem with the example is that the pointer automatically points to XData whereas I'm after CODE. I know the address of the code from which I would like to calculate the CRC, say 0x2000 to 0x2FFF.

Is it possible to define a pointer as, say:

uint8_t __code *Ptr;

And pass in an address, the 0x2000, as a uint32 to the CRC function ?

Basically I can't work out how to get a pointer to point to an address which is actually stored as a variable.

Any help would be appreciated, I'm not sure this is even possible.

David
  • 125
  • 1
  • 2
  • 7

1 Answers1

0

A pointer is an integer; it just has a special type to stop it getting mixed up with other pointers (or numbers).

On many architectures you can convert pointers to numbers and back again without loosing anything:

char *ptr = (char *)0x2000;
int address = (int)ptr;

[This doesn't work on architectures where the pointer's "integer" is a different size to an ordinary int "integer". There are a few other exotic cases you almost certainly don't care about also.]

In fact, the only way to get a pointer to do anything other than "be an integer" is to dereference it:

char c = *ptr;

or, the exact same thing using array notation:

char c = ptr[0];

or, only if ptr is a pointer to a struct type:

int v = ptr->value;

In all those cases, it uses the pointer as address, and fetches the referenced data from memory.

Finally, you can get a pointer to a variable using the "address-of" operator (a.k.a "reference" operator):

char c = 'Z';
char *ptr = &c;
ams
  • 24,923
  • 4
  • 54
  • 75