|
|
View previous topic :: View next topic |
Author |
Message |
JacquesB
Joined: 22 Aug 2008 Posts: 21
|
Problem with #locate and sizeof() |
Posted: Wed Feb 18, 2015 10:51 am |
|
|
Hi,
I'm having difficulties with the use of #locate and sizeof() to get the dimension of a structure
I have a receive buffer that share the same space as 2 structures, one for the header part and the 2nd for the payload. The 1st structure can be located with
Code: | #locate Rx_Hdr= RxBuff |
but I can't use
Code: | #locate Payload= RxBuff+ sizeof(Rx_Hdr) |
to place the payload after the header structure using sizeof()
error messages say: Expression must be a constant or simple variable
but it works with
Code: | #locate Payload= RxBuff+ 3 |
Why can't I use sizeof() with preprocessor #locate?
What am I missing?
Or is there a better way to do it?
Thanks
>>>>>>> SAMPLE code
Code: | #include "18F26K80.h"
...
// structure definition for the header
typedef struct _RESP_HDR{
unsigned int SUCC_FAIL;
unsigned int LOGIC_ID;
unsigned int DATA_LEN;
} RESP_HDR;
// structure definition for the payload
typedef struct _PAYL_RX{
int32 VAR1;
int32 VAR2;
int32 VAR3;
}PAYL_DATA;
char RxBuff[AHRS_RXBUFFLEN]; //!<
/// receive header(Rx_Hdr) share same address space as the receive buffer RxBuff[]
RESP_HDR Rx_Hdr; // ram structure holding the message header
#locate Rx_Hdr= RxBuff
/// receive Payload(Payload) share same address space as the receive buffer RxBuff[]
PAYL_DATA Payload; // ram structure holding the payload information
#locate Payload= RxBuff+ sizeof(Rx_Hdr) // LINE A: Problem with this line |
if LINE A: #locate Payload= RxBuff+ sizeof(Rx_Hdr)
results:
*** Error 51 "file.h" Line 338(31,37): A numeric expression must appear here
*** Error 48 "file.h" Line 338(38,44): Expecting a (
if LINE A: #locate Payload= (RxBuff+ (sizeof(Rx_Hdr)))
results:
*** Error 91 "file.h" Line 338(18,19): Expression must be a constant or simple variable
*** Error 43 "file.h" Line 338(30,31): Expecting a declaration
*** Error 43 "file.h" Line 338(32,33): Expecting a declaration
*** Error 43 "file.h" Line 338(33,39): Expecting a declaration
*** Error 43 "file.h" Line 338(39,40): Expecting a declaration
*** Error 48 "file.h" Line 338(40,46): Expecting a (
*** Error 43 "file.h" Line 338(47,48): Expecting a declaration
*** Error 43 "file.h" Line 338(48,49): Expecting a declaration
if line A:#locate Payload= AHRS_RxBuff+ sizeof(RESP_HDR)
results
*** Error 51 "file.h" Line 338(31,37): A numeric expression must appear here
if line A:#locate Payload= AHRS_RxBuff+ sizeof(_RESP_HDR)
results
*** Error 51 "file.h" Line 340(31,37): A numeric expression must appear here
*** Error 48 "file.h" Line 340(38,47): Expecting a ( |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Feb 18, 2015 11:47 am |
|
|
The better way, is a union. This is what this is for.... |
|
|
JacquesB
Joined: 22 Aug 2008 Posts: 21
|
|
Posted: Wed Feb 18, 2015 12:31 pm |
|
|
Yes but with a union all members start at the same memory location.
I want the payload structure to be at the end of the Rx_Hdr structure.
right now if I use #locate Payload= RxBuff+ 3, the program compile and I can access the different elements:
ex:
Code: |
RxBuff[ RxBuffidx]= c; // fill the rx buffer
x= Rx_Hdr.LOGIC_ID; // get the logic ID of the header
y= Payload.VAR1; // get the payload variable VAR1
|
my main concern is about the difference between:
Code: |
#locate Payload= RxBuff+ 3
|
and
Code: |
#locate Payload= RxBuff+ sizeof(Rx_Hdr)
|
I may redefine the header structure and I want the payload to be located properly
Unless there's a way with unions that I don't see... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Thu Feb 19, 2015 11:18 am |
|
|
JacquesB wrote: |
my main concern is about the difference between:
Code: |
#locate Payload= RxBuff+ 3
|
and
Code: |
#locate Payload= RxBuff+ sizeof(Rx_Hdr)
|
|
The main difference is that sizeof() is not a preprocessor environment component, so the preprocessor cannot parse what the value is. You can use it as a replacement in a macro since the preprocessor doesn't have to know the value of sizeof for replacement. However, if you try to force the preprocessor to evaluate the value of sizeof()...say in an #if or #locate...then it can balk. Remember that the Preprocessor has no concept of types (int, char, Rx_Hdr) nor how structs and unions are packed, so it cannot figure out the size of the type you put in. The preprocessor is very limited.
That's not to say that theoretically it cannot be parsed at the preprocessor (I am sure there are some compiler vendors who have), but it would involve bringing a lot of the compiler environment into the preprocessor, which is overkill. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Feb 19, 2015 12:02 pm |
|
|
and, as a comment, on locating things later in the union, just use a dummy.
The key point is that you are offsetting by the size of a variable. If you create a union, using a structure which contains a declared dummy variable of the same type, and then the area you want to be shared, the offset is automatic, and will adjust for the variable size....
The compiler doesn't actually have a 'preprocessor' as such.
What it does is an initial pass in which it does things as if they were pragmas. These include #build, #org, #locate. After this is allocates the variable sizes and locations. Then it starts on the actual code. Just the way that CCS works |
|
|
JacquesB
Joined: 22 Aug 2008 Posts: 21
|
|
Posted: Thu Feb 19, 2015 12:42 pm |
|
|
Thanks Jeremiah for your good explanations. I got it.
Thanks also to Ttelmah, I'll take your suggestion |
|
|
|
|
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
|