Codewarrior Kinetis Compiler

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

Codewarrior Kinetis Compiler

3,581 Views
lfresca
Contributor I

Hi,

 

I'm doing some development with the CodeWarrior 10.1 and the TWR-K60N512 development kit.

I'm having some trouble figuring out how to byte align some structures in my code.

Since the compiler is gcc based I was wondering if it supports the #pragma pack() directive?

I couldn't find any information in the compiler documentation, and tried it in my code, but it returns a warning stating that it's an illegal pragma.

Are there any language extensions that need to be turned on for the pragma to work, or is it simply not supported by the compiler.

 

 

Thanks in advance,

 

Best Regards

 

Luis Fresca

0 Kudos
15 Replies

1,372 Views
LuisCasado
NXP Employee
NXP Employee

Hello Luis,

 

You should use __atrribute__(align) as a workaround instead. This attribute is not properly documented in the Kinetis Compiler manual (MTWX47648).

 

typedef struct

{

unsigned long x __attribute__((aligned (1)));

unsigned char y __attribute__((aligned (1)));

} new_struct;

Best Regards,

0 Kudos

1,372 Views
lfresca
Contributor I

Hi,

 

Thanks for the reply.

I've tried that attribute, but it does not do what I want.

I want to force a struct to be byte aligned like #pragma pack(1) would do.

All __attribute__ (aligned(1)) does is tell the compiler to use at least one byte alignment in the structure.

Since the compiler is aligning the data to a 4 byte boundary it is compiling with the statement.

 

Here is the definition from gcc.org: http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Type-Attributes.html

 

Any compiler options to force struct alignment, like in Coldfire?

 

Thanks in advance,

 

Best Regards,

 

Luis Fresca

0 Kudos

1,372 Views
gustavod
Contributor III

Hi Luis,

 

We used pragma pack in Codewarrior 10.1 for Kinetis. We needed this to open bmp and wave files.

 

Ex:

#pragma pack (push,2)                                    // Align structure to 2 byte

typedef struct {   

      unsigned long ChunkID;                            //4 bytes = "RIFF" 0x52494646 big-endian form  

      unsigned long ChunkSize;                        //4 bytes

      unsigned long Format;                               //4 bytes = "WAVE" 0x57415645 big-endian

      unsigned long Subchunk1ID;                   //4 bytes = "fmt"  0x666d7420 big-endian form

      unsigned long Subchunk1Size;               //4 bytes  

      unsigned short AudioFormat;                   //2 bytes: 0 =???;  1 = Linear quantization; Others = compress

      unsignedshort NumChannels;                //2 bytes: Mono = 1; Stereo = 2; etc.  

      unsigned long SampleRate;                    //4 bytes: 8000, 44100, etc.

      unsigned long ByteRate;                           //4 bytes: SampleRate * NumChannels * BitsPerSample/8  

      unsigned short BlockAlign;                       //2 bytes: NumChannels * BitsPerSample/8  

      unsigned shor tBitsPerSamples;            //2 bytes: 8 = 8 bits; 16 = 16 bits; etc.  

      unsigned long Subchunk2ID;                   //4 bytes = "data" 0x64617461 big-endian form  

      unsigned long Subchunk2Size;               //4 bytes: NumSamples * NumChannels * BitsPerSample/8//

      unsigned long Data;                                  //(ChunkSize) bytes 

} WAVE_FILE;

#pragma pack (pop)

 

If this works for you mark "Accept as Solution".

 

Best regards,

Gustavo

0 Kudos

1,372 Views
lfresca
Contributor I

Hi,

 

Thank you for replying.

Like I said in the previous post, I've tried using #pragma pack() in my code, but the compiler keeps returning a illegal #pragma warning.

I'm also using Codewarrior 10.1, but it just doesn't seem to work in mine! 

Did you have to use any compiler options to be able to use this #pragma?????

 

Thank you again,

 

Best Regards,

 

Luis Fresca

0 Kudos

1,372 Views
lfresca
Contributor I

Update:

 

I've been looking at the Kinetis Documentation more closely and found a reference to the #pragma pack directive in the Kinetis Build Tool Reference Manual, in the Pragmas for Optimization section, page 316.

 

Still can't get it to work in my Codewarrior 10.1 project.

 

There is no information on the aforementioned document about any precondition needed to use this pragma.

 

Best Regards

 

Luis Fresca

0 Kudos

1,372 Views
gustavod
Contributor III

Luis,

 

You can download my project. I used two pragma packs (in files SD_API.h and SD_API.c). This code is compiling in CodeWarrior 10.1.

 

http://brtos.googlecode.com/files/BRTOS%201.6x%20K40%20-%20SD%20%2B%20Wave.rar

 

Regards,

Gustavo

0 Kudos

1,372 Views
gustavod
Contributor III

Use the FLASH version. The code is not compiling for RAM.

0 Kudos

1,372 Views
lfresca
Contributor I

Dear Gustavo, thanks for your help...

 

Please try this in your project to see if you have the same results...

 

Turn on the warnings for "Illegal Pragmas" & "Pad Bytes Added".

 

Then re-compile your project.

 

