View previous topic :: View next topic |
Author |
Message |
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
Is this PIC C-18 Compiler Structure Possible in CCS...? |
Posted: Sat Sep 15, 2007 8:42 pm |
|
|
I am converting the J1939 PIC-18 Complier code. There is a (Structure inside of a Union) in PIC-C;
Code: | union J1939_MESSAGE_UNION {
struct {
unsigned int PDUFormat_Top : 3; // This needs pre and post processing.
unsigned int DataPage : 1;
unsigned int Res : 1;
unsigned int Priority : 3;
unsigned char PDUFormat; // CA should use only PDUFormat.
unsigned char PDUSpecific;
unsigned char SourceAddress;
unsigned int DataLength : 4;
unsigned int RTR : 4; // RTR bit, value always 0x00
unsigned char Data[J1939_DATA_LENGTH];
};
unsigned char Array[J1939_MSG_LENGTH + J1939_DATA_LENGTH];
};
#define GroupExtension PDUSpecific
#define DestinationAddress PDUSpecific
typedef union J1939_MESSAGE_UNION J1939_MESSAGE; |
That I can't get to work in CCS. Does anybody know how to code this function..??? _________________ Thank you,
Chris |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 15, 2007 10:04 pm |
|
|
Quote: | I can't get to work in CCS |
Give specific details.
1. Post your compiler version.
2. Post your PIC.
3 Post any compiler error messages.
4 Post a small test program, that shows improper operation.
5. Also, provide #define statements for these two constants:
Quote: | J1939_DATA_LENGTH
J1939_MSG_LENGTH |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Sep 16, 2007 8:06 am |
|
|
ChrisL,
The CCS compiler does not support unnamed types in a union, this means you will have to add a name to the first struct in the enum.
It seems like the High-Tech compiler is suffering from the same limitation as I found a ported version which has the required changes: http://www.htsoft.com/forum/all/showflat.php/Cat/0/Number/24552/Main/20677
Code: | #define J1939_MSG_LENGTH 5
#define J1939_DATA_LENGTH 8
union J1939_MESSAGE_UNION {
struct {
unsigned int PDUFormat_Top : 3; // This needs pre and post processing.
unsigned int DataPage : 1;
unsigned int Res : 1;
unsigned int Priority : 3;
unsigned char PDUFormat; // CA should use only PDUFormat.
unsigned char PDUSpecific;
unsigned char SourceAddress;
unsigned int DataLength : 4;
unsigned int RTR : 4; // RTR bit, value always 0x00
unsigned char Data[J1939_DATA_LENGTH];
} Msg; <<----------------- Add this for CCS as well.
unsigned char Array[J1939_MSG_LENGTH + J1939_DATA_LENGTH];
}; |
A side note:
Unnamed union members are a part of the ANSI/ISO 9899-1999 standard. CCS provides a comparison to the older 1990 version of the standard in http://www.ccsinfo.com/downloads/ansi_compliance.pdf. Too bad I can not find a copy of the 1990 standard on the internet so I can not check the CCS compliance statements. |
|
|
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
|
Posted: Sun Sep 16, 2007 8:14 pm |
|
|
FINALLY, I figured out how to do this in CCS... Here is how it works...
Code: | In CCS here is a Structure inside of a Union...
/****************************Messaging*******************************************/
struct J1939_MESSAGE_STRUCT
{
unsigned int PDUFormat_Top:3; // This needs pre and post processing.
unsigned int DataPage:1;
unsigned int Res:1;
unsigned int Priority:3;
unsigned char PDUFormat; // CA should use only PDUFormat.
unsigned char PDUSpecific;
unsigned char SourceAddress;
unsigned int DataLength: 4;
unsigned int RTR: 4; // RTR bit, value always 0x00
unsigned char Data[J1939_DATA_LENGTH];
};
union J1939_MESSAGE_UNION
{
struct J1939_MESSAGE_STRUCT Msg;
unsigned char Array[J1939_MSG_LENGTH + J1939_DATA_LENGTH];
};
typedef union J1939_MESSAGE_UNION J1939_MESSAGE;
/*******************************************************************************/
It's aliased like this..
J1939_MESSAGE OneMessage;
It's addressed like this; OneMessage.Msg.SourceAddress
With a pointer like this; MsgPtr->Msg.PDUFormat_Top
|
After 5, 20 hour days working on the conversion I have functional J1939 code converted from the Library that Microchip has on their web site.
Sometimes you just need to stop and ask the question to get back on the right track...
ckielstra - I like your solution better. I will give it a try to make my code simpler...
Thanks to all that responded... _________________ Thank you,
Chris |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 25, 2007 7:58 am |
|
|
I asked CCS to add support for unnamed members for struct and unions. This is part of the ANSI/ISO 9899-1999 specification (and maybe in the 1990 version also).
Sept 24 I received an email saying this is implemented in the next release (4.058).
The new feature allows you to write code like: Code: | union foo
{
struct
{
int a;
int b;
}; <-- note: no name here, makes it an anonymous structure.
char c;
} bar;
char c;
....
bar.a = c; /* 'a' is a member of the anonymous structure
located inside 'bar' */ |
A nice example for this new feature is when converting bytes to words and vice versa.
Old situation Code: | union HL
{
int16 L;
struct
{
int8 low;
int8 high;
} hl;
};
usage:
union HL var1;
var1.L = 50000; // as an INT16
var1.hl.high = 195; // access the high byte
var1.hl.low = 80; // access the low byte
|
New situation Code: | union HL
{
int16 L;
struct
{
int8 low;
int8 high;
}; // no name
};
usage:
union HL var1;
var1.L = 50000;
var1.high = 195; // Note the easier notation
var1.low = 80;
|
|
|
|
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
|
Posted: Tue Sep 25, 2007 8:35 am |
|
|
ckielstra,
That's great...! _________________ Thank you,
Chris |
|
|
|