type casting in CW12 V3.1

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

type casting in CW12 V3.1

2,423 Views
markiscarabas
Contributor I
Hi,
 I have the question on typecasting mechanism :
 
 
The simple code is below (small memory model):
 
typedef struct
{
 char low;
 char high;
}SPLIT_T;
 
 
const unsigned char test_ch = (unsigned char )&handler; // Does not work ...storage dest is too small. I understand and would expect that test_ch would contain either msb or lsb of the handler's address!?
 
const SPLIT_T test_split = (SPLIT_T )&handler; // Compiler complaines - illegal typecast ????
 
void handler(void);
 
 
void main(void)
{
...
}
 
void handler(void)
{
...
}
 
Any ideas...
 
Regards, Mark
Labels (1)
Tags (1)
0 Kudos
2 Replies

478 Views
marc_paquette
Contributor V
Pointer types may only be converted to other pointer types (and to the integer type). In your case, try this:
 
htype* handler;
 
typedef struct
{
   char low;
   char high;
} SPLIT_T;
 
const SPLIT_T* test_ptr = (SPLIT_T*)&handler;
 
// test_ptr->low points to lower handler's address and test_ptr->high points to higher part of address, assuming processor is running in low-endian mode.
 
Marc.

478 Views
Lundin
Senior Contributor IV
The name "handler" is to be regarded as a function pointer. So there is no need to take the address of it. This should work:


typedef struct
{
char low;
char high;
}SPLIT_T;

void handler (void);

const SPLIT_T test_split = *(SPLIT_T*)handler;

And as mentioned, which part of the address that ends up where depends on little/big endian.

Since you seem to be using HC12 with big endian, you should swap the order of low/high.

On HC12, you also need to be careful with where you place "handler" in the memory. If you place it in banked flash, it would suddenly become a 3-byte "far" pointer where the ms byte is the flash page.