Force a pointer to be used to target flash in HiTech

HiTech had a great compiler for PSoC's M8C processor, but, then they were bought by Microchip. Subsequently the HiTech forums were taken off line. At the request of PSoCDeveloper members PSoCDeveloper has created this forum to allow HiTech users to support each other.

Moderator: ericb

Force a pointer to be used to target flash in HiTech

Postby baxsie » Tue Jun 09, 2009 12:23 pm

HiTech is very clever, and also sometimes obtuse.

I wanted to create a pointer to pass to a "memdifF" routine I had created. In ImageCraft, the "const" qualifier makes the pointer point to FLASH. So for ImageCraft, I had this routine:
Code: Select all
unsigned char memdifF(unsigned char *a,const unsigned char *b,unsigned char count)
  {
  while(count--)
    if((*a++)!=(*b++))
      return(1);
  return(0);
  }

Where *a (not labeled const) was a RAM pointer, and *b (const) was a FLASH pointer under ImageCraft.

The "const" qualifier in HiTech is not used as an indicator to point to FLASH. Instead the compiler sees if the pointer passed to memdifF has ever been assigned to a FLASH region, and if so compiles the FLASH version of memdifF. If the pointer was assigned to RAM then it compiles a RAM version of memdifF. If both kinds of calls are made, it passes in a "this is RAM" flag in the high bit of the high byte of the pointer, and conditionally accesses RAM or FLASH. All very clever, of course.

The problem was when I wanted to compare an area of flash to the ram data that the user was about to store there. If the data was the same, I did not want to write to the flash block. So I had a call that worked fine in ImageCraft:
Code: Select all
  if(memdifF(input,(const unsigned char *)(block*64),count))

The ImageCraft compiler happily worked. Not so lucky with this syntax under HiTech. The HiTech compiler has no pointer type information in "(const unsigned char *)(block*64)". The "const" just means "not to be changed" under HiTech, so HiTech has no idea that I want to point to FLASH. There is no "flash" keyword to force the pointer type.

OK, enough into. After a bunch of back-and forth (and I do mean a bunch) with HiTech's tech support I now have a way to make what I want to happen happen.

Include this information in a header file:
Code: Select all
//============================================================================
//Macro to force the compiler to treat a pointer as flash.
//This variable is not used, it just has to be somewhere
//that the compiler thinks is flash. The compiler seems
//to be happy with it pointing off into space.
extern const unsigned char
  make_me_flash @ 0x8000;
#define flash(x) ((&make_me_flash - &make_me_flash) + (x))
//Usage:
// (memdifF(input,flash(block*64),count))
//block*64 will be treated as a 16-bit flash pointer
//============================================================================

Then you call memdifF like this:
Code: Select all
  if(memdifF(input,flash(block*64),count))

Not as good as a "flash" keyword, IMO, but plenty good for those of us still on this sinking ship of a compiler :roll: Hopefully this may save you some trouble.
I'm not getting older. I'm getting bitter.
baxsie
The Big Cheese
The Big Cheese
 
Posts: 420
Joined: Mon Dec 22, 2003 10:08 pm

Re: Force a pointer to be used to target flash in HiTech

Postby rqn » Tue Jun 09, 2009 12:51 pm

Or rather more simply you could write:
Code: Select all
extern const unsigned char  flash[_FLASH_SIZE/_FLASH_BLOCKSIZE][_FLASH_BLOCKSIZE] @ 0x0;

and use array syntax instead of your macro, e.g.
Code: Select all
      if(memdifF(input, flash[block], count))

The _FLASH_xxx macros are predefined by the compiler for the specific chip being used.
rqn
Newbie
Newbie
 
Posts: 6
Joined: Wed Mar 18, 2009 3:02 pm

Re: Force a pointer to be used to target flash in HiTech

Postby baxsie » Tue Jun 09, 2009 1:06 pm

LOL, sounds like I should have asked here instead of telling.

I wonder why HiTech support did not bring up the flash macros. Sigh.
I'm not getting older. I'm getting bitter.
baxsie
The Big Cheese
The Big Cheese
 
Posts: 420
Joined: Mon Dec 22, 2003 10:08 pm

Re: Force a pointer to be used to target flash in HiTech

Postby baxsie » Tue Jun 09, 2009 1:54 pm

FYI: The array method compiles 6 bytes larger.
I'm not getting older. I'm getting bitter.
baxsie
The Big Cheese
The Big Cheese
 
Posts: 420
Joined: Mon Dec 22, 2003 10:08 pm


Return to “%s” HiTech C User-to-User Support

Who is online

Users browsing this forum: No registered users and 1 guest