You should see a warning regarding your #pragma pack() statements saying "illegal #pragma".

 

Also, from the results I see on my side, your WAVE_FILE struct is not being aligned to 2 bytes, but to 4 bytes, like in my project.

 

Try adding a unsigned char var to the end of your struct and rebuild your project.

If the compiler where aligning the struct to a 2 byte boundary you should get one pad byte added to the struct, but you should see that the compiler is adding 3 padding bytes, which means it's aligning your struct to a 4 byte boundary (which is the compiler default).

 

Once again. Thank you very much for your help.

 

Obrigado,

 

Luis Fresca

 

 

0 Kudos

1,372 Views
gustavod
Contributor III

Forgive me Luis.

 

This is the right manner to do it:

 

typedef struct __attribute__((packed, aligned(2)))

 

Regards,

Gustavo

0 Kudos

1,372 Views
LuisCasado
NXP Employee
NXP Employee

Hello,

 

#pragma pack is now supported by Kinetis compiler.

 

 

Best Regards,

 

Luis

0 Kudos

1,372 Views
lfresca
Contributor I

Hi Gustavo,

 

Just tried it in your code, and It's still aligning on a 4 byte boundary.

As you can see bellow I added a unsigned char variable in order to make struct out of alignment,

I used the attribute you talked about and still got 3 bytes padding added to the structure, which means that regardless of the attribute declaration it is still aligning to a 4 byte boundary.

 

typedef struct  __attribute__ ((packed,aligned(2)))

{   

unsigned long ChunkID;//4 bytes = "RIFF" 0x52494646 big-endian form  

unsigned long ChunkSize;//4 bytes  

unsigned long Format; //4 bytes = "WAVE" 0x57415645 big-endian form  

unsigned long Subchunk1ID; //4 bytes = "fmt"  0x666d7420 big-endian form  

unsigned long Subchunk1Size; //4 bytes  

unsigned short AudioFormat;//2 bytes: 0 =???;  1 = Linear quantization; Others = compression  

unsigned short NumChannels;//2 bytes: Mono = 1; Stereo = 2; etc.  

unsigned long SampleRate;//4 bytes: 8000, 44100, etc.  

unsigned long ByteRate;//4 bytes: SampleRate * NumChannels * BitsPerSample/8  

unsigned short BlockAlign;//2 bytes: NumChannels * BitsPerSample/8  

unsigned short BitsPerSamples;//2 bytes: 8 = 8 bits; 16 = 16 bits; etc.  

unsigned long Subchunk2ID;//4 bytes = "data" 0x64617461 big-endian form  

unsigned long Subchunk2Size;//4 bytes: NumSamples * NumChannels * BitsPerSample/8  

unsigned char x;  

// unsigned long Data;//(ChunkSize) bytes 

} WAVE_FILE;

 

If the compiler were aligning the data to a 2 byte boundary, you should only get one padding byte.

 

Stil, thanks for all the time you're wasting on my problem!!

 

Best Regards,

 

 

0 Kudos

1,372 Views
jimtrudeau
Senior Contributor I

Luis,

 

Did you ever get this working? There was some good help and advice here, but it didn't seem to work for you. I can't provide anything further, but the fact that I didn't see a positive resolution concerns me. There is a possibility there's a defect in the compiler. It could be user error, a wrong setting - since it appears to work for others. But...

 

If this is not working for you, and it involves the CodeWarrior compiler, would you please open up a formal serivce request? At the moment there is a rather long queue, the support team has been working an internal global training event. But, they will get to it. http://www.freescale.com/support. If you detail what you're doing and provide a project and code example, an apps engineer will get to it and perhaps be able to resolve the problem. If it turns out there is a defect, we definitely want to know about it.

 

Jim

0 Kudos

1,372 Views
lfresca
Contributor I

Hi Jim,

 

Short answer... No, I haven't been able to get this to work.

I've been exchanging emails with my Freescale supplier, and he forwarded the issue to Freescale's support team, so I'm guessing a service request is already open!

But I don't think it's a project settings issue, since I downloaded Gustavo's project, and to my knowledge project settings were imported along with the source, and still I could not get the same results he did.

There is the question of the compiler version. I'm using Codewarrior 10.1 on Windows XP, i'm not sure which one Gustavo has.

 

I'm going to wait a bit more time for some response from my supplier.

 

If I can't get the issue resolved, I'll post another message on this thread to keep you updated.

 

Thanks for interest.

 

Best Regards,

 

Luis Fresca 

 

P.S. - Once again, thanks for you help Gustavo.

0 Kudos

1,372 Views
mra59
Contributor II

Hi Luis

I'm facing the same alignement problem using CW 10.2 trying to have a structure 1 byte aligned:

have you got any new from Freescale about your issue?

 

Is there any option to be deactivate in compiler's options?

 

Mauro Righetto

 

 

0 Kudos

1,372 Views
gustavod
Contributor III
This is a little bit strange, cause we use this attribute in other projects. You can download another project of us in the brtos google code. We have projects with bitmap files that uses strctures with 1, 2 and 4 bytes. Without this attribute the code do not work. This is all i can say. Best regards, Gustavo
0 Kudos